Firmware
voted_sensors_update.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 
34 #pragma once
35 
42 #include "parameters.h"
43 
44 #include <drivers/drv_accel.h>
45 #include <drivers/drv_gyro.h>
46 #include <drivers/drv_mag.h>
47 #include <drivers/drv_baro.h>
48 #include <drivers/drv_hrt.h>
49 
50 #include <mathlib/mathlib.h>
51 
52 #include <lib/ecl/validation/data_validator.h>
53 #include <lib/ecl/validation/data_validator_group.h>
54 
55 #include <uORB/topics/sensor_combined.h>
56 #include <uORB/topics/sensor_preflight.h>
57 #include <uORB/topics/sensor_correction.h>
58 #include <uORB/topics/sensor_selection.h>
59 #include <uORB/topics/vehicle_air_data.h>
60 #include <uORB/topics/vehicle_magnetometer.h>
61 #include <uORB/topics/subsystem_info.h>
62 
63 #include <DevMgr.hpp>
64 
66 #include "common.h"
67 
68 namespace sensors
69 {
70 
77 {
78 public:
83  VotedSensorsUpdate(const Parameters &parameters, bool hil_enabled);
84 
89  int init(sensor_combined_s &raw);
90 
94  void initialize_sensors();
95 
99  void deinit();
100 
101  void print_status();
102 
107  void parameters_update();
108 
112  void sensors_poll(sensor_combined_s &raw, vehicle_air_data_s &airdata, vehicle_magnetometer_s &magnetometer);
113 
118  void set_relative_timestamps(sensor_combined_s &raw);
119 
123  void check_failover();
124 
125  int num_gyros() const { return _gyro.subscription_count; }
126  int gyro_fd(int idx) const { return _gyro.subscription[idx]; }
127 
128  int best_gyro_fd() const { return _gyro.subscription[_gyro.last_best_vote]; }
129 
133  void calc_accel_inconsistency(sensor_preflight_s &preflt);
134 
138  void calc_gyro_inconsistency(sensor_preflight_s &preflt);
139 
143  void calc_mag_inconsistency(sensor_preflight_s &preflt);
144 
145 private:
146 
147  struct SensorData {
148  SensorData()
149  : last_best_vote(0),
150  subscription_count(0),
151  voter(1),
152  last_failover_count(0)
153  {
154  for (unsigned i = 0; i < SENSOR_COUNT_MAX; i++) {
155  enabled[i] = true;
156  subscription[i] = -1;
157  priority[i] = 0;
158  }
159  }
160 
161  bool enabled[SENSOR_COUNT_MAX];
162 
163  int subscription[SENSOR_COUNT_MAX];
164  uint8_t priority[SENSOR_COUNT_MAX];
165  uint8_t last_best_vote;
166  int subscription_count;
167  DataValidatorGroup voter;
168  unsigned int last_failover_count;
169  };
170 
171  void init_sensor_class(const struct orb_metadata *meta, SensorData &sensor_data, uint8_t sensor_count_max);
172 
179  void accel_poll(struct sensor_combined_s &raw);
180 
187  void gyro_poll(struct sensor_combined_s &raw);
188 
195  void mag_poll(vehicle_magnetometer_s &magnetometer);
196 
203  void baro_poll(vehicle_air_data_s &airdata);
204 
209  bool check_failover(SensorData &sensor, const char *sensor_name, const uint64_t type);
210 
219  bool apply_gyro_calibration(DriverFramework::DevHandle &h, const struct gyro_calibration_s *gcal, const int device_id);
220 
229  bool apply_accel_calibration(DriverFramework::DevHandle &h, const struct accel_calibration_s *acal,
230  const int device_id);
231 
240  bool apply_mag_calibration(DriverFramework::DevHandle &h, const struct mag_calibration_s *mcal, const int device_id);
241 
242  SensorData _gyro;
243  SensorData _accel;
244  SensorData _mag;
245  SensorData _baro;
246 
247  orb_advert_t _mavlink_log_pub = nullptr;
248 
249  sensor_combined_s _last_sensor_data[SENSOR_COUNT_MAX];
250  vehicle_air_data_s _last_airdata[SENSOR_COUNT_MAX];
251  vehicle_magnetometer_s _last_magnetometer[SENSOR_COUNT_MAX];
253  uint64_t _last_accel_timestamp[ACCEL_COUNT_MAX];
255  matrix::Dcmf _board_rotation;
256  matrix::Dcmf _mag_rotation[MAG_COUNT_MAX];
258  const Parameters &_parameters;
259  const bool _hil_enabled;
261  float _accel_diff[3][2];
262  float _gyro_diff[3][2];
263  float _mag_diff[3][2];
265  /* sensor thermal compensation */
266  TemperatureCompensation _temperature_compensation;
267  struct sensor_correction_s _corrections;
268  orb_advert_t _sensor_correction_pub = nullptr;
269  bool _corrections_changed = false;
270 
271  /* sensor selection publication */
272  struct sensor_selection_s _selection = {};
273  orb_advert_t _sensor_selection_pub = nullptr;
274  bool _selection_changed = false;
276  /* subsystem info publication */
277  struct subsystem_info_s _info;
278  orb_advert_t _info_pub = nullptr;
279 
280  uint32_t _accel_device_id[SENSOR_COUNT_MAX] = {};
281  uint32_t _baro_device_id[SENSOR_COUNT_MAX] = {};
282  uint32_t _gyro_device_id[SENSOR_COUNT_MAX] = {};
283  uint32_t _mag_device_id[SENSOR_COUNT_MAX] = {};
284 
285 };
286 
287 
288 
289 } /* namespace sensors */
void sensors_poll(sensor_combined_s &raw, vehicle_air_data_s &airdata, vehicle_magnetometer_s &magnetometer)
read new sensor data
Definition: voted_sensors_update.cpp:1058
Accelerometer driver interface.
int init(sensor_combined_s &raw)
initialize subscriptions etc.
Definition: voted_sensors_update.cpp:92
Gyroscope driver interface.
gyro scaling factors; Vout = (Vin * Vscale) + Voffset
Definition: drv_gyro.h:54
void set_relative_timestamps(sensor_combined_s &raw)
set the relative timestamps of each sensor timestamp, based on the last sensors_poll, so that the data can be published.
Definition: voted_sensors_update.cpp:1103
void calc_gyro_inconsistency(sensor_preflight_s &preflt)
Calculates the magnitude in rad/s of the largest difference between the primary and any other gyro se...
Definition: voted_sensors_update.cpp:1161
void deinit()
deinitialize the object (we cannot use the destructor because it is called on the wrong thread) ...
Definition: voted_sensors_update.cpp:113
void check_failover()
check if a failover event occured.
Definition: voted_sensors_update.cpp:1095
void parameters_update()
call this whenever parameters got updated.
Definition: voted_sensors_update.cpp:132
mag scaling factors; Vout = (Vin * Vscale) + Voffset
Definition: drv_mag.h:56
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
High-resolution timer with callouts and timekeeping.
accel scaling factors; Vout = Vscale * (Vin + Voffset)
Definition: drv_accel.h:54
Definition: parameters.h:58
Sensor correction methods.
void calc_accel_inconsistency(sensor_preflight_s &preflt)
Calculates the magnitude in m/s/s of the largest difference between the primary and any other accel s...
Definition: voted_sensors_update.cpp:1112
void initialize_sensors()
This tries to find new sensor instances.
Definition: voted_sensors_update.cpp:105
Definition: common.h:43
Common header for mathlib exports.
Object metadata.
Definition: uORB.h:50
void calc_mag_inconsistency(sensor_preflight_s &preflt)
Calculates the magnitude in Gauss of the largest difference between the primary and any other magneto...
Definition: voted_sensors_update.cpp:1210
Barometric pressure sensor driver interface.
defines the list of parameters that are used within the sensors module
class TemperatureCompensation Applies temperature compensation to sensor data.
Definition: temperature_compensation.h:66
class VotedSensorsUpdate
Definition: voted_sensors_update.h:76
VotedSensorsUpdate(const Parameters &parameters, bool hil_enabled)
Definition: voted_sensors_update.cpp:54