Robot's source code

Dependencies:   mbed

Committer:
Near32
Date:
Sun Dec 14 18:03:34 2014 +0000
Revision:
22:80fdb12c167f
Parent:
21:5443f93819db
Child:
23:228317fe0457
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jagang 0:41149573d577 1 #include "mbed.h"
Jagang 0:41149573d577 2 #include "QEI.h"
Jagang 0:41149573d577 3 #include "Odometry.h"
Near32 17:f360e21d3307 4 #include <iostream>
Jagang 0:41149573d577 5
Near32 3:573a0dc8383f 6 /*---------------------------------------------------------------------------------------------------------*/
Near32 3:573a0dc8383f 7 /*---------------------------------------------------------------------------------------------------------*/
Near32 3:573a0dc8383f 8 /*KalmanFilter*/
Near32 3:573a0dc8383f 9 #include "EKF.h"
Near32 17:f360e21d3307 10 Mat<double> motion_bicycle3( Mat<double> state, Mat<double> command, double dt = 0.5);
Near32 17:f360e21d3307 11 Mat<double> sensor_bicycle3( Mat<double> state, Mat<double> command, Mat<double> d_state, double dt = 0.5 );
Near32 17:f360e21d3307 12 Mat<double> jmotion_bicycle3( Mat<double> state, Mat<double> command, double dt = 0.5);
Near32 17:f360e21d3307 13 Mat<double> jsensor_bicycle3( Mat<double> state, Mat<double> command, Mat<double> d_state, double dt = 0.5);
Near32 3:573a0dc8383f 14 void measurementCallback( Mat<double>* z, Odometry* odometry);
Near32 17:f360e21d3307 15 bool setPWM(PwmOut *servo,float p);
Near32 3:573a0dc8383f 16
Near32 3:573a0dc8383f 17 Mat<double> bicycle(3,1);
Near32 17:f360e21d3307 18 int reduc = 16;
Near32 3:573a0dc8383f 19 /*---------------------------------------------------------------------------------------------------------*/
Near32 3:573a0dc8383f 20 /*---------------------------------------------------------------------------------------------------------*/
Near32 3:573a0dc8383f 21
Near32 17:f360e21d3307 22 /*----------------------------------------------------------------------------------------------*/
Near32 17:f360e21d3307 23 /*Serial*/
Near32 17:f360e21d3307 24 Serial pcs(USBTX, USBRX); // tx, rx
Near32 17:f360e21d3307 25 /*----------------------------------------------------------------------------------------------*/
Jagang 21:5443f93819db 26
Jagang 0:41149573d577 27 int main()
Jagang 0:41149573d577 28 {
Near32 17:f360e21d3307 29
Near32 17:f360e21d3307 30
Near32 17:f360e21d3307 31 PwmOut pw1(p22);
Near32 17:f360e21d3307 32 DigitalOut dir1(p21);
Near32 17:f360e21d3307 33 PwmOut pw2(p24);
Near32 17:f360e21d3307 34 DigitalOut dir2(p23);
Near32 17:f360e21d3307 35
Near32 17:f360e21d3307 36 //mbuino
Near32 17:f360e21d3307 37 /*
Near32 17:f360e21d3307 38 PwmOut pw1(P0_17);
Near32 17:f360e21d3307 39 DigitalOut dir1(P0_18);
Near32 17:f360e21d3307 40 PwmOut pw2(P0_23);
Near32 17:f360e21d3307 41 DigitalOut dir2(P0_19);
Near32 17:f360e21d3307 42 */
Near32 17:f360e21d3307 43 /*
Near32 17:f360e21d3307 44 //nucleo
Near32 17:f360e21d3307 45 PwmOut pw1(PB_8);
Near32 17:f360e21d3307 46 DigitalOut dir1(D12);
Near32 17:f360e21d3307 47 PwmOut pw2(PB_9);
Near32 17:f360e21d3307 48 DigitalOut dir2(D13);
Near32 17:f360e21d3307 49 */
Near32 17:f360e21d3307 50 pw1.period_us(10);
Near32 17:f360e21d3307 51 pw2.period_us(10);
Near32 17:f360e21d3307 52
Near32 17:f360e21d3307 53
Near32 17:f360e21d3307 54 dir1.write(0);
Near32 17:f360e21d3307 55 dir2.write(0);
Near32 17:f360e21d3307 56 pw1.write(1.0);
Near32 17:f360e21d3307 57 pw2.write(0.8);
Near32 17:f360e21d3307 58 //setPWM(&pw1,0.9);
Near32 17:f360e21d3307 59 pcs.printf("mise à jour des pwm.\n");
Near32 17:f360e21d3307 60 //while(1);
Near32 3:573a0dc8383f 61 /*----------------------------------------------------------------------------------------------*/
Near32 3:573a0dc8383f 62 /*Odometry*/
Near32 17:f360e21d3307 63 QEI qei_left(p15,p16,NC,1024*reduc,QEI::X4_ENCODING);
Near32 17:f360e21d3307 64 //QEI qei_left(P0_2,P0_7,NC,1024*reduc,QEI::X4_ENCODING);//mbuino
Near32 17:f360e21d3307 65 //QEI qei_left(PA_3,PA_2,NC,1024*reduc,QEI::X4_ENCODING);//nucleo
Near32 17:f360e21d3307 66
Near32 17:f360e21d3307 67 QEI qei_right(p17,p18,NC,1024*reduc,QEI::X4_ENCODING);
Near32 17:f360e21d3307 68 //QEI qei_right(P0_8,P0_20,NC,1024*reduc,QEI::X4_ENCODING);//mbuino
Near32 17:f360e21d3307 69 //QEI qei_right(PA_10,PB_3,NC,1024*reduc,QEI::X4_ENCODING);//nucleo
Jagang 0:41149573d577 70
Jagang 0:41149573d577 71 Odometry odometry(&qei_left,&qei_right,0.07,0.07,0.26);
Near32 3:573a0dc8383f 72 /*----------------------------------------------------------------------------------------------*/
Jagang 0:41149573d577 73
Near32 3:573a0dc8383f 74
Near32 3:573a0dc8383f 75
Near32 3:573a0dc8383f 76 /*----------------------------------------------------------------------------------------------*/
Near32 3:573a0dc8383f 77 /*KalmanFilter*/
Near32 8:1150a13f6967 78 double phi_max = 100;
Near32 3:573a0dc8383f 79 /*en millimetres*/
Near32 3:573a0dc8383f 80 bicycle.set((double)100, 1,1); /*radius*/
Near32 3:573a0dc8383f 81 bicycle.set((double)100, 2,1);
Near32 3:573a0dc8383f 82 bicycle.set((double)66, 3,1); /*entre-roue*/
Near32 3:573a0dc8383f 83
Near32 3:573a0dc8383f 84 int nbrstate = 5;
Near32 3:573a0dc8383f 85 int nbrcontrol = 2;
Near32 17:f360e21d3307 86 int nbrobs = 5;
Near32 3:573a0dc8383f 87 double dt = (double)0.05;
Near32 8:1150a13f6967 88 double stdnoise = (double)0.05;
Near32 3:573a0dc8383f 89
Near32 3:573a0dc8383f 90 Mat<double> initX((double)0, nbrstate, 1);
Near32 3:573a0dc8383f 91 initX.set( (double)0, 3,1);
Near32 3:573a0dc8383f 92
Near32 3:573a0dc8383f 93 bool extended = true;
Near32 17:f360e21d3307 94 bool filterOn = false;
Near32 17:f360e21d3307 95 EKF<double> instance(&pcs, nbrstate, nbrcontrol, nbrobs, dt, stdnoise, /*current state*/ initX, extended, filterOn);
Near32 3:573a0dc8383f 96
Near32 17:f360e21d3307 97 instance.initMotion(motion_bicycle3);
Near32 17:f360e21d3307 98 instance.initSensor(sensor_bicycle3);
Near32 17:f360e21d3307 99 instance.initJMotion(jmotion_bicycle3);
Near32 17:f360e21d3307 100 instance.initJSensor(jsensor_bicycle3);
Near32 3:573a0dc8383f 101
Near32 3:573a0dc8383f 102 /*desired State : (x y theta phiright phileft)*/
Near32 3:573a0dc8383f 103 Mat<double> dX((double)0, nbrstate, 1);
Near32 17:f360e21d3307 104 dX.set( (double)100, 1,1);
Near32 17:f360e21d3307 105 dX.set( (double)0, 2,1);
Near32 4:4025c071b207 106 dX.set( (double)0, 3,1);
Near32 3:573a0dc8383f 107 dX.set( (double)0, 4,1);
Near32 3:573a0dc8383f 108 dX.set( (double)0, 5,1);
Near32 3:573a0dc8383f 109
Near32 17:f360e21d3307 110 Mat<double> ki((double)0, nbrcontrol, nbrstate);
Near32 17:f360e21d3307 111 Mat<double> kp((double)0, nbrcontrol, nbrstate);
Near32 17:f360e21d3307 112 Mat<double> kd((double)0, nbrcontrol, nbrstate);
Near32 17:f360e21d3307 113 //Mat<double> kdd((double)0.0015, nbrcontrol, nbrstate);
Near32 17:f360e21d3307 114
Near32 17:f360e21d3307 115 for(int i=1;i<=nbrstate;i++)
Near32 17:f360e21d3307 116 {
Near32 17:f360e21d3307 117 kp.set( (double)0.01, i, i);
Near32 17:f360e21d3307 118 kd.set( (double)0.0001, i, i);
Near32 17:f360e21d3307 119 ki.set( (double)0.0001, i, i);
Near32 17:f360e21d3307 120 }
Near32 17:f360e21d3307 121
Near32 17:f360e21d3307 122 instance.setKi(ki);
Near32 17:f360e21d3307 123 instance.setKp(kp);
Near32 17:f360e21d3307 124 instance.setKd(kd);
Near32 17:f360e21d3307 125 //instance.setKdd(kdd);
Near32 17:f360e21d3307 126
Near32 3:573a0dc8383f 127 Mat<double> u(transpose( instance.getCommand()) );
Near32 3:573a0dc8383f 128
Near32 3:573a0dc8383f 129 /*Observations*/
Near32 17:f360e21d3307 130 /*il nous faut 5 observation :*/
Near32 17:f360e21d3307 131 Mat<double> z((double)0,5,1);
Near32 3:573a0dc8383f 132 measurementCallback(&z, &odometry);
Near32 3:573a0dc8383f 133
Near32 3:573a0dc8383f 134 /*----------------------------------------------------------------------------------------------*/
Near32 3:573a0dc8383f 135
Near32 3:573a0dc8383f 136
Jagang 0:41149573d577 137 while(1)
Jagang 0:41149573d577 138 {
Near32 17:f360e21d3307 139 //wait(1);
Near32 17:f360e21d3307 140 pcs.printf("%f : %f : %f\n",odometry.getX()*100,odometry.getY()*100,odometry.getTheta()*180/3.14);
Near32 17:f360e21d3307 141
Near32 3:573a0dc8383f 142
Near32 3:573a0dc8383f 143 /*------------------------------------------------------------------------------------------*/
Near32 17:f360e21d3307 144 /*Asservissement*/
Near32 17:f360e21d3307 145
Near32 17:f360e21d3307 146 //measurementCallback(&z, &odometry);
Near32 17:f360e21d3307 147 instance.measurement_Callback( instance.getX(), dX, true );
Near32 17:f360e21d3307 148
Near32 3:573a0dc8383f 149 instance.state_Callback();
Near32 17:f360e21d3307 150
Near32 10:bca0274a007b 151 double phi_r = instance.getCommand().get(1,1);
Near32 10:bca0274a007b 152 double phi_l = instance.getCommand().get(2,1);
Near32 8:1150a13f6967 153
Near32 17:f360e21d3307 154 double phi_max = 100;
Near32 3:573a0dc8383f 155
Near32 17:f360e21d3307 156 instance.computeCommand(dX, (double)dt, -2);
Near32 22:80fdb12c167f 157 pcs.printf("command : \n phi_r = %f \n phi_l = %f \n", (phi_r/phi_max)*100, (phi_l/phi_max)*100);
Near32 17:f360e21d3307 158 //instance.getX().afficher();
Near32 17:f360e21d3307 159
Near32 17:f360e21d3307 160
Near32 17:f360e21d3307 161 if(phi_r <= 0)
Near32 17:f360e21d3307 162 dir1.write(0);
Near32 17:f360e21d3307 163 else
Near32 17:f360e21d3307 164 dir1.write(1);
Near32 17:f360e21d3307 165
Near32 17:f360e21d3307 166 if(phi_l <= 0)
Near32 17:f360e21d3307 167 dir2.write(0);
Near32 17:f360e21d3307 168 else
Near32 17:f360e21d3307 169 dir2.write(1);
Near32 17:f360e21d3307 170
Near32 17:f360e21d3307 171 if(abs(phi_r/phi_max) < 1.0)
Near32 22:80fdb12c167f 172 setPWM(&pw1, (float)abs((double)phi_r/phi_max));
Near32 17:f360e21d3307 173 else
Near32 17:f360e21d3307 174 cout << "P1..." << endl;
Near32 17:f360e21d3307 175
Near32 17:f360e21d3307 176 if(abs(phi_l/phi_max) < 1.0)
Near32 22:80fdb12c167f 177 setPWM(&pw2,(float)abs((double)phi_l/phi_max));
Near32 17:f360e21d3307 178 else
Near32 17:f360e21d3307 179 pcs.printf("P2...");
Near32 17:f360e21d3307 180
Near32 17:f360e21d3307 181 pcs.printf("\n\n----------------- Commande mise executee. ------------------ \n\n");
Near32 3:573a0dc8383f 182
Jagang 0:41149573d577 183 }
Jagang 0:41149573d577 184 }
Near32 3:573a0dc8383f 185
Near32 3:573a0dc8383f 186 void measurementCallback( Mat<double>* z, Odometry* odometry)
Near32 3:573a0dc8383f 187 {
Near32 3:573a0dc8383f 188 z->set( (double)/*conversionUnitée mm */odometry->getX(), 1,1);
Near32 3:573a0dc8383f 189 z->set( (double)/*conversionUnitée mm*/odometry->getY(), 2,1);
Near32 3:573a0dc8383f 190 z->set( (double)/*conversionUnitée rad*/odometry->getTheta(), 3,1);
Near32 3:573a0dc8383f 191 }
Near32 3:573a0dc8383f 192
Near32 17:f360e21d3307 193 Mat<double> motion_bicycle3( Mat<double> state, Mat<double> command, double dt)
Near32 3:573a0dc8383f 194 {
Near32 3:573a0dc8383f 195 Mat<double> r(state);
Near32 3:573a0dc8383f 196 double v = bicycle.get(1,1)/(2*bicycle.get(3,1))*(r.get(4,1)+r.get(5,1));
Near32 17:f360e21d3307 197 double w = bicycle.get(1,1)/(2*bicycle.get(3,1))*(r.get(4,1)-r.get(5,1));
Near32 3:573a0dc8383f 198
Near32 3:573a0dc8383f 199 r.set( r.get(1,1) + v*cos(r.get(3,1))*dt, 1,1);
Near32 3:573a0dc8383f 200 r.set( r.get(2,1) + v*sin(r.get(3,1))*dt, 2,1);
Near32 3:573a0dc8383f 201
Near32 17:f360e21d3307 202 double angle = (r.get(3,1) + dt*w);
Near32 3:573a0dc8383f 203 if( angle < -PI)
Near32 3:573a0dc8383f 204 {
Near32 3:573a0dc8383f 205 angle = angle - PI*ceil(angle/PI);
Near32 3:573a0dc8383f 206 }
Near32 3:573a0dc8383f 207 else if( angle > PI)
Near32 3:573a0dc8383f 208 {
Near32 3:573a0dc8383f 209 angle = angle - PI*floor(angle/PI);
Near32 3:573a0dc8383f 210 }
Near32 3:573a0dc8383f 211
Near32 17:f360e21d3307 212 r.set( atan21(sin(angle), cos(angle)), 3,1);
Near32 3:573a0dc8383f 213
Near32 17:f360e21d3307 214
Near32 17:f360e21d3307 215 /*----------------------------------------*/
Near32 17:f360e21d3307 216 /*Modele du moteur*/
Near32 17:f360e21d3307 217 /*----------------------------------------*/
Near32 17:f360e21d3307 218 double r1 = bicycle.get(3,1)/bicycle.get(1,1)*(command.get(1,1)/bicycle.get(3,1)+command.get(2,1));
Near32 17:f360e21d3307 219 double r2 = bicycle.get(3,1)/bicycle.get(1,1)*(command.get(1,1)/bicycle.get(3,1)-command.get(2,1));
Near32 17:f360e21d3307 220
Near32 17:f360e21d3307 221
Near32 17:f360e21d3307 222 r.set( r1, 4,1);
Near32 17:f360e21d3307 223 r.set( r2, 5,1);
Near32 17:f360e21d3307 224
Near32 17:f360e21d3307 225
Near32 17:f360e21d3307 226 /*----------------------------------------*/
Near32 17:f360e21d3307 227 /*----------------------------------------*/
Near32 3:573a0dc8383f 228
Near32 3:573a0dc8383f 229 return r;
Near32 3:573a0dc8383f 230 }
Near32 3:573a0dc8383f 231
Near32 3:573a0dc8383f 232
Near32 17:f360e21d3307 233 Mat<double> sensor_bicycle3( Mat<double> state, Mat<double> command, Mat<double> d_state, double dt)
Near32 17:f360e21d3307 234 {
Near32 17:f360e21d3307 235 return state;
Near32 3:573a0dc8383f 236 }
Near32 3:573a0dc8383f 237
Near32 17:f360e21d3307 238
Near32 17:f360e21d3307 239 Mat<double> jmotion_bicycle3( Mat<double> state, Mat<double> command, double dt)
Near32 3:573a0dc8383f 240 {
Near32 3:573a0dc8383f 241 double h = numeric_limits<double>::epsilon()*10e2;
Near32 3:573a0dc8383f 242 Mat<double> var( (double)0, state.getLine(), state.getColumn());
Near32 3:573a0dc8383f 243 var.set( h, 1,1);
Near32 17:f360e21d3307 244 Mat<double> G(motion_bicycle3(state, command, dt) - motion_bicycle3(state+var, command,dt));
Near32 3:573a0dc8383f 245
Near32 3:573a0dc8383f 246 for(int i=2;i<=state.getLine();i++)
Near32 3:573a0dc8383f 247 {
Near32 3:573a0dc8383f 248 var.set( (double)0, i-1,1);
Near32 3:573a0dc8383f 249 var.set( h, i,1);
Near32 17:f360e21d3307 250 G = operatorL(G, motion_bicycle3(state, command, dt) - motion_bicycle3(state+var, command,dt) );
Near32 3:573a0dc8383f 251 }
Near32 3:573a0dc8383f 252
Near32 3:573a0dc8383f 253
Near32 3:573a0dc8383f 254 return (1.0/h)*G;
Near32 3:573a0dc8383f 255 }
Near32 3:573a0dc8383f 256
Near32 17:f360e21d3307 257 Mat<double> jsensor_bicycle3( Mat<double> state, Mat<double> command, Mat<double> d_state, double dt)
Near32 3:573a0dc8383f 258 {
Near32 3:573a0dc8383f 259 double h = numeric_limits<double>::epsilon()*10e2;
Near32 3:573a0dc8383f 260 Mat<double> var((double)0, state.getLine(), state.getColumn());
Near32 3:573a0dc8383f 261 var.set( h, 1,1);
Near32 17:f360e21d3307 262 Mat<double> H(sensor_bicycle3(state, command, d_state, dt) - sensor_bicycle3(state+var, command, d_state, dt));
Near32 3:573a0dc8383f 263
Near32 3:573a0dc8383f 264 for(int i=2;i<=state.getLine();i++)
Near32 17:f360e21d3307 265 {
Near32 3:573a0dc8383f 266 var.set( (double)0, i-1,1);
Near32 3:573a0dc8383f 267 var.set( h, i,1);
Near32 17:f360e21d3307 268 Mat<double> temp(sensor_bicycle3(state, command, d_state, dt) - sensor_bicycle3(state+var, command, d_state, dt));
Near32 17:f360e21d3307 269
Near32 17:f360e21d3307 270 H = operatorL(H, temp );
Near32 17:f360e21d3307 271 pcs.printf("sensor bicycle %d...\n",i);
Near32 3:573a0dc8383f 272 }
Near32 3:573a0dc8383f 273
Near32 3:573a0dc8383f 274
Near32 3:573a0dc8383f 275 return (1.0/h)*H;
Near32 17:f360e21d3307 276 }
Near32 17:f360e21d3307 277
Near32 17:f360e21d3307 278 bool setPWM(PwmOut *servo,float p)
Near32 17:f360e21d3307 279 {
Near32 17:f360e21d3307 280 if(p <= 1.0f && p >= 0.0f)
Near32 17:f360e21d3307 281 {
Near32 17:f360e21d3307 282 servo->write(p);
Near32 17:f360e21d3307 283 return true;
Near32 17:f360e21d3307 284 }
Near32 17:f360e21d3307 285
Near32 17:f360e21d3307 286 return false;
Near32 3:573a0dc8383f 287 }