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
Diff: Hardwares/ArduCAM.cpp
- Revision:
- 32:5badeff825dc
- Parent:
- 29:f87d8790f57d
- Child:
- 33:e3fcc4d6bb9b
--- a/Hardwares/ArduCAM.cpp Thu Mar 02 04:26:33 2017 +0000 +++ b/Hardwares/ArduCAM.cpp Thu Mar 02 23:56:42 2017 +0000 @@ -3,6 +3,11 @@ #include "GlobalVariable.h" #include "SWUSBServer.h" +#include "CamRegBuf.h" +#include "OV2640RegProg.h" + +#include <string> + extern SPI g_spi_port; DigitalOut cam_cs(PIN_ACC_CS); @@ -41,10 +46,46 @@ bool ardu_cam_init() { + char buf[20]; + + + CamRegBuf * camReg = new CamRegBuf(g_core, OV2640_WRITE, OV2640_READ); + /* + camReg->SCCBWrite(0xff, 0x00); + uint8_t camVer = camReg->SCCBRead(0xDA); + sprintf(buf, "Cam MOD %#x", camVer); + g_core.GetUSBServer().PushReliableMsg('D', buf); + */ + camReg->SCCBWrite(0xff, 0x01); + camReg->SCCBWrite(0x12, 0x80); + wait_ms(5); + camReg->WriteRegSet(OV2640Prog::QVGA); + wait_ms(100); + + camReg->SCCBWrite(0xff, 0x01); + uint8_t camVer = camReg->SCCBRead(OV2640_PID_ADDR); + sprintf(buf, "Cam VerH %#x", camVer); + g_core.GetUSBServer().PushReliableMsg('D', buf); + + camVer = camReg->SCCBRead(OV2640_VER_ADDR); + sprintf(buf, "Cam VerL %#x", camVer); + g_core.GetUSBServer().PushReliableMsg('D', buf); + /* + camReg->SCCBWrite(0xff, 0x00); + camVer = camReg->SCCBRead(0xDA); + sprintf(buf, "Cam MOD %#x", camVer); + g_core.GetUSBServer().PushReliableMsg('D', buf); + */ + delete camReg; + camReg = NULL; + + + + uint8_t VerNum = ardu_cam_read_reg(0x40); VerNum = ardu_cam_read_reg(0x40); - char buf[20]; + sprintf(buf, "Ardu Ver %#x", VerNum); g_core.GetUSBServer().PushReliableMsg('D', buf); @@ -64,13 +105,13 @@ ardu_cam_set_mode(CAM2LCD_MODE); - unsigned char tempV = ardu_cam_read_reg(ARDUCHIP_MODE); - sprintf(buf, "Ardu Stat %#x", tempV); - g_core.GetUSBServer().PushReliableMsg('D', buf); + //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); + //tempV = ardu_cam_read_reg(ARDUCHIP_CAP_CTRL); + //sprintf(buf, "Ardu FS1 %#x", tempV); + //g_core.GetUSBServer().PushReliableMsg('D', buf); return true; } @@ -98,10 +139,20 @@ void ardu_cam_start_capture() { ardu_cam_write_reg(ARDUCHIP_FIFO, FIFO_CLEAR_MASK); - ardu_cam_write_reg(ARDUCHIP_CAP_CTRL, 0x05); + ardu_cam_write_reg(ARDUCHIP_CAP_CTRL, 0x00); ardu_cam_write_reg(ARDUCHIP_FIFO, FIFO_START_MASK); } +uint32_t ardu_cam_get_fifo_length() +{ + uint32_t len1,len2,len3,length=0; + len1 = ardu_cam_read_reg(FIFO_SIZE1); + len2 = ardu_cam_read_reg(FIFO_SIZE2); + len3 = ardu_cam_read_reg(FIFO_SIZE3) & 0x07; + length = ((len3 << 16) | (len2 << 8) | len1) & 0x07ffff; + return length; +} + int ardu_cam_bus_write(int address, int value) { // take the SS pin low to select the chip: @@ -127,3 +178,67 @@ cam_cs = 1; return value; } + +uint8_t ardu_cam_get_pixel() +{ + uint16_t VH = ardu_cam_read_reg(SINGLE_FIFO_READ); + uint16_t VL = ardu_cam_read_reg(SINGLE_FIFO_READ); + + //char buf[20]; + //sprintf(buf, "h%#x", VH); + //g_core.GetUSBServer().PushReliableMsg('D', buf); + //sprintf(buf, "l%#x", VL); + //g_core.GetUSBServer().PushReliableMsg('D', buf); + + VL = (VL & 0x00FF) | ((VH << 8) & 0xFF00); + uint8_t ch = ((VL & 0xF800) >> 11); + float pixel = (static_cast<float>(ch) * 0.33); + + //sprintf(buf, "c%#x", ch); + //g_core.GetUSBServer().PushReliableMsg('D', buf); + + ch = ((VL & 0x07E0) >> 5); + pixel += (static_cast<float>(ch) * 0.33); + + //sprintf(buf, "c%#x", ch); + //g_core.GetUSBServer().PushReliableMsg('D', buf); + + ch = ((VL & 0x001F)); + pixel += (static_cast<float>(ch) * 0.33); + + //sprintf(buf, "c%#x", ch); + //g_core.GetUSBServer().PushReliableMsg('D', buf); + //sprintf(buf, "p%#x", static_cast<uint8_t>(pixel)); + //g_core.GetUSBServer().PushReliableMsg('D', buf); + + 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); + + std::string lineBuf; + lineBuf.resize(321); + + for (uint8_t i = 0; i < 240; ++i) + { + lineBuf[0] = i; + sprintf(buf, "Line %d", i); + g_core.GetUSBServer().PushReliableMsg('D', buf); + + for (int j = 0; j < 320; ++j) + { + uint8_t p = ardu_cam_get_pixel(); + //if(j < 159) + lineBuf[j + 1] = p; + } + + g_core.GetUSBServer().PushReliableMsg('P', lineBuf); + wait(0.5); + } + +} \ No newline at end of file