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 Apr 18 22:27:14 2017 +0000
Branch:
Drift
Revision:
85:f3911aab41d9
Parent:
83:b2c7c6f76575
Child:
86:51048c1f132f
Fixed the running state terminate issue.

Who changed what in which revision?

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