Robotics Term Project / Mbed 2 deprecated Robottics_Motion

Dependencies:   mbed

Committer:
roger5641
Date:
Thu May 26 10:28:19 2016 +0000
Revision:
26:dbdbfdb4dd41
Parent:
25:7053736ea6cc
Child:
27:1696ef46b538
aa

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