Firmware
temperature_compensation.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2016 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 
43 #pragma once
44 
45 #include <parameters/param.h>
46 #include <mathlib/mathlib.h>
47 #include <matrix/math.hpp>
48 
49 #include "common.h"
50 
51 
52 namespace sensors
53 {
54 
55 static_assert(GYRO_COUNT_MAX == 3,
56  "GYRO_COUNT_MAX must be 3 (if changed, add/remove TC_* params to match the count)");
57 static_assert(ACCEL_COUNT_MAX == 3,
58  "ACCEL_COUNT_MAX must be 3 (if changed, add/remove TC_* params to match the count)");
59 static_assert(BARO_COUNT_MAX == 3,
60  "BARO_COUNT_MAX must be 3 (if changed, add/remove TC_* params to match the count)");
61 
67 {
68 public:
69 
71  int parameters_update(bool hil_enabled = false);
72 
76  int set_sensor_id_gyro(uint32_t device_id, int topic_instance);
77  int set_sensor_id_accel(uint32_t device_id, int topic_instance);
78  int set_sensor_id_baro(uint32_t device_id, int topic_instance);
79 
80 
93  int apply_corrections_gyro(int topic_instance, matrix::Vector3f &sensor_data, float temperature, float *offsets,
94  float *scales);
95 
96  int apply_corrections_accel(int topic_instance, matrix::Vector3f &sensor_data, float temperature, float *offsets,
97  float *scales);
98 
99  int apply_corrections_baro(int topic_instance, float &sensor_data, float temperature, float *offsets, float *scales);
100 
102  void print_status();
103 private:
104 
105  /* Struct containing parameters used by the single axis 5th order temperature compensation algorithm
106 
107  Input:
108 
109  measured_temp : temperature measured at the sensor (deg C)
110  raw_value : reading from the sensor before compensation
111  corrected_value : reading from the sensor after compensation for errors
112 
113  Compute:
114 
115  delta_temp = measured_temp - ref_temp
116  offset = x5 * delta_temp^5 + x4 * delta_temp^4 + x3 * delta_temp^3 + x2 * delta_temp^2 + x1 * delta_temp + x0
117  corrected_value = (raw_value - offset) * scale
118 
119  */
120  struct SensorCalData1D {
121  int32_t ID;
122  float x5;
123  float x4;
124  float x3;
125  float x2;
126  float x1;
127  float x0;
128  float scale;
129  float ref_temp;
130  float min_temp;
131  float max_temp;
132  };
133 
134  struct SensorCalHandles1D {
135  param_t ID;
136  param_t x5;
137  param_t x4;
138  param_t x3;
139  param_t x2;
140  param_t x1;
141  param_t x0;
142  param_t scale;
143  param_t ref_temp;
144  param_t min_temp;
145  param_t max_temp;
146  };
147 
148 
149  /* Struct containing parameters used by the 3-axis 3rd order temperature compensation algorithm
150 
151  Input:
152 
153  measured_temp : temperature measured at the sensor (deg C)
154  raw_value[3] : XYZ readings from the sensor before compensation
155  corrected_value[3] : XYZ readings from the sensor after compensation for errors
156 
157  Compute for each measurement index:
158 
159  delta_temp = measured_temp - ref_temp
160  offset = x3 * delta_temp^3 + x2 * delta_temp^2 + x1 * delta_temp + x0
161  corrected_value = (raw_value - offset) * scale
162 
163  */
164  struct SensorCalData3D {
165  int32_t ID;
166  float x3[3];
167  float x2[3];
168  float x1[3];
169  float x0[3];
170  float scale[3];
171  float ref_temp;
172  float min_temp;
173  float max_temp;
174  };
175 
176  struct SensorCalHandles3D {
177  param_t ID;
178  param_t x3[3];
179  param_t x2[3];
180  param_t x1[3];
181  param_t x0[3];
182  param_t scale[3];
183  param_t ref_temp;
184  param_t min_temp;
185  param_t max_temp;
186  };
187 
188  // create a struct containing all thermal calibration parameters
189  struct Parameters {
190  int32_t gyro_tc_enable;
191  SensorCalData3D gyro_cal_data[GYRO_COUNT_MAX];
192  int32_t accel_tc_enable;
193  SensorCalData3D accel_cal_data[ACCEL_COUNT_MAX];
194  int32_t baro_tc_enable;
195  SensorCalData1D baro_cal_data[BARO_COUNT_MAX];
196  };
197 
198  // create a struct containing the handles required to access all calibration parameters
199  struct ParameterHandles {
200  param_t gyro_tc_enable;
201  SensorCalHandles3D gyro_cal_handles[GYRO_COUNT_MAX];
202  param_t accel_tc_enable;
203  SensorCalHandles3D accel_cal_handles[ACCEL_COUNT_MAX];
204  param_t baro_tc_enable;
205  SensorCalHandles1D baro_cal_handles[BARO_COUNT_MAX];
206  };
207 
208 
213  static int initialize_parameter_handles(ParameterHandles &parameter_handles);
214 
215 
233  bool calc_thermal_offsets_1D(SensorCalData1D &coef, float measured_temp, float &offset);
234 
252  bool calc_thermal_offsets_3D(const SensorCalData3D &coef, float measured_temp, float offset[]);
253 
254 
255  Parameters _parameters;
256 
257 
258  struct PerSensorData {
259  PerSensorData()
260  {
261  for (int i = 0; i < SENSOR_COUNT_MAX; ++i) { device_mapping[i] = 255; last_temperature[i] = -100.0f; }
262  }
263  void reset_temperature()
264  {
265  for (int i = 0; i < SENSOR_COUNT_MAX; ++i) { last_temperature[i] = -100.0f; }
266  }
267  uint8_t device_mapping[SENSOR_COUNT_MAX];
268  float last_temperature[SENSOR_COUNT_MAX];
269  };
270  PerSensorData _gyro_data;
271  PerSensorData _accel_data;
272  PerSensorData _baro_data;
273 
274 
275  template<typename T>
276  static inline int set_sensor_id(uint32_t device_id, int topic_instance, PerSensorData &sensor_data,
277  const T *sensor_cal_data, uint8_t sensor_count_max);
278 };
279 
280 }
int apply_corrections_gyro(int topic_instance, matrix::Vector3f &sensor_data, float temperature, float *offsets, float *scales)
Apply Thermal corrections to gyro (& other) sensor data.
Definition: temperature_compensation.cpp:395
Global flash based parameter store.
int set_sensor_id_gyro(uint32_t device_id, int topic_instance)
supply information which device_id matches a specific uORB topic_instance (needed if a system has mul...
Definition: temperature_compensation.cpp:354
Definition: common.h:43
Common header for mathlib exports.
int parameters_update(bool hil_enabled=false)
(re)load the parameters.
Definition: temperature_compensation.cpp:154
void print_status()
output current configuration status to console
Definition: temperature_compensation.cpp:480
class TemperatureCompensation Applies temperature compensation to sensor data.
Definition: temperature_compensation.h:66
uint32_t param_t
Parameter handle.
Definition: param.h:98