der Roboter / Mbed 2 deprecated ThursWork

Dependencies:   m3pi_ng mbed

Revision:
1:9848e25989f1
Parent:
0:386c250325ce
--- a/main.cpp	Thu May 22 11:57:29 2014 +0000
+++ b/main.cpp	Thu May 22 14:39:07 2014 +0000
@@ -5,12 +5,12 @@
  
 using namespace std;
  
-m3pi thinggy;
- 
+m3pi thinggy; 
  
 int main() {
     
     float speed = 0.25;
+    float turn_speed = 0.2; 
     float correction;
     float k = -0.3;
     int sensor[5];
@@ -25,54 +25,57 @@
     thinggy.sensor_auto_calibrate();
  
     thinggy.calibrated_sensor(sensor);
+    
+    //find the average of the three sensors 
     returned = (sensor[1] + sensor[2] + sensor[3])/3;
  
-    
-    
     while(1) {
-        // change "if" to while
-        while(returned < 220){
+        
+        //check if it needs to turn  
+        while(returned <= 240){
+                //turns right 
                 while(sensor[0] < sensor[4] && thinggy.line_position() != 0){
-                    thinggy.left_motor(.2);
-                    thinggy.right_motor(-.2);
+                    thinggy.left_motor(turn_speed);
+                    thinggy.right_motor(-turn_speed);
                     thinggy.calibrated_sensor(sensor);
                 }
+                //turns left 
                 while(sensor[4] > sensor[0] && thinggy.line_position() != 0){
-                    thinggy.left_motor(-.2);
-                    thinggy.right_motor(.2);
+                    thinggy.left_motor(-turn_speed);
+                    thinggy.right_motor(turn_speed);
                     thinggy.calibrated_sensor(sensor);
                 }
                 thinggy.calibrated_sensor(sensor);
                 returned = (sensor[1] + sensor[2] + sensor[3])/3;
-        }
+        }//while returned <= 220
         
         // Curves and straightaways    
-        while(returned > 220){
- 
-        float position = thinggy.line_position();
-        correction = k*(position);
+        while(returned > 240){
+            float position = thinggy.line_position();
+            correction = k*(position);
        
-         // -1.0 is far left, 1.0 is far right, 0.0 in the middle
+            // -1.0 is far left, 1.0 is far right, 0.0 in the middle
         
-        //speed limiting for right motor
-        if(speed + correction > 1){
-            thinggy.right_motor(0.6);
-            thinggy.left_motor(speed-correction);
-        } 
-        else if(speed - correction > 1){
-            thinggy.right_motor(speed+correction);
-            thinggy.left_motor(0.6);
-        }
-        else{
-            thinggy.left_motor(speed-correction);
-            thinggy.right_motor(speed+correction);
-        }
-         thinggy.calibrated_sensor(sensor);
-         returned = (sensor[1] + sensor[2] + sensor[3])/3;   
-        }
-    thinggy.calibrated_sensor(sensor);
-    returned = (sensor[1] + sensor[2] + sensor[3])/3;
-    }
+            //speed limiting for right motor
+            if(speed + correction > 1){
+                thinggy.right_motor(0.6);
+                thinggy.left_motor(speed-correction);
+            }    
+            
+            //speed limiting for left motor
+            if(speed - correction > 1){
+                thinggy.left_motor(0.6);
+                thinggy.right_motor(speed+correction);
+            }
+            else{
+                thinggy.left_motor(speed-correction);
+                thinggy.right_motor(speed+correction);
+            }
+            thinggy.calibrated_sensor(sensor);
+            returned = (sensor[1] + sensor[2] + sensor[3])/3;   
+        }//while returned > 220
+        
+    }//while(1)
 }