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