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:
64:43ab429a37e0
Parent:
63:d9a81b3d69f5
Child:
65:295c222fdf88
diff -r d9a81b3d69f5 -r 43ab429a37e0 Hardwares/ArduCAM.cpp
--- a/Hardwares/ArduCAM.cpp	Sun Apr 09 22:08:34 2017 +0000
+++ b/Hardwares/ArduCAM.cpp	Mon Apr 10 16:44:31 2017 +0000
@@ -14,8 +14,8 @@
 #define CAM_BLK_CAL_ACTIVE
 
 //#define CAM_DISP_DEBUG
-#define CAM_DISP_DEBUG_CENTER
-#define CAM_DISP_IMG
+//#define CAM_DISP_DEBUG_CENTER
+//#define CAM_DISP_IMG
 
 #define IMG_PROC_SIGNAL 0xBB
 
@@ -25,12 +25,14 @@
 
 const static uint8_t CAM_BLK_CAL_LEFT  = ((RESOLUTION_WIDTH / 2) - 1);
 const static uint8_t CAM_BLK_CAL_RIGHT = ((RESOLUTION_WIDTH / 2) + 1);
+const static uint8_t TERMINATE_WIDTH = static_cast<uint8_t>(RESOLUTION_WIDTH * 0.14f);
 
 static DigitalOut cam_cs(PIN_ACC_CS, 1);
 
 static uint8_t temp_mid_pos = RESOLUTION_WIDTH / 2;
 static uint8_t black_calibrate = 70;
 static volatile uint8_t centerLine[CAM_ROI_UPPER_LIMIT];
+static volatile uint8_t is_encounter_terminate = 0;
 //static Thread * m_imgProcessThread = NULL;
 //static Ticker m_tickerImgProc;
 
@@ -357,6 +359,7 @@
         static float calTemp = 0.0f;
         calTemp = 0.0f;
         static uint16_t greenPixel = 0x00;
+        uint8_t isValid = 1;
         for (uint8_t j = 0; j < RESOLUTION_WIDTH; ++j)
         {
             if(CAM_BLK_CAL_LEFT < j && j <= CAM_BLK_CAL_RIGHT)
@@ -364,6 +367,10 @@
                 greenPixel = static_cast<uint16_t>(ardu_cam_spi_read_8(SINGLE_FIFO_READ)) << 8;
                 greenPixel = greenPixel | ardu_cam_spi_read_8(SINGLE_FIFO_READ);
                 greenPixel = (greenPixel & 0x07E0) >> 3;
+                if(static_cast<uint8_t>(greenPixel) < black_calibrate)
+                {
+                    isValid = 0;
+                }
                 calTemp += static_cast<uint8_t>(greenPixel);
             }
             else
@@ -372,8 +379,12 @@
                 ardu_cam_spi_read_8(SINGLE_FIFO_READ);
             }
         }
-        calTemp = calTemp / static_cast<float>(CAM_BLK_CAL_RIGHT - CAM_BLK_CAL_LEFT);
-        black_calibrate = static_cast<uint8_t>(calTemp * 0.7f);
+        
+        if(isValid)
+        {
+            calTemp = calTemp / static_cast<float>(CAM_BLK_CAL_RIGHT - CAM_BLK_CAL_LEFT);
+            black_calibrate = static_cast<uint8_t>(calTemp * 0.7f);
+        }
 #endif
         //cal_black_calibrate();
         uint8_t leftPos = 0;
@@ -393,6 +404,11 @@
 #endif
             get_img_row_info(0, i, &leftPos, &rightPos);
             temp_mid_pos = (leftPos + rightPos) / 2;
+            uint8_t dis_btw_left_right = rightPos - leftPos - 1;
+            if(dis_btw_left_right < TERMINATE_WIDTH)
+            {
+                is_encounter_terminate = 1;
+            }
 
 #ifdef CAM_DISP_DEBUG_CENTER
             ardu_utft_set_camimg_rowcol(CAM_ROI_UPPER_LIMIT - i, temp_mid_pos);
@@ -430,6 +446,11 @@
         
     }
 }
+
+uint8_t ardu_cam_get_is_encounter_terminate()
+{
+    return is_encounter_terminate;
+}
 /*
 void tick_image_proc()
 {