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/Camera.cpp@13:7dcb1642ef99, 2017-02-08 (annotated)
- Committer:
- hazheng
- Date:
- Wed Feb 08 23:47:02 2017 +0000
- Revision:
- 13:7dcb1642ef99
- Parent:
- 9:b72e18f80f49
- Child:
- 25:6f63053cee81
Finished reading SCCB from camera.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hazheng | 3:c8867972ffc7 | 1 | #include "Camera.h" |
hazheng | 3:c8867972ffc7 | 2 | |
hazheng | 13:7dcb1642ef99 | 3 | #include "Core.h" |
hazheng | 13:7dcb1642ef99 | 4 | #include "OV7725RegAddr.h" |
hazheng | 13:7dcb1642ef99 | 5 | #include "PinAssignment.h" |
hazheng | 13:7dcb1642ef99 | 6 | #include "SWUSBServer.h" |
hazheng | 13:7dcb1642ef99 | 7 | |
hazheng | 13:7dcb1642ef99 | 8 | |
hazheng | 3:c8867972ffc7 | 9 | DigitalOut testLED(LED_BLUE, 1); |
hazheng | 3:c8867972ffc7 | 10 | |
hazheng | 3:c8867972ffc7 | 11 | void Camera::OnPixelClock() |
hazheng | 3:c8867972ffc7 | 12 | { |
hazheng | 13:7dcb1642ef99 | 13 | /* |
hazheng | 3:c8867972ffc7 | 14 | m_pics[m_currentIndex][(m_currentRow * CAM_MAX_COL) + m_currentCol++] = 0;//m_dIn[0].read(); |
hazheng | 3:c8867972ffc7 | 15 | m_pics[m_currentIndex][(m_currentRow * CAM_MAX_COL) + m_currentCol++] = 0;//m_dIn[1].read(); |
hazheng | 3:c8867972ffc7 | 16 | m_pics[m_currentIndex][(m_currentRow * CAM_MAX_COL) + m_currentCol++] = 0;//m_dIn[2].read(); |
hazheng | 3:c8867972ffc7 | 17 | m_pics[m_currentIndex][(m_currentRow * CAM_MAX_COL) + m_currentCol++] = 0;//m_dIn[3].read(); |
hazheng | 3:c8867972ffc7 | 18 | m_pics[m_currentIndex][(m_currentRow * CAM_MAX_COL) + m_currentCol++] = 0;//m_dIn[4].read(); |
hazheng | 3:c8867972ffc7 | 19 | m_pics[m_currentIndex][(m_currentRow * CAM_MAX_COL) + m_currentCol++] = 0;//m_dIn[5].read(); |
hazheng | 3:c8867972ffc7 | 20 | m_pics[m_currentIndex][(m_currentRow * CAM_MAX_COL) + m_currentCol++] = 0;//m_dIn[6].read(); |
hazheng | 3:c8867972ffc7 | 21 | m_pics[m_currentIndex][(m_currentRow * CAM_MAX_COL) + m_currentCol++] = 0;//m_dIn[7].read(); |
hazheng | 13:7dcb1642ef99 | 22 | */ |
hazheng | 3:c8867972ffc7 | 23 | } |
hazheng | 3:c8867972ffc7 | 24 | |
hazheng | 3:c8867972ffc7 | 25 | void Camera::OnHorizontalClock() |
hazheng | 3:c8867972ffc7 | 26 | { |
hazheng | 3:c8867972ffc7 | 27 | m_currentRow++; |
hazheng | 3:c8867972ffc7 | 28 | m_currentCol = 0; |
hazheng | 3:c8867972ffc7 | 29 | } |
hazheng | 3:c8867972ffc7 | 30 | |
hazheng | 3:c8867972ffc7 | 31 | void Camera::OnFrameClock() |
hazheng | 3:c8867972ffc7 | 32 | { |
hazheng | 3:c8867972ffc7 | 33 | m_currentIndex = m_currentIndex == 0 ? 1 : 0; |
hazheng | 3:c8867972ffc7 | 34 | m_currentRow = 0; |
hazheng | 3:c8867972ffc7 | 35 | m_currentCol = 0; |
hazheng | 3:c8867972ffc7 | 36 | m_hasPic = true; |
hazheng | 3:c8867972ffc7 | 37 | |
hazheng | 3:c8867972ffc7 | 38 | testLED = testLED.read() == 1 ? 0 : 1; |
hazheng | 3:c8867972ffc7 | 39 | } |
hazheng | 3:c8867972ffc7 | 40 | |
hazheng | 13:7dcb1642ef99 | 41 | Camera::Camera(SW::Core & core) : |
hazheng | 13:7dcb1642ef99 | 42 | m_core(core), |
hazheng | 3:c8867972ffc7 | 43 | m_pClock(InterruptIn(PTD7)), |
hazheng | 3:c8867972ffc7 | 44 | m_href(InterruptIn(PTD6)), |
hazheng | 3:c8867972ffc7 | 45 | m_vsnyc(InterruptIn(PTA17)), |
hazheng | 13:7dcb1642ef99 | 46 | m_sccbCtrl(I2C(PIN_CC_SDA, PIN_CC_SCL)), |
hazheng | 3:c8867972ffc7 | 47 | m_currentIndex(0), |
hazheng | 3:c8867972ffc7 | 48 | m_currentRow(0), |
hazheng | 3:c8867972ffc7 | 49 | m_currentCol(0), |
hazheng | 3:c8867972ffc7 | 50 | m_hasPic(false) |
hazheng | 3:c8867972ffc7 | 51 | { |
hazheng | 13:7dcb1642ef99 | 52 | /* |
hazheng | 3:c8867972ffc7 | 53 | m_dIn.push_back(DigitalIn(PTE5)); |
hazheng | 3:c8867972ffc7 | 54 | m_dIn.push_back(DigitalIn(PTE4)); |
hazheng | 3:c8867972ffc7 | 55 | m_dIn.push_back(DigitalIn(PTE3)); |
hazheng | 3:c8867972ffc7 | 56 | m_dIn.push_back(DigitalIn(PTE2)); |
hazheng | 3:c8867972ffc7 | 57 | m_dIn.push_back(DigitalIn(PTB11)); |
hazheng | 3:c8867972ffc7 | 58 | m_dIn.push_back(DigitalIn(PTB10)); |
hazheng | 3:c8867972ffc7 | 59 | m_dIn.push_back(DigitalIn(PTB9)); |
hazheng | 3:c8867972ffc7 | 60 | m_dIn.push_back(DigitalIn(PTB8)); |
hazheng | 13:7dcb1642ef99 | 61 | */ |
hazheng | 3:c8867972ffc7 | 62 | //m_pClock.fall(callback(this, &Camera::OnPixelClock)); |
hazheng | 13:7dcb1642ef99 | 63 | //m_href.rise(callback(this, &Camera::OnHorizontalClock)); |
hazheng | 13:7dcb1642ef99 | 64 | //m_vsnyc.rise(callback(this, &Camera::OnFrameClock)); |
hazheng | 3:c8867972ffc7 | 65 | |
hazheng | 3:c8867972ffc7 | 66 | } |
hazheng | 3:c8867972ffc7 | 67 | |
hazheng | 3:c8867972ffc7 | 68 | Camera::~Camera() |
hazheng | 3:c8867972ffc7 | 69 | { |
hazheng | 3:c8867972ffc7 | 70 | |
hazheng | 3:c8867972ffc7 | 71 | } |
hazheng | 3:c8867972ffc7 | 72 | |
hazheng | 3:c8867972ffc7 | 73 | bool Camera::HasPicture() const |
hazheng | 3:c8867972ffc7 | 74 | { |
hazheng | 3:c8867972ffc7 | 75 | return m_hasPic; |
hazheng | 3:c8867972ffc7 | 76 | } |
hazheng | 3:c8867972ffc7 | 77 | |
hazheng | 3:c8867972ffc7 | 78 | const unsigned char * Camera::GetPicture() const |
hazheng | 3:c8867972ffc7 | 79 | { |
hazheng | 13:7dcb1642ef99 | 80 | return NULL; //m_pics[m_currentIndex == 0 ? 1 : 0]; |
hazheng | 13:7dcb1642ef99 | 81 | } |
hazheng | 13:7dcb1642ef99 | 82 | |
hazheng | 13:7dcb1642ef99 | 83 | //Blocking method. Do not use during the running state!! |
hazheng | 13:7dcb1642ef99 | 84 | void Camera::SCCBWrite(uint8_t RegAddr, uint8_t Data) |
hazheng | 13:7dcb1642ef99 | 85 | { |
hazheng | 13:7dcb1642ef99 | 86 | m_sccbCtrl.start(); |
hazheng | 13:7dcb1642ef99 | 87 | m_sccbCtrl.write(OV7725_WRITE); |
hazheng | 13:7dcb1642ef99 | 88 | wait_us(OV7725_WRITEWAIT); |
hazheng | 13:7dcb1642ef99 | 89 | m_sccbCtrl.write(RegAddr); |
hazheng | 13:7dcb1642ef99 | 90 | wait_us(OV7725_WRITEWAIT); |
hazheng | 13:7dcb1642ef99 | 91 | m_sccbCtrl.write(Data); |
hazheng | 13:7dcb1642ef99 | 92 | m_sccbCtrl.stop(); |
hazheng | 13:7dcb1642ef99 | 93 | } |
hazheng | 13:7dcb1642ef99 | 94 | |
hazheng | 13:7dcb1642ef99 | 95 | //Blocking method. Do not use during the running state!! |
hazheng | 13:7dcb1642ef99 | 96 | uint8_t Camera::SCCBRead(const uint8_t RegAddr) |
hazheng | 13:7dcb1642ef99 | 97 | { |
hazheng | 13:7dcb1642ef99 | 98 | m_sccbCtrl.start(); |
hazheng | 13:7dcb1642ef99 | 99 | m_sccbCtrl.write(OV7725_WRITE); |
hazheng | 13:7dcb1642ef99 | 100 | wait_us(OV7725_WRITEWAIT); |
hazheng | 13:7dcb1642ef99 | 101 | m_sccbCtrl.write(RegAddr); |
hazheng | 13:7dcb1642ef99 | 102 | m_sccbCtrl.stop(); |
hazheng | 13:7dcb1642ef99 | 103 | wait_us(OV7725_WRITEWAIT); |
hazheng | 13:7dcb1642ef99 | 104 | |
hazheng | 13:7dcb1642ef99 | 105 | m_sccbCtrl.start(); |
hazheng | 13:7dcb1642ef99 | 106 | m_sccbCtrl.write(OV7725_READ); |
hazheng | 13:7dcb1642ef99 | 107 | wait_us(OV7725_WRITEWAIT); |
hazheng | 13:7dcb1642ef99 | 108 | char readValue = m_sccbCtrl.read(OV7725_NOACK); |
hazheng | 13:7dcb1642ef99 | 109 | m_sccbCtrl.stop(); |
hazheng | 13:7dcb1642ef99 | 110 | |
hazheng | 13:7dcb1642ef99 | 111 | return readValue; |
hazheng | 13:7dcb1642ef99 | 112 | } |
hazheng | 13:7dcb1642ef99 | 113 | |
hazheng | 13:7dcb1642ef99 | 114 | //Blocking method. Do not use during the running state!! |
hazheng | 13:7dcb1642ef99 | 115 | void Camera::ReadRegisters() |
hazheng | 13:7dcb1642ef99 | 116 | { |
hazheng | 13:7dcb1642ef99 | 117 | |
hazheng | 13:7dcb1642ef99 | 118 | m_sccbCtrl.lock(); |
hazheng | 13:7dcb1642ef99 | 119 | m_sccbCtrl.frequency(100000); |
hazheng | 13:7dcb1642ef99 | 120 | for(int i = 0; i < OV7725_LAST_ADDR + 1; ++i) |
hazheng | 13:7dcb1642ef99 | 121 | { |
hazheng | 13:7dcb1642ef99 | 122 | if(!OV7725RegBuf::IsAddressReserved(i)) |
hazheng | 13:7dcb1642ef99 | 123 | { |
hazheng | 13:7dcb1642ef99 | 124 | char buf[10]; |
hazheng | 13:7dcb1642ef99 | 125 | sprintf(buf, "%#x-%#x", i, SCCBRead(i)); |
hazheng | 13:7dcb1642ef99 | 126 | m_core.GetUSBServer().PushUnreliableMsg('D', buf); |
hazheng | 13:7dcb1642ef99 | 127 | |
hazheng | 13:7dcb1642ef99 | 128 | wait(0.1); |
hazheng | 13:7dcb1642ef99 | 129 | } |
hazheng | 13:7dcb1642ef99 | 130 | } |
hazheng | 13:7dcb1642ef99 | 131 | m_sccbCtrl.unlock(); |
hazheng | 3:c8867972ffc7 | 132 | } |