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

Revision:
30:84be2d602dc0
Parent:
29:cfcf08d8ac79
Child:
31:739b91331f31
--- a/test.cpp	Tue Mar 04 00:46:18 2014 +0000
+++ b/test.cpp	Tue Mar 04 02:09:54 2014 +0000
@@ -16,27 +16,102 @@
 void __attribute__((noreturn)) infiniteReckonerReportLoop();
 void printBar(const char * name, uint16_t adcResult);
 
-void testAnalogReadWithFilter()
+uint16_t readPin18()
+{
+    // Set PCADC bit in PCONP register.  (Table 46).
+    LPC_SC->PCONP |= (1 << 12);
+    
+    // Select PCLK_ADC in PCLKSEL0 register. (Table 40, Table 531).
+    LPC_SC->PCLKSEL0 |= (3 << 24);  // PCLK for ADC = CCLK/8 
+    
+    // Enable ADC0 through PINSEL registers.  (Section 8.5).
+    LPC_PINCON->PINSEL1 = (LPC_PINCON->PINSEL1 & ~(3 << 20)) | (1 << 20);
+    
+    // Pin 18: P0.26/AD0.3/AOUT/RXD3
+    
+    // 7:0   Bitmap to select what channel to use
+    // 15:8  Select clock.
+    // 16    BURST = 0
+    // 20:17 Reserved
+    // 21    PDN = 1, A/D converter is operational
+    // 26:24 START = 001 to start conversion now
+    LPC_ADC->ADCR = (1 << 3) | (0xFF << 8) | (1 << 21);
+    LPC_ADC->ADCR |=  (1 << 24);  
+    
+    while(!(LPC_ADC->ADGDR >> 31 & 1))  // while not done
+    {
+    }
+    //return 2;
+    return LPC_ADC->ADGDR & 0xFFFF;
+}
+
+uint16_t readP10()
+{
+    DigitalInOut pin(p10);
+    pin.output();
+    pin = 1;
+    wait_us(20);
+    uint16_t value = 0x8000;
+    Timer timer;
+    timer.start();
+    pin.input();
+    while(timer.read_us() < 0x8000)
+    {
+        if(pin.read() == 0)
+        {
+            return timer.read_us();   
+        }
+    }
+    return value;
+}
+
+void testSensorGlitches()
 {
     AnalogIn testInput(p18);
     Pacer reportPacer(1000000);
     uint32_t badCount = 0, goodCount = 0;
+    pc.printf("hi\r\n");
+    
+    //uint16_t riseCount = 0;
+    uint16_t reading = 0xFF;
+    
     while(1)
     {
-        uint16_t reading = analogReadWithFilter(&testInput);
+        /** This digital filtering did not work
+        {
+            wait(0.01);
+            uint16_t raw = testInput.read_u16();
+            if (raw < reading)
+            {
+                riseCount = 0;
+                reading = raw;
+            }
+            else
+            {
+                riseCount++;
+                if (riseCount == 10)
+                {
+                    riseCount = 0;
+                    reading = raw;   
+                }
+            }
+        }
+        **/
+        reading = readP10();
+        
         if(reading > 100)
         {
             badCount += 1;
-            pc.printf("f %5d %11d %11d\r\n", reading, badCount, goodCount);   
+            //pc.printf("f %5d %11d %11d\r\n", reading, badCount, goodCount);
         }
         else
         {
-            goodCount += 1;   
+            goodCount += 1;
         }
         
         if (reportPacer.pace())
         {
-            pc.printf("Hello\r\n");   
+            pc.printf("h %5d %11d %11d\r\n", reading, badCount, goodCount);
         }
     }
 }