stop cart

Dependencies:   mbed ContinuousServo

Committer:
m211002
Date:
Wed Apr 17 20:10:31 2019 +0000
Revision:
1:cf9ba941d8c2
Parent:
0:9952fdc226ac
Sonar Subsystem stops car 4/17/19 2.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
m211002 0:9952fdc226ac 1 #include "mbed.h"
m211002 1:cf9ba941d8c2 2 #include "ContinuousServo.h"
m211002 1:cf9ba941d8c2 3 #include "Tach.h"
m211002 0:9952fdc226ac 4 Serial pc(USBTX,USBRX);
m211002 1:cf9ba941d8c2 5
m211002 0:9952fdc226ac 6 //sonar sensor
m211002 0:9952fdc226ac 7 AnalogIn sonar(p19); //range sensor 9.8mV/inch
m211002 1:cf9ba941d8c2 8 //servo
m211002 1:cf9ba941d8c2 9 ContinuousServo left(p23);
m211002 1:cf9ba941d8c2 10 ContinuousServo right(p26);
m211002 1:cf9ba941d8c2 11 //encoders
m211002 1:cf9ba941d8c2 12 Tach tLeft(p17,64);
m211002 1:cf9ba941d8c2 13 Tach tRight(p13,64);
m211002 0:9952fdc226ac 14 int main()
m211002 0:9952fdc226ac 15 {
m211002 0:9952fdc226ac 16 while(1) {
m211002 1:cf9ba941d8c2 17 float distance;
m211002 1:cf9ba941d8c2 18 float stop_val;
m211002 1:cf9ba941d8c2 19 int stop_dist;
m211002 1:cf9ba941d8c2 20 int num_iterations;
m211002 1:cf9ba941d8c2 21 int i;
m211002 1:cf9ba941d8c2 22
m211002 1:cf9ba941d8c2 23 pc.scanf("%d,%d",&num_iterations,&stop_dist);//reads in values from the Matlab
m211002 0:9952fdc226ac 24 for(i=1; i<=num_iterations; i++) { //will go until it meets the number of iterations
m211002 1:cf9ba941d8c2 25
m211002 1:cf9ba941d8c2 26 distance = sonar.read(); //takes the sensor redings and converts them to "distance" variable
m211002 1:cf9ba941d8c2 27 stop_val=.00275*stop_dist; //takes the input desired stop distance and multiplies it by a constant to achieve a stop value
m211002 1:cf9ba941d8c2 28
m211002 1:cf9ba941d8c2 29 if(distance>stop_val) { //if distance is greater than desired stopping, then keep driving forward
m211002 0:9952fdc226ac 30 left.speed(.5);
m211002 0:9952fdc226ac 31 right.speed(.5);
m211002 1:cf9ba941d8c2 32 } else { //else stop left and right wheels
m211002 1:cf9ba941d8c2 33 left.stop();
m211002 1:cf9ba941d8c2 34 right.stop();
m211002 1:cf9ba941d8c2 35 }
m211002 0:9952fdc226ac 36 wait(0.1);
m211002 1:cf9ba941d8c2 37 pc.printf("%f\n",distance);//mbed send out float values for Matlab
m211002 0:9952fdc226ac 38 }
m211002 0:9952fdc226ac 39 }
m211002 0:9952fdc226ac 40 }