-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiffDrive.h
374 lines (318 loc) · 16.5 KB
/
DiffDrive.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/**********************************************************************************************
* Arduino Differential Drive Velocity Control Library - Version 1.0.0
* by Binit shah <[email protected]> binitshah.com
*
* Full duplex control over serial of a differential drive robot through velocity commands,
* returning integrated SE2 odometry for motion updates in your localization filter.
*
* This library is licensed under the MIT License
**********************************************************************************************/
#ifndef DiffDrive_h
#define DiffDrive_h
#include "Arduino.h"
#include <PID_v1.h>
#define MICROSECONDS_PER_SECOND (1e6)
#define MAX_STR_LEN (200)
template<int pwm_left, int dir1_left, int dir2_left, int enca_left, int encb_left,
int pwm_right, int dir1_right, int dir2_right, int enca_right, int encb_right>
class DiffDrive {
public:
PID motor_velctrl_left;
PID motor_velctrl_right;
DiffDrive(double cpr, double R, double D, double L,
double kp_conservative, double ki_conservative, double kd_conservative,
double kp_aggressive, double ki_aggressive, double kd_aggressive);
/*
* Set the rate at which the PID controller runs.
*
* @param sample_time_microsec period between samples in microseconds
*/
void setSampleTime(int sample_time_microsec);
/*
* Set the conservative and aggressive PID constants. The aggressive
* constants are used where the robot can afford to gain speed quicker,
* such as in wide open areas.
*
* @param kp_conservative controller p constant used in tighter areas
* @param ki_conservative controller i constant used in tighter areas
* @param kd_conservative controller d constant used in tighter areas
* @param kp_aggressive controller p constant used in open areas
* @param ki_aggressive controller i constant used in open areas
* @param kd_aggressive controller d constant used in open areas
*/
void setPIDConstants(double kp_conservative, double ki_conservative, double kd_conservative,
double kp_aggressive, double ki_aggressive, double kd_aggressive);
/*
* Set the bounds of the PID's output. This is typically the resolution
* of the microcontroller's PWM. 8-bit PWM leads to 256 steps. Param
* min_pwm is negative max_pwm because that's how we encode wheel
* direction.
*
* @param min_pwm usually equal to negative max_pwm
* @param max_pwm usually equal to PWM's steps
*/
void setOutputLimits(double min_pwm, double max_pwm);
/*
* Set the bounds of the PID's output. This is typically the resolution
* of the microcontroller's PWM. 8-bit PWM leads to 256 steps. Param
* min_pwm is negative max_pwm because that's how we encode wheel
* direction.
*
* @param min_pwm usually equal to negative max_pwm
* @param max_pwm usually equal to PWM's steps
*/
void setRobotParams(double cpr, double R, double D, double L);
/*
* Run this method once in the Arduino setup.
*
* @param baud_rate the baud rate at which serial communicates
*/
void begin(uint32_t baud_rate);
/*
* Run this method once every Arduino main loop.
*/
void loop();
private:
double x, y, theta = 0.0f; // unit: x,y -> meters, theta -> rad/s
double gearbox_cpr; // unit: counts per revolution of geared shaft
double wheel_radius, wheel_base, single_intept_dist; // unit: meters
static volatile int32_t encoder_count_left, encoder_count_right; // unit: counts
static uint8_t enc_val_left, enc_val_right;
static void encoder_isr_left(void);
static void encoder_isr_right(void);
double wtarget_left, wtarget_right, wcurr_left, wcurr_right = 0.0f; // unit: rad/s
double cmd_left, cmd_right = 0; // unit: pwm
double kp_cons, ki_cons, kd_cons, kp_agg, ki_agg, kd_agg;
bool is_conservative = false;
uint32_t sample_time = 10000; // unit: microseconds
uint32_t prev_time_odom, prev_time_pid = 0; // unit: microseconds
int32_t prev_encoder_count_left, prev_encoder_count_right = 0; // unit: counts
String input = "";
const char start_marker = '<';
const char end_marker = '>';
void processAndReply();
};
// -------- Private Methods -------- //
const int8_t encoder_table_left[] = {0,1,-1,0,-1,0,0,1,1,0,0,-1,0,-1,1,0};
const int8_t encoder_table_right[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
template<int pwm_left, int dir1_left, int dir2_left, int enca_left, int encb_left,
int pwm_right, int dir1_right, int dir2_right, int enca_right, int encb_right>
volatile int32_t DiffDrive<pwm_left, dir1_left, dir2_left, enca_left, encb_left,
pwm_right, dir1_right, dir2_right, enca_right, encb_right>::encoder_count_left = 0;
template<int pwm_left, int dir1_left, int dir2_left, int enca_left, int encb_left,
int pwm_right, int dir1_right, int dir2_right, int enca_right, int encb_right>
volatile int32_t DiffDrive<pwm_left, dir1_left, dir2_left, enca_left, encb_left,
pwm_right, dir1_right, dir2_right, enca_right, encb_right>::encoder_count_right = 0;
template<int pwm_left, int dir1_left, int dir2_left, int enca_left, int encb_left,
int pwm_right, int dir1_right, int dir2_right, int enca_right, int encb_right>
uint8_t DiffDrive<pwm_left, dir1_left, dir2_left, enca_left, encb_left,
pwm_right, dir1_right, dir2_right, enca_right, encb_right>::enc_val_left = 0;
template<int pwm_left, int dir1_left, int dir2_left, int enca_left, int encb_left,
int pwm_right, int dir1_right, int dir2_right, int enca_right, int encb_right>
uint8_t DiffDrive<pwm_left, dir1_left, dir2_left, enca_left, encb_left,
pwm_right, dir1_right, dir2_right, enca_right, encb_right>::enc_val_right = 0;
template<int pwm_left, int dir1_left, int dir2_left, int enca_left, int encb_left,
int pwm_right, int dir1_right, int dir2_right, int enca_right, int encb_right>
void DiffDrive<pwm_left, dir1_left, dir2_left, enca_left, encb_left,
pwm_right, dir1_right, dir2_right, enca_right, encb_right>::encoder_isr_left() {
enc_val_left = (enc_val_left << 2) | (digitalRead(enca_left) << 1) | digitalRead(encb_left);
encoder_count_left += encoder_table_left[enc_val_left & 0b1111];
}
template<int pwm_left, int dir1_left, int dir2_left, int enca_left, int encb_left,
int pwm_right, int dir1_right, int dir2_right, int enca_right, int encb_right>
void DiffDrive<pwm_left, dir1_left, dir2_left, enca_left, encb_left,
pwm_right, dir1_right, dir2_right, enca_right, encb_right>::encoder_isr_right() {
enc_val_right = (enc_val_right << 2) | (digitalRead(enca_right) << 1) | digitalRead(encb_right);
encoder_count_right += encoder_table_right[enc_val_right & 0b1111];
}
template<int pwm_left, int dir1_left, int dir2_left, int enca_left, int encb_left,
int pwm_right, int dir1_right, int dir2_right, int enca_right, int encb_right>
void DiffDrive<pwm_left, dir1_left, dir2_left, enca_left, encb_left,
pwm_right, dir1_right, dir2_right, enca_right, encb_right>::processAndReply() {
char input_arr[MAX_STR_LEN];
input.toCharArray(input_arr, MAX_STR_LEN);
char* wcommand_left = strtok(input_arr, ",");
wtarget_left = atof(wcommand_left);
char* wcommand_right = strtok(NULL, ",");
wtarget_right = atof(wcommand_right);
char* cons_command = strtok(NULL, ",");
is_conservative = atoi(cons_command);
Serial.print("<");
Serial.print(x, 6);
Serial.print(",");
Serial.print(y, 6);
Serial.print(",");
Serial.print(theta, 6);
Serial.println(">");
x = 0;
y = 0;
theta = 0;
input = "";
}
// -------- Public Methods -------- //
template<int pwm_left, int dir1_left, int dir2_left, int enca_left, int encb_left,
int pwm_right, int dir1_right, int dir2_right, int enca_right, int encb_right>
DiffDrive<pwm_left, dir1_left, dir2_left, enca_left, encb_left,
pwm_right, dir1_right, dir2_right, enca_right, encb_right>::DiffDrive(
double cpr, double R, double D, double L,
double kp_conservative, double ki_conservative, double kd_conservative,
double kp_aggressive, double ki_aggressive, double kd_aggressive) :
gearbox_cpr(cpr), wheel_radius(R), wheel_base(D), single_intept_dist(L),
kp_cons(kp_conservative), ki_cons(ki_conservative), kd_cons(kd_conservative),
kp_agg(kp_aggressive), ki_agg(ki_aggressive), kd_agg(kd_aggressive),
motor_velctrl_left(&wcurr_left, &cmd_left, &wtarget_left, kp_aggressive, ki_aggressive, kd_aggressive, DIRECT),
motor_velctrl_right(&wcurr_right, &cmd_right, &wtarget_right, kp_aggressive, ki_aggressive, kd_aggressive, DIRECT) {
}
template<int pwm_left, int dir1_left, int dir2_left, int enca_left, int encb_left,
int pwm_right, int dir1_right, int dir2_right, int enca_right, int encb_right>
void DiffDrive<pwm_left, dir1_left, dir2_left, enca_left, encb_left,
pwm_right, dir1_right, dir2_right, enca_right, encb_right>::setSampleTime(int sample_time_microsec) {
sample_time = sample_time_microsec;
motor_velctrl_left.SetSampleTime(sample_time / 1000);
motor_velctrl_right.SetSampleTime(sample_time / 1000);
}
template<int pwm_left, int dir1_left, int dir2_left, int enca_left, int encb_left,
int pwm_right, int dir1_right, int dir2_right, int enca_right, int encb_right>
void DiffDrive<pwm_left, dir1_left, dir2_left, enca_left, encb_left,
pwm_right, dir1_right, dir2_right, enca_right, encb_right>::setPIDConstants(
double kp_conservative, double ki_conservative, double kd_conservative,
double kp_aggressive, double ki_aggressive, double kd_aggressive) {
kp_cons = kp_conservative;
ki_cons = ki_conservative;
kd_cons = kd_conservative;
kp_agg = kp_aggressive;
ki_agg = ki_aggressive;
kd_agg = kd_aggressive;
if (is_conservative) {
motor_velctrl_left.SetTunings(kp_cons, ki_cons, kd_cons, P_ON_M);
motor_velctrl_right.SetTunings(kp_cons, ki_cons, kd_cons, P_ON_M);
} else {
motor_velctrl_left.SetTunings(kp_agg, ki_agg, kd_agg, P_ON_M);
motor_velctrl_right.SetTunings(kp_agg, ki_agg, kd_agg, P_ON_M);
}
}
template<int pwm_left, int dir1_left, int dir2_left, int enca_left, int encb_left,
int pwm_right, int dir1_right, int dir2_right, int enca_right, int encb_right>
void DiffDrive<pwm_left, dir1_left, dir2_left, enca_left, encb_left,
pwm_right, dir1_right, dir2_right, enca_right, encb_right>::setOutputLimits(double min_pwm, double max_pwm) {
motor_velctrl_left.SetOutputLimits(min_pwm, max_pwm);
motor_velctrl_right.SetOutputLimits(min_pwm, max_pwm);
}
template<int pwm_left, int dir1_left, int dir2_left, int enca_left, int encb_left,
int pwm_right, int dir1_right, int dir2_right, int enca_right, int encb_right>
void DiffDrive<pwm_left, dir1_left, dir2_left, enca_left, encb_left,
pwm_right, dir1_right, dir2_right, enca_right, encb_right>::setRobotParams(
double cpr, double R, double D, double L) {
gearbox_cpr = cpr;
wheel_radius = R;
wheel_base = D;
single_intept_dist = L;
}
template<int pwm_left, int dir1_left, int dir2_left, int enca_left, int encb_left,
int pwm_right, int dir1_right, int dir2_right, int enca_right, int encb_right>
void DiffDrive<pwm_left, dir1_left, dir2_left, enca_left, encb_left,
pwm_right, dir1_right, dir2_right, enca_right, encb_right>::begin(uint32_t baud_rate) {
Serial.begin(baud_rate);
input.reserve(MAX_STR_LEN);
pinMode(pwm_left, OUTPUT);
pinMode(dir1_left, OUTPUT);
pinMode(dir2_left, OUTPUT);
pinMode(enca_left, INPUT);
pinMode(encb_left, INPUT);
pinMode(pwm_right, OUTPUT);
pinMode(dir1_right, OUTPUT);
pinMode(dir2_right, OUTPUT);
pinMode(enca_right, INPUT);
pinMode(encb_right, INPUT);
attachInterrupt(enca_left, encoder_isr_left, CHANGE);
attachInterrupt(encb_left, encoder_isr_left, CHANGE);
attachInterrupt(enca_right, encoder_isr_right, CHANGE);
attachInterrupt(encb_right, encoder_isr_right, CHANGE);
motor_velctrl_left.SetSampleTime(sample_time / 1000);
motor_velctrl_right.SetSampleTime(sample_time / 1000);
motor_velctrl_left.SetOutputLimits(-255, 255);
motor_velctrl_right.SetOutputLimits(-255, 255);
motor_velctrl_left.SetMode(AUTOMATIC);
motor_velctrl_right.SetMode(AUTOMATIC);
prev_time_pid = micros();
prev_time_odom = micros();
}
template<int pwm_left, int dir1_left, int dir2_left, int enca_left, int encb_left,
int pwm_right, int dir1_right, int dir2_right, int enca_right, int encb_right>
void DiffDrive<pwm_left, dir1_left, dir2_left, enca_left, encb_left,
pwm_right, dir1_right, dir2_right, enca_right, encb_right>::loop() {
uint32_t curr_time = micros();
if (curr_time - prev_time_pid > sample_time) {
noInterrupts();
double dcount_left = prev_encoder_count_left - encoder_count_left;
prev_encoder_count_left = encoder_count_left;
double dcount_right = prev_encoder_count_right - encoder_count_right;
prev_encoder_count_right = encoder_count_right;
interrupts();
double dt = curr_time - prev_time_odom;
if (dt > 0.0) {
wcurr_left = ((dcount_left / dt) / gearbox_cpr) * MICROSECONDS_PER_SECOND * TWO_PI;
wcurr_right = ((dcount_right / dt) / gearbox_cpr) * MICROSECONDS_PER_SECOND * TWO_PI;
double xdot = wcurr_left * ((wheel_radius / 2 * cos(theta)) + (wheel_radius * (single_intept_dist / wheel_base) * sin(theta))) +
wcurr_right * ((wheel_radius / 2 * cos(theta)) - (wheel_radius * (single_intept_dist / wheel_base) * sin(theta)));
double ydot = wcurr_left * ((wheel_radius / 2 * sin(theta)) - (wheel_radius * (single_intept_dist / wheel_base) * cos(theta))) +
wcurr_right * ((wheel_radius / 2 * sin(theta)) + (wheel_radius * (single_intept_dist / wheel_base) * cos(theta)));
double thetadot = wcurr_right * (wheel_radius / wheel_base) - wcurr_left * (wheel_radius / wheel_base);
x += xdot * (dt / MICROSECONDS_PER_SECOND);
y += ydot * (dt / MICROSECONDS_PER_SECOND);
theta += thetadot * (dt / MICROSECONDS_PER_SECOND);
theta = fmod(theta, TWO_PI);
if (theta < 0.0) {
theta += TWO_PI;
}
}
prev_time_odom = curr_time;
motor_velctrl_left.Compute();
motor_velctrl_right.Compute();
if (is_conservative) {
motor_velctrl_left.SetTunings(kp_cons, ki_cons, kd_cons);
motor_velctrl_right.SetTunings(kp_cons, ki_cons, kd_cons);
} else {
motor_velctrl_left.SetTunings(kp_agg, ki_agg, kd_agg);
motor_velctrl_right.SetTunings(kp_agg, ki_agg, kd_agg);
}
if (wtarget_left == 0.0) {
digitalWrite(dir1_left, LOW);
digitalWrite(dir2_left, LOW);
analogWrite(pwm_left, 0);
} else if (cmd_left >= 0) {
digitalWrite(dir1_left, LOW);
digitalWrite(dir2_left, HIGH);
analogWrite(pwm_left, abs(cmd_left));
} else if (cmd_left < 0) {
digitalWrite(dir1_left, HIGH);
digitalWrite(dir2_left, LOW);
analogWrite(pwm_left, abs(cmd_left));
}
if (wtarget_right == 0.0) {
digitalWrite(dir1_right, LOW);
digitalWrite(dir2_right, LOW);
analogWrite(pwm_right, 0);
} else if (cmd_right >= 0) {
digitalWrite(dir1_right, LOW);
digitalWrite(dir2_right, HIGH);
analogWrite(pwm_right, abs(cmd_right));
} else if (cmd_right < 0) {
digitalWrite(dir1_right, HIGH);
digitalWrite(dir2_right, LOW);
analogWrite(pwm_right, abs(cmd_right));
}
prev_time_pid = curr_time;
}
while (Serial.available() > 0) {
char input_char = (char) Serial.read();
if (input_char == end_marker) {
processAndReply();
} else if (input_char == start_marker) {
input = "";
} else {
input += input_char;
}
}
}
#endif