PX4 Firmware
PX4 Autopilot Software http://px4.io
calibration_routines.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file calibration_routines.cpp
36  * Calibration routines implementations.
37  *
38  * @author Lorenz Meier <lm@inf.ethz.ch>
39  */
40 
41 #include <px4_platform_common/defines.h>
42 #include <px4_platform_common/posix.h>
43 #include <px4_platform_common/time.h>
44 #include <stdio.h>
45 #include <unistd.h>
46 #include <math.h>
47 #include <float.h>
48 #include <poll.h>
49 #include <drivers/drv_hrt.h>
50 #include <systemlib/mavlink_log.h>
51 #include <lib/ecl/geo/geo.h>
52 #include <string.h>
53 #include <mathlib/mathlib.h>
54 #include <matrix/math.hpp>
55 
58 
59 #include <drivers/drv_tone_alarm.h>
60 
61 #include "calibration_routines.h"
62 #include "calibration_messages.h"
63 #include "commander_helper.h"
64 
65 int sphere_fit_least_squares(const float x[], const float y[], const float z[],
66  unsigned int size, unsigned int max_iterations, float delta, float *sphere_x, float *sphere_y, float *sphere_z,
67  float *sphere_radius)
68 {
69 
70  float x_sumplain = 0.0f;
71  float x_sumsq = 0.0f;
72  float x_sumcube = 0.0f;
73 
74  float y_sumplain = 0.0f;
75  float y_sumsq = 0.0f;
76  float y_sumcube = 0.0f;
77 
78  float z_sumplain = 0.0f;
79  float z_sumsq = 0.0f;
80  float z_sumcube = 0.0f;
81 
82  float xy_sum = 0.0f;
83  float xz_sum = 0.0f;
84  float yz_sum = 0.0f;
85 
86  float x2y_sum = 0.0f;
87  float x2z_sum = 0.0f;
88  float y2x_sum = 0.0f;
89  float y2z_sum = 0.0f;
90  float z2x_sum = 0.0f;
91  float z2y_sum = 0.0f;
92 
93  for (unsigned int i = 0; i < size; i++) {
94 
95  float x2 = x[i] * x[i];
96  float y2 = y[i] * y[i];
97  float z2 = z[i] * z[i];
98 
99  x_sumplain += x[i];
100  x_sumsq += x2;
101  x_sumcube += x2 * x[i];
102 
103  y_sumplain += y[i];
104  y_sumsq += y2;
105  y_sumcube += y2 * y[i];
106 
107  z_sumplain += z[i];
108  z_sumsq += z2;
109  z_sumcube += z2 * z[i];
110 
111  xy_sum += x[i] * y[i];
112  xz_sum += x[i] * z[i];
113  yz_sum += y[i] * z[i];
114 
115  x2y_sum += x2 * y[i];
116  x2z_sum += x2 * z[i];
117 
118  y2x_sum += y2 * x[i];
119  y2z_sum += y2 * z[i];
120 
121  z2x_sum += z2 * x[i];
122  z2y_sum += z2 * y[i];
123  }
124 
125  //
126  //Least Squares Fit a sphere A,B,C with radius squared Rsq to 3D data
127  //
128  // P is a structure that has been computed with the data earlier.
129  // P.npoints is the number of elements; the length of X,Y,Z are identical.
130  // P's members are logically named.
131  //
132  // X[n] is the x component of point n
133  // Y[n] is the y component of point n
134  // Z[n] is the z component of point n
135  //
136  // A is the x coordiante of the sphere
137  // B is the y coordiante of the sphere
138  // C is the z coordiante of the sphere
139  // Rsq is the radius squared of the sphere.
140  //
141  //This method should converge; maybe 5-100 iterations or more.
142  //
143  float x_sum = x_sumplain / size; //sum( X[n] )
144  float x_sum2 = x_sumsq / size; //sum( X[n]^2 )
145  float x_sum3 = x_sumcube / size; //sum( X[n]^3 )
146  float y_sum = y_sumplain / size; //sum( Y[n] )
147  float y_sum2 = y_sumsq / size; //sum( Y[n]^2 )
148  float y_sum3 = y_sumcube / size; //sum( Y[n]^3 )
149  float z_sum = z_sumplain / size; //sum( Z[n] )
150  float z_sum2 = z_sumsq / size; //sum( Z[n]^2 )
151  float z_sum3 = z_sumcube / size; //sum( Z[n]^3 )
152 
153  float XY = xy_sum / size; //sum( X[n] * Y[n] )
154  float XZ = xz_sum / size; //sum( X[n] * Z[n] )
155  float YZ = yz_sum / size; //sum( Y[n] * Z[n] )
156  float X2Y = x2y_sum / size; //sum( X[n]^2 * Y[n] )
157  float X2Z = x2z_sum / size; //sum( X[n]^2 * Z[n] )
158  float Y2X = y2x_sum / size; //sum( Y[n]^2 * X[n] )
159  float Y2Z = y2z_sum / size; //sum( Y[n]^2 * Z[n] )
160  float Z2X = z2x_sum / size; //sum( Z[n]^2 * X[n] )
161  float Z2Y = z2y_sum / size; //sum( Z[n]^2 * Y[n] )
162 
163  //Reduction of multiplications
164  float F0 = x_sum2 + y_sum2 + z_sum2;
165  float F1 = 0.5f * F0;
166  float F2 = -8.0f * (x_sum3 + Y2X + Z2X);
167  float F3 = -8.0f * (X2Y + y_sum3 + Z2Y);
168  float F4 = -8.0f * (X2Z + Y2Z + z_sum3);
169 
170  //Set initial conditions:
171  float A = x_sum;
172  float B = y_sum;
173  float C = z_sum;
174 
175  //First iteration computation:
176  float A2 = A * A;
177  float B2 = B * B;
178  float C2 = C * C;
179  float QS = A2 + B2 + C2;
180  float QB = -2.0f * (A * x_sum + B * y_sum + C * z_sum);
181 
182  //Set initial conditions:
183  float Rsq = F0 + QB + QS;
184 
185  //First iteration computation:
186  float Q0 = 0.5f * (QS - Rsq);
187  float Q1 = F1 + Q0;
188  float Q2 = 8.0f * (QS - Rsq + QB + F0);
189  float aA, aB, aC, nA, nB, nC, dA, dB, dC;
190 
191  //Iterate N times, ignore stop condition.
192  unsigned int n = 0;
193 
194  while (n < max_iterations) {
195  n++;
196 
197  //Compute denominator:
198  aA = Q2 + 16.0f * (A2 - 2.0f * A * x_sum + x_sum2);
199  aB = Q2 + 16.0f * (B2 - 2.0f * B * y_sum + y_sum2);
200  aC = Q2 + 16.0f * (C2 - 2.0f * C * z_sum + z_sum2);
201  aA = (fabsf(aA) < FLT_EPSILON) ? 1.0f : aA;
202  aB = (fabsf(aB) < FLT_EPSILON) ? 1.0f : aB;
203  aC = (fabsf(aC) < FLT_EPSILON) ? 1.0f : aC;
204 
205  //Compute next iteration
206  nA = A - ((F2 + 16.0f * (B * XY + C * XZ + x_sum * (-A2 - Q0) + A * (x_sum2 + Q1 - C * z_sum - B * y_sum))) / aA);
207  nB = B - ((F3 + 16.0f * (A * XY + C * YZ + y_sum * (-B2 - Q0) + B * (y_sum2 + Q1 - A * x_sum - C * z_sum))) / aB);
208  nC = C - ((F4 + 16.0f * (A * XZ + B * YZ + z_sum * (-C2 - Q0) + C * (z_sum2 + Q1 - A * x_sum - B * y_sum))) / aC);
209 
210  //Check for stop condition
211  dA = (nA - A);
212  dB = (nB - B);
213  dC = (nC - C);
214 
215  if ((dA * dA + dB * dB + dC * dC) <= delta) { break; }
216 
217  //Compute next iteration's values
218  A = nA;
219  B = nB;
220  C = nC;
221  A2 = A * A;
222  B2 = B * B;
223  C2 = C * C;
224  QS = A2 + B2 + C2;
225  QB = -2.0f * (A * x_sum + B * y_sum + C * z_sum);
226  Rsq = F0 + QB + QS;
227  Q0 = 0.5f * (QS - Rsq);
228  Q1 = F1 + Q0;
229  Q2 = 8.0f * (QS - Rsq + QB + F0);
230  }
231 
232  *sphere_x = A;
233  *sphere_y = B;
234  *sphere_z = C;
235  *sphere_radius = sqrtf(Rsq);
236 
237  return 0;
238 }
239 
240 int ellipsoid_fit_least_squares(const float x[], const float y[], const float z[],
241  unsigned int size, int max_iterations, float delta, float *offset_x, float *offset_y, float *offset_z,
242  float *sphere_radius, float *diag_x, float *diag_y, float *diag_z, float *offdiag_x, float *offdiag_y, float *offdiag_z)
243 {
244  float _fitness = 1.0e30f, _sphere_lambda = 1.0f, _ellipsoid_lambda = 1.0f;
245 
246  for (int i = 0; i < max_iterations; i++) {
247  run_lm_sphere_fit(x, y, z, _fitness, _sphere_lambda,
248  size, offset_x, offset_y, offset_z,
249  sphere_radius, diag_x, diag_y, diag_z, offdiag_x, offdiag_y, offdiag_z);
250 
251  }
252 
253  _fitness = 1.0e30f;
254 
255  for (int i = 0; i < max_iterations; i++) {
256  run_lm_ellipsoid_fit(x, y, z, _fitness, _ellipsoid_lambda,
257  size, offset_x, offset_y, offset_z,
258  sphere_radius, diag_x, diag_y, diag_z, offdiag_x, offdiag_y, offdiag_z);
259  }
260 
261  return 0;
262 }
263 
264 int run_lm_sphere_fit(const float x[], const float y[], const float z[], float &_fitness, float &_sphere_lambda,
265  unsigned int size, float *offset_x, float *offset_y, float *offset_z,
266  float *sphere_radius, float *diag_x, float *diag_y, float *diag_z, float *offdiag_x, float *offdiag_y, float *offdiag_z)
267 {
268  //Run Sphere Fit using Levenberg Marquardt LSq Fit
269  const float lma_damping = 10.0f;
270  float _samples_collected = size;
271  float fitness = _fitness;
272  float fit1 = 0.0f, fit2 = 0.0f;
273 
276  float JTFI[4] = {};
277  float residual = 0.0f;
278 
279  // Gauss Newton Part common for all kind of extensions including LM
280  for (uint16_t k = 0; k < _samples_collected; k++) {
281 
282  float sphere_jacob[4];
283  //Calculate Jacobian
284  float A = (*diag_x * (x[k] - *offset_x)) + (*offdiag_x * (y[k] - *offset_y)) + (*offdiag_y * (z[k] - *offset_z));
285  float B = (*offdiag_x * (x[k] - *offset_x)) + (*diag_y * (y[k] - *offset_y)) + (*offdiag_z * (z[k] - *offset_z));
286  float C = (*offdiag_y * (x[k] - *offset_x)) + (*offdiag_z * (y[k] - *offset_y)) + (*diag_z * (z[k] - *offset_z));
287  float length = sqrtf(A * A + B * B + C * C);
288 
289  // 0: partial derivative (radius wrt fitness fn) fn operated on sample
290  sphere_jacob[0] = 1.0f;
291  // 1-3: partial derivative (offsets wrt fitness fn) fn operated on sample
292  sphere_jacob[1] = 1.0f * (((*diag_x * A) + (*offdiag_x * B) + (*offdiag_y * C)) / length);
293  sphere_jacob[2] = 1.0f * (((*offdiag_x * A) + (*diag_y * B) + (*offdiag_z * C)) / length);
294  sphere_jacob[3] = 1.0f * (((*offdiag_y * A) + (*offdiag_z * B) + (*diag_z * C)) / length);
295  residual = *sphere_radius - length;
296 
297  for (uint8_t i = 0; i < 4; i++) {
298  // compute JTJ
299  for (uint8_t j = 0; j < 4; j++) {
300  JTJ(i, j) += sphere_jacob[i] * sphere_jacob[j];
301  JTJ2(i, j) += sphere_jacob[i] * sphere_jacob[j]; //a backup JTJ for LM
302  }
303 
304  JTFI[i] += sphere_jacob[i] * residual;
305  }
306  }
307 
308 
309  //------------------------Levenberg-Marquardt-part-starts-here---------------------------------//
310  //refer: http://en.wikipedia.org/wiki/Levenberg%E2%80%93Marquardt_algorithm#Choice_of_damping_parameter
311  float fit1_params[4] = {*sphere_radius, *offset_x, *offset_y, *offset_z};
312  float fit2_params[4];
313  memcpy(fit2_params, fit1_params, sizeof(fit1_params));
314 
315  for (uint8_t i = 0; i < 4; i++) {
316  JTJ(i, i) += _sphere_lambda;
317  JTJ2(i, i) += _sphere_lambda / lma_damping;
318  }
319 
320  if (!JTJ.I(JTJ)) {
321  return -1;
322  }
323 
324  if (!JTJ2.I(JTJ2)) {
325  return -1;
326  }
327 
328  for (uint8_t row = 0; row < 4; row++) {
329  for (uint8_t col = 0; col < 4; col++) {
330  fit1_params[row] -= JTFI[col] * JTJ(row, col);
331  fit2_params[row] -= JTFI[col] * JTJ2(row, col);
332  }
333  }
334 
335  //Calculate mean squared residuals
336  for (uint16_t k = 0; k < _samples_collected; k++) {
337  float A = (*diag_x * (x[k] - fit1_params[1])) + (*offdiag_x * (y[k] - fit1_params[2])) + (*offdiag_y *
338  (z[k] + fit1_params[3]));
339  float B = (*offdiag_x * (x[k] - fit1_params[1])) + (*diag_y * (y[k] - fit1_params[2])) + (*offdiag_z *
340  (z[k] + fit1_params[3]));
341  float C = (*offdiag_y * (x[k] - fit1_params[1])) + (*offdiag_z * (y[k] - fit1_params[2])) + (*diag_z *
342  (z[k] - fit1_params[3]));
343  float length = sqrtf(A * A + B * B + C * C);
344  residual = fit1_params[0] - length;
345  fit1 += residual * residual;
346 
347  A = (*diag_x * (x[k] - fit2_params[1])) + (*offdiag_x * (y[k] - fit2_params[2])) + (*offdiag_y *
348  (z[k] - fit2_params[3]));
349  B = (*offdiag_x * (x[k] - fit2_params[1])) + (*diag_y * (y[k] - fit2_params[2])) + (*offdiag_z *
350  (z[k] - fit2_params[3]));
351  C = (*offdiag_y * (x[k] - fit2_params[1])) + (*offdiag_z * (y[k] - fit2_params[2])) + (*diag_z *
352  (z[k] - fit2_params[3]));
353  length = sqrtf(A * A + B * B + C * C);
354  residual = fit2_params[0] - length;
355  fit2 += residual * residual;
356  }
357 
358  fit1 = sqrtf(fit1) / _samples_collected;
359  fit2 = sqrtf(fit2) / _samples_collected;
360 
361  if (fit1 > _fitness && fit2 > _fitness) {
362  _sphere_lambda *= lma_damping;
363 
364  } else if (fit2 < _fitness && fit2 < fit1) {
365  _sphere_lambda /= lma_damping;
366  memcpy(fit1_params, fit2_params, sizeof(fit1_params));
367  fitness = fit2;
368 
369  } else if (fit1 < _fitness) {
370  fitness = fit1;
371  }
372 
373  //--------------------Levenberg-Marquardt-part-ends-here--------------------------------//
374 
375  if (PX4_ISFINITE(fitness) && fitness < _fitness) {
376  _fitness = fitness;
377  *sphere_radius = fit1_params[0];
378  *offset_x = fit1_params[1];
379  *offset_y = fit1_params[2];
380  *offset_z = fit1_params[3];
381  return 0;
382 
383  } else {
384  return -1;
385  }
386 }
387 
388 int run_lm_ellipsoid_fit(const float x[], const float y[], const float z[], float &_fitness, float &_sphere_lambda,
389  unsigned int size, float *offset_x, float *offset_y, float *offset_z,
390  float *sphere_radius, float *diag_x, float *diag_y, float *diag_z, float *offdiag_x, float *offdiag_y, float *offdiag_z)
391 {
392  //Run Sphere Fit using Levenberg Marquardt LSq Fit
393  const float lma_damping = 10.0f;
394  float _samples_collected = size;
395  float fitness = _fitness;
396  float fit1 = 0.0f;
397  float fit2 = 0.0f;
398 
399  float JTJ[81] = {};
400  float JTJ2[81] = {};
401  float JTFI[9] = {};
402  float residual = 0.0f;
403  float ellipsoid_jacob[9];
404 
405  // Gauss Newton Part common for all kind of extensions including LM
406  for (uint16_t k = 0; k < _samples_collected; k++) {
407 
408  //Calculate Jacobian
409  float A = (*diag_x * (x[k] - *offset_x)) + (*offdiag_x * (y[k] - *offset_y)) + (*offdiag_y * (z[k] - *offset_z));
410  float B = (*offdiag_x * (x[k] - *offset_x)) + (*diag_y * (y[k] - *offset_y)) + (*offdiag_z * (z[k] - *offset_z));
411  float C = (*offdiag_y * (x[k] - *offset_x)) + (*offdiag_z * (y[k] - *offset_y)) + (*diag_z * (z[k] - *offset_z));
412  float length = sqrtf(A * A + B * B + C * C);
413  residual = *sphere_radius - length;
414  fit1 += residual * residual;
415  // 0-2: partial derivative (offset wrt fitness fn) fn operated on sample
416  ellipsoid_jacob[0] = 1.0f * (((*diag_x * A) + (*offdiag_x * B) + (*offdiag_y * C)) / length);
417  ellipsoid_jacob[1] = 1.0f * (((*offdiag_x * A) + (*diag_y * B) + (*offdiag_z * C)) / length);
418  ellipsoid_jacob[2] = 1.0f * (((*offdiag_y * A) + (*offdiag_z * B) + (*diag_z * C)) / length);
419  // 3-5: partial derivative (diag offset wrt fitness fn) fn operated on sample
420  ellipsoid_jacob[3] = -1.0f * ((x[k] - *offset_x) * A) / length;
421  ellipsoid_jacob[4] = -1.0f * ((y[k] - *offset_y) * B) / length;
422  ellipsoid_jacob[5] = -1.0f * ((z[k] - *offset_z) * C) / length;
423  // 6-8: partial derivative (off-diag offset wrt fitness fn) fn operated on sample
424  ellipsoid_jacob[6] = -1.0f * (((y[k] - *offset_y) * A) + ((x[k] - *offset_x) * B)) / length;
425  ellipsoid_jacob[7] = -1.0f * (((z[k] - *offset_z) * A) + ((x[k] - *offset_x) * C)) / length;
426  ellipsoid_jacob[8] = -1.0f * (((z[k] - *offset_z) * B) + ((y[k] - *offset_y) * C)) / length;
427 
428  for (uint8_t i = 0; i < 9; i++) {
429  // compute JTJ
430  for (uint8_t j = 0; j < 9; j++) {
431  JTJ[i * 9 + j] += ellipsoid_jacob[i] * ellipsoid_jacob[j];
432  JTJ2[i * 9 + j] += ellipsoid_jacob[i] * ellipsoid_jacob[j]; //a backup JTJ for LM
433  }
434 
435  JTFI[i] += ellipsoid_jacob[i] * residual;
436  }
437  }
438 
439 
440  //------------------------Levenberg-Marquardt-part-starts-here---------------------------------//
441  //refer: http://en.wikipedia.org/wiki/Levenberg%E2%80%93Marquardt_algorithm#Choice_of_damping_parameter
442  float fit1_params[9] = {*offset_x, *offset_y, *offset_z, *diag_x, *diag_y, *diag_z, *offdiag_x, *offdiag_y, *offdiag_z};
443  float fit2_params[9];
444  memcpy(fit2_params, fit1_params, sizeof(fit1_params));
445 
446  for (uint8_t i = 0; i < 9; i++) {
447  JTJ[i * 9 + i] += _sphere_lambda;
448  JTJ2[i * 9 + i] += _sphere_lambda / lma_damping;
449  }
450 
451 
452  if (!mat_inverse(JTJ, JTJ, 9)) {
453  return -1;
454  }
455 
456  if (!mat_inverse(JTJ2, JTJ2, 9)) {
457  return -1;
458  }
459 
460  for (uint8_t row = 0; row < 9; row++) {
461  for (uint8_t col = 0; col < 9; col++) {
462  fit1_params[row] -= JTFI[col] * JTJ[row * 9 + col];
463  fit2_params[row] -= JTFI[col] * JTJ2[row * 9 + col];
464  }
465  }
466 
467  //Calculate mean squared residuals
468  for (uint16_t k = 0; k < _samples_collected; k++) {
469  float A = (fit1_params[3] * (x[k] - fit1_params[0])) + (fit1_params[6] * (y[k] - fit1_params[1])) + (fit1_params[7] *
470  (z[k] - fit1_params[2]));
471  float B = (fit1_params[6] * (x[k] - fit1_params[0])) + (fit1_params[4] * (y[k] - fit1_params[1])) + (fit1_params[8] *
472  (z[k] - fit1_params[2]));
473  float C = (fit1_params[7] * (x[k] - fit1_params[0])) + (fit1_params[8] * (y[k] - fit1_params[1])) + (fit1_params[5] *
474  (z[k] - fit1_params[2]));
475  float length = sqrtf(A * A + B * B + C * C);
476  residual = *sphere_radius - length;
477  fit1 += residual * residual;
478 
479  A = (fit2_params[3] * (x[k] - fit2_params[0])) + (fit2_params[6] * (y[k] - fit2_params[1])) + (fit2_params[7] *
480  (z[k] - fit2_params[2]));
481  B = (fit2_params[6] * (x[k] - fit2_params[0])) + (fit2_params[4] * (y[k] - fit2_params[1])) + (fit2_params[8] *
482  (z[k] - fit2_params[2]));
483  C = (fit2_params[7] * (x[k] - fit2_params[0])) + (fit2_params[8] * (y[k] - fit2_params[1])) + (fit2_params[5] *
484  (z[k] - fit2_params[2]));
485  length = sqrtf(A * A + B * B + C * C);
486  residual = *sphere_radius - length;
487  fit2 += residual * residual;
488  }
489 
490  fit1 = sqrtf(fit1) / _samples_collected;
491  fit2 = sqrtf(fit2) / _samples_collected;
492 
493  if (fit1 > _fitness && fit2 > _fitness) {
494  _sphere_lambda *= lma_damping;
495 
496  } else if (fit2 < _fitness && fit2 < fit1) {
497  _sphere_lambda /= lma_damping;
498  memcpy(fit1_params, fit2_params, sizeof(fit1_params));
499  fitness = fit2;
500 
501  } else if (fit1 < _fitness) {
502  fitness = fit1;
503  }
504 
505  //--------------------Levenberg-Marquardt-part-ends-here--------------------------------//
506  if (PX4_ISFINITE(fitness) && fitness < _fitness) {
507  _fitness = fitness;
508  *offset_x = fit1_params[0];
509  *offset_y = fit1_params[1];
510  *offset_z = fit1_params[2];
511  *diag_x = fit1_params[3];
512  *diag_y = fit1_params[4];
513  *diag_z = fit1_params[5];
514  *offdiag_x = fit1_params[6];
515  *offdiag_y = fit1_params[7];
516  *offdiag_z = fit1_params[8];
517  return 0;
518 
519  } else {
520  return -1;
521  }
522 }
523 
525  bool lenient_still_position)
526 {
527  static constexpr unsigned ndim = 3;
528 
529  float accel_ema[ndim] = { 0.0f }; // exponential moving average of accel
530  float accel_disp[3] = { 0.0f, 0.0f, 0.0f }; // max-hold dispersion of accel
531  static constexpr float ema_len = 0.5f; // EMA time constant in seconds
532  static constexpr float normal_still_thr = 0.25; // normal still threshold
533  float still_thr2 = powf(lenient_still_position ? (normal_still_thr * 3) : normal_still_thr, 2);
534  static constexpr float accel_err_thr = 5.0f; // set accel error threshold to 5m/s^2
535  const hrt_abstime still_time = lenient_still_position ? 500000 : 1300000; // still time required in us
536 
537  px4_pollfd_struct_t fds[1];
538  fds[0].fd = accel_sub;
539  fds[0].events = POLLIN;
540 
541  const hrt_abstime t_start = hrt_absolute_time();
542 
543  /* set timeout to 30s */
544  static constexpr hrt_abstime timeout = 90000000;
545 
546  hrt_abstime t_timeout = t_start + timeout;
547  hrt_abstime t = t_start;
548  hrt_abstime t_prev = t_start;
549  hrt_abstime t_still = 0;
550 
551  unsigned poll_errcount = 0;
552 
553  while (true) {
554  /* wait blocking for new data */
555  int poll_ret = px4_poll(fds, 1, 1000);
556 
557  if (poll_ret) {
558  struct sensor_combined_s sensor;
559  orb_copy(ORB_ID(sensor_combined), accel_sub, &sensor);
560  t = hrt_absolute_time();
561  float dt = (t - t_prev) / 1000000.0f;
562  t_prev = t;
563  float w = dt / ema_len;
564 
565  for (unsigned i = 0; i < ndim; i++) {
566 
567  float di = sensor.accelerometer_m_s2[i];
568 
569  float d = di - accel_ema[i];
570  accel_ema[i] += d * w;
571  d = d * d;
572  accel_disp[i] = accel_disp[i] * (1.0f - w);
573 
574  if (d > still_thr2 * 8.0f) {
575  d = still_thr2 * 8.0f;
576  }
577 
578  if (d > accel_disp[i]) {
579  accel_disp[i] = d;
580  }
581  }
582 
583  /* still detector with hysteresis */
584  if (accel_disp[0] < still_thr2 &&
585  accel_disp[1] < still_thr2 &&
586  accel_disp[2] < still_thr2) {
587  /* is still now */
588  if (t_still == 0) {
589  /* first time */
590  calibration_log_info(mavlink_log_pub, "[cal] detected rest position, hold still...");
591  t_still = t;
592  t_timeout = t + timeout;
593 
594  } else {
595  /* still since t_still */
596  if (t > t_still + still_time) {
597  /* vehicle is still, exit from the loop to detection of its orientation */
598  break;
599  }
600  }
601 
602  } else if (accel_disp[0] > still_thr2 * 4.0f ||
603  accel_disp[1] > still_thr2 * 4.0f ||
604  accel_disp[2] > still_thr2 * 4.0f) {
605  /* not still, reset still start time */
606  if (t_still != 0) {
607  calibration_log_info(mavlink_log_pub, "[cal] detected motion, hold still...");
608  px4_usleep(200000);
609  t_still = 0;
610  }
611  }
612 
613  } else if (poll_ret == 0) {
614  poll_errcount++;
615  }
616 
617  if (t > t_timeout) {
618  poll_errcount++;
619  }
620 
621  if (poll_errcount > 1000) {
624  }
625  }
626 
627  if (fabsf(accel_ema[0] - CONSTANTS_ONE_G) < accel_err_thr &&
628  fabsf(accel_ema[1]) < accel_err_thr &&
629  fabsf(accel_ema[2]) < accel_err_thr) {
630  return DETECT_ORIENTATION_TAIL_DOWN; // [ g, 0, 0 ]
631  }
632 
633  if (fabsf(accel_ema[0] + CONSTANTS_ONE_G) < accel_err_thr &&
634  fabsf(accel_ema[1]) < accel_err_thr &&
635  fabsf(accel_ema[2]) < accel_err_thr) {
636  return DETECT_ORIENTATION_NOSE_DOWN; // [ -g, 0, 0 ]
637  }
638 
639  if (fabsf(accel_ema[0]) < accel_err_thr &&
640  fabsf(accel_ema[1] - CONSTANTS_ONE_G) < accel_err_thr &&
641  fabsf(accel_ema[2]) < accel_err_thr) {
642  return DETECT_ORIENTATION_LEFT; // [ 0, g, 0 ]
643  }
644 
645  if (fabsf(accel_ema[0]) < accel_err_thr &&
646  fabsf(accel_ema[1] + CONSTANTS_ONE_G) < accel_err_thr &&
647  fabsf(accel_ema[2]) < accel_err_thr) {
648  return DETECT_ORIENTATION_RIGHT; // [ 0, -g, 0 ]
649  }
650 
651  if (fabsf(accel_ema[0]) < accel_err_thr &&
652  fabsf(accel_ema[1]) < accel_err_thr &&
653  fabsf(accel_ema[2] - CONSTANTS_ONE_G) < accel_err_thr) {
654  return DETECT_ORIENTATION_UPSIDE_DOWN; // [ 0, 0, g ]
655  }
656 
657  if (fabsf(accel_ema[0]) < accel_err_thr &&
658  fabsf(accel_ema[1]) < accel_err_thr &&
659  fabsf(accel_ema[2] + CONSTANTS_ONE_G) < accel_err_thr) {
660  return DETECT_ORIENTATION_RIGHTSIDE_UP; // [ 0, 0, -g ]
661  }
662 
663  calibration_log_critical(mavlink_log_pub, "ERROR: invalid orientation");
664 
665  return DETECT_ORIENTATION_ERROR; // Can't detect orientation
666 }
667 
669 {
670  static const char *rgOrientationStrs[] = {
671  "back", // tail down
672  "front", // nose down
673  "left",
674  "right",
675  "up", // upside-down
676  "down", // right-side up
677  "error"
678  };
679 
680  return rgOrientationStrs[orientation];
681 }
682 
684  int cancel_sub,
685  bool side_data_collected[detect_orientation_side_count],
686  calibration_from_orientation_worker_t calibration_worker,
687  void *worker_data,
688  bool lenient_still_position)
689 {
691 
692  // Setup subscriptions to onboard accel sensor
693 
694  int sub_accel = orb_subscribe(ORB_ID(sensor_combined));
695 
696  if (sub_accel < 0) {
697  calibration_log_critical(mavlink_log_pub, CAL_QGC_FAILED_MSG, "No onboard accel");
698  return calibrate_return_error;
699  }
700 
701  unsigned orientation_failures = 0;
702 
703  // Rotate through all requested orientation
704  while (true) {
705  if (calibrate_cancel_check(mavlink_log_pub, cancel_sub)) {
707  break;
708  }
709 
710  if (orientation_failures > 4) {
711  result = calibrate_return_error;
712  calibration_log_critical(mavlink_log_pub, CAL_QGC_FAILED_MSG, "timeout: no motion");
713  break;
714  }
715 
716  unsigned int side_complete_count = 0;
717 
718  // Update the number of completed sides
719  for (unsigned i = 0; i < detect_orientation_side_count; i++) {
720  if (side_data_collected[i]) {
721  side_complete_count++;
722  }
723  }
724 
725  if (side_complete_count == detect_orientation_side_count) {
726  // We have completed all sides, move on
727  break;
728  }
729 
730  /* inform user which orientations are still needed */
731  char pendingStr[80];
732  pendingStr[0] = 0;
733 
734  for (unsigned int cur_orientation = 0; cur_orientation < detect_orientation_side_count; cur_orientation++) {
735  if (!side_data_collected[cur_orientation]) {
736  strncat(pendingStr, " ", sizeof(pendingStr) - 1);
737  strncat(pendingStr, detect_orientation_str((enum detect_orientation_return)cur_orientation), sizeof(pendingStr) - 1);
738  }
739  }
740 
741  calibration_log_info(mavlink_log_pub, "[cal] pending:%s", pendingStr);
742  px4_usleep(20000);
743  calibration_log_info(mavlink_log_pub, "[cal] hold vehicle still on a pending side");
744  px4_usleep(20000);
745  enum detect_orientation_return orient = detect_orientation(mavlink_log_pub, cancel_sub, sub_accel,
746  lenient_still_position);
747 
748  if (orient == DETECT_ORIENTATION_ERROR) {
749  orientation_failures++;
750  calibration_log_info(mavlink_log_pub, "[cal] detected motion, hold still...");
751  px4_usleep(20000);
752  continue;
753  }
754 
755  /* inform user about already handled side */
756  if (side_data_collected[orient]) {
757  orientation_failures++;
759  calibration_log_info(mavlink_log_pub, "[cal] %s side already completed", detect_orientation_str(orient));
760  px4_usleep(20000);
761  continue;
762  }
763 
765  px4_usleep(20000);
767  px4_usleep(20000);
768  orientation_failures = 0;
769 
770  // Call worker routine
771  result = calibration_worker(orient, cancel_sub, worker_data);
772 
773  if (result != calibrate_return_ok) {
774  break;
775  }
776 
778  px4_usleep(20000);
780  px4_usleep(20000);
781 
782  // Note that this side is complete
783  side_data_collected[orient] = true;
784 
785  // output neutral tune
787 
788  // temporary priority boost for the white blinking led to come trough
789  rgbled_set_color_and_mode(led_control_s::COLOR_WHITE, led_control_s::MODE_BLINK_FAST, 3, 1);
790  px4_usleep(200000);
791  }
792 
793  if (sub_accel >= 0) {
794  px4_close(sub_accel);
795  }
796 
797  return result;
798 }
799 
801 {
802  int vehicle_command_sub = orb_subscribe(ORB_ID(vehicle_command));
803 
804  if (vehicle_command_sub >= 0) {
805  // make sure we won't read any old messages
806  struct vehicle_command_s cmd;
807  bool update;
808 
809  while (orb_check(vehicle_command_sub, &update) == 0 && update) {
810  orb_copy(ORB_ID(vehicle_command), vehicle_command_sub, &cmd);
811  }
812  }
813 
814  return vehicle_command_sub;
815 }
816 
818 {
819  orb_unsubscribe(cmd_sub);
820 }
821 
822 static void calibrate_answer_command(orb_advert_t *mavlink_log_pub, struct vehicle_command_s &cmd, unsigned result)
823 {
824  switch (result) {
825  case vehicle_command_s::VEHICLE_CMD_RESULT_ACCEPTED:
826  tune_positive(true);
827  break;
828 
829  case vehicle_command_s::VEHICLE_CMD_RESULT_DENIED:
830  mavlink_log_critical(mavlink_log_pub, "command denied during calibration: %u", cmd.command);
831  tune_negative(true);
832  break;
833 
834  default:
835  break;
836  }
837 }
838 
840 {
841  px4_pollfd_struct_t fds[1];
842  fds[0].fd = cancel_sub;
843  fds[0].events = POLLIN;
844 
845  if (px4_poll(&fds[0], 1, 0) > 0) {
846  struct vehicle_command_s cmd;
847 
848  orb_copy(ORB_ID(vehicle_command), cancel_sub, &cmd);
849 
850  // ignore internal commands, such as VEHICLE_CMD_DO_MOUNT_CONTROL from vmount
851  if (cmd.from_external) {
852  if (cmd.command == vehicle_command_s::VEHICLE_CMD_PREFLIGHT_CALIBRATION &&
853  (int)cmd.param1 == 0 &&
854  (int)cmd.param2 == 0 &&
855  (int)cmd.param3 == 0 &&
856  (int)cmd.param4 == 0 &&
857  (int)cmd.param5 == 0 &&
858  (int)cmd.param6 == 0) {
859  calibrate_answer_command(mavlink_log_pub, cmd, vehicle_command_s::VEHICLE_CMD_RESULT_ACCEPTED);
861  return true;
862 
863  } else {
864  calibrate_answer_command(mavlink_log_pub, cmd, vehicle_command_s::VEHICLE_CMD_RESULT_DENIED);
865  }
866  }
867  }
868 
869  return false;
870 }
#define CAL_ERROR_SENSOR_MSG
static orb_advert_t * mavlink_log_pub
int orb_copy(const struct orb_metadata *meta, int handle, void *buffer)
Definition: uORB.cpp:90
#define CAL_QGC_SIDE_DONE_MSG
Definition of geo / math functions to perform geodesic calculations.
Common calibration messages.
detect_orientation_return
int run_lm_sphere_fit(const float x[], const float y[], const float z[], float &_fitness, float &_sphere_lambda, unsigned int size, float *offset_x, float *offset_y, float *offset_z, float *sphere_radius, float *diag_x, float *diag_y, float *diag_z, float *offdiag_x, float *offdiag_y, float *offdiag_z)
#define CAL_QGC_ORIENTATION_DETECTED_MSG
int px4_poll(px4_pollfd_struct_t *fds, nfds_t nfds, int timeout)
calibrate_return calibrate_from_orientation(orb_advert_t *mavlink_log_pub, int cancel_sub, bool side_data_collected[detect_orientation_side_count], calibration_from_orientation_worker_t calibration_worker, void *worker_data, bool lenient_still_position)
Perform calibration sequence which require a rest orientation detection prior to calibration.
float accelerometer_m_s2[3]
void tune_positive(bool use_buzzer)
Blink green LED and play positive tune (if use_buzzer == true).
int ellipsoid_fit_least_squares(const float x[], const float y[], const float z[], unsigned int size, int max_iterations, float delta, float *offset_x, float *offset_y, float *offset_z, float *sphere_radius, float *diag_x, float *diag_y, float *diag_z, float *offdiag_x, float *offdiag_y, float *offdiag_z)
#define FLT_EPSILON
High-resolution timer with callouts and timekeeping.
static constexpr float CONSTANTS_ONE_G
Definition: geo.h:51
calibrate_return(* calibration_from_orientation_worker_t)(detect_orientation_return orientation, int cancel_sub, void *worker_data)
Opaque worker data.
int sphere_fit_least_squares(const float x[], const float y[], const float z[], unsigned int size, unsigned int max_iterations, float delta, float *sphere_x, float *sphere_y, float *sphere_z, float *sphere_radius)
Least-squares fit of a sphere to a set of points.
void calibrate_cancel_unsubscribe(int cmd_sub)
Called to cancel the subscription to the cancel command.
int orb_subscribe(const struct orb_metadata *meta)
Definition: uORB.cpp:75
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
const char * detect_orientation_str(enum detect_orientation_return orientation)
Returns the human readable string representation of the orientation.
Commander helper functions definitions.
int run_lm_ellipsoid_fit(const float x[], const float y[], const float z[], float &_fitness, float &_sphere_lambda, unsigned int size, float *offset_x, float *offset_y, float *offset_z, float *sphere_radius, float *diag_x, float *diag_y, float *diag_z, float *offdiag_x, float *offdiag_y, float *offdiag_z)
#define CAL_QGC_CANCELLED_MSG
int orb_unsubscribe(int handle)
Definition: uORB.cpp:85
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
calibrate_return
SquareMatrix< Type, M > I() const
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
Driver for the PX4 audio alarm port, /dev/tone_alarm.
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
bool mat_inverse(float *A, float *inv, uint8_t n)
Definition: matrix_alg.cpp:215
void rgbled_set_color_and_mode(uint8_t color, uint8_t mode, uint8_t blinks, uint8_t prio)
bool calibrate_cancel_check(orb_advert_t *mavlink_log_pub, int cancel_sub)
Used to periodically check for a cancel command.
float dt
Definition: px4io.c:73
int orb_check(int handle, bool *updated)
Definition: uORB.cpp:95
static void calibrate_answer_command(orb_advert_t *mavlink_log_pub, struct vehicle_command_s &cmd, unsigned result)
#define calibration_log_critical(_pub, _text,...)
enum detect_orientation_return detect_orientation(orb_advert_t *mavlink_log_pub, int cancel_sub, int accel_sub, bool lenient_still_position)
Wait for vehicle to become still and detect it&#39;s orientation.
static const unsigned detect_orientation_side_count
#define CAL_QGC_FAILED_MSG
void set_tune(int tune)
int calibrate_cancel_subscribe()
Called at the beginning of calibration in order to subscribe to the cancel command.
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
void tune_negative(bool use_buzzer)
Blink red LED and play negative tune (if use_buzzer == true).
#define calibration_log_info(_pub, _text,...)
int px4_close(int fd)