-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBottomArduinoSPI.ino
476 lines (405 loc) · 12.4 KB
/
BottomArduinoSPI.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#include <SPI.h>
#include <elapsedMillis.h>
elapsedMillis timeElapsed;
int val;
//Regulates two motors to spin at same speed for encoder of motor one
int encoder0PinA = A1; //J3 motor on board
//int encoder0PinB = 4;
int encoder0Pos = 0; //Motor's angular position read by the encoder
int encoder0PinALast = LOW;
//for encoder for motor two
int encoder1PinA = A2; //J4 motor on board
//int encoder1PinB = 8;
int encoder1Pos = 0;
int encoder1PinALast = LOW;
int encoderCountpRev = 360;
//setpoint is turn rate to compare to/reach
int setpoint = 120; //(degrees/sec)
double Integral0 = 0; //accumulated error with motors from desired number of turns
double Integral1 = 0; //accumulated error with motors from desired number of turns
int n = LOW;
int m = LOW;
//for driver for IN1 and IN2 for motor one
int motor0pin1 = 2; // J3 on Board //right motors *******************SWITCH MOTORS IN HARDWARE
int motor0pin2 = 3; //pwm pin
int pwm0 = 80; //123
int digital0 = 1; //0?
//for driver for IN1 and IN2 for motor two
int motor1pin1 = 8; // J4 on Board //left motors
int motor1pin2 = 5; //pwm (controls voltage signal) pin
int pwm1 = 80; //123
int digital1 = 1; //0?
//**line follow vars
int set = 0;
int left_Q = A6;
int right_Q = A3;
int left_calib;
int right_calib;
//***line follow vars
int left_threshold;
int right_threshold;
int left_read;
int right_read;
//CONSTANT TO GET THE REFLECTANCE OF A LINE
int left_line_refl=870;
int right_line_refl=835;
int left_line=false;
int right_line=false;
int encoder0PrevCount = 0;
int lastSpeed0 = 0;
int encoder1PrevCount = 0;
int lastSpeed1 = 0;
double timeSec = .5;
//PID constants
//P (proportional) is how much to adjust when turn rate is not equal to set rate. Matters most.
double kP = 0.25;//0.20 or .15
//I (integral) is how much to adjust based on accumulated error
double kI = 0.2;//0.01 or .05
//D (derivative) how quickly it deviates from set rate. Adjusts quicker for greater rates
double kD = 0.211;//0.01 or .01
int test;
//char buff [50]; Use multiple parameters
char updated;
volatile byte indx;
volatile boolean process;
int interruptPin = 10;
int IRPin = 4; //S4 on J5
int in;
int trigPin = 9; //J10 on board
int echoPin = A3; //this is the ADC pin
long duration,cm;
void setup() {
pinMode (encoder0PinA, INPUT);
// pinMode (encoder0PinB, INPUT);
pinMode (motor0pin1, OUTPUT);
pinMode (motor0pin2, OUTPUT);
pinMode (encoder1PinA, INPUT);
//pinMode (encoder1PinB, INPUT);
pinMode (motor1pin1, OUTPUT);
pinMode (motor1pin2, OUTPUT);
Serial.begin(115200);
pinMode(MISO,OUTPUT); //init spi
//pinMode(3,OUTPUT);
pinMode(20,OUTPUT);
pinMode(22,OUTPUT);
//linefollow***************
left_calib=analogRead(left_Q);
right_calib=analogRead(right_Q);
left_threshold = abs((left_calib-left_line_refl)/2);
right_threshold = abs((right_calib-right_line_refl)/2);
//linefollow***************
SPCR |= bit (SPE); // slave control register
indx = 0; //buffer empty
process = false;
pinMode(interruptPin,INPUT);
int val = digitalRead(interruptPin);
delay(1000);
SPI.attachInterrupt();
// attachInterrupt(digitalPinToInterrupt(interruptPin), tester, LOW); //enable interrupt
//val = 1;
pinMode(IRPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
test = 0;
}
ISR (SPI_STC_vect) { //SPI Interrupt Service Routine
// digitalWrite(3,1);
Serial.println("entered ISR");
if (SPDR != c)
Serial.println("Value has been changed");
byte c = SPDR; //read byte from SPI data register
updated = c;// save data in the next index in the array buff
Serial.print("ISR Value: ");
Serial.println(updated);
// if (c == '\r') //check for the end of the word
process = true;
}
//Adjust PWM FOR PID CODE
void adjustPWM() {
int speedNow0 = calculateSpeed0();
int error0 = setpoint - speedNow0;
double dError0 = ((double)speedNow0 - (double)lastSpeed0) / timeSec;
Integral0 += (double) error0;
int speedNow1 = calculateSpeed1();
int error1 = setpoint - speedNow1;
double dError1 = ((double)speedNow1 - (double)lastSpeed1) / timeSec;
Integral1 += (double) error1;
if (Integral0 > 255) Integral0 = 255;
else if (Integral0 < 0) Integral0 = 0;
// Serial.print("Integral0: ");
// Serial.println( Integral0);
if (Integral1 > 255) Integral1 = 255;
else if (Integral1 < 0) Integral1 = 0;
// Serial.print("Integral1: ");
// Serial.println( Integral1);
int adjust0 = (kP * (double)error0) + kI * Integral0 + kD * dError0;
int adjust1 = (kP * (double)error1) + kI * Integral1 + kD * dError1;
if (digital0 == 0) pwm0 += adjust0;
else pwm0 -= adjust0;
if (digital1 == 0) pwm1 += adjust1;
else pwm1 -= adjust1;
if (pwm0 > 255) pwm0 = 255;
else if (pwm0 < 0) pwm0 = 0;
if (pwm1 > 255) pwm1 = 255;
else if (pwm1 < 0) pwm1 = 0;
lastSpeed0 = speedNow0;
lastSpeed1 = speedNow1;
//
// Serial.print("adjustment0: ");
// Serial.println( adjust0);
// Serial.print("PWM0: ");
// Serial.println( pwm0 );
//
// Serial.print("adjustment1: ");
// Serial.println( adjust1);
// Serial.print("PWM1: ");
// Serial.println( pwm1 );
}
int calculateSpeed0() {
int speedDetect = (encoder0Pos - encoder0PrevCount) / timeSec;
// Serial.print("Encoder0pos: ");
// Serial.print( encoder0Pos );
// Serial.print(" ");
// Serial.println( encoder0PrevCount);
encoder0PrevCount = encoder0Pos;
// Serial.print( "Speed0: ");
// Serial.println( speedDetect);
return speedDetect;
}
int calculateSpeed1() {
int speedDetect = (encoder1Pos - encoder1PrevCount) / timeSec;
// Serial.print("Encoder0pos: ");
// Serial.print( encoder1Pos);
// Serial.print(" ");
// Serial.println( encoder1PrevCount);
encoder1PrevCount = encoder1Pos;
// Serial.print( "Speed1: ");
// Serial.println( speedDetect);
return speedDetect;
}
void PID() {
if (digital0 == 1)
digitalWrite( motor0pin1, HIGH);
else digitalWrite( motor0pin1, LOW);
analogWrite( motor0pin2, pwm0);
if (digital1 == 1)
digitalWrite( motor1pin1, HIGH);
else digitalWrite( motor1pin1, LOW);
analogWrite( motor1pin2, pwm1);
timeElapsed = 0;
//calculateSpeed();
while ( timeElapsed < 500 ) {
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
encoder0Pos++;
}
//Serial.print (encoder0Pos);
//Serial.print ("/");
encoder0PinALast = n;
m = digitalRead(encoder1PinA);
if ((encoder1PinALast == LOW) && (m == HIGH)) {
encoder1Pos++;
}
// Serial.print (encoder1Pos);
//Serial.print ("/");
encoder1PinALast = m;
}
//unsigned long CurrentTime = millis();
//unsigned long ElapsedTime = CurrentTime - StartTime;
timeSec = 1.0 ;//(double)( ElapsedTime * .001);
// Serial.print( "Time: ");
// Serial.println(timeSec); // time needs to be fixed
adjustPWM();
// Serial.println(" ");
}
void moveForward() {
// Serial.println("forward code");
//low is nearby, high is far
in = digitalRead(IRPin);
// Serial.println(in);
digitalWrite(trigPin,LOW);
delayMicroseconds(30);
digitalWrite(trigPin,HIGH);
delayMicroseconds(30);
pinMode(echoPin,INPUT);
duration = pulseIn(echoPin,HIGH);
//convert the time into distance: 29ms per cm
cm = (duration/2)/29.1;
Serial.println(cm);
if (in == 1 || cm < 30){
//stop
digitalWrite(motor0pin2, HIGH);//1 high 2 low is clockwise
digitalWrite(motor0pin1, LOW);
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor1pin1, LOW);
delay(400);
digitalWrite(motor0pin2, LOW);
digitalWrite(motor1pin2, LOW);
delay(1000);
// Serial.println("stop");
}
else {
//move
digital0 = 1;
digital1 = 1;
PID();
// digitalWrite(motor0pin2, LOW);//1 high 2 low is clockwise
// digitalWrite(motor0pin1, HIGH);
// digitalWrite(motor1pin2, LOW);
// digitalWrite(motor1pin1, HIGH);
//delay(400);
// Serial.println("move");
}
//check again
if (in == 1 || cm < 10){
if( in == 1)
// Serial.println("IR detects");
//stop
digitalWrite(motor0pin2, HIGH);//1 high 2 low is clockwise
digitalWrite(motor0pin1, LOW);
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor1pin1, LOW);
delay(400);
digitalWrite(motor0pin2, LOW);
digitalWrite(motor1pin2, LOW);
delay(1000);
// Serial.println("stop");
}
}
//***Line follow functions***
void stop(){
digitalWrite(motor0pin1,LOW);
digitalWrite(motor1pin1,LOW);
analogWrite(motor0pin2,0);
analogWrite(motor1pin2, 0);
}
void veer_right(){
digitalWrite(motor0pin1,HIGH);
digitalWrite(motor1pin1,HIGH);
analogWrite(motor0pin2,140);
analogWrite(motor1pin2, 50);
}
void veer_left(){
digitalWrite(motor0pin1,HIGH);
digitalWrite(motor1pin1,HIGH);
analogWrite(motor0pin2,50);
analogWrite(motor1pin2, 140);
}
void readSensors(){
left_line= (((analogRead(left_Q)-left_line_refl)<left_threshold) ||(left_line_refl-analogRead(left_Q))>left_threshold);
right_line=((analogRead(right_Q)-right_line_refl<right_threshold) || (left_line_refl-analogRead(right_Q))>right_threshold);
}
void drive_forward() {
digitalWrite(motor0pin1,HIGH);
digitalWrite(motor1pin1,HIGH);
analogWrite(motor0pin2,75);
analogWrite(motor1pin2, 75);
}
void LineFollow() {
if (set == 0) {
left_calib=analogRead(left_Q);
right_calib=analogRead(right_Q);
left_threshold = abs((left_calib-left_line_refl)/2);
right_threshold = abs((right_calib-right_line_refl)/2);
// Serial.println(left_calib);
// Serial.println(right_calib);
// Serial.println(left_threshold);
// Serial.println(right_threshold);
}
set++;
readSensors();
// Serial.print("LEFT SENSOR: ");
// Serial.println(analogRead(left_Q));
// Serial.print("RIGHT SENSOR: ");
// Serial.println(analogRead(right_Q));
delay(20);
if(!left_line && !right_line){
drive_forward();
// Serial.println("forward no line");
}
else if(!left_line && right_line){
veer_left();
// Serial.println("veer left");
}
else if(left_line && !right_line){
veer_right();
// Serial.println("veer right");
}
else {
drive_forward();
// Serial.println("forward else");
}
}
//**Line follow functions***
void loop() {
// moveForward();
// Serial.println(" test");
if (process) {
// buff[indx] = 0; Use later for multiple parameters
process = false; //reset flag
// digitalWrite(3,0);
// Serial.println("work");
// Serial.print(buff); //print to serial monitor
// int i = 0;
// if (i < sizeof(buff)) { //Multiple parameteres
Serial.println(updated);
switch(updated) {
case 'F' : //fwd
// Serial.println("moving forward");
moveForward();
//digitalWrite(motor0pin2, LOW);
//digitalWrite(motor0pin1, HIGH);
//digitalWrite(motor1pin2, LOW);
//digitalWrite(motor1pin1, HIGH);
set = 0;
//delay(6000);
break;
case 'B' : //Backwards (back())
// Serial.println("back");
digital0 = 0;
digital1 = 0;
set = 0;
PID();
// delay(6000);
break;
case 'L' : //left
// Serial.println("Left");
digitalWrite(motor0pin2, LOW);
digitalWrite(motor0pin1, HIGH);
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor1pin1, LOW);
set = 0;
//delay(6000);
break;
case 'R' : //right
digitalWrite(motor0pin2, HIGH);
digitalWrite(motor0pin1, LOW);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor1pin1, HIGH);
set = 0;
//delay(6000);
break;
case 'S' : //stop
digitalWrite(motor0pin2, LOW);
digitalWrite(motor0pin1, LOW);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor1pin1, LOW);
//delay(6000);
set = 0;
break;
case 'T' : //Line Follow mode
LineFollow();
break;
// case 'M' : //move servo
break;
default:
set = 0;
//i++;
break;
}
// }
}
//TODO Case where switching functions
//SPDR = data //sends value to master via SPDR
//indx = 0; //reset button to zero
}