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:
Thu Mar 23 23:17:51 2017 +0000
Revision:
41:7b21c5e3599e
Parent:
40:be98219930e4
Child:
42:c4e1606087ff
Made the TFT screen working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hazheng 28:271fc8445e89 1 #include "ArduCAM.h"
hazheng 28:271fc8445e89 2
hazheng 33:e3fcc4d6bb9b 3
hazheng 29:f87d8790f57d 4 #include "GlobalVariable.h"
hazheng 29:f87d8790f57d 5 #include "SWUSBServer.h"
hazheng 29:f87d8790f57d 6
hazheng 32:5badeff825dc 7 #include "CamRegBuf.h"
hazheng 32:5badeff825dc 8
hazheng 41:7b21c5e3599e 9 #include "ArduUTFT.h"
hazheng 41:7b21c5e3599e 10
hazheng 32:5badeff825dc 11 #include <string>
hazheng 32:5badeff825dc 12
hazheng 28:271fc8445e89 13 extern SPI g_spi_port;
hazheng 28:271fc8445e89 14
hazheng 41:7b21c5e3599e 15 DigitalOut cam_cs(PIN_ACC_CS, 1);
hazheng 29:f87d8790f57d 16
hazheng 34:f79db3bc2f86 17
hazheng 34:f79db3bc2f86 18 inline void ardu_cam_spi_write_8(int address, int value)
hazheng 29:f87d8790f57d 19 {
hazheng 34:f79db3bc2f86 20 // take the SS pin low to select the chip:
hazheng 34:f79db3bc2f86 21 cam_cs = 0;
hazheng 34:f79db3bc2f86 22 // send in the address and value via SPI:
hazheng 34:f79db3bc2f86 23 g_spi_port.write(address | 0x80);
hazheng 34:f79db3bc2f86 24 g_spi_port.write(value);
hazheng 34:f79db3bc2f86 25 // take the SS pin high to de-select the chip:
hazheng 34:f79db3bc2f86 26 cam_cs = 1;
hazheng 34:f79db3bc2f86 27 }
hazheng 34:f79db3bc2f86 28
hazheng 34:f79db3bc2f86 29 inline uint8_t ardu_cam_spi_read_8(int address)
hazheng 34:f79db3bc2f86 30 {
hazheng 34:f79db3bc2f86 31 // take the SS pin low to select the chip:
hazheng 29:f87d8790f57d 32 cam_cs = 0;
hazheng 34:f79db3bc2f86 33 // send in the address and value via SPI:
hazheng 34:f79db3bc2f86 34 g_spi_port.write(address & 0x7F);
hazheng 34:f79db3bc2f86 35 uint8_t value = static_cast<uint8_t>(g_spi_port.write(0x00));
hazheng 34:f79db3bc2f86 36 // take the SS pin high to de-select the chip:
hazheng 29:f87d8790f57d 37 cam_cs = 1;
hazheng 34:f79db3bc2f86 38 return value;
hazheng 34:f79db3bc2f86 39 }
hazheng 34:f79db3bc2f86 40 /*
hazheng 34:f79db3bc2f86 41 inline void ardu_cam_spi_set_burst()
hazheng 34:f79db3bc2f86 42 {
hazheng 34:f79db3bc2f86 43 cam_cs = 0;
hazheng 34:f79db3bc2f86 44
hazheng 34:f79db3bc2f86 45 g_spi_port.write(BURST_FIFO_READ & 0x7F);
hazheng 29:f87d8790f57d 46
hazheng 34:f79db3bc2f86 47 cam_cs = 1;
hazheng 34:f79db3bc2f86 48 }
hazheng 34:f79db3bc2f86 49
hazheng 34:f79db3bc2f86 50 inline uint16_t ardu_cam_spi_burst_read_16()
hazheng 34:f79db3bc2f86 51 {
hazheng 34:f79db3bc2f86 52 cam_cs = 0;
hazheng 29:f87d8790f57d 53
hazheng 34:f79db3bc2f86 54 uint16_t value = static_cast<uint16_t>(g_spi_port.write(0x00));
hazheng 34:f79db3bc2f86 55 value = (value << 8) & 0xFF00;
hazheng 34:f79db3bc2f86 56 value = value | static_cast<uint16_t>(g_spi_port.write(0x00));
hazheng 34:f79db3bc2f86 57
hazheng 34:f79db3bc2f86 58 cam_cs = 1;
hazheng 34:f79db3bc2f86 59 return value;
hazheng 29:f87d8790f57d 60 }
hazheng 29:f87d8790f57d 61 */
hazheng 29:f87d8790f57d 62
hazheng 29:f87d8790f57d 63
hazheng 28:271fc8445e89 64 bool ardu_cam_init()
hazheng 28:271fc8445e89 65 {
hazheng 32:5badeff825dc 66 char buf[20];
hazheng 32:5badeff825dc 67
hazheng 36:7e747e19f660 68 CamRegBuf * camReg = new CamRegBuf(g_core, CAM_SCCB_WRITE, CAM_SCCB_READ);
hazheng 36:7e747e19f660 69
hazheng 36:7e747e19f660 70 camReg->WriteRegSet(ResetProg);
hazheng 32:5badeff825dc 71 wait_ms(5);
hazheng 37:7074a6118d03 72 camReg->WriteRegSet(QVGA);
hazheng 32:5badeff825dc 73 wait_ms(100);
hazheng 32:5badeff825dc 74
hazheng 36:7e747e19f660 75 #if defined(ARDUCAM_OV2640)
hazheng 32:5badeff825dc 76 camReg->SCCBWrite(0xff, 0x01);
hazheng 36:7e747e19f660 77 #endif
hazheng 36:7e747e19f660 78 uint8_t camVer = camReg->SCCBRead(CAM_PID_ADDR);
hazheng 32:5badeff825dc 79 sprintf(buf, "Cam VerH %#x", camVer);
hazheng 32:5badeff825dc 80 g_core.GetUSBServer().PushReliableMsg('D', buf);
hazheng 32:5badeff825dc 81
hazheng 36:7e747e19f660 82 camVer = camReg->SCCBRead(CAM_VER_ADDR);
hazheng 32:5badeff825dc 83 sprintf(buf, "Cam VerL %#x", camVer);
hazheng 32:5badeff825dc 84 g_core.GetUSBServer().PushReliableMsg('D', buf);
hazheng 36:7e747e19f660 85
hazheng 36:7e747e19f660 86
hazheng 32:5badeff825dc 87 delete camReg;
hazheng 32:5badeff825dc 88 camReg = NULL;
hazheng 32:5badeff825dc 89
hazheng 32:5badeff825dc 90
hazheng 32:5badeff825dc 91
hazheng 32:5badeff825dc 92
hazheng 34:f79db3bc2f86 93 uint8_t VerNum = ardu_cam_spi_read_8(0x40);
hazheng 34:f79db3bc2f86 94 VerNum = ardu_cam_spi_read_8(0x40);
hazheng 29:f87d8790f57d 95
hazheng 32:5badeff825dc 96
hazheng 29:f87d8790f57d 97 sprintf(buf, "Ardu Ver %#x", VerNum);
hazheng 29:f87d8790f57d 98 g_core.GetUSBServer().PushReliableMsg('D', buf);
hazheng 29:f87d8790f57d 99
hazheng 34:f79db3bc2f86 100 ardu_cam_spi_write_8(ARDUCHIP_TEST1, ARDUCHIP_TEST_MSG);
hazheng 34:f79db3bc2f86 101 uint8_t testV = ardu_cam_spi_read_8(ARDUCHIP_TEST1);
hazheng 35:ac4fcca21560 102 if(VerNum != ARDUCHIP_VER_NUM || testV != ARDUCHIP_TEST_MSG)
hazheng 29:f87d8790f57d 103 {
hazheng 29:f87d8790f57d 104 g_core.GetUSBServer().PushReliableMsg('D', "CameraInit Fa");
hazheng 29:f87d8790f57d 105 return false;
hazheng 29:f87d8790f57d 106 }
hazheng 29:f87d8790f57d 107 g_core.GetUSBServer().PushReliableMsg('D', "CameraInit Su");
hazheng 29:f87d8790f57d 108
hazheng 35:ac4fcca21560 109 //ardu_cam_set_mode(MCU2LCD_MODE);
hazheng 29:f87d8790f57d 110 ardu_cam_start_capture();
hazheng 29:f87d8790f57d 111
hazheng 29:f87d8790f57d 112 wait(0.1);
hazheng 28:271fc8445e89 113
hazheng 28:271fc8445e89 114
hazheng 32:5badeff825dc 115 //unsigned char tempV = ardu_cam_read_reg(ARDUCHIP_MODE);
hazheng 32:5badeff825dc 116 //sprintf(buf, "Ardu Stat %#x", tempV);
hazheng 32:5badeff825dc 117 //g_core.GetUSBServer().PushReliableMsg('D', buf);
hazheng 29:f87d8790f57d 118
hazheng 32:5badeff825dc 119 //tempV = ardu_cam_read_reg(ARDUCHIP_CAP_CTRL);
hazheng 32:5badeff825dc 120 //sprintf(buf, "Ardu FS1 %#x", tempV);
hazheng 32:5badeff825dc 121 //g_core.GetUSBServer().PushReliableMsg('D', buf);
hazheng 29:f87d8790f57d 122
hazheng 29:f87d8790f57d 123 return true;
hazheng 29:f87d8790f57d 124 }
hazheng 29:f87d8790f57d 125
hazheng 29:f87d8790f57d 126 void ardu_cam_start_capture()
hazheng 29:f87d8790f57d 127 {
hazheng 34:f79db3bc2f86 128 ardu_cam_spi_write_8(ARDUCHIP_FIFO, FIFO_CLEAR_MASK);
hazheng 34:f79db3bc2f86 129 ardu_cam_spi_write_8(ARDUCHIP_CAP_CTRL, 0x00);
hazheng 34:f79db3bc2f86 130 ardu_cam_spi_write_8(ARDUCHIP_FIFO, FIFO_START_MASK);
hazheng 29:f87d8790f57d 131 }
hazheng 29:f87d8790f57d 132
hazheng 32:5badeff825dc 133 uint32_t ardu_cam_get_fifo_length()
hazheng 32:5badeff825dc 134 {
hazheng 32:5badeff825dc 135 uint32_t len1,len2,len3,length=0;
hazheng 34:f79db3bc2f86 136 len1 = ardu_cam_spi_read_8(FIFO_SIZE1);
hazheng 34:f79db3bc2f86 137 len2 = ardu_cam_spi_read_8(FIFO_SIZE2);
hazheng 34:f79db3bc2f86 138 len3 = ardu_cam_spi_read_8(FIFO_SIZE3) & 0x07;
hazheng 32:5badeff825dc 139 length = ((len3 << 16) | (len2 << 8) | len1) & 0x07ffff;
hazheng 32:5badeff825dc 140 return length;
hazheng 32:5badeff825dc 141 }
hazheng 32:5badeff825dc 142
hazheng 32:5badeff825dc 143 uint8_t ardu_cam_get_pixel()
hazheng 32:5badeff825dc 144 {
hazheng 34:f79db3bc2f86 145 uint16_t VH = ardu_cam_spi_read_8(SINGLE_FIFO_READ);
hazheng 34:f79db3bc2f86 146 uint16_t VL = ardu_cam_spi_read_8(SINGLE_FIFO_READ);
hazheng 34:f79db3bc2f86 147 //uint16_t VL = ardu_cam_spi_burst_read_16();
hazheng 32:5badeff825dc 148
hazheng 32:5badeff825dc 149 VL = (VL & 0x00FF) | ((VH << 8) & 0xFF00);
hazheng 33:e3fcc4d6bb9b 150 uint8_t ch = ((VL & 0xF800) >> 9);// << 2;
hazheng 33:e3fcc4d6bb9b 151 float pixel = (static_cast<float>(ch) * 0.21);
hazheng 32:5badeff825dc 152
hazheng 33:e3fcc4d6bb9b 153 ch = ((VL & 0x07E0) >> 3);// << 2;
hazheng 33:e3fcc4d6bb9b 154 pixel += (static_cast<float>(ch) * 0.72);
hazheng 32:5badeff825dc 155
hazheng 33:e3fcc4d6bb9b 156 ch = (VL & 0x001F) << 2;
hazheng 33:e3fcc4d6bb9b 157 pixel += (static_cast<float>(ch) * 0.07);
hazheng 32:5badeff825dc 158
hazheng 32:5badeff825dc 159 return static_cast<uint8_t>(pixel);
hazheng 32:5badeff825dc 160 }
hazheng 32:5badeff825dc 161
hazheng 32:5badeff825dc 162 void ardu_cam_print_debug()
hazheng 32:5badeff825dc 163 {
hazheng 32:5badeff825dc 164 uint32_t len = ardu_cam_get_fifo_length();
hazheng 32:5badeff825dc 165 char buf[20];
hazheng 32:5badeff825dc 166 sprintf(buf, "Cam L %#x", len);
hazheng 32:5badeff825dc 167 g_core.GetUSBServer().PushReliableMsg('D', buf);
hazheng 32:5badeff825dc 168
hazheng 33:e3fcc4d6bb9b 169 //if(len < (RESOLUTION_HEIGHT * RESOLUTION_WIDTH * 2))
hazheng 33:e3fcc4d6bb9b 170 // return;
hazheng 33:e3fcc4d6bb9b 171
hazheng 33:e3fcc4d6bb9b 172 //Begin output the picture
hazheng 32:5badeff825dc 173 std::string lineBuf;
hazheng 37:7074a6118d03 174 #if defined(MANUAL_REDUCE_RESULOTION_BY2)
hazheng 37:7074a6118d03 175 lineBuf.resize((RESOLUTION_WIDTH / 2) + 1);
hazheng 37:7074a6118d03 176 #else
hazheng 33:e3fcc4d6bb9b 177 lineBuf.resize(RESOLUTION_WIDTH + 1);
hazheng 37:7074a6118d03 178 #endif
hazheng 32:5badeff825dc 179
hazheng 34:f79db3bc2f86 180 //ardu_cam_spi_set_burst();
hazheng 41:7b21c5e3599e 181
hazheng 33:e3fcc4d6bb9b 182 ardu_cam_get_pixel(); //Get the first dummy pixel
hazheng 41:7b21c5e3599e 183 //ardu_utft_set_xy(0, 0, 319, 239);
hazheng 41:7b21c5e3599e 184 for (uint8_t i = 0; i < RESOLUTION_HEIGHT; ++i)
hazheng 41:7b21c5e3599e 185 {
hazheng 41:7b21c5e3599e 186 ardu_utft_set_xy(0, i + 20, 319 - 80, 239);
hazheng 41:7b21c5e3599e 187 for (int j = 0; j < RESOLUTION_WIDTH; ++j)
hazheng 41:7b21c5e3599e 188 {
hazheng 41:7b21c5e3599e 189 uint8_t VH = ardu_cam_spi_read_8(SINGLE_FIFO_READ);
hazheng 41:7b21c5e3599e 190 uint8_t VL = ardu_cam_spi_read_8(SINGLE_FIFO_READ);
hazheng 41:7b21c5e3599e 191
hazheng 41:7b21c5e3599e 192 ardu_utft_write_DATA(VH, VL);
hazheng 41:7b21c5e3599e 193 }
hazheng 41:7b21c5e3599e 194 }
hazheng 41:7b21c5e3599e 195
hazheng 41:7b21c5e3599e 196 /*
hazheng 33:e3fcc4d6bb9b 197 for (uint8_t i = 0; i < RESOLUTION_HEIGHT; ++i)
hazheng 32:5badeff825dc 198 {
hazheng 37:7074a6118d03 199 #if defined(MANUAL_REDUCE_RESULOTION_BY2)
hazheng 37:7074a6118d03 200 lineBuf[0] = i / 2;
hazheng 37:7074a6118d03 201
hazheng 37:7074a6118d03 202 for (int j = 0; j < RESOLUTION_WIDTH; ++j)
hazheng 37:7074a6118d03 203 {
hazheng 37:7074a6118d03 204 if(i % 2 == 0 || j % 2 == 0)
hazheng 37:7074a6118d03 205 {
hazheng 37:7074a6118d03 206 ardu_cam_get_pixel();
hazheng 37:7074a6118d03 207 }
hazheng 37:7074a6118d03 208 else
hazheng 37:7074a6118d03 209 {
hazheng 37:7074a6118d03 210 lineBuf[(j / 2) + 1] = ardu_cam_get_pixel();
hazheng 37:7074a6118d03 211 }
hazheng 37:7074a6118d03 212 }
hazheng 37:7074a6118d03 213
hazheng 37:7074a6118d03 214 if(i % 2 == 0)
hazheng 37:7074a6118d03 215 {
hazheng 37:7074a6118d03 216
hazheng 37:7074a6118d03 217 }
hazheng 37:7074a6118d03 218 else
hazheng 37:7074a6118d03 219 {
hazheng 37:7074a6118d03 220 g_core.GetUSBServer().PushReliableMsg('P', lineBuf);
hazheng 37:7074a6118d03 221 wait(0.35);
hazheng 37:7074a6118d03 222 }
hazheng 37:7074a6118d03 223
hazheng 37:7074a6118d03 224 #else
hazheng 37:7074a6118d03 225
hazheng 32:5badeff825dc 226 lineBuf[0] = i;
hazheng 32:5badeff825dc 227
hazheng 33:e3fcc4d6bb9b 228 for (int j = 0; j < RESOLUTION_WIDTH; ++j)
hazheng 32:5badeff825dc 229 {
hazheng 33:e3fcc4d6bb9b 230 //uint8_t p = ardu_cam_get_pixel();
hazheng 33:e3fcc4d6bb9b 231 lineBuf[j + 1] = ardu_cam_get_pixel();
hazheng 32:5badeff825dc 232 }
hazheng 32:5badeff825dc 233
hazheng 32:5badeff825dc 234 g_core.GetUSBServer().PushReliableMsg('P', lineBuf);
hazheng 35:ac4fcca21560 235 wait(0.35);
hazheng 37:7074a6118d03 236
hazheng 37:7074a6118d03 237 #endif
hazheng 37:7074a6118d03 238
hazheng 32:5badeff825dc 239 }
hazheng 41:7b21c5e3599e 240 */
hazheng 32:5badeff825dc 241 }