Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
AI_Friday.cpp@30:2ce51e8719ff, 2016-05-26 (annotated)
- Committer:
- smilestone520
- Date:
- Thu May 26 11:29:00 2016 +0000
- Revision:
- 30:2ce51e8719ff
- Parent:
- 29:47519f2b14ad
- Child:
- 31:6a894ff3ed18
hahahaa
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| smilestone520 | 10:06cc2b1d2aaf | 1 | /*LAB_DCMotor*/ |
| smilestone520 | 10:06cc2b1d2aaf | 2 | #include "mbed.h" |
| smilestone520 | 10:06cc2b1d2aaf | 3 | |
| smilestone520 | 10:06cc2b1d2aaf | 4 | //The number will be compiled as type "double" in default |
| smilestone520 | 10:06cc2b1d2aaf | 5 | //Add a "f" after the number can make it compiled as type "float" |
| smilestone520 | 10:06cc2b1d2aaf | 6 | #define Ts 0.01f //period of timer1 (s) |
| smilestone520 | 10:06cc2b1d2aaf | 7 | #define Kp 0.003f |
| smilestone520 | 10:06cc2b1d2aaf | 8 | #define Ki 0.01f |
| smilestone520 | 10:06cc2b1d2aaf | 9 | |
| roger5641 | 17:d96afd9d2692 | 10 | |
| roger5641 | 17:d96afd9d2692 | 11 | PwmOut servo(A0); |
| smilestone520 | 10:06cc2b1d2aaf | 12 | PwmOut pwm1(D7); |
| smilestone520 | 10:06cc2b1d2aaf | 13 | PwmOut pwm1n(D11); |
| smilestone520 | 10:06cc2b1d2aaf | 14 | PwmOut pwm2(D8); |
| smilestone520 | 10:06cc2b1d2aaf | 15 | PwmOut pwm2n(A3); |
| smilestone520 | 10:06cc2b1d2aaf | 16 | |
| smilestone520 | 10:06cc2b1d2aaf | 17 | Serial bluetooth(D10,D2); |
| smilestone520 | 10:06cc2b1d2aaf | 18 | Serial pc(D1, D0); |
| smilestone520 | 10:06cc2b1d2aaf | 19 | |
| smilestone520 | 10:06cc2b1d2aaf | 20 | DigitalOut led1(A4); |
| smilestone520 | 10:06cc2b1d2aaf | 21 | DigitalOut led2(A5); |
| smilestone520 | 10:06cc2b1d2aaf | 22 | |
| smilestone520 | 10:06cc2b1d2aaf | 23 | //Motor1 sensor |
| smilestone520 | 10:06cc2b1d2aaf | 24 | InterruptIn HallA_1(A1); |
| smilestone520 | 10:06cc2b1d2aaf | 25 | InterruptIn HallB_1(A2); |
| smilestone520 | 10:06cc2b1d2aaf | 26 | //Motor2 sensor |
| smilestone520 | 10:06cc2b1d2aaf | 27 | InterruptIn HallA_2(D13); |
| smilestone520 | 10:06cc2b1d2aaf | 28 | InterruptIn HallB_2(D12); |
| smilestone520 | 10:06cc2b1d2aaf | 29 | |
| smilestone520 | 10:06cc2b1d2aaf | 30 | Ticker timer1; |
| smilestone520 | 10:06cc2b1d2aaf | 31 | void timer1_interrupt(void); |
| smilestone520 | 10:06cc2b1d2aaf | 32 | void CN_interrupt(void); |
| smilestone520 | 10:06cc2b1d2aaf | 33 | |
| smilestone520 | 10:06cc2b1d2aaf | 34 | void init_TIMER(void); |
| smilestone520 | 10:06cc2b1d2aaf | 35 | void init_PWM(void); |
| smilestone520 | 10:06cc2b1d2aaf | 36 | void init_CN(void); |
| smilestone520 | 20:5b892e37a958 | 37 | void funcBorder(void); |
| smilestone520 | 20:5b892e37a958 | 38 | void smallAngle( float ); |
| smilestone520 | 20:5b892e37a958 | 39 | void turnCW(float); |
| smilestone520 | 20:5b892e37a958 | 40 | void turnCCW(float); |
| smilestone520 | 10:06cc2b1d2aaf | 41 | |
| smilestone520 | 10:06cc2b1d2aaf | 42 | int8_t stateA_1=0, stateB_1=0, stateA_2=0, stateB_2=0; |
| smilestone520 | 10:06cc2b1d2aaf | 43 | int8_t state_1 = 0, state_1_old = 0, state_2 = 0, state_2_old = 0; |
| smilestone520 | 10:06cc2b1d2aaf | 44 | |
| smilestone520 | 10:06cc2b1d2aaf | 45 | int v1Count = 0; |
| smilestone520 | 10:06cc2b1d2aaf | 46 | int v2Count = 0; |
| smilestone520 | 10:06cc2b1d2aaf | 47 | |
| roger5641 | 17:d96afd9d2692 | 48 | float v1 = 0.0, v1_ref = 0.0; |
| smilestone520 | 10:06cc2b1d2aaf | 49 | float v1_err = 0.0, v1_ierr = 0.0, PIout_1 = 0.0, PIout_1_old = 0.0; |
| roger5641 | 17:d96afd9d2692 | 50 | float v2 = 0.0, v2_ref = 0.0; |
| smilestone520 | 10:06cc2b1d2aaf | 51 | float v2_err = 0.0, v2_ierr = 0.0, PIout_2 = 0.0, PIout_2_old = 0.0; |
| roger5641 | 17:d96afd9d2692 | 52 | |
| roger5641 | 17:d96afd9d2692 | 53 | int angle = 90; |
| roger5641 | 15:3e0a4079b28a | 54 | float servo_duty = 0.079;//0.079 +(0.084/180)*angle, -90<angle<90 |
| roger5641 | 15:3e0a4079b28a | 55 | |
| roger5641 | 18:2db6c97a4145 | 56 | int Receive_Data[33] = {0}; |
| roger5641 | 18:2db6c97a4145 | 57 | double Distance_Target = 0, Angle_Target = 0; |
| roger5641 | 15:3e0a4079b28a | 58 | int X_Position_1 = 0, Y_Position_1 = 0, Angle_1 = 0; |
| roger5641 | 15:3e0a4079b28a | 59 | int X_Position_2 = 0, Y_Position_2 = 0, Angle_2 = 0; |
| roger5641 | 15:3e0a4079b28a | 60 | |
| smilestone520 | 20:5b892e37a958 | 61 | float k_forward = 20, k_turn = 10; |
| smilestone520 | 20:5b892e37a958 | 62 | |
| smilestone520 | 11:44989c0bcea5 | 63 | //**** receive and return by bluetooth ************** // bluetooth.getc() |
| smilestone520 | 14:c46f22bcaa38 | 64 | float xC, yC; // car's position |
| smilestone520 | 14:c46f22bcaa38 | 65 | float angleC;// car's angle |
| roger5641 | 17:d96afd9d2692 | 66 | double xP,yP,distance; //position that car need to reach |
| smilestone520 | 14:c46f22bcaa38 | 67 | float angleR; // angleR = car-nextSpot direction ---> car direction |
| smilestone520 | 25:7053736ea6cc | 68 | |
| roger5641 | 17:d96afd9d2692 | 69 | int xGate = 800, yGate = 300; |
| roger5641 | 17:d96afd9d2692 | 70 | double angleGate, distanceGate; |
| smilestone520 | 11:44989c0bcea5 | 71 | int aI_State = 0; |
| smilestone520 | 21:6e8ab9487985 | 72 | int bor_state = 1; |
| roger5641 | 17:d96afd9d2692 | 73 | double pi = 3.1415926; |
| smilestone520 | 21:6e8ab9487985 | 74 | double borAngle=0; |
| smilestone520 | 11:44989c0bcea5 | 75 | |
| smilestone520 | 25:7053736ea6cc | 76 | float ballSize; // ball size |
| smilestone520 | 25:7053736ea6cc | 77 | |
| smilestone520 | 30:2ce51e8719ff | 78 | float longC = 65; // car's length //65 |
| smilestone520 | 30:2ce51e8719ff | 79 | int yB1= 65; |
| smilestone520 | 30:2ce51e8719ff | 80 | int yB2 = 580; |
| smilestone520 | 30:2ce51e8719ff | 81 | int xB3 = 170; |
| smilestone520 | 30:2ce51e8719ff | 82 | int xB4 = 860; // broder conditions |
| smilestone520 | 29:47519f2b14ad | 83 | //65 , 580, 170,860 |
| smilestone520 | 27:1696ef46b538 | 84 | int ticCheck = 10; |
| smilestone520 | 20:5b892e37a958 | 85 | |
| smilestone520 | 10:06cc2b1d2aaf | 86 | int main() { |
| smilestone520 | 10:06cc2b1d2aaf | 87 | |
| smilestone520 | 10:06cc2b1d2aaf | 88 | init_TIMER(); |
| smilestone520 | 10:06cc2b1d2aaf | 89 | init_PWM(); |
| smilestone520 | 10:06cc2b1d2aaf | 90 | init_CN(); |
| roger5641 | 17:d96afd9d2692 | 91 | servo.write(0.079 + (0.084/180)*angle); |
| smilestone520 | 10:06cc2b1d2aaf | 92 | |
| smilestone520 | 10:06cc2b1d2aaf | 93 | bluetooth.baud(115200); //設定鮑率 |
| smilestone520 | 10:06cc2b1d2aaf | 94 | pc.baud(57600); |
| smilestone520 | 10:06cc2b1d2aaf | 95 | |
| smilestone520 | 10:06cc2b1d2aaf | 96 | |
| smilestone520 | 10:06cc2b1d2aaf | 97 | while(1) |
| smilestone520 | 10:06cc2b1d2aaf | 98 | { |
| smilestone520 | 10:06cc2b1d2aaf | 99 | if(pc.readable()) |
| smilestone520 | 10:06cc2b1d2aaf | 100 | { |
| smilestone520 | 10:06cc2b1d2aaf | 101 | bluetooth.putc(pc.getc()); |
| smilestone520 | 10:06cc2b1d2aaf | 102 | } |
| smilestone520 | 10:06cc2b1d2aaf | 103 | if(bluetooth.readable()) |
| smilestone520 | 10:06cc2b1d2aaf | 104 | { |
| smilestone520 | 10:06cc2b1d2aaf | 105 | pc.putc(bluetooth.getc()); |
| smilestone520 | 10:06cc2b1d2aaf | 106 | } |
| smilestone520 | 10:06cc2b1d2aaf | 107 | } |
| smilestone520 | 10:06cc2b1d2aaf | 108 | } |
| smilestone520 | 10:06cc2b1d2aaf | 109 | |
| smilestone520 | 10:06cc2b1d2aaf | 110 | void timer1_interrupt(void) |
| smilestone520 | 10:06cc2b1d2aaf | 111 | { |
| roger5641 | 26:dbdbfdb4dd41 | 112 | for(int i=0; i<32; i++) |
| roger5641 | 26:dbdbfdb4dd41 | 113 | { |
| roger5641 | 26:dbdbfdb4dd41 | 114 | Receive_Data[i] = bluetooth.getc(); |
| roger5641 | 26:dbdbfdb4dd41 | 115 | } |
| roger5641 | 26:dbdbfdb4dd41 | 116 | //read data from matlab |
| roger5641 | 26:dbdbfdb4dd41 | 117 | //distance_target |
| roger5641 | 26:dbdbfdb4dd41 | 118 | Distance_Target = (Receive_Data[1]-0x30)*100 + (Receive_Data[2]-0x30)*10 + (Receive_Data[3]-0x30); |
| roger5641 | 26:dbdbfdb4dd41 | 119 | |
| roger5641 | 26:dbdbfdb4dd41 | 120 | if(Receive_Data[0] == '-')Distance_Target = -1* Distance_Target; |
| roger5641 | 26:dbdbfdb4dd41 | 121 | else Distance_Target = Distance_Target; |
| roger5641 | 26:dbdbfdb4dd41 | 122 | |
| roger5641 | 26:dbdbfdb4dd41 | 123 | //ang_rel_target |
| roger5641 | 26:dbdbfdb4dd41 | 124 | Angle_Target = (Receive_Data[5]-0x30)*100 + (Receive_Data[6]-0x30)*10 + (Receive_Data[7]-0x30); |
| roger5641 | 26:dbdbfdb4dd41 | 125 | |
| roger5641 | 26:dbdbfdb4dd41 | 126 | if(Receive_Data[4] == '-')Distance_Target = -1* Distance_Target; |
| roger5641 | 26:dbdbfdb4dd41 | 127 | else Distance_Target = Distance_Target; |
| roger5641 | 26:dbdbfdb4dd41 | 128 | |
| roger5641 | 26:dbdbfdb4dd41 | 129 | //x_position_car_1 |
| roger5641 | 26:dbdbfdb4dd41 | 130 | X_Position_1 = (Receive_Data[9]-0x30)*100 + (Receive_Data[10]-0x30)*10 + (Receive_Data[11]-0x30); |
| roger5641 | 26:dbdbfdb4dd41 | 131 | |
| roger5641 | 26:dbdbfdb4dd41 | 132 | if(Receive_Data[8] == '-')Distance_Target = -1* Distance_Target; |
| roger5641 | 26:dbdbfdb4dd41 | 133 | else Distance_Target = Distance_Target; |
| roger5641 | 26:dbdbfdb4dd41 | 134 | |
| roger5641 | 26:dbdbfdb4dd41 | 135 | //y_position_car_1 |
| roger5641 | 26:dbdbfdb4dd41 | 136 | Y_Position_1 = (Receive_Data[13]-0x30)*100 + (Receive_Data[14]-0x30)*10 + (Receive_Data[15]-0x30); |
| roger5641 | 26:dbdbfdb4dd41 | 137 | |
| roger5641 | 26:dbdbfdb4dd41 | 138 | if(Receive_Data[12] == '-')Distance_Target = -1* Distance_Target; |
| roger5641 | 26:dbdbfdb4dd41 | 139 | else Distance_Target = Distance_Target; |
| roger5641 | 26:dbdbfdb4dd41 | 140 | |
| roger5641 | 26:dbdbfdb4dd41 | 141 | //angle_car_1 |
| roger5641 | 26:dbdbfdb4dd41 | 142 | Angle_1 = (Receive_Data[17]-0x30)*100 + (Receive_Data[18]-0x30)*10 + (Receive_Data[19]-0x30); |
| roger5641 | 26:dbdbfdb4dd41 | 143 | |
| roger5641 | 26:dbdbfdb4dd41 | 144 | if(Receive_Data[16] == '-')Distance_Target = -1* Distance_Target; |
| roger5641 | 26:dbdbfdb4dd41 | 145 | else Distance_Target = Distance_Target; |
| roger5641 | 26:dbdbfdb4dd41 | 146 | |
| roger5641 | 26:dbdbfdb4dd41 | 147 | //x_position_car_2 |
| roger5641 | 26:dbdbfdb4dd41 | 148 | X_Position_2 = (Receive_Data[21]-0x30)*100 + (Receive_Data[22]-0x30)*10 + (Receive_Data[23]-0x30); |
| roger5641 | 26:dbdbfdb4dd41 | 149 | |
| roger5641 | 26:dbdbfdb4dd41 | 150 | if(Receive_Data[20] == '-')Distance_Target = -1* Distance_Target; |
| roger5641 | 26:dbdbfdb4dd41 | 151 | else Distance_Target = Distance_Target; |
| roger5641 | 26:dbdbfdb4dd41 | 152 | |
| roger5641 | 26:dbdbfdb4dd41 | 153 | //y_position_car_2 |
| roger5641 | 26:dbdbfdb4dd41 | 154 | Y_Position_2 = (Receive_Data[25]-0x30)*100 + (Receive_Data[26]-0x30)*10 + (Receive_Data[27]-0x30); |
| roger5641 | 26:dbdbfdb4dd41 | 155 | |
| roger5641 | 26:dbdbfdb4dd41 | 156 | if(Receive_Data[24] == '-')Distance_Target = -1* Distance_Target; |
| roger5641 | 26:dbdbfdb4dd41 | 157 | else Distance_Target = Distance_Target; |
| roger5641 | 26:dbdbfdb4dd41 | 158 | |
| roger5641 | 26:dbdbfdb4dd41 | 159 | //angle_car_1 |
| roger5641 | 26:dbdbfdb4dd41 | 160 | Angle_2 = (Receive_Data[29]-0x30)*100 + (Receive_Data[30]-0x30)*10 + (Receive_Data[31]-0x30); |
| roger5641 | 26:dbdbfdb4dd41 | 161 | |
| roger5641 | 26:dbdbfdb4dd41 | 162 | if(Receive_Data[28] == '-')Distance_Target = -1* Distance_Target; |
| roger5641 | 26:dbdbfdb4dd41 | 163 | else Distance_Target = Distance_Target; |
| roger5641 | 26:dbdbfdb4dd41 | 164 | |
| roger5641 | 26:dbdbfdb4dd41 | 165 | |
| roger5641 | 26:dbdbfdb4dd41 | 166 | |
| smilestone520 | 10:06cc2b1d2aaf | 167 | //Motor 1 |
| smilestone520 | 10:06cc2b1d2aaf | 168 | v1 = (float)v1Count * 100.0f / 12.0f * 60.0f / 29.0f; //unit: rpm |
| smilestone520 | 10:06cc2b1d2aaf | 169 | v1Count = 0; |
| smilestone520 | 10:06cc2b1d2aaf | 170 | |
| smilestone520 | 10:06cc2b1d2aaf | 171 | ///code for PI control/// |
| smilestone520 | 10:06cc2b1d2aaf | 172 | v1_err = v1_ref - v1; |
| smilestone520 | 10:06cc2b1d2aaf | 173 | v1_ierr = 0.01f*v1_err + v1_ierr; |
| smilestone520 | 10:06cc2b1d2aaf | 174 | PIout_1 = Kp*v1_err + Ki*v1_ierr; |
| smilestone520 | 10:06cc2b1d2aaf | 175 | |
| smilestone520 | 10:06cc2b1d2aaf | 176 | if(PIout_1 >= 0.5f)PIout_1 = 0.5f; |
| smilestone520 | 10:06cc2b1d2aaf | 177 | else if(PIout_1 <= -0.5f)PIout_1 = -0.5f; |
| smilestone520 | 10:06cc2b1d2aaf | 178 | pwm1.write(PIout_1 + 0.5f); |
| roger5641 | 17:d96afd9d2692 | 179 | TIM1->CCER |= 0x4; |
| smilestone520 | 10:06cc2b1d2aaf | 180 | |
| smilestone520 | 10:06cc2b1d2aaf | 181 | //Motor 2 |
| smilestone520 | 10:06cc2b1d2aaf | 182 | v2 = (float)v2Count * 100.0f / 12.0f * 60.0f / 29.0f; //unit: rpm |
| smilestone520 | 10:06cc2b1d2aaf | 183 | v2Count = 0; |
| smilestone520 | 10:06cc2b1d2aaf | 184 | |
| smilestone520 | 10:06cc2b1d2aaf | 185 | ///code for PI control/// |
| smilestone520 | 10:06cc2b1d2aaf | 186 | v2_err = v2_ref - v2; |
| smilestone520 | 10:06cc2b1d2aaf | 187 | v2_ierr = 0.01f*v2_err + v2_ierr; |
| smilestone520 | 10:06cc2b1d2aaf | 188 | PIout_2 = Kp*v2_err + Ki*v2_ierr; |
| smilestone520 | 10:06cc2b1d2aaf | 189 | |
| smilestone520 | 10:06cc2b1d2aaf | 190 | if(PIout_2 >= 0.5f)PIout_2 = 0.5f; |
| smilestone520 | 10:06cc2b1d2aaf | 191 | else if(PIout_2 <= -0.5f)PIout_2 = -0.5f; |
| smilestone520 | 10:06cc2b1d2aaf | 192 | pwm2.write(PIout_2 + 0.5f); |
| smilestone520 | 10:06cc2b1d2aaf | 193 | TIM1->CCER |= 0x40; |
| smilestone520 | 10:06cc2b1d2aaf | 194 | |
| roger5641 | 17:d96afd9d2692 | 195 | |
| smilestone520 | 11:44989c0bcea5 | 196 | //***** main AI ************************** |
| smilestone520 | 11:44989c0bcea5 | 197 | |
| smilestone520 | 11:44989c0bcea5 | 198 | |
| smilestone520 | 11:44989c0bcea5 | 199 | //***** get position information from the bluetooth |
| roger5641 | 17:d96afd9d2692 | 200 | xC = X_Position_1; |
| roger5641 | 17:d96afd9d2692 | 201 | yC = Y_Position_1; // car's position |
| smilestone520 | 11:44989c0bcea5 | 202 | |
| roger5641 | 17:d96afd9d2692 | 203 | angleC = Angle_1; // car's angle |
| roger5641 | 17:d96afd9d2692 | 204 | |
| roger5641 | 17:d96afd9d2692 | 205 | distance = Distance_Target; |
| roger5641 | 18:2db6c97a4145 | 206 | xP = Distance_Target*cos(Angle_Target*pi/180); |
| roger5641 | 18:2db6c97a4145 | 207 | yP = Distance_Target*sin(Angle_Target*pi/180);//position that car need to reach |
| smilestone520 | 11:44989c0bcea5 | 208 | |
| roger5641 | 17:d96afd9d2692 | 209 | angleR = Angle_Target; // angleR = car-nextSpot direction ---> car direction |
| smilestone520 | 11:44989c0bcea5 | 210 | |
| smilestone520 | 29:47519f2b14ad | 211 | yB1 += longC; |
| smilestone520 | 29:47519f2b14ad | 212 | yB2 -= longC ; |
| smilestone520 | 29:47519f2b14ad | 213 | xB3 += longC ; |
| smilestone520 | 29:47519f2b14ad | 214 | xB4 -= longC; |
| smilestone520 | 20:5b892e37a958 | 215 | |
| smilestone520 | 11:44989c0bcea5 | 216 | //**** AI_State ******************** |
| smilestone520 | 11:44989c0bcea5 | 217 | |
| smilestone520 | 11:44989c0bcea5 | 218 | switch(aI_State) |
| smilestone520 | 11:44989c0bcea5 | 219 | { |
| smilestone520 | 12:c1a667ca6c53 | 220 | case 0: // IDLE |
| smilestone520 | 12:c1a667ca6c53 | 221 | // IDLE check if stop color appear |
| smilestone520 | 12:c1a667ca6c53 | 222 | //check purple color appear or not |
| roger5641 | 17:d96afd9d2692 | 223 | if(xP==0 && yP==0 && angleR==0) |
| roger5641 | 17:d96afd9d2692 | 224 | { |
| roger5641 | 17:d96afd9d2692 | 225 | v1_ref = 0; |
| roger5641 | 17:d96afd9d2692 | 226 | v2_ref = 0; |
| roger5641 | 17:d96afd9d2692 | 227 | } |
| roger5641 | 17:d96afd9d2692 | 228 | else |
| roger5641 | 17:d96afd9d2692 | 229 | aI_State = 1; |
| smilestone520 | 14:c46f22bcaa38 | 230 | //****setSpecs(); |
| smilestone520 | 11:44989c0bcea5 | 231 | break; |
| smilestone520 | 16:a102929b2228 | 232 | |
| roger5641 | 17:d96afd9d2692 | 233 | case 1: /// border condition |
| smilestone520 | 12:c1a667ca6c53 | 234 | // check if car fit border conditions |
| smilestone520 | 21:6e8ab9487985 | 235 | switch(bor_state){ |
| smilestone520 | 21:6e8ab9487985 | 236 | case 1: |
| smilestone520 | 21:6e8ab9487985 | 237 | funcBorder(); |
| smilestone520 | 21:6e8ab9487985 | 238 | break; |
| smilestone520 | 22:d7b98234291c | 239 | case 2: // 前進 再判別 v1+ v2- |
| smilestone520 | 27:1696ef46b538 | 240 | if(ticCheck >0) //誤差容忍正負五度 |
| smilestone520 | 22:d7b98234291c | 241 | { |
| smilestone520 | 22:d7b98234291c | 242 | v1_ref = 100; |
| smilestone520 | 22:d7b98234291c | 243 | v2_ref = -100; |
| smilestone520 | 27:1696ef46b538 | 244 | ticCheck -- ; |
| smilestone520 | 22:d7b98234291c | 245 | } |
| smilestone520 | 27:1696ef46b538 | 246 | else |
| smilestone520 | 27:1696ef46b538 | 247 | { |
| smilestone520 | 27:1696ef46b538 | 248 | bor_state = 1; |
| smilestone520 | 27:1696ef46b538 | 249 | ticCheck = 10; |
| smilestone520 | 27:1696ef46b538 | 250 | } |
| smilestone520 | 27:1696ef46b538 | 251 | break; |
| smilestone520 | 27:1696ef46b538 | 252 | case 3: // 後退 再判別 v1- v2+ |
| smilestone520 | 27:1696ef46b538 | 253 | |
| smilestone520 | 27:1696ef46b538 | 254 | if(ticCheck >0) //誤差容忍正負五度 |
| smilestone520 | 27:1696ef46b538 | 255 | { |
| smilestone520 | 27:1696ef46b538 | 256 | v1_ref = -100; |
| smilestone520 | 27:1696ef46b538 | 257 | v2_ref = +100; |
| smilestone520 | 27:1696ef46b538 | 258 | ticCheck -- ; |
| smilestone520 | 27:1696ef46b538 | 259 | } |
| smilestone520 | 27:1696ef46b538 | 260 | else |
| smilestone520 | 27:1696ef46b538 | 261 | { |
| smilestone520 | 27:1696ef46b538 | 262 | bor_state = 1; |
| smilestone520 | 27:1696ef46b538 | 263 | ticCheck = 10; |
| smilestone520 | 27:1696ef46b538 | 264 | } |
| smilestone520 | 22:d7b98234291c | 265 | break; |
| smilestone520 | 22:d7b98234291c | 266 | case 4: // 順時轉到特定角 結束 v1-- v2 -- |
| smilestone520 | 27:1696ef46b538 | 267 | |
| smilestone520 | 28:d576b1190610 | 268 | if((borAngle-angleC)> 5 || (borAngle-angleC)<-5)//誤差容忍正負五度 |
| smilestone520 | 28:d576b1190610 | 269 | { |
| smilestone520 | 22:d7b98234291c | 270 | v1_ref = -100; |
| smilestone520 | 22:d7b98234291c | 271 | v2_ref = -100; |
| smilestone520 | 28:d576b1190610 | 272 | } |
| smilestone520 | 27:1696ef46b538 | 273 | else |
| smilestone520 | 27:1696ef46b538 | 274 | { |
| smilestone520 | 27:1696ef46b538 | 275 | borAngle = 0; |
| smilestone520 | 27:1696ef46b538 | 276 | aI_State = 2; |
| smilestone520 | 27:1696ef46b538 | 277 | bor_state = 1; |
| smilestone520 | 22:d7b98234291c | 278 | } |
| smilestone520 | 21:6e8ab9487985 | 279 | break; |
| smilestone520 | 22:d7b98234291c | 280 | case 5: // 逆時轉到特定角 結束 v1 ++ v2++ |
| smilestone520 | 25:7053736ea6cc | 281 | if((borAngle-angleC)> 5 || (borAngle-angleC)<-5)//誤差容忍正負五度 |
| smilestone520 | 23:948ce071cbb6 | 282 | { |
| smilestone520 | 23:948ce071cbb6 | 283 | v1_ref = 100; |
| smilestone520 | 23:948ce071cbb6 | 284 | v2_ref = 100; |
| smilestone520 | 23:948ce071cbb6 | 285 | } |
| smilestone520 | 27:1696ef46b538 | 286 | else |
| smilestone520 | 27:1696ef46b538 | 287 | { |
| smilestone520 | 27:1696ef46b538 | 288 | borAngle = 0; |
| smilestone520 | 27:1696ef46b538 | 289 | aI_State = 2; |
| smilestone520 | 27:1696ef46b538 | 290 | bor_state = 1; |
| smilestone520 | 27:1696ef46b538 | 291 | } |
| smilestone520 | 21:6e8ab9487985 | 292 | break; |
| smilestone520 | 21:6e8ab9487985 | 293 | |
| smilestone520 | 21:6e8ab9487985 | 294 | } |
| smilestone520 | 21:6e8ab9487985 | 295 | |
| roger5641 | 17:d96afd9d2692 | 296 | //****to case2 |
| smilestone520 | 11:44989c0bcea5 | 297 | break; |
| roger5641 | 17:d96afd9d2692 | 298 | case 2://move to get ball |
| roger5641 | 17:d96afd9d2692 | 299 | if(-3<=angleR<=3) // direct to the ball |
| roger5641 | 17:d96afd9d2692 | 300 | { |
| roger5641 | 17:d96afd9d2692 | 301 | if(distance<=10) // check夾具距離 |
| roger5641 | 17:d96afd9d2692 | 302 | { |
| roger5641 | 17:d96afd9d2692 | 303 | v1_ref = 0; |
| roger5641 | 17:d96afd9d2692 | 304 | v2_ref = 0; |
| roger5641 | 17:d96afd9d2692 | 305 | aI_State = 3; |
| roger5641 | 17:d96afd9d2692 | 306 | } |
| roger5641 | 17:d96afd9d2692 | 307 | else if(10<distance<50) |
| roger5641 | 17:d96afd9d2692 | 308 | { |
| roger5641 | 17:d96afd9d2692 | 309 | v1_ref = 100; |
| roger5641 | 17:d96afd9d2692 | 310 | v2_ref = -100; |
| roger5641 | 17:d96afd9d2692 | 311 | } |
| roger5641 | 17:d96afd9d2692 | 312 | else |
| roger5641 | 17:d96afd9d2692 | 313 | { |
| roger5641 | 17:d96afd9d2692 | 314 | v1_ref = 300; |
| roger5641 | 17:d96afd9d2692 | 315 | v2_ref = -300; |
| roger5641 | 17:d96afd9d2692 | 316 | } |
| roger5641 | 17:d96afd9d2692 | 317 | } |
| roger5641 | 17:d96afd9d2692 | 318 | |
| roger5641 | 17:d96afd9d2692 | 319 | else if(3<angleR<=15) // small angle right |
| roger5641 | 17:d96afd9d2692 | 320 | { |
| roger5641 | 17:d96afd9d2692 | 321 | if(distance<50) |
| roger5641 | 17:d96afd9d2692 | 322 | { |
| roger5641 | 17:d96afd9d2692 | 323 | v1_ref = 200; |
| smilestone520 | 20:5b892e37a958 | 324 | v2_ref = -(v1_ref-angleR*k_turn); |
| roger5641 | 17:d96afd9d2692 | 325 | } |
| roger5641 | 17:d96afd9d2692 | 326 | else |
| roger5641 | 17:d96afd9d2692 | 327 | { |
| roger5641 | 17:d96afd9d2692 | 328 | v1_ref = 300; |
| smilestone520 | 20:5b892e37a958 | 329 | v2_ref = -(v1_ref-angleR*k_turn); |
| roger5641 | 17:d96afd9d2692 | 330 | } |
| roger5641 | 17:d96afd9d2692 | 331 | |
| roger5641 | 17:d96afd9d2692 | 332 | } |
| smilestone520 | 20:5b892e37a958 | 333 | else if(-15 <= angleR < -3) // small angle left |
| roger5641 | 17:d96afd9d2692 | 334 | { |
| roger5641 | 17:d96afd9d2692 | 335 | if(distance<50) |
| roger5641 | 17:d96afd9d2692 | 336 | { |
| smilestone520 | 20:5b892e37a958 | 337 | v1_ref = -(v2_ref+angleR*k_turn); |
| roger5641 | 17:d96afd9d2692 | 338 | v2_ref = 200; |
| roger5641 | 17:d96afd9d2692 | 339 | } |
| roger5641 | 17:d96afd9d2692 | 340 | else |
| roger5641 | 17:d96afd9d2692 | 341 | { |
| smilestone520 | 20:5b892e37a958 | 342 | v1_ref = -(v2_ref+angleR*k_turn); |
| roger5641 | 17:d96afd9d2692 | 343 | v2_ref = 300; |
| roger5641 | 17:d96afd9d2692 | 344 | } |
| roger5641 | 17:d96afd9d2692 | 345 | |
| roger5641 | 17:d96afd9d2692 | 346 | } |
| roger5641 | 17:d96afd9d2692 | 347 | else if(15<angleR<=180) // big angle right |
| roger5641 | 17:d96afd9d2692 | 348 | { |
| roger5641 | 17:d96afd9d2692 | 349 | v1_ref = 200; |
| roger5641 | 17:d96afd9d2692 | 350 | v2_ref = 200; |
| roger5641 | 17:d96afd9d2692 | 351 | wait(10); //need to count the relation between angle and time in v = 200 |
| roger5641 | 17:d96afd9d2692 | 352 | if(distance<50) |
| roger5641 | 17:d96afd9d2692 | 353 | { |
| roger5641 | 17:d96afd9d2692 | 354 | v1_ref = 100; |
| roger5641 | 17:d96afd9d2692 | 355 | v2_ref = -100; |
| roger5641 | 17:d96afd9d2692 | 356 | } |
| roger5641 | 17:d96afd9d2692 | 357 | else |
| roger5641 | 17:d96afd9d2692 | 358 | { |
| roger5641 | 17:d96afd9d2692 | 359 | v1_ref = 300; |
| roger5641 | 17:d96afd9d2692 | 360 | v2_ref = -300; |
| roger5641 | 17:d96afd9d2692 | 361 | } |
| roger5641 | 17:d96afd9d2692 | 362 | |
| roger5641 | 17:d96afd9d2692 | 363 | } |
| roger5641 | 17:d96afd9d2692 | 364 | else if(-180<=angleR<-15) // big angle left |
| roger5641 | 17:d96afd9d2692 | 365 | { |
| roger5641 | 17:d96afd9d2692 | 366 | v1_ref = -200; |
| roger5641 | 17:d96afd9d2692 | 367 | v2_ref = -200; |
| roger5641 | 17:d96afd9d2692 | 368 | wait(10); //need to count the relation between angle and time in v = 200 |
| roger5641 | 17:d96afd9d2692 | 369 | if(0<distance<50) |
| roger5641 | 17:d96afd9d2692 | 370 | { |
| roger5641 | 17:d96afd9d2692 | 371 | v1_ref = 100; |
| roger5641 | 17:d96afd9d2692 | 372 | v2_ref = -100; |
| roger5641 | 17:d96afd9d2692 | 373 | } |
| roger5641 | 17:d96afd9d2692 | 374 | else |
| roger5641 | 17:d96afd9d2692 | 375 | { |
| roger5641 | 17:d96afd9d2692 | 376 | v1_ref = 300; |
| roger5641 | 17:d96afd9d2692 | 377 | v2_ref = -300; |
| roger5641 | 17:d96afd9d2692 | 378 | } |
| roger5641 | 17:d96afd9d2692 | 379 | |
| roger5641 | 17:d96afd9d2692 | 380 | } |
| roger5641 | 17:d96afd9d2692 | 381 | |
| roger5641 | 17:d96afd9d2692 | 382 | break; |
| roger5641 | 17:d96afd9d2692 | 383 | case 3: //****getBall(); |
| roger5641 | 17:d96afd9d2692 | 384 | if((distance == 10) && (-3<=angleR<=3)) |
| roger5641 | 17:d96afd9d2692 | 385 | { |
| roger5641 | 17:d96afd9d2692 | 386 | angle = -10; |
| roger5641 | 17:d96afd9d2692 | 387 | servo_duty = 0.079 + (0.084/180)*angle; |
| roger5641 | 17:d96afd9d2692 | 388 | servo.write(servo_duty); |
| roger5641 | 17:d96afd9d2692 | 389 | servo = 1; |
| roger5641 | 17:d96afd9d2692 | 390 | wait(0.1); |
| roger5641 | 17:d96afd9d2692 | 391 | servo = 0; |
| roger5641 | 17:d96afd9d2692 | 392 | } |
| roger5641 | 17:d96afd9d2692 | 393 | else |
| roger5641 | 17:d96afd9d2692 | 394 | aI_State = 2; |
| roger5641 | 17:d96afd9d2692 | 395 | |
| smilestone520 | 11:44989c0bcea5 | 396 | break; |
| smilestone520 | 12:c1a667ca6c53 | 397 | case 4: /// move to the gate and release ball |
| smilestone520 | 12:c1a667ca6c53 | 398 | // move to the point in front of the gate first |
| smilestone520 | 12:c1a667ca6c53 | 399 | // then head to the gate |
| roger5641 | 18:2db6c97a4145 | 400 | distanceGate = sqrt(((xGate-xP)*(xGate-xP))+((yGate-yP)*(yGate-yP))); |
| roger5641 | 17:d96afd9d2692 | 401 | angleGate = atan2((yGate-yP),(xGate-xP))*180/pi; |
| roger5641 | 17:d96afd9d2692 | 402 | if(-3<=angleGate<=3) // direct to the gate |
| roger5641 | 17:d96afd9d2692 | 403 | { |
| roger5641 | 17:d96afd9d2692 | 404 | if(distanceGate<=10) // check distance between car cemtroid and gate |
| roger5641 | 17:d96afd9d2692 | 405 | { |
| roger5641 | 17:d96afd9d2692 | 406 | v1_ref = 0; |
| roger5641 | 17:d96afd9d2692 | 407 | v2_ref = 0; |
| roger5641 | 17:d96afd9d2692 | 408 | aI_State = 5; |
| roger5641 | 17:d96afd9d2692 | 409 | } |
| roger5641 | 17:d96afd9d2692 | 410 | else if(10<distanceGate<50) |
| roger5641 | 17:d96afd9d2692 | 411 | { |
| roger5641 | 17:d96afd9d2692 | 412 | v1_ref = 100; |
| roger5641 | 17:d96afd9d2692 | 413 | v2_ref = -100; |
| roger5641 | 17:d96afd9d2692 | 414 | } |
| roger5641 | 17:d96afd9d2692 | 415 | else |
| roger5641 | 17:d96afd9d2692 | 416 | { |
| roger5641 | 17:d96afd9d2692 | 417 | v1_ref = 300; |
| roger5641 | 17:d96afd9d2692 | 418 | v2_ref = -300; |
| roger5641 | 17:d96afd9d2692 | 419 | } |
| roger5641 | 17:d96afd9d2692 | 420 | } |
| roger5641 | 17:d96afd9d2692 | 421 | else if(3<angleGate<=15) // small angle right |
| roger5641 | 17:d96afd9d2692 | 422 | { |
| roger5641 | 17:d96afd9d2692 | 423 | if(distanceGate<50) |
| roger5641 | 17:d96afd9d2692 | 424 | { |
| roger5641 | 17:d96afd9d2692 | 425 | v1_ref = 200; |
| smilestone520 | 20:5b892e37a958 | 426 | v2_ref = -(v1_ref-angleGate*k_turn); |
| roger5641 | 17:d96afd9d2692 | 427 | } |
| roger5641 | 17:d96afd9d2692 | 428 | else |
| roger5641 | 17:d96afd9d2692 | 429 | { |
| roger5641 | 17:d96afd9d2692 | 430 | v1_ref = 300; |
| smilestone520 | 20:5b892e37a958 | 431 | v2_ref = -(v1_ref-angleGate*k_turn); |
| roger5641 | 17:d96afd9d2692 | 432 | } |
| roger5641 | 17:d96afd9d2692 | 433 | } |
| roger5641 | 17:d96afd9d2692 | 434 | else if(-15<=angleGate<-3) // small angle left |
| roger5641 | 17:d96afd9d2692 | 435 | { |
| roger5641 | 17:d96afd9d2692 | 436 | if(distanceGate<50) |
| roger5641 | 17:d96afd9d2692 | 437 | { |
| smilestone520 | 20:5b892e37a958 | 438 | v1_ref = -(v2_ref+angleGate*k_turn); |
| roger5641 | 17:d96afd9d2692 | 439 | v2_ref = 200; |
| roger5641 | 17:d96afd9d2692 | 440 | } |
| roger5641 | 17:d96afd9d2692 | 441 | else |
| roger5641 | 17:d96afd9d2692 | 442 | { |
| smilestone520 | 20:5b892e37a958 | 443 | v1_ref = -(v2_ref+angleGate*k_turn); |
| roger5641 | 17:d96afd9d2692 | 444 | v2_ref = 300; |
| roger5641 | 17:d96afd9d2692 | 445 | } |
| roger5641 | 17:d96afd9d2692 | 446 | } |
| roger5641 | 17:d96afd9d2692 | 447 | else if(15<angleGate<=180) // big angle right |
| roger5641 | 17:d96afd9d2692 | 448 | { |
| roger5641 | 17:d96afd9d2692 | 449 | v1_ref = 200; |
| roger5641 | 17:d96afd9d2692 | 450 | v2_ref = 200; |
| roger5641 | 17:d96afd9d2692 | 451 | wait(10); //need to count the relation between angle and time in v = 200 |
| roger5641 | 17:d96afd9d2692 | 452 | if(distanceGate<50) |
| roger5641 | 17:d96afd9d2692 | 453 | { |
| roger5641 | 17:d96afd9d2692 | 454 | v1_ref = 100; |
| roger5641 | 17:d96afd9d2692 | 455 | v2_ref = -100; |
| roger5641 | 17:d96afd9d2692 | 456 | } |
| roger5641 | 17:d96afd9d2692 | 457 | else |
| roger5641 | 17:d96afd9d2692 | 458 | { |
| roger5641 | 17:d96afd9d2692 | 459 | v1_ref = 300; |
| roger5641 | 17:d96afd9d2692 | 460 | v2_ref = -300; |
| roger5641 | 17:d96afd9d2692 | 461 | } |
| roger5641 | 17:d96afd9d2692 | 462 | } |
| roger5641 | 17:d96afd9d2692 | 463 | else if(-180<=angleGate<-15) // big angle left |
| roger5641 | 17:d96afd9d2692 | 464 | { |
| roger5641 | 17:d96afd9d2692 | 465 | v1_ref = -200; |
| roger5641 | 17:d96afd9d2692 | 466 | v2_ref = -200; |
| roger5641 | 17:d96afd9d2692 | 467 | wait(10); //need to count the relation between angle and time in v = 200 |
| roger5641 | 17:d96afd9d2692 | 468 | if(0<distanceGate<50) |
| roger5641 | 17:d96afd9d2692 | 469 | { |
| roger5641 | 17:d96afd9d2692 | 470 | v1_ref = 100; |
| roger5641 | 17:d96afd9d2692 | 471 | v2_ref = -100; |
| roger5641 | 17:d96afd9d2692 | 472 | } |
| roger5641 | 17:d96afd9d2692 | 473 | else |
| roger5641 | 17:d96afd9d2692 | 474 | { |
| roger5641 | 17:d96afd9d2692 | 475 | v1_ref = 300; |
| roger5641 | 17:d96afd9d2692 | 476 | v2_ref = -300; |
| roger5641 | 17:d96afd9d2692 | 477 | } |
| roger5641 | 17:d96afd9d2692 | 478 | } |
| roger5641 | 17:d96afd9d2692 | 479 | |
| smilestone520 | 12:c1a667ca6c53 | 480 | break; |
| roger5641 | 17:d96afd9d2692 | 481 | case 5: // release ball |
| roger5641 | 17:d96afd9d2692 | 482 | if((-3<=angleGate<=3) && (distanceGate<=10)) // direct to the gate |
| roger5641 | 17:d96afd9d2692 | 483 | { |
| roger5641 | 17:d96afd9d2692 | 484 | angle = 90; // Fixture up |
| roger5641 | 17:d96afd9d2692 | 485 | servo_duty = 0.079 + (0.084/180)*angle; |
| roger5641 | 17:d96afd9d2692 | 486 | servo.write(servo_duty); |
| roger5641 | 17:d96afd9d2692 | 487 | servo = 1; |
| roger5641 | 17:d96afd9d2692 | 488 | wait(0.1); |
| roger5641 | 17:d96afd9d2692 | 489 | servo = 0; |
| roger5641 | 17:d96afd9d2692 | 490 | |
| roger5641 | 17:d96afd9d2692 | 491 | pwm1.write(0.1f + 0.5f); // push |
| roger5641 | 17:d96afd9d2692 | 492 | pwm2.write(-0.1f + 0.5f); |
| roger5641 | 17:d96afd9d2692 | 493 | wait(0.5); |
| roger5641 | 17:d96afd9d2692 | 494 | |
| roger5641 | 17:d96afd9d2692 | 495 | angle = -10; // Fixture down |
| roger5641 | 17:d96afd9d2692 | 496 | servo_duty = 0.079 + (0.084/180)*angle; |
| roger5641 | 17:d96afd9d2692 | 497 | servo.write(servo_duty); |
| roger5641 | 17:d96afd9d2692 | 498 | servo = 1; |
| roger5641 | 17:d96afd9d2692 | 499 | wait(0.1); |
| roger5641 | 17:d96afd9d2692 | 500 | servo = 0; |
| roger5641 | 17:d96afd9d2692 | 501 | |
| roger5641 | 17:d96afd9d2692 | 502 | pwm1.write(-0.3f + 0.5f); // back and leave gate |
| roger5641 | 17:d96afd9d2692 | 503 | pwm2.write(0.3f + 0.5f); |
| roger5641 | 17:d96afd9d2692 | 504 | wait(1); |
| roger5641 | 17:d96afd9d2692 | 505 | |
| roger5641 | 18:2db6c97a4145 | 506 | aI_State = 0; |
| roger5641 | 17:d96afd9d2692 | 507 | |
| roger5641 | 17:d96afd9d2692 | 508 | } |
| roger5641 | 17:d96afd9d2692 | 509 | |
| roger5641 | 17:d96afd9d2692 | 510 | break; |
| roger5641 | 26:dbdbfdb4dd41 | 511 | |
| smilestone520 | 11:44989c0bcea5 | 512 | } |
| roger5641 | 26:dbdbfdb4dd41 | 513 | |
| smilestone520 | 10:06cc2b1d2aaf | 514 | } |
| smilestone520 | 10:06cc2b1d2aaf | 515 | |
| smilestone520 | 10:06cc2b1d2aaf | 516 | void CN_interrupt(void) |
| smilestone520 | 10:06cc2b1d2aaf | 517 | { |
| smilestone520 | 10:06cc2b1d2aaf | 518 | //Motor 1 |
| smilestone520 | 10:06cc2b1d2aaf | 519 | stateA_1 = HallA_1.read(); |
| smilestone520 | 10:06cc2b1d2aaf | 520 | stateB_1 = HallB_1.read(); |
| smilestone520 | 10:06cc2b1d2aaf | 521 | |
| smilestone520 | 10:06cc2b1d2aaf | 522 | ///code for state determination/// |
| smilestone520 | 10:06cc2b1d2aaf | 523 | if(stateA_1==0&&stateB_1==0){ |
| smilestone520 | 10:06cc2b1d2aaf | 524 | state_1 = 1;} |
| smilestone520 | 10:06cc2b1d2aaf | 525 | else if(stateA_1==0&&stateB_1==1){ |
| smilestone520 | 10:06cc2b1d2aaf | 526 | state_1 = 2;} |
| smilestone520 | 10:06cc2b1d2aaf | 527 | else if(stateA_1==1&&stateB_1==1){ |
| smilestone520 | 10:06cc2b1d2aaf | 528 | state_1 = 3;} |
| smilestone520 | 10:06cc2b1d2aaf | 529 | else if(stateA_1==1&&stateB_1==0){ |
| smilestone520 | 10:06cc2b1d2aaf | 530 | state_1 = 4;} |
| smilestone520 | 10:06cc2b1d2aaf | 531 | |
| smilestone520 | 10:06cc2b1d2aaf | 532 | if(state_1 == 1) |
| smilestone520 | 10:06cc2b1d2aaf | 533 | { |
| smilestone520 | 10:06cc2b1d2aaf | 534 | if(state_1-state_1_old == -3) |
| smilestone520 | 10:06cc2b1d2aaf | 535 | v1Count=v1Count+1; |
| smilestone520 | 10:06cc2b1d2aaf | 536 | else if(state_1-state_1_old == -1) |
| smilestone520 | 10:06cc2b1d2aaf | 537 | v1Count=v1Count-1; |
| smilestone520 | 10:06cc2b1d2aaf | 538 | } |
| smilestone520 | 10:06cc2b1d2aaf | 539 | else if(state_1 == 2) |
| smilestone520 | 10:06cc2b1d2aaf | 540 | { |
| smilestone520 | 10:06cc2b1d2aaf | 541 | if(state_1-state_1_old == 1) |
| smilestone520 | 10:06cc2b1d2aaf | 542 | v1Count=v1Count+1; |
| smilestone520 | 10:06cc2b1d2aaf | 543 | else if(state_1-state_1_old == -1) |
| smilestone520 | 10:06cc2b1d2aaf | 544 | v1Count=v1Count-1; |
| smilestone520 | 10:06cc2b1d2aaf | 545 | } |
| smilestone520 | 10:06cc2b1d2aaf | 546 | else if(state_1 == 3) |
| smilestone520 | 10:06cc2b1d2aaf | 547 | { |
| smilestone520 | 10:06cc2b1d2aaf | 548 | if(state_1-state_1_old == 1) |
| smilestone520 | 10:06cc2b1d2aaf | 549 | v1Count=v1Count+1; |
| smilestone520 | 10:06cc2b1d2aaf | 550 | else if(state_1-state_1_old == -1) |
| smilestone520 | 10:06cc2b1d2aaf | 551 | v1Count=v1Count-1; |
| smilestone520 | 10:06cc2b1d2aaf | 552 | } |
| smilestone520 | 10:06cc2b1d2aaf | 553 | else if(state_1 == 4) |
| smilestone520 | 10:06cc2b1d2aaf | 554 | { |
| smilestone520 | 10:06cc2b1d2aaf | 555 | if(state_1-state_1_old == 1) |
| smilestone520 | 10:06cc2b1d2aaf | 556 | v1Count=v1Count+1; |
| smilestone520 | 10:06cc2b1d2aaf | 557 | else if(state_1-state_1_old == 3) |
| smilestone520 | 10:06cc2b1d2aaf | 558 | v1Count=v1Count-1; |
| smilestone520 | 10:06cc2b1d2aaf | 559 | } |
| smilestone520 | 10:06cc2b1d2aaf | 560 | state_1_old = state_1; |
| smilestone520 | 10:06cc2b1d2aaf | 561 | |
| smilestone520 | 10:06cc2b1d2aaf | 562 | |
| smilestone520 | 10:06cc2b1d2aaf | 563 | ////////////////////////////////// |
| smilestone520 | 10:06cc2b1d2aaf | 564 | |
| smilestone520 | 10:06cc2b1d2aaf | 565 | //Forward |
| smilestone520 | 10:06cc2b1d2aaf | 566 | //v1Count +1 |
| smilestone520 | 10:06cc2b1d2aaf | 567 | //Inverse |
| smilestone520 | 10:06cc2b1d2aaf | 568 | //v1Count -1 |
| smilestone520 | 10:06cc2b1d2aaf | 569 | |
| smilestone520 | 10:06cc2b1d2aaf | 570 | |
| smilestone520 | 10:06cc2b1d2aaf | 571 | //Motor 2 |
| smilestone520 | 10:06cc2b1d2aaf | 572 | stateA_2 = HallA_2.read(); |
| smilestone520 | 10:06cc2b1d2aaf | 573 | stateB_2 = HallB_2.read(); |
| smilestone520 | 10:06cc2b1d2aaf | 574 | |
| smilestone520 | 10:06cc2b1d2aaf | 575 | ///code for state determination/// |
| smilestone520 | 10:06cc2b1d2aaf | 576 | if(stateA_2==0&&stateB_2==0){ |
| smilestone520 | 10:06cc2b1d2aaf | 577 | state_2 = 1;} |
| smilestone520 | 10:06cc2b1d2aaf | 578 | else if(stateA_2==0&&stateB_2==1){ |
| smilestone520 | 10:06cc2b1d2aaf | 579 | state_2 = 2;} |
| smilestone520 | 10:06cc2b1d2aaf | 580 | else if(stateA_2==1&&stateB_2==1){ |
| smilestone520 | 10:06cc2b1d2aaf | 581 | state_2 = 3;} |
| smilestone520 | 10:06cc2b1d2aaf | 582 | else if(stateA_2==1&&stateB_2==0){ |
| smilestone520 | 10:06cc2b1d2aaf | 583 | state_2 = 4;} |
| smilestone520 | 10:06cc2b1d2aaf | 584 | |
| smilestone520 | 10:06cc2b1d2aaf | 585 | if(state_2 == 1) |
| smilestone520 | 10:06cc2b1d2aaf | 586 | { |
| smilestone520 | 10:06cc2b1d2aaf | 587 | if(state_2-state_2_old == -3) |
| smilestone520 | 10:06cc2b1d2aaf | 588 | v2Count=v2Count+1; |
| smilestone520 | 10:06cc2b1d2aaf | 589 | else if(state_2-state_2_old == -1) |
| smilestone520 | 10:06cc2b1d2aaf | 590 | v2Count=v2Count-1; |
| smilestone520 | 10:06cc2b1d2aaf | 591 | } |
| smilestone520 | 10:06cc2b1d2aaf | 592 | else if(state_2 == 2) |
| smilestone520 | 10:06cc2b1d2aaf | 593 | { |
| smilestone520 | 10:06cc2b1d2aaf | 594 | if(state_2-state_2_old == 1) |
| smilestone520 | 10:06cc2b1d2aaf | 595 | v2Count=v2Count+1; |
| smilestone520 | 10:06cc2b1d2aaf | 596 | else if(state_2-state_2_old == -1) |
| smilestone520 | 10:06cc2b1d2aaf | 597 | v2Count=v2Count-1; |
| smilestone520 | 10:06cc2b1d2aaf | 598 | } |
| smilestone520 | 10:06cc2b1d2aaf | 599 | else if(state_2 == 3) |
| smilestone520 | 10:06cc2b1d2aaf | 600 | { |
| smilestone520 | 10:06cc2b1d2aaf | 601 | if(state_2-state_2_old == 1) |
| smilestone520 | 10:06cc2b1d2aaf | 602 | v2Count=v2Count+1; |
| smilestone520 | 10:06cc2b1d2aaf | 603 | else if(state_2-state_2_old == -1) |
| smilestone520 | 10:06cc2b1d2aaf | 604 | v2Count=v2Count-1; |
| smilestone520 | 10:06cc2b1d2aaf | 605 | } |
| smilestone520 | 10:06cc2b1d2aaf | 606 | else if(state_2 == 4) |
| smilestone520 | 10:06cc2b1d2aaf | 607 | { |
| smilestone520 | 10:06cc2b1d2aaf | 608 | if(state_2-state_2_old == 1) |
| smilestone520 | 10:06cc2b1d2aaf | 609 | v2Count=v2Count+1; |
| smilestone520 | 10:06cc2b1d2aaf | 610 | else if(state_2-state_2_old == 3) |
| smilestone520 | 10:06cc2b1d2aaf | 611 | v2Count=v2Count-1; |
| smilestone520 | 10:06cc2b1d2aaf | 612 | } |
| smilestone520 | 10:06cc2b1d2aaf | 613 | state_2_old = state_2; |
| smilestone520 | 10:06cc2b1d2aaf | 614 | |
| smilestone520 | 10:06cc2b1d2aaf | 615 | ////////////////////////////////// |
| smilestone520 | 10:06cc2b1d2aaf | 616 | |
| smilestone520 | 10:06cc2b1d2aaf | 617 | //Forward |
| smilestone520 | 10:06cc2b1d2aaf | 618 | //v2Count +1 |
| smilestone520 | 10:06cc2b1d2aaf | 619 | //Inverse |
| smilestone520 | 10:06cc2b1d2aaf | 620 | //v2Count -1 |
| smilestone520 | 10:06cc2b1d2aaf | 621 | } |
| smilestone520 | 10:06cc2b1d2aaf | 622 | |
| smilestone520 | 10:06cc2b1d2aaf | 623 | void init_TIMER(void) |
| smilestone520 | 10:06cc2b1d2aaf | 624 | { |
| smilestone520 | 10:06cc2b1d2aaf | 625 | timer1.attach_us(&timer1_interrupt, 10000);//10ms interrupt period (100 Hz) |
| smilestone520 | 10:06cc2b1d2aaf | 626 | } |
| smilestone520 | 10:06cc2b1d2aaf | 627 | |
| smilestone520 | 10:06cc2b1d2aaf | 628 | void init_PWM(void) |
| smilestone520 | 10:06cc2b1d2aaf | 629 | { |
| smilestone520 | 10:06cc2b1d2aaf | 630 | pwm1.period_us(50); |
| smilestone520 | 10:06cc2b1d2aaf | 631 | pwm1.write(0.5); |
| smilestone520 | 10:06cc2b1d2aaf | 632 | TIM1->CCER |= 0x4; |
| smilestone520 | 10:06cc2b1d2aaf | 633 | |
| smilestone520 | 10:06cc2b1d2aaf | 634 | pwm2.period_us(50); |
| smilestone520 | 10:06cc2b1d2aaf | 635 | pwm2.write(0.5); |
| smilestone520 | 10:06cc2b1d2aaf | 636 | TIM1->CCER |= 0x40; |
| smilestone520 | 10:06cc2b1d2aaf | 637 | } |
| smilestone520 | 10:06cc2b1d2aaf | 638 | |
| smilestone520 | 10:06cc2b1d2aaf | 639 | void init_CN(void) |
| smilestone520 | 10:06cc2b1d2aaf | 640 | { |
| smilestone520 | 10:06cc2b1d2aaf | 641 | HallA_1.rise(&CN_interrupt); |
| smilestone520 | 10:06cc2b1d2aaf | 642 | HallA_1.fall(&CN_interrupt); |
| smilestone520 | 10:06cc2b1d2aaf | 643 | HallB_1.rise(&CN_interrupt); |
| smilestone520 | 10:06cc2b1d2aaf | 644 | HallB_1.fall(&CN_interrupt); |
| smilestone520 | 10:06cc2b1d2aaf | 645 | |
| smilestone520 | 10:06cc2b1d2aaf | 646 | HallA_2.rise(&CN_interrupt); |
| smilestone520 | 10:06cc2b1d2aaf | 647 | HallA_2.fall(&CN_interrupt); |
| smilestone520 | 10:06cc2b1d2aaf | 648 | HallB_2.rise(&CN_interrupt); |
| smilestone520 | 10:06cc2b1d2aaf | 649 | HallB_2.fall(&CN_interrupt); |
| smilestone520 | 10:06cc2b1d2aaf | 650 | |
| smilestone520 | 10:06cc2b1d2aaf | 651 | stateA_1 = HallA_1.read(); |
| smilestone520 | 10:06cc2b1d2aaf | 652 | stateB_1 = HallB_1.read(); |
| smilestone520 | 10:06cc2b1d2aaf | 653 | stateA_2 = HallA_2.read(); |
| smilestone520 | 10:06cc2b1d2aaf | 654 | stateB_2 = HallB_2.read(); |
| smilestone520 | 20:5b892e37a958 | 655 | } |
| smilestone520 | 20:5b892e37a958 | 656 | |
| smilestone520 | 20:5b892e37a958 | 657 | //**************** funcBorder() ***************************************** |
| smilestone520 | 20:5b892e37a958 | 658 | |
| smilestone520 | 20:5b892e37a958 | 659 | void funcBorder(void) // check if car is too close to the border // miss the ball restriction part |
| smilestone520 | 20:5b892e37a958 | 660 | { |
| smilestone520 | 20:5b892e37a958 | 661 | if(yC > yB1 || yC < yB2 || xC < xB3 || xC > xB4){ |
| smilestone520 | 20:5b892e37a958 | 662 | |
| smilestone520 | 20:5b892e37a958 | 663 | if(yC > yB1) // 靠近上邊界 ***************************** |
| smilestone520 | 20:5b892e37a958 | 664 | { |
| smilestone520 | 20:5b892e37a958 | 665 | if(xC < xB3) //左上角 |
| smilestone520 | 20:5b892e37a958 | 666 | { |
| smilestone520 | 20:5b892e37a958 | 667 | if(angleC <0 && angleC >=-90) // I |
| smilestone520 | 23:948ce071cbb6 | 668 | { |
| smilestone520 | 23:948ce071cbb6 | 669 | bor_state = 4; //clockwise, to angleC = 45 |
| smilestone520 | 23:948ce071cbb6 | 670 | borAngle = 45; |
| smilestone520 | 23:948ce071cbb6 | 671 | } |
| smilestone520 | 20:5b892e37a958 | 672 | |
| smilestone520 | 20:5b892e37a958 | 673 | else if(angleC > -180 && angleC <-90) // II |
| smilestone520 | 23:948ce071cbb6 | 674 | bor_state = 3; //back 0.5*longC, judge again |
| smilestone520 | 20:5b892e37a958 | 675 | |
| smilestone520 | 20:5b892e37a958 | 676 | else if(angleC <=180 && angleC >=90) // III |
| smilestone520 | 23:948ce071cbb6 | 677 | { |
| smilestone520 | 23:948ce071cbb6 | 678 | bor_state = 5;//counter clockwise, to angleC = 45 |
| smilestone520 | 23:948ce071cbb6 | 679 | borAngle = 45; |
| smilestone520 | 23:948ce071cbb6 | 680 | } |
| smilestone520 | 20:5b892e37a958 | 681 | |
| smilestone520 | 20:5b892e37a958 | 682 | else if(angleC < 90 && angleC >=0) // IV |
| smilestone520 | 23:948ce071cbb6 | 683 | bor_state = 2;//forward 0.5*longC, judge again |
| smilestone520 | 20:5b892e37a958 | 684 | |
| smilestone520 | 20:5b892e37a958 | 685 | } |
| smilestone520 | 20:5b892e37a958 | 686 | else if(xC > xB4) //右上角 |
| smilestone520 | 20:5b892e37a958 | 687 | { |
| smilestone520 | 20:5b892e37a958 | 688 | if(angleC <0 && angleC >=-90) // I |
| smilestone520 | 23:948ce071cbb6 | 689 | bor_state = 3;//back 0.5*longC, judge again |
| smilestone520 | 20:5b892e37a958 | 690 | |
| smilestone520 | 20:5b892e37a958 | 691 | else if(angleC > -180 && angleC <-90) // II |
| smilestone520 | 23:948ce071cbb6 | 692 | { |
| smilestone520 | 23:948ce071cbb6 | 693 | bor_state = 5;//counter clockwise, to angleC = 135 |
| smilestone520 | 23:948ce071cbb6 | 694 | borAngle = 135; |
| smilestone520 | 23:948ce071cbb6 | 695 | } |
| smilestone520 | 20:5b892e37a958 | 696 | |
| smilestone520 | 20:5b892e37a958 | 697 | else if(angleC <=180 && angleC >=90) // III |
| smilestone520 | 23:948ce071cbb6 | 698 | bor_state = 2;//forward 0.5*longC, judge again |
| smilestone520 | 20:5b892e37a958 | 699 | |
| smilestone520 | 20:5b892e37a958 | 700 | else if(angleC < 90 && angleC >=0) // IV |
| smilestone520 | 23:948ce071cbb6 | 701 | { |
| smilestone520 | 23:948ce071cbb6 | 702 | bor_state = 4;//clockwise, to angleC = 135 |
| smilestone520 | 23:948ce071cbb6 | 703 | borAngle = 135; |
| smilestone520 | 23:948ce071cbb6 | 704 | } |
| smilestone520 | 23:948ce071cbb6 | 705 | |
| smilestone520 | 20:5b892e37a958 | 706 | } |
| smilestone520 | 20:5b892e37a958 | 707 | else //上邊界 |
| smilestone520 | 20:5b892e37a958 | 708 | { |
| smilestone520 | 20:5b892e37a958 | 709 | if(angleC == -90 || angleC == 90) |
| smilestone520 | 24:f61847968e72 | 710 | aI_State = 2; // do nothing, turn to next point |
| smilestone520 | 20:5b892e37a958 | 711 | else |
| smilestone520 | 24:f61847968e72 | 712 | { |
| smilestone520 | 24:f61847968e72 | 713 | if(0 > borAngle >= -90 || 90< borAngle <=0) //CW |
| smilestone520 | 24:f61847968e72 | 714 | { |
| smilestone520 | 24:f61847968e72 | 715 | bor_state = 4; |
| smilestone520 | 24:f61847968e72 | 716 | borAngle = 90; |
| smilestone520 | 24:f61847968e72 | 717 | } |
| smilestone520 | 24:f61847968e72 | 718 | else |
| smilestone520 | 24:f61847968e72 | 719 | { |
| smilestone520 | 24:f61847968e72 | 720 | bor_state = 5; |
| smilestone520 | 24:f61847968e72 | 721 | borAngle = 90; |
| smilestone520 | 24:f61847968e72 | 722 | } |
| smilestone520 | 24:f61847968e72 | 723 | } // turn to 90, then turn to next point |
| smilestone520 | 20:5b892e37a958 | 724 | } |
| smilestone520 | 20:5b892e37a958 | 725 | |
| smilestone520 | 20:5b892e37a958 | 726 | |
| smilestone520 | 20:5b892e37a958 | 727 | } |
| smilestone520 | 20:5b892e37a958 | 728 | else if(yC < yB2) // 靠近下邊界 *E****************************************************88 |
| smilestone520 | 20:5b892e37a958 | 729 | { |
| smilestone520 | 20:5b892e37a958 | 730 | if(xC < xB3) //左下角 |
| smilestone520 | 20:5b892e37a958 | 731 | { |
| smilestone520 | 20:5b892e37a958 | 732 | if(angleC <0 && angleC >=-90) // I |
| smilestone520 | 23:948ce071cbb6 | 733 | bor_state = 2; //forward 0.5*longC, judge again |
| smilestone520 | 23:948ce071cbb6 | 734 | |
| smilestone520 | 20:5b892e37a958 | 735 | else if(angleC > -180 && angleC <-90) // II |
| smilestone520 | 23:948ce071cbb6 | 736 | { |
| smilestone520 | 23:948ce071cbb6 | 737 | bor_state = 4; //clockwise, to angleC = -45 |
| smilestone520 | 23:948ce071cbb6 | 738 | borAngle = -45; |
| smilestone520 | 23:948ce071cbb6 | 739 | } |
| smilestone520 | 20:5b892e37a958 | 740 | |
| smilestone520 | 20:5b892e37a958 | 741 | else if(angleC <=180 && angleC >=90) // III |
| smilestone520 | 23:948ce071cbb6 | 742 | bor_state = 3;//back 0.5*longC, judge again |
| smilestone520 | 20:5b892e37a958 | 743 | |
| smilestone520 | 20:5b892e37a958 | 744 | else if(angleC < 90 && angleC >=0) // IV |
| smilestone520 | 23:948ce071cbb6 | 745 | { |
| smilestone520 | 23:948ce071cbb6 | 746 | bor_state = 5;//counter clockwise, to angleC = -45 |
| smilestone520 | 23:948ce071cbb6 | 747 | borAngle = -45; |
| smilestone520 | 23:948ce071cbb6 | 748 | } |
| smilestone520 | 20:5b892e37a958 | 749 | |
| smilestone520 | 20:5b892e37a958 | 750 | } |
| smilestone520 | 20:5b892e37a958 | 751 | else if(xC > xB4) //右下角 |
| smilestone520 | 20:5b892e37a958 | 752 | { |
| smilestone520 | 20:5b892e37a958 | 753 | if(angleC <0 && angleC >=-90) // I |
| smilestone520 | 23:948ce071cbb6 | 754 | { |
| smilestone520 | 23:948ce071cbb6 | 755 | bor_state = 5;//counter clockwise, to angleC = -135 |
| smilestone520 | 23:948ce071cbb6 | 756 | borAngle = -135; |
| smilestone520 | 23:948ce071cbb6 | 757 | } |
| smilestone520 | 20:5b892e37a958 | 758 | |
| smilestone520 | 20:5b892e37a958 | 759 | else if(angleC > -180 && angleC <-90) // II |
| smilestone520 | 23:948ce071cbb6 | 760 | bor_state = 2;//forward 0.5*longC, judge again |
| smilestone520 | 20:5b892e37a958 | 761 | |
| smilestone520 | 20:5b892e37a958 | 762 | else if(angleC <=180 && angleC >=90) // III |
| smilestone520 | 23:948ce071cbb6 | 763 | { |
| smilestone520 | 23:948ce071cbb6 | 764 | bor_state = 4;//clockwise, to angleC = -135 |
| smilestone520 | 23:948ce071cbb6 | 765 | borAngle = -135; |
| smilestone520 | 23:948ce071cbb6 | 766 | } |
| smilestone520 | 20:5b892e37a958 | 767 | |
| smilestone520 | 20:5b892e37a958 | 768 | else if(angleC < 90 && angleC >=0) // IV |
| smilestone520 | 23:948ce071cbb6 | 769 | bor_state = 3;//back 0.5*longC, judge again |
| smilestone520 | 20:5b892e37a958 | 770 | |
| smilestone520 | 20:5b892e37a958 | 771 | } |
| smilestone520 | 20:5b892e37a958 | 772 | else //下邊界 |
| smilestone520 | 20:5b892e37a958 | 773 | { |
| smilestone520 | 20:5b892e37a958 | 774 | if(angleC == -90 || angleC == 90) |
| smilestone520 | 24:f61847968e72 | 775 | aI_State = 2; // do nothing, turn to next point |
| smilestone520 | 20:5b892e37a958 | 776 | else |
| smilestone520 | 24:f61847968e72 | 777 | { |
| smilestone520 | 24:f61847968e72 | 778 | if(0 > borAngle >= -90 || 90< borAngle <=0) //CCW |
| smilestone520 | 24:f61847968e72 | 779 | { |
| smilestone520 | 24:f61847968e72 | 780 | bor_state = 5; |
| smilestone520 | 24:f61847968e72 | 781 | borAngle = -90; |
| smilestone520 | 24:f61847968e72 | 782 | } |
| smilestone520 | 24:f61847968e72 | 783 | else |
| smilestone520 | 24:f61847968e72 | 784 | { |
| smilestone520 | 24:f61847968e72 | 785 | bor_state = 4; |
| smilestone520 | 24:f61847968e72 | 786 | borAngle = -90; |
| smilestone520 | 24:f61847968e72 | 787 | } |
| smilestone520 | 24:f61847968e72 | 788 | } // turn to -90, then turn to next point |
| smilestone520 | 20:5b892e37a958 | 789 | } |
| smilestone520 | 20:5b892e37a958 | 790 | |
| smilestone520 | 20:5b892e37a958 | 791 | |
| smilestone520 | 20:5b892e37a958 | 792 | } |
| smilestone520 | 20:5b892e37a958 | 793 | else if(xC < xB3) //靠近左邊界**************************************************************** |
| smilestone520 | 20:5b892e37a958 | 794 | { |
| smilestone520 | 20:5b892e37a958 | 795 | if(angleC == 0 || angleC == 180) |
| smilestone520 | 23:948ce071cbb6 | 796 | aI_State = 2; // do nothing, turn to next point |
| smilestone520 | 24:f61847968e72 | 797 | |
| smilestone520 | 24:f61847968e72 | 798 | else if(-180 < angleC < 0) // CW |
| smilestone520 | 24:f61847968e72 | 799 | { |
| smilestone520 | 24:f61847968e72 | 800 | bor_state = 4; |
| smilestone520 | 24:f61847968e72 | 801 | borAngle = 0; |
| smilestone520 | 24:f61847968e72 | 802 | } |
| smilestone520 | 20:5b892e37a958 | 803 | else |
| smilestone520 | 23:948ce071cbb6 | 804 | { |
| smilestone520 | 24:f61847968e72 | 805 | bor_state = 5; |
| smilestone520 | 24:f61847968e72 | 806 | borAngle = 0; |
| smilestone520 | 24:f61847968e72 | 807 | } |
| smilestone520 | 24:f61847968e72 | 808 | // turn to 0, then turn to next point |
| smilestone520 | 20:5b892e37a958 | 809 | } |
| smilestone520 | 20:5b892e37a958 | 810 | |
| smilestone520 | 20:5b892e37a958 | 811 | else if(xC > xB4) //靠近右邊界 *********************************************88 |
| smilestone520 | 20:5b892e37a958 | 812 | { |
| smilestone520 | 20:5b892e37a958 | 813 | if(angleC == 0 || angleC == 180) |
| smilestone520 | 23:948ce071cbb6 | 814 | aI_State = 2; // do nothing, turn to next point |
| smilestone520 | 24:f61847968e72 | 815 | |
| smilestone520 | 24:f61847968e72 | 816 | else if(-180 < angleC < 0) // CCW |
| smilestone520 | 24:f61847968e72 | 817 | { |
| smilestone520 | 24:f61847968e72 | 818 | bor_state = 5; |
| smilestone520 | 24:f61847968e72 | 819 | borAngle = 180; |
| smilestone520 | 24:f61847968e72 | 820 | } // turn to 90, then turn to next point |
| smilestone520 | 23:948ce071cbb6 | 821 | else |
| smilestone520 | 24:f61847968e72 | 822 | { |
| smilestone520 | 24:f61847968e72 | 823 | bor_state = 4; |
| smilestone520 | 24:f61847968e72 | 824 | borAngle = 180; |
| smilestone520 | 24:f61847968e72 | 825 | } |
| smilestone520 | 23:948ce071cbb6 | 826 | } |
| smilestone520 | 23:948ce071cbb6 | 827 | } |
| smilestone520 | 23:948ce071cbb6 | 828 | else |
| smilestone520 | 23:948ce071cbb6 | 829 | {aI_State = 2;} |
| smilestone520 | 23:948ce071cbb6 | 830 | } // funcBorder() |
| smilestone520 | 23:948ce071cbb6 | 831 | |
| smilestone520 | 20:5b892e37a958 | 832 | //**************** smallAngle() ***************************************** |
| smilestone520 | 20:5b892e37a958 | 833 | |
| smilestone520 | 20:5b892e37a958 | 834 | void smallAngle( float goodAngle ) // use the smallest turn to right angle ccw / cw, based on 0~360 degree system |
| smilestone520 | 24:f61847968e72 | 835 | { |
| smilestone520 | 20:5b892e37a958 | 836 | if(angleR > 0) // CW |
| smilestone520 | 20:5b892e37a958 | 837 | { |
| smilestone520 | 20:5b892e37a958 | 838 | // turn clockwise to goodAngle; |
| smilestone520 | 20:5b892e37a958 | 839 | } |
| smilestone520 | 20:5b892e37a958 | 840 | else if(angleR < 0) //CCW |
| smilestone520 | 20:5b892e37a958 | 841 | { |
| smilestone520 | 20:5b892e37a958 | 842 | // turn counter clockwise to goodAngle; |
| smilestone520 | 20:5b892e37a958 | 843 | } |
| smilestone520 | 20:5b892e37a958 | 844 | |
| smilestone520 | 24:f61847968e72 | 845 | } |
| smilestone520 | 20:5b892e37a958 | 846 | //*************** turnCW() *********************************** |
| smilestone520 | 20:5b892e37a958 | 847 | |
| smilestone520 | 20:5b892e37a958 | 848 | void turnCW(float goodAngle) |
| smilestone520 | 20:5b892e37a958 | 849 | { |
| smilestone520 | 20:5b892e37a958 | 850 | // turn clockwise to goodAngle; |
| smilestone520 | 20:5b892e37a958 | 851 | } |
| smilestone520 | 20:5b892e37a958 | 852 | |
| smilestone520 | 20:5b892e37a958 | 853 | //*************** turnCCW() *********************************** |
| smilestone520 | 20:5b892e37a958 | 854 | |
| smilestone520 | 20:5b892e37a958 | 855 | void turnCCW(float goodAngle) |
| smilestone520 | 20:5b892e37a958 | 856 | { |
| smilestone520 | 20:5b892e37a958 | 857 | // turn counter clockwise to goodAngle; |
| smilestone520 | 10:06cc2b1d2aaf | 858 | } |