Robot's source code

Dependencies:   mbed

Committer:
Jagang
Date:
Sun Oct 05 14:12:00 2014 +0000
Revision:
13:ae5718ac8511
Parent:
11:5513638d3c13
Child:
15:ae29e83947ff
Creating Map and Obstacle classes

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"
Jagang 13:ae5718ac8511 4 #include "Map.h"
Jagang 0:41149573d577 5
Jagang 0:41149573d577 6
Near32 3:573a0dc8383f 7 /*---------------------------------------------------------------------------------------------------------*/
Near32 3:573a0dc8383f 8 /*---------------------------------------------------------------------------------------------------------*/
Near32 3:573a0dc8383f 9 /*KalmanFilter*/
Near32 3:573a0dc8383f 10 #include "EKF.h"
Near32 3:573a0dc8383f 11 Mat<double> motion_bicycle2( Mat<double> state, Mat<double> command, double dt = 0.5);
Near32 3:573a0dc8383f 12 Mat<double> sensor_bicycle2( Mat<double> state, Mat<double> command, Mat<double> d_state, double dt = 0.5 );
Near32 3:573a0dc8383f 13 Mat<double> jmotion_bicycle2( Mat<double> state, Mat<double> command, double dt = 0.5);
Near32 3:573a0dc8383f 14 Mat<double> jsensor_bicycle2( Mat<double> state, Mat<double> command, Mat<double> d_state, double dt = 0.5);
Near32 3:573a0dc8383f 15 void measurementCallback( Mat<double>* z, Odometry* odometry);
Near32 3:573a0dc8383f 16
Near32 3:573a0dc8383f 17 Mat<double> bicycle(3,1);
Near32 3:573a0dc8383f 18 /*---------------------------------------------------------------------------------------------------------*/
Near32 3:573a0dc8383f 19 /*---------------------------------------------------------------------------------------------------------*/
Near32 3:573a0dc8383f 20
Jagang 13:ae5718ac8511 21 /* --- Initialisation de la liste des obstable --- */
Jagang 13:ae5718ac8511 22 int Obstacle::lastId = 0;
Near32 3:573a0dc8383f 23
Jagang 0:41149573d577 24 int main()
Jagang 0:41149573d577 25 {
Near32 3:573a0dc8383f 26 /*----------------------------------------------------------------------------------------------*/
Near32 3:573a0dc8383f 27 /*Odometry*/
Jagang 0:41149573d577 28 QEI qei_left(p15,p16,NC,1024,QEI::X4_ENCODING);
Jagang 0:41149573d577 29 QEI qei_right(p17,p18,NC,1024,QEI::X4_ENCODING);
Jagang 0:41149573d577 30
Jagang 0:41149573d577 31 Odometry odometry(&qei_left,&qei_right,0.07,0.07,0.26);
Near32 3:573a0dc8383f 32 /*----------------------------------------------------------------------------------------------*/
Jagang 0:41149573d577 33
Near32 3:573a0dc8383f 34
Near32 3:573a0dc8383f 35
Near32 3:573a0dc8383f 36 /*----------------------------------------------------------------------------------------------*/
Near32 3:573a0dc8383f 37 /*KalmanFilter*/
Near32 8:1150a13f6967 38 double phi_max = 100;
Near32 3:573a0dc8383f 39 /*en millimetres*/
Near32 3:573a0dc8383f 40 bicycle.set((double)100, 1,1); /*radius*/
Near32 3:573a0dc8383f 41 bicycle.set((double)100, 2,1);
Near32 3:573a0dc8383f 42 bicycle.set((double)66, 3,1); /*entre-roue*/
Near32 3:573a0dc8383f 43
Near32 3:573a0dc8383f 44 int nbrstate = 5;
Near32 3:573a0dc8383f 45 int nbrcontrol = 2;
Near32 3:573a0dc8383f 46 int nbrobs = 3;
Near32 3:573a0dc8383f 47 double dt = (double)0.05;
Near32 8:1150a13f6967 48 double stdnoise = (double)0.05;
Near32 3:573a0dc8383f 49
Near32 3:573a0dc8383f 50 Mat<double> initX((double)0, nbrstate, 1);
Near32 3:573a0dc8383f 51 initX.set( (double)0, 3,1);
Near32 3:573a0dc8383f 52
Near32 3:573a0dc8383f 53 bool extended = true;
Near32 3:573a0dc8383f 54 EKF<double> instance(nbrstate, nbrcontrol, nbrobs, dt, stdnoise, /*current state*/ initX, extended);
Near32 3:573a0dc8383f 55
Near32 3:573a0dc8383f 56 instance.initMotion(motion_bicycle2);
Near32 3:573a0dc8383f 57 instance.initSensor(sensor_bicycle2);
Near32 3:573a0dc8383f 58 instance.initJMotion(jmotion_bicycle2);
Near32 3:573a0dc8383f 59 instance.initJSensor(jsensor_bicycle2);
Near32 3:573a0dc8383f 60
Near32 3:573a0dc8383f 61 /*desired State : (x y theta phiright phileft)*/
Near32 3:573a0dc8383f 62 Mat<double> dX((double)0, nbrstate, 1);
Near32 4:4025c071b207 63 dX.set( (double)0, 1,1);
Near32 4:4025c071b207 64 dX.set( (double)10, 2,1);
Near32 4:4025c071b207 65 dX.set( (double)0, 3,1);
Near32 3:573a0dc8383f 66 dX.set( (double)0, 4,1);
Near32 3:573a0dc8383f 67 dX.set( (double)0, 5,1);
Near32 3:573a0dc8383f 68
Near32 3:573a0dc8383f 69 Mat<double> u(transpose( instance.getCommand()) );
Near32 3:573a0dc8383f 70
Near32 3:573a0dc8383f 71 /*Observations*/
Near32 3:573a0dc8383f 72 Mat<double> z(3,1);
Near32 3:573a0dc8383f 73 measurementCallback(&z, &odometry);
Near32 3:573a0dc8383f 74
Near32 3:573a0dc8383f 75 /*----------------------------------------------------------------------------------------------*/
Near32 3:573a0dc8383f 76
Near32 3:573a0dc8383f 77
Near32 3:573a0dc8383f 78
Near32 3:573a0dc8383f 79
Near32 3:573a0dc8383f 80 /*----------------------------------------------------------------------------------------------*/
Near32 3:573a0dc8383f 81 /*Serial*/
Jagang 0:41149573d577 82 Serial pc(USBTX, USBRX); // tx, rx
Near32 3:573a0dc8383f 83 /*----------------------------------------------------------------------------------------------*/
Jagang 0:41149573d577 84
Jagang 0:41149573d577 85 while(1)
Jagang 0:41149573d577 86 {
Jagang 0:41149573d577 87 wait(1);
Jagang 0:41149573d577 88 pc.printf("%f : %f : %f\n",odometry.getX()*100,odometry.getY()*100,odometry.getTheta()*180/3.14);
Near32 3:573a0dc8383f 89
Near32 3:573a0dc8383f 90
Near32 3:573a0dc8383f 91 /*------------------------------------------------------------------------------------------*/
Near32 3:573a0dc8383f 92 /*Asservissement*/
Near32 3:573a0dc8383f 93 measurementCallback(&z, &odometry);
Near32 3:573a0dc8383f 94 instance.measurement_Callback( z, dX );
Near32 3:573a0dc8383f 95 instance.state_Callback();
Near32 10:bca0274a007b 96 double phi_r = instance.getCommand().get(1,1);
Near32 10:bca0274a007b 97 double phi_l = instance.getCommand().get(2,1);
Near32 8:1150a13f6967 98
Near32 3:573a0dc8383f 99
Near32 3:573a0dc8383f 100 instance.computeCommand(dX, (double)dt, -1);
Near32 11:5513638d3c13 101 pc.printf("command : \n phi_r = %f \n phi_l = %f \n", phi_r/phi_max*100, phi_l/phi_max*100);
Near32 3:573a0dc8383f 102
Near32 3:573a0dc8383f 103 /*------------------------------------------------------------------------------------------*/
Near32 3:573a0dc8383f 104
Jagang 0:41149573d577 105 }
Jagang 0:41149573d577 106 }
Near32 3:573a0dc8383f 107
Near32 3:573a0dc8383f 108 void measurementCallback( Mat<double>* z, Odometry* odometry)
Near32 3:573a0dc8383f 109 {
Near32 3:573a0dc8383f 110 z->set( (double)/*conversionUnitée mm */odometry->getX(), 1,1);
Near32 3:573a0dc8383f 111 z->set( (double)/*conversionUnitée mm*/odometry->getY(), 2,1);
Near32 3:573a0dc8383f 112 z->set( (double)/*conversionUnitée rad*/odometry->getTheta(), 3,1);
Near32 3:573a0dc8383f 113 }
Near32 3:573a0dc8383f 114
Near32 3:573a0dc8383f 115 Mat<double> motion_bicycle2( Mat<double> state, Mat<double> command, double dt)
Near32 3:573a0dc8383f 116 {
Near32 3:573a0dc8383f 117 Mat<double> r(state);
Near32 3:573a0dc8383f 118 double v = bicycle.get(1,1)/(2*bicycle.get(3,1))*(r.get(4,1)+r.get(5,1));
Near32 3:573a0dc8383f 119 //double w = bicycle.get(1,1)/(2*bicycle.get(3,1))*(r.get(4,1)-r.get(5,1));
Near32 3:573a0dc8383f 120
Near32 3:573a0dc8383f 121 r.set( r.get(1,1) + v*cos(r.get(3,1))*dt, 1,1);
Near32 3:573a0dc8383f 122 r.set( r.get(2,1) + v*sin(r.get(3,1))*dt, 2,1);
Near32 3:573a0dc8383f 123
Near32 3:573a0dc8383f 124 double angle = (r.get(3,1) + dt/bicycle.get(3,1)*(r.get(4,1)-r.get(5,1)));
Near32 3:573a0dc8383f 125 if( angle < -PI)
Near32 3:573a0dc8383f 126 {
Near32 3:573a0dc8383f 127 angle = angle - PI*ceil(angle/PI);
Near32 3:573a0dc8383f 128 }
Near32 3:573a0dc8383f 129 else if( angle > PI)
Near32 3:573a0dc8383f 130 {
Near32 3:573a0dc8383f 131 angle = angle - PI*floor(angle/PI);
Near32 3:573a0dc8383f 132 }
Near32 3:573a0dc8383f 133
Near32 3:573a0dc8383f 134 r.set( angle, 3,1);
Near32 3:573a0dc8383f 135
Near32 3:573a0dc8383f 136 r.set( bicycle.get(3,1)/bicycle.get(1,1)*(command.get(1,1)/bicycle.get(3,1)+command.get(2,1)), 4,1);
Near32 3:573a0dc8383f 137 r.set( bicycle.get(3,1)/bicycle.get(1,1)*(command.get(1,1)/bicycle.get(3,1)-command.get(2,1)), 5,1);
Near32 3:573a0dc8383f 138
Near32 3:573a0dc8383f 139 return r;
Near32 3:573a0dc8383f 140 }
Near32 3:573a0dc8383f 141
Near32 3:573a0dc8383f 142
Near32 3:573a0dc8383f 143 Mat<double> sensor_bicycle2( Mat<double> state, Mat<double> command, Mat<double> d_state, double dt)
Near32 3:573a0dc8383f 144 {
Near32 3:573a0dc8383f 145 return extract(state-d_state, 1,1, 3,1);
Near32 3:573a0dc8383f 146 }
Near32 3:573a0dc8383f 147
Near32 3:573a0dc8383f 148 Mat<double> jmotion_bicycle2( Mat<double> state, Mat<double> command, double dt)
Near32 3:573a0dc8383f 149 {
Near32 3:573a0dc8383f 150 double h = numeric_limits<double>::epsilon()*10e2;
Near32 3:573a0dc8383f 151 Mat<double> var( (double)0, state.getLine(), state.getColumn());
Near32 3:573a0dc8383f 152 var.set( h, 1,1);
Near32 3:573a0dc8383f 153 Mat<double> G(motion_bicycle2(state, command, dt) - motion_bicycle2(state+var, command,dt));
Near32 3:573a0dc8383f 154
Near32 3:573a0dc8383f 155 for(int i=2;i<=state.getLine();i++)
Near32 3:573a0dc8383f 156 {
Near32 3:573a0dc8383f 157 var.set( (double)0, i-1,1);
Near32 3:573a0dc8383f 158 var.set( h, i,1);
Near32 3:573a0dc8383f 159 G = operatorL(G, motion_bicycle2(state, command, dt) - motion_bicycle2(state+var, command,dt) );
Near32 3:573a0dc8383f 160 }
Near32 3:573a0dc8383f 161
Near32 3:573a0dc8383f 162
Near32 3:573a0dc8383f 163 return (1.0/h)*G;
Near32 3:573a0dc8383f 164 }
Near32 3:573a0dc8383f 165
Near32 3:573a0dc8383f 166 Mat<double> jsensor_bicycle2( Mat<double> state, Mat<double> command, Mat<double> d_state, double dt)
Near32 3:573a0dc8383f 167 {
Near32 3:573a0dc8383f 168 double h = numeric_limits<double>::epsilon()*10e2;
Near32 3:573a0dc8383f 169 Mat<double> var((double)0, state.getLine(), state.getColumn());
Near32 3:573a0dc8383f 170 var.set( h, 1,1);
Near32 3:573a0dc8383f 171 Mat<double> H(sensor_bicycle2(state, command, d_state, dt) - sensor_bicycle2(state+var, command, d_state, dt));
Near32 3:573a0dc8383f 172
Near32 3:573a0dc8383f 173 for(int i=2;i<=state.getLine();i++)
Near32 3:573a0dc8383f 174 {
Near32 3:573a0dc8383f 175 var.set( (double)0, i-1,1);
Near32 3:573a0dc8383f 176 var.set( h, i,1);
Near32 3:573a0dc8383f 177 H = operatorL(H, sensor_bicycle2(state, command, d_state, dt) - sensor_bicycle2(state+var, command, d_state, dt) );
Near32 3:573a0dc8383f 178 }
Near32 3:573a0dc8383f 179
Near32 3:573a0dc8383f 180
Near32 3:573a0dc8383f 181 return (1.0/h)*H;
Near32 3:573a0dc8383f 182 }