-
Notifications
You must be signed in to change notification settings - Fork 1
/
Alex_v3.ino
897 lines (741 loc) · 18.5 KB
/
Alex_v3.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
#include <serialize.h>
#include "packet.h"
#include "constants.h"
#define COLOUR_CHECK true
#define IR_CHECK true
typedef enum
{
STOP=0,
FORWARD=1,
BACKWARD=2,
LEFT=3,
RIGHT=4
} TDirection;
volatile TDirection dir = STOP;
/*
* Alex's configuration constants
*/
// Number of ticks per revolution from the
// wheel encoder.
#define COUNTS_PER_REV 192
// Wheel circumference in cm.
// We will use this to calculate forward/backward distance traveled
// by taking revs * WHEEL_CIRC
#define WHEEL_CIRC 21 //MAYBE
// Motor control pins. You need to adjust these till
// Alex moves in the correct direction
#define LF 6 // Left forward pin
#define LR 5 // Left reverse pin
#define RF 10 // Right forward pin
#define RR 11 // Right reverse pin
// PI, for calculating turn circumference
#define PI 3.141592654
// Alex's length and breath in cm
#define ALEX_LENGTH 18
#define ALEX_BREADTH 13
// TCS230 or TCS3200 pins wiring to Arduino
//#define S0 0
//#define S1 1
//#define S3 7
#define S2 4
#define sensorOut 8
// IR
#define FRONT 7
#define BACK 9
#define LEFT 12
#define RIGHT 13
/*
* Alex's State Variables
*/
// Alex's diagonal. We compute and store this once
// since it is expensive to compute and really doesn't change.
float AlexDiagonal = 0.0;
// Alex's turnign circumference, calculated once
float AlexCirc = 0.0;
// Store the ticks from Alex's left and
// right encoders for moving forwards and backwards
volatile unsigned long leftForwardTicks;
volatile unsigned long rightForwardTicks;
volatile unsigned long leftReverseTicks;
volatile unsigned long rightReverseTicks;
//Left and right encoder ticks for turning
volatile unsigned long leftForwardTicksTurns;
volatile unsigned long rightForwardTicksTurns;
volatile unsigned long leftReverseTicksTurns;
volatile unsigned long rightReverseTicksTurns;
// Forward and backward distance traveled
volatile unsigned long forwardDist;
volatile unsigned long reverseDist;
// Variable to keep track of whether we have moved a commanded distance
unsigned long deltaDist;
unsigned long newDist;
// Variables to keep track of our turing angle
unsigned long deltaTicks;
unsigned long targetTicks;
// Stores frequency read by the photodiodes
int redFrequency = 0;
int greenFrequency = 0;
int count = 0;
int red_ave = 100;
int green_ave = 0;
int color;
int red;
int green;
// Store values for IR sensor
int IR;
int front_out;
int back_out;
int left_out;
int right_out;
/*
*
* Alex Communication Routines.
*
*/
TResult readPacket(TPacket *packet)
{
// Reads in data from the serial port and
// deserializes it.Returns deserialized
// data in "packet".
char buffer[PACKET_SIZE];
int len;
len = readSerial(buffer);
if(len == 0)
return PACKET_INCOMPLETE;
else
return deserialize(buffer, len, packet);
}
void sendStatus()
{
// Implement code to send back a packet containing key
// information like leftTicks, rightTicks, leftRevs, rightRevs
// forwardDist and reverseDist
// Use the params array to store this information, and set the
// packetType and command files accordingly, then use sendResponse
// to send out the packet. See sendMessage on how to use sendResponse.
//
TPacket statusPacket;
statusPacket.packetType = PACKET_TYPE_RESPONSE;
statusPacket.command = RESP_STATUS;
statusPacket.params[0] = leftForwardTicks;
statusPacket.params[1] = rightForwardTicks;
statusPacket.params[2] = leftReverseTicks;
statusPacket.params[3] = rightReverseTicks;
statusPacket.params[4] = leftForwardTicksTurns;
statusPacket.params[5] = rightForwardTicksTurns;
statusPacket.params[6] = leftReverseTicksTurns;
statusPacket.params[7] = rightReverseTicksTurns;
statusPacket.params[8] = forwardDist;
statusPacket.params[9] = reverseDist;
if (COLOUR_CHECK) {
statusPacket.params[10] = color;
// statusPacket.params[10] = red;
// statusPacket.params[10] = green;
}
if (IR_CHECK) {
statusPacket.params[11] = IR;
}
sendResponse(&statusPacket);
}
void sendMessage(const char *message)
{
// Sends text messages back to the Pi. Useful
// for debugging.
TPacket messagePacket;
messagePacket.packetType=PACKET_TYPE_MESSAGE;
strncpy(messagePacket.data, message, MAX_STR_LEN);
sendResponse(&messagePacket);
}
void sendBadPacket()
{
// Tell the Pi that it sent us a packet with a bad
// magic number.
TPacket badPacket;
badPacket.packetType = PACKET_TYPE_ERROR;
badPacket.command = RESP_BAD_PACKET;
sendResponse(&badPacket);
}
void sendBadChecksum()
{
// Tell the Pi that it sent us a packet with a bad
// checksum.
TPacket badChecksum;
badChecksum.packetType = PACKET_TYPE_ERROR;
badChecksum.command = RESP_BAD_CHECKSUM;
sendResponse(&badChecksum);
}
void sendBadCommand()
{
// Tell the Pi that we don't understand its
// command sent to us.
TPacket badCommand;
badCommand.packetType=PACKET_TYPE_ERROR;
badCommand.command=RESP_BAD_COMMAND;
sendResponse(&badCommand);
}
void sendBadResponse()
{
TPacket badResponse;
badResponse.packetType = PACKET_TYPE_ERROR;
badResponse.command = RESP_BAD_RESPONSE;
sendResponse(&badResponse);
}
void sendOK()
{
TPacket okPacket;
okPacket.packetType = PACKET_TYPE_RESPONSE;
okPacket.command = RESP_OK;
sendResponse(&okPacket);
}
void sendResponse(TPacket *packet)
{
// Takes a packet, serializes it then sends it out
// over the serial port.
char buffer[PACKET_SIZE];
int len;
len = serialize(buffer, packet, sizeof(TPacket));
writeSerial(buffer, len);
}
/*
* Setup and start codes for external interrupts and
* pullup resistors.
*
*/
// Enable pull up resistors on pins 2 and 3
void enablePullups()
{
// Use bare-metal to enable the pull-up resistors on pins
// 2 and 3. These are pins PD2 and PD3 respectively.
// We set bits 2 and 3 in DDRD to 0 to make them inputs.
DDRD &= 0b11110011;
PORTD |= 0b00001100;
}
// Functions to be called by INT0 and INT1 ISRs.
void leftISR()
{
if (dir == FORWARD){
leftForwardTicks++;
forwardDist = (unsigned long) ((float) leftForwardTicks / COUNTS_PER_REV * WHEEL_CIRC);
}
else if (dir == BACKWARD){
leftReverseTicks++;
reverseDist = (unsigned long) ((float) leftReverseTicks / COUNTS_PER_REV * WHEEL_CIRC);
}
else if (dir == LEFT){
leftReverseTicksTurns++;
}
else if (dir == RIGHT){
leftForwardTicksTurns++;
}
}
void rightISR()
{
if (dir == FORWARD){
rightForwardTicks++;
}
else if (dir == BACKWARD){
rightReverseTicks++;
}
else if (dir == LEFT){
rightForwardTicksTurns++;
}
else if (dir == RIGHT){
rightReverseTicksTurns++;
}
}
// Set up the external interrupt pins INT0 and INT1
// for falling edge triggered. Use bare-metal.
void setupEINT()
{
// Use bare-metal to configure pins 2 and 3 to be
// falling edge triggered. Remember to enable
// the INT0 and INT1 interrupts.
EICRA = 0b00001010;
EIMSK = 0b00000011;
}
// Implement the external interrupt ISRs below.
// INT0 ISR should call leftISR while INT1 ISR
// should call rightISR.
ISR(INT0_vect)
{
leftISR();
}
ISR(INT1_vect)
{
rightISR();
}
// Implement INT0 and INT1 ISRs above.
/*
* Setup and start codes for serial communications
*
*/
// Set up the serial connection. For now we are using
// Arduino Wiring, you will replace this later
// with bare-metal code.
void setupSerial()
{
// To replace later with bare-metal.
Serial.begin(57600);
}
// Start the serial connection. For now we are using
// Arduino wiring and this function is empty. We will
// replace this later with bare-metal code.
void startSerial()
{
// Empty for now. To be replaced with bare-metal code
// later on.
}
// Read the serial port. Returns the read character in
// ch if available. Also returns TRUE if ch is valid.
// This will be replaced later with bare-metal code.
int readSerial(char *buffer)
{
int count=0;
while(Serial.available())
buffer[count++] = Serial.read();
return count;
}
// Write to the serial port. Replaced later with
// bare-metal code
void writeSerial(const char *buffer, int len)
{
Serial.write(buffer, len);
}
/*
* Alex's motor drivers.
*
*/
// Set up Alex's motors. Right now this is empty, but
// later you will replace it with code to set up the PWMs
// to drive the motors.
void setupMotors()
{
/* Our motor set up is:
* A1IN - Pin 5, PD5, OC0B
* A2IN - Pin 6, PD6, OC0A
* B1IN - Pin 10, PB2, OC1B
* B2In - pIN 11, PB3, OC2A
*/
}
// Start the PWM for Alex's motors.
// We will implement this later. For now it is
// blank.
void startMotors()
{
}
// Convert percentages to PWM values
int pwmVal(float speed)
{
if(speed < 0.0)
speed = 0;
if(speed > 100.0)
speed = 100.0;
return (int) ((speed / 100.0) * 255.0);
}
// Move Alex forward "dist" cm at speed "speed".
// "speed" is expressed as a percentage. E.g. 50 is
// move forward at half speed.
// Specifying a distance of 0 means Alex will
// continue moving forward indefinitely.
//void forward(float dist, float speed)
void forward(float dist)
{
if (dist > 0)
deltaDist = dist*0.92;
else
deltaDist = 5;
newDist = forwardDist + deltaDist;
dir = FORWARD;
//int val = pwmVal(speed);
// For now we will ignore dist and move
// forward indefinitely. We will fix this
// in Week 9.
// LF = Left forward pin, LR = Left reverse pin
// RF = Right forward pin, RR = Right reverse pin
// This will be replaced later with bare-metal code.
analogWrite(LF, 204);
analogWrite(RF, 160);
// analogWrite(LF, val);
// analogWrite(RF, 0.7*val);
analogWrite(LR,0);
analogWrite(RR, 0);
}
// Reverse Alex "dist" cm at speed "speed".
// "speed" is expressed as a percentage. E.g. 50 is
// reverse at half speed.
// Specifying a distance of 0 means Alex will
// continue reversing indefinitely.
//void reverse(float dist, float speed)
void reverse(float dist)
{
if(dist>0)
deltaDist = dist*0.92;
else
deltaDist = 5;
newDist = reverseDist + deltaDist;
dir = BACKWARD;
//int val = pwmVal(speed);
// For now we will ignore dist and
// reverse indefinitely. We will fix this
// in Week 9.
// LF = Left forward pin, LR = Left reverse pin
// RF = Right forward pin, RR = Right reverse pin
// This will be replaced later with bare-metal code.
analogWrite(LR, 204);
analogWrite(RR,160);
// analogWrite(LR, val);
// analogWrite(RR, 0.7*val);
analogWrite(LF, 0);
analogWrite(RF, 0);
}
// New function to estimate number of wheel ticks
// needed to turn an angle
unsigned long computeDeltaTicks(float ang)
{
// We will assume that angular distance moved = linear distance moved in one wheel
// revolution. This is (probably) incorrect but simplifies calculation.
// # of wheel revs to make one full 360 turn is AlexCirc / WHEEL_CIRC
// This is for 360 degrees. For ang degrees it will be (ang * AlexCirc) / (360 * WHEEL_CIRC)
// To convert to ticks, we multiply by COUNTS_PER_REV.
unsigned long ticks = (unsigned long) ((ang * AlexCirc * COUNTS_PER_REV) / (360.0 * WHEEL_CIRC));
return ticks;
}
// Turn Alex left "ang" degrees at speed "speed".
// "speed" is expressed as a percentage. E.g. 50 is
// turn left at half speed.
// Specifying an angle of 0 degrees will cause Alex to
// turn left indefinitely.
void left(float ang)//, float speed)
{
if (ang > 0)
deltaTicks = computeDeltaTicks(ang*10)* 0.35;
else
deltaTicks = 10;
dir = LEFT;
// int val = pwmVal(speed);
//
// if(val<255*0.8){
// val = 255*0.8;
// }
targetTicks = leftReverseTicksTurns + deltaTicks;
// For now we will ignore ang. We will fix this in Week 9.
// We will also replace this code with bare-metal later.
// To turn left we reverse the left wheel and move
// the right wheel forward.
// analogWrite(LR, 220);
// analogWrite(RF, 220);
analogWrite(LR, 255*0.8);
analogWrite(RF, 255*0.8*0.7);
analogWrite(LF, 0);
analogWrite(RR, 0);
}
// Turn Alex right "ang" degrees at speed "speed".
// "speed" is expressed as a percentage. E.g. 50 is
// turn left at half speed.
// Specifying an angle of 0 degrees will cause Alex to
// turn right indefinitely.
void right(float ang)//, float speed)
{
if (ang > 0)
deltaTicks = computeDeltaTicks(ang*10) * 0.25;
else
deltaTicks = 10;
dir = RIGHT;
// int val = pwmVal(speed);
//
// if (val < 255*0.8) {
// val = 255*0.8;
// }
targetTicks = rightReverseTicksTurns + deltaTicks;
// For now we will ignore ang. We will fix this in Week 9.
// We will also replace this code with bare-metal later.
// To turn right we reverse the right wheel and move
// the left wheel forward.
analogWrite(RR, 255*0.8*0.7);
analogWrite(LF, 255*0.8);
// analogWrite(RR, 220);
// analogWrite(LF, 220);
analogWrite(LR, 0);
analogWrite(RF, 0);
}
// Stop Alex. To replace with bare-metal code later.
void stop()
{
dir = STOP;
analogWrite(LF, 0);
analogWrite(LR, 0);
analogWrite(RF, 0);
analogWrite(RR, 0);
}
/*
* Alex's setup and run codes
*
*/
// Clears all our counters
void clearCounters()
{
leftForwardTicks=0;
rightForwardTicks=0;
leftReverseTicks=0;
rightReverseTicks=0;
leftForwardTicksTurns=0;
rightForwardTicksTurns=0;
leftReverseTicksTurns=0;
rightReverseTicksTurns=0;
forwardDist=0;
reverseDist=0;
}
// Clears one particular counter
void clearOneCounter(int which)
{
clearCounters();
}
// Intialize Vincet's internal states
void initializeState()
{
clearCounters();
}
void handleCommand(TPacket *command)
{
switch(command->command)
{
// For movement commands, param[0] = distance, param[1] = speed.
case COMMAND_FORWARD:
sendOK();
// forward((float) command->params[0], (float) command->params[1]);
// forward(5.0);
forward((float) command->params[0]);
break;
case COMMAND_REVERSE:
sendOK();
// reverse(5.0);//(float) command->params[0], (float) command->params[1]);
reverse((float) command->params[0]);
break;
case COMMAND_TURN_LEFT:
sendOK();
// left();//(float) command->params[0], (float) command->params[1]);
left((float) command->params[0]);
break;
case COMMAND_TURN_RIGHT:
sendOK();
// right();//(float) command->params[0], (float) command->params[1]);
right((float) command->params[0]);
break;
case COMMAND_STOP:
sendOK();
stop();
break;
case COMMAND_GET_STATS:
sendOK();
sendStatus();
break;
/*
* Implement code for other commands here.
*
*/
default:
sendBadCommand();
}
}
void waitForHello()
{
int exit=0;
while(!exit)
{
TPacket hello;
TResult result;
do
{
result = readPacket(&hello);
} while (result == PACKET_INCOMPLETE);
if(result == PACKET_OK)
{
if(hello.packetType == PACKET_TYPE_HELLO)
{
sendOK();
exit=1;
}
else
sendBadResponse();
}
else
if(result == PACKET_BAD)
{
sendBadPacket();
}
else
if(result == PACKET_CHECKSUM_BAD)
sendBadChecksum();
} // !exit
}
void setup() {
// Compute the diagonal
AlexDiagonal = sqrt((ALEX_LENGTH * ALEX_LENGTH) + (ALEX_BREADTH * ALEX_BREADTH));
AlexCirc = PI * AlexDiagonal;
// Setting the outputs for TCS3200 color sensor
//pinMode(S0, OUTPUT);
//pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
//pinMode(S3, OUTPUT);
pinMode(sensorOut, INPUT);
// Setting for IR sensor
pinMode(FRONT, INPUT);
pinMode(LEFT, INPUT);
pinMode(RIGHT, INPUT);
pinMode(BACK, INPUT);
// Setting frequency scaling to 20%
//digitalWrite(S0,HIGH);
//digitalWrite(S1,LOW);
// Begins serial communication
Serial.begin(9600);
cli();
setupEINT();
setupSerial();
startSerial();
setupMotors();
startMotors();
enablePullups();
initializeState();
sei();
}
void handlePacket(TPacket *packet)
{
switch(packet->packetType)
{
case PACKET_TYPE_COMMAND:
handleCommand(packet);
break;
case PACKET_TYPE_RESPONSE:
break;
case PACKET_TYPE_ERROR:
break;
case PACKET_TYPE_MESSAGE:
break;
case PACKET_TYPE_HELLO:
break;
}
}
void loop() {
TPacket recvPacket; // This holds commands from he Pi
TResult result = readPacket(&recvPacket);
if(result == PACKET_OK)
handlePacket(&recvPacket);
else if(result == PACKET_BAD)
{
sendBadPacket();
}
else if(result == PACKET_CHECKSUM_BAD)
{
sendBadChecksum();
}
if(deltaDist > 0)
{
if (dir == FORWARD)
{
if (forwardDist > newDist)
{
deltaDist = 0;
newDist = 0;
stop();
}
}
else if(dir==BACKWARD)
{
if(reverseDist > newDist)
{
deltaDist = 0;
newDist = 0;
stop();
}
}
else if (dir == STOP)
{
deltaTicks = 0;
targetTicks = 0;
stop();
}
}
if (deltaTicks > 0)
{
if (dir == LEFT)
{
if (leftReverseTicksTurns >= targetTicks)
{
deltaTicks = 0;
targetTicks = 0;
stop();
}
}
else if (dir == RIGHT)
{
if (rightReverseTicksTurns >= targetTicks)
{
deltaTicks = 0;
targetTicks = 0;
stop();
}
}
else if (dir == STOP)
{
deltaTicks = 0;
targetTicks = 0;
stop();
}
}
if (COLOUR_CHECK) {
// Setting RED (R) filtered photodiodes to be read
digitalWrite(S2,LOW);
//digitalWrite(S3,LOW);
// Reading the output frequency
redFrequency = pulseIn(sensorOut, LOW);
// Setting GREEN (G) filtered photodiodes to be read
digitalWrite(S2,HIGH);
//digitalWrite(S3,HIGH);
// Reading the output frequency
greenFrequency = pulseIn(sensorOut, LOW);
count += 1;
if (count == 4) {
red_ave /= 5;
green_ave /= 5;
red = red_ave;
green = green_ave;
// if (red_ave < green_ave) {
if (red_ave > 100) {
color = 0;
}
else if ((red_ave <= 100) & (green_ave <akk 100)){
color = 1;
}
else color = 9;
count = 0; // reset
red_ave = 0;
green_ave = 0;
} else {
red_ave += redFrequency;
green_ave += greenFrequency;
}
}
if (IR_CHECK){
//reading IR sensors
front_out = digitalRead(FRONT);
back_out = digitalRead(BACK);
left_out = digitalRead(LEFT);
right_out = digitalRead(RIGHT);
if(front_out == 0){
stop();
delay(50);
reverse(2);
delay(50);
IR = 0;
//sendStatus();
}
else if(back_out == 0){
stop();
delay(50);
forward(2);
delay(50);
IR = 1;
//sendStatus();
}
}
}