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:
Sun Jul 28 22:20:12 2019 +0000
Parent:
45:81dd782bc0b4
Child:
47:9773dc14c834
Commit message:
Some minor changes. The overall dead reckoning is working now, but I don't think I did much to fix it.

Changed in this revision

line_tracker.cpp Show annotated file Show diff for this revision Revisions of this file
line_tracker.h 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
--- a/line_tracker.cpp	Sun Jul 28 01:52:34 2019 +0000
+++ b/line_tracker.cpp	Sun Jul 28 22:20:12 2019 +0000
@@ -24,20 +24,20 @@
 
 void LineTracker::updateCalibratedValues()
 {
-    for(uint8_t s = 0; s < LINE_SENSOR_COUNT; s++)
+    for (uint8_t s = 0; s < LINE_SENSOR_COUNT; s++)
     {
         uint16_t calmin = calibratedMinimum[s];
         uint16_t calmax = calibratedMaximum[s];
         uint16_t denominator = calmax - calmin;
         int32_t x = 0;
-        if(denominator != 0)
+        if (denominator != 0)
         {
             x = ((int32_t)rawValues[s] - calmin) * 1000 / denominator;
-            if(x < 0)
+            if (x < 0)
             {
                 x = 0;
             }
-            else if(x > 1000)
+            else if (x > 1000)
             {
                 x = 1000;
             }
@@ -52,7 +52,7 @@
     uint32_t sum = 0;
     
     lineVisible = false;
-    for(uint8_t s = 0; s < LINE_SENSOR_COUNT; s++)
+    for (uint8_t s = 0; s < LINE_SENSOR_COUNT; s++)
     {
         // keep track of whether we see the line at all
         uint16_t value = calibratedValues[s];
@@ -71,15 +71,15 @@
     
     if (lineVisible)
     {
-        linePosition = avg/sum;   
+        linePosition = avg / sum;   
     }
     else
     {
         // We cannot see the line, so just snap the position to the left-most or right-most
         // depending on what we saw previousl.
         
-        const uint32_t max = (LINE_SENSOR_COUNT-1)*1000;
-        if(linePosition < max/2)
+        const uint32_t max = (LINE_SENSOR_COUNT - 1) * 1000;
+        if(linePosition < max / 2)
         {
             linePosition = 0;
         }
--- a/line_tracker.h	Sun Jul 28 01:52:34 2019 +0000
+++ b/line_tracker.h	Sun Jul 28 22:20:12 2019 +0000
@@ -14,9 +14,9 @@
     uint16_t getLinePosition();
     
     uint16_t rawValues[LINE_SENSOR_COUNT];
-    uint16_t calibratedValues[LINE_SENSOR_COUNT];
+    uint16_t calibratedMinimum[LINE_SENSOR_COUNT];
     uint16_t calibratedMaximum[LINE_SENSOR_COUNT];
-    uint16_t calibratedMinimum[LINE_SENSOR_COUNT];
+    uint16_t calibratedValues[LINE_SENSOR_COUNT];
     
     private:
     void readRawValues();
--- a/main.cpp	Sun Jul 28 01:52:34 2019 +0000
+++ b/main.cpp	Sun Jul 28 22:20:12 2019 +0000
@@ -61,31 +61,29 @@
     //testL3gAndShowAverage();
     //testTurnSensor();
     //testReckoner();
+    //testCloseness();  // didn't do it in 2019
     //testDriveHome();
     //testFinalSettleIn();  // doesn't really work
     //testLineSensorsAndCalibrate();
-    testLineFollowing();
-    //testTurnInPlace();
-    //testCloseness();
-    //testLogger();
-
-    loadLineCalibration();
-    doGyroCalibration();
+    //testLineFollowing();
+    //testTurnInPlace();  // didn't do it in 2019
+    //testLogger();       // didn't do it in 2019
 
     doDeadReckoning();
 }
 
 void doDeadReckoning()
 {
+    loadLineCalibration();
+    doGyroCalibration();
+    turnSensor.start();
+
     setLeds(1, 0, 0, 0);
     waitForSignalToStart();
     
     setLeds(0, 1, 0, 0);    
     findLine();
 
-    //setLeds(1, 1, 0, 0);
-    //turnRightToFindLine();
-    
     setLeds(0, 0, 1, 0);
     followLineToEnd();
     
@@ -225,6 +223,7 @@
         updateReckoner();
     }
     reckoner.reset();
+    turnSensor.reset();
     while(button1DefinitelyPressed())
     {
         updateReckoner();
@@ -289,24 +288,6 @@
     }
 }
 
-/**
-void turnRightToFindLine()
-{   
-    while(1)
-    {
-        lineTracker.read();
-        lineTracker.updateCalibration();
-        updateReckonerFromEncoders();
-        
-        if(lineTracker.getLineVisible())
-        {
-            break;   
-        }
-        
-        motorsSpeedSet(300, 100);        
-    }
-}**/
-
 void followLineToEnd()
 {
     Timer timer;
--- a/main.h	Sun Jul 28 01:52:34 2019 +0000
+++ b/main.h	Sun Jul 28 22:20:12 2019 +0000
@@ -10,7 +10,6 @@
 
 void waitForSignalToStart();
 void findLineAndCalibrate(); void findLine();  // two alternatives
-void turnRightToFindLine();
 void followLineToEnd();
 void driveHomeAlmost();
 void finalSettleIn();
--- a/test.cpp	Sun Jul 28 01:52:34 2019 +0000
+++ b/test.cpp	Sun Jul 28 22:20:12 2019 +0000
@@ -108,19 +108,18 @@
 // This also tests the LineTracker by printing out a lot of data from it.
 void testLineFollowing()
 {
+    loadLineCalibration();
     doGyroCalibration();
     turnSensor.start();
 
-    led1 = 1;   
-    while(!button1DefinitelyPressed())
+    led1 = 1;
+    while (!button1DefinitelyPressed())
     {
         updateReckoner();
     }
-    led2 = 1;
     
     Pacer reportPacer(200000);
     
-    loadLineCalibration();
     uint16_t loopCount = 0;
     while(1)
     {
@@ -131,6 +130,10 @@
         
         loopCount += 1;
         
+        led2 = lineTracker.calibratedValues[0] > 500;
+        led3 = lineTracker.calibratedValues[1] > 500;
+        led4 = lineTracker.calibratedValues[2] > 500;
+
         if (lineVisiblePrevious != lineTracker.getLineVisible())
         {
             pc.printf("%5d ! %1d %4d | %5d %5d | %4d %4d %4d\r\n",
@@ -327,30 +330,36 @@
 
 void testLineSensorsAndCalibrate()
 {
-    led1 = 1;
     Pacer reportPacer(100000);
     
     const uint16_t * values = lineTracker.rawValues;
     const uint16_t * min = lineTracker.calibratedMinimum;
     const uint16_t * max = lineTracker.calibratedMaximum;
+    const uint16_t * calValues = lineTracker.calibratedValues;
+
+    // Comment this out, and hold down button 1 while exposing the line sensor
+    // to its typical surfaces to do calibration.
+    loadLineCalibration();
 
     const bool printBarGraph = true;
     while (1)
     {        
         lineTracker.read();
         
-        // Hold down button 1 and expose the line sensor to its typical
-        // values to do calibration.
         if (button1DefinitelyPressed())
         {
-            led2 = 1;
+            led1 = 0;
             lineTracker.updateCalibration();
         }
         else
         {
-            led2 = 0;
+            led1 = 1;
         }
         
+        led2 = calValues[0] > 500;
+        led3 = calValues[1] > 500;
+        led4 = calValues[2] > 500;
+        
         if (reportPacer.pace())
         {
             if (printBarGraph)