Larus glider flight sensor system 3.9.2024
Software-In-The-Loop test and validation system
Loading...
Searching...
No Matches
compass_calibration.h
Go to the documentation of this file.
1/***********************************************************************/
25#ifndef COMPASS_CALIBRATION_H_
26#define COMPASS_CALIBRATION_H_
27
29#include "float3vector.h"
31#include "persistent_data.h"
33
36{
37public:
38 single_axis_calibration_t( float _offset=0.0f, float slope=1.0f)
39 : offset( _offset),
40 scale( 1.0f / slope),
41 variance ( 1.0e10f)
42 {}
43
46 {
47 offset = result.y_offset;
48 scale = 1.0f / result.slope;
49 variance = (result.variance_offset + result.variance_slope) * 0.5f; // use variance average
50 }
51
53 void refresh ( float _offset, float _slope, float _variance_offset, float _variance_slope)
54 {
56 scale = 1.0f / _slope;
57 variance = ( _variance_offset + _variance_slope) * 0.5f; // use variance average
58 }
59
62 {
63 return (sensor_reading - offset) * scale;
64 }
65
66 float offset;
67 float scale;
68 float variance;
69};
70
72template <class sample_type, class evaluation_type> class compass_calibration_t
73{
74public:
78
80 {
82
83 // shortcut while no data available
85 return in;
86
87 for( unsigned i=0; i<3; ++i)
89 return out;
90 }
91
92 bool set_default (void)
93 {
94 float variance;
95 calibration_done = true;
96 for( unsigned i=0; i<3; ++i)
97 {
98 calibration[i].offset = 0.0f;
99 calibration[i].scale = 1.0f;
100 calibration[i].variance = 0.1f;
101 }
102
103 calibration_done = true;
104 return false; // no error;
105 }
106
107
111 float scale_factor)
112 {
114 return false;
116 return false;
117
118 // now we have enough entropy and evaluate our result
122
123 for (unsigned i = 0; i < 3; ++i)
124 {
127
128 calibration_candidate[i].refresh(
131 (new_calibration_data_right[i].variance_offset + new_calibration_data_left[i].variance_offset) / SQR(scale_factor) / TWO,
132 (new_calibration_data_right[i].variance_slope + new_calibration_data_left[i].variance_slope) / TWO);
133
134 mag_calibrator_right[i].reset();
135 mag_calibrator_left[i].reset();
136 }
137
139 return false; // we keep the old calibration
140
141 // now we take the new calibration
142 for (unsigned i = 0; i < 3; ++i)
144
145 return true;
146 }
147
148 bool available () const
149 {
150 return calibration_done;
151 }
152
154 {
155 return calibration_done ? calibration : 0;
156 }
157
159 {
160 for( unsigned i=0; i<3; ++i)
161 {
162 if( fabs( new_calibration[i].offset - calibration[i].offset) > MAG_OFFSET_CHANGE_LIMIT)
163 return true;
164
166 return true;
167
168 if( new_calibration[i].variance < calibration[i].variance)
169 return true;
170}
171 return false;
172 }
173
174 void write_into_EEPROM (void) const
175 {
176 if( calibration_done == false)
177 return;
178
180
181 float variance = 0.0f;
182 for( unsigned i=0; i<3; ++i)
183 {
186 variance += calibration[i].variance;
187 }
188 write_EEPROM_value(MAG_STD_DEVIATION, SQRT( variance / 6.0f));
189 }
190
192 {
193 float variance;
194 calibration_done = false;
195 if( true == read_EEPROM_value( MAG_STD_DEVIATION, variance))
196 return true; // error
197 variance = SQR( variance); // has been stored as STD DEV
198
199 for( unsigned i=0; i<3; ++i)
200 {
201 if( true == read_EEPROM_value( (EEPROM_PARAMETER_ID)(MAG_X_OFF + 2*i), calibration[i].offset))
202 return true; // error
203
204 if( true == read_EEPROM_value( (EEPROM_PARAMETER_ID)(MAG_X_SCALE + 2*i), calibration[i].scale))
205 return true; // error
206
207 calibration[i].variance = variance;
208 }
209
210 calibration_done = true;
211 return false; // no error;
212 }
213
216};
217
218#endif /* COMPASS_CALIBRATION_H_ */
linear least square fit for arbitrary data
Tuning parameters for navigation, wind an variometer.
#define MINIMUM_MAG_CALIBRATION_SAMPLES
#define MAG_SCALE_CHANGE_LIMIT
#define MAG_OFFSET_CHANGE_LIMIT
Maintains 3 axes magnetic calibration data.
single_axis_calibration_t calibration[3]
bool set_calibration_if_changed(linear_least_square_fit< sample_type, evaluation_type > mag_calibrator_right[3], linear_least_square_fit< sample_type, evaluation_type > mag_calibrator_left[3], float scale_factor)
const single_axis_calibration_t * get_calibration(void) const
float3vector calibrate(const float3vector &in)
void write_into_EEPROM(void) const
bool parameters_changed_significantly(single_axis_calibration_t const *new_calibration) const
maintain offset and slope data for one sensor axis
float scale
convert sensor-units into SI-data
float variance
measure of precision: sensor calibration parameter variance
void refresh(linear_least_square_result< float > &result)
feed in new calibration from linear least square fit
single_axis_calibration_t(float _offset=0.0f, float slope=1.0f)
float offset
sensor offset in sensor units
float calibrate(float sensor_reading)
calibrate instant sensor reading using calibration data
void refresh(float _offset, float _slope, float _variance_offset, float _variance_slope)
feed in new calibration from linear least square fit
mathematical vector of arbitrary type and size
Definition vector.h:40
#define TWO
#define SQRT(x)
bool write_EEPROM_value(EEPROM_PARAMETER_ID id, float value)
bool read_EEPROM_value(EEPROM_PARAMETER_ID id, float &value)
bool EEPROM_initialize(void)
definitions for persistent data in EEPROM or config. file
#define SQR(x)
collection of system tuning parameters