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