![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
the car will go right after a third of the time and then go left.
Dependencies: mbed Tach ContinuousServo
main.cpp@0:86beccf9bdc8, 2019-04-18 (annotated)
- Committer:
- m211002
- Date:
- Thu Apr 18 15:41:30 2019 +0000
- Revision:
- 0:86beccf9bdc8
Turn Subsystem: car turns left or right
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
m211002 | 0:86beccf9bdc8 | 1 | #include "mbed.h" |
m211002 | 0:86beccf9bdc8 | 2 | #include "ContinuousServo.h" |
m211002 | 0:86beccf9bdc8 | 3 | #include "Tach.h" |
m211002 | 0:86beccf9bdc8 | 4 | Serial pc(USBTX,USBRX); |
m211002 | 0:86beccf9bdc8 | 5 | |
m211002 | 0:86beccf9bdc8 | 6 | //sonar sensor |
m211002 | 0:86beccf9bdc8 | 7 | AnalogIn sonar(p19); //range sensor 9.8mV/inch |
m211002 | 0:86beccf9bdc8 | 8 | //servo |
m211002 | 0:86beccf9bdc8 | 9 | ContinuousServo left(p23); |
m211002 | 0:86beccf9bdc8 | 10 | ContinuousServo right(p26); |
m211002 | 0:86beccf9bdc8 | 11 | //encoders |
m211002 | 0:86beccf9bdc8 | 12 | Tach tLeft(p17,64); |
m211002 | 0:86beccf9bdc8 | 13 | Tach tRight(p13,64); |
m211002 | 0:86beccf9bdc8 | 14 | int main() |
m211002 | 0:86beccf9bdc8 | 15 | { |
m211002 | 0:86beccf9bdc8 | 16 | while(1) { |
m211002 | 0:86beccf9bdc8 | 17 | float var1; |
m211002 | 0:86beccf9bdc8 | 18 | int num_iterations; |
m211002 | 0:86beccf9bdc8 | 19 | int i=1; |
m211002 | 0:86beccf9bdc8 | 20 | |
m211002 | 0:86beccf9bdc8 | 21 | pc.scanf("%d",&num_iterations);//reads in values from the Matlab |
m211002 | 0:86beccf9bdc8 | 22 | while(i<=num_iterations) { |
m211002 | 0:86beccf9bdc8 | 23 | i++; |
m211002 | 0:86beccf9bdc8 | 24 | left.speed(.25); |
m211002 | 0:86beccf9bdc8 | 25 | right.speed(-0.25); |
m211002 | 0:86beccf9bdc8 | 26 | var1=num_iterations/3; |
m211002 | 0:86beccf9bdc8 | 27 | |
m211002 | 0:86beccf9bdc8 | 28 | if(i==var1) { |
m211002 | 0:86beccf9bdc8 | 29 | left.speed(.25); |
m211002 | 0:86beccf9bdc8 | 30 | right.speed(.25); |
m211002 | 0:86beccf9bdc8 | 31 | wait(0.9); |
m211002 | 0:86beccf9bdc8 | 32 | } |
m211002 | 0:86beccf9bdc8 | 33 | if(i==var1*2) { |
m211002 | 0:86beccf9bdc8 | 34 | left.speed(-0.25); |
m211002 | 0:86beccf9bdc8 | 35 | right.speed(-0.25); |
m211002 | 0:86beccf9bdc8 | 36 | wait(0.9); |
m211002 | 0:86beccf9bdc8 | 37 | } |
m211002 | 0:86beccf9bdc8 | 38 | wait(0.1); |
m211002 | 0:86beccf9bdc8 | 39 | pc.printf("%d\n",i);//mbed send out float values for Matlab |
m211002 | 0:86beccf9bdc8 | 40 | } |
m211002 | 0:86beccf9bdc8 | 41 | left.stop(); |
m211002 | 0:86beccf9bdc8 | 42 | right.stop(); |
m211002 | 0:86beccf9bdc8 | 43 | } |
m211002 | 0:86beccf9bdc8 | 44 | } |
m211002 | 0:86beccf9bdc8 | 45 | |
m211002 | 0:86beccf9bdc8 | 46 |