Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of 2015robot_main by
autoMode.h@60:4a75f3f3a934, 2015-10-02 (annotated)
- Committer:
- DeguNaoto
- Date:
- Fri Oct 02 07:24:52 2015 +0000
- Revision:
- 60:4a75f3f3a934
- Parent:
- 57:3fbd487e055e
- Child:
- 61:e018207ae860
????????????
Who changed what in which revision?
User | Revision | Line number | New 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 | 31:74e77ef0831c | 5 | PID velocity_controller(36.0,5274.0 ,0.0,RATE); |
DeguNaoto | 34:f9ef622f4376 | 6 | PID direction_controller(36.0,3.0,0.0,RATE); |
DeguNaoto | 21:79b94cb922f0 | 7 | |
DeguNaoto | 57:3fbd487e055e | 8 | #ifdef IM920 |
DeguNaoto | 52:d9e1629da852 | 9 | /***IM920 correspondence***/ |
DeguNaoto | 52:d9e1629da852 | 10 | void autoIM920() { |
DeguNaoto | 52:d9e1629da852 | 11 | if(b==7){ /*mode change*/ |
DeguNaoto | 52:d9e1629da852 | 12 | if(edge7){ |
DeguNaoto | 52:d9e1629da852 | 13 | edge7=0; |
DeguNaoto | 52:d9e1629da852 | 14 | autoflag=0; |
DeguNaoto | 52:d9e1629da852 | 15 | Indicator4=0; |
DeguNaoto | 52:d9e1629da852 | 16 | IndicatorAuto=1; |
DeguNaoto | 52:d9e1629da852 | 17 | } |
DeguNaoto | 52:d9e1629da852 | 18 | } |
DeguNaoto | 52:d9e1629da852 | 19 | else if(b==6){ /*start*/ |
DeguNaoto | 52:d9e1629da852 | 20 | targ_velocity=speed; |
DeguNaoto | 52:d9e1629da852 | 21 | sendData(4,defoR); |
DeguNaoto | 52:d9e1629da852 | 22 | wait(0.1); |
DeguNaoto | 52:d9e1629da852 | 23 | sendData(5,defoL); |
DeguNaoto | 52:d9e1629da852 | 24 | wait(0.1); |
DeguNaoto | 52:d9e1629da852 | 25 | stateR = defoR; |
DeguNaoto | 52:d9e1629da852 | 26 | stateL = defoL; |
DeguNaoto | 52:d9e1629da852 | 27 | } |
DeguNaoto | 52:d9e1629da852 | 28 | else if(b==5){ /*reset*/ |
DeguNaoto | 52:d9e1629da852 | 29 | sendData(4,31); |
DeguNaoto | 52:d9e1629da852 | 30 | wait(0.1); |
DeguNaoto | 52:d9e1629da852 | 31 | sendData(5,31); |
DeguNaoto | 52:d9e1629da852 | 32 | wait(0.1); |
DeguNaoto | 52:d9e1629da852 | 33 | } |
DeguNaoto | 52:d9e1629da852 | 34 | else if(b==8){ /*set defo*/ |
DeguNaoto | 57:3fbd487e055e | 35 | sendData(5,defoR); |
DeguNaoto | 52:d9e1629da852 | 36 | wait(0.1); |
DeguNaoto | 57:3fbd487e055e | 37 | sendData(4,defoL); |
DeguNaoto | 52:d9e1629da852 | 38 | wait(0.1); |
DeguNaoto | 52:d9e1629da852 | 39 | } |
DeguNaoto | 52:d9e1629da852 | 40 | else if(b==9){ /*stop*/ |
DeguNaoto | 29:03bb1ddbe456 | 41 | Motor_swing=0; |
DeguNaoto | 22:3996c3f41922 | 42 | sita=PI/4.0,x=0.0,y=0.0; |
DeguNaoto | 22:3996c3f41922 | 43 | targ_velocity=0.0; |
DeguNaoto | 22:3996c3f41922 | 44 | targ_sita=PI/4.0; |
DeguNaoto | 22:3996c3f41922 | 45 | velocity_controller.reset(); |
DeguNaoto | 22:3996c3f41922 | 46 | direction_controller.reset(); |
DeguNaoto | 52:d9e1629da852 | 47 | } |
DeguNaoto | 52:d9e1629da852 | 48 | else if(b==1){ /*L down*/ |
DeguNaoto | 52:d9e1629da852 | 49 | if(edge1){ |
DeguNaoto | 52:d9e1629da852 | 50 | edge1=0; |
DeguNaoto | 52:d9e1629da852 | 51 | if(stateL!=1) stateL--; |
DeguNaoto | 57:3fbd487e055e | 52 | sendData(4,stateL); |
DeguNaoto | 52:d9e1629da852 | 53 | } |
DeguNaoto | 52:d9e1629da852 | 54 | } |
DeguNaoto | 52:d9e1629da852 | 55 | else if(b==2){ /*L up*/ |
DeguNaoto | 52:d9e1629da852 | 56 | if(edge2){ |
DeguNaoto | 52:d9e1629da852 | 57 | edge2=0; |
DeguNaoto | 52:d9e1629da852 | 58 | if(stateL!=28) stateL++; |
DeguNaoto | 57:3fbd487e055e | 59 | sendData(4,stateL); |
DeguNaoto | 22:3996c3f41922 | 60 | } |
DeguNaoto | 52:d9e1629da852 | 61 | } |
DeguNaoto | 52:d9e1629da852 | 62 | else if(b==3){ /*R down*/ |
DeguNaoto | 52:d9e1629da852 | 63 | if(edge3){ |
DeguNaoto | 52:d9e1629da852 | 64 | edge3=0; |
DeguNaoto | 52:d9e1629da852 | 65 | if(stateR!=1) stateR--; |
DeguNaoto | 57:3fbd487e055e | 66 | sendData(5,stateR); |
DeguNaoto | 52:d9e1629da852 | 67 | } |
DeguNaoto | 52:d9e1629da852 | 68 | } |
DeguNaoto | 52:d9e1629da852 | 69 | else if(b==4){ /*R up*/ |
DeguNaoto | 52:d9e1629da852 | 70 | if(edge4){ |
DeguNaoto | 52:d9e1629da852 | 71 | edge4=0; |
DeguNaoto | 52:d9e1629da852 | 72 | if(stateR!=28) stateR++; |
DeguNaoto | 57:3fbd487e055e | 73 | sendData(5,stateR); |
DeguNaoto | 52:d9e1629da852 | 74 | } |
DeguNaoto | 52:d9e1629da852 | 75 | } |
DeguNaoto | 52:d9e1629da852 | 76 | if(b!=7) edge7=1; |
DeguNaoto | 52:d9e1629da852 | 77 | if(b!=1) edge1=1; |
DeguNaoto | 52:d9e1629da852 | 78 | if(b!=2) edge2=1; |
DeguNaoto | 52:d9e1629da852 | 79 | if(b!=3) edge3=1; |
DeguNaoto | 52:d9e1629da852 | 80 | if(b!=4) edge4=1; |
DeguNaoto | 22:3996c3f41922 | 81 | } |
DeguNaoto | 57:3fbd487e055e | 82 | #else |
DeguNaoto | 57:3fbd487e055e | 83 | void autoPS3(){ |
DeguNaoto | 57:3fbd487e055e | 84 | if(circle){ /*mode change*/ |
DeguNaoto | 57:3fbd487e055e | 85 | if(edge_circle){ |
DeguNaoto | 57:3fbd487e055e | 86 | edge_circle=0; |
DeguNaoto | 57:3fbd487e055e | 87 | autoflag=0; |
DeguNaoto | 57:3fbd487e055e | 88 | Indicator4=0; |
DeguNaoto | 57:3fbd487e055e | 89 | IndicatorAuto=1; |
DeguNaoto | 57:3fbd487e055e | 90 | } |
DeguNaoto | 57:3fbd487e055e | 91 | } |
DeguNaoto | 57:3fbd487e055e | 92 | else if(up){ /*start*/ |
DeguNaoto | 57:3fbd487e055e | 93 | targ_velocity=speed; |
DeguNaoto | 60:4a75f3f3a934 | 94 | // sendData(5,defoR); |
DeguNaoto | 60:4a75f3f3a934 | 95 | sendData(5,13); |
DeguNaoto | 57:3fbd487e055e | 96 | wait(0.1); |
DeguNaoto | 60:4a75f3f3a934 | 97 | // sendData(4,defoL); |
DeguNaoto | 60:4a75f3f3a934 | 98 | sendData(4,13); |
DeguNaoto | 57:3fbd487e055e | 99 | wait(0.1); |
DeguNaoto | 57:3fbd487e055e | 100 | stateR = defoR; |
DeguNaoto | 57:3fbd487e055e | 101 | stateL = defoL; |
DeguNaoto | 57:3fbd487e055e | 102 | } |
DeguNaoto | 57:3fbd487e055e | 103 | else if(square){ /*reset*/ |
DeguNaoto | 57:3fbd487e055e | 104 | sendData(4,31); |
DeguNaoto | 57:3fbd487e055e | 105 | wait(0.1); |
DeguNaoto | 57:3fbd487e055e | 106 | sendData(5,31); |
DeguNaoto | 57:3fbd487e055e | 107 | wait(0.1); |
DeguNaoto | 57:3fbd487e055e | 108 | } |
DeguNaoto | 57:3fbd487e055e | 109 | else if(triangle){ /*set defo*/ |
DeguNaoto | 60:4a75f3f3a934 | 110 | sendData(5,defoR); |
DeguNaoto | 57:3fbd487e055e | 111 | wait(0.1); |
DeguNaoto | 60:4a75f3f3a934 | 112 | sendData(4,defoL); |
DeguNaoto | 57:3fbd487e055e | 113 | wait(0.1); |
DeguNaoto | 57:3fbd487e055e | 114 | } |
DeguNaoto | 57:3fbd487e055e | 115 | else if(cross){ /*stop*/ |
DeguNaoto | 57:3fbd487e055e | 116 | Motor_swing=0; |
DeguNaoto | 57:3fbd487e055e | 117 | sita=PI/4.0,x=0.0,y=0.0; |
DeguNaoto | 57:3fbd487e055e | 118 | targ_velocity=0.0; |
DeguNaoto | 57:3fbd487e055e | 119 | targ_sita=PI/4.0; |
DeguNaoto | 57:3fbd487e055e | 120 | velocity_controller.reset(); |
DeguNaoto | 57:3fbd487e055e | 121 | direction_controller.reset(); |
DeguNaoto | 57:3fbd487e055e | 122 | } |
DeguNaoto | 57:3fbd487e055e | 123 | else if(l1){ /*L up*/ |
DeguNaoto | 57:3fbd487e055e | 124 | if(edge_l1){ |
DeguNaoto | 57:3fbd487e055e | 125 | edge_l1=0; |
DeguNaoto | 57:3fbd487e055e | 126 | if(stateL!=2) stateL--; |
DeguNaoto | 57:3fbd487e055e | 127 | sendData(4,stateL); |
DeguNaoto | 57:3fbd487e055e | 128 | } |
DeguNaoto | 57:3fbd487e055e | 129 | } |
DeguNaoto | 57:3fbd487e055e | 130 | else if(l2){ /*L down*/ |
DeguNaoto | 57:3fbd487e055e | 131 | if(edge_l2){ |
DeguNaoto | 57:3fbd487e055e | 132 | edge_l2=0; |
DeguNaoto | 57:3fbd487e055e | 133 | if(stateL!=28) stateL++; |
DeguNaoto | 57:3fbd487e055e | 134 | sendData(4,stateL); |
DeguNaoto | 57:3fbd487e055e | 135 | } |
DeguNaoto | 57:3fbd487e055e | 136 | } |
DeguNaoto | 57:3fbd487e055e | 137 | else if(r1){ /*R up*/ |
DeguNaoto | 57:3fbd487e055e | 138 | if(edge_r1){ |
DeguNaoto | 57:3fbd487e055e | 139 | edge_r1=0; |
DeguNaoto | 57:3fbd487e055e | 140 | if(stateR!=2) stateR--; |
DeguNaoto | 57:3fbd487e055e | 141 | sendData(5,stateR); |
DeguNaoto | 57:3fbd487e055e | 142 | } |
DeguNaoto | 57:3fbd487e055e | 143 | } |
DeguNaoto | 57:3fbd487e055e | 144 | else if(r2){ /*R down*/ |
DeguNaoto | 57:3fbd487e055e | 145 | if(edge_r2){ |
DeguNaoto | 57:3fbd487e055e | 146 | edge_r2=0; |
DeguNaoto | 57:3fbd487e055e | 147 | if(stateR!=28) stateR++; |
DeguNaoto | 57:3fbd487e055e | 148 | sendData(5,stateR); |
DeguNaoto | 57:3fbd487e055e | 149 | } |
DeguNaoto | 57:3fbd487e055e | 150 | } |
DeguNaoto | 57:3fbd487e055e | 151 | } |
DeguNaoto | 57:3fbd487e055e | 152 | #endif |
DeguNaoto | 21:79b94cb922f0 | 153 | /***The function is PID controller initialize.***/ |
DeguNaoto | 21:79b94cb922f0 | 154 | inline void initializeControllers() |
DeguNaoto | 21:79b94cb922f0 | 155 | { |
DeguNaoto | 21:79b94cb922f0 | 156 | velocity_controller.setInputLimits(-200000.0, 200000.0); //x1 |
DeguNaoto | 21:79b94cb922f0 | 157 | direction_controller.setInputLimits(-10.0, 10.0); //x2 |
DeguNaoto | 21:79b94cb922f0 | 158 | |
DeguNaoto | 21:79b94cb922f0 | 159 | //Pwm output from MOVE_OUTPUT_LIMIT_BOTTOM to MOVE_OUTPUT_LIMIT_TOP |
DeguNaoto | 21:79b94cb922f0 | 160 | velocity_controller.setOutputLimits(-1.0, 1.0); |
DeguNaoto | 21:79b94cb922f0 | 161 | direction_controller.setOutputLimits(-1.0, 1.0); |
DeguNaoto | 31:74e77ef0831c | 162 | |
DeguNaoto | 21:79b94cb922f0 | 163 | //set bias. 初期値 |
DeguNaoto | 21:79b94cb922f0 | 164 | velocity_controller.setBias(0.0); |
DeguNaoto | 21:79b94cb922f0 | 165 | direction_controller.setBias(0.0); |
DeguNaoto | 21:79b94cb922f0 | 166 | |
DeguNaoto | 21:79b94cb922f0 | 167 | //set mode. |
DeguNaoto | 21:79b94cb922f0 | 168 | velocity_controller.setMode(AUTO_MODE); |
DeguNaoto | 21:79b94cb922f0 | 169 | direction_controller.setMode(AUTO_MODE); |
DeguNaoto | 21:79b94cb922f0 | 170 | } |
DeguNaoto | 21:79b94cb922f0 | 171 | |
DeguNaoto | 21:79b94cb922f0 | 172 | /***The function is following move speed.***/ |
DeguNaoto | 21:79b94cb922f0 | 173 | inline void velocity_following() |
DeguNaoto | 21:79b94cb922f0 | 174 | { |
DeguNaoto | 21:79b94cb922f0 | 175 | velocity_controller.setSetPoint((float)targ_velocity); |
DeguNaoto | 21:79b94cb922f0 | 176 | velocity_controller.setProcessValue((float)velocity); |
DeguNaoto | 21:79b94cb922f0 | 177 | x1 = (double)velocity_controller.compute(); |
DeguNaoto | 21:79b94cb922f0 | 178 | } |
DeguNaoto | 21:79b94cb922f0 | 179 | |
DeguNaoto | 21:79b94cb922f0 | 180 | inline void sita_following() |
DeguNaoto | 21:79b94cb922f0 | 181 | { |
DeguNaoto | 21:79b94cb922f0 | 182 | direction_controller.setSetPoint((float)targ_sita); |
DeguNaoto | 21:79b94cb922f0 | 183 | direction_controller.setProcessValue((float)sita); |
DeguNaoto | 55:db1797ac6cb1 | 184 | //direction_controller.setSetPoint(0.0); /*目標値とのずれをなくす*/ |
DeguNaoto | 21:79b94cb922f0 | 185 | //direction_controller.setProcessValue(-y); |
DeguNaoto | 21:79b94cb922f0 | 186 | x2 = (double)direction_controller.compute(); |
DeguNaoto | 21:79b94cb922f0 | 187 | } |
DeguNaoto | 21:79b94cb922f0 | 188 | |
DeguNaoto | 21:79b94cb922f0 | 189 | inline void move_following() |
DeguNaoto | 21:79b94cb922f0 | 190 | { |
DeguNaoto | 21:79b94cb922f0 | 191 | velocity_following(); |
DeguNaoto | 21:79b94cb922f0 | 192 | sita_following(); |
DeguNaoto | 21:79b94cb922f0 | 193 | Vr = ( x1 + x2 ) / 2.0; |
DeguNaoto | 21:79b94cb922f0 | 194 | Vl = ( x1 - x2 ) / 2.0; |
DeguNaoto | 21:79b94cb922f0 | 195 | if( abs(Vr) < 0.05 ) Vr = 0.0; |
DeguNaoto | 21:79b94cb922f0 | 196 | if( abs(Vl) < 0.05 ) Vl = 0.0; |
DeguNaoto | 21:79b94cb922f0 | 197 | Move_r( ( float ) Vr ); |
DeguNaoto | 21:79b94cb922f0 | 198 | Move_l( ( float ) Vl ); |
DeguNaoto | 21:79b94cb922f0 | 199 | } |
DeguNaoto | 21:79b94cb922f0 | 200 | |
DeguNaoto | 21:79b94cb922f0 | 201 | #endif /*autoMode.h*/ |