rad 0.2.2 → 0.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +34 -0
- data/Manifest.txt +113 -7
- data/{README.txt → README.rdoc} +17 -5
- data/Rakefile +3 -0
- data/bin/rad +106 -1
- data/lib/examples/add_hysteresis.rb +13 -0
- data/lib/examples/basic_blink.rb +10 -0
- data/lib/examples/blink_m_address_assignment.rb +104 -0
- data/lib/examples/blink_m_hello.rb +14 -0
- data/lib/examples/blink_m_multi.rb +61 -0
- data/lib/examples/blink_with_serial.rb +16 -0
- data/lib/examples/configure_pa_lcd_boot.rb +91 -0
- data/lib/examples/debounce_methods.rb +49 -0
- data/lib/examples/external_variable_fu.rb +26 -0
- data/lib/examples/external_variables.rb +32 -0
- data/lib/examples/first_sound.rb +23 -0
- data/lib/examples/frequency_generator.rb +30 -0
- data/lib/examples/hello_array.rb +48 -0
- data/lib/examples/hello_array2.rb +79 -0
- data/lib/examples/hello_array_eeprom.rb +59 -0
- data/lib/examples/hello_clock.rb +84 -0
- data/lib/examples/hello_eeprom.rb +51 -0
- data/lib/examples/hello_eeprom_lcdpa.rb +81 -0
- data/lib/examples/hello_format_print.rb +94 -0
- data/lib/examples/hello_lcd_charset.rb +75 -0
- data/lib/examples/hello_pa_lcd.rb +59 -0
- data/lib/examples/hello_servos.rb +88 -0
- data/lib/examples/hello_spectra_sound.rb +38 -0
- data/lib/examples/hello_world.rb +11 -0
- data/lib/examples/hello_xbee.rb +12 -0
- data/lib/examples/hysteresis_duel.rb +39 -0
- data/lib/examples/i2c_with_clock_chip.rb +124 -0
- data/lib/examples/midi_beat_box.rb +86 -0
- data/lib/examples/midi_scales.rb +94 -0
- data/lib/examples/motor_knob.rb +30 -0
- data/lib/examples/servo_buttons.rb +23 -0
- data/lib/examples/servo_calibrate_continuous.rb +92 -0
- data/lib/examples/servo_throttle.rb +40 -0
- data/lib/examples/sparkfun_lcd.rb +48 -0
- data/lib/examples/spectra_soft_pot.rb +34 -0
- data/lib/examples/times_method.rb +8 -0
- data/lib/examples/toggle.rb +10 -0
- data/lib/examples/twitter.rb +57 -0
- data/lib/examples/two_wire.rb +14 -0
- data/lib/libraries/AFSoftSerial/AFSoftSerial.cpp +321 -0
- data/lib/libraries/AFSoftSerial/AFSoftSerial.h +61 -0
- data/lib/libraries/AFSoftSerial/keywords.txt +18 -0
- data/lib/libraries/AF_XPort/AF_XPort.cpp +166 -0
- data/lib/libraries/AF_XPort/AF_XPort.h +48 -0
- data/lib/libraries/DS1307/DS1307.cpp +162 -0
- data/lib/libraries/DS1307/DS1307.h +66 -0
- data/lib/libraries/{SWSerLCDpa → DS1307}/keywords.txt +1 -1
- data/lib/libraries/FrequencyTimer2/FrequencyTimer2.cpp +144 -0
- data/lib/libraries/FrequencyTimer2/FrequencyTimer2.h +42 -0
- data/lib/libraries/FrequencyTimer2/keywords.txt +22 -0
- data/lib/libraries/I2CEEPROM/I2CEEPROM.cpp +120 -0
- data/lib/libraries/I2CEEPROM/I2CEEPROM.h +70 -0
- data/lib/libraries/I2CEEPROM/keywords.txt +21 -0
- data/lib/libraries/LoopTimer/LoopTimer.cpp +35 -0
- data/lib/libraries/LoopTimer/LoopTimer.h +34 -0
- data/lib/libraries/LoopTimer/keywords.txt +27 -0
- data/lib/libraries/OneWire/OneWire.cpp +194 -0
- data/lib/libraries/OneWire/OneWire.h +63 -0
- data/lib/libraries/OneWire/keywords.txt +35 -0
- data/lib/libraries/OneWire/readme.txt +13 -0
- data/lib/libraries/SWSerLCDpa/SWSerLCDpa.cpp +93 -47
- data/lib/libraries/SWSerLCDpa/SWSerLCDpa.h +16 -9
- data/lib/libraries/SWSerLCDsf/SWSerLCDsf.cpp +311 -0
- data/lib/libraries/SWSerLCDsf/SWSerLCDsf.h +67 -0
- data/lib/libraries/Servo/Servo.cpp +192 -0
- data/lib/libraries/Servo/Servo.h +61 -0
- data/lib/libraries/Stepper/Stepper.cpp +220 -0
- data/lib/libraries/Stepper/Stepper.h +86 -0
- data/lib/libraries/Stepper/keywords.txt +28 -0
- data/lib/libraries/Wire/Wire.cpp +262 -0
- data/lib/libraries/Wire/Wire.h +67 -0
- data/lib/libraries/Wire/keywords.txt +31 -0
- data/lib/libraries/Wire/twi.h +57 -0
- data/lib/libraries/Wire/utility/twi.c +449 -0
- data/lib/libraries/Wire/utility/twi.h +57 -0
- data/lib/plugins/bitwise_ops.rb +54 -0
- data/lib/plugins/blink.rb +25 -0
- data/lib/plugins/blink_m.rb +356 -0
- data/lib/plugins/debounce.rb +138 -0
- data/lib/plugins/debug_output_to_lcd.rb +71 -0
- data/lib/plugins/hysteresis.rb +52 -0
- data/lib/plugins/input_output_state.rb +84 -0
- data/lib/plugins/lcd_padding.rb +58 -0
- data/lib/plugins/mem_test.rb +37 -0
- data/lib/plugins/midi.rb +60 -0
- data/lib/plugins/parallax_ping.rb +50 -0
- data/lib/plugins/servo_pulse.rb +31 -0
- data/lib/plugins/servo_setup.rb +86 -0
- data/lib/plugins/smoother.rb +54 -0
- data/lib/plugins/spark_fun_serial_lcd.rb +100 -0
- data/lib/plugins/spectra_symbol.rb +79 -0
- data/lib/plugins/twitter_connect.rb +145 -0
- data/lib/rad/README.rdoc +5 -0
- data/lib/rad/arduino_plugin.rb +246 -0
- data/lib/rad/arduino_sketch.rb +351 -257
- data/lib/rad/generators/makefile/makefile.erb +1 -1
- data/lib/rad/generators/makefile/makefile.rb +9 -10
- data/lib/rad/hardware_library.rb +813 -0
- data/lib/rad/init.rb +3 -1
- data/lib/rad/rad_processor.rb +128 -0
- data/lib/rad/rad_rewriter.rb +94 -0
- data/lib/rad/rad_type_checker.rb +26 -0
- data/lib/rad/sim/arduino_sketch.rb +57 -0
- data/lib/rad/sketch_compiler.rb +47 -0
- data/lib/rad/tasks/build_and_make.rake +146 -24
- data/lib/rad/variable_processing.rb +153 -0
- data/lib/rad/version.rb +1 -1
- data/spec/examples/hello_world.rb +11 -0
- data/spec/examples/serial_motor.rb +12 -0
- data/spec/models/sketch_compiler_spec.rb +96 -0
- data/spec/sim/hello_world_spec.rb +42 -0
- data/test/test_array_processing.rb +179 -0
- data/test/test_plugin_loading.rb +151 -0
- data/test/test_translation_post_processing.rb +185 -0
- data/test/test_variable_processing.rb +238 -0
- data/website/index.html +22 -7
- data/website/stylesheets/screen.css +32 -1
- metadata +130 -13
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/*
|
|
2
|
+
SWSerLCDsf.h - Software serial to SparkFun controller chip based
|
|
3
|
+
LCD display library Adapted from SoftwareSerial.cpp (c) 2006 David A. Mellis
|
|
4
|
+
by Brian B. Riley, Underhill Center, Vermont, USA, July 2008
|
|
5
|
+
|
|
6
|
+
This library is free software; you can redistribute it and/or
|
|
7
|
+
modify it under the terms of the GNU Lesser General Public
|
|
8
|
+
License as published by the Free Software Foundation; either
|
|
9
|
+
version 2.1 of the License, or (at your option) any later version.
|
|
10
|
+
|
|
11
|
+
This library is distributed in the hope that it will be useful,
|
|
12
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14
|
+
Lesser General Public License for more details.
|
|
15
|
+
|
|
16
|
+
You should have received a copy of the GNU Lesser General Public
|
|
17
|
+
License along with this library; if not, write to the Free Software
|
|
18
|
+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
#ifndef SWSerLCDsf_h
|
|
22
|
+
#define SWSerLCDsf_h
|
|
23
|
+
|
|
24
|
+
#include <inttypes.h>
|
|
25
|
+
|
|
26
|
+
class SWSerLCDsf
|
|
27
|
+
{
|
|
28
|
+
private:
|
|
29
|
+
uint8_t _transmitPin;
|
|
30
|
+
long _baudRate;
|
|
31
|
+
int _bitPeriod;
|
|
32
|
+
byte _rows;
|
|
33
|
+
byte _cols;
|
|
34
|
+
int _geometry;
|
|
35
|
+
void printNumber(unsigned long, uint8_t);
|
|
36
|
+
|
|
37
|
+
public:
|
|
38
|
+
SWSerLCDsf(uint8_t);
|
|
39
|
+
SWSerLCDsf(uint8_t, int);
|
|
40
|
+
void begin(long);
|
|
41
|
+
void print(char);
|
|
42
|
+
void print(const char[]);
|
|
43
|
+
void print(uint8_t);
|
|
44
|
+
void print(int);
|
|
45
|
+
void print(unsigned int);
|
|
46
|
+
void print(long);
|
|
47
|
+
void print(unsigned long);
|
|
48
|
+
void print(long, int);
|
|
49
|
+
void clearscr(void);
|
|
50
|
+
void clearscr(const char[]);
|
|
51
|
+
void clearscr(int);
|
|
52
|
+
void clearscr(long, int);
|
|
53
|
+
void home(void);
|
|
54
|
+
void home(const char[]);
|
|
55
|
+
void home(int);
|
|
56
|
+
void home(long, int);
|
|
57
|
+
void setxy(byte, byte);
|
|
58
|
+
void setxy(byte, byte, const char[]);
|
|
59
|
+
void setxy(byte, byte, int);
|
|
60
|
+
void setxy(byte, byte, long, int);
|
|
61
|
+
void setgeo(int);
|
|
62
|
+
void setintensity(int);
|
|
63
|
+
void setcmd(byte, byte);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
#endif
|
|
67
|
+
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Servo.h - Arduino Software Servo Library
|
|
3
|
+
Author: ????
|
|
4
|
+
Modified: Brian Riley <brianbr@wulfden.org> Jun/Jul 2008
|
|
5
|
+
Copyright (c) 2007 David A. Mellis. All right reserved.
|
|
6
|
+
|
|
7
|
+
This library is free software; you can redistribute it and/or
|
|
8
|
+
modify it under the terms of the GNU Lesser General Public
|
|
9
|
+
License as published by the Free Software Foundation; either
|
|
10
|
+
version 2.1 of the License, or (at your option) any later version.
|
|
11
|
+
|
|
12
|
+
This library is distributed in the hope that it will be useful,
|
|
13
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
15
|
+
Lesser General Public License for more details.
|
|
16
|
+
|
|
17
|
+
You should have received a copy of the GNU Lesser General Public
|
|
18
|
+
License along with this library; if not, write to the Free Software
|
|
19
|
+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
#include "WConstants.h"
|
|
23
|
+
#include <Servo.h>
|
|
24
|
+
|
|
25
|
+
Servo *Servo::first;
|
|
26
|
+
|
|
27
|
+
#define NO_ANGLE (0xff)
|
|
28
|
+
|
|
29
|
+
Servo::Servo() : pin(0),angle(NO_ANGLE),pulse0(0),min16(DEFLT_MINP),max16(DEFLT_MAXP),next(0)
|
|
30
|
+
{}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
uint8_t Servo::attach(int pinArg)
|
|
34
|
+
{
|
|
35
|
+
pin = pinArg;
|
|
36
|
+
angle = NO_ANGLE;
|
|
37
|
+
pulse0 = 0;
|
|
38
|
+
next = first;
|
|
39
|
+
first = this;
|
|
40
|
+
digitalWrite(pin,0);
|
|
41
|
+
pinMode(pin,OUTPUT);
|
|
42
|
+
return 1;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
uint8_t Servo::attach(int pinArg, int angleArg)
|
|
46
|
+
{
|
|
47
|
+
attach(pinArg);
|
|
48
|
+
write(angleArg);
|
|
49
|
+
return 1;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
uint8_t Servo::attach(int pinArg, uint16_t minp, uint16_t maxp)
|
|
54
|
+
{
|
|
55
|
+
attach(pinArg);
|
|
56
|
+
min16 = minp/16;
|
|
57
|
+
max16 = maxp/16;
|
|
58
|
+
return 1;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
uint8_t Servo::attach(int pinArg, int angleArg, uint16_t minp, uint16_t maxp)
|
|
62
|
+
{
|
|
63
|
+
attach(pinArg);
|
|
64
|
+
min16 = minp/16;
|
|
65
|
+
max16 = maxp/16;
|
|
66
|
+
write(angleArg);
|
|
67
|
+
return 1;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
void Servo::detach()
|
|
71
|
+
{
|
|
72
|
+
for ( Servo **p = &first; *p != 0; p = &((*p)->next) ) {
|
|
73
|
+
if ( *p == this) {
|
|
74
|
+
*p = this->next;
|
|
75
|
+
this->next = 0;
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
void Servo::write(int angleArg)
|
|
82
|
+
{
|
|
83
|
+
if ( angleArg < 0) angleArg = 0;
|
|
84
|
+
if ( angleArg > 180) angleArg = 180;
|
|
85
|
+
angle = angleArg;
|
|
86
|
+
// bleh, have to use longs to prevent overflow, could be tricky if always a 16MHz clock, but not true
|
|
87
|
+
// That 64L on the end is the TCNT0 prescaler, it will need to change if the clock's prescaler changes,
|
|
88
|
+
// but then there will likely be an overflow problem, so it will have to be handled by a human.
|
|
89
|
+
pulse0 = (min16*16L*clockCyclesPerMicrosecond() + (max16-min16)*(16L*clockCyclesPerMicrosecond())*angle/180L)/64L;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
void Servo::position(int angleArg)
|
|
93
|
+
{
|
|
94
|
+
write(angleArg);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
void Servo::speed(int speedVal)
|
|
98
|
+
{
|
|
99
|
+
speedVal += 90;
|
|
100
|
+
write(speedVal);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
uint8_t Servo::read()
|
|
104
|
+
{
|
|
105
|
+
return angle;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
uint8_t Servo::attached()
|
|
109
|
+
{
|
|
110
|
+
for ( Servo *p = first; p != 0; p = p->next ) {
|
|
111
|
+
if ( p == this) return 1;
|
|
112
|
+
}
|
|
113
|
+
return 0;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/*************************************************
|
|
117
|
+
* refresh() - the heart of the method
|
|
118
|
+
*************************************************/
|
|
119
|
+
|
|
120
|
+
void Servo::refresh()
|
|
121
|
+
{
|
|
122
|
+
uint8_t count = 0, i = 0;
|
|
123
|
+
uint16_t base = 0;
|
|
124
|
+
Servo *p;
|
|
125
|
+
static unsigned long lastRefresh = 0;
|
|
126
|
+
unsigned long m = millis();
|
|
127
|
+
|
|
128
|
+
// if we haven't wrapped millis, and 20ms have not passed,
|
|
129
|
+
// then don't do anything
|
|
130
|
+
|
|
131
|
+
if ( m >= lastRefresh && m < lastRefresh + 20) return;
|
|
132
|
+
lastRefresh = m;
|
|
133
|
+
|
|
134
|
+
for ( p = first; p != 0; p = p->next ) if ( p->pulse0) count++;
|
|
135
|
+
|
|
136
|
+
if ( count == 0) return;
|
|
137
|
+
|
|
138
|
+
// gather all the servos in an array
|
|
139
|
+
|
|
140
|
+
Servo *s[count];
|
|
141
|
+
|
|
142
|
+
for ( p = first; p != 0; p = p->next ) if ( p->pulse0) s[i++] = p;
|
|
143
|
+
|
|
144
|
+
// bubblesort the servos by pulse time, ascending order
|
|
145
|
+
|
|
146
|
+
for(;;) {
|
|
147
|
+
uint8_t moved = 0;
|
|
148
|
+
for ( i = 1; i < count; i++) {
|
|
149
|
+
if ( s[i]->pulse0 < s[i-1]->pulse0) {
|
|
150
|
+
Servo *t = s[i];
|
|
151
|
+
s[i] = s[i-1];
|
|
152
|
+
s[i-1] = t;
|
|
153
|
+
moved = 1;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if ( !moved) break;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/*********************************************************************
|
|
160
|
+
* Turn on all the pins
|
|
161
|
+
* Note the timing error here... when you have many servos going, the
|
|
162
|
+
* ones at the front will get a pulse that is a few microseconds too long.
|
|
163
|
+
* Figure about 4uS/servo after them. This could be compensated, but I feel
|
|
164
|
+
* it is within the margin of error of software servos that could catch
|
|
165
|
+
* an extra interrupt handler at any time.
|
|
166
|
+
********************************************************************/
|
|
167
|
+
|
|
168
|
+
for ( i = 0; i < count; i++) digitalWrite( s[i]->pin, 1);
|
|
169
|
+
|
|
170
|
+
uint8_t start = TCNT0;
|
|
171
|
+
uint8_t now = start;
|
|
172
|
+
uint8_t last = now;
|
|
173
|
+
|
|
174
|
+
// Now wait for each pin's time in turn..
|
|
175
|
+
|
|
176
|
+
for ( i = 0; i < count; i++) {
|
|
177
|
+
uint16_t go = start + s[i]->pulse0;
|
|
178
|
+
|
|
179
|
+
// loop until we reach or pass 'go' time
|
|
180
|
+
|
|
181
|
+
for (;;) {
|
|
182
|
+
now = TCNT0;
|
|
183
|
+
if ( now < last) base += 256;
|
|
184
|
+
last = now;
|
|
185
|
+
|
|
186
|
+
if ( base+now > go) {
|
|
187
|
+
digitalWrite( s[i]->pin,0);
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Servo.h - Arduino Software Servo Library
|
|
3
|
+
Author: ????
|
|
4
|
+
Modified: Brian Riley <brianbr@wulfden.org> Jun/Jul 2008
|
|
5
|
+
Copyright (c) 2007 David A. Mellis. All right reserved.
|
|
6
|
+
|
|
7
|
+
This library is free software; you can redistribute it and/or
|
|
8
|
+
modify it under the terms of the GNU Lesser General Public
|
|
9
|
+
License as published by the Free Software Foundation; either
|
|
10
|
+
version 2.1 of the License, or (at your option) any later version.
|
|
11
|
+
|
|
12
|
+
This library is distributed in the hope that it will be useful,
|
|
13
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
15
|
+
Lesser General Public License for more details.
|
|
16
|
+
|
|
17
|
+
You should have received a copy of the GNU Lesser General Public
|
|
18
|
+
License along with this library; if not, write to the Free Software
|
|
19
|
+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
20
|
+
*/
|
|
21
|
+
#ifndef SERVO_IS_IN
|
|
22
|
+
#define SERVO_IS_IN
|
|
23
|
+
|
|
24
|
+
#include <inttypes.h>
|
|
25
|
+
#include <wiring.h>
|
|
26
|
+
|
|
27
|
+
#define DEFLT_MINP 34
|
|
28
|
+
#define DEFLT_MAXP 150
|
|
29
|
+
|
|
30
|
+
class Servo
|
|
31
|
+
{
|
|
32
|
+
private:
|
|
33
|
+
uint8_t pin;
|
|
34
|
+
uint8_t angle; // in degrees
|
|
35
|
+
uint16_t pulse0; // pulse width in TCNT0 counts
|
|
36
|
+
uint8_t min16; // minimum pulse, 16uS units (default is DEFLT_MINP)
|
|
37
|
+
uint8_t max16; // maximum pulse, 16uS units, 0-4ms range (default is DEFLT_MAXP)
|
|
38
|
+
class Servo *next;
|
|
39
|
+
static Servo* first;
|
|
40
|
+
void write(int); // specify the angle in degrees, 0 to 180
|
|
41
|
+
public:
|
|
42
|
+
Servo();
|
|
43
|
+
uint8_t attach(int); // attach to a pin, sets pinMode, returns 0 on failure, won't
|
|
44
|
+
// position the servo until a subsequent write() happens
|
|
45
|
+
uint8_t attach(int,int);
|
|
46
|
+
// same, except position is also is specified
|
|
47
|
+
uint8_t attach(int,uint16_t,uint16_t);
|
|
48
|
+
// same, except min/max pulse is specified
|
|
49
|
+
uint8_t attach(int,int,uint16_t,uint16_t);
|
|
50
|
+
// same, except position and min/max pulse is specified
|
|
51
|
+
void detach();
|
|
52
|
+
void position(int); // enter an angle from 0 to 180
|
|
53
|
+
void speed(int); // enter a speed from -90 to +90
|
|
54
|
+
uint8_t read();
|
|
55
|
+
uint8_t attached();
|
|
56
|
+
static void refresh(); // must be called at least every 50ms or so to keep servo alive
|
|
57
|
+
// you can call more often, it won't happen more than once
|
|
58
|
+
// every 20ms
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
#endif
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Stepper.cpp - - Stepper library for Wiring/Arduino - Version 0.4
|
|
3
|
+
|
|
4
|
+
Original library (0.1) by Tom Igoe.
|
|
5
|
+
Two-wire modifications (0.2) by Sebastian Gassner
|
|
6
|
+
Combination version (0.3) by Tom Igoe and David Mellis
|
|
7
|
+
Bug fix for four-wire (0.4) by Tom Igoe, bug fix from Noah Shibley
|
|
8
|
+
|
|
9
|
+
Drives a unipolar or bipolar stepper motor using 2 wires or 4 wires
|
|
10
|
+
|
|
11
|
+
When wiring multiple stepper motors to a microcontroller,
|
|
12
|
+
you quickly run out of output pins, with each motor requiring 4 connections.
|
|
13
|
+
|
|
14
|
+
By making use of the fact that at any time two of the four motor
|
|
15
|
+
coils are the inverse of the other two, the number of
|
|
16
|
+
control connections can be reduced from 4 to 2.
|
|
17
|
+
|
|
18
|
+
A slightly modified circuit around a Darlington transistor array or an L293 H-bridge
|
|
19
|
+
connects to only 2 microcontroler pins, inverts the signals received,
|
|
20
|
+
and delivers the 4 (2 plus 2 inverted ones) output signals required
|
|
21
|
+
for driving a stepper motor.
|
|
22
|
+
|
|
23
|
+
The sequence of control signals for 4 control wires is as follows:
|
|
24
|
+
|
|
25
|
+
Step C0 C1 C2 C3
|
|
26
|
+
1 1 0 1 0
|
|
27
|
+
2 0 1 1 0
|
|
28
|
+
3 0 1 0 1
|
|
29
|
+
4 1 0 0 1
|
|
30
|
+
|
|
31
|
+
The sequence of controls signals for 2 control wires is as follows
|
|
32
|
+
(columns C1 and C2 from above):
|
|
33
|
+
|
|
34
|
+
Step C0 C1
|
|
35
|
+
1 0 1
|
|
36
|
+
2 1 1
|
|
37
|
+
3 1 0
|
|
38
|
+
4 0 0
|
|
39
|
+
|
|
40
|
+
The circuits can be found at
|
|
41
|
+
|
|
42
|
+
http://www.arduino.cc/en/Tutorial/Stepper
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
#include "WProgram.h"
|
|
49
|
+
#include "Stepper.h"
|
|
50
|
+
|
|
51
|
+
/*
|
|
52
|
+
* two-wire constructor.
|
|
53
|
+
* Sets which wires should control the motor.
|
|
54
|
+
*/
|
|
55
|
+
Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2)
|
|
56
|
+
{
|
|
57
|
+
this->step_number = 0; // which step the motor is on
|
|
58
|
+
this->speed = 0; // the motor speed, in revolutions per minute
|
|
59
|
+
this->direction = 0; // motor direction
|
|
60
|
+
this->last_step_time = 0; // time stamp in ms of the last step taken
|
|
61
|
+
this->number_of_steps = number_of_steps; // total number of steps for this motor
|
|
62
|
+
|
|
63
|
+
// Arduino pins for the motor control connection:
|
|
64
|
+
this->motor_pin_1 = motor_pin_1;
|
|
65
|
+
this->motor_pin_2 = motor_pin_2;
|
|
66
|
+
|
|
67
|
+
// setup the pins on the microcontroller:
|
|
68
|
+
pinMode(this->motor_pin_1, OUTPUT);
|
|
69
|
+
pinMode(this->motor_pin_2, OUTPUT);
|
|
70
|
+
|
|
71
|
+
// When there are only 2 pins, set the other two to 0:
|
|
72
|
+
this->motor_pin_3 = 0;
|
|
73
|
+
this->motor_pin_4 = 0;
|
|
74
|
+
|
|
75
|
+
// pin_count is used by the stepMotor() method:
|
|
76
|
+
this->pin_count = 2;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
/*
|
|
81
|
+
* constructor for four-pin version
|
|
82
|
+
* Sets which wires should control the motor.
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4)
|
|
86
|
+
{
|
|
87
|
+
this->step_number = 0; // which step the motor is on
|
|
88
|
+
this->speed = 0; // the motor speed, in revolutions per minute
|
|
89
|
+
this->direction = 0; // motor direction
|
|
90
|
+
this->last_step_time = 0; // time stamp in ms of the last step taken
|
|
91
|
+
this->number_of_steps = number_of_steps; // total number of steps for this motor
|
|
92
|
+
|
|
93
|
+
// Arduino pins for the motor control connection:
|
|
94
|
+
this->motor_pin_1 = motor_pin_1;
|
|
95
|
+
this->motor_pin_2 = motor_pin_2;
|
|
96
|
+
this->motor_pin_3 = motor_pin_3;
|
|
97
|
+
this->motor_pin_4 = motor_pin_4;
|
|
98
|
+
|
|
99
|
+
// setup the pins on the microcontroller:
|
|
100
|
+
pinMode(this->motor_pin_1, OUTPUT);
|
|
101
|
+
pinMode(this->motor_pin_2, OUTPUT);
|
|
102
|
+
pinMode(this->motor_pin_3, OUTPUT);
|
|
103
|
+
pinMode(this->motor_pin_4, OUTPUT);
|
|
104
|
+
|
|
105
|
+
// pin_count is used by the stepMotor() method:
|
|
106
|
+
this->pin_count = 4;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/*
|
|
110
|
+
Sets the speed in revs per minute
|
|
111
|
+
|
|
112
|
+
*/
|
|
113
|
+
void Stepper::set_speed(long whatSpeed)
|
|
114
|
+
{
|
|
115
|
+
this->step_delay = 60L * 1000L / this->number_of_steps / whatSpeed;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/*
|
|
119
|
+
Moves the motor steps_to_move steps. If the number is negative,
|
|
120
|
+
the motor moves in the reverse direction.
|
|
121
|
+
*/
|
|
122
|
+
void Stepper::set_steps(int steps_to_move)
|
|
123
|
+
{
|
|
124
|
+
int steps_left = abs(steps_to_move); // how many steps to take
|
|
125
|
+
|
|
126
|
+
// determine direction based on whether steps_to_mode is + or -:
|
|
127
|
+
if (steps_to_move > 0) {this->direction = 1;}
|
|
128
|
+
if (steps_to_move < 0) {this->direction = 0;}
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
// decrement the number of steps, moving one step each time:
|
|
132
|
+
while(steps_left > 0) {
|
|
133
|
+
// move only if the appropriate delay has passed:
|
|
134
|
+
if (millis() - this->last_step_time >= this->step_delay) {
|
|
135
|
+
// step the motor to step number 0, 1, 2, or 3:
|
|
136
|
+
stepMotor(this->step_number % 4);
|
|
137
|
+
// get the timeStamp of when you stepped:
|
|
138
|
+
this->last_step_time = millis();
|
|
139
|
+
// increment or decrement the step number,
|
|
140
|
+
// depending on direction:
|
|
141
|
+
if (this->direction == 1) {
|
|
142
|
+
this->step_number++;
|
|
143
|
+
if (this->step_number == this->number_of_steps) {
|
|
144
|
+
this->step_number = 0;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
if (this->step_number == 0) {
|
|
149
|
+
this->step_number = this->number_of_steps;
|
|
150
|
+
}
|
|
151
|
+
this->step_number--;
|
|
152
|
+
}
|
|
153
|
+
// decrement the steps left:
|
|
154
|
+
steps_left--;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/*
|
|
160
|
+
* Moves the motor forward or backwards.
|
|
161
|
+
*/
|
|
162
|
+
void Stepper::stepMotor(int thisStep)
|
|
163
|
+
{
|
|
164
|
+
if (this->pin_count == 2) {
|
|
165
|
+
switch (thisStep) {
|
|
166
|
+
case 0: /* 01 */
|
|
167
|
+
digitalWrite(motor_pin_1, LOW);
|
|
168
|
+
digitalWrite(motor_pin_2, HIGH);
|
|
169
|
+
break;
|
|
170
|
+
case 1: /* 11 */
|
|
171
|
+
digitalWrite(motor_pin_1, HIGH);
|
|
172
|
+
digitalWrite(motor_pin_2, HIGH);
|
|
173
|
+
break;
|
|
174
|
+
case 2: /* 10 */
|
|
175
|
+
digitalWrite(motor_pin_1, HIGH);
|
|
176
|
+
digitalWrite(motor_pin_2, LOW);
|
|
177
|
+
break;
|
|
178
|
+
case 3: /* 00 */
|
|
179
|
+
digitalWrite(motor_pin_1, LOW);
|
|
180
|
+
digitalWrite(motor_pin_2, LOW);
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (this->pin_count == 4) {
|
|
185
|
+
switch (thisStep) {
|
|
186
|
+
case 0: // 1010
|
|
187
|
+
digitalWrite(motor_pin_1, HIGH);
|
|
188
|
+
digitalWrite(motor_pin_2, LOW);
|
|
189
|
+
digitalWrite(motor_pin_3, HIGH);
|
|
190
|
+
digitalWrite(motor_pin_4, LOW);
|
|
191
|
+
break;
|
|
192
|
+
case 1: // 0110
|
|
193
|
+
digitalWrite(motor_pin_1, LOW);
|
|
194
|
+
digitalWrite(motor_pin_2, HIGH);
|
|
195
|
+
digitalWrite(motor_pin_3, HIGH);
|
|
196
|
+
digitalWrite(motor_pin_4, LOW);
|
|
197
|
+
break;
|
|
198
|
+
case 2: //0101
|
|
199
|
+
digitalWrite(motor_pin_1, LOW);
|
|
200
|
+
digitalWrite(motor_pin_2, HIGH);
|
|
201
|
+
digitalWrite(motor_pin_3, LOW);
|
|
202
|
+
digitalWrite(motor_pin_4, HIGH);
|
|
203
|
+
break;
|
|
204
|
+
case 3: //1001
|
|
205
|
+
digitalWrite(motor_pin_1, HIGH);
|
|
206
|
+
digitalWrite(motor_pin_2, LOW);
|
|
207
|
+
digitalWrite(motor_pin_3, LOW);
|
|
208
|
+
digitalWrite(motor_pin_4, HIGH);
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/*
|
|
215
|
+
version() returns the version of the library:
|
|
216
|
+
*/
|
|
217
|
+
int Stepper::version(void)
|
|
218
|
+
{
|
|
219
|
+
return 4;
|
|
220
|
+
}
|