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
Hardwares/ArduCAM.cpp@37:7074a6118d03, 2017-03-14 (annotated)
- Committer:
- hazheng
- Date:
- Tue Mar 14 23:36:13 2017 +0000
- Revision:
- 37:7074a6118d03
- Parent:
- 36:7e747e19f660
- Child:
- 40:be98219930e4
Added support for OV7670.
Who changed what in which revision?
User | Revision | Line number | New 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 | 32:5badeff825dc | 9 | #include <string> |
hazheng | 32:5badeff825dc | 10 | |
hazheng | 28:271fc8445e89 | 11 | extern SPI g_spi_port; |
hazheng | 28:271fc8445e89 | 12 | |
hazheng | 29:f87d8790f57d | 13 | DigitalOut cam_cs(PIN_ACC_CS); |
hazheng | 29:f87d8790f57d | 14 | |
hazheng | 29:f87d8790f57d | 15 | DigitalOut testLED2(LED_RED, 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 | 29:f87d8790f57d | 113 | ardu_cam_set_mode(CAM2LCD_MODE); |
hazheng | 28:271fc8445e89 | 114 | |
hazheng | 28:271fc8445e89 | 115 | |
hazheng | 32:5badeff825dc | 116 | //unsigned char tempV = ardu_cam_read_reg(ARDUCHIP_MODE); |
hazheng | 32:5badeff825dc | 117 | //sprintf(buf, "Ardu Stat %#x", tempV); |
hazheng | 32:5badeff825dc | 118 | //g_core.GetUSBServer().PushReliableMsg('D', buf); |
hazheng | 29:f87d8790f57d | 119 | |
hazheng | 32:5badeff825dc | 120 | //tempV = ardu_cam_read_reg(ARDUCHIP_CAP_CTRL); |
hazheng | 32:5badeff825dc | 121 | //sprintf(buf, "Ardu FS1 %#x", tempV); |
hazheng | 32:5badeff825dc | 122 | //g_core.GetUSBServer().PushReliableMsg('D', buf); |
hazheng | 29:f87d8790f57d | 123 | |
hazheng | 29:f87d8790f57d | 124 | return true; |
hazheng | 29:f87d8790f57d | 125 | } |
hazheng | 29:f87d8790f57d | 126 | |
hazheng | 29:f87d8790f57d | 127 | void ardu_cam_set_mode(uint8_t mode) |
hazheng | 29:f87d8790f57d | 128 | { |
hazheng | 29:f87d8790f57d | 129 | //ardu_cam_bus_io(ARDUCHIP_MODE, mode); |
hazheng | 29:f87d8790f57d | 130 | switch(mode) |
hazheng | 29:f87d8790f57d | 131 | { |
hazheng | 29:f87d8790f57d | 132 | case MCU2LCD_MODE: |
hazheng | 34:f79db3bc2f86 | 133 | ardu_cam_spi_write_8(ARDUCHIP_MODE, MCU2LCD_MODE); |
hazheng | 29:f87d8790f57d | 134 | break; |
hazheng | 29:f87d8790f57d | 135 | case CAM2LCD_MODE: |
hazheng | 34:f79db3bc2f86 | 136 | ardu_cam_spi_write_8(ARDUCHIP_MODE, CAM2LCD_MODE); |
hazheng | 29:f87d8790f57d | 137 | break; |
hazheng | 29:f87d8790f57d | 138 | //case LCD2MCU_MODE: |
hazheng | 34:f79db3bc2f86 | 139 | //ardu_cam_spi_write_8(ARDUCHIP_MODE, LCD2MCU_MODE); |
hazheng | 29:f87d8790f57d | 140 | //break; |
hazheng | 29:f87d8790f57d | 141 | default: |
hazheng | 34:f79db3bc2f86 | 142 | ardu_cam_spi_write_8(ARDUCHIP_MODE, CAM2LCD_MODE); |
hazheng | 29:f87d8790f57d | 143 | break; |
hazheng | 29:f87d8790f57d | 144 | } |
hazheng | 29:f87d8790f57d | 145 | } |
hazheng | 29:f87d8790f57d | 146 | |
hazheng | 29:f87d8790f57d | 147 | void ardu_cam_start_capture() |
hazheng | 29:f87d8790f57d | 148 | { |
hazheng | 34:f79db3bc2f86 | 149 | ardu_cam_spi_write_8(ARDUCHIP_FIFO, FIFO_CLEAR_MASK); |
hazheng | 34:f79db3bc2f86 | 150 | ardu_cam_spi_write_8(ARDUCHIP_CAP_CTRL, 0x00); |
hazheng | 34:f79db3bc2f86 | 151 | ardu_cam_spi_write_8(ARDUCHIP_FIFO, FIFO_START_MASK); |
hazheng | 29:f87d8790f57d | 152 | } |
hazheng | 29:f87d8790f57d | 153 | |
hazheng | 32:5badeff825dc | 154 | uint32_t ardu_cam_get_fifo_length() |
hazheng | 32:5badeff825dc | 155 | { |
hazheng | 32:5badeff825dc | 156 | uint32_t len1,len2,len3,length=0; |
hazheng | 34:f79db3bc2f86 | 157 | len1 = ardu_cam_spi_read_8(FIFO_SIZE1); |
hazheng | 34:f79db3bc2f86 | 158 | len2 = ardu_cam_spi_read_8(FIFO_SIZE2); |
hazheng | 34:f79db3bc2f86 | 159 | len3 = ardu_cam_spi_read_8(FIFO_SIZE3) & 0x07; |
hazheng | 32:5badeff825dc | 160 | length = ((len3 << 16) | (len2 << 8) | len1) & 0x07ffff; |
hazheng | 32:5badeff825dc | 161 | return length; |
hazheng | 32:5badeff825dc | 162 | } |
hazheng | 32:5badeff825dc | 163 | |
hazheng | 32:5badeff825dc | 164 | uint8_t ardu_cam_get_pixel() |
hazheng | 32:5badeff825dc | 165 | { |
hazheng | 34:f79db3bc2f86 | 166 | uint16_t VH = ardu_cam_spi_read_8(SINGLE_FIFO_READ); |
hazheng | 34:f79db3bc2f86 | 167 | uint16_t VL = ardu_cam_spi_read_8(SINGLE_FIFO_READ); |
hazheng | 34:f79db3bc2f86 | 168 | //uint16_t VL = ardu_cam_spi_burst_read_16(); |
hazheng | 32:5badeff825dc | 169 | |
hazheng | 32:5badeff825dc | 170 | VL = (VL & 0x00FF) | ((VH << 8) & 0xFF00); |
hazheng | 33:e3fcc4d6bb9b | 171 | uint8_t ch = ((VL & 0xF800) >> 9);// << 2; |
hazheng | 33:e3fcc4d6bb9b | 172 | float pixel = (static_cast<float>(ch) * 0.21); |
hazheng | 32:5badeff825dc | 173 | |
hazheng | 33:e3fcc4d6bb9b | 174 | ch = ((VL & 0x07E0) >> 3);// << 2; |
hazheng | 33:e3fcc4d6bb9b | 175 | pixel += (static_cast<float>(ch) * 0.72); |
hazheng | 32:5badeff825dc | 176 | |
hazheng | 33:e3fcc4d6bb9b | 177 | ch = (VL & 0x001F) << 2; |
hazheng | 33:e3fcc4d6bb9b | 178 | pixel += (static_cast<float>(ch) * 0.07); |
hazheng | 32:5badeff825dc | 179 | |
hazheng | 32:5badeff825dc | 180 | return static_cast<uint8_t>(pixel); |
hazheng | 32:5badeff825dc | 181 | } |
hazheng | 32:5badeff825dc | 182 | |
hazheng | 32:5badeff825dc | 183 | void ardu_cam_print_debug() |
hazheng | 32:5badeff825dc | 184 | { |
hazheng | 32:5badeff825dc | 185 | uint32_t len = ardu_cam_get_fifo_length(); |
hazheng | 32:5badeff825dc | 186 | char buf[20]; |
hazheng | 32:5badeff825dc | 187 | sprintf(buf, "Cam L %#x", len); |
hazheng | 32:5badeff825dc | 188 | g_core.GetUSBServer().PushReliableMsg('D', buf); |
hazheng | 32:5badeff825dc | 189 | |
hazheng | 33:e3fcc4d6bb9b | 190 | //if(len < (RESOLUTION_HEIGHT * RESOLUTION_WIDTH * 2)) |
hazheng | 33:e3fcc4d6bb9b | 191 | // return; |
hazheng | 33:e3fcc4d6bb9b | 192 | |
hazheng | 33:e3fcc4d6bb9b | 193 | //Begin output the picture |
hazheng | 32:5badeff825dc | 194 | std::string lineBuf; |
hazheng | 37:7074a6118d03 | 195 | #if defined(MANUAL_REDUCE_RESULOTION_BY2) |
hazheng | 37:7074a6118d03 | 196 | lineBuf.resize((RESOLUTION_WIDTH / 2) + 1); |
hazheng | 37:7074a6118d03 | 197 | #else |
hazheng | 33:e3fcc4d6bb9b | 198 | lineBuf.resize(RESOLUTION_WIDTH + 1); |
hazheng | 37:7074a6118d03 | 199 | #endif |
hazheng | 32:5badeff825dc | 200 | |
hazheng | 34:f79db3bc2f86 | 201 | //ardu_cam_spi_set_burst(); |
hazheng | 33:e3fcc4d6bb9b | 202 | ardu_cam_get_pixel(); //Get the first dummy pixel |
hazheng | 33:e3fcc4d6bb9b | 203 | |
hazheng | 33:e3fcc4d6bb9b | 204 | for (uint8_t i = 0; i < RESOLUTION_HEIGHT; ++i) |
hazheng | 32:5badeff825dc | 205 | { |
hazheng | 37:7074a6118d03 | 206 | #if defined(MANUAL_REDUCE_RESULOTION_BY2) |
hazheng | 37:7074a6118d03 | 207 | lineBuf[0] = i / 2; |
hazheng | 37:7074a6118d03 | 208 | |
hazheng | 37:7074a6118d03 | 209 | for (int j = 0; j < RESOLUTION_WIDTH; ++j) |
hazheng | 37:7074a6118d03 | 210 | { |
hazheng | 37:7074a6118d03 | 211 | if(i % 2 == 0 || j % 2 == 0) |
hazheng | 37:7074a6118d03 | 212 | { |
hazheng | 37:7074a6118d03 | 213 | ardu_cam_get_pixel(); |
hazheng | 37:7074a6118d03 | 214 | } |
hazheng | 37:7074a6118d03 | 215 | else |
hazheng | 37:7074a6118d03 | 216 | { |
hazheng | 37:7074a6118d03 | 217 | lineBuf[(j / 2) + 1] = ardu_cam_get_pixel(); |
hazheng | 37:7074a6118d03 | 218 | } |
hazheng | 37:7074a6118d03 | 219 | } |
hazheng | 37:7074a6118d03 | 220 | |
hazheng | 37:7074a6118d03 | 221 | if(i % 2 == 0) |
hazheng | 37:7074a6118d03 | 222 | { |
hazheng | 37:7074a6118d03 | 223 | |
hazheng | 37:7074a6118d03 | 224 | } |
hazheng | 37:7074a6118d03 | 225 | else |
hazheng | 37:7074a6118d03 | 226 | { |
hazheng | 37:7074a6118d03 | 227 | g_core.GetUSBServer().PushReliableMsg('P', lineBuf); |
hazheng | 37:7074a6118d03 | 228 | wait(0.35); |
hazheng | 37:7074a6118d03 | 229 | } |
hazheng | 37:7074a6118d03 | 230 | |
hazheng | 37:7074a6118d03 | 231 | #else |
hazheng | 37:7074a6118d03 | 232 | |
hazheng | 32:5badeff825dc | 233 | lineBuf[0] = i; |
hazheng | 32:5badeff825dc | 234 | |
hazheng | 33:e3fcc4d6bb9b | 235 | for (int j = 0; j < RESOLUTION_WIDTH; ++j) |
hazheng | 32:5badeff825dc | 236 | { |
hazheng | 33:e3fcc4d6bb9b | 237 | //uint8_t p = ardu_cam_get_pixel(); |
hazheng | 33:e3fcc4d6bb9b | 238 | lineBuf[j + 1] = ardu_cam_get_pixel(); |
hazheng | 32:5badeff825dc | 239 | } |
hazheng | 32:5badeff825dc | 240 | |
hazheng | 32:5badeff825dc | 241 | g_core.GetUSBServer().PushReliableMsg('P', lineBuf); |
hazheng | 35:ac4fcca21560 | 242 | wait(0.35); |
hazheng | 37:7074a6118d03 | 243 | |
hazheng | 37:7074a6118d03 | 244 | #endif |
hazheng | 37:7074a6118d03 | 245 | |
hazheng | 32:5badeff825dc | 246 | } |
hazheng | 32:5badeff825dc | 247 | |
hazheng | 32:5badeff825dc | 248 | } |