Clark Lin
/
BX-car_s
QQQ
Fork of BX-car_s by
main.cpp@14:303b22b76d7a, 2014-06-28 (annotated)
- Committer:
- TonyLin
- Date:
- Sat Jun 28 07:32:39 2014 +0000
- Revision:
- 14:303b22b76d7a
- Parent:
- 13:a33a7705fe2b
- Child:
- 15:57567d3ee27e
Dynamic kp!!!!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
backman | 0:68c173249c01 | 1 | #include "mbed.h" |
backman | 7:fd976e1ced33 | 2 | #include "controller.h" |
backman | 1:82bc25a7b68b | 3 | #include "servo_api.h" |
backman | 1:82bc25a7b68b | 4 | #include "camera_api.h" |
backman | 6:5a39bde2e016 | 5 | #include "motor_api.h" |
backman | 7:fd976e1ced33 | 6 | #include "pot.h" |
TonyLin | 13:a33a7705fe2b | 7 | |
backman | 3:c5f2281b3ed2 | 8 | |
backman | 3:c5f2281b3ed2 | 9 | #define Debug_cam_uart |
backman | 6:5a39bde2e016 | 10 | #define R_eye |
TonyLin | 14:303b22b76d7a | 11 | #define motor_on |
backman | 6:5a39bde2e016 | 12 | #define Pcontroller |
backman | 8:8e49e21d80a2 | 13 | #define task_ma_time |
TonyLin | 13:a33a7705fe2b | 14 | #define offset 65 |
TonyLin | 13:a33a7705fe2b | 15 | #include "TSISensor.h" |
backman | 12:418e39749f48 | 16 | |
TonyLin | 13:a33a7705fe2b | 17 | Serial pc(USBTX, USBRX); |
TonyLin | 14:303b22b76d7a | 18 | BX_servo servo; |
backman | 6:5a39bde2e016 | 19 | BX_camera cam; |
backman | 6:5a39bde2e016 | 20 | BX_motor MotorA('A'); |
backman | 6:5a39bde2e016 | 21 | BX_motor MotorB('B'); |
TonyLin | 13:a33a7705fe2b | 22 | BX_pot pot1('1'); // 90/30=3 |
TonyLin | 13:a33a7705fe2b | 23 | BX_pot pot2('2'); |
TonyLin | 13:a33a7705fe2b | 24 | PID cam_to_M_ctrlr(10.0,118.0,0.06,0.11,(0.104/30),0.0,0.0,10); |
backman | 8:8e49e21d80a2 | 25 | DigitalOut task_pin(PTD1); |
backman | 9:33b99cb45e99 | 26 | TSISensor tsi; |
backman | 12:418e39749f48 | 27 | |
TonyLin | 13:a33a7705fe2b | 28 | void run(); |
backman | 12:418e39749f48 | 29 | |
TonyLin | 14:303b22b76d7a | 30 | int main() |
TonyLin | 14:303b22b76d7a | 31 | { |
TonyLin | 13:a33a7705fe2b | 32 | pc.baud(115200); |
TonyLin | 14:303b22b76d7a | 33 | |
TonyLin | 14:303b22b76d7a | 34 | while(1) { |
TonyLin | 13:a33a7705fe2b | 35 | if(tsi.readPercentage()>0.00011) |
TonyLin | 14:303b22b76d7a | 36 | break; |
backman | 12:418e39749f48 | 37 | } |
TonyLin | 13:a33a7705fe2b | 38 | run(); |
TonyLin | 14:303b22b76d7a | 39 | |
TonyLin | 14:303b22b76d7a | 40 | pc.baud(115200); |
TonyLin | 13:a33a7705fe2b | 41 | return 0; |
TonyLin | 14:303b22b76d7a | 42 | } |
backman | 12:418e39749f48 | 43 | |
backman | 12:418e39749f48 | 44 | |
backman | 7:fd976e1ced33 | 45 | |
TonyLin | 14:303b22b76d7a | 46 | void run() |
TonyLin | 14:303b22b76d7a | 47 | { |
TonyLin | 14:303b22b76d7a | 48 | double motor; |
TonyLin | 14:303b22b76d7a | 49 | double b_r_c; |
TonyLin | 14:303b22b76d7a | 50 | double PID_v; |
TonyLin | 14:303b22b76d7a | 51 | double error , P, I, D, kp, r_kp; |
TonyLin | 14:303b22b76d7a | 52 | double last_error=0.0 ,last_I=0.0, last_brc, brc_df; |
TonyLin | 14:303b22b76d7a | 53 | bool first_time=true, r_kp_neg=false; |
TonyLin | 14:303b22b76d7a | 54 | |
TonyLin | 14:303b22b76d7a | 55 | while(1) { |
TonyLin | 14:303b22b76d7a | 56 | #ifdef task_ma_time |
TonyLin | 14:303b22b76d7a | 57 | task_pin=1; |
TonyLin | 14:303b22b76d7a | 58 | #endif |
TonyLin | 14:303b22b76d7a | 59 | |
TonyLin | 14:303b22b76d7a | 60 | cam.read(); |
TonyLin | 14:303b22b76d7a | 61 | #ifdef Debug_cam_uart |
TonyLin | 14:303b22b76d7a | 62 | #ifdef L_eye |
TonyLin | 14:303b22b76d7a | 63 | for(int i=0; i<128; i++) { |
TonyLin | 14:303b22b76d7a | 64 | if(i==64) |
TonyLin | 14:303b22b76d7a | 65 | pc.printf("X"); |
TonyLin | 14:303b22b76d7a | 66 | else |
TonyLin | 14:303b22b76d7a | 67 | pc.printf("%c", cam.sign_line_imageL[i]); |
TonyLin | 14:303b22b76d7a | 68 | } |
TonyLin | 14:303b22b76d7a | 69 | pc.printf(" || "); |
TonyLin | 14:303b22b76d7a | 70 | #endif |
TonyLin | 14:303b22b76d7a | 71 | #ifdef R_eye |
TonyLin | 14:303b22b76d7a | 72 | for(int i=128; i>=0; i--) { |
TonyLin | 14:303b22b76d7a | 73 | if(i==64) |
TonyLin | 14:303b22b76d7a | 74 | pc.printf("X"); |
TonyLin | 14:303b22b76d7a | 75 | else if(i<10) |
TonyLin | 14:303b22b76d7a | 76 | pc.printf("-"); |
TonyLin | 14:303b22b76d7a | 77 | else if(i>117) |
TonyLin | 14:303b22b76d7a | 78 | pc.printf("-"); |
TonyLin | 14:303b22b76d7a | 79 | else |
TonyLin | 14:303b22b76d7a | 80 | pc.printf("%c", cam.sign_line_imageR[i]); |
TonyLin | 14:303b22b76d7a | 81 | } |
TonyLin | 14:303b22b76d7a | 82 | pc.printf("\r\n"); |
TonyLin | 14:303b22b76d7a | 83 | #endif |
TonyLin | 14:303b22b76d7a | 84 | #endif |
TonyLin | 14:303b22b76d7a | 85 | |
TonyLin | 14:303b22b76d7a | 86 | #ifdef motor_on |
TonyLin | 14:303b22b76d7a | 87 | motor=pot1.read(); |
TonyLin | 14:303b22b76d7a | 88 | MotorA.rotate(motor); |
TonyLin | 14:303b22b76d7a | 89 | MotorB.rotate(motor); |
TonyLin | 14:303b22b76d7a | 90 | #endif |
TonyLin | 14:303b22b76d7a | 91 | |
TonyLin | 14:303b22b76d7a | 92 | b_r_c=(double)cam.black_centerR(); |
TonyLin | 14:303b22b76d7a | 93 | pc.printf("%d %d speed :%f bk_center %f PID:%f \r\n",cam.de_v,cam.de_v2,motor,b_r_c,PID_v); |
TonyLin | 14:303b22b76d7a | 94 | |
TonyLin | 14:303b22b76d7a | 95 | //compute |
TonyLin | 14:303b22b76d7a | 96 | //stand at 65 |
TonyLin | 14:303b22b76d7a | 97 | error=b_r_c-offset; |
TonyLin | 14:303b22b76d7a | 98 | brc_df=b_r_c-last_brc; |
TonyLin | 14:303b22b76d7a | 99 | last_brc=b_r_c; |
backman | 12:418e39749f48 | 100 | |
TonyLin | 14:303b22b76d7a | 101 | if(first_time==true){ |
TonyLin | 14:303b22b76d7a | 102 | r_kp=error; |
TonyLin | 14:303b22b76d7a | 103 | first_time=false; |
TonyLin | 13:a33a7705fe2b | 104 | } |
TonyLin | 13:a33a7705fe2b | 105 | else{ |
TonyLin | 14:303b22b76d7a | 106 | r_kp+=df; |
TonyLin | 14:303b22b76d7a | 107 | } |
backman | 12:418e39749f48 | 108 | |
TonyLin | 14:303b22b76d7a | 109 | if(r_kp<0.0){ |
TonyLin | 14:303b22b76d7a | 110 | r_kp*=-1.0; |
TonyLin | 14:303b22b76d7a | 111 | r_kp_neg=true; |
TonyLin | 14:303b22b76d7a | 112 | } |
TonyLin | 14:303b22b76d7a | 113 | kp=0.025/(4000/r_kp); |
TonyLin | 14:303b22b76d7a | 114 | if(r_kp_neg==true){ |
TonyLin | 14:303b22b76d7a | 115 | r_kp_neg=false; |
TonyLin | 14:303b22b76d7a | 116 | r_kp*=-1.0; |
TonyLin | 14:303b22b76d7a | 117 | } |
TonyLin | 14:303b22b76d7a | 118 | |
TonyLin | 14:303b22b76d7a | 119 | P=kp*error; |
TonyLin | 14:303b22b76d7a | 120 | |
TonyLin | 14:303b22b76d7a | 121 | I=last_I*2.0/3.0+error; |
TonyLin | 14:303b22b76d7a | 122 | last_I=I; |
TonyLin | 14:303b22b76d7a | 123 | I=(kp*0.02/0.04)*I; |
TonyLin | 14:303b22b76d7a | 124 | |
TonyLin | 14:303b22b76d7a | 125 | D=error-last_error; |
TonyLin | 14:303b22b76d7a | 126 | last_error=error; |
TonyLin | 14:303b22b76d7a | 127 | D=(kp*0.04/0.02)*D; |
TonyLin | 14:303b22b76d7a | 128 | |
TonyLin | 14:303b22b76d7a | 129 | PID_v=P+D; |
TonyLin | 14:303b22b76d7a | 130 | |
TonyLin | 14:303b22b76d7a | 131 | |
TonyLin | 14:303b22b76d7a | 132 | if(PID_v < 0.0) { |
TonyLin | 13:a33a7705fe2b | 133 | PID_v*=-1; |
TonyLin | 14:303b22b76d7a | 134 | PID_v=0.085-(PID_v); |
TonyLin | 14:303b22b76d7a | 135 | } else if(PID_v > 0.0) |
TonyLin | 14:303b22b76d7a | 136 | PID_v=0.085+(PID_v); |
TonyLin | 13:a33a7705fe2b | 137 | else |
TonyLin | 13:a33a7705fe2b | 138 | PID_v=0.085; |
TonyLin | 14:303b22b76d7a | 139 | |
TonyLin | 13:a33a7705fe2b | 140 | servo.set_angle(PID_v); |
TonyLin | 14:303b22b76d7a | 141 | |
TonyLin | 14:303b22b76d7a | 142 | #ifdef task_ma_time |
TonyLin | 14:303b22b76d7a | 143 | task_pin=0; |
TonyLin | 14:303b22b76d7a | 144 | #endif |
TonyLin | 14:303b22b76d7a | 145 | } |
TonyLin | 13:a33a7705fe2b | 146 | } |