1 | #ifndef RoboClaw_h |
2 | #define RoboClaw_h |
3 |
|
4 | #include <stdarg.h> |
5 |
|
6 | #include <inttypes.h> |
7 | #include <Stream.h> |
8 | #include <HardwareSerial.h> |
9 | #ifdef __AVR__ |
10 | #include <SoftwareSerial.h> |
11 | #endif |
12 |
|
13 | /****************************************************************************** |
14 | * Definitions |
15 | ******************************************************************************/ |
16 |
|
17 | #define _RC_VERSION 10 // software version of this library |
18 | #ifndef GCC_VERSION |
19 | #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) |
20 | #endif |
21 |
|
22 | #define _SS_VERSION 16 |
23 |
|
24 | class RoboClaw : public Stream |
25 | { |
26 | uint16_t crc; |
27 | uint32_t timeout; |
28 | |
29 | HardwareSerial *hserial; |
30 | #ifdef __AVR__ |
31 | SoftwareSerial *sserial; |
32 | #endif |
33 | |
34 | enum {M1FORWARD = 0, |
35 | M1BACKWARD = 1, |
36 | SETMINMB = 2, |
37 | SETMAXMB = 3, |
38 | M2FORWARD = 4, |
39 | M2BACKWARD = 5, |
40 | M17BIT = 6, |
41 | M27BIT = 7, |
42 | MIXEDFORWARD = 8, |
43 | MIXEDBACKWARD = 9, |
44 | MIXEDRIGHT = 10, |
45 | MIXEDLEFT = 11, |
46 | MIXEDFB = 12, |
47 | MIXEDLR = 13, |
48 | GETM1ENC = 16, |
49 | GETM2ENC = 17, |
50 | GETM1SPEED = 18, |
51 | GETM2SPEED = 19, |
52 | RESETENC = 20, |
53 | GETVERSION = 21, |
54 | SETM1ENCCOUNT = 22, |
55 | SETM2ENCCOUNT = 23, |
56 | GETMBATT = 24, |
57 | GETLBATT = 25, |
58 | SETMINLB = 26, |
59 | SETMAXLB = 27, |
60 | SETM1PID = 28, |
61 | SETM2PID = 29, |
62 | GETM1ISPEED = 30, |
63 | GETM2ISPEED = 31, |
64 | M1DUTY = 32, |
65 | M2DUTY = 33, |
66 | MIXEDDUTY = 34, |
67 | M1SPEED = 35, |
68 | M2SPEED = 36, |
69 | MIXEDSPEED = 37, |
70 | M1SPEEDACCEL = 38, |
71 | M2SPEEDACCEL = 39, |
72 | MIXEDSPEEDACCEL = 40, |
73 | M1SPEEDDIST = 41, |
74 | M2SPEEDDIST = 42, |
75 | MIXEDSPEEDDIST = 43, |
76 | M1SPEEDACCELDIST = 44, |
77 | M2SPEEDACCELDIST = 45, |
78 | MIXEDSPEEDACCELDIST = 46, |
79 | GETBUFFERS = 47, |
80 | GETPWMS = 48, |
81 | GETCURRENTS = 49, |
82 | MIXEDSPEED2ACCEL = 50, |
83 | MIXEDSPEED2ACCELDIST = 51, |
84 | M1DUTYACCEL = 52, |
85 | M2DUTYACCEL = 53, |
86 | MIXEDDUTYACCEL = 54, |
87 | READM1PID = 55, |
88 | READM2PID = 56, |
89 | SETMAINVOLTAGES = 57, |
90 | SETLOGICVOLTAGES = 58, |
91 | GETMINMAXMAINVOLTAGES = 59, |
92 | GETMINMAXLOGICVOLTAGES = 60, |
93 | SETM1POSPID = 61, |
94 | SETM2POSPID = 62, |
95 | READM1POSPID = 63, |
96 | READM2POSPID = 64, |
97 | M1SPEEDACCELDECCELPOS = 65, |
98 | M2SPEEDACCELDECCELPOS = 66, |
99 | MIXEDSPEEDACCELDECCELPOS = 67, |
100 | SETM1DEFAULTACCEL = 68, |
101 | SETM2DEFAULTACCEL = 69, |
102 | SETPINFUNCTIONS = 74, |
103 | GETPINFUNCTIONS = 75, |
104 | SETDEADBAND = 76, |
105 | GETDEADBAND = 77, |
106 | GETENCODERS = 78, |
107 | GETISPEEDS = 79, |
108 | RESTOREDEFAULTS = 80, |
109 | GETTEMP = 82, |
110 | GETTEMP2 = 83, //Only valid on some models |
111 | GETERROR = 90, |
112 | GETENCODERMODE = 91, |
113 | SETM1ENCODERMODE = 92, |
114 | SETM2ENCODERMODE = 93, |
115 | WRITENVM = 94, |
116 | READNVM = 95, //Reloads values from Flash into Ram |
117 | SETCONFIG = 98, |
118 | GETCONFIG = 99, |
119 | SETM1MAXCURRENT = 133, |
120 | SETM2MAXCURRENT = 134, |
121 | GETM1MAXCURRENT = 135, |
122 | GETM2MAXCURRENT = 136, |
123 | SETPWMMODE = 148, |
124 | GETPWMMODE = 149, |
125 | FLAGBOOTLOADER = 255}; //Only available via USB communications |
126 | public: |
127 | // public methods |
128 | RoboClaw(HardwareSerial *hserial,uint32_t tout); |
129 | #ifdef __AVR__ |
130 | RoboClaw(SoftwareSerial *sserial,uint32_t tout); |
131 | #endif |
132 | |
133 | ~RoboClaw(); |
134 |
|
135 | bool ForwardM1(uint8_t address, uint8_t speed); |
136 | bool BackwardM1(uint8_t address, uint8_t speed); |
137 | bool SetMinVoltageMainBattery(uint8_t address, uint8_t voltage); |
138 | bool SetMaxVoltageMainBattery(uint8_t address, uint8_t voltage); |
139 | bool ForwardM2(uint8_t address, uint8_t speed); |
140 | bool BackwardM2(uint8_t address, uint8_t speed); |
141 | bool ForwardBackwardM1(uint8_t address, uint8_t speed); |
142 | bool ForwardBackwardM2(uint8_t address, uint8_t speed); |
143 | bool ForwardMixed(uint8_t address, uint8_t speed); |
144 | bool BackwardMixed(uint8_t address, uint8_t speed); |
145 | bool TurnRightMixed(uint8_t address, uint8_t speed); |
146 | bool TurnLeftMixed(uint8_t address, uint8_t speed); |
147 | bool ForwardBackwardMixed(uint8_t address, uint8_t speed); |
148 | bool LeftRightMixed(uint8_t address, uint8_t speed); |
149 | uint32_t ReadEncM1(uint8_t address, uint8_t *status=NULL,bool *valid=NULL); |
150 | uint32_t ReadEncM2(uint8_t address, uint8_t *status=NULL,bool *valid=NULL); |
151 | bool SetEncM1(uint8_t address, int32_t val); |
152 | bool SetEncM2(uint8_t address, int32_t val); |
153 | uint32_t ReadSpeedM1(uint8_t address, uint8_t *status=NULL,bool *valid=NULL); |
154 | uint32_t ReadSpeedM2(uint8_t address, uint8_t *status=NULL,bool *valid=NULL); |
155 | bool ResetEncoders(uint8_t address); |
156 | bool ReadVersion(uint8_t address,char *version); |
157 | uint16_t ReadMainBatteryVoltage(uint8_t address,bool *valid=NULL); |
158 | uint16_t ReadLogicBatteryVoltage(uint8_t address,bool *valid=NULL); |
159 | bool SetMinVoltageLogicBattery(uint8_t address, uint8_t voltage); |
160 | bool SetMaxVoltageLogicBattery(uint8_t address, uint8_t voltage); |
161 | bool SetM1VelocityPID(uint8_t address, float Kp, float Ki, float Kd, uint32_t qpps); |
162 | bool SetM2VelocityPID(uint8_t address, float Kp, float Ki, float Kd, uint32_t qpps); |
163 | uint32_t ReadISpeedM1(uint8_t address,uint8_t *status=NULL,bool *valid=NULL); |
164 | uint32_t ReadISpeedM2(uint8_t address,uint8_t *status=NULL,bool *valid=NULL); |
165 | bool DutyM1(uint8_t address, uint16_t duty); |
166 | bool DutyM2(uint8_t address, uint16_t duty); |
167 | bool DutyM1M2(uint8_t address, uint16_t duty1, uint16_t duty2); |
168 | bool SpeedM1(uint8_t address, uint32_t speed); |
169 | bool SpeedM2(uint8_t address, uint32_t speed); |
170 | bool SpeedM1M2(uint8_t address, uint32_t speed1, uint32_t speed2); |
171 | bool SpeedAccelM1(uint8_t address, uint32_t accel, uint32_t speed); |
172 | bool SpeedAccelM2(uint8_t address, uint32_t accel, uint32_t speed); |
173 | bool SpeedAccelM1M2(uint8_t address, uint32_t accel, uint32_t speed1, uint32_t speed2); |
174 | bool SpeedDistanceM1(uint8_t address, uint32_t speed, uint32_t distance, uint8_t flag=0); |
175 | bool SpeedDistanceM2(uint8_t address, uint32_t speed, uint32_t distance, uint8_t flag=0); |
176 | bool SpeedDistanceM1M2(uint8_t address, uint32_t speed1, uint32_t distance1, uint32_t speed2, uint32_t distance2, uint8_t flag=0); |
177 | bool SpeedAccelDistanceM1(uint8_t address, uint32_t accel, uint32_t speed, uint32_t distance, uint8_t flag=0); |
178 | bool SpeedAccelDistanceM2(uint8_t address, uint32_t accel, uint32_t speed, uint32_t distance, uint8_t flag=0); |
179 | bool SpeedAccelDistanceM1M2(uint8_t address, uint32_t accel, uint32_t speed1, uint32_t distance1, uint32_t speed2, uint32_t distance2, uint8_t flag=0); |
180 | bool ReadBuffers(uint8_t address, uint8_t &depth1, uint8_t &depth2); |
181 | bool ReadPWMs(uint8_t address, int16_t &pwm1, int16_t &pwm2); |
182 | bool ReadCurrents(uint8_t address, int16_t ¤t1, int16_t ¤t2); |
183 | bool SpeedAccelM1M2_2(uint8_t address, uint32_t accel1, uint32_t speed1, uint32_t accel2, uint32_t speed2); |
184 | bool SpeedAccelDistanceM1M2_2(uint8_t address, uint32_t accel1, uint32_t speed1, uint32_t distance1, uint32_t accel2, uint32_t speed2, uint32_t distance2, uint8_t flag=0); |
185 | bool DutyAccelM1(uint8_t address, uint16_t duty, uint32_t accel); |
186 | bool DutyAccelM2(uint8_t address, uint16_t duty, uint32_t accel); |
187 | bool DutyAccelM1M2(uint8_t address, uint16_t duty1, uint32_t accel1, uint16_t duty2, uint32_t accel2); |
188 | bool ReadM1VelocityPID(uint8_t address,float &Kp_fp,float &Ki_fp,float &Kd_fp,uint32_t &qpps); |
189 | bool ReadM2VelocityPID(uint8_t address,float &Kp_fp,float &Ki_fp,float &Kd_fp,uint32_t &qpps); |
190 | bool SetMainVoltages(uint8_t address,uint16_t min,uint16_t max); |
191 | bool SetLogicVoltages(uint8_t address,uint16_t min,uint16_t max); |
192 | bool ReadMinMaxMainVoltages(uint8_t address,uint16_t &min,uint16_t &max); |
193 | bool ReadMinMaxLogicVoltages(uint8_t address,uint16_t &min,uint16_t &max); |
194 | bool SetM1PositionPID(uint8_t address,float kp,float ki,float kd,uint32_t kiMax,uint32_t deadzone,uint32_t min,uint32_t max); |
195 | bool SetM2PositionPID(uint8_t address,float kp,float ki,float kd,uint32_t kiMax,uint32_t deadzone,uint32_t min,uint32_t max); |
196 | bool ReadM1PositionPID(uint8_t address,float &Kp,float &Ki,float &Kd,uint32_t &KiMax,uint32_t &DeadZone,uint32_t &Min,uint32_t &Max); |
197 | bool ReadM2PositionPID(uint8_t address,float &Kp,float &Ki,float &Kd,uint32_t &KiMax,uint32_t &DeadZone,uint32_t &Min,uint32_t &Max); |
198 | bool SpeedAccelDeccelPositionM1(uint8_t address,uint32_t accel,uint32_t speed,uint32_t deccel,uint32_t position,uint8_t flag); |
199 | bool SpeedAccelDeccelPositionM2(uint8_t address,uint32_t accel,uint32_t speed,uint32_t deccel,uint32_t position,uint8_t flag); |
200 | bool SpeedAccelDeccelPositionM1M2(uint8_t address,uint32_t accel1,uint32_t speed1,uint32_t deccel1,uint32_t position1,uint32_t accel2,uint32_t speed2,uint32_t deccel2,uint32_t position2,uint8_t flag); |
201 | bool SetM1DefaultAccel(uint8_t address, uint32_t accel); |
202 | bool SetM2DefaultAccel(uint8_t address, uint32_t accel); |
203 | bool SetPinFunctions(uint8_t address, uint8_t S3mode, uint8_t S4mode, uint8_t S5mode); |
204 | bool GetPinFunctions(uint8_t address, uint8_t &S3mode, uint8_t &S4mode, uint8_t &S5mode); |
205 | bool SetDeadBand(uint8_t address, uint8_t Min, uint8_t Max); |
206 | bool GetDeadBand(uint8_t address, uint8_t &Min, uint8_t &Max); |
207 | bool ReadEncoders(uint8_t address,uint32_t &enc1,uint32_t &enc2); |
208 | bool ReadISpeeds(uint8_t address,uint32_t &ispeed1,uint32_t &ispeed2); |
209 | bool RestoreDefaults(uint8_t address); |
210 | bool ReadTemp(uint8_t address, uint16_t &temp); |
211 | bool ReadTemp2(uint8_t address, uint16_t &temp); |
212 | uint16_t ReadError(uint8_t address,bool *valid=NULL); |
213 | bool ReadEncoderModes(uint8_t address, uint8_t &M1mode, uint8_t &M2mode); |
214 | bool SetM1EncoderMode(uint8_t address,uint8_t mode); |
215 | bool SetM2EncoderMode(uint8_t address,uint8_t mode); |
216 | bool WriteNVM(uint8_t address); |
217 | bool ReadNVM(uint8_t address); |
218 | bool SetConfig(uint8_t address, uint16_t config); |
219 | bool GetConfig(uint8_t address, uint16_t &config); |
220 | bool SetM1MaxCurrent(uint8_t address,uint32_t max); |
221 | bool SetM2MaxCurrent(uint8_t address,uint32_t max); |
222 | bool ReadM1MaxCurrent(uint8_t address,uint32_t &max); |
223 | bool ReadM2MaxCurrent(uint8_t address,uint32_t &max); |
224 | bool SetPWMMode(uint8_t address, uint8_t mode); |
225 | bool GetPWMMode(uint8_t address, uint8_t &mode); |
226 | |
227 | static int16_t library_version() { return _SS_VERSION; } |
228 |
|
229 | virtual int available(); |
230 | void begin(long speed); |
231 | bool isListening(); |
232 | bool overflow(); |
233 | int peek(); |
234 | virtual int read(); |
235 | int read(uint32_t timeout); |
236 | bool listen(); |
237 | virtual size_t write(uint8_t byte); |
238 | virtual void flush(); |
239 | void clear(); |
240 |
|
241 | private: |
242 | void crc_clear(); |
243 | void crc_update (uint8_t data); |
244 | uint16_t crc_get(); |
245 | bool write_n(uint8_t byte,...); |
246 | bool read_n(uint8_t byte,uint8_t address,uint8_t cmd,...); |
247 | uint32_t Read4_1(uint8_t address,uint8_t cmd,uint8_t *status,bool *valid); |
248 | uint32_t Read4(uint8_t address,uint8_t cmd,bool *valid); |
249 | uint16_t Read2(uint8_t address,uint8_t cmd,bool *valid); |
250 | uint8_t Read1(uint8_t address,uint8_t cmd,bool *valid); |
251 | |
252 | }; |
253 |
|
254 | #endif |
255 |
|