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
- Committer:
- hazheng
- Date:
- 2017-03-24
- Revision:
- 42:c4e1606087ff
- Parent:
- 41:7b21c5e3599e
- Child:
- 43:0d1886f4848a
File content as of revision 42:c4e1606087ff:
#include "ArduCAM.h" #include "GlobalVariable.h" #include "SWUSBServer.h" #include "CamRegBuf.h" #include "ArduUTFT.h" #include <string> extern SPI g_spi_port; DigitalOut cam_cs(PIN_ACC_CS, 1); inline void ardu_cam_spi_write_8(int address, int value) { // take the SS pin low to select the chip: cam_cs = 0; // send in the address and value via SPI: g_spi_port.write(address | 0x80); g_spi_port.write(value); // take the SS pin high to de-select the chip: cam_cs = 1; } inline uint8_t ardu_cam_spi_read_8(int address) { // take the SS pin low to select the chip: cam_cs = 0; // send in the address and value via SPI: g_spi_port.write(address & 0x7F); uint8_t value = static_cast<uint8_t>(g_spi_port.write(0x00)); // take the SS pin high to de-select the chip: cam_cs = 1; return value; } /* inline void ardu_cam_spi_set_burst() { cam_cs = 0; g_spi_port.write(BURST_FIFO_READ & 0x7F); cam_cs = 1; } inline uint16_t ardu_cam_spi_burst_read_16() { cam_cs = 0; uint16_t value = static_cast<uint16_t>(g_spi_port.write(0x00)); value = (value << 8) & 0xFF00; value = value | static_cast<uint16_t>(g_spi_port.write(0x00)); cam_cs = 1; return value; } */ bool ardu_cam_init() { char buf[20]; CamRegBuf * camReg = new CamRegBuf(g_core, CAM_SCCB_WRITE, CAM_SCCB_READ); camReg->WriteRegSet(ResetProg); wait_ms(5); camReg->WriteRegSet(QVGA); wait_ms(100); #if defined(ARDUCAM_OV2640) camReg->SCCBWrite(0xff, 0x01); #endif uint8_t camVer = camReg->SCCBRead(CAM_PID_ADDR); sprintf(buf, "Cam VerH %#x", camVer); g_core.GetUSBServer().PushReliableMsg('D', buf); camVer = camReg->SCCBRead(CAM_VER_ADDR); sprintf(buf, "Cam VerL %#x", camVer); g_core.GetUSBServer().PushReliableMsg('D', buf); delete camReg; camReg = NULL; uint8_t VerNum = ardu_cam_spi_read_8(0x40); VerNum = ardu_cam_spi_read_8(0x40); sprintf(buf, "Ardu Ver %#x", VerNum); g_core.GetUSBServer().PushReliableMsg('D', buf); ardu_cam_spi_write_8(ARDUCHIP_TEST1, ARDUCHIP_TEST_MSG); uint8_t testV = ardu_cam_spi_read_8(ARDUCHIP_TEST1); if(VerNum != ARDUCHIP_VER_NUM || testV != ARDUCHIP_TEST_MSG) { g_core.GetUSBServer().PushReliableMsg('D', "CameraInit Fa"); return false; } g_core.GetUSBServer().PushReliableMsg('D', "CameraInit Su"); //ardu_cam_set_mode(MCU2LCD_MODE); ardu_cam_start_capture(); wait(0.1); //unsigned char tempV = ardu_cam_read_reg(ARDUCHIP_MODE); //sprintf(buf, "Ardu Stat %#x", tempV); //g_core.GetUSBServer().PushReliableMsg('D', buf); //tempV = ardu_cam_read_reg(ARDUCHIP_CAP_CTRL); //sprintf(buf, "Ardu FS1 %#x", tempV); //g_core.GetUSBServer().PushReliableMsg('D', buf); return true; } void ardu_cam_start_capture() { ardu_cam_spi_write_8(ARDUCHIP_FIFO, FIFO_CLEAR_MASK); ardu_cam_spi_write_8(ARDUCHIP_CAP_CTRL, 0x00); ardu_cam_spi_write_8(ARDUCHIP_FIFO, FIFO_START_MASK); } uint32_t ardu_cam_get_fifo_length() { uint32_t len1,len2,len3,length=0; len1 = ardu_cam_spi_read_8(FIFO_SIZE1); len2 = ardu_cam_spi_read_8(FIFO_SIZE2); len3 = ardu_cam_spi_read_8(FIFO_SIZE3) & 0x07; length = ((len3 << 16) | (len2 << 8) | len1) & 0x07ffff; return length; } uint8_t ardu_cam_get_pixel() { uint16_t VH = ardu_cam_spi_read_8(SINGLE_FIFO_READ); uint16_t VL = ardu_cam_spi_read_8(SINGLE_FIFO_READ); //uint16_t VL = ardu_cam_spi_burst_read_16(); VL = (VL & 0x00FF) | ((VH << 8) & 0xFF00); uint8_t ch = ((VL & 0xF800) >> 9);// << 2; float pixel = (static_cast<float>(ch) * 0.21); ch = ((VL & 0x07E0) >> 3);// << 2; pixel += (static_cast<float>(ch) * 0.72); ch = (VL & 0x001F) << 2; pixel += (static_cast<float>(ch) * 0.07); return static_cast<uint8_t>(pixel); } void ardu_cam_print_debug() { uint32_t len = ardu_cam_get_fifo_length(); char buf[20]; sprintf(buf, "Cam L %#x", len); g_core.GetUSBServer().PushReliableMsg('D', buf); //if(len < (RESOLUTION_HEIGHT * RESOLUTION_WIDTH * 2)) // return; //Begin output the picture std::string lineBuf; #if defined(MANUAL_REDUCE_RESULOTION_BY2) lineBuf.resize((RESOLUTION_WIDTH / 2) + 1); #else lineBuf.resize(RESOLUTION_WIDTH + 1); #endif //ardu_cam_spi_set_burst(); //ardu_cam_get_pixel(); //Get the first dummy pixel //ardu_utft_set_xy(0, 0, 319, 239); for (uint8_t i = 0; i < RESOLUTION_HEIGHT; ++i) { ardu_utft_set_xy(0, i + 20, 300, i + 20); for (int j = 0; j < RESOLUTION_WIDTH; ++j) { uint8_t VH = ardu_cam_spi_read_8(SINGLE_FIFO_READ); uint8_t VL = ardu_cam_spi_read_8(SINGLE_FIFO_READ); ardu_utft_write_DATA(VH, VL); } } /* for (uint8_t i = 0; i < RESOLUTION_HEIGHT; ++i) { #if defined(MANUAL_REDUCE_RESULOTION_BY2) lineBuf[0] = i / 2; for (int j = 0; j < RESOLUTION_WIDTH; ++j) { if(i % 2 == 0 || j % 2 == 0) { ardu_cam_get_pixel(); } else { lineBuf[(j / 2) + 1] = ardu_cam_get_pixel(); } } if(i % 2 == 0) { } else { g_core.GetUSBServer().PushReliableMsg('P', lineBuf); wait(0.35); } #else lineBuf[0] = i; for (int j = 0; j < RESOLUTION_WIDTH; ++j) { //uint8_t p = ardu_cam_get_pixel(); lineBuf[j + 1] = ardu_cam_get_pixel(); } g_core.GetUSBServer().PushReliableMsg('P', lineBuf); wait(0.35); #endif } */ }