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:
Sun Apr 16 21:31:26 2017 +0000
Branch:
Drift
Revision:
74:2d45b954deb1
Parent:
73:1dcf56e9f1d4
Child:
76:5de2cbe0c271
6 sec stable version 3

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
hazheng 63:d9a81b3d69f5 6 //#define SW_DEBUG
hazheng 56:7d3395ae022d 7 #include "SWCommon.h"
hazheng 56:7d3395ae022d 8 #include "GlobalVariable.h"
hazheng 56:7d3395ae022d 9
Bobymicjohn 8:92f6baeea027 10 #include "Motor.h"
Bobymicjohn 11:676ea42afd56 11 #include "Servo.h"
Bobymicjohn 15:eb6a274b3dfb 12 #include "WheelEncoder.h"
Bobymicjohn 11:676ea42afd56 13 #include "Core.h"
Bobymicjohn 11:676ea42afd56 14 #include "SWUSBServer.h"
hazheng 29:f87d8790f57d 15
hazheng 29:f87d8790f57d 16 #include "ArduCAM.h"
hazheng 40:be98219930e4 17 #include "ArduUTFT.h"
hazheng 62:bc5caf59fe39 18 #include "IMUManager.h"
Bobymicjohn 10:fedb5786a109 19
Bobymicjohn 10:fedb5786a109 20 #include "PinAssignment.h"
Bobymicjohn 8:92f6baeea027 21
hazheng 28:271fc8445e89 22 SPI g_spi_port(PIN_SPI_MOSI, PIN_SPI_MISO, PIN_SPI_SCK);
hazheng 29:f87d8790f57d 23 SW::Core g_core;
hazheng 5:577b582e4fe9 24
hazheng 56:7d3395ae022d 25 #ifdef SW_DEBUG
hazheng 56:7d3395ae022d 26 #include <rtos.h>
hazheng 56:7d3395ae022d 27 Mutex g_sw_spi_lock;
hazheng 56:7d3395ae022d 28 #endif
hazheng 56:7d3395ae022d 29
hazheng 3:c8867972ffc7 30 int main(void) {
hazheng 16:66c7a09e71ee 31
hazheng 16:66c7a09e71ee 32 Timer timer;
hazheng 16:66c7a09e71ee 33
hazheng 48:f76b5e252444 34 g_spi_port.frequency(5000000);
hazheng 29:f87d8790f57d 35 //g_spi_port.format(8, 0);
hazheng 29:f87d8790f57d 36
hazheng 29:f87d8790f57d 37 //SW::Core core;
Bobymicjohn 10:fedb5786a109 38
hazheng 46:a5eb9bd3bb55 39 //Motor motor(g_core);
hazheng 45:501b7909139a 40 //Servo servo(g_core);
hazheng 25:6f63053cee81 41 //WheelEncoder wheelEncoder(core);
hazheng 29:f87d8790f57d 42 //Camera cam(core);
hazheng 46:a5eb9bd3bb55 43 motor_init();
hazheng 45:501b7909139a 44 servo_init();
hazheng 16:66c7a09e71ee 45
hazheng 68:b15cab740371 46 //bool isRegRead = false;
Bobymicjohn 14:88302da8bff0 47
hazheng 40:be98219930e4 48
hazheng 40:be98219930e4 49 ardu_utft_init();
hazheng 29:f87d8790f57d 50
hazheng 41:7b21c5e3599e 51
hazheng 41:7b21c5e3599e 52 ardu_cam_init();
hazheng 41:7b21c5e3599e 53
hazheng 62:bc5caf59fe39 54
hazheng 64:43ab429a37e0 55 //uint8_t IMUInitResult = imu_manager_init();
hazheng 64:43ab429a37e0 56 //LOGI("IMU Init: %#x", IMUInitResult);
hazheng 64:43ab429a37e0 57 //imu_manager_calibrate();
hazheng 64:43ab429a37e0 58 //imu_manager_begin_tick();
hazheng 62:bc5caf59fe39 59 //wait(0.5);
hazheng 62:bc5caf59fe39 60
hazheng 68:b15cab740371 61 /* DeltaTime Calculation
hazheng 16:66c7a09e71ee 62 timer.start();
hazheng 18:bf6c5f8281eb 63 float timeWas = timer.read();
hazheng 68:b15cab740371 64 */
hazheng 46:a5eb9bd3bb55 65 servo_set_angle(0.0f);
Bobymicjohn 54:f1f5648dfacf 66
Bobymicjohn 54:f1f5648dfacf 67 float speedRatio = 1.0f;
hazheng 73:1dcf56e9f1d4 68 //float lastAngle = 0.0f;
hazheng 71:5601d2faa61d 69 float lastAngleAbs = 0.0f;
Bobymicjohn 58:996effac29b9 70 float cornerRatio = 1.0f;
Bobymicjohn 54:f1f5648dfacf 71
hazheng 68:b15cab740371 72 //DebugCounter counter(10, PTE5);
hazheng 57:0d8a155d511d 73
hazheng 5:577b582e4fe9 74 while (1)
hazheng 5:577b582e4fe9 75 {
hazheng 68:b15cab740371 76 /* DeltaTime Calculation
hazheng 18:bf6c5f8281eb 77 float deltaTime = timeWas;
hazheng 18:bf6c5f8281eb 78 timeWas = timer.read();
hazheng 18:bf6c5f8281eb 79 deltaTime = timeWas - deltaTime;
hazheng 68:b15cab740371 80 */
hazheng 41:7b21c5e3599e 81
hazheng 41:7b21c5e3599e 82
hazheng 44:15de535c4005 83 //ardu_cam_display_img_utft();
hazheng 57:0d8a155d511d 84 image_processing();
hazheng 64:43ab429a37e0 85 uint8_t shouldTerminate = ardu_cam_get_is_encounter_terminate();
hazheng 64:43ab429a37e0 86
hazheng 64:43ab429a37e0 87 if(shouldTerminate)
hazheng 64:43ab429a37e0 88 {
hazheng 65:295c222fdf88 89 ardu_utft_print("F", 230, 100);
hazheng 65:295c222fdf88 90 servo_set_angle(0.0f);
hazheng 66:c9b64f5337cc 91 motor_set_right_speed(-1.0f);
hazheng 66:c9b64f5337cc 92 motor_set_left_speed(-1.0f);
hazheng 68:b15cab740371 93 wait(0.4f);
hazheng 64:43ab429a37e0 94 motor_set_right_speed(0.0f);
hazheng 64:43ab429a37e0 95 motor_set_left_speed(0.0f);
hazheng 64:43ab429a37e0 96 return 0;
hazheng 64:43ab429a37e0 97 }
hazheng 65:295c222fdf88 98 //else
hazheng 65:295c222fdf88 99 //{
hazheng 65:295c222fdf88 100 // ardu_utft_print(" ", 230, 100);
hazheng 65:295c222fdf88 101 //}
hazheng 64:43ab429a37e0 102
hazheng 65:295c222fdf88 103 const volatile uint8_t* centerLine = ardu_cam_get_center_array();
hazheng 46:a5eb9bd3bb55 104
hazheng 46:a5eb9bd3bb55 105 /////////////Calculate the curvature:
hazheng 57:0d8a155d511d 106 float srcY = (0.0f + 1.0f) / 2.0f;
hazheng 57:0d8a155d511d 107 float srcX = static_cast<float>(centerLine[0] + centerLine[1]) / 2.0f;
hazheng 46:a5eb9bd3bb55 108
hazheng 57:0d8a155d511d 109 float destY = static_cast<float>(CAM_ROI_UPPER_LIMIT - 1 + (CAM_ROI_UPPER_LIMIT - 2)) / 2.0f;
hazheng 57:0d8a155d511d 110 float destX = static_cast<float>(centerLine[CAM_ROI_UPPER_LIMIT - 1] + centerLine[CAM_ROI_UPPER_LIMIT - 2]) / 2.0f;
hazheng 46:a5eb9bd3bb55 111
hazheng 46:a5eb9bd3bb55 112 float disY = destY - srcY;
hazheng 46:a5eb9bd3bb55 113 float disX = destX - srcX;
hazheng 46:a5eb9bd3bb55 114
hazheng 52:078b521c9edf 115 float angleRadians = -1.0f * static_cast<float>(atan(static_cast<double>(disX / disY)));
Bobymicjohn 58:996effac29b9 116 float angleDegrees = (angleRadians * (180.0f / PI));
Bobymicjohn 47:a682be9908b9 117
hazheng 46:a5eb9bd3bb55 118 ///////////////////////////////////////
hazheng 46:a5eb9bd3bb55 119
hazheng 46:a5eb9bd3bb55 120 /////////////Calculate the offset:
hazheng 44:15de535c4005 121 float centerPos = static_cast<float>(RESOLUTION_WIDTH / 2);
hazheng 52:078b521c9edf 122 float offsetDegrees = ((srcX - centerPos) / centerPos) * SERVO_MAX_ANGLE;
hazheng 46:a5eb9bd3bb55 123 //////////////////////////////////////
hazheng 46:a5eb9bd3bb55 124
hazheng 73:1dcf56e9f1d4 125 const uint8_t lineFoundRefRow = ((2 * CAM_ROI_UPPER_LIMIT) - 1) - (CAM_ROI_UPPER_LIMIT / 2);
Bobymicjohn 67:1b5c8add3d01 126
hazheng 73:1dcf56e9f1d4 127 float totalAngleDegrees = (angleDegrees * 0.52f) + (offsetDegrees * (centerLine[lineFoundRefRow] != BOTH_FOUND ? 0.90f : 0.35f));
hazheng 73:1dcf56e9f1d4 128
hazheng 73:1dcf56e9f1d4 129 if((totalAngleDegrees < 0.0f && centerLine[lineFoundRefRow] == LEFT_FOUND) ||
hazheng 73:1dcf56e9f1d4 130 (totalAngleDegrees > 0.0f && centerLine[lineFoundRefRow] == RIGHT_FOUND))
hazheng 65:295c222fdf88 131 {
hazheng 71:5601d2faa61d 132 totalAngleDegrees = ((-0.1f) * totalAngleDegrees);// + ((0.2f) * lastAngle);
hazheng 65:295c222fdf88 133 }
hazheng 73:1dcf56e9f1d4 134 LOGI("%d %5.3f ", centerLine[lineFoundRefRow], totalAngleDegrees);
hazheng 52:078b521c9edf 135 servo_set_angle(totalAngleDegrees);
hazheng 52:078b521c9edf 136
hazheng 65:295c222fdf88 137
hazheng 52:078b521c9edf 138 if(totalAngleDegrees > SERVO_MAX_ANGLE)
hazheng 52:078b521c9edf 139 totalAngleDegrees = SERVO_MAX_ANGLE;
hazheng 52:078b521c9edf 140 else if(totalAngleDegrees < -SERVO_MAX_ANGLE)
hazheng 52:078b521c9edf 141 totalAngleDegrees = -SERVO_MAX_ANGLE;
hazheng 73:1dcf56e9f1d4 142
hazheng 73:1dcf56e9f1d4 143 float totalAngleDegreesAbs = abs(totalAngleDegrees);
Bobymicjohn 54:f1f5648dfacf 144
hazheng 71:5601d2faa61d 145 if(totalAngleDegreesAbs > lastAngleAbs)
Bobymicjohn 58:996effac29b9 146 {
hazheng 74:2d45b954deb1 147 cornerRatio = 0.4;//(lastAngle / totalAngleDegrees);
Bobymicjohn 58:996effac29b9 148 }
hazheng 71:5601d2faa61d 149 else if(totalAngleDegreesAbs < lastAngleAbs)
Bobymicjohn 58:996effac29b9 150 {
hazheng 74:2d45b954deb1 151 cornerRatio = 0.8;//(lastAngle / totalAngleDegrees);
Bobymicjohn 58:996effac29b9 152 }
Bobymicjohn 58:996effac29b9 153 else
Bobymicjohn 58:996effac29b9 154 {
Bobymicjohn 58:996effac29b9 155 cornerRatio = 1.0f;
Bobymicjohn 58:996effac29b9 156 }
Bobymicjohn 54:f1f5648dfacf 157
hazheng 74:2d45b954deb1 158 if(totalAngleDegreesAbs > 20.0f)
hazheng 52:078b521c9edf 159 {
hazheng 74:2d45b954deb1 160 speedRatio = 0.50f;
hazheng 74:2d45b954deb1 161 }
hazheng 74:2d45b954deb1 162 else if(totalAngleDegreesAbs > 18.0f)
hazheng 74:2d45b954deb1 163 {
hazheng 74:2d45b954deb1 164 speedRatio = 0.87f;
Bobymicjohn 54:f1f5648dfacf 165 }
hazheng 57:0d8a155d511d 166 else if(totalAngleDegreesAbs > 15.0f)
Bobymicjohn 54:f1f5648dfacf 167 {
hazheng 74:2d45b954deb1 168 speedRatio = 0.91f;
Bobymicjohn 54:f1f5648dfacf 169 }
Bobymicjohn 58:996effac29b9 170 else if(totalAngleDegreesAbs > 12.0f)
Bobymicjohn 54:f1f5648dfacf 171 {
hazheng 74:2d45b954deb1 172 speedRatio = 0.95f;
hazheng 52:078b521c9edf 173 }
hazheng 52:078b521c9edf 174 else
hazheng 52:078b521c9edf 175 {
Bobymicjohn 54:f1f5648dfacf 176 speedRatio = 1.0f;
Bobymicjohn 54:f1f5648dfacf 177 }
Bobymicjohn 54:f1f5648dfacf 178
hazheng 65:295c222fdf88 179
Bobymicjohn 54:f1f5648dfacf 180 if(totalAngleDegrees < 0.0f)
Bobymicjohn 54:f1f5648dfacf 181 {
Bobymicjohn 58:996effac29b9 182 motor_set_left_speed(speedRatio * cornerRatio * ((MOTOR_DIFF_MIN_SPEED) + ((1.0f - MOTOR_DIFF_MIN_SPEED) * ((SERVO_MAX_ANGLE - abs(totalAngleDegrees)) / SERVO_MAX_ANGLE))));
Bobymicjohn 58:996effac29b9 183 motor_set_right_speed(speedRatio * cornerRatio * 1.0f);
Bobymicjohn 54:f1f5648dfacf 184 }
Bobymicjohn 54:f1f5648dfacf 185 else
Bobymicjohn 54:f1f5648dfacf 186 {
Bobymicjohn 58:996effac29b9 187 motor_set_right_speed(speedRatio * cornerRatio * ((MOTOR_DIFF_MIN_SPEED) + ((1.0f - MOTOR_DIFF_MIN_SPEED) * ((SERVO_MAX_ANGLE - abs(totalAngleDegrees)) / SERVO_MAX_ANGLE))));
Bobymicjohn 58:996effac29b9 188 motor_set_left_speed(speedRatio * cornerRatio * 1.0f);
hazheng 52:078b521c9edf 189 }
hazheng 65:295c222fdf88 190
hazheng 73:1dcf56e9f1d4 191 //lastAngle = totalAngleDegrees;
hazheng 71:5601d2faa61d 192 lastAngleAbs = totalAngleDegreesAbs;
hazheng 68:b15cab740371 193 }
hazheng 68:b15cab740371 194 }
hazheng 68:b15cab740371 195
hazheng 68:b15cab740371 196 /*
hazheng 68:b15cab740371 197 PwmOut servo(PTE20);
hazheng 68:b15cab740371 198
hazheng 68:b15cab740371 199 int main() {
hazheng 68:b15cab740371 200 servo.period(0.020); // servo requires a 20ms period
hazheng 68:b15cab740371 201
hazheng 68:b15cab740371 202 while (1) {
hazheng 68:b15cab740371 203 for(float offset=0.0; offset<0.001; offset+=0.0001) {
hazheng 68:b15cab740371 204 servo.pulsewidth(0.001 + offset); // servo position determined by a pulsewidth between 1-2ms
hazheng 68:b15cab740371 205 wait(0.25);
hazheng 68:b15cab740371 206 }
hazheng 68:b15cab740371 207 }
hazheng 68:b15cab740371 208
hazheng 68:b15cab740371 209 }
hazheng 68:b15cab740371 210 */
hazheng 68:b15cab740371 211
hazheng 68:b15cab740371 212 /* //code for accelerometer sensor.
hazheng 68:b15cab740371 213 const char regAddr = 0x0D;
hazheng 68:b15cab740371 214 char readValue = 0;
hazheng 68:b15cab740371 215 int result1 = m_sccbCtrl.write(0x1D<<1, &regAddr, 1, true);
hazheng 68:b15cab740371 216 int result2 = m_sccbCtrl.read(0x1D<<1, &readValue, 1, false);
hazheng 68:b15cab740371 217 char buf[20];
hazheng 68:b15cab740371 218 sprintf(buf, "%#x-%#x-%d-%d", regAddr, readValue, result1, result2);
hazheng 68:b15cab740371 219 m_core.GetUSBServer().PushUnreliableMsg('D', buf);
hazheng 68:b15cab740371 220 */
hazheng 68:b15cab740371 221
hazheng 68:b15cab740371 222 //g_core.Update(deltaTime);
hazheng 68:b15cab740371 223
hazheng 68:b15cab740371 224 //if(!isRegRead && g_core.GetUSBServer().GetStatus() == SER_STAT_RUNNING && timer.read() > 2.5f && ardu_cam_is_capture_finished())
hazheng 68:b15cab740371 225 //{
hazheng 68:b15cab740371 226 //ardu_cam_print_debug();
hazheng 68:b15cab740371 227 // isRegRead = true;
hazheng 68:b15cab740371 228 //}
hazheng 68:b15cab740371 229
hazheng 57:0d8a155d511d 230 //LOGI("FPS: %f", 1 / deltaTime);
hazheng 62:bc5caf59fe39 231
hazheng 62:bc5caf59fe39 232 //imu_manager_init();
hazheng 63:d9a81b3d69f5 233 //imu_manager_update();
hazheng 64:43ab429a37e0 234 //float imuTemp = imu_manager_get_temp();
hazheng 63:d9a81b3d69f5 235
hazheng 63:d9a81b3d69f5 236 //const volatile struct imu_vec3* AccelV = imu_manager_get_accl();
hazheng 63:d9a81b3d69f5 237
hazheng 64:43ab429a37e0 238 //LOGI("A: %5.3f, %5.3f, %5.3f.T%5.2f ", AccelV->x, AccelV->y, AccelV->z, imuTemp);
hazheng 62:bc5caf59fe39 239
hazheng 63:d9a81b3d69f5 240 //const volatile struct imu_vec3* VelocityV = imu_manager_get_velocity();
hazheng 63:d9a81b3d69f5 241
hazheng 64:43ab429a37e0 242 //LOGI("V: %5.3f, %5.3f, %5.3f.T%5.2f ", VelocityV->x, VelocityV->y, VelocityV->z, imuTemp);
hazheng 62:bc5caf59fe39 243
hazheng 64:43ab429a37e0 244 //const volatile struct imu_vec3* PositionV = imu_manager_get_position();
hazheng 63:d9a81b3d69f5 245
hazheng 64:43ab429a37e0 246 //LOGI("P: %5.3f, %5.3f, %5.3f ", PositionV->x, PositionV->y, PositionV->z);
hazheng 62:bc5caf59fe39 247
hazheng 68:b15cab740371 248 //counter.Update();
hazheng 57:0d8a155d511d 249
hazheng 52:078b521c9edf 250 /*
Bobymicjohn 47:a682be9908b9 251 //////// Steer Vehicle / Adjust Speed for Differential //////////
Bobymicjohn 47:a682be9908b9 252 servo_set_angle((angleDegrees * -0.3f) + (offsetPercent * SERVO_MAX_ANGLE * 0.3f));
Bobymicjohn 47:a682be9908b9 253
Bobymicjohn 47:a682be9908b9 254 if(angleRadians > 0.366)
Bobymicjohn 47:a682be9908b9 255 angleRadians = 0.366;
Bobymicjohn 47:a682be9908b9 256 else if(angleRadians < -0.366)
Bobymicjohn 47:a682be9908b9 257 angleRadians = -0.366;
Bobymicjohn 47:a682be9908b9 258
Bobymicjohn 47:a682be9908b9 259 if(angleRadians < 0)
Bobymicjohn 47:a682be9908b9 260 {
Bobymicjohn 47:a682be9908b9 261 motor_set_left_speed((0.5) + (0.5) * ((0.366 - abs(angleRadians))/0.366));
Bobymicjohn 47:a682be9908b9 262 motor_set_right_speed(1.0);
Bobymicjohn 47:a682be9908b9 263 }
Bobymicjohn 47:a682be9908b9 264 else
Bobymicjohn 47:a682be9908b9 265 {
Bobymicjohn 47:a682be9908b9 266 motor_set_right_speed((0.5) + (0.5) * ((0.366 - abs(angleRadians))/0.366));
Bobymicjohn 47:a682be9908b9 267 motor_set_left_speed(1.0);
Bobymicjohn 47:a682be9908b9 268 }
hazheng 52:078b521c9edf 269 */
hazheng 50:c387c88141fb 270
hazheng 50:c387c88141fb 271
hazheng 46:a5eb9bd3bb55 272 //char buf[20];
hazheng 46:a5eb9bd3bb55 273 //sprintf(buf, "angle %f", angle);
hazheng 46:a5eb9bd3bb55 274 //g_core.GetUSBServer().PushUnreliableMsg('D', buf);
hazheng 46:a5eb9bd3bb55 275 /*
hazheng 46:a5eb9bd3bb55 276 std::string tempStr = "XX";
hazheng 46:a5eb9bd3bb55 277 for(uint8_t i = 0; i < CAM_ROI_UPPER_LIMIT; ++i)
hazheng 46:a5eb9bd3bb55 278 {
hazheng 46:a5eb9bd3bb55 279 tempStr[0] = i;
hazheng 46:a5eb9bd3bb55 280 tempStr[1] = centerLine[i];
hazheng 46:a5eb9bd3bb55 281 g_core.GetUSBServer().PushUnreliableMsg('L', tempStr);
hazheng 46:a5eb9bd3bb55 282 }
hazheng 46:a5eb9bd3bb55 283 */
hazheng 46:a5eb9bd3bb55 284
hazheng 46:a5eb9bd3bb55 285 /*
hazheng 44:15de535c4005 286 if(offsetPercent > 0.1)
hazheng 44:15de535c4005 287 {
hazheng 46:a5eb9bd3bb55 288 motor_set_speeds(0.05f, 0.05f);
hazheng 44:15de535c4005 289 }
hazheng 44:15de535c4005 290 else
hazheng 44:15de535c4005 291 {
hazheng 46:a5eb9bd3bb55 292 motor_set_speeds(0.13f, 0.13f);
hazheng 44:15de535c4005 293 }
hazheng 46:a5eb9bd3bb55 294 */
hazheng 68:b15cab740371 295 //wait(0.01);