-
Notifications
You must be signed in to change notification settings - Fork 1
/
aero_motorControl_withPi.ino
375 lines (323 loc) · 9.15 KB
/
aero_motorControl_withPi.ino
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
375
//(pi*radius[cm])/(600*velocity[cm/s])
//wire setup for encoders
//5V to blue
//GND to green
//motor 1 = A in code motor 2 = B in code
//pwm motor value|encoder time interval
// 250|350
// 200|370
// 150|410
// 100|500
// 50|950
//--------------------
//TODO:
//Improve readability and variable/function names
//--------------------
//values for error calc
float errorA;
float errorB;
float K_pA;
float K_pB;
float P_outputA;
float P_outputB;
//pin setup for motors
#define MOTOR_A 1
#define MOTOR_A_CCW 6
#define MOTOR_A_CW 4
#define PWM_A 5
#define MOTOR_B 0
#define MOTOR_B_CCW 8
#define MOTOR_B_CW 9
#define PWM_B 10
//motor speed 0-255
int motorValueA = 0;
int motorValueB = 0;
//desired speed in cm/s
float givenSpeedA = 0;
float givenSpeedB = 0;
//desired time interval for encoders
int givenTimeA = 800;
int givenTimeB = 400;
//desired direction
bool dirA = false;
bool dirB = true;
//check for if interrupt has been triggered recently
bool encoderStateA = false;
bool encoderStateB = false;
//current/previous encoder time intervals
unsigned long timeAcur = 0;
unsigned long timeBcur = 0;
unsigned long timeApre = 0;
unsigned long timeBpre = 0;
//actual time interval for encoders
unsigned long actualTimeA = 1000;
unsigned long actualTimeB = 1000;
//serial communication variables
const byte numChars = 64;
char receivedChars[numChars];
int id, spin, brake, dir1, vel1, dir2, vel2;
boolean newData = false;
byte ledPin = 13; // the onboard LED
void setup()
{
pinMode(MOTOR_A_CCW, OUTPUT);
pinMode(MOTOR_A_CW, OUTPUT);
pinMode(PWM_A, OUTPUT);
pinMode(MOTOR_B_CCW, OUTPUT);
pinMode(MOTOR_B_CW, OUTPUT);
pinMode(PWM_B, OUTPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
Serial.begin(115200);
attachInterrupt(digitalPinToInterrupt(3), EncoderA, RISING);
attachInterrupt(digitalPinToInterrupt(2), EncoderB, RISING);
//LED blink to show successful boot
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
Serial.println("<Arduino is ready>");
}
void loop()
{
//the arduino has a 64 byte serial buffer so dont send more bytes than that before processing them or else bytes will be lost, sometimes it may need to be flushed/emptied this can be done as follows
/*
while (Serial.available())
Serial.read();
*/
//original code for revceiving serial bytes from a python program
//received bytes were a single string eg "R F2 B' U' F U D' R2 D' F' B2 U' R2 U R2 U' B2 R2 U'"
//this got divided at each space into an array of strings, each entry was a motor command where the letter denoted which motor to rotate, the number denoted how many times to rotate and the apostrophe denoted opposite rotation
//eg "F2" rotated the front motor 90 degrees cw 2 times, "U" rotated the up motor 90 degrees cw 1 time, "D'" rotated the down motor 90 degrees ccw
/*
char cubei;
String cubef[19];
int a = 0;
//only enters when some serial value is received
while (Serial.available()) {
//temporarily save the current byte received
cubei = Serial.read();
//if the byte is alphanumeric save the byte to the current array index
if (isAlphaNumeric(cubei)) {
cubef[a] += cubei;
}
//if the byte is a space move 1 index forward
else if (isSpace(cubei)) {
a++;
}
//if the byte was anything else ie \0 null terminator exit the while loop
else {
break;
}
}
*/
//changes the desired time interval of encoders to a new user inputed value
//not robust at all for now, only use numbers or else it might break
//give a number within ~300 (max speed) to ~1000 (min speed)
//conditions for start/end characters can be added just vaguely keep these 3 lines and will it should work
/* String temp = "";
while (Serial.available()) {
//pretty sure this just appends the next value to the string, idk if its good practise or not
temp += Serial.read();
} */
rxMessage();
if(newData)
{
Serial.println(receivedChars);
char* token = strtok(receivedChars, " ");
id = atoi(token);
spin = atoi(strtok(0, " "));
brake = atoi(strtok(0, " "));
dir1 = atoi(strtok(0, " "));
vel1 = atoi(strtok(0, " "));
dir2 = atoi(strtok(0, " "));
vel2 = atoi(strtok(0, " "));
}
//immediately acknowledge message
returnMessage();
//change values w/ new data
if(spin == 1)
{
turn();
spin = 0;
}
if (dir1 == 1)
{
dirA = true;
}
else
{
dirA = false;
}
if (dir2 == 1)
{
dirB = true;
}
else
{
dirB = false;
}
//givenTimeA = temp.toInt();
//givenTimeB = temp.toInt();
givenSpeedA = vel1;
givenSpeedB = vel2;
//calculates the new time interval of encoders
interruptCheck();
//calculates desired encoder time interval based on desired speed in cm/s
speedCalc();
//sets motor speed and directions
motorA(dirA, motorValueA);
motorB(dirB, motorValueB);
}
//--------------------------
//calls micros() when interrupt is triggered, keeps previous micros() value for calculating time interval of encoder
void EncoderA()
{
timeApre = timeAcur;
timeAcur = micros();
encoderStateA = true;
}
void EncoderB()
{
timeBpre = timeBcur;
timeBcur = micros();
encoderStateB = true;
}
//receive message from pi
void rxMessage() {
static boolean rxInProgress = false;
static byte index = 0;
char startMarker = '<';
char endMarker = '>';
char receivedString;
while (Serial.available() > 0 && newData == false) {
receivedString = Serial.read();
if (rxInProgress == true) {
if (receivedString != endMarker) {
receivedChars[index] = receivedString;
index++;
if (index >= numChars) {
index = numChars - 1;
}
}
else {
receivedChars[index] = '\0'; // terminate the string
rxInProgress = false;
index = 0;
newData = true;
}
}
else if (receivedString == startMarker) {
rxInProgress = true;
}
}
}
//return current
void returnMessage() {
if (newData == true) {
Serial.print("<");
Serial.print(id);
Serial.print(" ");
Serial.print(spin);
Serial.print(" ");
Serial.print(brake);
Serial.print(" ");
Serial.print(dir1);
Serial.print(" ");
Serial.print(vel1);
Serial.print(" ");
Serial.print(dir2);
Serial.print(" ");
Serial.print(vel2);
Serial.print('>');
// change the state of the LED everytime a reply is sent
digitalWrite(ledPin, ! digitalRead(ledPin));
newData = false;
delay(1000);
}
}
//sets motor speed and direction, direction is single bool, motor speed is scaled from digital input to analog output (0-1023 -> 0-255)
void motorA(bool dirA, int motorValueA)
{
digitalWrite(MOTOR_A_CCW, dirA);
digitalWrite(MOTOR_A_CW, !dirA);
//motorValueA = map(motorValueA, 0, 1023, 0, 255);
analogWrite(PWM_A, motorValueA);
}
void motorB(bool dirB, int motorValueB)
{
digitalWrite(MOTOR_B_CCW, dirB);
digitalWrite(MOTOR_B_CW, !dirB);
//motorValueB = map(motorValueB, 0, 1023, 0, 255);
analogWrite(PWM_B, motorValueB);
}
//takes desired speed in cm/s and converts to encoder time interval
void speedCalc()
{
//convert givenSpeedA and givenSpeedB to encoder time intervals
givenTimeA = 106.68 / (givenSpeedA / 60);
givenTimeB = 106.68 / (givenSpeedB / 60);
//calculate error between desired and actual time
errorA = givenTimeA - actualTimeA;
errorB = givenTimeB - actualTimeB;
//multiply error by proportional coefficient K_p
P_outputA = K_pA * errorA;
P_outputB = K_pB * errorB;
if(P_outputA >= 0)
{
givenTimeA = givenTimeA + P_outputA;
}
else
{
givenTimeA = givenTimeA - P_outputA;
}
if(P_outputB >= 0)
{
givenTimeB = givenTimeB + P_outputB;
}
else
{
givenTimeB = givenTimeB - P_outputB;
}
}
void interruptCheck()
{
//checks if interrupt has been triggered
if (encoderStateA)
{
actualTimeA = timeAcur - timeApre;
encoderStateA = false;
}
if (encoderStateB)
{
actualTimeB = timeBcur - timeBpre;
encoderStateB = false;
}
}
void brakeUntil(int length)
{
digitalWrite(MOTOR_A_CCW, HIGH);
digitalWrite(MOTOR_A_CW, HIGH);
digitalWrite(PWM_A, HIGH);
digitalWrite(MOTOR_B_CCW, HIGH);
digitalWrite(MOTOR_B_CW, HIGH);
digitalWrite(PWM_B, HIGH);
delay(length);
}
//turns the robot 180 degrees
void turn()
{
for (int i = 0; i < 100; i++) {
digitalWrite(MOTOR_A_CCW, true);
digitalWrite(MOTOR_A_CW, false);
analogWrite(PWM_A, 255);
digitalWrite(MOTOR_B_CCW, false);
digitalWrite(MOTOR_B_CW, true);
analogWrite(PWM_B, 255);
delay(1);
}
analogWrite(PWM_A, 0);
analogWrite(PWM_B, 0);
}