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
main.cpp@32:5badeff825dc, 2017-03-02 (annotated)
- Committer:
- hazheng
- Date:
- Thu Mar 02 23:56:42 2017 +0000
- Revision:
- 32:5badeff825dc
- Parent:
- 29:f87d8790f57d
- Child:
- 36:7e747e19f660
Successfully get the first picture from the camera and displayed on the computer screen.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hazheng | 3:c8867972ffc7 | 1 | #include "mbed.h" |
hazheng | 5:577b582e4fe9 | 2 | |
Bobymicjohn | 8:92f6baeea027 | 3 | #include "Motor.h" |
Bobymicjohn | 11:676ea42afd56 | 4 | #include "Servo.h" |
Bobymicjohn | 15:eb6a274b3dfb | 5 | #include "WheelEncoder.h" |
Bobymicjohn | 11:676ea42afd56 | 6 | #include "Core.h" |
Bobymicjohn | 11:676ea42afd56 | 7 | #include "SWUSBServer.h" |
hazheng | 29:f87d8790f57d | 8 | //#include "Camera.h" |
hazheng | 29:f87d8790f57d | 9 | #include "OV7725RegBuf.h" |
hazheng | 29:f87d8790f57d | 10 | |
hazheng | 29:f87d8790f57d | 11 | #include "ArduCAM.h" |
Bobymicjohn | 10:fedb5786a109 | 12 | |
Bobymicjohn | 10:fedb5786a109 | 13 | #include "PinAssignment.h" |
hazheng | 29:f87d8790f57d | 14 | #include "GlobalVariable.h" |
Bobymicjohn | 8:92f6baeea027 | 15 | |
hazheng | 28:271fc8445e89 | 16 | SPI g_spi_port(PIN_SPI_MOSI, PIN_SPI_MISO, PIN_SPI_SCK); |
hazheng | 29:f87d8790f57d | 17 | SW::Core g_core; |
hazheng | 5:577b582e4fe9 | 18 | |
hazheng | 3:c8867972ffc7 | 19 | int main(void) { |
hazheng | 16:66c7a09e71ee | 20 | |
hazheng | 16:66c7a09e71ee | 21 | Timer timer; |
hazheng | 16:66c7a09e71ee | 22 | |
hazheng | 29:f87d8790f57d | 23 | g_spi_port.frequency(1000000); |
hazheng | 29:f87d8790f57d | 24 | //g_spi_port.format(8, 0); |
hazheng | 29:f87d8790f57d | 25 | |
hazheng | 29:f87d8790f57d | 26 | //SW::Core core; |
Bobymicjohn | 10:fedb5786a109 | 27 | |
hazheng | 25:6f63053cee81 | 28 | //Motor motor(core); |
hazheng | 25:6f63053cee81 | 29 | //Servo servo(core); |
hazheng | 25:6f63053cee81 | 30 | //WheelEncoder wheelEncoder(core); |
hazheng | 29:f87d8790f57d | 31 | //Camera cam(core); |
hazheng | 6:0682e1c90119 | 32 | |
hazheng | 16:66c7a09e71ee | 33 | |
hazheng | 29:f87d8790f57d | 34 | bool isRegRead = false; |
Bobymicjohn | 14:88302da8bff0 | 35 | |
Bobymicjohn | 21:60377d6f1cc2 | 36 | //motor.setLeftSpeed(0.2); |
hazheng | 32:5badeff825dc | 37 | //ardu_cam_print_debug(); |
hazheng | 29:f87d8790f57d | 38 | ardu_cam_init(); |
hazheng | 32:5badeff825dc | 39 | //ardu_cam_print_debug(); |
hazheng | 29:f87d8790f57d | 40 | |
hazheng | 18:bf6c5f8281eb | 41 | float tempCount = 0; |
hazheng | 18:bf6c5f8281eb | 42 | //timer.reset(); |
hazheng | 16:66c7a09e71ee | 43 | timer.start(); |
hazheng | 18:bf6c5f8281eb | 44 | float timeWas = timer.read(); |
hazheng | 5:577b582e4fe9 | 45 | while (1) |
hazheng | 5:577b582e4fe9 | 46 | { |
hazheng | 18:bf6c5f8281eb | 47 | |
hazheng | 18:bf6c5f8281eb | 48 | float deltaTime = timeWas; |
hazheng | 18:bf6c5f8281eb | 49 | timeWas = timer.read(); |
hazheng | 18:bf6c5f8281eb | 50 | deltaTime = timeWas - deltaTime; |
hazheng | 18:bf6c5f8281eb | 51 | |
Bobymicjohn | 11:676ea42afd56 | 52 | //led = 0; |
hazheng | 29:f87d8790f57d | 53 | g_core.Update(deltaTime); |
hazheng | 25:6f63053cee81 | 54 | //motor.Update(deltaTime); |
hazheng | 25:6f63053cee81 | 55 | //servo.Update(deltaTime); |
hazheng | 25:6f63053cee81 | 56 | //wheelEncoder.Update(deltaTime); |
hazheng | 29:f87d8790f57d | 57 | //cam.Update(deltaTime); |
hazheng | 16:66c7a09e71ee | 58 | |
hazheng | 18:bf6c5f8281eb | 59 | tempCount += deltaTime; |
hazheng | 18:bf6c5f8281eb | 60 | if(tempCount > 1.0f) |
hazheng | 5:577b582e4fe9 | 61 | { |
hazheng | 29:f87d8790f57d | 62 | //led = !led; |
hazheng | 18:bf6c5f8281eb | 63 | tempCount = 0.0f; |
hazheng | 3:c8867972ffc7 | 64 | } |
hazheng | 3:c8867972ffc7 | 65 | |
hazheng | 29:f87d8790f57d | 66 | |
hazheng | 32:5badeff825dc | 67 | if(!isRegRead && g_core.GetUSBServer().GetStatus() == SER_STAT_RUNNING && timer.read() > 2.5f && ardu_cam_get_fifo_length() > 0) |
hazheng | 13:7dcb1642ef99 | 68 | { |
hazheng | 32:5badeff825dc | 69 | //OV7725RegBuf * regBuf = new OV7725RegBuf(g_core); |
hazheng | 32:5badeff825dc | 70 | //regBuf->ReadRegisters(); |
hazheng | 32:5badeff825dc | 71 | //delete regBuf; |
hazheng | 32:5badeff825dc | 72 | ardu_cam_print_debug(); |
hazheng | 13:7dcb1642ef99 | 73 | isRegRead = true; |
hazheng | 13:7dcb1642ef99 | 74 | } |
hazheng | 29:f87d8790f57d | 75 | |
Bobymicjohn | 8:92f6baeea027 | 76 | wait(0.01); |
hazheng | 3:c8867972ffc7 | 77 | } |
hazheng | 3:c8867972ffc7 | 78 | } |
hazheng | 3:c8867972ffc7 | 79 | |
hazheng | 3:c8867972ffc7 | 80 | /* |
hazheng | 3:c8867972ffc7 | 81 | PwmOut servo(PTE20); |
hazheng | 3:c8867972ffc7 | 82 | |
hazheng | 3:c8867972ffc7 | 83 | int main() { |
hazheng | 3:c8867972ffc7 | 84 | servo.period(0.020); // servo requires a 20ms period |
hazheng | 3:c8867972ffc7 | 85 | |
hazheng | 3:c8867972ffc7 | 86 | while (1) { |
hazheng | 3:c8867972ffc7 | 87 | for(float offset=0.0; offset<0.001; offset+=0.0001) { |
hazheng | 3:c8867972ffc7 | 88 | servo.pulsewidth(0.001 + offset); // servo position determined by a pulsewidth between 1-2ms |
hazheng | 3:c8867972ffc7 | 89 | wait(0.25); |
hazheng | 3:c8867972ffc7 | 90 | } |
hazheng | 3:c8867972ffc7 | 91 | } |
hazheng | 3:c8867972ffc7 | 92 | |
hazheng | 3:c8867972ffc7 | 93 | } |
hazheng | 13:7dcb1642ef99 | 94 | */ |
hazheng | 13:7dcb1642ef99 | 95 | |
hazheng | 13:7dcb1642ef99 | 96 | /* //code for accelerometer sensor. |
hazheng | 13:7dcb1642ef99 | 97 | const char regAddr = 0x0D; |
hazheng | 13:7dcb1642ef99 | 98 | char readValue = 0; |
hazheng | 13:7dcb1642ef99 | 99 | int result1 = m_sccbCtrl.write(0x1D<<1, ®Addr, 1, true); |
hazheng | 13:7dcb1642ef99 | 100 | int result2 = m_sccbCtrl.read(0x1D<<1, &readValue, 1, false); |
hazheng | 13:7dcb1642ef99 | 101 | char buf[20]; |
hazheng | 13:7dcb1642ef99 | 102 | sprintf(buf, "%#x-%#x-%d-%d", regAddr, readValue, result1, result2); |
hazheng | 13:7dcb1642ef99 | 103 | m_core.GetUSBServer().PushUnreliableMsg('D', buf); |
hazheng | 3:c8867972ffc7 | 104 | */ |