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