2015_robocon_bteam / Mbed 2 deprecated 2015robot_main

Dependencies:   PID QEI mbed

Fork of 2015robot_main by Naoto Deguchi

Committer:
DeguNaoto
Date:
Sun Oct 18 00:17:31 2015 +0000
Revision:
103:ffd3ca4a7a71
Parent:
101:b67d33e56b66
Child:
105:0be5d5d64782
20151018 ???????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DeguNaoto 21:79b94cb922f0 1 #ifndef AUTOMODE_H
DeguNaoto 21:79b94cb922f0 2 #define AUTOMODE_H
DeguNaoto 21:79b94cb922f0 3
DeguNaoto 21:79b94cb922f0 4 /***PID Controller***/
DeguNaoto 66:14df82661dfa 5 //PID velocity_controller(36.0,5274.0 ,0.0,RATE);
DeguNaoto 66:14df82661dfa 6 //PID direction_controller(36.0,3.0,0.0,RATE);
DeguNaoto 83:e1638c58e1f1 7 PID velocity_controller(9.0,5274.0,0.0,RATE);
DeguNaoto 34:f9ef622f4376 8 PID direction_controller(36.0,3.0,0.0,RATE);
DeguNaoto 21:79b94cb922f0 9
DeguNaoto 96:a972ba5cf2d7 10 Timeout OpStart;
DeguNaoto 96:a972ba5cf2d7 11
DeguNaoto 96:a972ba5cf2d7 12 void OpponentsStart(){
DeguNaoto 101:b67d33e56b66 13 spcount = 0.0;
DeguNaoto 101:b67d33e56b66 14 step = 15;
DeguNaoto 101:b67d33e56b66 15 CStep = 15;
DeguNaoto 101:b67d33e56b66 16 flaga = 1;
DeguNaoto 101:b67d33e56b66 17 flagf = 1;
DeguNaoto 96:a972ba5cf2d7 18 }
DeguNaoto 96:a972ba5cf2d7 19
DeguNaoto 57:3fbd487e055e 20 #ifdef IM920
DeguNaoto 52:d9e1629da852 21 /***IM920 correspondence***/
DeguNaoto 52:d9e1629da852 22 void autoIM920() {
DeguNaoto 52:d9e1629da852 23 if(b==7){ /*mode change*/
unicore32 80:7e24b99b9b61 24 if(edge7) {
DeguNaoto 52:d9e1629da852 25 edge7=0;
DeguNaoto 52:d9e1629da852 26 autoflag=0;
DeguNaoto 52:d9e1629da852 27 Indicator4=0;
DeguNaoto 52:d9e1629da852 28 IndicatorAuto=1;
DeguNaoto 86:5f0b065cb4d3 29 flaga=0;
DeguNaoto 101:b67d33e56b66 30 Move_l(0.0);
DeguNaoto 101:b67d33e56b66 31 Move_r(0.0);
DeguNaoto 52:d9e1629da852 32 }
DeguNaoto 52:d9e1629da852 33 }
DeguNaoto 89:048a72382b0a 34 else if((b==6)&&(!flaga)){ /*start*/
DeguNaoto 83:e1638c58e1f1 35 if(edge6){
DeguNaoto 83:e1638c58e1f1 36 edge6=0;
DeguNaoto 83:e1638c58e1f1 37 resetState();
DeguNaoto 83:e1638c58e1f1 38 flagf=1;
DeguNaoto 89:048a72382b0a 39 spcount=0.0;
DeguNaoto 89:048a72382b0a 40 // targ_velocity=speed;
DeguNaoto 85:dd18a2d79956 41 // sendData(7,70);
DeguNaoto 103:ffd3ca4a7a71 42 sendData(5,69); //right
DeguNaoto 89:048a72382b0a 43 wait(0.05);
DeguNaoto 103:ffd3ca4a7a71 44 sendData(4,69); //left
DeguNaoto 89:048a72382b0a 45 wait(0.05);
DeguNaoto 85:dd18a2d79956 46 stateR = 69;
DeguNaoto 85:dd18a2d79956 47 stateL = 69;
DeguNaoto 83:e1638c58e1f1 48 step = 0;
DeguNaoto 83:e1638c58e1f1 49 CStep = 1;
DeguNaoto 86:5f0b065cb4d3 50 flaga = 1;
DeguNaoto 83:e1638c58e1f1 51 }
DeguNaoto 52:d9e1629da852 52 }
DeguNaoto 89:048a72382b0a 53 else if((b==5)&&(!flaga)){ /*middle start*/
DeguNaoto 83:e1638c58e1f1 54 if(edge5){
DeguNaoto 83:e1638c58e1f1 55 edge5=0;
DeguNaoto 83:e1638c58e1f1 56 resetState();
DeguNaoto 83:e1638c58e1f1 57 flagf = 1;
DeguNaoto 89:048a72382b0a 58 spcount=0.0;
DeguNaoto 89:048a72382b0a 59 // targ_velocity=speed;
DeguNaoto 89:048a72382b0a 60 // sendData(7,59);
DeguNaoto 103:ffd3ca4a7a71 61 sendData(5,60); //right
DeguNaoto 89:048a72382b0a 62 wait(0.05);
DeguNaoto 89:048a72382b0a 63 sendData(4,61); //left
DeguNaoto 89:048a72382b0a 64 wait(0.05);
DeguNaoto 88:cbed08732872 65 stateR = 59;
DeguNaoto 88:cbed08732872 66 stateL = 59;
DeguNaoto 83:e1638c58e1f1 67 step = 5;
DeguNaoto 101:b67d33e56b66 68 CStep = 7;
DeguNaoto 87:6b6b6611d33f 69 flaga = 1;
DeguNaoto 83:e1638c58e1f1 70 }
DeguNaoto 61:e018207ae860 71 }
DeguNaoto 89:048a72382b0a 72 else if((b==8)&&(!flaga)){ /*opponents start*/
DeguNaoto 83:e1638c58e1f1 73 if(edge8){
DeguNaoto 83:e1638c58e1f1 74 edge8=0;
DeguNaoto 83:e1638c58e1f1 75 resetState();
DeguNaoto 89:048a72382b0a 76 // targ_velocity = speed;
DeguNaoto 88:cbed08732872 77 // sendData(7,70);
DeguNaoto 103:ffd3ca4a7a71 78 sendData(5,71); //right
DeguNaoto 89:048a72382b0a 79 wait(0.05);
DeguNaoto 103:ffd3ca4a7a71 80 sendData(4,71); //left
DeguNaoto 103:ffd3ca4a7a71 81 // sendData(4,72); //left
DeguNaoto 89:048a72382b0a 82 wait(0.05);
DeguNaoto 88:cbed08732872 83 stateR = 70;
DeguNaoto 88:cbed08732872 84 stateL = 70;
DeguNaoto 97:5e6f513b26f2 85 step=114;
DeguNaoto 97:5e6f513b26f2 86 CStep=114;
DeguNaoto 103:ffd3ca4a7a71 87 OpStart.attach(&OpponentsStart,2.0);
DeguNaoto 96:a972ba5cf2d7 88 // spcount=0.0;
DeguNaoto 96:a972ba5cf2d7 89 // step = 15;
DeguNaoto 96:a972ba5cf2d7 90 // CStep = 15;
DeguNaoto 96:a972ba5cf2d7 91 // flaga = 1;
DeguNaoto 101:b67d33e56b66 92 // flagf = 1;
DeguNaoto 83:e1638c58e1f1 93 }
DeguNaoto 52:d9e1629da852 94 }
DeguNaoto 90:9fe39ebe721f 95 else if(b==9){ /******/
DeguNaoto 52:d9e1629da852 96 }
DeguNaoto 52:d9e1629da852 97 else if(b==1){ /*L down*/
DeguNaoto 52:d9e1629da852 98 if(edge1){
DeguNaoto 52:d9e1629da852 99 edge1=0;
DeguNaoto 52:d9e1629da852 100 if(stateL!=1) stateL--;
DeguNaoto 57:3fbd487e055e 101 sendData(4,stateL);
DeguNaoto 52:d9e1629da852 102 }
DeguNaoto 52:d9e1629da852 103 }
DeguNaoto 52:d9e1629da852 104 else if(b==2){ /*L up*/
DeguNaoto 52:d9e1629da852 105 if(edge2){
DeguNaoto 52:d9e1629da852 106 edge2=0;
unicore32 69:bf094811b4a9 107 if(stateL!=MAX_VALUE) stateL++;
DeguNaoto 57:3fbd487e055e 108 sendData(4,stateL);
DeguNaoto 22:3996c3f41922 109 }
DeguNaoto 52:d9e1629da852 110 }
DeguNaoto 52:d9e1629da852 111 else if(b==3){ /*R down*/
DeguNaoto 52:d9e1629da852 112 if(edge3){
DeguNaoto 52:d9e1629da852 113 edge3=0;
DeguNaoto 52:d9e1629da852 114 if(stateR!=1) stateR--;
DeguNaoto 57:3fbd487e055e 115 sendData(5,stateR);
DeguNaoto 52:d9e1629da852 116 }
DeguNaoto 52:d9e1629da852 117 }
DeguNaoto 52:d9e1629da852 118 else if(b==4){ /*R up*/
DeguNaoto 52:d9e1629da852 119 if(edge4){
DeguNaoto 52:d9e1629da852 120 edge4=0;
unicore32 69:bf094811b4a9 121 if(stateR!=MAX_VALUE) stateR++;
DeguNaoto 57:3fbd487e055e 122 sendData(5,stateR);
DeguNaoto 52:d9e1629da852 123 }
DeguNaoto 52:d9e1629da852 124 }
DeguNaoto 75:4cdcd50ee666 125 if(a2){
DeguNaoto 75:4cdcd50ee666 126 skip = 1;
DeguNaoto 75:4cdcd50ee666 127 }
DeguNaoto 75:4cdcd50ee666 128 else if(!a2){
DeguNaoto 75:4cdcd50ee666 129 skip = 0;
DeguNaoto 75:4cdcd50ee666 130 }
DeguNaoto 52:d9e1629da852 131 if(b!=1) edge1=1;
DeguNaoto 52:d9e1629da852 132 if(b!=2) edge2=1;
DeguNaoto 52:d9e1629da852 133 if(b!=3) edge3=1;
DeguNaoto 52:d9e1629da852 134 if(b!=4) edge4=1;
DeguNaoto 84:919a335ac81e 135 if(b!=5) edge5=1;
DeguNaoto 83:e1638c58e1f1 136 if(b!=6) edge6=1;
DeguNaoto 84:919a335ac81e 137 if(b!=7) edge7=1;
DeguNaoto 83:e1638c58e1f1 138 if(b!=8) edge8=1;
DeguNaoto 22:3996c3f41922 139 }
DeguNaoto 57:3fbd487e055e 140 #else
DeguNaoto 57:3fbd487e055e 141 void autoPS3(){
DeguNaoto 57:3fbd487e055e 142 if(circle){ /*mode change*/
DeguNaoto 57:3fbd487e055e 143 if(edge_circle){
DeguNaoto 57:3fbd487e055e 144 edge_circle=0;
DeguNaoto 57:3fbd487e055e 145 autoflag=0;
DeguNaoto 57:3fbd487e055e 146 Indicator4=0;
DeguNaoto 57:3fbd487e055e 147 IndicatorAuto=1;
DeguNaoto 87:6b6b6611d33f 148 flaga = 0;
DeguNaoto 57:3fbd487e055e 149 }
DeguNaoto 57:3fbd487e055e 150 }
DeguNaoto 57:3fbd487e055e 151 else if(up){ /*start*/
DeguNaoto 84:919a335ac81e 152 if(edge_up){
DeguNaoto 84:919a335ac81e 153 edge_up=0;
DeguNaoto 84:919a335ac81e 154 resetState();
DeguNaoto 84:919a335ac81e 155 flagf=1;
DeguNaoto 84:919a335ac81e 156 targ_velocity=speed;
DeguNaoto 85:dd18a2d79956 157 // sendData(7,70);
DeguNaoto 101:b67d33e56b66 158 sendData(5,68);
DeguNaoto 84:919a335ac81e 159 wait(0.1);
DeguNaoto 101:b67d33e56b66 160 sendData(4,68);
DeguNaoto 85:dd18a2d79956 161 stateR = 69;
DeguNaoto 85:dd18a2d79956 162 stateL = 69;
DeguNaoto 84:919a335ac81e 163 step = 0;
DeguNaoto 84:919a335ac81e 164 CStep = 1;
DeguNaoto 87:6b6b6611d33f 165 flaga = 1;
DeguNaoto 84:919a335ac81e 166 }
DeguNaoto 61:e018207ae860 167 }
DeguNaoto 61:e018207ae860 168 else if(right){ /*middle start*/
DeguNaoto 84:919a335ac81e 169 if(edge_right){
DeguNaoto 84:919a335ac81e 170 edge_right=0;
DeguNaoto 84:919a335ac81e 171 resetState();
DeguNaoto 84:919a335ac81e 172 flagf = 1;
DeguNaoto 84:919a335ac81e 173 targ_velocity=speed;
DeguNaoto 84:919a335ac81e 174 sendData(7,61);
DeguNaoto 85:dd18a2d79956 175 /*sendData(4,61);
DeguNaoto 85:dd18a2d79956 176 wait(0.1);
DeguNaoto 85:dd18a2d79956 177 sendData(5,58);*/
DeguNaoto 84:919a335ac81e 178 stateR = 61;
DeguNaoto 84:919a335ac81e 179 stateL = 61;
DeguNaoto 84:919a335ac81e 180 step = 5;
DeguNaoto 84:919a335ac81e 181 CStep = 6;
DeguNaoto 87:6b6b6611d33f 182 flaga = 1;
DeguNaoto 84:919a335ac81e 183 }
DeguNaoto 77:7dd154b5ace3 184 }
DeguNaoto 77:7dd154b5ace3 185 else if(down){ /*opponents start*/
DeguNaoto 84:919a335ac81e 186 if(edge_down){
DeguNaoto 84:919a335ac81e 187 edge_down=0;
DeguNaoto 84:919a335ac81e 188 resetState();
DeguNaoto 84:919a335ac81e 189 flagf = 1;
DeguNaoto 84:919a335ac81e 190 targ_velocity = speed;
DeguNaoto 85:dd18a2d79956 191 sendData(7,70);
DeguNaoto 85:dd18a2d79956 192 stateR = 70;
DeguNaoto 85:dd18a2d79956 193 stateL = 70;
DeguNaoto 84:919a335ac81e 194 step = 15;
DeguNaoto 84:919a335ac81e 195 CStep = 15;
DeguNaoto 87:6b6b6611d33f 196 flaga = 1;
DeguNaoto 84:919a335ac81e 197 }
DeguNaoto 57:3fbd487e055e 198 }
DeguNaoto 57:3fbd487e055e 199 else if(square){ /*reset*/
unicore32 68:2b2b88ecdcce 200 sendData(7,255);
DeguNaoto 57:3fbd487e055e 201 }
DeguNaoto 57:3fbd487e055e 202 else if(cross){ /*stop*/
DeguNaoto 57:3fbd487e055e 203 Motor_swing=0;
DeguNaoto 64:a98fe602c26d 204 // sita=PI/4.0,x=0.0,y=0.0;
DeguNaoto 57:3fbd487e055e 205 targ_velocity=0.0;
DeguNaoto 79:7f86e18f40ef 206 #ifdef BLUE
DeguNaoto 57:3fbd487e055e 207 targ_sita=PI/4.0;
DeguNaoto 79:7f86e18f40ef 208 #else
DeguNaoto 79:7f86e18f40ef 209 targ_sita=-PI/4.0;
DeguNaoto 79:7f86e18f40ef 210 #endif
DeguNaoto 57:3fbd487e055e 211 velocity_controller.reset();
DeguNaoto 57:3fbd487e055e 212 direction_controller.reset();
DeguNaoto 57:3fbd487e055e 213 }
DeguNaoto 57:3fbd487e055e 214 else if(l1){ /*L up*/
DeguNaoto 57:3fbd487e055e 215 if(edge_l1){
DeguNaoto 57:3fbd487e055e 216 edge_l1=0;
DeguNaoto 61:e018207ae860 217 if(stateL!=1) stateL--;
DeguNaoto 57:3fbd487e055e 218 sendData(4,stateL);
DeguNaoto 57:3fbd487e055e 219 }
DeguNaoto 57:3fbd487e055e 220 }
DeguNaoto 57:3fbd487e055e 221 else if(l2){ /*L down*/
DeguNaoto 57:3fbd487e055e 222 if(edge_l2){
DeguNaoto 57:3fbd487e055e 223 edge_l2=0;
unicore32 69:bf094811b4a9 224 if(stateL!=MAX_VALUE) stateL++;
DeguNaoto 57:3fbd487e055e 225 sendData(4,stateL);
DeguNaoto 57:3fbd487e055e 226 }
DeguNaoto 57:3fbd487e055e 227 }
DeguNaoto 57:3fbd487e055e 228 else if(r1){ /*R up*/
DeguNaoto 57:3fbd487e055e 229 if(edge_r1){
DeguNaoto 57:3fbd487e055e 230 edge_r1=0;
DeguNaoto 61:e018207ae860 231 if(stateR!=1) stateR--;
DeguNaoto 57:3fbd487e055e 232 sendData(5,stateR);
DeguNaoto 57:3fbd487e055e 233 }
DeguNaoto 57:3fbd487e055e 234 }
DeguNaoto 57:3fbd487e055e 235 else if(r2){ /*R down*/
DeguNaoto 57:3fbd487e055e 236 if(edge_r2){
DeguNaoto 57:3fbd487e055e 237 edge_r2=0;
unicore32 69:bf094811b4a9 238 if(stateR!=MAX_VALUE) stateR++;
DeguNaoto 57:3fbd487e055e 239 sendData(5,stateR);
DeguNaoto 57:3fbd487e055e 240 }
DeguNaoto 57:3fbd487e055e 241 }
DeguNaoto 75:4cdcd50ee666 242 if(triangle){
DeguNaoto 75:4cdcd50ee666 243 skip = 1;
DeguNaoto 75:4cdcd50ee666 244 }
DeguNaoto 75:4cdcd50ee666 245 else if(!triangle){
DeguNaoto 75:4cdcd50ee666 246 skip = 0;
DeguNaoto 75:4cdcd50ee666 247 }
DeguNaoto 57:3fbd487e055e 248 }
DeguNaoto 57:3fbd487e055e 249 #endif
DeguNaoto 21:79b94cb922f0 250 /***The function is PID controller initialize.***/
DeguNaoto 21:79b94cb922f0 251 inline void initializeControllers()
DeguNaoto 21:79b94cb922f0 252 {
DeguNaoto 66:14df82661dfa 253 // velocity_controller.setInputLimits(-200000.0, 200000.0); //x1
DeguNaoto 66:14df82661dfa 254 velocity_controller.setInputLimits(-20000.0, 20000.0); //x1
DeguNaoto 21:79b94cb922f0 255 direction_controller.setInputLimits(-10.0, 10.0); //x2
DeguNaoto 21:79b94cb922f0 256
DeguNaoto 21:79b94cb922f0 257 //Pwm output from MOVE_OUTPUT_LIMIT_BOTTOM to MOVE_OUTPUT_LIMIT_TOP
DeguNaoto 79:7f86e18f40ef 258 // velocity_controller.setOutputLimits(-1.0, 1.0);
DeguNaoto 79:7f86e18f40ef 259 velocity_controller.setOutputLimits(0.0, 1.0);
DeguNaoto 21:79b94cb922f0 260 direction_controller.setOutputLimits(-1.0, 1.0);
DeguNaoto 31:74e77ef0831c 261
DeguNaoto 21:79b94cb922f0 262 //set bias. 初期値
DeguNaoto 21:79b94cb922f0 263 velocity_controller.setBias(0.0);
DeguNaoto 21:79b94cb922f0 264 direction_controller.setBias(0.0);
DeguNaoto 21:79b94cb922f0 265
DeguNaoto 21:79b94cb922f0 266 //set mode.
DeguNaoto 21:79b94cb922f0 267 velocity_controller.setMode(AUTO_MODE);
DeguNaoto 21:79b94cb922f0 268 direction_controller.setMode(AUTO_MODE);
DeguNaoto 21:79b94cb922f0 269 }
DeguNaoto 21:79b94cb922f0 270
DeguNaoto 21:79b94cb922f0 271 /***The function is following move speed.***/
DeguNaoto 21:79b94cb922f0 272 inline void velocity_following()
DeguNaoto 21:79b94cb922f0 273 {
DeguNaoto 21:79b94cb922f0 274 velocity_controller.setSetPoint((float)targ_velocity);
DeguNaoto 21:79b94cb922f0 275 velocity_controller.setProcessValue((float)velocity);
DeguNaoto 21:79b94cb922f0 276 x1 = (double)velocity_controller.compute();
DeguNaoto 21:79b94cb922f0 277 }
DeguNaoto 21:79b94cb922f0 278
DeguNaoto 21:79b94cb922f0 279 inline void sita_following()
DeguNaoto 21:79b94cb922f0 280 {
DeguNaoto 21:79b94cb922f0 281 direction_controller.setSetPoint((float)targ_sita);
DeguNaoto 21:79b94cb922f0 282 direction_controller.setProcessValue((float)sita);
DeguNaoto 55:db1797ac6cb1 283 //direction_controller.setSetPoint(0.0); /*目標値とのずれをなくす*/
DeguNaoto 21:79b94cb922f0 284 //direction_controller.setProcessValue(-y);
DeguNaoto 21:79b94cb922f0 285 x2 = (double)direction_controller.compute();
DeguNaoto 21:79b94cb922f0 286 }
DeguNaoto 21:79b94cb922f0 287
DeguNaoto 21:79b94cb922f0 288 inline void move_following()
DeguNaoto 21:79b94cb922f0 289 {
DeguNaoto 21:79b94cb922f0 290 velocity_following();
DeguNaoto 21:79b94cb922f0 291 sita_following();
DeguNaoto 64:a98fe602c26d 292
DeguNaoto 64:a98fe602c26d 293 if(flagf==0){
DeguNaoto 79:7f86e18f40ef 294 Vr = ( 2.0*(-x1) + x2 ) / 3.0;
DeguNaoto 79:7f86e18f40ef 295 Vl = ( 2.0*(-x1) - x2 ) / 3.0;
DeguNaoto 63:dad3f4e5e99c 296 }
DeguNaoto 64:a98fe602c26d 297 else if(flagf==1){
DeguNaoto 64:a98fe602c26d 298 if(x2>0){
DeguNaoto 64:a98fe602c26d 299 Vr = x1;
DeguNaoto 64:a98fe602c26d 300 Vl = x1 - x2;
DeguNaoto 64:a98fe602c26d 301 }
DeguNaoto 64:a98fe602c26d 302 else{
DeguNaoto 64:a98fe602c26d 303 Vr = x1 + x2;
DeguNaoto 64:a98fe602c26d 304 Vl = x1;
DeguNaoto 64:a98fe602c26d 305 }
DeguNaoto 63:dad3f4e5e99c 306 }
DeguNaoto 63:dad3f4e5e99c 307
DeguNaoto 64:a98fe602c26d 308 if( abs(Vr) < 0.2 ) Vr = 0.0;
DeguNaoto 64:a98fe602c26d 309 if( abs(Vl) < 0.2 ) Vl = 0.0;
DeguNaoto 21:79b94cb922f0 310 Move_r( ( float ) Vr );
DeguNaoto 21:79b94cb922f0 311 Move_l( ( float ) Vl );
DeguNaoto 21:79b94cb922f0 312 }
DeguNaoto 21:79b94cb922f0 313
DeguNaoto 21:79b94cb922f0 314 #endif /*autoMode.h*/