Robot's source code

Dependencies:   mbed

Committer:
Near32
Date:
Fri Oct 03 14:11:17 2014 +0000
Revision:
10:bca0274a007b
Parent:
8:1150a13f6967
Child:
11:5513638d3c13
coefficient

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