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 haofan Zheng

Committer:
hazheng
Date:
Wed Mar 01 16:31:36 2017 +0000
Revision:
29:f87d8790f57d
Parent:
28:271fc8445e89
Child:
32:5badeff825dc
Added test code for ArduCam. Cam is not working yet.

Who changed what in which revision?

UserRevisionLine numberNew 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);
Bobymicjohn 21:60377d6f1cc2 37
hazheng 29:f87d8790f57d 38 ardu_cam_init();
hazheng 29:f87d8790f57d 39
hazheng 18:bf6c5f8281eb 40 float tempCount = 0;
hazheng 18:bf6c5f8281eb 41 //timer.reset();
hazheng 16:66c7a09e71ee 42 timer.start();
hazheng 18:bf6c5f8281eb 43 float timeWas = timer.read();
hazheng 5:577b582e4fe9 44 while (1)
hazheng 5:577b582e4fe9 45 {
hazheng 18:bf6c5f8281eb 46
hazheng 18:bf6c5f8281eb 47 float deltaTime = timeWas;
hazheng 18:bf6c5f8281eb 48 timeWas = timer.read();
hazheng 18:bf6c5f8281eb 49 deltaTime = timeWas - deltaTime;
hazheng 18:bf6c5f8281eb 50
Bobymicjohn 11:676ea42afd56 51 //led = 0;
hazheng 29:f87d8790f57d 52 g_core.Update(deltaTime);
hazheng 25:6f63053cee81 53 //motor.Update(deltaTime);
hazheng 25:6f63053cee81 54 //servo.Update(deltaTime);
hazheng 25:6f63053cee81 55 //wheelEncoder.Update(deltaTime);
hazheng 29:f87d8790f57d 56 //cam.Update(deltaTime);
hazheng 16:66c7a09e71ee 57
hazheng 18:bf6c5f8281eb 58 tempCount += deltaTime;
hazheng 18:bf6c5f8281eb 59 if(tempCount > 1.0f)
hazheng 5:577b582e4fe9 60 {
hazheng 29:f87d8790f57d 61 //led = !led;
hazheng 18:bf6c5f8281eb 62 tempCount = 0.0f;
hazheng 3:c8867972ffc7 63 }
hazheng 3:c8867972ffc7 64
hazheng 29:f87d8790f57d 65
hazheng 29:f87d8790f57d 66 if(!isRegRead && g_core.GetUSBServer().GetStatus() == SER_STAT_RUNNING)
hazheng 13:7dcb1642ef99 67 {
hazheng 29:f87d8790f57d 68 OV7725RegBuf * regBuf = new OV7725RegBuf(g_core);
hazheng 29:f87d8790f57d 69 regBuf->ReadRegisters();
hazheng 29:f87d8790f57d 70 delete regBuf;
hazheng 13:7dcb1642ef99 71 isRegRead = true;
hazheng 13:7dcb1642ef99 72 }
hazheng 29:f87d8790f57d 73
Bobymicjohn 8:92f6baeea027 74 wait(0.01);
hazheng 3:c8867972ffc7 75 }
hazheng 3:c8867972ffc7 76 }
hazheng 3:c8867972ffc7 77
hazheng 3:c8867972ffc7 78 /*
hazheng 3:c8867972ffc7 79 PwmOut servo(PTE20);
hazheng 3:c8867972ffc7 80
hazheng 3:c8867972ffc7 81 int main() {
hazheng 3:c8867972ffc7 82 servo.period(0.020); // servo requires a 20ms period
hazheng 3:c8867972ffc7 83
hazheng 3:c8867972ffc7 84 while (1) {
hazheng 3:c8867972ffc7 85 for(float offset=0.0; offset<0.001; offset+=0.0001) {
hazheng 3:c8867972ffc7 86 servo.pulsewidth(0.001 + offset); // servo position determined by a pulsewidth between 1-2ms
hazheng 3:c8867972ffc7 87 wait(0.25);
hazheng 3:c8867972ffc7 88 }
hazheng 3:c8867972ffc7 89 }
hazheng 3:c8867972ffc7 90
hazheng 3:c8867972ffc7 91 }
hazheng 13:7dcb1642ef99 92 */
hazheng 13:7dcb1642ef99 93
hazheng 13:7dcb1642ef99 94 /* //code for accelerometer sensor.
hazheng 13:7dcb1642ef99 95 const char regAddr = 0x0D;
hazheng 13:7dcb1642ef99 96 char readValue = 0;
hazheng 13:7dcb1642ef99 97 int result1 = m_sccbCtrl.write(0x1D<<1, &regAddr, 1, true);
hazheng 13:7dcb1642ef99 98 int result2 = m_sccbCtrl.read(0x1D<<1, &readValue, 1, false);
hazheng 13:7dcb1642ef99 99 char buf[20];
hazheng 13:7dcb1642ef99 100 sprintf(buf, "%#x-%#x-%d-%d", regAddr, readValue, result1, result2);
hazheng 13:7dcb1642ef99 101 m_core.GetUSBServer().PushUnreliableMsg('D', buf);
hazheng 3:c8867972ffc7 102 */