SmartWheels self-driving race car. Designed for NXP Cup. Uses FRDM-KL25Z, area-scan camera, and simple image processing to detect and navigate any NXP spec track.

Dependencies:   TSI USBDevice mbed-dev

Fork of SmartWheels by haofan Zheng

Revision:
46:a5eb9bd3bb55
Parent:
44:15de535c4005
Child:
48:f76b5e252444
--- a/Hardwares/ArduCAM.cpp	Thu Mar 30 03:50:23 2017 +0000
+++ b/Hardwares/ArduCAM.cpp	Thu Mar 30 22:34:20 2017 +0000
@@ -10,20 +10,26 @@
 
 #include <string>
 
-#define CAM_ROI_UPPER_LIMIT 15
 #define CAM_BLK_CAL_LEFT    75
 #define CAM_BLK_CAL_RIGHT   85
 
+//#define CAM_DISP_DEBUG
+#define CAM_DISP_DEBUG_CENTER
+//#define CAM_DISP_IMG
+
+#ifdef __cplusplus
+extern "C" {
+#endif
 
 extern SPI g_spi_port;
 
-DigitalOut cam_cs(PIN_ACC_CS, 1);
+static DigitalOut cam_cs(PIN_ACC_CS, 1);
 
-uint8_t temp_mid_pos = RESOLUTION_WIDTH / 2;
-uint8_t black_calibrate = 70;
-volatile uint8_t centerLine[CAM_ROI_UPPER_LIMIT];
+static uint8_t temp_mid_pos = RESOLUTION_WIDTH / 2;
+static uint8_t black_calibrate = 70;
+static volatile uint8_t centerLine[CAM_ROI_UPPER_LIMIT];
 //volatile const uint8_t* outCenterLine = centerLine;
-Thread * m_imgProcessThread = NULL;
+static Thread * m_imgProcessThread = NULL;
 
 void image_processing();
 void cal_black_calibrate();
@@ -81,7 +87,7 @@
     CamRegBuf * camReg = new CamRegBuf(g_core, CAM_SCCB_WRITE, CAM_SCCB_READ);
 
     camReg->WriteRegSet(ResetProg);
-    wait_ms(5);
+    wait_ms(10);
     camReg->WriteRegSet(QVGA);
     wait_ms(10);
     
@@ -134,7 +140,8 @@
     //sprintf(buf, "Ardu FS1 %#x", tempV);
     //g_core.GetUSBServer().PushReliableMsg('D', buf);
     cal_black_calibrate();
-    m_imgProcessThread = new Thread(callback(image_processing));
+    m_imgProcessThread = new Thread;
+    m_imgProcessThread->start(callback(image_processing));
     
     return true;
 }
@@ -261,7 +268,7 @@
     {
         pixel = static_cast<uint16_t>(ardu_cam_spi_read_8(SINGLE_FIFO_READ)) << 8;
         pixel = pixel | ardu_cam_spi_read_8(SINGLE_FIFO_READ);
-        
+
         if(CAM_BLK_CAL_LEFT < j && j <= CAM_BLK_CAL_RIGHT)
         {
             temp += static_cast<uint8_t>((pixel >> 3) & 0x00FF);
@@ -291,20 +298,34 @@
     {
         pixel = static_cast<uint16_t>(ardu_cam_spi_read_8(SINGLE_FIFO_READ)) << 8;
         pixel = pixel | ardu_cam_spi_read_8(SINGLE_FIFO_READ);
-        
+
         pGreen = static_cast<uint8_t>((pixel >> 3) & 0x00FF);
         if((pGreen < black_calibrate))
         {
             if(j < temp_mid_pos)
             {
                 *left = j;
+#ifdef CAM_DISP_DEBUG
+                ardu_utft_write_DATA(0xF8, 0x00);
+#endif
             }
             else if(!isRightFound)
             {
                 *right = j;
                 isRightFound = 1;
+#ifdef CAM_DISP_DEBUG
+                ardu_utft_write_DATA(0xF8, 0x00);
+#endif
             }
         }
+#ifdef CAM_DISP_DEBUG
+        else
+        {
+            ardu_utft_write_DATA(static_cast<uint8_t>(pixel >> 8), static_cast<uint8_t>(pixel));
+        }
+#elif defined(CAM_DISP_IMG)
+        ardu_utft_write_DATA(static_cast<uint8_t>(pixel >> 8), static_cast<uint8_t>(pixel));
+#endif
     }
 }
 
@@ -327,19 +348,23 @@
         uint8_t rightPos = 0;
         for (uint8_t i = 0; i < CAM_ROI_UPPER_LIMIT; ++i)
         {
+#if defined(CAM_DISP_IMG) or defined(CAM_DISP_DEBUG)
+            ardu_utft_set_camimg_row(CAM_ROI_UPPER_LIMIT - i);
+#endif
             get_img_row_info(0, i, &leftPos, &rightPos);
             temp_mid_pos = (leftPos + rightPos) / 2;
-/*
-#ifdef DEBUG_DISPLAY_TO_SCREEN
+
+#ifdef CAM_DISP_DEBUG_CENTER
             ardu_utft_set_camimg_rowcol(CAM_ROI_UPPER_LIMIT - i, temp_mid_pos);
             ardu_utft_write_DATA(0xF8, 0x00);
 #endif
-*/
+
             centerLine[CAM_ROI_UPPER_LIMIT - i - 1] = temp_mid_pos;
         }
     }
 }
-/*
+
+
 void ardu_cam_display_img_utft()
 {
     ardu_cam_start_capture();
@@ -347,12 +372,26 @@
     while (!(ardu_cam_spi_read_8(ARDUCHIP_TRIG) & CAP_DONE_MASK));
     
     temp_mid_pos = RESOLUTION_WIDTH / 2;
-    cal_black_calibrate();
+    
     uint8_t leftPos = 0;
     uint8_t rightPos = 0;
     for (uint8_t i = 0; i < CAM_ROI_UPPER_LIMIT; ++i)
     {
+        ardu_utft_set_camimg_row(CAM_ROI_UPPER_LIMIT - i);
+        for (uint8_t j = 0; j < RESOLUTION_WIDTH; ++j)
+        {
+            uint8_t VH = ardu_cam_spi_read_8(SINGLE_FIFO_READ);
+            uint8_t VL = ardu_cam_spi_read_8(SINGLE_FIFO_READ);
+            
+            ardu_utft_write_DATA(VH, VL);
+        }
         
     }
 }
-*/
\ No newline at end of file
+
+#ifdef __cplusplus
+}
+#endif
+
+
+