Basic Line Following Program motors: p25, p10 inputs: p15, p16 Current settings: lineToSpeedFactor = 10 / 3 speedModifier = -4

Dependencies:   mbed

Fork of RenBuggyLineFollower by Dan Argust

This small program lets a buggy follow a line (black line on white background)

Files at this revision

API Documentation at this revision

Comitter:
DanArgust
Date:
Thu Jul 14 10:55:33 2016 +0000
Parent:
1:fefc1e3f0093
Child:
3:936111f70e37
Commit message:
added 'searching' functionality

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Jul 14 09:11:06 2016 +0000
+++ b/main.cpp	Thu Jul 14 10:55:33 2016 +0000
@@ -6,9 +6,7 @@
 *detect and follow a thin black line.                    *
 *********************************************************/
 
-#include "mbed.h" //"mbed.h" is a library that makes it easier to program microcontrollers
-#include "USBSerial.h"
-//#include "TimedMovement.h" //"TimedMovement.h" contains the functions that we will use to move the buggy
+#include "mbed.h"
 
 AnalogIn ain0(p15);
 AnalogIn ain1(p16);
@@ -20,9 +18,8 @@
 float Rmotor;
 float newLine;
 float oldLine;
-
-//USBSerial USB;
-Ticker timer;
+bool searching;
+int counter;
 
 void getMotorSpeeds(float a,float b){
     float inputs[2] = {a,b};
@@ -30,7 +27,7 @@
     Lmotor = 0;
     Rmotor = 0;
     newLine = 0;
-    float line_to_speed_factor = 5;
+    float line_to_speed_factor = max_speed/0.3;
     for(int i = 0;i<2;++i){
         newLine += inputs[i]*(i+1);
     }
@@ -44,14 +41,24 @@
             newLine = 2;
         else
             newLine = 1;
+        searching = true
+    }
+    if(searching){
+        if(abs(a - b)<0.2){
+            newLine = oldLine;
+        }
+        counter++
+        if(counter>300){
+            newLine = 3 - oldLine;
+            counter = 0;
+        }
+        else{
+            searching = false;
+        }
     }
     oldLine = newLine;
-    /*USB.printf("\033[2J");
-    USB.printf("\033[0;0H");
-    USB.printf("input a = %f\n\rinput b = %f\n\r",a,b);
-    USB.printf("%f\n",line);*/
-    Lmotor = newLine*line_to_speed_factor-6.5;
-    Rmotor = 2-(newLine*line_to_speed_factor-6.5);
+    Lmotor = newLine*line_to_speed_factor-4;
+    Rmotor = 2-(newLine*line_to_speed_factor-4);
     if(Lmotor>max_speed){
         Lmotor = max_speed;
     }
@@ -60,16 +67,10 @@
     }
 }
 
-/*void PrintOutputs(){
-    USB.printf("\033[2J");
-    USB.printf("\033[0;0H");
-    USB.printf("Left  : %f\n\r",Lmotor);
-    USB.printf("Right : %f\n\r",Rmotor);
-}*/
-
 int main()
 {
-    //timer.attach(&PrintOutputs,0.1);
+    searching = false
+    counter = 0
     while(true){
         getMotorSpeeds(ain0.read(),ain1.read());
         pwm0 = Lmotor;