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

Committer:
hazheng
Date:
Fri Apr 14 18:44:41 2017 +0000
Revision:
70:311d32a596db
Parent:
65:295c222fdf88
Child:
72:b8f2eebc8912
Fixed the camera not centered issue. Reduce the effect of the corner ratio.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hazheng 28:271fc8445e89 1 #include "ArduCAM.h"
hazheng 28:271fc8445e89 2
hazheng 57:0d8a155d511d 3 //#define SW_DEBUG
hazheng 29:f87d8790f57d 4 #include "GlobalVariable.h"
hazheng 56:7d3395ae022d 5 #include "SWCommon.h"
hazheng 56:7d3395ae022d 6
hazheng 29:f87d8790f57d 7
hazheng 32:5badeff825dc 8 #include "CamRegBuf.h"
hazheng 32:5badeff825dc 9
hazheng 41:7b21c5e3599e 10 #include "ArduUTFT.h"
hazheng 41:7b21c5e3599e 11
hazheng 32:5badeff825dc 12 #include <string>
hazheng 32:5badeff825dc 13
hazheng 57:0d8a155d511d 14 #define CAM_BLK_CAL_ACTIVE
hazheng 44:15de535c4005 15
hazheng 46:a5eb9bd3bb55 16 //#define CAM_DISP_DEBUG
hazheng 64:43ab429a37e0 17 //#define CAM_DISP_DEBUG_CENTER
hazheng 64:43ab429a37e0 18 //#define CAM_DISP_IMG
hazheng 46:a5eb9bd3bb55 19
hazheng 57:0d8a155d511d 20 #define IMG_PROC_SIGNAL 0xBB
hazheng 57:0d8a155d511d 21
hazheng 46:a5eb9bd3bb55 22 #ifdef __cplusplus
hazheng 46:a5eb9bd3bb55 23 extern "C" {
hazheng 46:a5eb9bd3bb55 24 #endif
hazheng 44:15de535c4005 25
hazheng 57:0d8a155d511d 26 const static uint8_t CAM_BLK_CAL_LEFT = ((RESOLUTION_WIDTH / 2) - 1);
hazheng 57:0d8a155d511d 27 const static uint8_t CAM_BLK_CAL_RIGHT = ((RESOLUTION_WIDTH / 2) + 1);
hazheng 70:311d32a596db 28 const static uint8_t TERMINATE_WIDTH = static_cast<uint8_t>(RESOLUTION_WIDTH * 0.28f);
hazheng 57:0d8a155d511d 29
hazheng 46:a5eb9bd3bb55 30 static DigitalOut cam_cs(PIN_ACC_CS, 1);
hazheng 29:f87d8790f57d 31
hazheng 46:a5eb9bd3bb55 32 static uint8_t temp_mid_pos = RESOLUTION_WIDTH / 2;
hazheng 46:a5eb9bd3bb55 33 static uint8_t black_calibrate = 70;
hazheng 65:295c222fdf88 34 static volatile uint8_t centerLine[CAM_ROI_UPPER_LIMIT * 2];
hazheng 64:43ab429a37e0 35 static volatile uint8_t is_encounter_terminate = 0;
hazheng 63:d9a81b3d69f5 36 //static Thread * m_imgProcessThread = NULL;
hazheng 63:d9a81b3d69f5 37 //static Ticker m_tickerImgProc;
hazheng 44:15de535c4005 38
hazheng 57:0d8a155d511d 39 //void image_processing();
hazheng 44:15de535c4005 40 void cal_black_calibrate();
hazheng 63:d9a81b3d69f5 41 //void tick_image_proc();
hazheng 34:f79db3bc2f86 42
hazheng 34:f79db3bc2f86 43 inline void ardu_cam_spi_write_8(int address, int value)
hazheng 29:f87d8790f57d 44 {
hazheng 34:f79db3bc2f86 45 // take the SS pin low to select the chip:
hazheng 56:7d3395ae022d 46 #ifdef SW_DEBUG
hazheng 56:7d3395ae022d 47 g_sw_spi_lock.lock();
hazheng 56:7d3395ae022d 48 #endif
hazheng 34:f79db3bc2f86 49 cam_cs = 0;
hazheng 34:f79db3bc2f86 50 // send in the address and value via SPI:
hazheng 34:f79db3bc2f86 51 g_spi_port.write(address | 0x80);
hazheng 34:f79db3bc2f86 52 g_spi_port.write(value);
hazheng 34:f79db3bc2f86 53 // take the SS pin high to de-select the chip:
hazheng 34:f79db3bc2f86 54 cam_cs = 1;
hazheng 56:7d3395ae022d 55 #ifdef SW_DEBUG
hazheng 56:7d3395ae022d 56 g_sw_spi_lock.unlock();
hazheng 56:7d3395ae022d 57 #endif
hazheng 34:f79db3bc2f86 58 }
hazheng 34:f79db3bc2f86 59
hazheng 34:f79db3bc2f86 60 inline uint8_t ardu_cam_spi_read_8(int address)
hazheng 34:f79db3bc2f86 61 {
hazheng 34:f79db3bc2f86 62 // take the SS pin low to select the chip:
hazheng 56:7d3395ae022d 63 #ifdef SW_DEBUG
hazheng 56:7d3395ae022d 64 g_sw_spi_lock.lock();
hazheng 56:7d3395ae022d 65 #endif
hazheng 29:f87d8790f57d 66 cam_cs = 0;
hazheng 34:f79db3bc2f86 67 // send in the address and value via SPI:
hazheng 34:f79db3bc2f86 68 g_spi_port.write(address & 0x7F);
hazheng 34:f79db3bc2f86 69 uint8_t value = static_cast<uint8_t>(g_spi_port.write(0x00));
hazheng 34:f79db3bc2f86 70 // take the SS pin high to de-select the chip:
hazheng 29:f87d8790f57d 71 cam_cs = 1;
hazheng 56:7d3395ae022d 72 #ifdef SW_DEBUG
hazheng 56:7d3395ae022d 73 g_sw_spi_lock.unlock();
hazheng 56:7d3395ae022d 74 #endif
hazheng 34:f79db3bc2f86 75 return value;
hazheng 34:f79db3bc2f86 76 }
hazheng 34:f79db3bc2f86 77 /*
hazheng 34:f79db3bc2f86 78 inline void ardu_cam_spi_set_burst()
hazheng 34:f79db3bc2f86 79 {
hazheng 34:f79db3bc2f86 80 cam_cs = 0;
hazheng 34:f79db3bc2f86 81
hazheng 34:f79db3bc2f86 82 g_spi_port.write(BURST_FIFO_READ & 0x7F);
hazheng 29:f87d8790f57d 83
hazheng 34:f79db3bc2f86 84 cam_cs = 1;
hazheng 34:f79db3bc2f86 85 }
hazheng 34:f79db3bc2f86 86
hazheng 34:f79db3bc2f86 87 inline uint16_t ardu_cam_spi_burst_read_16()
hazheng 34:f79db3bc2f86 88 {
hazheng 34:f79db3bc2f86 89 cam_cs = 0;
hazheng 29:f87d8790f57d 90
hazheng 34:f79db3bc2f86 91 uint16_t value = static_cast<uint16_t>(g_spi_port.write(0x00));
hazheng 34:f79db3bc2f86 92 value = (value << 8) & 0xFF00;
hazheng 34:f79db3bc2f86 93 value = value | static_cast<uint16_t>(g_spi_port.write(0x00));
hazheng 34:f79db3bc2f86 94
hazheng 34:f79db3bc2f86 95 cam_cs = 1;
hazheng 34:f79db3bc2f86 96 return value;
hazheng 29:f87d8790f57d 97 }
hazheng 29:f87d8790f57d 98 */
hazheng 29:f87d8790f57d 99
hazheng 29:f87d8790f57d 100
hazheng 28:271fc8445e89 101 bool ardu_cam_init()
hazheng 28:271fc8445e89 102 {
hazheng 56:7d3395ae022d 103 //char buf[20];
hazheng 32:5badeff825dc 104
hazheng 36:7e747e19f660 105 CamRegBuf * camReg = new CamRegBuf(g_core, CAM_SCCB_WRITE, CAM_SCCB_READ);
hazheng 36:7e747e19f660 106
hazheng 36:7e747e19f660 107 camReg->WriteRegSet(ResetProg);
hazheng 46:a5eb9bd3bb55 108 wait_ms(10);
hazheng 37:7074a6118d03 109 camReg->WriteRegSet(QVGA);
hazheng 44:15de535c4005 110 wait_ms(10);
hazheng 32:5badeff825dc 111
hazheng 36:7e747e19f660 112 #if defined(ARDUCAM_OV2640)
hazheng 32:5badeff825dc 113 camReg->SCCBWrite(0xff, 0x01);
hazheng 36:7e747e19f660 114 #endif
Bobymicjohn 54:f1f5648dfacf 115 //uint8_t camVer = camReg->SCCBRead(CAM_PID_ADDR);
Bobymicjohn 54:f1f5648dfacf 116 //sprintf(buf, "Cam VerH %#x", camVer);
Bobymicjohn 54:f1f5648dfacf 117 //g_core.GetUSBServer().PushReliableMsg('D', buf);
hazheng 32:5badeff825dc 118
Bobymicjohn 54:f1f5648dfacf 119 //camVer = camReg->SCCBRead(CAM_VER_ADDR);
Bobymicjohn 54:f1f5648dfacf 120 //sprintf(buf, "Cam VerL %#x", camVer);
Bobymicjohn 54:f1f5648dfacf 121 //g_core.GetUSBServer().PushReliableMsg('D', buf);
hazheng 36:7e747e19f660 122
hazheng 36:7e747e19f660 123
hazheng 32:5badeff825dc 124 delete camReg;
hazheng 32:5badeff825dc 125 camReg = NULL;
hazheng 32:5badeff825dc 126
hazheng 32:5badeff825dc 127
hazheng 32:5badeff825dc 128
hazheng 32:5badeff825dc 129
hazheng 34:f79db3bc2f86 130 uint8_t VerNum = ardu_cam_spi_read_8(0x40);
hazheng 34:f79db3bc2f86 131 VerNum = ardu_cam_spi_read_8(0x40);
hazheng 29:f87d8790f57d 132
hazheng 32:5badeff825dc 133
Bobymicjohn 54:f1f5648dfacf 134 //sprintf(buf, "Ardu Ver %#x", VerNum);
Bobymicjohn 54:f1f5648dfacf 135 //g_core.GetUSBServer().PushReliableMsg('D', buf);
hazheng 29:f87d8790f57d 136
hazheng 34:f79db3bc2f86 137 ardu_cam_spi_write_8(ARDUCHIP_TEST1, ARDUCHIP_TEST_MSG);
hazheng 34:f79db3bc2f86 138 uint8_t testV = ardu_cam_spi_read_8(ARDUCHIP_TEST1);
hazheng 35:ac4fcca21560 139 if(VerNum != ARDUCHIP_VER_NUM || testV != ARDUCHIP_TEST_MSG)
hazheng 29:f87d8790f57d 140 {
Bobymicjohn 54:f1f5648dfacf 141 //g_core.GetUSBServer().PushReliableMsg('D', "CameraInit Fa");
hazheng 29:f87d8790f57d 142 return false;
hazheng 29:f87d8790f57d 143 }
Bobymicjohn 54:f1f5648dfacf 144 //g_core.GetUSBServer().PushReliableMsg('D', "CameraInit Su");
hazheng 29:f87d8790f57d 145
hazheng 35:ac4fcca21560 146 //ardu_cam_set_mode(MCU2LCD_MODE);
hazheng 43:0d1886f4848a 147 ardu_cam_spi_write_8(ARDUCHIP_CAP_CTRL, 0x00);
hazheng 44:15de535c4005 148 //ardu_cam_start_capture();
hazheng 29:f87d8790f57d 149
hazheng 28:271fc8445e89 150
hazheng 32:5badeff825dc 151 //unsigned char tempV = ardu_cam_read_reg(ARDUCHIP_MODE);
hazheng 32:5badeff825dc 152 //sprintf(buf, "Ardu Stat %#x", tempV);
hazheng 32:5badeff825dc 153 //g_core.GetUSBServer().PushReliableMsg('D', buf);
hazheng 29:f87d8790f57d 154
hazheng 32:5badeff825dc 155 //tempV = ardu_cam_read_reg(ARDUCHIP_CAP_CTRL);
hazheng 32:5badeff825dc 156 //sprintf(buf, "Ardu FS1 %#x", tempV);
hazheng 32:5badeff825dc 157 //g_core.GetUSBServer().PushReliableMsg('D', buf);
hazheng 44:15de535c4005 158 cal_black_calibrate();
hazheng 57:0d8a155d511d 159 //m_imgProcessThread = new Thread(osPriorityRealtime); //osPriorityAboveNormal //osPriorityHigh //osPriorityRealtime
hazheng 57:0d8a155d511d 160 //m_imgProcessThread->set_priority(osPriorityRealtime);
hazheng 57:0d8a155d511d 161 //m_imgProcessThread->start(callback(image_processing));
hazheng 57:0d8a155d511d 162 //m_tickerImgProc.attach(&tick_image_proc, 0.1f);
hazheng 29:f87d8790f57d 163
hazheng 29:f87d8790f57d 164 return true;
hazheng 29:f87d8790f57d 165 }
hazheng 29:f87d8790f57d 166
hazheng 29:f87d8790f57d 167 void ardu_cam_start_capture()
hazheng 29:f87d8790f57d 168 {
hazheng 34:f79db3bc2f86 169 ardu_cam_spi_write_8(ARDUCHIP_FIFO, FIFO_CLEAR_MASK);
hazheng 48:f76b5e252444 170
hazheng 34:f79db3bc2f86 171 ardu_cam_spi_write_8(ARDUCHIP_FIFO, FIFO_START_MASK);
hazheng 29:f87d8790f57d 172 }
hazheng 29:f87d8790f57d 173
hazheng 32:5badeff825dc 174 uint32_t ardu_cam_get_fifo_length()
hazheng 32:5badeff825dc 175 {
hazheng 32:5badeff825dc 176 uint32_t len1,len2,len3,length=0;
hazheng 34:f79db3bc2f86 177 len1 = ardu_cam_spi_read_8(FIFO_SIZE1);
hazheng 34:f79db3bc2f86 178 len2 = ardu_cam_spi_read_8(FIFO_SIZE2);
hazheng 34:f79db3bc2f86 179 len3 = ardu_cam_spi_read_8(FIFO_SIZE3) & 0x07;
hazheng 32:5badeff825dc 180 length = ((len3 << 16) | (len2 << 8) | len1) & 0x07ffff;
hazheng 32:5badeff825dc 181 return length;
hazheng 32:5badeff825dc 182 }
hazheng 32:5badeff825dc 183
hazheng 32:5badeff825dc 184 uint8_t ardu_cam_get_pixel()
hazheng 32:5badeff825dc 185 {
hazheng 34:f79db3bc2f86 186 uint16_t VH = ardu_cam_spi_read_8(SINGLE_FIFO_READ);
hazheng 34:f79db3bc2f86 187 uint16_t VL = ardu_cam_spi_read_8(SINGLE_FIFO_READ);
hazheng 34:f79db3bc2f86 188 //uint16_t VL = ardu_cam_spi_burst_read_16();
hazheng 32:5badeff825dc 189
hazheng 32:5badeff825dc 190 VL = (VL & 0x00FF) | ((VH << 8) & 0xFF00);
hazheng 33:e3fcc4d6bb9b 191 uint8_t ch = ((VL & 0xF800) >> 9);// << 2;
hazheng 33:e3fcc4d6bb9b 192 float pixel = (static_cast<float>(ch) * 0.21);
hazheng 32:5badeff825dc 193
hazheng 33:e3fcc4d6bb9b 194 ch = ((VL & 0x07E0) >> 3);// << 2;
hazheng 33:e3fcc4d6bb9b 195 pixel += (static_cast<float>(ch) * 0.72);
hazheng 32:5badeff825dc 196
hazheng 33:e3fcc4d6bb9b 197 ch = (VL & 0x001F) << 2;
hazheng 33:e3fcc4d6bb9b 198 pixel += (static_cast<float>(ch) * 0.07);
hazheng 32:5badeff825dc 199
hazheng 32:5badeff825dc 200 return static_cast<uint8_t>(pixel);
hazheng 32:5badeff825dc 201 }
hazheng 32:5badeff825dc 202
hazheng 32:5badeff825dc 203 void ardu_cam_print_debug()
hazheng 32:5badeff825dc 204 {
hazheng 32:5badeff825dc 205 uint32_t len = ardu_cam_get_fifo_length();
Bobymicjohn 54:f1f5648dfacf 206 //char buf[20];
Bobymicjohn 54:f1f5648dfacf 207 //sprintf(buf, "Cam L %#x", len);
Bobymicjohn 54:f1f5648dfacf 208 //g_core.GetUSBServer().PushReliableMsg('D', buf);
hazheng 32:5badeff825dc 209
hazheng 33:e3fcc4d6bb9b 210 //if(len < (RESOLUTION_HEIGHT * RESOLUTION_WIDTH * 2))
hazheng 33:e3fcc4d6bb9b 211 // return;
hazheng 33:e3fcc4d6bb9b 212
hazheng 33:e3fcc4d6bb9b 213 //Begin output the picture
hazheng 32:5badeff825dc 214 std::string lineBuf;
hazheng 37:7074a6118d03 215 #if defined(MANUAL_REDUCE_RESULOTION_BY2)
hazheng 37:7074a6118d03 216 lineBuf.resize((RESOLUTION_WIDTH / 2) + 1);
hazheng 37:7074a6118d03 217 #else
hazheng 33:e3fcc4d6bb9b 218 lineBuf.resize(RESOLUTION_WIDTH + 1);
hazheng 37:7074a6118d03 219 #endif
hazheng 32:5badeff825dc 220
hazheng 34:f79db3bc2f86 221 //ardu_cam_spi_set_burst();
hazheng 41:7b21c5e3599e 222
hazheng 42:c4e1606087ff 223 //ardu_cam_get_pixel(); //Get the first dummy pixel
hazheng 41:7b21c5e3599e 224
hazheng 43:0d1886f4848a 225
hazheng 33:e3fcc4d6bb9b 226 for (uint8_t i = 0; i < RESOLUTION_HEIGHT; ++i)
hazheng 32:5badeff825dc 227 {
hazheng 37:7074a6118d03 228 #if defined(MANUAL_REDUCE_RESULOTION_BY2)
hazheng 37:7074a6118d03 229 lineBuf[0] = i / 2;
hazheng 37:7074a6118d03 230
hazheng 37:7074a6118d03 231 for (int j = 0; j < RESOLUTION_WIDTH; ++j)
hazheng 37:7074a6118d03 232 {
hazheng 37:7074a6118d03 233 if(i % 2 == 0 || j % 2 == 0)
hazheng 37:7074a6118d03 234 {
hazheng 37:7074a6118d03 235 ardu_cam_get_pixel();
hazheng 37:7074a6118d03 236 }
hazheng 37:7074a6118d03 237 else
hazheng 37:7074a6118d03 238 {
hazheng 37:7074a6118d03 239 lineBuf[(j / 2) + 1] = ardu_cam_get_pixel();
hazheng 37:7074a6118d03 240 }
hazheng 37:7074a6118d03 241 }
hazheng 37:7074a6118d03 242
hazheng 37:7074a6118d03 243 if(i % 2 == 0)
hazheng 37:7074a6118d03 244 {
hazheng 37:7074a6118d03 245
hazheng 37:7074a6118d03 246 }
hazheng 37:7074a6118d03 247 else
hazheng 37:7074a6118d03 248 {
hazheng 37:7074a6118d03 249 g_core.GetUSBServer().PushReliableMsg('P', lineBuf);
hazheng 37:7074a6118d03 250 wait(0.35);
hazheng 37:7074a6118d03 251 }
hazheng 37:7074a6118d03 252
hazheng 37:7074a6118d03 253 #else
hazheng 37:7074a6118d03 254
hazheng 32:5badeff825dc 255 lineBuf[0] = i;
hazheng 32:5badeff825dc 256
hazheng 33:e3fcc4d6bb9b 257 for (int j = 0; j < RESOLUTION_WIDTH; ++j)
hazheng 32:5badeff825dc 258 {
hazheng 33:e3fcc4d6bb9b 259 //uint8_t p = ardu_cam_get_pixel();
hazheng 33:e3fcc4d6bb9b 260 lineBuf[j + 1] = ardu_cam_get_pixel();
hazheng 32:5badeff825dc 261 }
hazheng 32:5badeff825dc 262
Bobymicjohn 54:f1f5648dfacf 263 //g_core.GetUSBServer().PushReliableMsg('P', lineBuf);
hazheng 35:ac4fcca21560 264 wait(0.35);
hazheng 37:7074a6118d03 265
hazheng 37:7074a6118d03 266 #endif
hazheng 37:7074a6118d03 267
hazheng 32:5badeff825dc 268 }
hazheng 43:0d1886f4848a 269
hazheng 43:0d1886f4848a 270 }
hazheng 43:0d1886f4848a 271
hazheng 44:15de535c4005 272 uint8_t ardu_cam_is_capture_finished()
hazheng 44:15de535c4005 273 {
hazheng 44:15de535c4005 274 return (ardu_cam_spi_read_8(ARDUCHIP_TRIG) & CAP_DONE_MASK);
hazheng 44:15de535c4005 275 }
hazheng 44:15de535c4005 276
hazheng 44:15de535c4005 277 void cal_black_calibrate()
hazheng 44:15de535c4005 278 {
hazheng 44:15de535c4005 279 ardu_cam_start_capture();
hazheng 44:15de535c4005 280
hazheng 44:15de535c4005 281 while (!(ardu_cam_spi_read_8(ARDUCHIP_TRIG) & CAP_DONE_MASK));
hazheng 44:15de535c4005 282
hazheng 44:15de535c4005 283 float temp = 0.0f;
hazheng 44:15de535c4005 284 static uint16_t pixel = 0x00;
hazheng 44:15de535c4005 285 for (uint8_t j = 0; j < RESOLUTION_WIDTH; ++j)
hazheng 44:15de535c4005 286 {
hazheng 44:15de535c4005 287 pixel = static_cast<uint16_t>(ardu_cam_spi_read_8(SINGLE_FIFO_READ)) << 8;
hazheng 44:15de535c4005 288 pixel = pixel | ardu_cam_spi_read_8(SINGLE_FIFO_READ);
hazheng 46:a5eb9bd3bb55 289
hazheng 44:15de535c4005 290 if(CAM_BLK_CAL_LEFT < j && j <= CAM_BLK_CAL_RIGHT)
hazheng 44:15de535c4005 291 {
hazheng 44:15de535c4005 292 temp += static_cast<uint8_t>((pixel >> 3) & 0x00FF);
hazheng 44:15de535c4005 293 }
hazheng 44:15de535c4005 294 }
hazheng 44:15de535c4005 295 temp = temp / (CAM_BLK_CAL_RIGHT - CAM_BLK_CAL_LEFT);
hazheng 44:15de535c4005 296 black_calibrate = temp * 0.7f;
hazheng 44:15de535c4005 297 }
hazheng 44:15de535c4005 298
hazheng 65:295c222fdf88 299 inline void get_img_row_info(const uint8_t display, const uint8_t rowI, uint8_t * left, uint8_t * right, uint8_t* isBorderFound)
hazheng 44:15de535c4005 300 {
hazheng 44:15de535c4005 301 *left = 0;
hazheng 44:15de535c4005 302 *right = RESOLUTION_WIDTH;
hazheng 65:295c222fdf88 303 *isBorderFound = BOTH_NOT_FOUND;
hazheng 44:15de535c4005 304 uint8_t isRightFound = 0;
hazheng 44:15de535c4005 305 static uint16_t pixel = 0x0000;
hazheng 44:15de535c4005 306 static uint8_t pGreen = 0x00;
hazheng 44:15de535c4005 307
hazheng 44:15de535c4005 308 for (uint8_t j = 0; j < RESOLUTION_WIDTH; ++j)
hazheng 44:15de535c4005 309 {
hazheng 44:15de535c4005 310 pixel = static_cast<uint16_t>(ardu_cam_spi_read_8(SINGLE_FIFO_READ)) << 8;
hazheng 44:15de535c4005 311 pixel = pixel | ardu_cam_spi_read_8(SINGLE_FIFO_READ);
hazheng 46:a5eb9bd3bb55 312
hazheng 57:0d8a155d511d 313 pGreen = static_cast<uint8_t>((pixel & 0x07E0) >> 3);
hazheng 44:15de535c4005 314 if((pGreen < black_calibrate))
hazheng 44:15de535c4005 315 {
hazheng 44:15de535c4005 316 if(j < temp_mid_pos)
hazheng 44:15de535c4005 317 {
hazheng 65:295c222fdf88 318 *isBorderFound = LEFT_FOUND;
hazheng 44:15de535c4005 319 *left = j;
hazheng 46:a5eb9bd3bb55 320 #ifdef CAM_DISP_DEBUG
hazheng 46:a5eb9bd3bb55 321 ardu_utft_write_DATA(0xF8, 0x00);
hazheng 46:a5eb9bd3bb55 322 #endif
hazheng 44:15de535c4005 323 }
hazheng 44:15de535c4005 324 else if(!isRightFound)
hazheng 44:15de535c4005 325 {
hazheng 44:15de535c4005 326 *right = j;
hazheng 44:15de535c4005 327 isRightFound = 1;
hazheng 46:a5eb9bd3bb55 328 #ifdef CAM_DISP_DEBUG
hazheng 46:a5eb9bd3bb55 329 ardu_utft_write_DATA(0xF8, 0x00);
hazheng 46:a5eb9bd3bb55 330 #endif
hazheng 44:15de535c4005 331 }
hazheng 44:15de535c4005 332 }
hazheng 46:a5eb9bd3bb55 333 #ifdef CAM_DISP_DEBUG
hazheng 46:a5eb9bd3bb55 334 else
hazheng 46:a5eb9bd3bb55 335 {
hazheng 46:a5eb9bd3bb55 336 ardu_utft_write_DATA(static_cast<uint8_t>(pixel >> 8), static_cast<uint8_t>(pixel));
hazheng 46:a5eb9bd3bb55 337 }
hazheng 50:c387c88141fb 338
hazheng 46:a5eb9bd3bb55 339 #elif defined(CAM_DISP_IMG)
hazheng 46:a5eb9bd3bb55 340 ardu_utft_write_DATA(static_cast<uint8_t>(pixel >> 8), static_cast<uint8_t>(pixel));
hazheng 46:a5eb9bd3bb55 341 #endif
hazheng 44:15de535c4005 342 }
hazheng 65:295c222fdf88 343
hazheng 65:295c222fdf88 344 if(*isBorderFound == LEFT_FOUND && isRightFound)
hazheng 65:295c222fdf88 345 {
hazheng 65:295c222fdf88 346 *isBorderFound = BOTH_FOUND;
hazheng 65:295c222fdf88 347 }
hazheng 65:295c222fdf88 348 else if(isRightFound)
hazheng 65:295c222fdf88 349 {
hazheng 65:295c222fdf88 350 *isBorderFound = RIGHT_FOUND;
hazheng 65:295c222fdf88 351 }
hazheng 65:295c222fdf88 352
hazheng 44:15de535c4005 353 }
hazheng 44:15de535c4005 354
hazheng 44:15de535c4005 355 volatile const uint8_t* ardu_cam_get_center_array()
hazheng 44:15de535c4005 356 {
hazheng 44:15de535c4005 357 return centerLine;
hazheng 44:15de535c4005 358 }
hazheng 44:15de535c4005 359
hazheng 44:15de535c4005 360 void image_processing()
hazheng 44:15de535c4005 361 {
hazheng 63:d9a81b3d69f5 362
hazheng 57:0d8a155d511d 363 //while(true)
hazheng 44:15de535c4005 364 {
hazheng 44:15de535c4005 365 ardu_cam_start_capture();
hazheng 44:15de535c4005 366
hazheng 44:15de535c4005 367 while (!(ardu_cam_spi_read_8(ARDUCHIP_TRIG) & CAP_DONE_MASK));
hazheng 44:15de535c4005 368
hazheng 44:15de535c4005 369 temp_mid_pos = RESOLUTION_WIDTH / 2;
hazheng 57:0d8a155d511d 370 #ifdef CAM_BLK_CAL_ACTIVE
hazheng 57:0d8a155d511d 371 static float calTemp = 0.0f;
hazheng 57:0d8a155d511d 372 calTemp = 0.0f;
hazheng 57:0d8a155d511d 373 static uint16_t greenPixel = 0x00;
hazheng 64:43ab429a37e0 374 uint8_t isValid = 1;
hazheng 57:0d8a155d511d 375 for (uint8_t j = 0; j < RESOLUTION_WIDTH; ++j)
hazheng 57:0d8a155d511d 376 {
hazheng 57:0d8a155d511d 377 if(CAM_BLK_CAL_LEFT < j && j <= CAM_BLK_CAL_RIGHT)
hazheng 57:0d8a155d511d 378 {
hazheng 57:0d8a155d511d 379 greenPixel = static_cast<uint16_t>(ardu_cam_spi_read_8(SINGLE_FIFO_READ)) << 8;
hazheng 57:0d8a155d511d 380 greenPixel = greenPixel | ardu_cam_spi_read_8(SINGLE_FIFO_READ);
hazheng 57:0d8a155d511d 381 greenPixel = (greenPixel & 0x07E0) >> 3;
hazheng 64:43ab429a37e0 382 if(static_cast<uint8_t>(greenPixel) < black_calibrate)
hazheng 64:43ab429a37e0 383 {
hazheng 64:43ab429a37e0 384 isValid = 0;
hazheng 64:43ab429a37e0 385 }
hazheng 57:0d8a155d511d 386 calTemp += static_cast<uint8_t>(greenPixel);
hazheng 57:0d8a155d511d 387 }
hazheng 57:0d8a155d511d 388 else
hazheng 57:0d8a155d511d 389 {
hazheng 57:0d8a155d511d 390 ardu_cam_spi_read_8(SINGLE_FIFO_READ);
hazheng 57:0d8a155d511d 391 ardu_cam_spi_read_8(SINGLE_FIFO_READ);
hazheng 57:0d8a155d511d 392 }
hazheng 57:0d8a155d511d 393 }
hazheng 64:43ab429a37e0 394
hazheng 64:43ab429a37e0 395 if(isValid)
hazheng 64:43ab429a37e0 396 {
hazheng 64:43ab429a37e0 397 calTemp = calTemp / static_cast<float>(CAM_BLK_CAL_RIGHT - CAM_BLK_CAL_LEFT);
hazheng 64:43ab429a37e0 398 black_calibrate = static_cast<uint8_t>(calTemp * 0.7f);
hazheng 64:43ab429a37e0 399 }
hazheng 57:0d8a155d511d 400 #endif
hazheng 44:15de535c4005 401 //cal_black_calibrate();
hazheng 44:15de535c4005 402 uint8_t leftPos = 0;
hazheng 44:15de535c4005 403 uint8_t rightPos = 0;
hazheng 65:295c222fdf88 404 uint8_t isBorderFound = BOTH_NOT_FOUND;
hazheng 65:295c222fdf88 405 //is_encounter_terminate = 0;
hazheng 44:15de535c4005 406 for (uint8_t i = 0; i < CAM_ROI_UPPER_LIMIT; ++i)
hazheng 57:0d8a155d511d 407 /*{
hazheng 57:0d8a155d511d 408 for (uint8_t j = 0; j < RESOLUTION_WIDTH; ++j)
hazheng 57:0d8a155d511d 409 {
hazheng 57:0d8a155d511d 410 ardu_cam_spi_read_8(SINGLE_FIFO_READ);
hazheng 57:0d8a155d511d 411 ardu_cam_spi_read_8(SINGLE_FIFO_READ);
hazheng 57:0d8a155d511d 412 }
hazheng 57:0d8a155d511d 413 }*/
hazheng 57:0d8a155d511d 414 //for(uint8_t i = CAM_ROI_UPPER_LIMIT; i < (2 * CAM_ROI_UPPER_LIMIT); ++i)
hazheng 44:15de535c4005 415 {
hazheng 46:a5eb9bd3bb55 416 #if defined(CAM_DISP_IMG) or defined(CAM_DISP_DEBUG)
hazheng 46:a5eb9bd3bb55 417 ardu_utft_set_camimg_row(CAM_ROI_UPPER_LIMIT - i);
hazheng 46:a5eb9bd3bb55 418 #endif
hazheng 65:295c222fdf88 419 get_img_row_info(0, i, &leftPos, &rightPos, &isBorderFound);
hazheng 44:15de535c4005 420 temp_mid_pos = (leftPos + rightPos) / 2;
hazheng 64:43ab429a37e0 421 uint8_t dis_btw_left_right = rightPos - leftPos - 1;
hazheng 64:43ab429a37e0 422 if(dis_btw_left_right < TERMINATE_WIDTH)
hazheng 64:43ab429a37e0 423 {
hazheng 64:43ab429a37e0 424 is_encounter_terminate = 1;
hazheng 64:43ab429a37e0 425 }
hazheng 46:a5eb9bd3bb55 426
hazheng 46:a5eb9bd3bb55 427 #ifdef CAM_DISP_DEBUG_CENTER
hazheng 44:15de535c4005 428 ardu_utft_set_camimg_rowcol(CAM_ROI_UPPER_LIMIT - i, temp_mid_pos);
hazheng 44:15de535c4005 429 ardu_utft_write_DATA(0xF8, 0x00);
hazheng 44:15de535c4005 430 #endif
hazheng 46:a5eb9bd3bb55 431
hazheng 44:15de535c4005 432 centerLine[CAM_ROI_UPPER_LIMIT - i - 1] = temp_mid_pos;
hazheng 65:295c222fdf88 433 centerLine[(2 * CAM_ROI_UPPER_LIMIT) - i - 1] = isBorderFound;
hazheng 44:15de535c4005 434 }
hazheng 56:7d3395ae022d 435
hazheng 57:0d8a155d511d 436
hazheng 63:d9a81b3d69f5 437 //counter.Update();
hazheng 57:0d8a155d511d 438 //Thread::signal_wait(IMG_PROC_SIGNAL, osWaitForever);
hazheng 44:15de535c4005 439 }
hazheng 44:15de535c4005 440 }
hazheng 46:a5eb9bd3bb55 441
hazheng 46:a5eb9bd3bb55 442
hazheng 43:0d1886f4848a 443 void ardu_cam_display_img_utft()
hazheng 43:0d1886f4848a 444 {
hazheng 43:0d1886f4848a 445 ardu_cam_start_capture();
hazheng 44:15de535c4005 446
hazheng 44:15de535c4005 447 while (!(ardu_cam_spi_read_8(ARDUCHIP_TRIG) & CAP_DONE_MASK));
hazheng 44:15de535c4005 448
hazheng 44:15de535c4005 449 temp_mid_pos = RESOLUTION_WIDTH / 2;
hazheng 46:a5eb9bd3bb55 450
hazheng 44:15de535c4005 451 for (uint8_t i = 0; i < CAM_ROI_UPPER_LIMIT; ++i)
hazheng 43:0d1886f4848a 452 {
hazheng 46:a5eb9bd3bb55 453 ardu_utft_set_camimg_row(CAM_ROI_UPPER_LIMIT - i);
hazheng 46:a5eb9bd3bb55 454 for (uint8_t j = 0; j < RESOLUTION_WIDTH; ++j)
hazheng 46:a5eb9bd3bb55 455 {
hazheng 46:a5eb9bd3bb55 456 uint8_t VH = ardu_cam_spi_read_8(SINGLE_FIFO_READ);
hazheng 46:a5eb9bd3bb55 457 uint8_t VL = ardu_cam_spi_read_8(SINGLE_FIFO_READ);
hazheng 46:a5eb9bd3bb55 458
hazheng 46:a5eb9bd3bb55 459 ardu_utft_write_DATA(VH, VL);
hazheng 46:a5eb9bd3bb55 460 }
hazheng 44:15de535c4005 461
hazheng 43:0d1886f4848a 462 }
hazheng 44:15de535c4005 463 }
hazheng 64:43ab429a37e0 464
hazheng 64:43ab429a37e0 465 uint8_t ardu_cam_get_is_encounter_terminate()
hazheng 64:43ab429a37e0 466 {
hazheng 64:43ab429a37e0 467 return is_encounter_terminate;
hazheng 64:43ab429a37e0 468 }
hazheng 63:d9a81b3d69f5 469 /*
hazheng 57:0d8a155d511d 470 void tick_image_proc()
hazheng 57:0d8a155d511d 471 {
hazheng 57:0d8a155d511d 472 m_imgProcessThread->signal_set(IMG_PROC_SIGNAL);
hazheng 57:0d8a155d511d 473 }
hazheng 63:d9a81b3d69f5 474 */
hazheng 46:a5eb9bd3bb55 475 #ifdef __cplusplus
hazheng 46:a5eb9bd3bb55 476 }
hazheng 46:a5eb9bd3bb55 477 #endif
hazheng 46:a5eb9bd3bb55 478
hazheng 46:a5eb9bd3bb55 479
hazheng 46:a5eb9bd3bb55 480