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 Feb 22 04:44:44 2014 +0000
Parent:
9:9734347b5756
Child:
11:bd14d512340a
Commit message:
Successfully tested the encoders, printing out a nice bar graph.

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
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
test.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/line_sensors.cpp	Sat Feb 22 04:44:44 2014 +0000
@@ -0,0 +1,7 @@
+#include "line_sensors.h"
+
+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
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/line_sensors.h	Sat Feb 22 04:44:44 2014 +0000
@@ -0,0 +1,7 @@
+#pragma once
+
+#include <mbed.h>
+
+#define LINE_SENSOR_COUNT 3
+
+extern AnalogIn lineSensorsAnalog[LINE_SENSOR_COUNT];
\ No newline at end of file
--- a/main.cpp	Sat Feb 22 03:03:37 2014 +0000
+++ b/main.cpp	Sat Feb 22 04:44:44 2014 +0000
@@ -7,7 +7,7 @@
 #include "pc_serial.h"
 #include "test.h"
 
-int main()
+int __attribute__((noreturn)) main()
 {
     pc.baud(115200);
     
@@ -17,7 +17,8 @@
 
     // Test routines
     //testMotors();
-    testEncoders();
+    //testEncoders();
+    testLineSensors();
 
     while(1)
     {
--- a/test.cpp	Sat Feb 22 03:03:37 2014 +0000
+++ b/test.cpp	Sat Feb 22 04:44:44 2014 +0000
@@ -8,11 +8,42 @@
 #include "leds.h"
 #include "encoders.h"
 #include "pc_serial.h"
+#include "line_sensors.h"
+
+void printBar(const char * name, uint16_t adcResult);
+
+void testLineSensors()
+{
+    led1 = 1;
+    Pacer reportPacer(100000);
+    bool const printBarGraph = true;
+    while (1)
+    {
+        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);
+            }
+            else
+            {
+                pc.printf("%8d %8d %8d\n", left, middle, right);
+            }
+        }
+    }
+}
 
 void testEncoders()
 {
     Pacer reportPacer(500000);
-    Pacer blinkPacer(200000);
+    led1 = 1;
     while(1)
     {
         while(encoderBuffer.hasEvents())
@@ -25,12 +56,7 @@
             led2 = 1;
             pc.printf("%8d %8d\n", encoderLeft.getCount(), encoderRight.getCount());
             led2 = 0;
-        }
-        
-        if (blinkPacer.pace())
-        {
-            led1 = !led1;
-        }
+       }
     }
 }
 
@@ -66,4 +92,15 @@
         motorsSpeedSet(1200, 1200);
         wait(2);
     }
+}
+
+void printBar(const char * name, uint16_t adcResult)
+{
+    pc.printf("%-2s %5d |", name, adcResult);
+    uint8_t width = adcResult >> 10;
+    uint8_t i;
+    for(i = 0; i < width; i++){ pc.putc('#'); }
+    for(; i < 63; i++){ pc.putc(' '); }
+    pc.putc('|');
+    pc.putc('\n');
 }
\ No newline at end of file
--- a/test.h	Sat Feb 22 03:03:37 2014 +0000
+++ b/test.h	Sat Feb 22 04:44:44 2014 +0000
@@ -1,4 +1,5 @@
 #pragma once
 
-void testEncoders();
-void testMotors();
\ No newline at end of file
+void __attribute__((noreturn)) testEncoders();
+void __attribute__((noreturn)) testMotors();
+void __attribute__((noreturn)) testLineSensors();
\ No newline at end of file