Robot's source code
Dependencies: mbed
Asservissement/Asserv.h@90:294b16267109, 2015-04-23 (annotated)
- Committer:
- Near32
- Date:
- Thu Apr 23 18:20:35 2015 +0000
- Revision:
- 90:294b16267109
- Parent:
- 89:d05001d85a02
- Child:
- 91:c0b436c52ede
testing pid...
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Near32 | 36:54f86bc6fd80 | 1 | /*KalmanFilter*/ |
Near32 | 36:54f86bc6fd80 | 2 | #include "EKF.h" |
Near32 | 36:54f86bc6fd80 | 3 | #include "Odometry.h" |
Near32 | 89:d05001d85a02 | 4 | //#define debugAsserv |
Near32 | 89:d05001d85a02 | 5 | |
Near32 | 43:87bdce65341f | 6 | /* |
Near32 | 36:54f86bc6fd80 | 7 | template<typename T> |
Near32 | 36:54f86bc6fd80 | 8 | Mat<T> motion_bicycle3( Mat<T> state, Mat<T> command, T dt = (T)0.5); |
Near32 | 36:54f86bc6fd80 | 9 | template<typename T> |
Near32 | 36:54f86bc6fd80 | 10 | Mat<T> sensor_bicycle3( Mat<T> state, Mat<T> command, Mat<T> d_state, T dt = 0.5 ); |
Near32 | 36:54f86bc6fd80 | 11 | template<typename T> |
Near32 | 36:54f86bc6fd80 | 12 | Mat<T> jmotion_bicycle3( Mat<T> state, Mat<T> command, T dt = 0.5); |
Near32 | 36:54f86bc6fd80 | 13 | template<typename T> |
Near32 | 36:54f86bc6fd80 | 14 | Mat<T> jmotion_bicycle3_command(Mat<T> state, Mat<T> command, T dt = 0.5); |
Near32 | 36:54f86bc6fd80 | 15 | template<typename T> |
Near32 | 36:54f86bc6fd80 | 16 | Mat<T> jsensor_bicycle3( Mat<T> state, Mat<T> command, Mat<T> d_state, T dt = 0.5); |
Near32 | 36:54f86bc6fd80 | 17 | template<typename T> |
Near32 | 36:54f86bc6fd80 | 18 | bool setPWM(PwmOut *servo,T p); |
Near32 | 43:87bdce65341f | 19 | */ |
Near32 | 36:54f86bc6fd80 | 20 | |
Near32 | 36:54f86bc6fd80 | 21 | template<typename T> |
Near32 | 36:54f86bc6fd80 | 22 | bool setPWM(PwmOut *servo,T p) |
Near32 | 36:54f86bc6fd80 | 23 | { |
Near32 | 36:54f86bc6fd80 | 24 | if(p <= (T)1.0 && p >= (T)0.0) |
Near32 | 36:54f86bc6fd80 | 25 | { |
Near32 | 36:54f86bc6fd80 | 26 | servo->write((float)p); |
Near32 | 36:54f86bc6fd80 | 27 | return true; |
Near32 | 36:54f86bc6fd80 | 28 | } |
Near32 | 36:54f86bc6fd80 | 29 | |
Near32 | 36:54f86bc6fd80 | 30 | return false; |
Near32 | 36:54f86bc6fd80 | 31 | } |
Near32 | 36:54f86bc6fd80 | 32 | |
Near32 | 36:54f86bc6fd80 | 33 | template<typename T> |
Near32 | 36:54f86bc6fd80 | 34 | Mat<T> motion_bicycle3( Mat<T> state, Mat<T> command, T dt) |
Near32 | 43:87bdce65341f | 35 | { |
Near32 | 36:54f86bc6fd80 | 36 | Mat<T> bicycle(3,1); |
Near32 | 36:54f86bc6fd80 | 37 | bicycle.set((T)36, 1,1); /*radius*/ |
Near32 | 36:54f86bc6fd80 | 38 | bicycle.set((T)36, 2,1); |
Near32 | 36:54f86bc6fd80 | 39 | bicycle.set((T)220, 3,1); /*entre-roue*/ |
Near32 | 47:4909e97570f6 | 40 | Mat<T> r(state); |
Near32 | 47:4909e97570f6 | 41 | |
Near32 | 47:4909e97570f6 | 42 | /*state v w */ |
Near32 | 47:4909e97570f6 | 43 | /* |
Near32 | 36:54f86bc6fd80 | 44 | T v = state.get(4,1); |
Near32 | 36:54f86bc6fd80 | 45 | T w = state.get(5,1); |
Near32 | 47:4909e97570f6 | 46 | */ |
Near32 | 47:4909e97570f6 | 47 | /*state phi_l phi_r */ |
Near32 | 47:4909e97570f6 | 48 | T v = bicycle.get(1,1)/(2*bicycle.get(3,1))*(r.get(4,1)+r.get(5,1)); |
Near32 | 47:4909e97570f6 | 49 | T w = bicycle.get(1,1)/(2*bicycle.get(3,1))*(r.get(4,1)-r.get(5,1)); |
Near32 | 36:54f86bc6fd80 | 50 | |
Near32 | 36:54f86bc6fd80 | 51 | r.set( r.get(1,1) + v*cos(r.get(3,1))*dt, 1,1); |
Near32 | 36:54f86bc6fd80 | 52 | r.set( r.get(2,1) + v*sin(r.get(3,1))*dt, 2,1); |
Near32 | 36:54f86bc6fd80 | 53 | |
Near32 | 36:54f86bc6fd80 | 54 | T angle = (r.get(3,1) + dt*w); |
Near32 | 89:d05001d85a02 | 55 | if( angle < -(T)PI) |
Near32 | 36:54f86bc6fd80 | 56 | { |
Near32 | 89:d05001d85a02 | 57 | angle = angle - (T)PI*ceil(angle/PI); |
Near32 | 36:54f86bc6fd80 | 58 | } |
Near32 | 89:d05001d85a02 | 59 | else if( angle > (T)PI) |
Near32 | 36:54f86bc6fd80 | 60 | { |
Near32 | 89:d05001d85a02 | 61 | angle = angle - (T)PI*floor(angle/PI); |
Near32 | 36:54f86bc6fd80 | 62 | } |
Near32 | 36:54f86bc6fd80 | 63 | |
Near32 | 81:08eacb6179d8 | 64 | r.set( angle, 3,1); |
Near32 | 36:54f86bc6fd80 | 65 | |
Near32 | 36:54f86bc6fd80 | 66 | |
Near32 | 36:54f86bc6fd80 | 67 | /*----------------------------------------*/ |
Near32 | 36:54f86bc6fd80 | 68 | /*Modele du moteur*/ |
Near32 | 36:54f86bc6fd80 | 69 | /*----------------------------------------*/ |
Near32 | 36:54f86bc6fd80 | 70 | //double r1 = bicycle.get(3,1)/bicycle.get(1,1)*(command.get(1,1)/bicycle.get(3,1)+command.get(2,1)); |
Near32 | 47:4909e97570f6 | 71 | //double r2 = bicycle.get(3,1)/bicycle.get(1,1)*(command.get(1,1)/bicycle.get(3,1)-command.get(2,1)); |
Near32 | 47:4909e97570f6 | 72 | /*state v w */ |
Near32 | 47:4909e97570f6 | 73 | /* |
Near32 | 36:54f86bc6fd80 | 74 | T r1 = bicycle.get(1,1)/2*(command.get(1,1)+command.get(2,1)); |
Near32 | 36:54f86bc6fd80 | 75 | T r2 = bicycle.get(1,1)/bicycle.get(3,1)*(command.get(1,1)-command.get(2,1)); |
Near32 | 47:4909e97570f6 | 76 | */ |
Near32 | 47:4909e97570f6 | 77 | /*state phi_l phi_r*/ |
Near32 | 47:4909e97570f6 | 78 | T r1 = command.get(1,1); |
Near32 | 47:4909e97570f6 | 79 | T r2 = command.get(2,1); |
Near32 | 36:54f86bc6fd80 | 80 | |
Near32 | 36:54f86bc6fd80 | 81 | |
Near32 | 36:54f86bc6fd80 | 82 | r.set( r1, 4,1); |
Near32 | 36:54f86bc6fd80 | 83 | r.set( r2, 5,1); |
Near32 | 36:54f86bc6fd80 | 84 | |
Near32 | 36:54f86bc6fd80 | 85 | |
Near32 | 36:54f86bc6fd80 | 86 | /*----------------------------------------*/ |
Near32 | 36:54f86bc6fd80 | 87 | /*----------------------------------------*/ |
Near32 | 36:54f86bc6fd80 | 88 | |
Near32 | 36:54f86bc6fd80 | 89 | return r; |
Near32 | 36:54f86bc6fd80 | 90 | } |
Near32 | 36:54f86bc6fd80 | 91 | |
Near32 | 36:54f86bc6fd80 | 92 | template<typename T> |
Near32 | 36:54f86bc6fd80 | 93 | Mat<T> sensor_bicycle3( Mat<T> state, Mat<T> command, Mat<T> d_state, T dt) |
Near32 | 36:54f86bc6fd80 | 94 | { |
Near32 | 36:54f86bc6fd80 | 95 | /* |
Near32 | 36:54f86bc6fd80 | 96 | double angle = state.get(3,1); |
Near32 | 36:54f86bc6fd80 | 97 | if( angle < -PI) |
Near32 | 36:54f86bc6fd80 | 98 | { |
Near32 | 36:54f86bc6fd80 | 99 | angle = angle - PI*ceil(angle/PI); |
Near32 | 36:54f86bc6fd80 | 100 | } |
Near32 | 36:54f86bc6fd80 | 101 | else if( angle > PI) |
Near32 | 36:54f86bc6fd80 | 102 | { |
Near32 | 36:54f86bc6fd80 | 103 | angle = angle - PI*floor(angle/PI); |
Near32 | 36:54f86bc6fd80 | 104 | } |
Near32 | 36:54f86bc6fd80 | 105 | |
Near32 | 36:54f86bc6fd80 | 106 | state.set( atan21(sin(angle), cos(angle)), 3,1); |
Near32 | 36:54f86bc6fd80 | 107 | */ |
Near32 | 36:54f86bc6fd80 | 108 | return state; |
Near32 | 36:54f86bc6fd80 | 109 | } |
Near32 | 36:54f86bc6fd80 | 110 | |
Near32 | 36:54f86bc6fd80 | 111 | template<typename T> |
Near32 | 36:54f86bc6fd80 | 112 | Mat<T> jmotion_bicycle3( Mat<T> state, Mat<T> command, T dt) |
Near32 | 43:87bdce65341f | 113 | { |
Near32 | 43:87bdce65341f | 114 | T h = pow(numeric_limits<T>::epsilon(),(T)0.5)*norme2(state)+ pow(numeric_limits<T>::epsilon(), (T)0.5); |
Near32 | 36:54f86bc6fd80 | 115 | Mat<T> var( (T)0, state.getLine(), state.getColumn()); |
Near32 | 36:54f86bc6fd80 | 116 | var.set( h, 1,1); |
Near32 | 36:54f86bc6fd80 | 117 | Mat<T> G(motion_bicycle3(state+var, command, dt) - motion_bicycle3(state-var, command,dt)); |
Near32 | 36:54f86bc6fd80 | 118 | |
Near32 | 36:54f86bc6fd80 | 119 | for(int i=2;i<=state.getLine();i++) |
Near32 | 36:54f86bc6fd80 | 120 | { |
Near32 | 36:54f86bc6fd80 | 121 | var.set( (T)0, i-1,1); |
Near32 | 36:54f86bc6fd80 | 122 | var.set( h, i,1); |
Near32 | 36:54f86bc6fd80 | 123 | G = operatorL(G, motion_bicycle3(state+var, command, dt) - motion_bicycle3(state-var, command,dt) ); |
Near32 | 36:54f86bc6fd80 | 124 | } |
Near32 | 36:54f86bc6fd80 | 125 | |
Near32 | 36:54f86bc6fd80 | 126 | |
Near32 | 36:54f86bc6fd80 | 127 | return ((T)1.0/(2*h))*G; |
Near32 | 36:54f86bc6fd80 | 128 | } |
Near32 | 36:54f86bc6fd80 | 129 | |
Near32 | 36:54f86bc6fd80 | 130 | template<typename T> |
Near32 | 36:54f86bc6fd80 | 131 | Mat<T> jmotion_bicycle3_command(Mat<T> state, Mat<T> command, T dt) |
Near32 | 36:54f86bc6fd80 | 132 | { |
Near32 | 43:87bdce65341f | 133 | T h = pow(numeric_limits<T>::epsilon(), (T)0.5)+sqrt(numeric_limits<T>::epsilon())*norme2(state); |
Near32 | 36:54f86bc6fd80 | 134 | Mat<T> var( (T)0, command.getLine(), command.getColumn()); |
Near32 | 36:54f86bc6fd80 | 135 | var.set( h, 1,1); |
Near32 | 36:54f86bc6fd80 | 136 | Mat<T> G(motion_bicycle3(state, command+var, dt) - motion_bicycle3(state, command-var,dt)); |
Near32 | 36:54f86bc6fd80 | 137 | |
Near32 | 36:54f86bc6fd80 | 138 | for(int i=2;i<=command.getLine();i++) |
Near32 | 36:54f86bc6fd80 | 139 | { |
Near32 | 36:54f86bc6fd80 | 140 | var.set( (T)0, i-1,1); |
Near32 | 36:54f86bc6fd80 | 141 | var.set( h, i,1); |
Near32 | 36:54f86bc6fd80 | 142 | G = operatorL(G, motion_bicycle3(state, command+var, dt) - motion_bicycle3(state, command-var,dt) ); |
Near32 | 36:54f86bc6fd80 | 143 | } |
Near32 | 36:54f86bc6fd80 | 144 | |
Near32 | 36:54f86bc6fd80 | 145 | |
Near32 | 36:54f86bc6fd80 | 146 | return (1.0/(2*h))*G; |
Near32 | 36:54f86bc6fd80 | 147 | } |
Near32 | 36:54f86bc6fd80 | 148 | |
Near32 | 36:54f86bc6fd80 | 149 | |
Near32 | 36:54f86bc6fd80 | 150 | template<typename T> |
Near32 | 36:54f86bc6fd80 | 151 | Mat<T> jsensor_bicycle3( Mat<T> state, Mat<T> command, Mat<T> d_state, T dt) |
Near32 | 36:54f86bc6fd80 | 152 | { |
Near32 | 36:54f86bc6fd80 | 153 | T h = pow(numeric_limits<T>::epsilon(), (T)0.5)+sqrt(numeric_limits<T>::epsilon())*norme2(state); |
Near32 | 36:54f86bc6fd80 | 154 | Mat<T> var((T)0, state.getLine(), state.getColumn()); |
Near32 | 36:54f86bc6fd80 | 155 | var.set( h, 1,1); |
Near32 | 36:54f86bc6fd80 | 156 | Mat<T> H(sensor_bicycle3(state+var, command, d_state, dt) - sensor_bicycle3(state-var, command, d_state, dt)); |
Near32 | 36:54f86bc6fd80 | 157 | |
Near32 | 36:54f86bc6fd80 | 158 | for(int i=2;i<=state.getLine();i++) |
Near32 | 36:54f86bc6fd80 | 159 | { |
Near32 | 36:54f86bc6fd80 | 160 | var.set( (T)0, i-1,1); |
Near32 | 36:54f86bc6fd80 | 161 | var.set( h, i,1); |
Near32 | 36:54f86bc6fd80 | 162 | H = operatorL(H, sensor_bicycle3(state+var, command, d_state, dt) - sensor_bicycle3(state-var, command, d_state, dt) ); |
Near32 | 36:54f86bc6fd80 | 163 | } |
Near32 | 36:54f86bc6fd80 | 164 | |
Near32 | 36:54f86bc6fd80 | 165 | |
Near32 | 36:54f86bc6fd80 | 166 | return ((T)1.0/(2*h))*H; |
Near32 | 36:54f86bc6fd80 | 167 | /* |
Near32 | 36:54f86bc6fd80 | 168 | double h = sqrt(numeric_limits<double>::epsilon())*10e2+sqrt(numeric_limits<double>::epsilon())*norme2(state); |
Near32 | 36:54f86bc6fd80 | 169 | Mat<double> var((double)0, state.getLine(), state.getColumn()); |
Near32 | 36:54f86bc6fd80 | 170 | var.set( h, 1,1); |
Near32 | 36:54f86bc6fd80 | 171 | Mat<double> H(sensor_bicycle3(state+var, command, d_state, dt) - sensor_bicycle3(state-var, command, d_state, dt)); |
Near32 | 36:54f86bc6fd80 | 172 | |
Near32 | 36:54f86bc6fd80 | 173 | for(int i=2;i<=state.getLine();i++) |
Near32 | 36:54f86bc6fd80 | 174 | { |
Near32 | 36:54f86bc6fd80 | 175 | var.set( (double)0, i-1,1); |
Near32 | 36:54f86bc6fd80 | 176 | var.set( h, i,1); |
Near32 | 36:54f86bc6fd80 | 177 | H = operatorL(H, sensor_bicycle3(state+var, command, d_state, dt) - sensor_bicycle3(state-var, command, d_state, dt) ); |
Near32 | 36:54f86bc6fd80 | 178 | |
Near32 | 36:54f86bc6fd80 | 179 | } |
Near32 | 36:54f86bc6fd80 | 180 | |
Near32 | 36:54f86bc6fd80 | 181 | |
Near32 | 36:54f86bc6fd80 | 182 | return (1.0/(2*h))*H; |
Near32 | 36:54f86bc6fd80 | 183 | */ |
Near32 | 43:87bdce65341f | 184 | } |
Near32 | 43:87bdce65341f | 185 | //int reduc = 16; |
Near32 | 43:87bdce65341f | 186 | |
Near32 | 43:87bdce65341f | 187 | extern Serial logger; |
Near32 | 43:87bdce65341f | 188 | |
Near32 | 43:87bdce65341f | 189 | /*---------------------------------------------------------------------------------------------------------*/ |
Near32 | 43:87bdce65341f | 190 | /*---------------------------------------------------------------------------------------------------------*/ |
Near32 | 43:87bdce65341f | 191 | |
Near32 | 43:87bdce65341f | 192 | template<typename T> |
Near32 | 43:87bdce65341f | 193 | class Asserv |
Near32 | 43:87bdce65341f | 194 | { |
Near32 | 43:87bdce65341f | 195 | private : |
Near32 | 43:87bdce65341f | 196 | |
Near32 | 43:87bdce65341f | 197 | Odometry* odometry; |
Near32 | 43:87bdce65341f | 198 | EKF<T>* instanceEKF; |
Near32 | 43:87bdce65341f | 199 | int nbrstate; |
Near32 | 43:87bdce65341f | 200 | int nbrcontrol; |
Near32 | 43:87bdce65341f | 201 | int nbrobs; |
Near32 | 43:87bdce65341f | 202 | T dt; |
Near32 | 43:87bdce65341f | 203 | |
Near32 | 43:87bdce65341f | 204 | T stdnoise; |
Near32 | 43:87bdce65341f | 205 | Mat<T> X; |
Near32 | 43:87bdce65341f | 206 | Mat<T> dX; |
Near32 | 50:16c033eea17d | 207 | |
Near32 | 50:16c033eea17d | 208 | Mat<T> dXalpha; |
Near32 | 50:16c033eea17d | 209 | Mat<T> dXrho; |
Near32 | 50:16c033eea17d | 210 | Mat<T> dXbeta; |
Near32 | 50:16c033eea17d | 211 | |
Near32 | 43:87bdce65341f | 212 | Mat<T> initX; |
Near32 | 43:87bdce65341f | 213 | Mat<T> z; |
Near32 | 43:87bdce65341f | 214 | Mat<T> u; |
Near32 | 43:87bdce65341f | 215 | bool extended; |
Near32 | 43:87bdce65341f | 216 | bool filterOn; |
Near32 | 43:87bdce65341f | 217 | |
Near32 | 43:87bdce65341f | 218 | Mat<T> ki; |
Near32 | 43:87bdce65341f | 219 | Mat<T> kp; |
Near32 | 43:87bdce65341f | 220 | Mat<T> kd; |
Near32 | 43:87bdce65341f | 221 | |
Near32 | 43:87bdce65341f | 222 | public : |
Near32 | 43:87bdce65341f | 223 | |
Near32 | 43:87bdce65341f | 224 | T phi_r; |
Near32 | 43:87bdce65341f | 225 | T phi_l; |
Near32 | 43:87bdce65341f | 226 | T phi_max; |
Near32 | 43:87bdce65341f | 227 | bool execution; |
Near32 | 50:16c033eea17d | 228 | bool isarrived; |
Near32 | 50:16c033eea17d | 229 | bool isarrivedalpha; |
Near32 | 50:16c033eea17d | 230 | bool isarrivedrho; |
Near32 | 89:d05001d85a02 | 231 | int behaviour; |
Near32 | 43:87bdce65341f | 232 | |
Near32 | 43:87bdce65341f | 233 | Asserv(Odometry* odometry) |
Near32 | 43:87bdce65341f | 234 | { |
Near32 | 43:87bdce65341f | 235 | /*Odometry*/ |
Near32 | 43:87bdce65341f | 236 | this->odometry = odometry; |
Near32 | 44:d5f95af61243 | 237 | phi_max = (T)100.0; |
Near32 | 43:87bdce65341f | 238 | |
Near32 | 43:87bdce65341f | 239 | /*KalmanFilter*/ |
Near32 | 43:87bdce65341f | 240 | //double phi_max = 100; |
Near32 | 43:87bdce65341f | 241 | /*en millimetres*/ |
Near32 | 43:87bdce65341f | 242 | |
Near32 | 43:87bdce65341f | 243 | nbrstate = 5; |
Near32 | 43:87bdce65341f | 244 | nbrcontrol = 2; |
Near32 | 43:87bdce65341f | 245 | nbrobs = 5; |
Near32 | 43:87bdce65341f | 246 | dt = (T)0.05; |
Near32 | 43:87bdce65341f | 247 | stdnoise = (T)0.05; |
Near32 | 43:87bdce65341f | 248 | |
Near32 | 43:87bdce65341f | 249 | initX = Mat<T>((T)0, nbrstate, 1); |
Near32 | 43:87bdce65341f | 250 | initX.set( (T)0, 3,1); |
Near32 | 43:87bdce65341f | 251 | X = initX; |
Near32 | 43:87bdce65341f | 252 | |
Near32 | 43:87bdce65341f | 253 | extended = true; |
Near32 | 43:87bdce65341f | 254 | filterOn = true; |
Near32 | 43:87bdce65341f | 255 | |
Near32 | 43:87bdce65341f | 256 | instanceEKF = new EKF<T>(nbrstate, nbrcontrol, nbrobs, dt, stdnoise, /*current state*/ initX, extended, filterOn); |
Near32 | 43:87bdce65341f | 257 | |
Near32 | 43:87bdce65341f | 258 | instanceEKF->initMotion(motion_bicycle3); |
Near32 | 43:87bdce65341f | 259 | instanceEKF->initSensor(sensor_bicycle3); |
Near32 | 43:87bdce65341f | 260 | instanceEKF->initJMotion(jmotion_bicycle3); |
Near32 | 43:87bdce65341f | 261 | //instanceEKF.initJMotionCommand(jmotion_bicycle3_command); |
Near32 | 43:87bdce65341f | 262 | instanceEKF->initJSensor(jsensor_bicycle3); |
Near32 | 43:87bdce65341f | 263 | |
Near32 | 43:87bdce65341f | 264 | /*desired State : (x y theta phiright phileft)*/ |
Near32 | 43:87bdce65341f | 265 | dX = Mat<T>((T)0, nbrstate, 1); |
Near32 | 43:87bdce65341f | 266 | dX.set( (T)0, 1,1); |
Near32 | 43:87bdce65341f | 267 | dX.set( (T)0, 2,1); |
Near32 | 43:87bdce65341f | 268 | dX.set( (T)0, 3,1); |
Near32 | 43:87bdce65341f | 269 | dX.set( (T)0, 4,1); |
Near32 | 50:16c033eea17d | 270 | dX.set( (T)0, 5,1); |
Near32 | 50:16c033eea17d | 271 | |
Near32 | 50:16c033eea17d | 272 | dXalpha = dX; |
Near32 | 50:16c033eea17d | 273 | dXrho = dX; |
Near32 | 43:87bdce65341f | 274 | |
Near32 | 43:87bdce65341f | 275 | ki = Mat<T>((T)0, nbrcontrol, nbrstate); |
Near32 | 43:87bdce65341f | 276 | kp = Mat<T>((T)0, nbrcontrol, nbrstate); |
Near32 | 43:87bdce65341f | 277 | kd = Mat<T>((T)0, nbrcontrol, nbrstate); |
Near32 | 43:87bdce65341f | 278 | //Mat<double> kdd((double)0.0015, nbrcontrol, nbrstate); |
Near32 | 43:87bdce65341f | 279 | |
Near32 | 43:87bdce65341f | 280 | for(int i=1;i<=nbrstate;i++) |
Near32 | 43:87bdce65341f | 281 | { |
Near32 | 43:87bdce65341f | 282 | kp.set( (T)0.01, i, i); |
Near32 | 43:87bdce65341f | 283 | kd.set( (T)0.0001, i, i); |
Near32 | 43:87bdce65341f | 284 | ki.set( (T)0.0001, i, i); |
Near32 | 43:87bdce65341f | 285 | } |
Near32 | 43:87bdce65341f | 286 | |
Near32 | 43:87bdce65341f | 287 | instanceEKF->setKi(ki); |
Near32 | 43:87bdce65341f | 288 | instanceEKF->setKp(kp); |
Near32 | 43:87bdce65341f | 289 | instanceEKF->setKd(kd); |
Near32 | 43:87bdce65341f | 290 | //instance.setKdd(kdd); |
Near32 | 43:87bdce65341f | 291 | |
Near32 | 43:87bdce65341f | 292 | u = Mat<T>(transpose( instanceEKF->getCommand()) ); |
Near32 | 43:87bdce65341f | 293 | |
Near32 | 43:87bdce65341f | 294 | /*Observations*/ |
Near32 | 43:87bdce65341f | 295 | /*il nous faut 5 observation : mais on n'en met à jour que 3...*/ |
Near32 | 43:87bdce65341f | 296 | z = Mat<T>((T)0,5,1); |
Near32 | 43:87bdce65341f | 297 | this->measurementCallback(&z); |
Near32 | 43:87bdce65341f | 298 | |
Near32 | 43:87bdce65341f | 299 | /*----------------------------------------------------------------------------------------------*/ |
Near32 | 43:87bdce65341f | 300 | isarrived = true; |
Near32 | 50:16c033eea17d | 301 | isarrivedalpha = true; |
Near32 | 50:16c033eea17d | 302 | isarrivedrho = true; |
Near32 | 43:87bdce65341f | 303 | execution = false; |
Near32 | 89:d05001d85a02 | 304 | behaviour = 0; //alpha : 1 : rho : 2 : dX (beta) |
Near32 | 43:87bdce65341f | 305 | |
Near32 | 43:87bdce65341f | 306 | } |
Near32 | 43:87bdce65341f | 307 | |
Near32 | 43:87bdce65341f | 308 | ~Asserv() |
Near32 | 43:87bdce65341f | 309 | { |
Near32 | 43:87bdce65341f | 310 | delete instanceEKF; |
Near32 | 43:87bdce65341f | 311 | } |
Near32 | 43:87bdce65341f | 312 | |
Near32 | 43:87bdce65341f | 313 | void setGoal(T x, T y, T theta) |
Near32 | 43:87bdce65341f | 314 | { |
Near32 | 43:87bdce65341f | 315 | dX.set(x,1,1); |
Near32 | 43:87bdce65341f | 316 | dX.set(y,2,1); |
Near32 | 43:87bdce65341f | 317 | dX.set(theta,3,1); |
Near32 | 43:87bdce65341f | 318 | dX.set((T)0,4,1); |
Near32 | 43:87bdce65341f | 319 | dX.set((T)0,5,1); |
Near32 | 43:87bdce65341f | 320 | execution = true; |
Near32 | 43:87bdce65341f | 321 | isarrived = false; |
Near32 | 50:16c033eea17d | 322 | isarrivedalpha = false; |
Near32 | 50:16c033eea17d | 323 | isarrivedrho = false; |
Near32 | 50:16c033eea17d | 324 | |
Near32 | 90:294b16267109 | 325 | T alpha = atan2((dX.get(2,1)-X.get(2,1)),( dX.get(1,1) - X.get(1,1))); |
Near32 | 90:294b16267109 | 326 | alpha = alpha - atan2(sin(X.get(3,1)),cos(X.get(3,1))); |
Near32 | 90:294b16267109 | 327 | |
Near32 | 90:294b16267109 | 328 | while(alpha > (T)PI) |
Near32 | 90:294b16267109 | 329 | { |
Near32 | 90:294b16267109 | 330 | alpha -= 2.0f*(T)PI; |
Near32 | 90:294b16267109 | 331 | } |
Near32 | 90:294b16267109 | 332 | while(alpha <= -(T)PI) |
Near32 | 90:294b16267109 | 333 | { |
Near32 | 90:294b16267109 | 334 | alpha += 2.0f*(T)PI; |
Near32 | 90:294b16267109 | 335 | } |
Near32 | 90:294b16267109 | 336 | |
Near32 | 51:960ed3e8b648 | 337 | dXalpha.set( alpha, 3,1); |
Near32 | 50:16c033eea17d | 338 | |
Near32 | 50:16c033eea17d | 339 | dXrho = dX; |
Near32 | 51:960ed3e8b648 | 340 | dXrho.set( alpha, 3,1); |
Near32 | 43:87bdce65341f | 341 | |
Near32 | 43:87bdce65341f | 342 | } |
Near32 | 43:87bdce65341f | 343 | |
Near32 | 75:23222b28dc90 | 344 | bool isArrived() { return isarrived;} |
Near32 | 75:23222b28dc90 | 345 | bool isArrivedRho() { return isarrivedrho;} |
Near32 | 43:87bdce65341f | 346 | |
Near32 | 43:87bdce65341f | 347 | void stop() |
Near32 | 43:87bdce65341f | 348 | { |
Near32 | 43:87bdce65341f | 349 | execution = false; |
Near32 | 43:87bdce65341f | 350 | } |
Near32 | 43:87bdce65341f | 351 | |
Near32 | 43:87bdce65341f | 352 | void update(T deltat) |
Near32 | 43:87bdce65341f | 353 | { |
Near32 | 43:87bdce65341f | 354 | if(execution) |
Near32 | 43:87bdce65341f | 355 | { |
Near32 | 47:4909e97570f6 | 356 | dt = deltat; |
Near32 | 47:4909e97570f6 | 357 | |
Near32 | 43:87bdce65341f | 358 | /*Asservissement*/ |
Near32 | 47:4909e97570f6 | 359 | this->measurementCallback(&z); |
Near32 | 73:d8e1b543fbe3 | 360 | //transpose(z).afficherMblue(); |
Near32 | 50:16c033eea17d | 361 | |
Near32 | 50:16c033eea17d | 362 | |
Near32 | 50:16c033eea17d | 363 | /*Gestion des comportements : alpha --> rho --> beta --> isarrived = true;*/ |
Near32 | 50:16c033eea17d | 364 | /*------------------------------------------------------------------------*/ |
Near32 | 89:d05001d85a02 | 365 | |
Near32 | 89:d05001d85a02 | 366 | Mat<T> dXbehaviour(dX); |
Near32 | 90:294b16267109 | 367 | if(isarrivedalpha && phi_l+phi_r <= phi_max/20 && behaviour == 0) |
Near32 | 50:16c033eea17d | 368 | { |
Near32 | 50:16c033eea17d | 369 | behaviour=1; |
Near32 | 50:16c033eea17d | 370 | } |
Near32 | 90:294b16267109 | 371 | if(isarrivedalpha && isarrivedrho && phi_l+phi_r <= phi_max/20 && behaviour == 1) |
Near32 | 50:16c033eea17d | 372 | { |
Near32 | 50:16c033eea17d | 373 | behaviour = 2; |
Near32 | 50:16c033eea17d | 374 | } |
Near32 | 50:16c033eea17d | 375 | |
Near32 | 50:16c033eea17d | 376 | switch(behaviour) |
Near32 | 50:16c033eea17d | 377 | { |
Near32 | 50:16c033eea17d | 378 | case 0: |
Near32 | 90:294b16267109 | 379 | if(fabs_(dXalpha.get(3,1)-X.get(3,1) ) <= (T)0.02) |
Near32 | 50:16c033eea17d | 380 | { |
Near32 | 50:16c033eea17d | 381 | behaviour = 1; |
Near32 | 50:16c033eea17d | 382 | dXbehaviour = dXrho; |
Near32 | 51:960ed3e8b648 | 383 | isarrivedalpha = true; |
Near32 | 50:16c033eea17d | 384 | } |
Near32 | 50:16c033eea17d | 385 | else |
Near32 | 50:16c033eea17d | 386 | { |
Near32 | 50:16c033eea17d | 387 | dXbehaviour = dXalpha; |
Near32 | 50:16c033eea17d | 388 | isarrivedalpha = false; |
Near32 | 50:16c033eea17d | 389 | } |
Near32 | 50:16c033eea17d | 390 | |
Near32 | 50:16c033eea17d | 391 | break; |
Near32 | 50:16c033eea17d | 392 | |
Near32 | 50:16c033eea17d | 393 | case 1: |
Near32 | 89:d05001d85a02 | 394 | if(norme2( extract(dXrho-X,1,1, 2,1) ) <= (T)5) |
Near32 | 50:16c033eea17d | 395 | { |
Near32 | 50:16c033eea17d | 396 | behaviour = 2; |
Near32 | 50:16c033eea17d | 397 | dXbehaviour = dX; |
Near32 | 50:16c033eea17d | 398 | isarrivedrho = true; |
Near32 | 50:16c033eea17d | 399 | } |
Near32 | 50:16c033eea17d | 400 | else |
Near32 | 50:16c033eea17d | 401 | { |
Near32 | 50:16c033eea17d | 402 | dXbehaviour = dXrho; |
Near32 | 50:16c033eea17d | 403 | isarrivedrho = false; |
Near32 | 50:16c033eea17d | 404 | } |
Near32 | 50:16c033eea17d | 405 | break; |
Near32 | 50:16c033eea17d | 406 | |
Near32 | 50:16c033eea17d | 407 | default: |
Near32 | 90:294b16267109 | 408 | if(norme2(dX-X) <=5) |
Near32 | 50:16c033eea17d | 409 | { |
Near32 | 50:16c033eea17d | 410 | isarrived = true; |
Near32 | 50:16c033eea17d | 411 | } |
Near32 | 50:16c033eea17d | 412 | else |
Near32 | 50:16c033eea17d | 413 | { |
Near32 | 51:960ed3e8b648 | 414 | isarrived = false; |
Near32 | 50:16c033eea17d | 415 | } |
Near32 | 51:960ed3e8b648 | 416 | dXbehaviour = dX; |
Near32 | 50:16c033eea17d | 417 | break; |
Near32 | 50:16c033eea17d | 418 | } |
Near32 | 89:d05001d85a02 | 419 | |
Near32 | 50:16c033eea17d | 420 | /*------------------------------------------------*/ |
Near32 | 89:d05001d85a02 | 421 | //if( norme2( extract( dX-X,1,1,2,1)) <= 10 ) |
Near32 | 89:d05001d85a02 | 422 | // isarrivedrho = true; |
Near32 | 75:23222b28dc90 | 423 | |
Near32 | 81:08eacb6179d8 | 424 | #ifdef debugAsserv |
Near32 | 87:e8b64b4174b8 | 425 | logger.printf("BEHAVIOUR ASSERV : %d\r\n", behaviour); |
Near32 | 81:08eacb6179d8 | 426 | #endif |
Near32 | 89:d05001d85a02 | 427 | instanceEKF->measurement_Callback( z, dXbehaviour, true ); |
Near32 | 89:d05001d85a02 | 428 | |
Near32 | 89:d05001d85a02 | 429 | #ifdef debugAsserv |
Near32 | 89:d05001d85a02 | 430 | logger.printf("MEASUREMENT CALLBACK : DONE.\r\n"); |
Near32 | 89:d05001d85a02 | 431 | #endif |
Near32 | 90:294b16267109 | 432 | //instanceEKF->state_Callback(); |
Near32 | 90:294b16267109 | 433 | instanceEKF->setX(z); |
Near32 | 89:d05001d85a02 | 434 | X = instanceEKF->getX(); |
Near32 | 89:d05001d85a02 | 435 | |
Near32 | 89:d05001d85a02 | 436 | #ifdef debugAsserv |
Near32 | 89:d05001d85a02 | 437 | logger.printf("STATE CALLBACK : DONE. \r\n"); |
Near32 | 89:d05001d85a02 | 438 | transpose(X).afficherMblue(); |
Near32 | 89:d05001d85a02 | 439 | transpose(z).afficherMblue(); |
Near32 | 89:d05001d85a02 | 440 | #endif |
Near32 | 43:87bdce65341f | 441 | |
Near32 | 50:16c033eea17d | 442 | instanceEKF->computeCommand(dXbehaviour, (T)dt, -1); |
Near32 | 89:d05001d85a02 | 443 | |
Near32 | 89:d05001d85a02 | 444 | #ifdef debugAsserv |
Near32 | 89:d05001d85a02 | 445 | logger.printf("COMMAND CALLBACK : DONE. \r\n"); |
Near32 | 89:d05001d85a02 | 446 | #endif |
Near32 | 47:4909e97570f6 | 447 | //instanceEKF->computeCommand(dX, (T)dt, -2); |
Near32 | 44:d5f95af61243 | 448 | phi_l = instanceEKF->getCommand().get(1,1); |
Near32 | 44:d5f95af61243 | 449 | phi_r = instanceEKF->getCommand().get(2,1); |
Near32 | 43:87bdce65341f | 450 | } |
Near32 | 43:87bdce65341f | 451 | else |
Near32 | 43:87bdce65341f | 452 | { |
Near32 | 43:87bdce65341f | 453 | phi_r = (T)0; |
Near32 | 43:87bdce65341f | 454 | phi_l = (T)0; |
Near32 | 43:87bdce65341f | 455 | } |
Near32 | 43:87bdce65341f | 456 | } |
Near32 | 43:87bdce65341f | 457 | |
Near32 | 47:4909e97570f6 | 458 | void measurementCallbackVW( Mat<T>* z) |
Near32 | 43:87bdce65341f | 459 | { |
Near32 | 43:87bdce65341f | 460 | z->set( (T)/*conversionUnitée mm */this->odometry->getX(), 1,1); |
Near32 | 43:87bdce65341f | 461 | z->set( (T)/*conversionUnitée mm*/this->odometry->getY(), 2,1); |
Near32 | 89:d05001d85a02 | 462 | |
Near32 | 43:87bdce65341f | 463 | T theta = (T)this->odometry->getTheta(); |
Near32 | 43:87bdce65341f | 464 | theta = atan21(sin(theta),cos(theta)); |
Near32 | 89:d05001d85a02 | 465 | |
Near32 | 43:87bdce65341f | 466 | z->set( (double)/*conversionUnitée rad*/theta, 3,1);//odometry->getTheta(), 3,1); |
Near32 | 89:d05001d85a02 | 467 | |
Near32 | 43:87bdce65341f | 468 | T vx = (T)this->odometry->getVx(); |
Near32 | 43:87bdce65341f | 469 | T vy = (T)this->odometry->getVy(); |
Near32 | 43:87bdce65341f | 470 | z->set( sqrt(vx*vx+vy*vy),4,1); |
Near32 | 43:87bdce65341f | 471 | z->set( (T)odometry->getW(),5,1); |
Near32 | 43:87bdce65341f | 472 | |
Near32 | 44:d5f95af61243 | 473 | //transpose(*z).afficherMblue(); |
Near32 | 47:4909e97570f6 | 474 | } |
Near32 | 47:4909e97570f6 | 475 | |
Near32 | 47:4909e97570f6 | 476 | void measurementCallback( Mat<T>* z) |
Near32 | 47:4909e97570f6 | 477 | { |
Near32 | 47:4909e97570f6 | 478 | z->set( (T)/*conversionUnitée mm */this->odometry->getX(), 1,1); |
Near32 | 47:4909e97570f6 | 479 | z->set( (T)/*conversionUnitée mm*/this->odometry->getY(), 2,1); |
Near32 | 47:4909e97570f6 | 480 | T theta = (T)this->odometry->getTheta(); |
Jagang | 78:7c7cefbe1772 | 481 | //theta = atan2(sin(theta),cos(theta)); |
Near32 | 47:4909e97570f6 | 482 | z->set( (double)/*conversionUnitée rad*/theta, 3,1);//odometry->getTheta(), 3,1); |
Near32 | 47:4909e97570f6 | 483 | |
Near32 | 47:4909e97570f6 | 484 | //State : phi_l phi_r |
Near32 | 47:4909e97570f6 | 485 | z->set( (T)this->odometry->getPhileft(),4,1); |
Near32 | 47:4909e97570f6 | 486 | z->set( (T)this->odometry->getPhiright(),5,1); |
Near32 | 47:4909e97570f6 | 487 | |
Near32 | 47:4909e97570f6 | 488 | //transpose(*z).afficherMblue(); |
Near32 | 43:87bdce65341f | 489 | } |
Near32 | 43:87bdce65341f | 490 | |
Near32 | 43:87bdce65341f | 491 | Mat<T> getX() |
Near32 | 43:87bdce65341f | 492 | { |
Near32 | 43:87bdce65341f | 493 | return X; |
Near32 | 43:87bdce65341f | 494 | } |
Near32 | 43:87bdce65341f | 495 | |
Near32 | 43:87bdce65341f | 496 | T getPhiR() |
Near32 | 43:87bdce65341f | 497 | { |
Near32 | 43:87bdce65341f | 498 | return phi_r; |
Near32 | 43:87bdce65341f | 499 | } |
Near32 | 43:87bdce65341f | 500 | |
Near32 | 43:87bdce65341f | 501 | T getPhiL() |
Near32 | 43:87bdce65341f | 502 | { |
Near32 | 43:87bdce65341f | 503 | return phi_l; |
Near32 | 43:87bdce65341f | 504 | } |
Near32 | 43:87bdce65341f | 505 | |
Near32 | 43:87bdce65341f | 506 | T getPhiMax() |
Near32 | 43:87bdce65341f | 507 | { |
Near32 | 43:87bdce65341f | 508 | return phi_max; |
Near32 | 43:87bdce65341f | 509 | } |
Near32 | 43:87bdce65341f | 510 | |
Near32 | 43:87bdce65341f | 511 | |
Near32 | 43:87bdce65341f | 512 | }; |