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:
Tue Mar 04 03:04:00 2014 +0000
Parent:
30:84be2d602dc0
Child:
32:83a13b06093c
Commit message:
testLineSensors seems to work fine with QTR-3RC.

Changed in this revision

line_sensors.cpp Show annotated file Show diff for this revision Revisions of this file
line_sensors.h Show annotated file Show diff for this revision Revisions of this file
line_tracker.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
test.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/line_sensors.cpp	Tue Mar 04 02:09:54 2014 +0000
+++ b/line_sensors.cpp	Tue Mar 04 03:04:00 2014 +0000
@@ -1,11 +1,53 @@
 #include "line_sensors.h"
 
+
+/**
 AnalogIn lineSensorsAnalog[LINE_SENSOR_COUNT] = {
     AnalogIn(p20), // brown wire, left-most sensor
     AnalogIn(p19), // orange wire, middle sensor
     AnalogIn(p17), // blue wire, right-most sensor
+};  // TODO: remove
+**/
+
+DigitalInOut lineSensorsDigital[LINE_SENSOR_COUNT] = {
+    DigitalInOut(p18), // brown wire, left-most sensor
+    DigitalInOut(p19), // orange wire, middle sensor
+    DigitalInOut(p20), // blue wire, right-most sensor
 };
 
+void readSensors(uint16_t * values)
+{
+    for(uint8_t i = 0; i < LINE_SENSOR_COUNT; i++)
+    {
+        values[i] = 1000;
+        lineSensorsDigital[i].mode(PullNone);
+        lineSensorsDigital[i].output();
+        lineSensorsDigital[i].write(1);
+    }
+    
+    wait_us(10);
+    
+    Timer timer;
+    timer.start();
+
+    for(uint8_t i = 0; i < LINE_SENSOR_COUNT; i++)
+    {
+        lineSensorsDigital[i].input();
+    }
+
+    while(timer.read_us() < 1000)
+    {
+        for(uint8_t i = 0; i < LINE_SENSOR_COUNT; i++)
+        {
+            if (values[i] == 1000 && lineSensorsDigital[i].read() == 0)
+            {
+                values[i] = timer.read_us();   
+            }
+        }
+    }
+}
+
+
 /**
 uint16_t analogReadWithFilter(AnalogIn * input)
 {
--- a/line_sensors.h	Tue Mar 04 02:09:54 2014 +0000
+++ b/line_sensors.h	Tue Mar 04 03:04:00 2014 +0000
@@ -4,6 +4,8 @@
 
 #define LINE_SENSOR_COUNT 3
 
-extern AnalogIn lineSensorsAnalog[LINE_SENSOR_COUNT];
+//extern AnalogIn lineSensorsAnalog[LINE_SENSOR_COUNT];
 
-uint16_t analogReadWithFilter(AnalogIn * input);
\ No newline at end of file
+uint16_t analogReadWithFilter(AnalogIn * input);
+
+void readSensors(uint16_t * values);
\ No newline at end of file
--- a/line_tracker.cpp	Tue Mar 04 02:09:54 2014 +0000
+++ b/line_tracker.cpp	Tue Mar 04 03:04:00 2014 +0000
@@ -1,10 +1,5 @@
 #include "line_tracker.h"
 
-static uint16_t readSensor(uint8_t index)
-{
-    return lineSensorsAnalog[index].read_u16();
-}
-
 LineTracker::LineTracker()
 {
     for(uint8_t s = 0; s < LINE_SENSOR_COUNT; s++)
@@ -24,10 +19,7 @@
 
 void LineTracker::readRawValues()
 {
-    for(uint8_t s = 0; s < LINE_SENSOR_COUNT; s++)
-    {
-        rawValues[s] = readSensor(s);
-    }
+    readSensors(rawValues);
 }
 
 void LineTracker::updateCalibratedValues()
--- a/main.cpp	Tue Mar 04 02:09:54 2014 +0000
+++ b/main.cpp	Tue Mar 04 03:04:00 2014 +0000
@@ -38,7 +38,7 @@
     // Test routines
     //testMotors();
     //testEncoders();
-    //testLineSensors();
+    testLineSensors();
     //testReckoner();
     //testButtons();
     //testDriveHome();
@@ -46,7 +46,7 @@
     //testCalibrate();
     //testLineFollowing();
     //testAnalog();
-    testSensorGlitches();
+    //testSensorGlitches();
 
     // Real routines for the contest.
     loadCalibration();
--- a/test.cpp	Tue Mar 04 02:09:54 2014 +0000
+++ b/test.cpp	Tue Mar 04 03:04:00 2014 +0000
@@ -48,14 +48,15 @@
 uint16_t readP10()
 {
     DigitalInOut pin(p10);
+    pin.mode(PullNone);
     pin.output();
     pin = 1;
     wait_us(20);
-    uint16_t value = 0x8000;
+    uint16_t value = 1000;
     Timer timer;
     timer.start();
     pin.input();
-    while(timer.read_us() < 0x8000)
+    while(timer.read_us() < 1000)
     {
         if(pin.read() == 0)
         {
@@ -265,25 +266,51 @@
 {
     led1 = 1;
     Pacer reportPacer(100000);
+    Pacer clearStatsPacer(2000000);
+    
+    uint16_t min[LINE_SENSOR_COUNT];
+    uint16_t max[LINE_SENSOR_COUNT];
+    
     bool const printBarGraph = true;
     while (1)
     {
+        if (clearStatsPacer.pace())
+        {
+            for(uint8_t i = 0; i < LINE_SENSOR_COUNT; i++)
+            {
+                min[i] = 0xFFFF;
+                max[i] = 0;
+            }
+        }
+        
+        //values[0] = lineSensorsAnalog[0].read_u16();
+        //values[1] = lineSensorsAnalog[1].read_u16();
+        //values[2] = lineSensorsAnalog[2].read_u16();
+
+        uint16_t values[3];
+        readSensors(values);
+        
+        for(uint8_t i = 0; i < LINE_SENSOR_COUNT; i++)
+        {
+            if (values[i] > max[i]){ max[i] = values[i]; }
+            if (values[i] < min[i]){ min[i] = values[i]; }
+        }
+        
         if (reportPacer.pace())
         {
-            uint16_t left = lineSensorsAnalog[0].read_u16();
-            uint16_t middle = lineSensorsAnalog[1].read_u16();
-            uint16_t right = lineSensorsAnalog[2].read_u16();
-            
             if (printBarGraph)
             {
                 pc.printf("\x1B[0;0H");  // VT100 command for "go to 0,0"
-                printBar("L", left);
-                printBar("M", middle);
-                printBar("R", right);
+                printBar("L", values[0]);
+                printBar("M", values[1]);
+                printBar("R", values[2]);
+                pc.printf("%4d %4d    \r\n", min[0], max[0]);
+                pc.printf("%4d %4d    \r\n", min[1], max[1]);
+                pc.printf("%4d %4d    \r\n", min[2], max[2]);
             }
             else
             {
-                pc.printf("%8d %8d %8d\n", left, middle, right);
+                pc.printf("%8d %8d %8d\r\n", values[0], values[1], values[2]);
             }
         }
     }
@@ -356,7 +383,7 @@
         if(reportPacer.pace())
         {
             led2 = 1;
-            pc.printf("%8d %8d\n", encoderLeft.getCount(), encoderRight.getCount());
+            pc.printf("%8d %8d\r\n", encoderLeft.getCount(), encoderRight.getCount());
             led2 = 0;
        }
     }
@@ -413,13 +440,16 @@
    
 }
 
-void printBar(const char * name, uint16_t adcResult)
+// with should be between 0 and 63
+void printBar(const char * name, uint16_t result)
 {
-    pc.printf("%-2s %5d |", name, adcResult);
-    uint8_t width = adcResult >> 10;
+    pc.printf("%-2s %5d |", name, result);
+    uint16_t width = result >> 4;
+    if (width > 63) { width = 63; }
     uint8_t i;
     for(i = 0; i < width; i++){ pc.putc('#'); }
     for(; i < 63; i++){ pc.putc(' '); }
     pc.putc('|');
+    pc.putc('\r');
     pc.putc('\n');
 }
\ No newline at end of file