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
StateMachine/RunningState.cpp@83:b2c7c6f76575, 2017-04-18 (annotated)
- Committer:
- hazheng
- Date:
- Tue Apr 18 21:26:53 2017 +0000
- Branch:
- Drift
- Revision:
- 83:b2c7c6f76575
- Parent:
- 82:992ba6f31e24
- Child:
- 85:f3911aab41d9
Clean the code a little bit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hazheng | 80:c85cb93713b3 | 1 | #include "RunningState.h" |
hazheng | 80:c85cb93713b3 | 2 | |
hazheng | 80:c85cb93713b3 | 3 | #include "StateManager.h" |
hazheng | 80:c85cb93713b3 | 4 | |
hazheng | 80:c85cb93713b3 | 5 | #include "ArduCAM.h" |
hazheng | 80:c85cb93713b3 | 6 | #include "ArduUTFT.h" |
hazheng | 80:c85cb93713b3 | 7 | #include "Motor.h" |
hazheng | 80:c85cb93713b3 | 8 | #include "Servo.h" |
hazheng | 80:c85cb93713b3 | 9 | |
hazheng | 80:c85cb93713b3 | 10 | #define PI 3.14159265f |
hazheng | 83:b2c7c6f76575 | 11 | #include <math.h> |
hazheng | 80:c85cb93713b3 | 12 | |
hazheng | 80:c85cb93713b3 | 13 | RunningState::RunningState() : |
hazheng | 80:c85cb93713b3 | 14 | m_speedRatio(1.0f), |
hazheng | 80:c85cb93713b3 | 15 | m_lastAngle(0.0f), |
hazheng | 80:c85cb93713b3 | 16 | m_lastAngleAbs(0.0f), |
hazheng | 80:c85cb93713b3 | 17 | m_cornerRatio(1.0f), |
hazheng | 80:c85cb93713b3 | 18 | m_lastLineFound(BOTH_FOUND) |
hazheng | 80:c85cb93713b3 | 19 | {} |
hazheng | 80:c85cb93713b3 | 20 | |
hazheng | 80:c85cb93713b3 | 21 | RunningState::~RunningState() |
hazheng | 80:c85cb93713b3 | 22 | {} |
hazheng | 80:c85cb93713b3 | 23 | |
hazheng | 80:c85cb93713b3 | 24 | void RunningState::DrawUserInterface() |
hazheng | 80:c85cb93713b3 | 25 | { |
hazheng | 81:32bd7a25a699 | 26 | ardu_utft_clr_scr(); |
hazheng | 80:c85cb93713b3 | 27 | |
hazheng | 81:32bd7a25a699 | 28 | ardu_utft_set_color(255, 0, 0); |
hazheng | 81:32bd7a25a699 | 29 | ardu_utft_fill_rect(10, 10, 310, 230); |
hazheng | 81:32bd7a25a699 | 30 | ardu_utft_set_color(255, 255, 255); |
hazheng | 81:32bd7a25a699 | 31 | ardu_utft_print("STOP", 170, 120); |
hazheng | 80:c85cb93713b3 | 32 | } |
hazheng | 80:c85cb93713b3 | 33 | |
hazheng | 80:c85cb93713b3 | 34 | void RunningState::Update(float deltaTime) |
hazheng | 80:c85cb93713b3 | 35 | { |
hazheng | 80:c85cb93713b3 | 36 | image_processing(); |
hazheng | 80:c85cb93713b3 | 37 | uint8_t shouldTerminate = ardu_cam_get_is_encounter_terminate(); |
hazheng | 80:c85cb93713b3 | 38 | |
hazheng | 80:c85cb93713b3 | 39 | if(shouldTerminate) |
hazheng | 80:c85cb93713b3 | 40 | { |
hazheng | 80:c85cb93713b3 | 41 | TerminateProcess(); |
hazheng | 80:c85cb93713b3 | 42 | return; |
hazheng | 80:c85cb93713b3 | 43 | } |
hazheng | 80:c85cb93713b3 | 44 | //else |
hazheng | 80:c85cb93713b3 | 45 | //{ |
hazheng | 80:c85cb93713b3 | 46 | // ardu_utft_print(" ", 230, 100); |
hazheng | 80:c85cb93713b3 | 47 | //} |
hazheng | 80:c85cb93713b3 | 48 | |
hazheng | 80:c85cb93713b3 | 49 | const volatile uint8_t* centerLine = ardu_cam_get_center_array(); |
hazheng | 80:c85cb93713b3 | 50 | |
hazheng | 80:c85cb93713b3 | 51 | /////////////Calculate the curvature: |
hazheng | 80:c85cb93713b3 | 52 | float srcY = (0.0f + 1.0f) / 2.0f; |
hazheng | 80:c85cb93713b3 | 53 | float srcX = static_cast<float>(centerLine[0] + centerLine[1]) / 2.0f; |
hazheng | 80:c85cb93713b3 | 54 | |
hazheng | 80:c85cb93713b3 | 55 | float destY = static_cast<float>(CAM_ROI_UPPER_LIMIT - 1 + (CAM_ROI_UPPER_LIMIT - 2)) / 2.0f; |
hazheng | 80:c85cb93713b3 | 56 | float destX = static_cast<float>(centerLine[CAM_ROI_UPPER_LIMIT - 1] + centerLine[CAM_ROI_UPPER_LIMIT - 2]) / 2.0f; |
hazheng | 80:c85cb93713b3 | 57 | |
hazheng | 80:c85cb93713b3 | 58 | float disY = destY - srcY; |
hazheng | 80:c85cb93713b3 | 59 | float disX = destX - srcX; |
hazheng | 80:c85cb93713b3 | 60 | |
hazheng | 80:c85cb93713b3 | 61 | float angleRadians = -1.0f * static_cast<float>(atan(static_cast<double>(disX / disY))); |
hazheng | 80:c85cb93713b3 | 62 | float angleDegrees = (angleRadians * (180.0f / PI)); |
hazheng | 80:c85cb93713b3 | 63 | |
hazheng | 80:c85cb93713b3 | 64 | /////////////////////////////////////// |
hazheng | 80:c85cb93713b3 | 65 | |
hazheng | 80:c85cb93713b3 | 66 | /////////////Calculate the offset: |
hazheng | 80:c85cb93713b3 | 67 | float centerPos = static_cast<float>(RESOLUTION_WIDTH / 2); |
hazheng | 80:c85cb93713b3 | 68 | float offsetDegrees = ((srcX - centerPos) / centerPos) * SERVO_MAX_ANGLE; |
hazheng | 80:c85cb93713b3 | 69 | ////////////////////////////////////// |
hazheng | 80:c85cb93713b3 | 70 | |
hazheng | 80:c85cb93713b3 | 71 | const uint8_t lineFoundRefRow = ((2 * CAM_ROI_UPPER_LIMIT) - 1) - (CAM_ROI_UPPER_LIMIT / 2); |
hazheng | 80:c85cb93713b3 | 72 | //const uint8_t lineFoundRefTop = ((2 * CAM_ROI_UPPER_LIMIT) - 1); |
hazheng | 80:c85cb93713b3 | 73 | //const uint8_t lineFoundRefBut = (CAM_ROI_UPPER_LIMIT); |
hazheng | 80:c85cb93713b3 | 74 | const uint8_t isBorderFound = ardu_cam_get_is_border_found(); |
hazheng | 80:c85cb93713b3 | 75 | |
hazheng | 80:c85cb93713b3 | 76 | float totalAngleDegrees = (angleDegrees * 0.52f) + (offsetDegrees * (centerLine[lineFoundRefRow] != BOTH_FOUND ? 0.90f : 0.35f)); |
hazheng | 80:c85cb93713b3 | 77 | |
hazheng | 80:c85cb93713b3 | 78 | if(totalAngleDegrees > SERVO_MAX_ANGLE) |
hazheng | 80:c85cb93713b3 | 79 | totalAngleDegrees = SERVO_MAX_ANGLE; |
hazheng | 80:c85cb93713b3 | 80 | else if(totalAngleDegrees < -SERVO_MAX_ANGLE) |
hazheng | 80:c85cb93713b3 | 81 | totalAngleDegrees = -SERVO_MAX_ANGLE; |
hazheng | 80:c85cb93713b3 | 82 | |
hazheng | 80:c85cb93713b3 | 83 | if((totalAngleDegrees < 0.0f && (isBorderFound == LEFT_FOUND)) || |
hazheng | 80:c85cb93713b3 | 84 | (totalAngleDegrees > 0.0f && (isBorderFound == RIGHT_FOUND))) |
hazheng | 80:c85cb93713b3 | 85 | { |
hazheng | 82:992ba6f31e24 | 86 | totalAngleDegrees = ((-0.2f) * totalAngleDegrees);// + ((0.2f) * m_lastAngle); |
hazheng | 80:c85cb93713b3 | 87 | } |
hazheng | 80:c85cb93713b3 | 88 | //LOGI("%d %5.3f ", centerLine[lineFoundRefRow], totalAngleDegrees); |
hazheng | 80:c85cb93713b3 | 89 | servo_set_angle(totalAngleDegrees); |
hazheng | 80:c85cb93713b3 | 90 | |
hazheng | 80:c85cb93713b3 | 91 | |
hazheng | 80:c85cb93713b3 | 92 | float totalAngleDegreesAbs = abs(totalAngleDegrees); |
hazheng | 80:c85cb93713b3 | 93 | |
hazheng | 80:c85cb93713b3 | 94 | if(totalAngleDegreesAbs > m_lastAngleAbs) |
hazheng | 80:c85cb93713b3 | 95 | { |
hazheng | 80:c85cb93713b3 | 96 | m_cornerRatio = 0.4;//(m_lastAngle / totalAngleDegrees); |
hazheng | 80:c85cb93713b3 | 97 | } |
hazheng | 80:c85cb93713b3 | 98 | else if(totalAngleDegreesAbs < m_lastAngleAbs) |
hazheng | 80:c85cb93713b3 | 99 | { |
hazheng | 80:c85cb93713b3 | 100 | m_cornerRatio = 0.8;//(m_lastAngle / totalAngleDegrees); |
hazheng | 80:c85cb93713b3 | 101 | } |
hazheng | 80:c85cb93713b3 | 102 | else |
hazheng | 80:c85cb93713b3 | 103 | { |
hazheng | 80:c85cb93713b3 | 104 | m_cornerRatio = 1.0f; |
hazheng | 80:c85cb93713b3 | 105 | } |
hazheng | 80:c85cb93713b3 | 106 | |
hazheng | 80:c85cb93713b3 | 107 | if(totalAngleDegreesAbs > 20.0f) |
hazheng | 80:c85cb93713b3 | 108 | { |
hazheng | 80:c85cb93713b3 | 109 | m_speedRatio = 0.350f; |
hazheng | 80:c85cb93713b3 | 110 | } |
hazheng | 80:c85cb93713b3 | 111 | else if(totalAngleDegreesAbs > 18.0f) |
hazheng | 80:c85cb93713b3 | 112 | { |
hazheng | 80:c85cb93713b3 | 113 | m_speedRatio = 0.80f; |
hazheng | 80:c85cb93713b3 | 114 | } |
hazheng | 80:c85cb93713b3 | 115 | else if(totalAngleDegreesAbs > 15.0f) |
hazheng | 80:c85cb93713b3 | 116 | { |
hazheng | 80:c85cb93713b3 | 117 | m_speedRatio = 0.90f; |
hazheng | 80:c85cb93713b3 | 118 | } |
hazheng | 80:c85cb93713b3 | 119 | else if(totalAngleDegreesAbs > 12.0f) |
hazheng | 80:c85cb93713b3 | 120 | { |
hazheng | 80:c85cb93713b3 | 121 | m_speedRatio = 0.92f; |
hazheng | 80:c85cb93713b3 | 122 | } |
hazheng | 80:c85cb93713b3 | 123 | else |
hazheng | 80:c85cb93713b3 | 124 | { |
hazheng | 80:c85cb93713b3 | 125 | m_speedRatio = 1.0f; |
hazheng | 80:c85cb93713b3 | 126 | } |
hazheng | 80:c85cb93713b3 | 127 | |
hazheng | 80:c85cb93713b3 | 128 | |
hazheng | 80:c85cb93713b3 | 129 | if(totalAngleDegrees < 0.0f) |
hazheng | 80:c85cb93713b3 | 130 | { |
hazheng | 80:c85cb93713b3 | 131 | motor_set_left_speed(m_speedRatio * m_cornerRatio * ((MOTOR_DIFF_MIN_SPEED) + ((1.0f - MOTOR_DIFF_MIN_SPEED) * ((SERVO_MAX_ANGLE - abs(totalAngleDegrees)) / SERVO_MAX_ANGLE)))); |
hazheng | 80:c85cb93713b3 | 132 | motor_set_right_speed(m_speedRatio * m_cornerRatio * 1.0f); |
hazheng | 80:c85cb93713b3 | 133 | } |
hazheng | 80:c85cb93713b3 | 134 | else |
hazheng | 80:c85cb93713b3 | 135 | { |
hazheng | 80:c85cb93713b3 | 136 | motor_set_right_speed(m_speedRatio * m_cornerRatio * ((MOTOR_DIFF_MIN_SPEED) + ((1.0f - MOTOR_DIFF_MIN_SPEED) * ((SERVO_MAX_ANGLE - abs(totalAngleDegrees)) / SERVO_MAX_ANGLE)))); |
hazheng | 80:c85cb93713b3 | 137 | motor_set_left_speed(m_speedRatio * m_cornerRatio * 1.0f); |
hazheng | 80:c85cb93713b3 | 138 | } |
hazheng | 80:c85cb93713b3 | 139 | |
hazheng | 80:c85cb93713b3 | 140 | m_lastAngle = totalAngleDegrees; |
hazheng | 80:c85cb93713b3 | 141 | m_lastAngleAbs = totalAngleDegreesAbs; |
hazheng | 80:c85cb93713b3 | 142 | m_lastLineFound = centerLine[lineFoundRefRow]; |
hazheng | 80:c85cb93713b3 | 143 | } |
hazheng | 80:c85cb93713b3 | 144 | |
hazheng | 80:c85cb93713b3 | 145 | uint8_t RunningState::HasTouchPosFunction() const |
hazheng | 80:c85cb93713b3 | 146 | { |
hazheng | 80:c85cb93713b3 | 147 | return 0; |
hazheng | 80:c85cb93713b3 | 148 | } |
hazheng | 80:c85cb93713b3 | 149 | |
hazheng | 80:c85cb93713b3 | 150 | uint8_t RunningState::HasTouchIrqFunction() const |
hazheng | 80:c85cb93713b3 | 151 | { |
hazheng | 80:c85cb93713b3 | 152 | return 1; |
hazheng | 80:c85cb93713b3 | 153 | } |
hazheng | 80:c85cb93713b3 | 154 | |
hazheng | 80:c85cb93713b3 | 155 | void RunningState::TouchPosCallback(int16_t x, int16_t y) |
hazheng | 80:c85cb93713b3 | 156 | { |
hazheng | 80:c85cb93713b3 | 157 | |
hazheng | 80:c85cb93713b3 | 158 | } |
hazheng | 80:c85cb93713b3 | 159 | |
hazheng | 80:c85cb93713b3 | 160 | void RunningState::TouchIrqCallback() |
hazheng | 80:c85cb93713b3 | 161 | { |
hazheng | 80:c85cb93713b3 | 162 | TerminateProcess(); |
hazheng | 80:c85cb93713b3 | 163 | } |
hazheng | 80:c85cb93713b3 | 164 | |
hazheng | 80:c85cb93713b3 | 165 | void RunningState::TerminateProcess() |
hazheng | 80:c85cb93713b3 | 166 | { |
hazheng | 82:992ba6f31e24 | 167 | //ardu_utft_print("F", 230, 100); |
hazheng | 80:c85cb93713b3 | 168 | servo_set_angle(0.0f); |
hazheng | 80:c85cb93713b3 | 169 | motor_set_right_speed(-1.0f); |
hazheng | 80:c85cb93713b3 | 170 | motor_set_left_speed(-1.0f); |
hazheng | 80:c85cb93713b3 | 171 | wait(0.4f); |
hazheng | 80:c85cb93713b3 | 172 | motor_set_right_speed(0.0f); |
hazheng | 80:c85cb93713b3 | 173 | motor_set_left_speed(0.0f); |
hazheng | 82:992ba6f31e24 | 174 | |
hazheng | 82:992ba6f31e24 | 175 | state_manager_switch_state(STANDBY_STATE); |
hazheng | 80:c85cb93713b3 | 176 | } |