2014 Eurobot fork

Dependencies:   mbed-rtos mbed QEI

Committer:
madcowswe
Date:
Fri Apr 12 21:07:00 2013 +0000
Revision:
49:665bdca0f2cd
Parent:
48:254b124cef02
Child:
72:7996aa8286ae
Kalman online phase estimation works but is SOOO hacked together

Who changed what in which revision?

UserRevisionLine numberNew 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
rsavitski 38:c9058a401410 22 DigitalOut OLED4(LED4);
madcowswe 48:254b124cef02 23 DigitalOut OLED3(LED3);
rsavitski 38:c9058a401410 24 DigitalOut OLED1(LED1);
madcowswe 26:7cb3a21d9a2e 25 DigitalOut OLED2(LED2);
madcowswe 20:70d651156779 26
madcowswe 16:52250d8d8fce 27 //State variables
madcowswe 48:254b124cef02 28 Matrix<float, 4, 1> X;
madcowswe 48:254b124cef02 29 Matrix<float, 4, 4> P;
madcowswe 16:52250d8d8fce 30 Mutex statelock;
madcowswe 16:52250d8d8fce 31
madcowswe 16:52250d8d8fce 32 float RawReadings[maxmeasure+1];
madcowswe 49:665bdca0f2cd 33 volatile int sensorseenflags = 0;
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 48:254b124cef02 52
madcowswe 29:00e1493b44f0 53 //WARNING: HARDCODED! TODO: fix it so it works for both sides!
madcowswe 48:254b124cef02 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 48:254b124cef02 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 48:254b124cef02 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 48:254b124cef02 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 48:254b124cef02 74
madcowswe 20:70d651156779 75 printf("solved pos from sonar: %f, %f \r\n", x_coor, y_coor);
madcowswe 48:254b124cef02 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 48:254b124cef02 87
madcowswe 48:254b124cef02 88 for (int i = 0; i < 3; i++) {
madcowswe 48:254b124cef02 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 48:254b124cef02 94
madcowswe 31:ada943ecaceb 95 printf("refbeacon is %d\r\n", refbeacon);
madcowswe 48:254b124cef02 96
madcowswe 31:ada943ecaceb 97 int cnt = 0;
madcowswe 16:52250d8d8fce 98 for (int i = 0; i < 3; i++) {
madcowswe 16:52250d8d8fce 99
madcowswe 48:254b124cef02 100 if (sensorseenflags & 1<<(3+i)) {
madcowswe 31:ada943ecaceb 101 cnt++;
madcowswe 48:254b124cef02 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 48:254b124cef02 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 48:254b124cef02 108
madcowswe 31:ada943ecaceb 109 frombrefoffset += constrainAngle(IR_Offsets[i] - IR_Offsets[refbeacon]);
madcowswe 31:ada943ecaceb 110 }
madcowswe 16:52250d8d8fce 111 }
madcowswe 49:665bdca0f2cd 112
madcowswe 49:665bdca0f2cd 113 printf("Used IR info from %d beacons\r\n", cnt);
madcowswe 20:70d651156779 114
madcowswe 48:254b124cef02 115 X(3,0) = constrainAngle(IR_Offsets[refbeacon] + frombrefoffset/cnt);
madcowswe 16:52250d8d8fce 116
madcowswe 16:52250d8d8fce 117 //debug
madcowswe 48:254b124cef02 118 printf("Offsets IR: %0.4f\r\n",X(3,0)*180/PI);
madcowswe 16:52250d8d8fce 119
madcowswe 16:52250d8d8fce 120 statelock.lock();
madcowswe 48:254b124cef02 121 X(0,0) = x_coor-TURRET_FWD_PLACEMENT;
madcowswe 19:4b993a9a156e 122 X(1,0) = y_coor;
madcowswe 48:254b124cef02 123 X(2,0) = 0; //TODO: assume facing east, need to account for both starting pos
madcowswe 48:254b124cef02 124
madcowswe 48:254b124cef02 125 P = 0.02*0.02, 0, 0, 0,
madcowswe 48:254b124cef02 126 0, 0.02*0.02, 0, 0,
madcowswe 48:254b124cef02 127 0, 0, 0.4*0.4, -0.4*0.4,
madcowswe 49:665bdca0f2cd 128 0, 0, -0.4*0.4, 0.4*0.4 + 0.05*0.05;
madcowswe 48:254b124cef02 129
madcowswe 16:52250d8d8fce 130 statelock.unlock();
madcowswe 48:254b124cef02 131
madcowswe 20:70d651156779 132 Kalman_inited = 1;
madcowswe 16:52250d8d8fce 133 }
madcowswe 16:52250d8d8fce 134
madcowswe 20:70d651156779 135
madcowswe 48:254b124cef02 136 State getState()
madcowswe 48:254b124cef02 137 {
madcowswe 20:70d651156779 138 statelock.lock();
madcowswe 20:70d651156779 139 State state = {X(0,0), X(1,0), X(2,0)};
madcowswe 20:70d651156779 140 statelock.unlock();
madcowswe 20:70d651156779 141 return state;
madcowswe 20:70d651156779 142 }
madcowswe 20:70d651156779 143
madcowswe 20:70d651156779 144
madcowswe 21:167dacfe0b14 145 void predictloop(void const*)
madcowswe 16:52250d8d8fce 146 {
madcowswe 16:52250d8d8fce 147
madcowswe 49:665bdca0f2cd 148 OLED4 = !Printing::registerID(0, 3) || OLED4;
madcowswe 49:665bdca0f2cd 149 OLED4 = !Printing::registerID(1, 4) || OLED4;
madcowswe 49:665bdca0f2cd 150 OLED4 = !Printing::registerID(8, 1) || OLED4;
madcowswe 16:52250d8d8fce 151
madcowswe 16:52250d8d8fce 152 float lastleft = 0;
madcowswe 16:52250d8d8fce 153 float lastright = 0;
madcowswe 16:52250d8d8fce 154
madcowswe 16:52250d8d8fce 155 while (1) {
madcowswe 16:52250d8d8fce 156 Thread::signal_wait(0x1);
madcowswe 16:52250d8d8fce 157 OLED1 = !OLED1;
madcowswe 16:52250d8d8fce 158
madcowswe 20:70d651156779 159 float leftenc = left_encoder.getTicks() * ENCODER_M_PER_TICK;
madcowswe 20:70d651156779 160 float rightenc = right_encoder.getTicks() * ENCODER_M_PER_TICK;
madcowswe 16:52250d8d8fce 161
madcowswe 20:70d651156779 162 float dleft = leftenc-lastleft;
madcowswe 20:70d651156779 163 float dright = rightenc-lastright;
madcowswe 16:52250d8d8fce 164
madcowswe 16:52250d8d8fce 165 lastleft = leftenc;
madcowswe 16:52250d8d8fce 166 lastright = rightenc;
madcowswe 16:52250d8d8fce 167
madcowswe 16:52250d8d8fce 168
madcowswe 16:52250d8d8fce 169 //The below calculation are in body frame (where +x is forward)
madcowswe 16:52250d8d8fce 170 float dxp, dyp,d,r;
madcowswe 20:70d651156779 171 float thetap = (dright - dleft) / ENCODER_WHEELBASE;
madcowswe 20:70d651156779 172 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 173 d = (dright + dleft)/2.0f;
madcowswe 16:52250d8d8fce 174 dxp = d*cos(thetap/2.0f);
madcowswe 16:52250d8d8fce 175 dyp = d*sin(thetap/2.0f);
madcowswe 16:52250d8d8fce 176
madcowswe 16:52250d8d8fce 177 } else { //calculate circle arc
madcowswe 16:52250d8d8fce 178 //float r = (right + left) / (4.0f * PI * thetap);
madcowswe 16:52250d8d8fce 179 r = (dright + dleft) / (2.0f*thetap);
madcowswe 20:70d651156779 180 dxp = r*sin(thetap);
madcowswe 16:52250d8d8fce 181 dyp = r - r*cos(thetap);
madcowswe 16:52250d8d8fce 182 }
madcowswe 16:52250d8d8fce 183
madcowswe 16:52250d8d8fce 184 statelock.lock();
madcowswe 16:52250d8d8fce 185
madcowswe 20:70d651156779 186 float tempX2 = X(2,0);
madcowswe 16:52250d8d8fce 187 //rotating to cartesian frame and updating state
madcowswe 20:70d651156779 188 X(0,0) += dxp * cos(X(2,0)) - dyp * sin(X(2,0));
madcowswe 20:70d651156779 189 X(1,0) += dxp * sin(X(2,0)) + dyp * cos(X(2,0));
madcowswe 20:70d651156779 190 X(2,0) = constrainAngle(X(2,0) + thetap);
madcowswe 48:254b124cef02 191 //X(3,0) += 0;
madcowswe 16:52250d8d8fce 192
madcowswe 16:52250d8d8fce 193 //Linearising F around X
madcowswe 48:254b124cef02 194 Matrix<float, 4, 4> F;
madcowswe 48:254b124cef02 195 F = 1, 0, (dxp * -sin(tempX2) - dyp * cos(tempX2)), 0,
madcowswe 48:254b124cef02 196 0, 1, (dxp * cos(tempX2) - dyp * sin(tempX2)), 0,
madcowswe 48:254b124cef02 197 0, 0, 1, 0,
madcowswe 48:254b124cef02 198 0, 0, 0, 1;
madcowswe 16:52250d8d8fce 199
madcowswe 16:52250d8d8fce 200 //Generating forward and rotational variance
madcowswe 16:52250d8d8fce 201 float varfwd = fwdvarperunit * abs(dright + dleft) / 2.0f;
madcowswe 16:52250d8d8fce 202 float varang = varperang * abs(thetap);
madcowswe 20:70d651156779 203 float varxydt = xyvarpertime * KALMAN_PREDICT_PERIOD;
madcowswe 20:70d651156779 204 float varangdt = angvarpertime * KALMAN_PREDICT_PERIOD;
madcowswe 16:52250d8d8fce 205
madcowswe 16:52250d8d8fce 206 //Rotating into cartesian frame
madcowswe 16:52250d8d8fce 207 Matrix<float, 2, 2> Qsub,Qsubrot,Qrot;
madcowswe 16:52250d8d8fce 208 Qsub = varfwd + varxydt, 0,
madcowswe 16:52250d8d8fce 209 0, varxydt;
madcowswe 16:52250d8d8fce 210
madcowswe 20:70d651156779 211 Qrot = Rotmatrix(X(2,0));
madcowswe 16:52250d8d8fce 212
madcowswe 16:52250d8d8fce 213 Qsubrot = Qrot * Qsub * trans(Qrot);
madcowswe 16:52250d8d8fce 214
madcowswe 16:52250d8d8fce 215 //Generate Q
madcowswe 48:254b124cef02 216 Matrix<float, 4, 4> Q;//(Qsubrot);
madcowswe 48:254b124cef02 217 Q = Qsubrot(0,0), Qsubrot(0,1), 0, 0,
madcowswe 48:254b124cef02 218 Qsubrot(1,0), Qsubrot(1,1), 0, 0,
madcowswe 48:254b124cef02 219 0, 0, varang + varangdt, 0,
madcowswe 48:254b124cef02 220 0, 0, 0, 0;
madcowswe 16:52250d8d8fce 221
madcowswe 16:52250d8d8fce 222 P = F * P * trans(F) + Q;
madcowswe 16:52250d8d8fce 223
madcowswe 20:70d651156779 224 //printf("x: %f, y: %f, t: %f\r\n", X(0,0), X(1,0), X(2,0));
madcowswe 21:167dacfe0b14 225 //Update Printing
madcowswe 21:167dacfe0b14 226 float statecpy[] = {X(0,0), X(1,0), X(2,0)};
madcowswe 21:167dacfe0b14 227 Printing::updateval(0, statecpy, 3);
madcowswe 49:665bdca0f2cd 228
madcowswe 49:665bdca0f2cd 229 Printing::updateval(8, X(3,0));
madcowswe 16:52250d8d8fce 230
madcowswe 21:167dacfe0b14 231 float Pcpy[] = {P(0,0), P(0,1), P(1,0), P(1,1)};
madcowswe 21:167dacfe0b14 232 Printing::updateval(1, Pcpy, 4);
madcowswe 16:52250d8d8fce 233
madcowswe 16:52250d8d8fce 234 statelock.unlock();
madcowswe 16:52250d8d8fce 235 }
madcowswe 16:52250d8d8fce 236 }
madcowswe 16:52250d8d8fce 237
madcowswe 20:70d651156779 238
madcowswe 48:254b124cef02 239 void predict_event_setter()
madcowswe 48:254b124cef02 240 {
madcowswe 20:70d651156779 241 if(predict_thread_ptr)
madcowswe 20:70d651156779 242 predict_thread_ptr->signal_set(0x1);
madcowswe 20:70d651156779 243 else
madcowswe 20:70d651156779 244 OLED4 = 1;
madcowswe 20:70d651156779 245 }
madcowswe 20:70d651156779 246
madcowswe 48:254b124cef02 247 void start_predict_ticker(Thread* predict_thread_ptr_in)
madcowswe 48:254b124cef02 248 {
madcowswe 20:70d651156779 249 predict_thread_ptr = predict_thread_ptr_in;
madcowswe 20:70d651156779 250 predictticker.attach(predict_event_setter, KALMAN_PREDICT_PERIOD);
madcowswe 20:70d651156779 251 }
madcowswe 20:70d651156779 252
madcowswe 20:70d651156779 253 void runupdate(measurement_t type, float value, float variance)
madcowswe 16:52250d8d8fce 254 {
madcowswe 31:ada943ecaceb 255 sensorseenflags |= 1<<type;
madcowswe 49:665bdca0f2cd 256 RawReadings[type] = value;
madcowswe 31:ada943ecaceb 257
madcowswe 48:254b124cef02 258 if (Kalman_inited) {
madcowswe 16:52250d8d8fce 259 measurmentdata* measured = (measurmentdata*)measureMQ.alloc();
madcowswe 16:52250d8d8fce 260 if (measured) {
madcowswe 16:52250d8d8fce 261 measured->mtype = type;
madcowswe 31:ada943ecaceb 262 measured->value = RawReadings[type];
madcowswe 16:52250d8d8fce 263 measured->variance = variance;
madcowswe 16:52250d8d8fce 264
madcowswe 16:52250d8d8fce 265 osStatus putret = measureMQ.put(measured);
madcowswe 20:70d651156779 266 //if (putret)
madcowswe 48:254b124cef02 267 //OLED4 = 1;
madcowswe 16:52250d8d8fce 268 // printf("putting in MQ error code %#x\r\n", putret);
madcowswe 16:52250d8d8fce 269 } else {
madcowswe 20:70d651156779 270 //OLED4 = 1;
madcowswe 16:52250d8d8fce 271 //printf("MQalloc returned NULL ptr\r\n");
madcowswe 16:52250d8d8fce 272 }
madcowswe 48:254b124cef02 273
madcowswe 16:52250d8d8fce 274 }
madcowswe 48:254b124cef02 275
madcowswe 16:52250d8d8fce 276
madcowswe 16:52250d8d8fce 277 }
madcowswe 26:7cb3a21d9a2e 278
madcowswe 21:167dacfe0b14 279 void Kalman::updateloop(void const*)
madcowswe 16:52250d8d8fce 280 {
madcowswe 16:52250d8d8fce 281
madcowswe 16:52250d8d8fce 282 //sonar Y chanels
madcowswe 26:7cb3a21d9a2e 283 OLED4 = !Printing::registerID(2, 1);
madcowswe 26:7cb3a21d9a2e 284 OLED4 = !Printing::registerID(3, 1);
madcowswe 26:7cb3a21d9a2e 285 OLED4 = !Printing::registerID(4, 1);
madcowswe 16:52250d8d8fce 286
madcowswe 16:52250d8d8fce 287 //IR Y chanels
madcowswe 26:7cb3a21d9a2e 288 OLED4 = !Printing::registerID(5, 1);
madcowswe 26:7cb3a21d9a2e 289 OLED4 = !Printing::registerID(6, 1);
madcowswe 26:7cb3a21d9a2e 290 OLED4 = !Printing::registerID(7, 1);
madcowswe 16:52250d8d8fce 291
madcowswe 16:52250d8d8fce 292 bool aborton2stddev = false;
madcowswe 16:52250d8d8fce 293
madcowswe 48:254b124cef02 294 Matrix<float, 1, 4> H;
madcowswe 16:52250d8d8fce 295
madcowswe 26:7cb3a21d9a2e 296 float Y,S;
madcowswe 48:254b124cef02 297 const Matrix<float, 4, 4> I4( identity< Matrix<float, 4, 4> >() );
madcowswe 16:52250d8d8fce 298
madcowswe 16:52250d8d8fce 299
madcowswe 16:52250d8d8fce 300 while (1) {
madcowswe 16:52250d8d8fce 301 OLED2 = !OLED2;
madcowswe 16:52250d8d8fce 302
madcowswe 16:52250d8d8fce 303 osEvent evt = measureMQ.get();
madcowswe 16:52250d8d8fce 304
madcowswe 16:52250d8d8fce 305 if (evt.status == osEventMail) {
madcowswe 16:52250d8d8fce 306
madcowswe 16:52250d8d8fce 307 measurmentdata &measured = *(measurmentdata*)evt.value.p;
madcowswe 26:7cb3a21d9a2e 308 measurement_t type = measured.mtype; //Note, may support more measurment types than sonar in the future!
madcowswe 26:7cb3a21d9a2e 309 float value = measured.value;
madcowswe 26:7cb3a21d9a2e 310 float variance = measured.variance;
madcowswe 16:52250d8d8fce 311
madcowswe 16:52250d8d8fce 312 // don't forget to free the memory
madcowswe 16:52250d8d8fce 313 measureMQ.free(&measured);
madcowswe 16:52250d8d8fce 314
madcowswe 16:52250d8d8fce 315 if (type <= maxmeasure) {
madcowswe 16:52250d8d8fce 316
madcowswe 26:7cb3a21d9a2e 317 if (type <= SONAR2) {
madcowswe 16:52250d8d8fce 318
madcowswe 26:7cb3a21d9a2e 319 float dist = value;
madcowswe 16:52250d8d8fce 320 int sonarid = type;
madcowswe 16:52250d8d8fce 321 aborton2stddev = true;
madcowswe 16:52250d8d8fce 322
madcowswe 16:52250d8d8fce 323 statelock.lock();
madcowswe 48:254b124cef02 324
madcowswe 26:7cb3a21d9a2e 325 float fp_ct = TURRET_FWD_PLACEMENT*cos(X(2,0));
madcowswe 26:7cb3a21d9a2e 326 float fp_st = TURRET_FWD_PLACEMENT*sin(X(2,0));
madcowswe 48:254b124cef02 327
madcowswe 26:7cb3a21d9a2e 328 float rbx = X(0,0) + fp_ct - beaconpos[sonarid].x;
madcowswe 26:7cb3a21d9a2e 329 float rby = X(1,0) + fp_st - beaconpos[sonarid].y;
madcowswe 48:254b124cef02 330
madcowswe 26:7cb3a21d9a2e 331 float expecdist = hypot(rbx, rby);//sqrt(rbx*rbx + rby*rby);
madcowswe 16:52250d8d8fce 332 Y = dist - expecdist;
madcowswe 16:52250d8d8fce 333
madcowswe 16:52250d8d8fce 334 //send to ui
madcowswe 26:7cb3a21d9a2e 335 Printing::updateval(sonarid+2, Y);
madcowswe 16:52250d8d8fce 336
madcowswe 26:7cb3a21d9a2e 337 float r_expecdist = 1.0f/expecdist;
madcowswe 16:52250d8d8fce 338
madcowswe 26:7cb3a21d9a2e 339 float dhdx = rbx * r_expecdist;
madcowswe 26:7cb3a21d9a2e 340 float dhdy = rby * r_expecdist;
madcowswe 27:664e81033846 341 float dhdt = fp_ct*dhdy - fp_st*dhdx;
madcowswe 16:52250d8d8fce 342
madcowswe 48:254b124cef02 343 H = dhdx, dhdy, dhdt, 0;
madcowswe 16:52250d8d8fce 344
madcowswe 26:7cb3a21d9a2e 345 } else if (type <= IR2) {
madcowswe 26:7cb3a21d9a2e 346
madcowswe 26:7cb3a21d9a2e 347 aborton2stddev = true;
madcowswe 16:52250d8d8fce 348 int IRidx = type-3;
madcowswe 16:52250d8d8fce 349
madcowswe 26:7cb3a21d9a2e 350 statelock.lock();
madcowswe 48:254b124cef02 351
madcowswe 26:7cb3a21d9a2e 352 float fp_ct = TURRET_FWD_PLACEMENT*cos(X(2,0));
madcowswe 26:7cb3a21d9a2e 353 float fp_st = TURRET_FWD_PLACEMENT*sin(X(2,0));
madcowswe 16:52250d8d8fce 354
madcowswe 26:7cb3a21d9a2e 355 float brx = beaconpos[IRidx].x - (X(0,0) + fp_ct);
madcowswe 26:7cb3a21d9a2e 356 float bry = beaconpos[IRidx].y - (X(1,0) + fp_st);
madcowswe 16:52250d8d8fce 357
madcowswe 48:254b124cef02 358 float expecang = atan2(bry, brx) - X(2,0) + X(3,0); //constrainAngle can be called late
madcowswe 26:7cb3a21d9a2e 359 Y = constrainAngle(value - expecang);
madcowswe 16:52250d8d8fce 360
madcowswe 16:52250d8d8fce 361 //send to ui
madcowswe 26:7cb3a21d9a2e 362 Printing::updateval(IRidx + 5, Y);
madcowswe 16:52250d8d8fce 363
madcowswe 26:7cb3a21d9a2e 364 float r_dstsq = 1.0f/(brx*brx + bry*bry);
madcowswe 26:7cb3a21d9a2e 365 float dhdx = -bry*r_dstsq;
madcowswe 26:7cb3a21d9a2e 366 float dhdy = brx*r_dstsq;
madcowswe 26:7cb3a21d9a2e 367 float dhdt = fp_ct*dhdy - fp_st*dhdx - 1.0f;
madcowswe 48:254b124cef02 368 float dhdp = 1;
madcowswe 48:254b124cef02 369 H = dhdx, dhdy, dhdt, dhdp;
madcowswe 16:52250d8d8fce 370 }
madcowswe 16:52250d8d8fce 371
madcowswe 48:254b124cef02 372 Matrix<float, 4, 1> PHt (P * trans(H));
madcowswe 49:665bdca0f2cd 373 S = (H * PHt)(0,0) + variance*10; //TODO: Temp Hack!
madcowswe 16:52250d8d8fce 374
madcowswe 48:254b124cef02 375 OLED3 = 0;
madcowswe 16:52250d8d8fce 376 if (aborton2stddev && Y*Y > 4 * S) {
madcowswe 48:254b124cef02 377 OLED3 = 1;
madcowswe 16:52250d8d8fce 378 statelock.unlock();
madcowswe 16:52250d8d8fce 379 continue;
madcowswe 16:52250d8d8fce 380 }
madcowswe 16:52250d8d8fce 381
madcowswe 48:254b124cef02 382 Matrix<float, 4, 1> K (PHt * (1/S));
madcowswe 16:52250d8d8fce 383
madcowswe 16:52250d8d8fce 384 //Updating state
madcowswe 26:7cb3a21d9a2e 385 X += K * Y;
madcowswe 26:7cb3a21d9a2e 386 X(2,0) = constrainAngle(X(2,0));
madcowswe 48:254b124cef02 387 X(3,0) = constrainAngle(X(3,0));
madcowswe 16:52250d8d8fce 388
madcowswe 48:254b124cef02 389 P = (I4 - K * H) * P;
madcowswe 16:52250d8d8fce 390
madcowswe 16:52250d8d8fce 391 statelock.unlock();
madcowswe 16:52250d8d8fce 392
madcowswe 16:52250d8d8fce 393 }
madcowswe 16:52250d8d8fce 394
madcowswe 16:52250d8d8fce 395 } else {
madcowswe 16:52250d8d8fce 396 OLED4 = 1;
madcowswe 16:52250d8d8fce 397 //printf("ERROR: in updateloop, code %#x", evt);
madcowswe 16:52250d8d8fce 398 }
madcowswe 16:52250d8d8fce 399
madcowswe 16:52250d8d8fce 400 }
madcowswe 16:52250d8d8fce 401
madcowswe 16:52250d8d8fce 402 }
madcowswe 16:52250d8d8fce 403
madcowswe 19:4b993a9a156e 404
madcowswe 16:52250d8d8fce 405 } //Kalman Namespace