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