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:
Tue Mar 14 22:23:15 2017 +0000
Revision:
36:7e747e19f660
Parent:
32:5badeff825dc
Child:
37:7074a6118d03
Removed trash codes. Made it easier to switch different camera.

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
hazheng 29:f87d8790f57d 9 #include "ArduCAM.h"
Bobymicjohn 10:fedb5786a109 10
Bobymicjohn 10:fedb5786a109 11 #include "PinAssignment.h"
hazheng 29:f87d8790f57d 12 #include "GlobalVariable.h"
Bobymicjohn 8:92f6baeea027 13
hazheng 28:271fc8445e89 14 SPI g_spi_port(PIN_SPI_MOSI, PIN_SPI_MISO, PIN_SPI_SCK);
hazheng 29:f87d8790f57d 15 SW::Core g_core;
hazheng 5:577b582e4fe9 16
hazheng 3:c8867972ffc7 17 int main(void) {
hazheng 16:66c7a09e71ee 18
hazheng 16:66c7a09e71ee 19 Timer timer;
hazheng 16:66c7a09e71ee 20
hazheng 29:f87d8790f57d 21 g_spi_port.frequency(1000000);
hazheng 29:f87d8790f57d 22 //g_spi_port.format(8, 0);
hazheng 29:f87d8790f57d 23
hazheng 29:f87d8790f57d 24 //SW::Core core;
Bobymicjohn 10:fedb5786a109 25
hazheng 25:6f63053cee81 26 //Motor motor(core);
hazheng 25:6f63053cee81 27 //Servo servo(core);
hazheng 25:6f63053cee81 28 //WheelEncoder wheelEncoder(core);
hazheng 29:f87d8790f57d 29 //Camera cam(core);
hazheng 6:0682e1c90119 30
hazheng 16:66c7a09e71ee 31
hazheng 29:f87d8790f57d 32 bool isRegRead = false;
Bobymicjohn 14:88302da8bff0 33
Bobymicjohn 21:60377d6f1cc2 34 //motor.setLeftSpeed(0.2);
hazheng 32:5badeff825dc 35 //ardu_cam_print_debug();
hazheng 29:f87d8790f57d 36 ardu_cam_init();
hazheng 32:5badeff825dc 37 //ardu_cam_print_debug();
hazheng 29:f87d8790f57d 38
hazheng 18:bf6c5f8281eb 39 float tempCount = 0;
hazheng 18:bf6c5f8281eb 40 //timer.reset();
hazheng 16:66c7a09e71ee 41 timer.start();
hazheng 18:bf6c5f8281eb 42 float timeWas = timer.read();
hazheng 5:577b582e4fe9 43 while (1)
hazheng 5:577b582e4fe9 44 {
hazheng 18:bf6c5f8281eb 45
hazheng 18:bf6c5f8281eb 46 float deltaTime = timeWas;
hazheng 18:bf6c5f8281eb 47 timeWas = timer.read();
hazheng 18:bf6c5f8281eb 48 deltaTime = timeWas - deltaTime;
hazheng 18:bf6c5f8281eb 49
Bobymicjohn 11:676ea42afd56 50 //led = 0;
hazheng 29:f87d8790f57d 51 g_core.Update(deltaTime);
hazheng 25:6f63053cee81 52 //motor.Update(deltaTime);
hazheng 25:6f63053cee81 53 //servo.Update(deltaTime);
hazheng 25:6f63053cee81 54 //wheelEncoder.Update(deltaTime);
hazheng 29:f87d8790f57d 55 //cam.Update(deltaTime);
hazheng 16:66c7a09e71ee 56
hazheng 18:bf6c5f8281eb 57 tempCount += deltaTime;
hazheng 18:bf6c5f8281eb 58 if(tempCount > 1.0f)
hazheng 5:577b582e4fe9 59 {
hazheng 29:f87d8790f57d 60 //led = !led;
hazheng 18:bf6c5f8281eb 61 tempCount = 0.0f;
hazheng 3:c8867972ffc7 62 }
hazheng 3:c8867972ffc7 63
hazheng 29:f87d8790f57d 64
hazheng 32:5badeff825dc 65 if(!isRegRead && g_core.GetUSBServer().GetStatus() == SER_STAT_RUNNING && timer.read() > 2.5f && ardu_cam_get_fifo_length() > 0)
hazheng 13:7dcb1642ef99 66 {
hazheng 32:5badeff825dc 67 //OV7725RegBuf * regBuf = new OV7725RegBuf(g_core);
hazheng 32:5badeff825dc 68 //regBuf->ReadRegisters();
hazheng 32:5badeff825dc 69 //delete regBuf;
hazheng 32:5badeff825dc 70 ardu_cam_print_debug();
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 */