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@57:0d8a155d511d, 2017-04-08 (annotated)
- Committer:
- hazheng
- Date:
- Sat Apr 08 15:58:52 2017 +0000
- Revision:
- 57:0d8a155d511d
- Parent:
- 56:7d3395ae022d
- Child:
- 58:996effac29b9
- Child:
- 62:bc5caf59fe39
move the image processing to the main thread. Created the debug counter.
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 | |
hazheng | 44:15de535c4005 | 3 | #include <math.h> |
hazheng | 46:a5eb9bd3bb55 | 4 | #define PI 3.14159265f |
hazheng | 44:15de535c4005 | 5 | |
hazheng | 57:0d8a155d511d | 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" |
Bobymicjohn | 10:fedb5786a109 | 18 | |
Bobymicjohn | 10:fedb5786a109 | 19 | #include "PinAssignment.h" |
Bobymicjohn | 8:92f6baeea027 | 20 | |
hazheng | 28:271fc8445e89 | 21 | SPI g_spi_port(PIN_SPI_MOSI, PIN_SPI_MISO, PIN_SPI_SCK); |
hazheng | 29:f87d8790f57d | 22 | SW::Core g_core; |
hazheng | 5:577b582e4fe9 | 23 | |
hazheng | 56:7d3395ae022d | 24 | #ifdef SW_DEBUG |
hazheng | 56:7d3395ae022d | 25 | #include <rtos.h> |
hazheng | 56:7d3395ae022d | 26 | Mutex g_sw_spi_lock; |
hazheng | 56:7d3395ae022d | 27 | #endif |
hazheng | 56:7d3395ae022d | 28 | |
hazheng | 3:c8867972ffc7 | 29 | int main(void) { |
hazheng | 16:66c7a09e71ee | 30 | |
hazheng | 16:66c7a09e71ee | 31 | Timer timer; |
hazheng | 16:66c7a09e71ee | 32 | |
hazheng | 48:f76b5e252444 | 33 | g_spi_port.frequency(5000000); |
hazheng | 29:f87d8790f57d | 34 | //g_spi_port.format(8, 0); |
hazheng | 29:f87d8790f57d | 35 | |
hazheng | 29:f87d8790f57d | 36 | //SW::Core core; |
Bobymicjohn | 10:fedb5786a109 | 37 | |
hazheng | 46:a5eb9bd3bb55 | 38 | //Motor motor(g_core); |
hazheng | 45:501b7909139a | 39 | //Servo servo(g_core); |
hazheng | 25:6f63053cee81 | 40 | //WheelEncoder wheelEncoder(core); |
hazheng | 29:f87d8790f57d | 41 | //Camera cam(core); |
hazheng | 46:a5eb9bd3bb55 | 42 | motor_init(); |
hazheng | 45:501b7909139a | 43 | servo_init(); |
hazheng | 16:66c7a09e71ee | 44 | |
hazheng | 29:f87d8790f57d | 45 | bool isRegRead = false; |
Bobymicjohn | 14:88302da8bff0 | 46 | |
hazheng | 40:be98219930e4 | 47 | |
hazheng | 40:be98219930e4 | 48 | ardu_utft_init(); |
hazheng | 29:f87d8790f57d | 49 | |
hazheng | 41:7b21c5e3599e | 50 | |
hazheng | 41:7b21c5e3599e | 51 | ardu_cam_init(); |
hazheng | 41:7b21c5e3599e | 52 | |
hazheng | 18:bf6c5f8281eb | 53 | //timer.reset(); |
hazheng | 16:66c7a09e71ee | 54 | timer.start(); |
hazheng | 18:bf6c5f8281eb | 55 | float timeWas = timer.read(); |
hazheng | 44:15de535c4005 | 56 | |
hazheng | 46:a5eb9bd3bb55 | 57 | servo_set_angle(0.0f); |
Bobymicjohn | 54:f1f5648dfacf | 58 | |
Bobymicjohn | 54:f1f5648dfacf | 59 | float speedRatio = 1.0f; |
Bobymicjohn | 54:f1f5648dfacf | 60 | |
hazheng | 57:0d8a155d511d | 61 | DebugCounter counter(10, PTE5); |
hazheng | 57:0d8a155d511d | 62 | |
hazheng | 5:577b582e4fe9 | 63 | while (1) |
hazheng | 5:577b582e4fe9 | 64 | { |
hazheng | 18:bf6c5f8281eb | 65 | float deltaTime = timeWas; |
hazheng | 18:bf6c5f8281eb | 66 | timeWas = timer.read(); |
hazheng | 18:bf6c5f8281eb | 67 | deltaTime = timeWas - deltaTime; |
hazheng | 18:bf6c5f8281eb | 68 | |
Bobymicjohn | 54:f1f5648dfacf | 69 | //g_core.Update(deltaTime); |
hazheng | 41:7b21c5e3599e | 70 | |
Bobymicjohn | 54:f1f5648dfacf | 71 | //if(!isRegRead && g_core.GetUSBServer().GetStatus() == SER_STAT_RUNNING && timer.read() > 2.5f && ardu_cam_is_capture_finished()) |
Bobymicjohn | 54:f1f5648dfacf | 72 | //{ |
hazheng | 44:15de535c4005 | 73 | //ardu_cam_print_debug(); |
Bobymicjohn | 54:f1f5648dfacf | 74 | // isRegRead = true; |
Bobymicjohn | 54:f1f5648dfacf | 75 | //} |
hazheng | 41:7b21c5e3599e | 76 | |
hazheng | 44:15de535c4005 | 77 | //ardu_cam_display_img_utft(); |
hazheng | 57:0d8a155d511d | 78 | image_processing(); |
hazheng | 55:d4db9eef4a9d | 79 | const volatile uint8_t * centerLine = ardu_cam_get_center_array(); |
hazheng | 46:a5eb9bd3bb55 | 80 | |
hazheng | 46:a5eb9bd3bb55 | 81 | /////////////Calculate the curvature: |
hazheng | 57:0d8a155d511d | 82 | float srcY = (0.0f + 1.0f) / 2.0f; |
hazheng | 57:0d8a155d511d | 83 | float srcX = static_cast<float>(centerLine[0] + centerLine[1]) / 2.0f; |
hazheng | 46:a5eb9bd3bb55 | 84 | |
hazheng | 57:0d8a155d511d | 85 | float destY = static_cast<float>(CAM_ROI_UPPER_LIMIT - 1 + (CAM_ROI_UPPER_LIMIT - 2)) / 2.0f; |
hazheng | 57:0d8a155d511d | 86 | float destX = static_cast<float>(centerLine[CAM_ROI_UPPER_LIMIT - 1] + centerLine[CAM_ROI_UPPER_LIMIT - 2]) / 2.0f; |
hazheng | 46:a5eb9bd3bb55 | 87 | |
hazheng | 46:a5eb9bd3bb55 | 88 | float disY = destY - srcY; |
hazheng | 46:a5eb9bd3bb55 | 89 | float disX = destX - srcX; |
hazheng | 46:a5eb9bd3bb55 | 90 | |
hazheng | 52:078b521c9edf | 91 | float angleRadians = -1.0f * static_cast<float>(atan(static_cast<double>(disX / disY))); |
Bobymicjohn | 47:a682be9908b9 | 92 | |
Bobymicjohn | 47:a682be9908b9 | 93 | float angleDegrees = (angleRadians * (180.0f / PI)); |
hazheng | 46:a5eb9bd3bb55 | 94 | /////////////////////////////////////// |
hazheng | 46:a5eb9bd3bb55 | 95 | |
hazheng | 46:a5eb9bd3bb55 | 96 | /////////////Calculate the offset: |
hazheng | 44:15de535c4005 | 97 | float centerPos = static_cast<float>(RESOLUTION_WIDTH / 2); |
hazheng | 52:078b521c9edf | 98 | float offsetDegrees = ((srcX - centerPos) / centerPos) * SERVO_MAX_ANGLE; |
hazheng | 46:a5eb9bd3bb55 | 99 | ////////////////////////////////////// |
hazheng | 46:a5eb9bd3bb55 | 100 | |
hazheng | 57:0d8a155d511d | 101 | float totalAngleDegrees = (angleDegrees * 0.50f) + (offsetDegrees * 0.35f); |
Bobymicjohn | 54:f1f5648dfacf | 102 | float totalAngleDegreesAbs = abs(totalAngleDegrees); |
hazheng | 52:078b521c9edf | 103 | servo_set_angle(totalAngleDegrees); |
hazheng | 52:078b521c9edf | 104 | |
hazheng | 52:078b521c9edf | 105 | if(totalAngleDegrees > SERVO_MAX_ANGLE) |
hazheng | 52:078b521c9edf | 106 | totalAngleDegrees = SERVO_MAX_ANGLE; |
hazheng | 52:078b521c9edf | 107 | else if(totalAngleDegrees < -SERVO_MAX_ANGLE) |
hazheng | 52:078b521c9edf | 108 | totalAngleDegrees = -SERVO_MAX_ANGLE; |
Bobymicjohn | 54:f1f5648dfacf | 109 | |
Bobymicjohn | 54:f1f5648dfacf | 110 | |
hazheng | 57:0d8a155d511d | 111 | if(totalAngleDegreesAbs > 19.0f) |
hazheng | 52:078b521c9edf | 112 | { |
hazheng | 57:0d8a155d511d | 113 | speedRatio = 0.82f; |
Bobymicjohn | 54:f1f5648dfacf | 114 | } |
hazheng | 57:0d8a155d511d | 115 | else if(totalAngleDegreesAbs > 15.0f) |
Bobymicjohn | 54:f1f5648dfacf | 116 | { |
hazheng | 57:0d8a155d511d | 117 | speedRatio = 0.87f; |
Bobymicjohn | 54:f1f5648dfacf | 118 | } |
hazheng | 57:0d8a155d511d | 119 | else if(totalAngleDegreesAbs > 10.0f) |
Bobymicjohn | 54:f1f5648dfacf | 120 | { |
hazheng | 57:0d8a155d511d | 121 | speedRatio = 0.95f; |
hazheng | 52:078b521c9edf | 122 | } |
hazheng | 52:078b521c9edf | 123 | else |
hazheng | 52:078b521c9edf | 124 | { |
Bobymicjohn | 54:f1f5648dfacf | 125 | speedRatio = 1.0f; |
Bobymicjohn | 54:f1f5648dfacf | 126 | } |
Bobymicjohn | 54:f1f5648dfacf | 127 | |
Bobymicjohn | 54:f1f5648dfacf | 128 | if(totalAngleDegrees < 0.0f) |
Bobymicjohn | 54:f1f5648dfacf | 129 | { |
Bobymicjohn | 54:f1f5648dfacf | 130 | motor_set_left_speed(speedRatio * ((MOTOR_DIFF_MIN_SPEED) + ((1.0f - MOTOR_DIFF_MIN_SPEED) * ((SERVO_MAX_ANGLE - abs(totalAngleDegrees)) / SERVO_MAX_ANGLE)))); |
Bobymicjohn | 54:f1f5648dfacf | 131 | motor_set_right_speed(speedRatio * 1.0f); |
Bobymicjohn | 54:f1f5648dfacf | 132 | } |
Bobymicjohn | 54:f1f5648dfacf | 133 | else |
Bobymicjohn | 54:f1f5648dfacf | 134 | { |
Bobymicjohn | 54:f1f5648dfacf | 135 | motor_set_right_speed(speedRatio * ((MOTOR_DIFF_MIN_SPEED) + ((1.0f - MOTOR_DIFF_MIN_SPEED) * ((SERVO_MAX_ANGLE - abs(totalAngleDegrees)) / SERVO_MAX_ANGLE)))); |
Bobymicjohn | 54:f1f5648dfacf | 136 | motor_set_left_speed(speedRatio * 1.0f); |
hazheng | 52:078b521c9edf | 137 | } |
hazheng | 56:7d3395ae022d | 138 | |
hazheng | 57:0d8a155d511d | 139 | //LOGI("FPS: %f", 1 / deltaTime); |
hazheng | 57:0d8a155d511d | 140 | counter.Update(); |
hazheng | 57:0d8a155d511d | 141 | |
hazheng | 52:078b521c9edf | 142 | /* |
Bobymicjohn | 47:a682be9908b9 | 143 | //////// Steer Vehicle / Adjust Speed for Differential ////////// |
Bobymicjohn | 47:a682be9908b9 | 144 | servo_set_angle((angleDegrees * -0.3f) + (offsetPercent * SERVO_MAX_ANGLE * 0.3f)); |
Bobymicjohn | 47:a682be9908b9 | 145 | |
Bobymicjohn | 47:a682be9908b9 | 146 | if(angleRadians > 0.366) |
Bobymicjohn | 47:a682be9908b9 | 147 | angleRadians = 0.366; |
Bobymicjohn | 47:a682be9908b9 | 148 | else if(angleRadians < -0.366) |
Bobymicjohn | 47:a682be9908b9 | 149 | angleRadians = -0.366; |
Bobymicjohn | 47:a682be9908b9 | 150 | |
Bobymicjohn | 47:a682be9908b9 | 151 | if(angleRadians < 0) |
Bobymicjohn | 47:a682be9908b9 | 152 | { |
Bobymicjohn | 47:a682be9908b9 | 153 | motor_set_left_speed((0.5) + (0.5) * ((0.366 - abs(angleRadians))/0.366)); |
Bobymicjohn | 47:a682be9908b9 | 154 | motor_set_right_speed(1.0); |
Bobymicjohn | 47:a682be9908b9 | 155 | } |
Bobymicjohn | 47:a682be9908b9 | 156 | else |
Bobymicjohn | 47:a682be9908b9 | 157 | { |
Bobymicjohn | 47:a682be9908b9 | 158 | motor_set_right_speed((0.5) + (0.5) * ((0.366 - abs(angleRadians))/0.366)); |
Bobymicjohn | 47:a682be9908b9 | 159 | motor_set_left_speed(1.0); |
Bobymicjohn | 47:a682be9908b9 | 160 | } |
hazheng | 52:078b521c9edf | 161 | */ |
hazheng | 50:c387c88141fb | 162 | |
hazheng | 50:c387c88141fb | 163 | |
hazheng | 46:a5eb9bd3bb55 | 164 | //char buf[20]; |
hazheng | 46:a5eb9bd3bb55 | 165 | //sprintf(buf, "angle %f", angle); |
hazheng | 46:a5eb9bd3bb55 | 166 | //g_core.GetUSBServer().PushUnreliableMsg('D', buf); |
hazheng | 46:a5eb9bd3bb55 | 167 | /* |
hazheng | 46:a5eb9bd3bb55 | 168 | std::string tempStr = "XX"; |
hazheng | 46:a5eb9bd3bb55 | 169 | for(uint8_t i = 0; i < CAM_ROI_UPPER_LIMIT; ++i) |
hazheng | 46:a5eb9bd3bb55 | 170 | { |
hazheng | 46:a5eb9bd3bb55 | 171 | tempStr[0] = i; |
hazheng | 46:a5eb9bd3bb55 | 172 | tempStr[1] = centerLine[i]; |
hazheng | 46:a5eb9bd3bb55 | 173 | g_core.GetUSBServer().PushUnreliableMsg('L', tempStr); |
hazheng | 46:a5eb9bd3bb55 | 174 | } |
hazheng | 46:a5eb9bd3bb55 | 175 | */ |
hazheng | 46:a5eb9bd3bb55 | 176 | |
hazheng | 46:a5eb9bd3bb55 | 177 | /* |
hazheng | 44:15de535c4005 | 178 | if(offsetPercent > 0.1) |
hazheng | 44:15de535c4005 | 179 | { |
hazheng | 46:a5eb9bd3bb55 | 180 | motor_set_speeds(0.05f, 0.05f); |
hazheng | 44:15de535c4005 | 181 | } |
hazheng | 44:15de535c4005 | 182 | else |
hazheng | 44:15de535c4005 | 183 | { |
hazheng | 46:a5eb9bd3bb55 | 184 | motor_set_speeds(0.13f, 0.13f); |
hazheng | 44:15de535c4005 | 185 | } |
hazheng | 46:a5eb9bd3bb55 | 186 | */ |
hazheng | 43:0d1886f4848a | 187 | //wait(0.01); |
hazheng | 3:c8867972ffc7 | 188 | } |
hazheng | 3:c8867972ffc7 | 189 | } |
hazheng | 3:c8867972ffc7 | 190 | |
hazheng | 3:c8867972ffc7 | 191 | /* |
hazheng | 3:c8867972ffc7 | 192 | PwmOut servo(PTE20); |
hazheng | 3:c8867972ffc7 | 193 | |
hazheng | 3:c8867972ffc7 | 194 | int main() { |
hazheng | 3:c8867972ffc7 | 195 | servo.period(0.020); // servo requires a 20ms period |
hazheng | 3:c8867972ffc7 | 196 | |
hazheng | 3:c8867972ffc7 | 197 | while (1) { |
hazheng | 3:c8867972ffc7 | 198 | for(float offset=0.0; offset<0.001; offset+=0.0001) { |
hazheng | 3:c8867972ffc7 | 199 | servo.pulsewidth(0.001 + offset); // servo position determined by a pulsewidth between 1-2ms |
hazheng | 3:c8867972ffc7 | 200 | wait(0.25); |
hazheng | 3:c8867972ffc7 | 201 | } |
hazheng | 3:c8867972ffc7 | 202 | } |
hazheng | 3:c8867972ffc7 | 203 | |
hazheng | 3:c8867972ffc7 | 204 | } |
hazheng | 13:7dcb1642ef99 | 205 | */ |
hazheng | 13:7dcb1642ef99 | 206 | |
hazheng | 13:7dcb1642ef99 | 207 | /* //code for accelerometer sensor. |
hazheng | 13:7dcb1642ef99 | 208 | const char regAddr = 0x0D; |
hazheng | 13:7dcb1642ef99 | 209 | char readValue = 0; |
hazheng | 13:7dcb1642ef99 | 210 | int result1 = m_sccbCtrl.write(0x1D<<1, ®Addr, 1, true); |
hazheng | 13:7dcb1642ef99 | 211 | int result2 = m_sccbCtrl.read(0x1D<<1, &readValue, 1, false); |
hazheng | 13:7dcb1642ef99 | 212 | char buf[20]; |
hazheng | 13:7dcb1642ef99 | 213 | sprintf(buf, "%#x-%#x-%d-%d", regAddr, readValue, result1, result2); |
hazheng | 13:7dcb1642ef99 | 214 | m_core.GetUSBServer().PushUnreliableMsg('D', buf); |
hazheng | 3:c8867972ffc7 | 215 | */ |