2014 Eurobot fork
Dependencies: mbed-rtos mbed QEI
Processes/Kalman/Kalman.cpp@31:ada943ecaceb, 2013-04-10 (annotated)
- Committer:
- madcowswe
- Date:
- Wed Apr 10 18:04:47 2013 +0000
- Revision:
- 31:ada943ecaceb
- Parent:
- 29:00e1493b44f0
- Child:
- 35:f8e7f0a72a3d
Made calibration more robust
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
madcowswe | 16:52250d8d8fce | 1 | //*************************************************************************************** |
madcowswe | 16:52250d8d8fce | 2 | //Kalman Filter implementation |
madcowswe | 16:52250d8d8fce | 3 | //*************************************************************************************** |
madcowswe | 16:52250d8d8fce | 4 | #include "Kalman.h" |
madcowswe | 16:52250d8d8fce | 5 | #include "rtos.h" |
madcowswe | 16:52250d8d8fce | 6 | #include "math.h" |
madcowswe | 16:52250d8d8fce | 7 | #include "supportfuncs.h" |
madcowswe | 20:70d651156779 | 8 | #include "Encoder.h" |
madcowswe | 21:167dacfe0b14 | 9 | #include "globals.h" |
madcowswe | 21:167dacfe0b14 | 10 | #include "Printing.h" |
madcowswe | 16:52250d8d8fce | 11 | |
madcowswe | 20:70d651156779 | 12 | #include "tvmet/Matrix.h" |
madcowswe | 16:52250d8d8fce | 13 | using namespace tvmet; |
madcowswe | 16:52250d8d8fce | 14 | |
madcowswe | 16:52250d8d8fce | 15 | |
madcowswe | 16:52250d8d8fce | 16 | |
madcowswe | 16:52250d8d8fce | 17 | namespace Kalman |
madcowswe | 16:52250d8d8fce | 18 | { |
madcowswe | 16:52250d8d8fce | 19 | |
madcowswe | 20:70d651156779 | 20 | Ticker predictticker; |
madcowswe | 20:70d651156779 | 21 | |
madcowswe | 20:70d651156779 | 22 | DigitalOut OLED4(LED4); |
madcowswe | 20:70d651156779 | 23 | DigitalOut OLED1(LED1); |
madcowswe | 26:7cb3a21d9a2e | 24 | DigitalOut OLED2(LED2); |
madcowswe | 20:70d651156779 | 25 | |
madcowswe | 16:52250d8d8fce | 26 | //State variables |
madcowswe | 19:4b993a9a156e | 27 | Matrix<float, 3, 1> X; |
madcowswe | 16:52250d8d8fce | 28 | Matrix<float, 3, 3> P; |
madcowswe | 16:52250d8d8fce | 29 | Mutex statelock; |
madcowswe | 16:52250d8d8fce | 30 | |
madcowswe | 16:52250d8d8fce | 31 | float RawReadings[maxmeasure+1]; |
madcowswe | 31:ada943ecaceb | 32 | int sensorseenflags = 0; |
madcowswe | 22:6e3218cf75f8 | 33 | float IRphaseOffset; |
madcowswe | 16:52250d8d8fce | 34 | |
madcowswe | 20:70d651156779 | 35 | bool Kalman_inited = 0; |
madcowswe | 16:52250d8d8fce | 36 | |
madcowswe | 16:52250d8d8fce | 37 | struct measurmentdata { |
madcowswe | 16:52250d8d8fce | 38 | measurement_t mtype; |
madcowswe | 16:52250d8d8fce | 39 | float value; |
madcowswe | 16:52250d8d8fce | 40 | float variance; |
madcowswe | 19:4b993a9a156e | 41 | }; |
madcowswe | 16:52250d8d8fce | 42 | |
madcowswe | 16:52250d8d8fce | 43 | Mail <measurmentdata, 16> measureMQ; |
madcowswe | 16:52250d8d8fce | 44 | |
madcowswe | 20:70d651156779 | 45 | Thread* predict_thread_ptr = NULL; |
madcowswe | 16:52250d8d8fce | 46 | |
madcowswe | 16:52250d8d8fce | 47 | |
madcowswe | 16:52250d8d8fce | 48 | //Note: this init function assumes that the robot faces east, theta=0, in the +x direction |
madcowswe | 16:52250d8d8fce | 49 | void KalmanInit() |
madcowswe | 16:52250d8d8fce | 50 | { |
madcowswe | 20:70d651156779 | 51 | printf("kalmaninit \r\n"); |
madcowswe | 20:70d651156779 | 52 | |
madcowswe | 29:00e1493b44f0 | 53 | //WARNING: HARDCODED! TODO: fix it so it works for both sides! |
madcowswe | 20:70d651156779 | 54 | |
madcowswe | 31:ada943ecaceb | 55 | printf("waiting for all sonar, and at least 1 IR\r\n"); |
madcowswe | 31:ada943ecaceb | 56 | while( ((sensorseenflags & 0x7)^0x7) || !(sensorseenflags & 0x7<<3) ); |
madcowswe | 31:ada943ecaceb | 57 | |
madcowswe | 16:52250d8d8fce | 58 | //solve for our position (assume perfect bias) |
madcowswe | 20:70d651156779 | 59 | const float d = beaconpos[2].y - beaconpos[1].y; |
madcowswe | 20:70d651156779 | 60 | const float i = beaconpos[2].y - beaconpos[0].y; |
madcowswe | 20:70d651156779 | 61 | const float j = beaconpos[2].x - beaconpos[0].x; |
madcowswe | 20:70d651156779 | 62 | float r1 = RawReadings[SONAR2]; |
madcowswe | 19:4b993a9a156e | 63 | float r2 = RawReadings[SONAR1]; |
madcowswe | 20:70d651156779 | 64 | float r3 = RawReadings[SONAR0]; |
madcowswe | 20:70d651156779 | 65 | |
madcowswe | 20:70d651156779 | 66 | printf("ranges: 0: %0.4f, 1: %0.4f, 2: %0.4f \r\n", r1, r2, r3); |
madcowswe | 17:6263e90bf3ba | 67 | |
madcowswe | 19:4b993a9a156e | 68 | float y_coor = (r1*r1-r2*r2+d*d)/(2*d); |
madcowswe | 17:6263e90bf3ba | 69 | float x_coor = (r1*r1-r3*r3+i*i+j*j)/(2*j) - (i*y_coor)/j; |
madcowswe | 20:70d651156779 | 70 | |
madcowswe | 20:70d651156779 | 71 | //coordinate system hack (for now) |
madcowswe | 20:70d651156779 | 72 | x_coor = beaconpos[2].x - x_coor; |
madcowswe | 20:70d651156779 | 73 | y_coor = beaconpos[2].y - y_coor; |
madcowswe | 20:70d651156779 | 74 | |
madcowswe | 20:70d651156779 | 75 | printf("solved pos from sonar: %f, %f \r\n", x_coor, y_coor); |
madcowswe | 20:70d651156779 | 76 | |
madcowswe | 16:52250d8d8fce | 77 | //IR |
madcowswe | 16:52250d8d8fce | 78 | float IRMeasuresloc[3]; |
madcowswe | 16:52250d8d8fce | 79 | IRMeasuresloc[0] = RawReadings[IR0]; |
madcowswe | 16:52250d8d8fce | 80 | IRMeasuresloc[1] = RawReadings[IR1]; |
madcowswe | 16:52250d8d8fce | 81 | IRMeasuresloc[2] = RawReadings[IR2]; |
madcowswe | 20:70d651156779 | 82 | printf("IR 0: %0.4f, 1: %0.4f, 2: %0.4f \r\n", IRMeasuresloc[0]*180/PI, IRMeasuresloc[1]*180/PI, IRMeasuresloc[2]*180/PI); |
madcowswe | 16:52250d8d8fce | 83 | |
madcowswe | 31:ada943ecaceb | 84 | float IR_Offsets[3] = {0}; |
madcowswe | 31:ada943ecaceb | 85 | float frombrefoffset = 0; |
madcowswe | 31:ada943ecaceb | 86 | int refbeacon = 0; |
madcowswe | 31:ada943ecaceb | 87 | |
madcowswe | 31:ada943ecaceb | 88 | for (int i = 0; i < 3; i++){ |
madcowswe | 31:ada943ecaceb | 89 | if (sensorseenflags & 1<<(3+i)){ |
madcowswe | 31:ada943ecaceb | 90 | refbeacon = i; |
madcowswe | 31:ada943ecaceb | 91 | break; |
madcowswe | 31:ada943ecaceb | 92 | } |
madcowswe | 31:ada943ecaceb | 93 | } |
madcowswe | 31:ada943ecaceb | 94 | |
madcowswe | 31:ada943ecaceb | 95 | printf("refbeacon is %d\r\n", refbeacon); |
madcowswe | 31:ada943ecaceb | 96 | |
madcowswe | 31:ada943ecaceb | 97 | int cnt = 0; |
madcowswe | 16:52250d8d8fce | 98 | for (int i = 0; i < 3; i++) { |
madcowswe | 16:52250d8d8fce | 99 | |
madcowswe | 31:ada943ecaceb | 100 | if (sensorseenflags & 1<<(3+i)){ |
madcowswe | 31:ada943ecaceb | 101 | cnt++; |
madcowswe | 31:ada943ecaceb | 102 | |
madcowswe | 31:ada943ecaceb | 103 | //Compute IR offset |
madcowswe | 31:ada943ecaceb | 104 | float angle_est = atan2(beaconpos[i].y - y_coor,beaconpos[i].x - x_coor); |
madcowswe | 31:ada943ecaceb | 105 | |
madcowswe | 31:ada943ecaceb | 106 | //printf("Angle %d : %f \n\r",i,angle_est*180/PI ); |
madcowswe | 31:ada943ecaceb | 107 | IR_Offsets[i] = constrainAngle(IRMeasuresloc[i] - angle_est); |
madcowswe | 31:ada943ecaceb | 108 | |
madcowswe | 31:ada943ecaceb | 109 | frombrefoffset += constrainAngle(IR_Offsets[i] - IR_Offsets[refbeacon]); |
madcowswe | 31:ada943ecaceb | 110 | } |
madcowswe | 16:52250d8d8fce | 111 | } |
madcowswe | 20:70d651156779 | 112 | |
madcowswe | 31:ada943ecaceb | 113 | IRphaseOffset = constrainAngle(IR_Offsets[refbeacon] + frombrefoffset/cnt); |
madcowswe | 16:52250d8d8fce | 114 | |
madcowswe | 16:52250d8d8fce | 115 | //debug |
madcowswe | 22:6e3218cf75f8 | 116 | printf("Offsets IR: %0.4f\r\n",IRphaseOffset*180/PI); |
madcowswe | 16:52250d8d8fce | 117 | |
madcowswe | 16:52250d8d8fce | 118 | statelock.lock(); |
madcowswe | 26:7cb3a21d9a2e | 119 | X(0,0) = x_coor-TURRET_FWD_PLACEMENT; //assume facing east |
madcowswe | 19:4b993a9a156e | 120 | X(1,0) = y_coor; |
madcowswe | 19:4b993a9a156e | 121 | X(2,0) = 0; |
madcowswe | 21:167dacfe0b14 | 122 | |
madcowswe | 21:167dacfe0b14 | 123 | P = 0.02*0.02, 0, 0, |
madcowswe | 21:167dacfe0b14 | 124 | 0, 0.02*0.02, 0, |
madcowswe | 21:167dacfe0b14 | 125 | 0, 0, 0.04; |
madcowswe | 16:52250d8d8fce | 126 | statelock.unlock(); |
madcowswe | 20:70d651156779 | 127 | |
madcowswe | 20:70d651156779 | 128 | Kalman_inited = 1; |
madcowswe | 16:52250d8d8fce | 129 | } |
madcowswe | 16:52250d8d8fce | 130 | |
madcowswe | 20:70d651156779 | 131 | |
madcowswe | 20:70d651156779 | 132 | State getState(){ |
madcowswe | 20:70d651156779 | 133 | statelock.lock(); |
madcowswe | 20:70d651156779 | 134 | State state = {X(0,0), X(1,0), X(2,0)}; |
madcowswe | 20:70d651156779 | 135 | statelock.unlock(); |
madcowswe | 20:70d651156779 | 136 | return state; |
madcowswe | 20:70d651156779 | 137 | } |
madcowswe | 20:70d651156779 | 138 | |
madcowswe | 20:70d651156779 | 139 | |
madcowswe | 21:167dacfe0b14 | 140 | void predictloop(void const*) |
madcowswe | 16:52250d8d8fce | 141 | { |
madcowswe | 16:52250d8d8fce | 142 | |
madcowswe | 21:167dacfe0b14 | 143 | OLED4 = !Printing::registerID(0, 3); |
madcowswe | 21:167dacfe0b14 | 144 | OLED4 = !Printing::registerID(1, 4); |
madcowswe | 16:52250d8d8fce | 145 | |
madcowswe | 16:52250d8d8fce | 146 | float lastleft = 0; |
madcowswe | 16:52250d8d8fce | 147 | float lastright = 0; |
madcowswe | 16:52250d8d8fce | 148 | |
madcowswe | 16:52250d8d8fce | 149 | while (1) { |
madcowswe | 16:52250d8d8fce | 150 | Thread::signal_wait(0x1); |
madcowswe | 16:52250d8d8fce | 151 | OLED1 = !OLED1; |
madcowswe | 16:52250d8d8fce | 152 | |
madcowswe | 20:70d651156779 | 153 | float leftenc = left_encoder.getTicks() * ENCODER_M_PER_TICK; |
madcowswe | 20:70d651156779 | 154 | float rightenc = right_encoder.getTicks() * ENCODER_M_PER_TICK; |
madcowswe | 16:52250d8d8fce | 155 | |
madcowswe | 20:70d651156779 | 156 | float dleft = leftenc-lastleft; |
madcowswe | 20:70d651156779 | 157 | float dright = rightenc-lastright; |
madcowswe | 16:52250d8d8fce | 158 | |
madcowswe | 16:52250d8d8fce | 159 | lastleft = leftenc; |
madcowswe | 16:52250d8d8fce | 160 | lastright = rightenc; |
madcowswe | 16:52250d8d8fce | 161 | |
madcowswe | 16:52250d8d8fce | 162 | |
madcowswe | 16:52250d8d8fce | 163 | //The below calculation are in body frame (where +x is forward) |
madcowswe | 16:52250d8d8fce | 164 | float dxp, dyp,d,r; |
madcowswe | 20:70d651156779 | 165 | float thetap = (dright - dleft) / ENCODER_WHEELBASE; |
madcowswe | 20:70d651156779 | 166 | if (abs(thetap) < 0.01f) { //if the rotation through the integration step is small, approximate with a straight line to avoid numerical error |
madcowswe | 16:52250d8d8fce | 167 | d = (dright + dleft)/2.0f; |
madcowswe | 16:52250d8d8fce | 168 | dxp = d*cos(thetap/2.0f); |
madcowswe | 16:52250d8d8fce | 169 | dyp = d*sin(thetap/2.0f); |
madcowswe | 16:52250d8d8fce | 170 | |
madcowswe | 16:52250d8d8fce | 171 | } else { //calculate circle arc |
madcowswe | 16:52250d8d8fce | 172 | //float r = (right + left) / (4.0f * PI * thetap); |
madcowswe | 16:52250d8d8fce | 173 | r = (dright + dleft) / (2.0f*thetap); |
madcowswe | 20:70d651156779 | 174 | dxp = r*sin(thetap); |
madcowswe | 16:52250d8d8fce | 175 | dyp = r - r*cos(thetap); |
madcowswe | 16:52250d8d8fce | 176 | } |
madcowswe | 16:52250d8d8fce | 177 | |
madcowswe | 16:52250d8d8fce | 178 | statelock.lock(); |
madcowswe | 16:52250d8d8fce | 179 | |
madcowswe | 20:70d651156779 | 180 | float tempX2 = X(2,0); |
madcowswe | 16:52250d8d8fce | 181 | //rotating to cartesian frame and updating state |
madcowswe | 20:70d651156779 | 182 | X(0,0) += dxp * cos(X(2,0)) - dyp * sin(X(2,0)); |
madcowswe | 20:70d651156779 | 183 | X(1,0) += dxp * sin(X(2,0)) + dyp * cos(X(2,0)); |
madcowswe | 20:70d651156779 | 184 | X(2,0) = constrainAngle(X(2,0) + thetap); |
madcowswe | 16:52250d8d8fce | 185 | |
madcowswe | 16:52250d8d8fce | 186 | //Linearising F around X |
madcowswe | 20:70d651156779 | 187 | float avgX2 = (X(2,0) + tempX2)/2.0f; |
madcowswe | 16:52250d8d8fce | 188 | Matrix<float, 3, 3> F; |
madcowswe | 16:52250d8d8fce | 189 | F = 1, 0, (dxp * -sin(avgX2) - dyp * cos(avgX2)), |
madcowswe | 16:52250d8d8fce | 190 | 0, 1, (dxp * cos(avgX2) - dyp * sin(avgX2)), |
madcowswe | 16:52250d8d8fce | 191 | 0, 0, 1; |
madcowswe | 16:52250d8d8fce | 192 | |
madcowswe | 16:52250d8d8fce | 193 | //Generating forward and rotational variance |
madcowswe | 16:52250d8d8fce | 194 | float varfwd = fwdvarperunit * abs(dright + dleft) / 2.0f; |
madcowswe | 16:52250d8d8fce | 195 | float varang = varperang * abs(thetap); |
madcowswe | 20:70d651156779 | 196 | float varxydt = xyvarpertime * KALMAN_PREDICT_PERIOD; |
madcowswe | 20:70d651156779 | 197 | float varangdt = angvarpertime * KALMAN_PREDICT_PERIOD; |
madcowswe | 16:52250d8d8fce | 198 | |
madcowswe | 16:52250d8d8fce | 199 | //Rotating into cartesian frame |
madcowswe | 16:52250d8d8fce | 200 | Matrix<float, 2, 2> Qsub,Qsubrot,Qrot; |
madcowswe | 16:52250d8d8fce | 201 | Qsub = varfwd + varxydt, 0, |
madcowswe | 16:52250d8d8fce | 202 | 0, varxydt; |
madcowswe | 16:52250d8d8fce | 203 | |
madcowswe | 20:70d651156779 | 204 | Qrot = Rotmatrix(X(2,0)); |
madcowswe | 16:52250d8d8fce | 205 | |
madcowswe | 16:52250d8d8fce | 206 | Qsubrot = Qrot * Qsub * trans(Qrot); |
madcowswe | 16:52250d8d8fce | 207 | |
madcowswe | 16:52250d8d8fce | 208 | //Generate Q |
madcowswe | 16:52250d8d8fce | 209 | Matrix<float, 3, 3> Q;//(Qsubrot); |
madcowswe | 16:52250d8d8fce | 210 | Q = Qsubrot(0,0), Qsubrot(0,1), 0, |
madcowswe | 16:52250d8d8fce | 211 | Qsubrot(1,0), Qsubrot(1,1), 0, |
madcowswe | 16:52250d8d8fce | 212 | 0, 0, varang + varangdt; |
madcowswe | 16:52250d8d8fce | 213 | |
madcowswe | 16:52250d8d8fce | 214 | P = F * P * trans(F) + Q; |
madcowswe | 16:52250d8d8fce | 215 | |
madcowswe | 20:70d651156779 | 216 | //printf("x: %f, y: %f, t: %f\r\n", X(0,0), X(1,0), X(2,0)); |
madcowswe | 21:167dacfe0b14 | 217 | //Update Printing |
madcowswe | 21:167dacfe0b14 | 218 | float statecpy[] = {X(0,0), X(1,0), X(2,0)}; |
madcowswe | 21:167dacfe0b14 | 219 | Printing::updateval(0, statecpy, 3); |
madcowswe | 16:52250d8d8fce | 220 | |
madcowswe | 21:167dacfe0b14 | 221 | float Pcpy[] = {P(0,0), P(0,1), P(1,0), P(1,1)}; |
madcowswe | 21:167dacfe0b14 | 222 | Printing::updateval(1, Pcpy, 4); |
madcowswe | 16:52250d8d8fce | 223 | |
madcowswe | 16:52250d8d8fce | 224 | statelock.unlock(); |
madcowswe | 16:52250d8d8fce | 225 | } |
madcowswe | 16:52250d8d8fce | 226 | } |
madcowswe | 16:52250d8d8fce | 227 | |
madcowswe | 20:70d651156779 | 228 | |
madcowswe | 20:70d651156779 | 229 | void predict_event_setter(){ |
madcowswe | 20:70d651156779 | 230 | if(predict_thread_ptr) |
madcowswe | 20:70d651156779 | 231 | predict_thread_ptr->signal_set(0x1); |
madcowswe | 20:70d651156779 | 232 | else |
madcowswe | 20:70d651156779 | 233 | OLED4 = 1; |
madcowswe | 20:70d651156779 | 234 | } |
madcowswe | 20:70d651156779 | 235 | |
madcowswe | 20:70d651156779 | 236 | void start_predict_ticker(Thread* predict_thread_ptr_in){ |
madcowswe | 20:70d651156779 | 237 | predict_thread_ptr = predict_thread_ptr_in; |
madcowswe | 20:70d651156779 | 238 | predictticker.attach(predict_event_setter, KALMAN_PREDICT_PERIOD); |
madcowswe | 20:70d651156779 | 239 | } |
madcowswe | 20:70d651156779 | 240 | |
madcowswe | 20:70d651156779 | 241 | void runupdate(measurement_t type, float value, float variance) |
madcowswe | 16:52250d8d8fce | 242 | { |
madcowswe | 31:ada943ecaceb | 243 | sensorseenflags |= 1<<type; |
madcowswe | 31:ada943ecaceb | 244 | |
madcowswe | 20:70d651156779 | 245 | if (!Kalman_inited) { |
madcowswe | 16:52250d8d8fce | 246 | RawReadings[type] = value; |
madcowswe | 20:70d651156779 | 247 | } else { |
madcowswe | 17:6263e90bf3ba | 248 | |
madcowswe | 20:70d651156779 | 249 | if (type >= IR0 && type <= IR2) |
madcowswe | 22:6e3218cf75f8 | 250 | RawReadings[type] = value - IRphaseOffset; |
madcowswe | 20:70d651156779 | 251 | else |
madcowswe | 20:70d651156779 | 252 | RawReadings[type] = value; |
madcowswe | 20:70d651156779 | 253 | |
madcowswe | 17:6263e90bf3ba | 254 | |
madcowswe | 16:52250d8d8fce | 255 | measurmentdata* measured = (measurmentdata*)measureMQ.alloc(); |
madcowswe | 16:52250d8d8fce | 256 | if (measured) { |
madcowswe | 16:52250d8d8fce | 257 | measured->mtype = type; |
madcowswe | 31:ada943ecaceb | 258 | measured->value = RawReadings[type]; |
madcowswe | 16:52250d8d8fce | 259 | measured->variance = variance; |
madcowswe | 16:52250d8d8fce | 260 | |
madcowswe | 16:52250d8d8fce | 261 | osStatus putret = measureMQ.put(measured); |
madcowswe | 20:70d651156779 | 262 | //if (putret) |
madcowswe | 20:70d651156779 | 263 | //OLED4 = 1; |
madcowswe | 16:52250d8d8fce | 264 | // printf("putting in MQ error code %#x\r\n", putret); |
madcowswe | 16:52250d8d8fce | 265 | } else { |
madcowswe | 20:70d651156779 | 266 | //OLED4 = 1; |
madcowswe | 16:52250d8d8fce | 267 | //printf("MQalloc returned NULL ptr\r\n"); |
madcowswe | 16:52250d8d8fce | 268 | } |
madcowswe | 20:70d651156779 | 269 | |
madcowswe | 16:52250d8d8fce | 270 | } |
madcowswe | 20:70d651156779 | 271 | |
madcowswe | 16:52250d8d8fce | 272 | |
madcowswe | 16:52250d8d8fce | 273 | } |
madcowswe | 26:7cb3a21d9a2e | 274 | |
madcowswe | 21:167dacfe0b14 | 275 | void Kalman::updateloop(void const*) |
madcowswe | 16:52250d8d8fce | 276 | { |
madcowswe | 16:52250d8d8fce | 277 | |
madcowswe | 16:52250d8d8fce | 278 | //sonar Y chanels |
madcowswe | 26:7cb3a21d9a2e | 279 | OLED4 = !Printing::registerID(2, 1); |
madcowswe | 26:7cb3a21d9a2e | 280 | OLED4 = !Printing::registerID(3, 1); |
madcowswe | 26:7cb3a21d9a2e | 281 | OLED4 = !Printing::registerID(4, 1); |
madcowswe | 16:52250d8d8fce | 282 | |
madcowswe | 16:52250d8d8fce | 283 | //IR Y chanels |
madcowswe | 26:7cb3a21d9a2e | 284 | OLED4 = !Printing::registerID(5, 1); |
madcowswe | 26:7cb3a21d9a2e | 285 | OLED4 = !Printing::registerID(6, 1); |
madcowswe | 26:7cb3a21d9a2e | 286 | OLED4 = !Printing::registerID(7, 1); |
madcowswe | 16:52250d8d8fce | 287 | |
madcowswe | 16:52250d8d8fce | 288 | bool aborton2stddev = false; |
madcowswe | 16:52250d8d8fce | 289 | |
madcowswe | 16:52250d8d8fce | 290 | Matrix<float, 1, 3> H; |
madcowswe | 16:52250d8d8fce | 291 | |
madcowswe | 26:7cb3a21d9a2e | 292 | float Y,S; |
madcowswe | 26:7cb3a21d9a2e | 293 | const Matrix<float, 3, 3> I3( identity< Matrix<float, 3, 3> >() ); |
madcowswe | 16:52250d8d8fce | 294 | |
madcowswe | 16:52250d8d8fce | 295 | |
madcowswe | 16:52250d8d8fce | 296 | while (1) { |
madcowswe | 16:52250d8d8fce | 297 | OLED2 = !OLED2; |
madcowswe | 16:52250d8d8fce | 298 | |
madcowswe | 16:52250d8d8fce | 299 | osEvent evt = measureMQ.get(); |
madcowswe | 16:52250d8d8fce | 300 | |
madcowswe | 16:52250d8d8fce | 301 | if (evt.status == osEventMail) { |
madcowswe | 16:52250d8d8fce | 302 | |
madcowswe | 16:52250d8d8fce | 303 | measurmentdata &measured = *(measurmentdata*)evt.value.p; |
madcowswe | 26:7cb3a21d9a2e | 304 | measurement_t type = measured.mtype; //Note, may support more measurment types than sonar in the future! |
madcowswe | 26:7cb3a21d9a2e | 305 | float value = measured.value; |
madcowswe | 26:7cb3a21d9a2e | 306 | float variance = measured.variance; |
madcowswe | 16:52250d8d8fce | 307 | |
madcowswe | 16:52250d8d8fce | 308 | // don't forget to free the memory |
madcowswe | 16:52250d8d8fce | 309 | measureMQ.free(&measured); |
madcowswe | 16:52250d8d8fce | 310 | |
madcowswe | 16:52250d8d8fce | 311 | if (type <= maxmeasure) { |
madcowswe | 16:52250d8d8fce | 312 | |
madcowswe | 26:7cb3a21d9a2e | 313 | if (type <= SONAR2) { |
madcowswe | 16:52250d8d8fce | 314 | |
madcowswe | 26:7cb3a21d9a2e | 315 | float dist = value; |
madcowswe | 16:52250d8d8fce | 316 | int sonarid = type; |
madcowswe | 16:52250d8d8fce | 317 | aborton2stddev = true; |
madcowswe | 16:52250d8d8fce | 318 | |
madcowswe | 16:52250d8d8fce | 319 | statelock.lock(); |
madcowswe | 26:7cb3a21d9a2e | 320 | |
madcowswe | 26:7cb3a21d9a2e | 321 | float fp_ct = TURRET_FWD_PLACEMENT*cos(X(2,0)); |
madcowswe | 26:7cb3a21d9a2e | 322 | float fp_st = TURRET_FWD_PLACEMENT*sin(X(2,0)); |
madcowswe | 26:7cb3a21d9a2e | 323 | |
madcowswe | 26:7cb3a21d9a2e | 324 | float rbx = X(0,0) + fp_ct - beaconpos[sonarid].x; |
madcowswe | 26:7cb3a21d9a2e | 325 | float rby = X(1,0) + fp_st - beaconpos[sonarid].y; |
madcowswe | 26:7cb3a21d9a2e | 326 | |
madcowswe | 26:7cb3a21d9a2e | 327 | float expecdist = hypot(rbx, rby);//sqrt(rbx*rbx + rby*rby); |
madcowswe | 16:52250d8d8fce | 328 | Y = dist - expecdist; |
madcowswe | 16:52250d8d8fce | 329 | |
madcowswe | 16:52250d8d8fce | 330 | //send to ui |
madcowswe | 26:7cb3a21d9a2e | 331 | Printing::updateval(sonarid+2, Y); |
madcowswe | 16:52250d8d8fce | 332 | |
madcowswe | 26:7cb3a21d9a2e | 333 | float r_expecdist = 1.0f/expecdist; |
madcowswe | 16:52250d8d8fce | 334 | |
madcowswe | 26:7cb3a21d9a2e | 335 | float dhdx = rbx * r_expecdist; |
madcowswe | 26:7cb3a21d9a2e | 336 | float dhdy = rby * r_expecdist; |
madcowswe | 27:664e81033846 | 337 | float dhdt = fp_ct*dhdy - fp_st*dhdx; |
madcowswe | 16:52250d8d8fce | 338 | |
madcowswe | 26:7cb3a21d9a2e | 339 | H = dhdx, dhdy, dhdt; |
madcowswe | 16:52250d8d8fce | 340 | |
madcowswe | 26:7cb3a21d9a2e | 341 | } else if (type <= IR2) { |
madcowswe | 26:7cb3a21d9a2e | 342 | |
madcowswe | 26:7cb3a21d9a2e | 343 | aborton2stddev = true; |
madcowswe | 16:52250d8d8fce | 344 | int IRidx = type-3; |
madcowswe | 16:52250d8d8fce | 345 | |
madcowswe | 26:7cb3a21d9a2e | 346 | statelock.lock(); |
madcowswe | 26:7cb3a21d9a2e | 347 | |
madcowswe | 26:7cb3a21d9a2e | 348 | float fp_ct = TURRET_FWD_PLACEMENT*cos(X(2,0)); |
madcowswe | 26:7cb3a21d9a2e | 349 | float fp_st = TURRET_FWD_PLACEMENT*sin(X(2,0)); |
madcowswe | 16:52250d8d8fce | 350 | |
madcowswe | 26:7cb3a21d9a2e | 351 | float brx = beaconpos[IRidx].x - (X(0,0) + fp_ct); |
madcowswe | 26:7cb3a21d9a2e | 352 | float bry = beaconpos[IRidx].y - (X(1,0) + fp_st); |
madcowswe | 16:52250d8d8fce | 353 | |
madcowswe | 26:7cb3a21d9a2e | 354 | float expecang = atan2(bry, brx) - X(2,0); //constrainAngle can be called late |
madcowswe | 26:7cb3a21d9a2e | 355 | Y = constrainAngle(value - expecang); |
madcowswe | 16:52250d8d8fce | 356 | |
madcowswe | 16:52250d8d8fce | 357 | //send to ui |
madcowswe | 26:7cb3a21d9a2e | 358 | Printing::updateval(IRidx + 5, Y); |
madcowswe | 16:52250d8d8fce | 359 | |
madcowswe | 26:7cb3a21d9a2e | 360 | float r_dstsq = 1.0f/(brx*brx + bry*bry); |
madcowswe | 26:7cb3a21d9a2e | 361 | float dhdx = -bry*r_dstsq; |
madcowswe | 26:7cb3a21d9a2e | 362 | float dhdy = brx*r_dstsq; |
madcowswe | 26:7cb3a21d9a2e | 363 | float dhdt = fp_ct*dhdy - fp_st*dhdx - 1.0f; |
madcowswe | 26:7cb3a21d9a2e | 364 | H = dhdx, dhdy, dhdt; |
madcowswe | 16:52250d8d8fce | 365 | } |
madcowswe | 16:52250d8d8fce | 366 | |
madcowswe | 16:52250d8d8fce | 367 | Matrix<float, 3, 1> PH (P * trans(H)); |
madcowswe | 31:ada943ecaceb | 368 | S = (H * PH)(0,0) + variance*10; //TODO: temp hack |
madcowswe | 16:52250d8d8fce | 369 | |
madcowswe | 16:52250d8d8fce | 370 | if (aborton2stddev && Y*Y > 4 * S) { |
madcowswe | 16:52250d8d8fce | 371 | statelock.unlock(); |
madcowswe | 16:52250d8d8fce | 372 | continue; |
madcowswe | 16:52250d8d8fce | 373 | } |
madcowswe | 16:52250d8d8fce | 374 | |
madcowswe | 16:52250d8d8fce | 375 | Matrix<float, 3, 1> K (PH * (1/S)); |
madcowswe | 16:52250d8d8fce | 376 | |
madcowswe | 16:52250d8d8fce | 377 | //Updating state |
madcowswe | 26:7cb3a21d9a2e | 378 | X += K * Y; |
madcowswe | 26:7cb3a21d9a2e | 379 | X(2,0) = constrainAngle(X(2,0)); |
madcowswe | 16:52250d8d8fce | 380 | |
madcowswe | 16:52250d8d8fce | 381 | P = (I3 - K * H) * P; |
madcowswe | 16:52250d8d8fce | 382 | |
madcowswe | 16:52250d8d8fce | 383 | statelock.unlock(); |
madcowswe | 16:52250d8d8fce | 384 | |
madcowswe | 16:52250d8d8fce | 385 | } |
madcowswe | 16:52250d8d8fce | 386 | |
madcowswe | 16:52250d8d8fce | 387 | } else { |
madcowswe | 16:52250d8d8fce | 388 | OLED4 = 1; |
madcowswe | 16:52250d8d8fce | 389 | //printf("ERROR: in updateloop, code %#x", evt); |
madcowswe | 16:52250d8d8fce | 390 | } |
madcowswe | 16:52250d8d8fce | 391 | |
madcowswe | 16:52250d8d8fce | 392 | } |
madcowswe | 16:52250d8d8fce | 393 | |
madcowswe | 16:52250d8d8fce | 394 | } |
madcowswe | 16:52250d8d8fce | 395 | |
madcowswe | 19:4b993a9a156e | 396 | |
madcowswe | 16:52250d8d8fce | 397 | } //Kalman Namespace |