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:
Thu Mar 30 22:34:20 2017 +0000
Revision:
46:a5eb9bd3bb55
Parent:
45:501b7909139a
Child:
47:a682be9908b9
Child:
48:f76b5e252444
Changed more code into pure C style. Finished driving complete circle! Both screen and camera works fine!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hazheng 3:c8867972ffc7 1 #include "mbed.h"
hazheng 5:577b582e4fe9 2
hazheng 44:15de535c4005 3 #include <math.h>
hazheng 46:a5eb9bd3bb55 4 #define PI 3.14159265f
hazheng 44:15de535c4005 5
Bobymicjohn 8:92f6baeea027 6 #include "Motor.h"
Bobymicjohn 11:676ea42afd56 7 #include "Servo.h"
Bobymicjohn 15:eb6a274b3dfb 8 #include "WheelEncoder.h"
Bobymicjohn 11:676ea42afd56 9 #include "Core.h"
Bobymicjohn 11:676ea42afd56 10 #include "SWUSBServer.h"
hazheng 29:f87d8790f57d 11
hazheng 29:f87d8790f57d 12 #include "ArduCAM.h"
hazheng 40:be98219930e4 13 #include "ArduUTFT.h"
Bobymicjohn 10:fedb5786a109 14
Bobymicjohn 10:fedb5786a109 15 #include "PinAssignment.h"
hazheng 29:f87d8790f57d 16 #include "GlobalVariable.h"
Bobymicjohn 8:92f6baeea027 17
hazheng 28:271fc8445e89 18 SPI g_spi_port(PIN_SPI_MOSI, PIN_SPI_MISO, PIN_SPI_SCK);
hazheng 29:f87d8790f57d 19 SW::Core g_core;
hazheng 5:577b582e4fe9 20
hazheng 3:c8867972ffc7 21 int main(void) {
hazheng 16:66c7a09e71ee 22
hazheng 16:66c7a09e71ee 23 Timer timer;
hazheng 16:66c7a09e71ee 24
hazheng 44:15de535c4005 25 g_spi_port.frequency(5500000);
hazheng 29:f87d8790f57d 26 //g_spi_port.format(8, 0);
hazheng 29:f87d8790f57d 27
hazheng 29:f87d8790f57d 28 //SW::Core core;
Bobymicjohn 10:fedb5786a109 29
hazheng 46:a5eb9bd3bb55 30 //Motor motor(g_core);
hazheng 45:501b7909139a 31 //Servo servo(g_core);
hazheng 25:6f63053cee81 32 //WheelEncoder wheelEncoder(core);
hazheng 29:f87d8790f57d 33 //Camera cam(core);
hazheng 46:a5eb9bd3bb55 34 motor_init();
hazheng 45:501b7909139a 35 servo_init();
hazheng 16:66c7a09e71ee 36
hazheng 29:f87d8790f57d 37 bool isRegRead = false;
Bobymicjohn 14:88302da8bff0 38
hazheng 40:be98219930e4 39
hazheng 40:be98219930e4 40 ardu_utft_init();
hazheng 29:f87d8790f57d 41
hazheng 41:7b21c5e3599e 42
hazheng 41:7b21c5e3599e 43 ardu_cam_init();
hazheng 41:7b21c5e3599e 44
hazheng 18:bf6c5f8281eb 45 float tempCount = 0;
hazheng 18:bf6c5f8281eb 46 //timer.reset();
hazheng 16:66c7a09e71ee 47 timer.start();
hazheng 18:bf6c5f8281eb 48 float timeWas = timer.read();
hazheng 44:15de535c4005 49
hazheng 46:a5eb9bd3bb55 50 motor_set_speeds(0.12f, 0.12f);
hazheng 46:a5eb9bd3bb55 51 servo_set_angle(0.0f);
hazheng 5:577b582e4fe9 52 while (1)
hazheng 5:577b582e4fe9 53 {
hazheng 18:bf6c5f8281eb 54
hazheng 46:a5eb9bd3bb55 55 //motor_set_speeds(0.12f, 0.12f);
hazheng 46:a5eb9bd3bb55 56 //servo_set_angle(0.0f);
hazheng 46:a5eb9bd3bb55 57
hazheng 18:bf6c5f8281eb 58 float deltaTime = timeWas;
hazheng 18:bf6c5f8281eb 59 timeWas = timer.read();
hazheng 18:bf6c5f8281eb 60 deltaTime = timeWas - deltaTime;
hazheng 18:bf6c5f8281eb 61
Bobymicjohn 11:676ea42afd56 62 //led = 0;
hazheng 29:f87d8790f57d 63 g_core.Update(deltaTime);
hazheng 46:a5eb9bd3bb55 64 //motor.Update(deltaTime);
hazheng 45:501b7909139a 65 //servo.Update(deltaTime);
hazheng 25:6f63053cee81 66 //wheelEncoder.Update(deltaTime);
hazheng 29:f87d8790f57d 67 //cam.Update(deltaTime);
hazheng 16:66c7a09e71ee 68
hazheng 18:bf6c5f8281eb 69 tempCount += deltaTime;
hazheng 18:bf6c5f8281eb 70 if(tempCount > 1.0f)
hazheng 5:577b582e4fe9 71 {
hazheng 29:f87d8790f57d 72 //led = !led;
hazheng 18:bf6c5f8281eb 73 tempCount = 0.0f;
hazheng 3:c8867972ffc7 74 }
hazheng 3:c8867972ffc7 75
hazheng 41:7b21c5e3599e 76
hazheng 44:15de535c4005 77 if(!isRegRead && g_core.GetUSBServer().GetStatus() == SER_STAT_RUNNING && timer.read() > 2.5f && ardu_cam_is_capture_finished())
hazheng 13:7dcb1642ef99 78 {
hazheng 32:5badeff825dc 79 //OV7725RegBuf * regBuf = new OV7725RegBuf(g_core);
hazheng 32:5badeff825dc 80 //regBuf->ReadRegisters();
hazheng 32:5badeff825dc 81 //delete regBuf;
hazheng 44:15de535c4005 82 //ardu_cam_print_debug();
hazheng 13:7dcb1642ef99 83 isRegRead = true;
hazheng 13:7dcb1642ef99 84 }
hazheng 41:7b21c5e3599e 85
hazheng 44:15de535c4005 86 //ardu_cam_display_img_utft();
hazheng 46:a5eb9bd3bb55 87
hazheng 44:15de535c4005 88 volatile const uint8_t * centerLine = ardu_cam_get_center_array();
hazheng 46:a5eb9bd3bb55 89
hazheng 46:a5eb9bd3bb55 90 /////////////Calculate the curvature:
hazheng 46:a5eb9bd3bb55 91 float srcY = (0.0f + 1.0f) / 2.0f;
hazheng 46:a5eb9bd3bb55 92 float srcX = static_cast<float>(centerLine[0] + centerLine[1]) / 2.0f;
hazheng 46:a5eb9bd3bb55 93
hazheng 46:a5eb9bd3bb55 94 float destY = static_cast<float>(CAM_ROI_UPPER_LIMIT - 1 + (CAM_ROI_UPPER_LIMIT - 2)) / 2.0f;
hazheng 46:a5eb9bd3bb55 95 float destX = static_cast<float>(centerLine[CAM_ROI_UPPER_LIMIT - 1] + centerLine[CAM_ROI_UPPER_LIMIT - 2]) / 2.0f;
hazheng 46:a5eb9bd3bb55 96
hazheng 46:a5eb9bd3bb55 97 float disY = destY - srcY;
hazheng 46:a5eb9bd3bb55 98 float disX = destX - srcX;
hazheng 46:a5eb9bd3bb55 99
hazheng 46:a5eb9bd3bb55 100 float angle = static_cast<float>(atan(static_cast<double>(disX / disY)) * (180.0f / PI));
hazheng 46:a5eb9bd3bb55 101 ///////////////////////////////////////
hazheng 46:a5eb9bd3bb55 102
hazheng 46:a5eb9bd3bb55 103 /////////////Calculate the offset:
hazheng 44:15de535c4005 104 float centerPos = static_cast<float>(RESOLUTION_WIDTH / 2);
hazheng 44:15de535c4005 105 float offsetPercent = (static_cast<float>(centerLine[3]) - centerPos);
hazheng 44:15de535c4005 106 offsetPercent = offsetPercent / centerPos;
hazheng 46:a5eb9bd3bb55 107 //////////////////////////////////////
hazheng 46:a5eb9bd3bb55 108
hazheng 46:a5eb9bd3bb55 109 servo_set_angle((angle * -0.4f) + (offsetPercent * SERVO_MAX_ANGLE));
hazheng 46:a5eb9bd3bb55 110
hazheng 46:a5eb9bd3bb55 111 //char buf[20];
hazheng 46:a5eb9bd3bb55 112 //sprintf(buf, "angle %f", angle);
hazheng 46:a5eb9bd3bb55 113 //g_core.GetUSBServer().PushUnreliableMsg('D', buf);
hazheng 46:a5eb9bd3bb55 114 /*
hazheng 46:a5eb9bd3bb55 115 std::string tempStr = "XX";
hazheng 46:a5eb9bd3bb55 116 for(uint8_t i = 0; i < CAM_ROI_UPPER_LIMIT; ++i)
hazheng 46:a5eb9bd3bb55 117 {
hazheng 46:a5eb9bd3bb55 118 tempStr[0] = i;
hazheng 46:a5eb9bd3bb55 119 tempStr[1] = centerLine[i];
hazheng 46:a5eb9bd3bb55 120 g_core.GetUSBServer().PushUnreliableMsg('L', tempStr);
hazheng 46:a5eb9bd3bb55 121 }
hazheng 46:a5eb9bd3bb55 122 */
hazheng 46:a5eb9bd3bb55 123
hazheng 46:a5eb9bd3bb55 124 /*
hazheng 44:15de535c4005 125 if(offsetPercent > 0.1)
hazheng 44:15de535c4005 126 {
hazheng 46:a5eb9bd3bb55 127 motor_set_speeds(0.05f, 0.05f);
hazheng 44:15de535c4005 128 }
hazheng 44:15de535c4005 129 else
hazheng 44:15de535c4005 130 {
hazheng 46:a5eb9bd3bb55 131 motor_set_speeds(0.13f, 0.13f);
hazheng 44:15de535c4005 132 }
hazheng 46:a5eb9bd3bb55 133 */
hazheng 43:0d1886f4848a 134 //wait(0.01);
hazheng 3:c8867972ffc7 135 }
hazheng 3:c8867972ffc7 136 }
hazheng 3:c8867972ffc7 137
hazheng 3:c8867972ffc7 138 /*
hazheng 3:c8867972ffc7 139 PwmOut servo(PTE20);
hazheng 3:c8867972ffc7 140
hazheng 3:c8867972ffc7 141 int main() {
hazheng 3:c8867972ffc7 142 servo.period(0.020); // servo requires a 20ms period
hazheng 3:c8867972ffc7 143
hazheng 3:c8867972ffc7 144 while (1) {
hazheng 3:c8867972ffc7 145 for(float offset=0.0; offset<0.001; offset+=0.0001) {
hazheng 3:c8867972ffc7 146 servo.pulsewidth(0.001 + offset); // servo position determined by a pulsewidth between 1-2ms
hazheng 3:c8867972ffc7 147 wait(0.25);
hazheng 3:c8867972ffc7 148 }
hazheng 3:c8867972ffc7 149 }
hazheng 3:c8867972ffc7 150
hazheng 3:c8867972ffc7 151 }
hazheng 13:7dcb1642ef99 152 */
hazheng 13:7dcb1642ef99 153
hazheng 13:7dcb1642ef99 154 /* //code for accelerometer sensor.
hazheng 13:7dcb1642ef99 155 const char regAddr = 0x0D;
hazheng 13:7dcb1642ef99 156 char readValue = 0;
hazheng 13:7dcb1642ef99 157 int result1 = m_sccbCtrl.write(0x1D<<1, &regAddr, 1, true);
hazheng 13:7dcb1642ef99 158 int result2 = m_sccbCtrl.read(0x1D<<1, &readValue, 1, false);
hazheng 13:7dcb1642ef99 159 char buf[20];
hazheng 13:7dcb1642ef99 160 sprintf(buf, "%#x-%#x-%d-%d", regAddr, readValue, result1, result2);
hazheng 13:7dcb1642ef99 161 m_core.GetUSBServer().PushUnreliableMsg('D', buf);
hazheng 3:c8867972ffc7 162 */