David's dead reckoning code for the LVBots competition on March 6th. Uses the mbed LPC1768, DRV8835, QTR-3RC, and two DC motors with encoders.

Dependencies:   PololuEncoder Pacer mbed GeneralDebouncer

Files at this revision

API Documentation at this revision

Comitter:
DavidEGrayson
Date:
Sat Mar 01 03:13:57 2014 +0000
Parent:
27:2456f68be679
Child:
29:cfcf08d8ac79
Commit message:
Discovered that 4 out of the 6 analog inputs on the mbed are severely messed up. Might need to get a new mbed or do digital filtering (a median of three readings out to work).

Changed in this revision

line_sensors.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
test.cpp Show annotated file Show diff for this revision Revisions of this file
test.h Show annotated file Show diff for this revision Revisions of this file
--- a/line_sensors.cpp	Sat Mar 01 01:46:35 2014 +0000
+++ b/line_sensors.cpp	Sat Mar 01 03:13:57 2014 +0000
@@ -3,5 +3,5 @@
 AnalogIn lineSensorsAnalog[LINE_SENSOR_COUNT] = {
     AnalogIn(p20), // brown wire, left-most sensor
     AnalogIn(p19), // orange wire, middle sensor
-    AnalogIn(p18), // blue wire, right-most sensor
+    AnalogIn(p17), // blue wire, right-most sensor
 };
\ No newline at end of file
--- a/main.cpp	Sat Mar 01 01:46:35 2014 +0000
+++ b/main.cpp	Sat Mar 01 03:13:57 2014 +0000
@@ -44,13 +44,17 @@
     //testDriveHome();
     //testFinalSettleIn();
     //testCalibrate();
+    //testLineFollowing();
+    testAnalog();
 
     // Real routines for the contest.
+    loadCalibration();
+    
     setLeds(1, 0, 0, 0);
     waitForSignalToStart();
     setLeds(0, 1, 0, 0);
     
-    loadCalibrationAndFindLine();
+    findLine();
     //findLineAndCalibrate();
     
     //setLeds(1, 1, 0, 0);
@@ -65,6 +69,16 @@
     while(1){}
 }
 
+void loadCalibration()
+{
+    lineTracker.calibratedMinimum[0] = 34872;
+    lineTracker.calibratedMinimum[1] = 29335;
+    lineTracker.calibratedMinimum[2] = 23845;
+    lineTracker.calibratedMaximum[0] = 59726;
+    lineTracker.calibratedMaximum[1] = 60110;
+    lineTracker.calibratedMaximum[2] = 58446;   
+}
+
 void updateReckonerFromEncoders()
 {
     while(encoderBuffer.hasEvents())
@@ -167,15 +181,27 @@
     motorsSpeedSet(speedLeft, speedRight);   
 }
 
-void loadCalibrationAndFindLine()
+void updateMotorsToFollowLine()
 {
-    lineTracker.calibratedMinimum[0] = 34872;
-    lineTracker.calibratedMinimum[1] = 29335;
-    lineTracker.calibratedMinimum[2] = 23845;
-    lineTracker.calibratedMaximum[0] = 59726;
-    lineTracker.calibratedMaximum[1] = 60110;
-    lineTracker.calibratedMaximum[2] = 58446;
+    const int followLineStrength = 300;
+
+    int16_t speedLeft = drivingSpeed;
+    int16_t speedRight = drivingSpeed;
+    int16_t reduction = (lineTracker.getLinePosition() - 1000) * followLineStrength / 1000;
+    if(reduction < 0)
+    {
+        speedLeft = reduceSpeed(speedLeft, -reduction);   
+    }
+    else
+    {
+        speedRight = reduceSpeed(speedRight, reduction);
+    }
     
+    motorsSpeedSet(speedLeft, speedRight);      
+}
+
+void findLine()
+{   
     GeneralDebouncer lineStatus(10000);
     while(1)
     {
@@ -252,7 +278,6 @@
     
     GeneralDebouncer lineStatus(10000);
     const uint32_t lineDebounceTime = 100000;
-    const int followLineStrength = 300;
     
     while(1)
     {
@@ -263,25 +288,12 @@
         
         bool lostLine = lineStatus.getState() == false &&
           lineStatus.getTimeInCurrentStateMicroseconds() > lineDebounceTime;        
-        if(lostLine && timer.read_us() >= 300000)
+        if(lostLine && timer.read_us() >= 2000000)
         {
             break;   
         }
         
-        int16_t speedLeft = drivingSpeed;
-        int16_t speedRight = drivingSpeed;
-        int16_t reduction = (lineTracker.getLinePosition() - 1500) * followLineStrength / 1500;
-        if(reduction < 0)
-        {
-            speedLeft = reduceSpeed(speedLeft, -reduction);   
-        }
-        else
-        {
-            speedRight = reduceSpeed(speedRight, reduction);
-        }
-        
-        
-        motorsSpeedSet(speedLeft, speedRight);        
+        updateMotorsToFollowLine();     
     }
 }
 
--- a/main.h	Sat Mar 01 01:46:35 2014 +0000
+++ b/main.h	Sat Mar 01 03:13:57 2014 +0000
@@ -3,13 +3,16 @@
 #include "reckoner.h"
 #include "line_tracker.h"
 
+void loadCalibration();
+
 void waitForSignalToStart();
-void findLineAndCalibrate(); void loadCalibrationAndFindLine();  // two alternatives
+void findLineAndCalibrate(); void findLine();  // two alternatives
 void turnRightToFindLine();
 void followLineToEnd();
 void driveHomeAlmost();
 void finalSettleIn();
 
+void updateMotorsToFollowLine();
 void updateReckonerFromEncoders();
 float determinant();
 float dotProduct();
--- a/test.cpp	Sat Mar 01 01:46:35 2014 +0000
+++ b/test.cpp	Sat Mar 01 03:13:57 2014 +0000
@@ -16,6 +16,74 @@
 void __attribute__((noreturn)) infiniteReckonerReportLoop();
 void printBar(const char * name, uint16_t adcResult);
 
+void testAnalog()
+{
+    AnalogIn testInput(p18);
+    
+    uint32_t badCount = 0, goodCount = 0;
+    
+    Pacer reportPacer(1000000);
+    while(1)
+    {
+        uint16_t reading = testInput.read_u16();
+        if(reading > 100)
+        {
+            badCount += 1;
+            pc.printf("%5d %11d %11d\r\n", reading, badCount, goodCount);   
+        }
+        else
+        {
+            goodCount += 1;   
+        }
+        
+        if (reportPacer.pace())
+        {
+            pc.printf("Hello\r\n");   
+        }
+    }
+}
+
+// This also tests the LineTracker by printing out a lot of data from it.
+void testLineFollowing()
+{
+    led1 = 1;   
+    while(!button1DefinitelyPressed())
+    {
+        updateReckonerFromEncoders();
+    }
+    led2 = 1;
+    
+    Pacer reportPacer(200000);
+    
+    loadCalibration();
+    uint16_t loopCount = 0;
+    while(1)
+    {
+        updateReckonerFromEncoders();
+        bool lineVisiblePrevious = lineTracker.getLineVisible();
+        lineTracker.read();
+        updateMotorsToFollowLine();
+        
+        loopCount += 1;
+        
+        if (lineVisiblePrevious != lineTracker.getLineVisible())
+        {
+            pc.printf("%5d ! %1d %4d | %4d %4d %4d | %5d %5d %5d\r\n",
+                loopCount, lineTracker.getLineVisible(), lineTracker.getLinePosition(),
+                lineTracker.calibratedValues[0], lineTracker.calibratedValues[1], lineTracker.calibratedValues[2],
+                lineTracker.rawValues[0], lineTracker.rawValues[1], lineTracker.rawValues[2]
+                );
+        }
+        
+        if (reportPacer.pace())
+        {
+            pc.printf("%5d   %1d %4d | %4d %4d %4d\r\n", loopCount, lineTracker.getLineVisible(), lineTracker.getLinePosition(),
+                lineTracker.calibratedValues[0], lineTracker.calibratedValues[1], lineTracker.calibratedValues[2]
+                );
+        }
+    }
+}
+
 void testDriveHome()
 {
     led1 = 1;   
--- a/test.h	Sat Mar 01 01:46:35 2014 +0000
+++ b/test.h	Sat Mar 01 03:13:57 2014 +0000
@@ -7,4 +7,6 @@
 void __attribute__((noreturn)) testButtons();
 void __attribute__((noreturn)) testDriveHome();
 void __attribute__((noreturn)) testFinalSettleIn();
-void __attribute__((noreturn)) testCalibrate();
\ No newline at end of file
+void __attribute__((noreturn)) testCalibrate();
+void __attribute__((noreturn)) testLineFollowing();
+void __attribute__((noreturn)) testAnalog();
\ No newline at end of file