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@28:271fc8445e89, 2017-02-27 (annotated)
- Committer:
- hazheng
- Date:
- Mon Feb 27 21:43:59 2017 +0000
- Revision:
- 28:271fc8445e89
- Parent:
- 26:5814404856e2
- Child:
- 29:f87d8790f57d
Added basic framework for the ArduCAM. Started to change the hardware interface code into Pure C style, so that the call process will be faster and program size could be smaller.
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 | 13:7dcb1642ef99 | 8 | #include "Camera.h" |
Bobymicjohn | 10:fedb5786a109 | 9 | |
Bobymicjohn | 10:fedb5786a109 | 10 | #include "PinAssignment.h" |
Bobymicjohn | 8:92f6baeea027 | 11 | |
Bobymicjohn | 11:676ea42afd56 | 12 | //DigitalOut led(LED1, 1); |
hazheng | 28:271fc8445e89 | 13 | SPI g_spi_port(PIN_SPI_MOSI, PIN_SPI_MISO, PIN_SPI_SCK); |
hazheng | 5:577b582e4fe9 | 14 | |
hazheng | 3:c8867972ffc7 | 15 | int main(void) { |
hazheng | 16:66c7a09e71ee | 16 | |
hazheng | 16:66c7a09e71ee | 17 | Timer timer; |
hazheng | 16:66c7a09e71ee | 18 | |
Bobymicjohn | 11:676ea42afd56 | 19 | SW::Core core; |
Bobymicjohn | 10:fedb5786a109 | 20 | |
hazheng | 25:6f63053cee81 | 21 | //Motor motor(core); |
hazheng | 25:6f63053cee81 | 22 | //Servo servo(core); |
hazheng | 25:6f63053cee81 | 23 | //WheelEncoder wheelEncoder(core); |
hazheng | 13:7dcb1642ef99 | 24 | Camera cam(core); |
hazheng | 6:0682e1c90119 | 25 | |
hazheng | 18:bf6c5f8281eb | 26 | DigitalOut led(LED3, 1); |
hazheng | 16:66c7a09e71ee | 27 | |
hazheng | 25:6f63053cee81 | 28 | //bool isRegRead = false; |
Bobymicjohn | 14:88302da8bff0 | 29 | |
Bobymicjohn | 21:60377d6f1cc2 | 30 | //motor.setLeftSpeed(0.2); |
Bobymicjohn | 21:60377d6f1cc2 | 31 | |
hazheng | 18:bf6c5f8281eb | 32 | float tempCount = 0; |
hazheng | 18:bf6c5f8281eb | 33 | //timer.reset(); |
hazheng | 16:66c7a09e71ee | 34 | timer.start(); |
hazheng | 18:bf6c5f8281eb | 35 | float timeWas = timer.read(); |
hazheng | 5:577b582e4fe9 | 36 | while (1) |
hazheng | 5:577b582e4fe9 | 37 | { |
hazheng | 18:bf6c5f8281eb | 38 | |
hazheng | 18:bf6c5f8281eb | 39 | float deltaTime = timeWas; |
hazheng | 18:bf6c5f8281eb | 40 | timeWas = timer.read(); |
hazheng | 18:bf6c5f8281eb | 41 | deltaTime = timeWas - deltaTime; |
hazheng | 18:bf6c5f8281eb | 42 | |
Bobymicjohn | 11:676ea42afd56 | 43 | //led = 0; |
hazheng | 16:66c7a09e71ee | 44 | core.Update(deltaTime); |
hazheng | 25:6f63053cee81 | 45 | //motor.Update(deltaTime); |
hazheng | 25:6f63053cee81 | 46 | //servo.Update(deltaTime); |
hazheng | 25:6f63053cee81 | 47 | //wheelEncoder.Update(deltaTime); |
hazheng | 26:5814404856e2 | 48 | cam.Update(deltaTime); |
hazheng | 16:66c7a09e71ee | 49 | |
hazheng | 18:bf6c5f8281eb | 50 | tempCount += deltaTime; |
hazheng | 18:bf6c5f8281eb | 51 | if(tempCount > 1.0f) |
hazheng | 5:577b582e4fe9 | 52 | { |
hazheng | 18:bf6c5f8281eb | 53 | led = !led; |
hazheng | 18:bf6c5f8281eb | 54 | tempCount = 0.0f; |
hazheng | 3:c8867972ffc7 | 55 | } |
hazheng | 3:c8867972ffc7 | 56 | |
Bobymicjohn | 15:eb6a274b3dfb | 57 | /* |
hazheng | 13:7dcb1642ef99 | 58 | if(!isRegRead && core.GetUSBServer().GetStatus() == SER_STAT_RUNNING) |
hazheng | 13:7dcb1642ef99 | 59 | { |
hazheng | 13:7dcb1642ef99 | 60 | cam.ReadRegisters(); |
hazheng | 13:7dcb1642ef99 | 61 | isRegRead = true; |
hazheng | 13:7dcb1642ef99 | 62 | } |
Bobymicjohn | 15:eb6a274b3dfb | 63 | */ |
Bobymicjohn | 8:92f6baeea027 | 64 | wait(0.01); |
hazheng | 3:c8867972ffc7 | 65 | } |
hazheng | 3:c8867972ffc7 | 66 | } |
hazheng | 3:c8867972ffc7 | 67 | |
hazheng | 3:c8867972ffc7 | 68 | /* |
hazheng | 3:c8867972ffc7 | 69 | PwmOut servo(PTE20); |
hazheng | 3:c8867972ffc7 | 70 | |
hazheng | 3:c8867972ffc7 | 71 | int main() { |
hazheng | 3:c8867972ffc7 | 72 | servo.period(0.020); // servo requires a 20ms period |
hazheng | 3:c8867972ffc7 | 73 | |
hazheng | 3:c8867972ffc7 | 74 | while (1) { |
hazheng | 3:c8867972ffc7 | 75 | for(float offset=0.0; offset<0.001; offset+=0.0001) { |
hazheng | 3:c8867972ffc7 | 76 | servo.pulsewidth(0.001 + offset); // servo position determined by a pulsewidth between 1-2ms |
hazheng | 3:c8867972ffc7 | 77 | wait(0.25); |
hazheng | 3:c8867972ffc7 | 78 | } |
hazheng | 3:c8867972ffc7 | 79 | } |
hazheng | 3:c8867972ffc7 | 80 | |
hazheng | 3:c8867972ffc7 | 81 | } |
hazheng | 13:7dcb1642ef99 | 82 | */ |
hazheng | 13:7dcb1642ef99 | 83 | |
hazheng | 13:7dcb1642ef99 | 84 | /* //code for accelerometer sensor. |
hazheng | 13:7dcb1642ef99 | 85 | const char regAddr = 0x0D; |
hazheng | 13:7dcb1642ef99 | 86 | char readValue = 0; |
hazheng | 13:7dcb1642ef99 | 87 | int result1 = m_sccbCtrl.write(0x1D<<1, ®Addr, 1, true); |
hazheng | 13:7dcb1642ef99 | 88 | int result2 = m_sccbCtrl.read(0x1D<<1, &readValue, 1, false); |
hazheng | 13:7dcb1642ef99 | 89 | char buf[20]; |
hazheng | 13:7dcb1642ef99 | 90 | sprintf(buf, "%#x-%#x-%d-%d", regAddr, readValue, result1, result2); |
hazheng | 13:7dcb1642ef99 | 91 | m_core.GetUSBServer().PushUnreliableMsg('D', buf); |
hazheng | 3:c8867972ffc7 | 92 | */ |