Robotics Term Project / Mbed 2 deprecated Robottics_Motion

Dependencies:   mbed

Committer:
smilestone520
Date:
Fri May 27 00:41:22 2016 +0000
Revision:
34:b0ae940a2fb5
Parent:
33:587e6dc249b8
hahah

Who changed what in which revision?

UserRevisionLine numberNew 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
smilestone520 33:587e6dc249b8 53 int angle = 0;
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 34:b0ae940a2fb5 61 float k_forward = 0.05, k_turn = 1;
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 33:587e6dc249b8 68 int xGate = 306, yGate = 124; //左們136 328, 右們874,316
roger5641 17:d96afd9d2692 69 double angleGate, distanceGate;
smilestone520 33:587e6dc249b8 70 int aI_State = 2;
smilestone520 21:6e8ab9487985 71 int bor_state = 1;
roger5641 17:d96afd9d2692 72 double pi = 3.1415926;
smilestone520 21:6e8ab9487985 73 double borAngle=0;
smilestone520 11:44989c0bcea5 74
smilestone520 25:7053736ea6cc 75 float ballSize; // ball size
smilestone520 25:7053736ea6cc 76
smilestone520 33:587e6dc249b8 77 float longC = 32; // car's length //65
smilestone520 33:587e6dc249b8 78 int yB1 = 70;
smilestone520 33:587e6dc249b8 79 int yB2 = 232;
smilestone520 33:587e6dc249b8 80 int xB3 = 27;
smilestone520 33:587e6dc249b8 81 int xB4 = 305; // broder conditions
smilestone520 29:47519f2b14ad 82 //65 , 580, 170,860
smilestone520 27:1696ef46b538 83 int ticCheck = 10;
smilestone520 20:5b892e37a958 84
smilestone520 10:06cc2b1d2aaf 85 int main() {
smilestone520 10:06cc2b1d2aaf 86
smilestone520 10:06cc2b1d2aaf 87 init_TIMER();
smilestone520 10:06cc2b1d2aaf 88 init_PWM();
smilestone520 10:06cc2b1d2aaf 89 init_CN();
roger5641 17:d96afd9d2692 90 servo.write(0.079 + (0.084/180)*angle);
smilestone520 10:06cc2b1d2aaf 91
smilestone520 10:06cc2b1d2aaf 92 bluetooth.baud(115200); //設定鮑率
smilestone520 10:06cc2b1d2aaf 93 pc.baud(57600);
smilestone520 10:06cc2b1d2aaf 94
smilestone520 10:06cc2b1d2aaf 95
smilestone520 10:06cc2b1d2aaf 96 while(1)
smilestone520 10:06cc2b1d2aaf 97 {
smilestone520 10:06cc2b1d2aaf 98 if(pc.readable())
smilestone520 10:06cc2b1d2aaf 99 {
smilestone520 10:06cc2b1d2aaf 100 bluetooth.putc(pc.getc());
smilestone520 10:06cc2b1d2aaf 101 }
smilestone520 10:06cc2b1d2aaf 102 if(bluetooth.readable())
smilestone520 10:06cc2b1d2aaf 103 {
smilestone520 10:06cc2b1d2aaf 104 pc.putc(bluetooth.getc());
smilestone520 10:06cc2b1d2aaf 105 }
smilestone520 10:06cc2b1d2aaf 106 }
smilestone520 10:06cc2b1d2aaf 107 }
smilestone520 10:06cc2b1d2aaf 108
smilestone520 10:06cc2b1d2aaf 109 void timer1_interrupt(void)
smilestone520 10:06cc2b1d2aaf 110 {
roger5641 26:dbdbfdb4dd41 111 for(int i=0; i<32; i++)
roger5641 26:dbdbfdb4dd41 112 {
roger5641 26:dbdbfdb4dd41 113 Receive_Data[i] = bluetooth.getc();
roger5641 26:dbdbfdb4dd41 114 }
roger5641 26:dbdbfdb4dd41 115 //read data from matlab
roger5641 26:dbdbfdb4dd41 116 //distance_target
roger5641 26:dbdbfdb4dd41 117 Distance_Target = (Receive_Data[1]-0x30)*100 + (Receive_Data[2]-0x30)*10 + (Receive_Data[3]-0x30);
roger5641 26:dbdbfdb4dd41 118
roger5641 26:dbdbfdb4dd41 119 if(Receive_Data[0] == '-')Distance_Target = -1* Distance_Target;
roger5641 26:dbdbfdb4dd41 120 else Distance_Target = Distance_Target;
roger5641 26:dbdbfdb4dd41 121
roger5641 26:dbdbfdb4dd41 122 //ang_rel_target
roger5641 26:dbdbfdb4dd41 123 Angle_Target = (Receive_Data[5]-0x30)*100 + (Receive_Data[6]-0x30)*10 + (Receive_Data[7]-0x30);
roger5641 26:dbdbfdb4dd41 124
roger5641 26:dbdbfdb4dd41 125 if(Receive_Data[4] == '-')Distance_Target = -1* Distance_Target;
roger5641 26:dbdbfdb4dd41 126 else Distance_Target = Distance_Target;
roger5641 26:dbdbfdb4dd41 127
roger5641 26:dbdbfdb4dd41 128 //x_position_car_1
roger5641 26:dbdbfdb4dd41 129 X_Position_1 = (Receive_Data[9]-0x30)*100 + (Receive_Data[10]-0x30)*10 + (Receive_Data[11]-0x30);
roger5641 26:dbdbfdb4dd41 130
roger5641 26:dbdbfdb4dd41 131 if(Receive_Data[8] == '-')Distance_Target = -1* Distance_Target;
roger5641 26:dbdbfdb4dd41 132 else Distance_Target = Distance_Target;
roger5641 26:dbdbfdb4dd41 133
roger5641 26:dbdbfdb4dd41 134 //y_position_car_1
smilestone520 32:fb8f40fde82c 135 Y_Position_1 = (Receive_Data[13]-0x30)*100 + (Receive_Data[14]-0x30)*10 + (Receive_Data[15]-0x30);
roger5641 26:dbdbfdb4dd41 136
roger5641 26:dbdbfdb4dd41 137 if(Receive_Data[12] == '-')Distance_Target = -1* Distance_Target;
roger5641 26:dbdbfdb4dd41 138 else Distance_Target = Distance_Target;
roger5641 26:dbdbfdb4dd41 139
roger5641 26:dbdbfdb4dd41 140 //angle_car_1
roger5641 26:dbdbfdb4dd41 141 Angle_1 = (Receive_Data[17]-0x30)*100 + (Receive_Data[18]-0x30)*10 + (Receive_Data[19]-0x30);
roger5641 26:dbdbfdb4dd41 142
roger5641 26:dbdbfdb4dd41 143 if(Receive_Data[16] == '-')Distance_Target = -1* Distance_Target;
roger5641 26:dbdbfdb4dd41 144 else Distance_Target = Distance_Target;
roger5641 26:dbdbfdb4dd41 145
roger5641 26:dbdbfdb4dd41 146 //x_position_car_2
roger5641 26:dbdbfdb4dd41 147 X_Position_2 = (Receive_Data[21]-0x30)*100 + (Receive_Data[22]-0x30)*10 + (Receive_Data[23]-0x30);
roger5641 26:dbdbfdb4dd41 148
roger5641 26:dbdbfdb4dd41 149 if(Receive_Data[20] == '-')Distance_Target = -1* Distance_Target;
roger5641 26:dbdbfdb4dd41 150 else Distance_Target = Distance_Target;
roger5641 26:dbdbfdb4dd41 151
roger5641 26:dbdbfdb4dd41 152 //y_position_car_2
roger5641 26:dbdbfdb4dd41 153 Y_Position_2 = (Receive_Data[25]-0x30)*100 + (Receive_Data[26]-0x30)*10 + (Receive_Data[27]-0x30);
roger5641 26:dbdbfdb4dd41 154
roger5641 26:dbdbfdb4dd41 155 if(Receive_Data[24] == '-')Distance_Target = -1* Distance_Target;
roger5641 26:dbdbfdb4dd41 156 else Distance_Target = Distance_Target;
roger5641 26:dbdbfdb4dd41 157
roger5641 26:dbdbfdb4dd41 158 //angle_car_1
roger5641 26:dbdbfdb4dd41 159 Angle_2 = (Receive_Data[29]-0x30)*100 + (Receive_Data[30]-0x30)*10 + (Receive_Data[31]-0x30);
roger5641 26:dbdbfdb4dd41 160
roger5641 26:dbdbfdb4dd41 161 if(Receive_Data[28] == '-')Distance_Target = -1* Distance_Target;
roger5641 26:dbdbfdb4dd41 162 else Distance_Target = Distance_Target;
roger5641 26:dbdbfdb4dd41 163
roger5641 26:dbdbfdb4dd41 164
roger5641 26:dbdbfdb4dd41 165
smilestone520 10:06cc2b1d2aaf 166 //Motor 1
smilestone520 10:06cc2b1d2aaf 167 v1 = (float)v1Count * 100.0f / 12.0f * 60.0f / 29.0f; //unit: rpm
smilestone520 10:06cc2b1d2aaf 168 v1Count = 0;
smilestone520 10:06cc2b1d2aaf 169
smilestone520 10:06cc2b1d2aaf 170 ///code for PI control///
smilestone520 10:06cc2b1d2aaf 171 v1_err = v1_ref - v1;
smilestone520 10:06cc2b1d2aaf 172 v1_ierr = 0.01f*v1_err + v1_ierr;
smilestone520 34:b0ae940a2fb5 173 //PIout_1 = Kp*v1_err + Ki*v1_ierr;
smilestone520 34:b0ae940a2fb5 174 PIout_1 = v1_ref;
smilestone520 10:06cc2b1d2aaf 175 if(PIout_1 >= 0.5f)PIout_1 = 0.5f;
smilestone520 10:06cc2b1d2aaf 176 else if(PIout_1 <= -0.5f)PIout_1 = -0.5f;
smilestone520 10:06cc2b1d2aaf 177 pwm1.write(PIout_1 + 0.5f);
roger5641 17:d96afd9d2692 178 TIM1->CCER |= 0x4;
smilestone520 10:06cc2b1d2aaf 179
smilestone520 10:06cc2b1d2aaf 180 //Motor 2
smilestone520 10:06cc2b1d2aaf 181 v2 = (float)v2Count * 100.0f / 12.0f * 60.0f / 29.0f; //unit: rpm
smilestone520 10:06cc2b1d2aaf 182 v2Count = 0;
smilestone520 10:06cc2b1d2aaf 183
smilestone520 10:06cc2b1d2aaf 184 ///code for PI control///
smilestone520 10:06cc2b1d2aaf 185 v2_err = v2_ref - v2;
smilestone520 10:06cc2b1d2aaf 186 v2_ierr = 0.01f*v2_err + v2_ierr;
smilestone520 34:b0ae940a2fb5 187 //PIout_2 = Kp*v2_err + Ki*v2_ierr;
smilestone520 34:b0ae940a2fb5 188 PIout_2 = v2_ref;
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 33:587e6dc249b8 242 v1_ref = 50;
smilestone520 33:587e6dc249b8 243 v2_ref = -50;
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 33:587e6dc249b8 256 v1_ref = -50;
smilestone520 33:587e6dc249b8 257 v2_ref = 50;
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 33:587e6dc249b8 270 v1_ref = -50;
smilestone520 33:587e6dc249b8 271 v2_ref = -50;
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 33:587e6dc249b8 283 v1_ref = 50;
smilestone520 33:587e6dc249b8 284 v2_ref = 50;
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
smilestone520 34:b0ae940a2fb5 299
smilestone520 34:b0ae940a2fb5 300 if((40<=distance <= 50) && (-5<=angleR<=5))
smilestone520 33:587e6dc249b8 301 {
smilestone520 33:587e6dc249b8 302 aI_State = 3;
smilestone520 33:587e6dc249b8 303 }
smilestone520 33:587e6dc249b8 304 else
smilestone520 33:587e6dc249b8 305 {
smilestone520 34:b0ae940a2fb5 306 v1_ref = k_forward*(distance - 40) + k_turn*angleR;
smilestone520 34:b0ae940a2fb5 307 v2_ref = -k_forward*(distance - 40) + k_turn*angleR;
smilestone520 33:587e6dc249b8 308 }
roger5641 17:d96afd9d2692 309 break;
roger5641 17:d96afd9d2692 310 case 3: //****getBall();
smilestone520 34:b0ae940a2fb5 311 if((40<=distance <= 50) && (-5<=angleR<=5))
roger5641 17:d96afd9d2692 312 {
roger5641 17:d96afd9d2692 313 angle = -10;
roger5641 17:d96afd9d2692 314 servo_duty = 0.079 + (0.084/180)*angle;
roger5641 17:d96afd9d2692 315 servo.write(servo_duty);
roger5641 17:d96afd9d2692 316 servo = 1;
roger5641 17:d96afd9d2692 317 wait(0.1);
roger5641 17:d96afd9d2692 318 servo = 0;
roger5641 17:d96afd9d2692 319 }
roger5641 17:d96afd9d2692 320 else
roger5641 17:d96afd9d2692 321 aI_State = 2;
roger5641 17:d96afd9d2692 322
smilestone520 11:44989c0bcea5 323 break;
smilestone520 12:c1a667ca6c53 324 case 4: /// move to the gate and release ball
smilestone520 12:c1a667ca6c53 325 // move to the point in front of the gate first
smilestone520 12:c1a667ca6c53 326 // then head to the gate
roger5641 18:2db6c97a4145 327 distanceGate = sqrt(((xGate-xP)*(xGate-xP))+((yGate-yP)*(yGate-yP)));
roger5641 17:d96afd9d2692 328 angleGate = atan2((yGate-yP),(xGate-xP))*180/pi;
roger5641 17:d96afd9d2692 329 if(-3<=angleGate<=3) // direct to the gate
roger5641 17:d96afd9d2692 330 {
smilestone520 33:587e6dc249b8 331 if(distanceGate<=80) // check distance between car cemtroid and gate
roger5641 17:d96afd9d2692 332 {
roger5641 17:d96afd9d2692 333 v1_ref = 0;
roger5641 17:d96afd9d2692 334 v2_ref = 0;
roger5641 17:d96afd9d2692 335 aI_State = 5;
roger5641 17:d96afd9d2692 336 }
smilestone520 33:587e6dc249b8 337 else if(80<distanceGate<150)
roger5641 17:d96afd9d2692 338 {
smilestone520 33:587e6dc249b8 339 v1_ref = 50;
smilestone520 33:587e6dc249b8 340 v2_ref = -50;
roger5641 17:d96afd9d2692 341 }
roger5641 17:d96afd9d2692 342 else
roger5641 17:d96afd9d2692 343 {
smilestone520 33:587e6dc249b8 344 v1_ref = 50;
smilestone520 33:587e6dc249b8 345 v2_ref = -50;
roger5641 17:d96afd9d2692 346 }
roger5641 17:d96afd9d2692 347 }
roger5641 17:d96afd9d2692 348 else if(3<angleGate<=15) // small angle right
roger5641 17:d96afd9d2692 349 {
roger5641 17:d96afd9d2692 350 if(distanceGate<50)
roger5641 17:d96afd9d2692 351 {
smilestone520 33:587e6dc249b8 352 v1_ref = 50;
smilestone520 20:5b892e37a958 353 v2_ref = -(v1_ref-angleGate*k_turn);
roger5641 17:d96afd9d2692 354 }
roger5641 17:d96afd9d2692 355 else
roger5641 17:d96afd9d2692 356 {
smilestone520 33:587e6dc249b8 357 v1_ref = 50;
smilestone520 20:5b892e37a958 358 v2_ref = -(v1_ref-angleGate*k_turn);
roger5641 17:d96afd9d2692 359 }
roger5641 17:d96afd9d2692 360 }
roger5641 17:d96afd9d2692 361 else if(-15<=angleGate<-3) // small angle left
roger5641 17:d96afd9d2692 362 {
smilestone520 33:587e6dc249b8 363 if(distanceGate<150)
roger5641 17:d96afd9d2692 364 {
smilestone520 20:5b892e37a958 365 v1_ref = -(v2_ref+angleGate*k_turn);
smilestone520 33:587e6dc249b8 366 v2_ref = 50;
roger5641 17:d96afd9d2692 367 }
roger5641 17:d96afd9d2692 368 else
roger5641 17:d96afd9d2692 369 {
smilestone520 20:5b892e37a958 370 v1_ref = -(v2_ref+angleGate*k_turn);
roger5641 17:d96afd9d2692 371 v2_ref = 300;
roger5641 17:d96afd9d2692 372 }
roger5641 17:d96afd9d2692 373 }
roger5641 17:d96afd9d2692 374 else if(15<angleGate<=180) // big angle right
roger5641 17:d96afd9d2692 375 {
smilestone520 33:587e6dc249b8 376 v1_ref = 50;
smilestone520 33:587e6dc249b8 377 v2_ref = 50;
smilestone520 33:587e6dc249b8 378 wait(0.005); //need to count the relation between angle and time in v = 200
smilestone520 33:587e6dc249b8 379 if(0<=distanceGate<150)
roger5641 17:d96afd9d2692 380 {
smilestone520 33:587e6dc249b8 381 v1_ref = 50;
smilestone520 33:587e6dc249b8 382 v2_ref = -50;
roger5641 17:d96afd9d2692 383 }
roger5641 17:d96afd9d2692 384 else
roger5641 17:d96afd9d2692 385 {
smilestone520 33:587e6dc249b8 386 v1_ref = 50;
smilestone520 33:587e6dc249b8 387 v2_ref = -50;
roger5641 17:d96afd9d2692 388 }
roger5641 17:d96afd9d2692 389 }
roger5641 17:d96afd9d2692 390 else if(-180<=angleGate<-15) // big angle left
roger5641 17:d96afd9d2692 391 {
smilestone520 33:587e6dc249b8 392 v1_ref = -50;
smilestone520 33:587e6dc249b8 393 v2_ref = -50;
smilestone520 33:587e6dc249b8 394 wait(0.005); //need to count the relation between angle and time in v = 200
smilestone520 33:587e6dc249b8 395 if(0<distanceGate<150)
roger5641 17:d96afd9d2692 396 {
smilestone520 33:587e6dc249b8 397 v1_ref = 50;
smilestone520 33:587e6dc249b8 398 v2_ref = -50;
roger5641 17:d96afd9d2692 399 }
roger5641 17:d96afd9d2692 400 else
roger5641 17:d96afd9d2692 401 {
smilestone520 33:587e6dc249b8 402 v1_ref = 50;
smilestone520 33:587e6dc249b8 403 v2_ref = -50;
roger5641 17:d96afd9d2692 404 }
roger5641 17:d96afd9d2692 405 }
roger5641 17:d96afd9d2692 406
smilestone520 12:c1a667ca6c53 407 break;
roger5641 17:d96afd9d2692 408 case 5: // release ball
roger5641 17:d96afd9d2692 409 if((-3<=angleGate<=3) && (distanceGate<=10)) // direct to the gate
roger5641 17:d96afd9d2692 410 {
roger5641 17:d96afd9d2692 411 angle = 90; // Fixture up
roger5641 17:d96afd9d2692 412 servo_duty = 0.079 + (0.084/180)*angle;
roger5641 17:d96afd9d2692 413 servo.write(servo_duty);
roger5641 17:d96afd9d2692 414 servo = 1;
smilestone520 33:587e6dc249b8 415 //wait(0.1);
roger5641 17:d96afd9d2692 416 servo = 0;
roger5641 17:d96afd9d2692 417
roger5641 17:d96afd9d2692 418 pwm1.write(0.1f + 0.5f); // push
roger5641 17:d96afd9d2692 419 pwm2.write(-0.1f + 0.5f);
smilestone520 33:587e6dc249b8 420 //wait(0.5);
roger5641 17:d96afd9d2692 421
roger5641 17:d96afd9d2692 422 angle = -10; // Fixture down
roger5641 17:d96afd9d2692 423 servo_duty = 0.079 + (0.084/180)*angle;
roger5641 17:d96afd9d2692 424 servo.write(servo_duty);
roger5641 17:d96afd9d2692 425 servo = 1;
smilestone520 33:587e6dc249b8 426 //wait(0.1);
roger5641 17:d96afd9d2692 427 servo = 0;
roger5641 17:d96afd9d2692 428
roger5641 17:d96afd9d2692 429 pwm1.write(-0.3f + 0.5f); // back and leave gate
roger5641 17:d96afd9d2692 430 pwm2.write(0.3f + 0.5f);
smilestone520 33:587e6dc249b8 431 //wait(1);
roger5641 17:d96afd9d2692 432
roger5641 18:2db6c97a4145 433 aI_State = 0;
roger5641 17:d96afd9d2692 434
roger5641 17:d96afd9d2692 435 }
roger5641 17:d96afd9d2692 436
roger5641 17:d96afd9d2692 437 break;
roger5641 26:dbdbfdb4dd41 438
smilestone520 11:44989c0bcea5 439 }
roger5641 26:dbdbfdb4dd41 440
smilestone520 10:06cc2b1d2aaf 441 }
smilestone520 10:06cc2b1d2aaf 442
smilestone520 10:06cc2b1d2aaf 443 void CN_interrupt(void)
smilestone520 10:06cc2b1d2aaf 444 {
smilestone520 34:b0ae940a2fb5 445 //Motor 1
smilestone520 10:06cc2b1d2aaf 446 stateA_1 = HallA_1.read();
smilestone520 10:06cc2b1d2aaf 447 stateB_1 = HallB_1.read();
smilestone520 10:06cc2b1d2aaf 448
smilestone520 10:06cc2b1d2aaf 449 ///code for state determination///
smilestone520 10:06cc2b1d2aaf 450 if(stateA_1==0&&stateB_1==0){
smilestone520 10:06cc2b1d2aaf 451 state_1 = 1;}
smilestone520 10:06cc2b1d2aaf 452 else if(stateA_1==0&&stateB_1==1){
smilestone520 10:06cc2b1d2aaf 453 state_1 = 2;}
smilestone520 10:06cc2b1d2aaf 454 else if(stateA_1==1&&stateB_1==1){
smilestone520 10:06cc2b1d2aaf 455 state_1 = 3;}
smilestone520 10:06cc2b1d2aaf 456 else if(stateA_1==1&&stateB_1==0){
smilestone520 10:06cc2b1d2aaf 457 state_1 = 4;}
smilestone520 10:06cc2b1d2aaf 458
smilestone520 10:06cc2b1d2aaf 459 if(state_1 == 1)
smilestone520 10:06cc2b1d2aaf 460 {
smilestone520 10:06cc2b1d2aaf 461 if(state_1-state_1_old == -3)
smilestone520 10:06cc2b1d2aaf 462 v1Count=v1Count+1;
smilestone520 10:06cc2b1d2aaf 463 else if(state_1-state_1_old == -1)
smilestone520 10:06cc2b1d2aaf 464 v1Count=v1Count-1;
smilestone520 10:06cc2b1d2aaf 465 }
smilestone520 10:06cc2b1d2aaf 466 else if(state_1 == 2)
smilestone520 10:06cc2b1d2aaf 467 {
smilestone520 10:06cc2b1d2aaf 468 if(state_1-state_1_old == 1)
smilestone520 10:06cc2b1d2aaf 469 v1Count=v1Count+1;
smilestone520 10:06cc2b1d2aaf 470 else if(state_1-state_1_old == -1)
smilestone520 10:06cc2b1d2aaf 471 v1Count=v1Count-1;
smilestone520 10:06cc2b1d2aaf 472 }
smilestone520 10:06cc2b1d2aaf 473 else if(state_1 == 3)
smilestone520 10:06cc2b1d2aaf 474 {
smilestone520 10:06cc2b1d2aaf 475 if(state_1-state_1_old == 1)
smilestone520 10:06cc2b1d2aaf 476 v1Count=v1Count+1;
smilestone520 10:06cc2b1d2aaf 477 else if(state_1-state_1_old == -1)
smilestone520 10:06cc2b1d2aaf 478 v1Count=v1Count-1;
smilestone520 10:06cc2b1d2aaf 479 }
smilestone520 10:06cc2b1d2aaf 480 else if(state_1 == 4)
smilestone520 10:06cc2b1d2aaf 481 {
smilestone520 10:06cc2b1d2aaf 482 if(state_1-state_1_old == 1)
smilestone520 10:06cc2b1d2aaf 483 v1Count=v1Count+1;
smilestone520 10:06cc2b1d2aaf 484 else if(state_1-state_1_old == 3)
smilestone520 10:06cc2b1d2aaf 485 v1Count=v1Count-1;
smilestone520 10:06cc2b1d2aaf 486 }
smilestone520 10:06cc2b1d2aaf 487 state_1_old = state_1;
smilestone520 10:06cc2b1d2aaf 488
smilestone520 10:06cc2b1d2aaf 489
smilestone520 10:06cc2b1d2aaf 490 //////////////////////////////////
smilestone520 10:06cc2b1d2aaf 491
smilestone520 10:06cc2b1d2aaf 492 //Forward
smilestone520 10:06cc2b1d2aaf 493 //v1Count +1
smilestone520 10:06cc2b1d2aaf 494 //Inverse
smilestone520 10:06cc2b1d2aaf 495 //v1Count -1
smilestone520 10:06cc2b1d2aaf 496
smilestone520 10:06cc2b1d2aaf 497
smilestone520 10:06cc2b1d2aaf 498 //Motor 2
smilestone520 10:06cc2b1d2aaf 499 stateA_2 = HallA_2.read();
smilestone520 10:06cc2b1d2aaf 500 stateB_2 = HallB_2.read();
smilestone520 10:06cc2b1d2aaf 501
smilestone520 10:06cc2b1d2aaf 502 ///code for state determination///
smilestone520 10:06cc2b1d2aaf 503 if(stateA_2==0&&stateB_2==0){
smilestone520 10:06cc2b1d2aaf 504 state_2 = 1;}
smilestone520 10:06cc2b1d2aaf 505 else if(stateA_2==0&&stateB_2==1){
smilestone520 10:06cc2b1d2aaf 506 state_2 = 2;}
smilestone520 10:06cc2b1d2aaf 507 else if(stateA_2==1&&stateB_2==1){
smilestone520 10:06cc2b1d2aaf 508 state_2 = 3;}
smilestone520 10:06cc2b1d2aaf 509 else if(stateA_2==1&&stateB_2==0){
smilestone520 10:06cc2b1d2aaf 510 state_2 = 4;}
smilestone520 10:06cc2b1d2aaf 511
smilestone520 10:06cc2b1d2aaf 512 if(state_2 == 1)
smilestone520 10:06cc2b1d2aaf 513 {
smilestone520 10:06cc2b1d2aaf 514 if(state_2-state_2_old == -3)
smilestone520 10:06cc2b1d2aaf 515 v2Count=v2Count+1;
smilestone520 10:06cc2b1d2aaf 516 else if(state_2-state_2_old == -1)
smilestone520 10:06cc2b1d2aaf 517 v2Count=v2Count-1;
smilestone520 10:06cc2b1d2aaf 518 }
smilestone520 10:06cc2b1d2aaf 519 else if(state_2 == 2)
smilestone520 10:06cc2b1d2aaf 520 {
smilestone520 10:06cc2b1d2aaf 521 if(state_2-state_2_old == 1)
smilestone520 10:06cc2b1d2aaf 522 v2Count=v2Count+1;
smilestone520 10:06cc2b1d2aaf 523 else if(state_2-state_2_old == -1)
smilestone520 10:06cc2b1d2aaf 524 v2Count=v2Count-1;
smilestone520 10:06cc2b1d2aaf 525 }
smilestone520 10:06cc2b1d2aaf 526 else if(state_2 == 3)
smilestone520 10:06cc2b1d2aaf 527 {
smilestone520 10:06cc2b1d2aaf 528 if(state_2-state_2_old == 1)
smilestone520 10:06cc2b1d2aaf 529 v2Count=v2Count+1;
smilestone520 10:06cc2b1d2aaf 530 else if(state_2-state_2_old == -1)
smilestone520 10:06cc2b1d2aaf 531 v2Count=v2Count-1;
smilestone520 10:06cc2b1d2aaf 532 }
smilestone520 10:06cc2b1d2aaf 533 else if(state_2 == 4)
smilestone520 10:06cc2b1d2aaf 534 {
smilestone520 10:06cc2b1d2aaf 535 if(state_2-state_2_old == 1)
smilestone520 10:06cc2b1d2aaf 536 v2Count=v2Count+1;
smilestone520 10:06cc2b1d2aaf 537 else if(state_2-state_2_old == 3)
smilestone520 10:06cc2b1d2aaf 538 v2Count=v2Count-1;
smilestone520 10:06cc2b1d2aaf 539 }
smilestone520 34:b0ae940a2fb5 540 state_2_old = state_2;
smilestone520 10:06cc2b1d2aaf 541
smilestone520 10:06cc2b1d2aaf 542 //////////////////////////////////
smilestone520 10:06cc2b1d2aaf 543
smilestone520 10:06cc2b1d2aaf 544 //Forward
smilestone520 10:06cc2b1d2aaf 545 //v2Count +1
smilestone520 10:06cc2b1d2aaf 546 //Inverse
smilestone520 10:06cc2b1d2aaf 547 //v2Count -1
smilestone520 33:587e6dc249b8 548
smilestone520 33:587e6dc249b8 549
smilestone520 10:06cc2b1d2aaf 550 }
smilestone520 10:06cc2b1d2aaf 551
smilestone520 10:06cc2b1d2aaf 552 void init_TIMER(void)
smilestone520 10:06cc2b1d2aaf 553 {
smilestone520 10:06cc2b1d2aaf 554 timer1.attach_us(&timer1_interrupt, 10000);//10ms interrupt period (100 Hz)
smilestone520 10:06cc2b1d2aaf 555 }
smilestone520 10:06cc2b1d2aaf 556
smilestone520 10:06cc2b1d2aaf 557 void init_PWM(void)
smilestone520 10:06cc2b1d2aaf 558 {
smilestone520 10:06cc2b1d2aaf 559 pwm1.period_us(50);
smilestone520 10:06cc2b1d2aaf 560 pwm1.write(0.5);
smilestone520 10:06cc2b1d2aaf 561 TIM1->CCER |= 0x4;
smilestone520 10:06cc2b1d2aaf 562
smilestone520 10:06cc2b1d2aaf 563 pwm2.period_us(50);
smilestone520 10:06cc2b1d2aaf 564 pwm2.write(0.5);
smilestone520 10:06cc2b1d2aaf 565 TIM1->CCER |= 0x40;
smilestone520 10:06cc2b1d2aaf 566 }
smilestone520 10:06cc2b1d2aaf 567
smilestone520 10:06cc2b1d2aaf 568 void init_CN(void)
smilestone520 10:06cc2b1d2aaf 569 {
smilestone520 10:06cc2b1d2aaf 570 HallA_1.rise(&CN_interrupt);
smilestone520 10:06cc2b1d2aaf 571 HallA_1.fall(&CN_interrupt);
smilestone520 10:06cc2b1d2aaf 572 HallB_1.rise(&CN_interrupt);
smilestone520 10:06cc2b1d2aaf 573 HallB_1.fall(&CN_interrupt);
smilestone520 10:06cc2b1d2aaf 574
smilestone520 10:06cc2b1d2aaf 575 HallA_2.rise(&CN_interrupt);
smilestone520 10:06cc2b1d2aaf 576 HallA_2.fall(&CN_interrupt);
smilestone520 10:06cc2b1d2aaf 577 HallB_2.rise(&CN_interrupt);
smilestone520 10:06cc2b1d2aaf 578 HallB_2.fall(&CN_interrupt);
smilestone520 10:06cc2b1d2aaf 579
smilestone520 10:06cc2b1d2aaf 580 stateA_1 = HallA_1.read();
smilestone520 10:06cc2b1d2aaf 581 stateB_1 = HallB_1.read();
smilestone520 10:06cc2b1d2aaf 582 stateA_2 = HallA_2.read();
smilestone520 10:06cc2b1d2aaf 583 stateB_2 = HallB_2.read();
smilestone520 20:5b892e37a958 584 }
smilestone520 20:5b892e37a958 585
smilestone520 20:5b892e37a958 586 //**************** funcBorder() *****************************************
smilestone520 20:5b892e37a958 587
smilestone520 20:5b892e37a958 588 void funcBorder(void) // check if car is too close to the border // miss the ball restriction part
smilestone520 20:5b892e37a958 589 {
smilestone520 20:5b892e37a958 590 if(yC > yB1 || yC < yB2 || xC < xB3 || xC > xB4){
smilestone520 20:5b892e37a958 591
smilestone520 20:5b892e37a958 592 if(yC > yB1) // 靠近上邊界 *****************************
smilestone520 20:5b892e37a958 593 {
smilestone520 20:5b892e37a958 594 if(xC < xB3) //左上角
smilestone520 20:5b892e37a958 595 {
smilestone520 20:5b892e37a958 596 if(angleC <0 && angleC >=-90) // I
smilestone520 23:948ce071cbb6 597 {
smilestone520 23:948ce071cbb6 598 bor_state = 4; //clockwise, to angleC = 45
smilestone520 23:948ce071cbb6 599 borAngle = 45;
smilestone520 23:948ce071cbb6 600 }
smilestone520 20:5b892e37a958 601
smilestone520 20:5b892e37a958 602 else if(angleC > -180 && angleC <-90) // II
smilestone520 23:948ce071cbb6 603 bor_state = 3; //back 0.5*longC, judge again
smilestone520 20:5b892e37a958 604
smilestone520 20:5b892e37a958 605 else if(angleC <=180 && angleC >=90) // III
smilestone520 23:948ce071cbb6 606 {
smilestone520 23:948ce071cbb6 607 bor_state = 5;//counter clockwise, to angleC = 45
smilestone520 23:948ce071cbb6 608 borAngle = 45;
smilestone520 23:948ce071cbb6 609 }
smilestone520 20:5b892e37a958 610
smilestone520 20:5b892e37a958 611 else if(angleC < 90 && angleC >=0) // IV
smilestone520 23:948ce071cbb6 612 bor_state = 2;//forward 0.5*longC, judge again
smilestone520 20:5b892e37a958 613
smilestone520 20:5b892e37a958 614 }
smilestone520 20:5b892e37a958 615 else if(xC > xB4) //右上角
smilestone520 20:5b892e37a958 616 {
smilestone520 20:5b892e37a958 617 if(angleC <0 && angleC >=-90) // I
smilestone520 23:948ce071cbb6 618 bor_state = 3;//back 0.5*longC, judge again
smilestone520 20:5b892e37a958 619
smilestone520 20:5b892e37a958 620 else if(angleC > -180 && angleC <-90) // II
smilestone520 23:948ce071cbb6 621 {
smilestone520 23:948ce071cbb6 622 bor_state = 5;//counter clockwise, to angleC = 135
smilestone520 23:948ce071cbb6 623 borAngle = 135;
smilestone520 23:948ce071cbb6 624 }
smilestone520 20:5b892e37a958 625
smilestone520 20:5b892e37a958 626 else if(angleC <=180 && angleC >=90) // III
smilestone520 23:948ce071cbb6 627 bor_state = 2;//forward 0.5*longC, judge again
smilestone520 20:5b892e37a958 628
smilestone520 20:5b892e37a958 629 else if(angleC < 90 && angleC >=0) // IV
smilestone520 23:948ce071cbb6 630 {
smilestone520 23:948ce071cbb6 631 bor_state = 4;//clockwise, to angleC = 135
smilestone520 23:948ce071cbb6 632 borAngle = 135;
smilestone520 23:948ce071cbb6 633 }
smilestone520 23:948ce071cbb6 634
smilestone520 20:5b892e37a958 635 }
smilestone520 20:5b892e37a958 636 else //上邊界
smilestone520 20:5b892e37a958 637 {
smilestone520 20:5b892e37a958 638 if(angleC == -90 || angleC == 90)
smilestone520 24:f61847968e72 639 aI_State = 2; // do nothing, turn to next point
smilestone520 20:5b892e37a958 640 else
smilestone520 24:f61847968e72 641 {
smilestone520 24:f61847968e72 642 if(0 > borAngle >= -90 || 90< borAngle <=0) //CW
smilestone520 24:f61847968e72 643 {
smilestone520 24:f61847968e72 644 bor_state = 4;
smilestone520 24:f61847968e72 645 borAngle = 90;
smilestone520 24:f61847968e72 646 }
smilestone520 24:f61847968e72 647 else
smilestone520 24:f61847968e72 648 {
smilestone520 24:f61847968e72 649 bor_state = 5;
smilestone520 24:f61847968e72 650 borAngle = 90;
smilestone520 24:f61847968e72 651 }
smilestone520 24:f61847968e72 652 } // turn to 90, then turn to next point
smilestone520 20:5b892e37a958 653 }
smilestone520 20:5b892e37a958 654
smilestone520 20:5b892e37a958 655
smilestone520 20:5b892e37a958 656 }
smilestone520 20:5b892e37a958 657 else if(yC < yB2) // 靠近下邊界 *E****************************************************88
smilestone520 20:5b892e37a958 658 {
smilestone520 20:5b892e37a958 659 if(xC < xB3) //左下角
smilestone520 20:5b892e37a958 660 {
smilestone520 20:5b892e37a958 661 if(angleC <0 && angleC >=-90) // I
smilestone520 23:948ce071cbb6 662 bor_state = 2; //forward 0.5*longC, judge again
smilestone520 23:948ce071cbb6 663
smilestone520 20:5b892e37a958 664 else if(angleC > -180 && angleC <-90) // II
smilestone520 23:948ce071cbb6 665 {
smilestone520 23:948ce071cbb6 666 bor_state = 4; //clockwise, to angleC = -45
smilestone520 23:948ce071cbb6 667 borAngle = -45;
smilestone520 23:948ce071cbb6 668 }
smilestone520 20:5b892e37a958 669
smilestone520 20:5b892e37a958 670 else if(angleC <=180 && angleC >=90) // III
smilestone520 23:948ce071cbb6 671 bor_state = 3;//back 0.5*longC, judge again
smilestone520 20:5b892e37a958 672
smilestone520 20:5b892e37a958 673 else if(angleC < 90 && angleC >=0) // IV
smilestone520 23:948ce071cbb6 674 {
smilestone520 23:948ce071cbb6 675 bor_state = 5;//counter clockwise, to angleC = -45
smilestone520 23:948ce071cbb6 676 borAngle = -45;
smilestone520 23:948ce071cbb6 677 }
smilestone520 20:5b892e37a958 678
smilestone520 20:5b892e37a958 679 }
smilestone520 20:5b892e37a958 680 else if(xC > xB4) //右下角
smilestone520 20:5b892e37a958 681 {
smilestone520 20:5b892e37a958 682 if(angleC <0 && angleC >=-90) // I
smilestone520 23:948ce071cbb6 683 {
smilestone520 23:948ce071cbb6 684 bor_state = 5;//counter clockwise, to angleC = -135
smilestone520 23:948ce071cbb6 685 borAngle = -135;
smilestone520 23:948ce071cbb6 686 }
smilestone520 20:5b892e37a958 687
smilestone520 20:5b892e37a958 688 else if(angleC > -180 && angleC <-90) // II
smilestone520 23:948ce071cbb6 689 bor_state = 2;//forward 0.5*longC, judge again
smilestone520 20:5b892e37a958 690
smilestone520 20:5b892e37a958 691 else if(angleC <=180 && angleC >=90) // III
smilestone520 23:948ce071cbb6 692 {
smilestone520 23:948ce071cbb6 693 bor_state = 4;//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 < 90 && angleC >=0) // IV
smilestone520 23:948ce071cbb6 698 bor_state = 3;//back 0.5*longC, judge again
smilestone520 20:5b892e37a958 699
smilestone520 20:5b892e37a958 700 }
smilestone520 20:5b892e37a958 701 else //下邊界
smilestone520 20:5b892e37a958 702 {
smilestone520 20:5b892e37a958 703 if(angleC == -90 || angleC == 90)
smilestone520 24:f61847968e72 704 aI_State = 2; // do nothing, turn to next point
smilestone520 20:5b892e37a958 705 else
smilestone520 24:f61847968e72 706 {
smilestone520 24:f61847968e72 707 if(0 > borAngle >= -90 || 90< borAngle <=0) //CCW
smilestone520 24:f61847968e72 708 {
smilestone520 24:f61847968e72 709 bor_state = 5;
smilestone520 24:f61847968e72 710 borAngle = -90;
smilestone520 24:f61847968e72 711 }
smilestone520 24:f61847968e72 712 else
smilestone520 24:f61847968e72 713 {
smilestone520 24:f61847968e72 714 bor_state = 4;
smilestone520 24:f61847968e72 715 borAngle = -90;
smilestone520 24:f61847968e72 716 }
smilestone520 24:f61847968e72 717 } // turn to -90, then turn to next point
smilestone520 20:5b892e37a958 718 }
smilestone520 20:5b892e37a958 719
smilestone520 20:5b892e37a958 720
smilestone520 20:5b892e37a958 721 }
smilestone520 20:5b892e37a958 722 else if(xC < xB3) //靠近左邊界****************************************************************
smilestone520 20:5b892e37a958 723 {
smilestone520 20:5b892e37a958 724 if(angleC == 0 || angleC == 180)
smilestone520 23:948ce071cbb6 725 aI_State = 2; // do nothing, turn to next point
smilestone520 24:f61847968e72 726
smilestone520 24:f61847968e72 727 else if(-180 < angleC < 0) // CW
smilestone520 24:f61847968e72 728 {
smilestone520 24:f61847968e72 729 bor_state = 4;
smilestone520 24:f61847968e72 730 borAngle = 0;
smilestone520 24:f61847968e72 731 }
smilestone520 20:5b892e37a958 732 else
smilestone520 23:948ce071cbb6 733 {
smilestone520 24:f61847968e72 734 bor_state = 5;
smilestone520 24:f61847968e72 735 borAngle = 0;
smilestone520 24:f61847968e72 736 }
smilestone520 24:f61847968e72 737 // turn to 0, then turn to next point
smilestone520 20:5b892e37a958 738 }
smilestone520 20:5b892e37a958 739
smilestone520 20:5b892e37a958 740 else if(xC > xB4) //靠近右邊界 *********************************************88
smilestone520 20:5b892e37a958 741 {
smilestone520 20:5b892e37a958 742 if(angleC == 0 || angleC == 180)
smilestone520 23:948ce071cbb6 743 aI_State = 2; // do nothing, turn to next point
smilestone520 24:f61847968e72 744
smilestone520 24:f61847968e72 745 else if(-180 < angleC < 0) // CCW
smilestone520 24:f61847968e72 746 {
smilestone520 24:f61847968e72 747 bor_state = 5;
smilestone520 24:f61847968e72 748 borAngle = 180;
smilestone520 24:f61847968e72 749 } // turn to 90, then turn to next point
smilestone520 23:948ce071cbb6 750 else
smilestone520 24:f61847968e72 751 {
smilestone520 24:f61847968e72 752 bor_state = 4;
smilestone520 24:f61847968e72 753 borAngle = 180;
smilestone520 24:f61847968e72 754 }
smilestone520 23:948ce071cbb6 755 }
smilestone520 23:948ce071cbb6 756 }
smilestone520 23:948ce071cbb6 757 else
smilestone520 23:948ce071cbb6 758 {aI_State = 2;}
smilestone520 23:948ce071cbb6 759 } // funcBorder()
smilestone520 23:948ce071cbb6 760
smilestone520 20:5b892e37a958 761 //**************** smallAngle() *****************************************
smilestone520 20:5b892e37a958 762
smilestone520 20:5b892e37a958 763 void smallAngle( float goodAngle ) // use the smallest turn to right angle ccw / cw, based on 0~360 degree system
smilestone520 24:f61847968e72 764 {
smilestone520 20:5b892e37a958 765 if(angleR > 0) // CW
smilestone520 20:5b892e37a958 766 {
smilestone520 20:5b892e37a958 767 // turn clockwise to goodAngle;
smilestone520 20:5b892e37a958 768 }
smilestone520 20:5b892e37a958 769 else if(angleR < 0) //CCW
smilestone520 20:5b892e37a958 770 {
smilestone520 20:5b892e37a958 771 // turn counter clockwise to goodAngle;
smilestone520 20:5b892e37a958 772 }
smilestone520 20:5b892e37a958 773
smilestone520 24:f61847968e72 774 }
smilestone520 20:5b892e37a958 775 //*************** turnCW() ***********************************
smilestone520 20:5b892e37a958 776
smilestone520 20:5b892e37a958 777 void turnCW(float goodAngle)
smilestone520 20:5b892e37a958 778 {
smilestone520 20:5b892e37a958 779 // turn clockwise to goodAngle;
smilestone520 20:5b892e37a958 780 }
smilestone520 20:5b892e37a958 781
smilestone520 20:5b892e37a958 782 //*************** turnCCW() ***********************************
smilestone520 20:5b892e37a958 783
smilestone520 20:5b892e37a958 784 void turnCCW(float goodAngle)
smilestone520 20:5b892e37a958 785 {
smilestone520 20:5b892e37a958 786 // turn counter clockwise to goodAngle;
smilestone520 10:06cc2b1d2aaf 787 }