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