Explanation of Variables

Dependencies:   m3pi_ng mbed

Fork of BlueToothStuff by der Roboter

Committer:
mmpeter
Date:
Thu May 22 11:57:29 2014 +0000
Revision:
0:386c250325ce
Child:
1:6402638c6f6d
Thursday's work 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mmpeter 0:386c250325ce 1 #include "mbed.h"
mmpeter 0:386c250325ce 2 #include "m3pi_ng.h"
mmpeter 0:386c250325ce 3 #include "cmath"
mmpeter 0:386c250325ce 4 #include "iostream"
mmpeter 0:386c250325ce 5
mmpeter 0:386c250325ce 6 using namespace std;
mmpeter 0:386c250325ce 7
mmpeter 0:386c250325ce 8 m3pi thinggy;
mmpeter 0:386c250325ce 9
mmpeter 0:386c250325ce 10
mmpeter 0:386c250325ce 11 int main() {
mmpeter 0:386c250325ce 12
mmpeter 0:386c250325ce 13 float speed = 0.25;
mmpeter 0:386c250325ce 14 float correction;
mmpeter 0:386c250325ce 15 float k = -0.3;
mmpeter 0:386c250325ce 16 int sensor[5];
mmpeter 0:386c250325ce 17 int returned;
mmpeter 0:386c250325ce 18 thinggy.locate(0,1);
mmpeter 0:386c250325ce 19 thinggy.printf("Villan");
mmpeter 0:386c250325ce 20 thinggy.locate(0,0);
mmpeter 0:386c250325ce 21 thinggy.printf("Pimpin");
mmpeter 0:386c250325ce 22
mmpeter 0:386c250325ce 23 wait(1.0);
mmpeter 0:386c250325ce 24
mmpeter 0:386c250325ce 25 thinggy.sensor_auto_calibrate();
mmpeter 0:386c250325ce 26
mmpeter 0:386c250325ce 27 thinggy.calibrated_sensor(sensor);
mmpeter 0:386c250325ce 28 returned = (sensor[1] + sensor[2] + sensor[3])/3;
mmpeter 0:386c250325ce 29
mmpeter 0:386c250325ce 30
mmpeter 0:386c250325ce 31
mmpeter 0:386c250325ce 32 while(1) {
mmpeter 0:386c250325ce 33 // change "if" to while
mmpeter 0:386c250325ce 34 while(returned < 220){
mmpeter 0:386c250325ce 35 while(sensor[0] < sensor[4] && thinggy.line_position() != 0){
mmpeter 0:386c250325ce 36 thinggy.left_motor(.2);
mmpeter 0:386c250325ce 37 thinggy.right_motor(-.2);
mmpeter 0:386c250325ce 38 thinggy.calibrated_sensor(sensor);
mmpeter 0:386c250325ce 39 }
mmpeter 0:386c250325ce 40 while(sensor[4] > sensor[0] && thinggy.line_position() != 0){
mmpeter 0:386c250325ce 41 thinggy.left_motor(-.2);
mmpeter 0:386c250325ce 42 thinggy.right_motor(.2);
mmpeter 0:386c250325ce 43 thinggy.calibrated_sensor(sensor);
mmpeter 0:386c250325ce 44 }
mmpeter 0:386c250325ce 45 thinggy.calibrated_sensor(sensor);
mmpeter 0:386c250325ce 46 returned = (sensor[1] + sensor[2] + sensor[3])/3;
mmpeter 0:386c250325ce 47 }
mmpeter 0:386c250325ce 48
mmpeter 0:386c250325ce 49 // Curves and straightaways
mmpeter 0:386c250325ce 50 while(returned > 220){
mmpeter 0:386c250325ce 51
mmpeter 0:386c250325ce 52 float position = thinggy.line_position();
mmpeter 0:386c250325ce 53 correction = k*(position);
mmpeter 0:386c250325ce 54
mmpeter 0:386c250325ce 55 // -1.0 is far left, 1.0 is far right, 0.0 in the middle
mmpeter 0:386c250325ce 56
mmpeter 0:386c250325ce 57 //speed limiting for right motor
mmpeter 0:386c250325ce 58 if(speed + correction > 1){
mmpeter 0:386c250325ce 59 thinggy.right_motor(0.6);
mmpeter 0:386c250325ce 60 thinggy.left_motor(speed-correction);
mmpeter 0:386c250325ce 61 }
mmpeter 0:386c250325ce 62 else if(speed - correction > 1){
mmpeter 0:386c250325ce 63 thinggy.right_motor(speed+correction);
mmpeter 0:386c250325ce 64 thinggy.left_motor(0.6);
mmpeter 0:386c250325ce 65 }
mmpeter 0:386c250325ce 66 else{
mmpeter 0:386c250325ce 67 thinggy.left_motor(speed-correction);
mmpeter 0:386c250325ce 68 thinggy.right_motor(speed+correction);
mmpeter 0:386c250325ce 69 }
mmpeter 0:386c250325ce 70 thinggy.calibrated_sensor(sensor);
mmpeter 0:386c250325ce 71 returned = (sensor[1] + sensor[2] + sensor[3])/3;
mmpeter 0:386c250325ce 72 }
mmpeter 0:386c250325ce 73 thinggy.calibrated_sensor(sensor);
mmpeter 0:386c250325ce 74 returned = (sensor[1] + sensor[2] + sensor[3])/3;
mmpeter 0:386c250325ce 75 }
mmpeter 0:386c250325ce 76 }
mmpeter 0:386c250325ce 77
mmpeter 0:386c250325ce 78
mmpeter 0:386c250325ce 79