sensors.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, James Jackson, Daniel Koch, and Craig Bidstrup,
3  * BYU MAGICC Lab
4  *
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef ROSFLIGHT_FIRMWARE_SENSORS_H
34 #define ROSFLIGHT_FIRMWARE_SENSORS_H
35 
36 #include <stdint.h>
37 #include <stdbool.h>
38 #include <turbomath/turbomath.h>
39 
40 #pragma GCC diagnostic push
41 #pragma GCC diagnostic ignored "-Wmissing-field-initializers" // Ignore warning about leaving struct fields blank
42 
43 namespace rosflight_firmware
44 {
45 // Fix type, as defined in sensor_msgs/NavSatStatus
46 typedef enum
47 {
48  NO_FIX, // Unable to fix position
49  FIX, // Unaugmented fix
50  SBAS_FIX, // with satellite-based augmentation
51  GBAS_FIX // with ground-based augmentation
52 } GNSSFixType;
53 
54 #pragma GCC diagnostic push // Allow anonymous nested unions and structs
55 #pragma GCC diagnostic ignored "-Wpedantic"
56 
57 struct GNSSData
58 {
59  GNSSFixType fix_type;
60  uint32_t time_of_week;
61  uint64_t time; // Unix time, in seconds
62  uint64_t nanos; // Fractional time
63  int32_t lat; // deg*10^-7
64  int32_t lon; // deg*10^-7
65  int32_t height; // mm
66  int32_t vel_n; // mm/s
67  int32_t vel_e; // mm/s
68  int32_t vel_d; // mm/s
69  uint32_t h_acc; // mm
70  uint32_t v_acc; // mm
71  struct
72  {
73  int32_t x; // cm
74  int32_t y; // cm
75  int32_t z; // cm
76  uint32_t p_acc; // cm
77  int32_t vx; // cm/s
78  int32_t vy; // cm/s
79  int32_t vz; // cm/s
80  uint32_t s_acc; // cm/s
81  } ecef;
82  uint64_t rosflight_timestamp; // microseconds, time stamp of last byte in the message
83 };
84 
85 struct GNSSRaw
86 {
87  uint64_t time_of_week;
88  uint16_t year;
89  uint8_t month;
90  uint8_t day;
91  uint8_t hour;
92  uint8_t min;
93  uint8_t sec;
94  uint8_t valid;
95  uint32_t t_acc;
96  int32_t nano;
97  uint8_t fix_type;
98  uint8_t num_sat;
99  int32_t lon;
100  int32_t lat;
101  int32_t height;
102  int32_t height_msl;
103  uint32_t h_acc;
104  uint32_t v_acc;
105  int32_t vel_n;
106  int32_t vel_e;
107  int32_t vel_d;
108  int32_t g_speed;
109  int32_t head_mot;
110  uint32_t s_acc;
111  uint32_t head_acc;
112  uint16_t p_dop;
113  uint64_t rosflight_timestamp; // microseconds, time stamp of last byte in the message
114 };
115 
116 #pragma GCC diagnostic pop
117 
118 class ROSflight;
119 
120 class Sensors
121 {
122 public:
123  struct Data
124  {
126  turbomath::Vector gyro = {0, 0, 0};
127  turbomath::Quaternion fcu_orientation = {1, 0, 0, 0};
128  float imu_temperature = 0;
129  uint64_t imu_time = 0;
130 
131  float diff_pressure_velocity = 0;
132  float diff_pressure = 0;
133  float diff_pressure_temp = 0;
134  bool diff_pressure_valid = false;
135 
136  float baro_altitude = 0;
137  float baro_pressure = 0;
138  float baro_temperature = 0;
139  bool baro_valid = false;
140 
141  float sonar_range = 0;
142  bool sonar_range_valid = false;
143 
144  GNSSData gnss_data = {};
145  bool gnss_new_data = false;
146  float gps_CNO = 0; // What is this?
147  bool gnss_present = false;
148  GNSSRaw gnss_raw = {};
149 
150  turbomath::Vector mag = {0, 0, 0};
151 
152  bool baro_present = false;
153  bool mag_present = false;
154  bool sonar_present = false;
155  bool diff_pressure_present = false;
156  };
157 
159 
160  inline const Data &data() const
161  {
162  return data_;
163  }
164  void get_filtered_IMU(turbomath::Vector &accel, turbomath::Vector &gyro, uint64_t &stamp_us);
165 
166  // function declarations
167  void init();
168  bool run();
169  void param_change_callback(uint16_t param_id);
170 
171  // Calibration Functions
172  bool start_imu_calibration(void);
173  bool start_gyro_calibration(void);
174  bool start_baro_calibration(void);
175  bool start_diff_pressure_calibration(void);
176  bool gyro_calibration_complete(void);
177 
178  inline bool should_send_imu_data(void)
179  {
180  if (imu_data_sent_)
181  return false;
182  else
183  imu_data_sent_ = true;
184  return true;
185  }
186 
187 private:
188  static const float BARO_MAX_CHANGE_RATE;
189  static const float BARO_SAMPLE_RATE;
190  static const float DIFF_MAX_CHANGE_RATE;
191  static const float DIFF_SAMPLE_RATE;
192  static const float SONAR_MAX_CHANGE_RATE;
193  static const float SONAR_SAMPLE_RATE;
194  static const int SENSOR_CAL_DELAY_CYCLES;
195  static const int SENSOR_CAL_CYCLES;
196  static const float BARO_MAX_CALIBRATION_VARIANCE;
198 
200  {
201  private:
202  float max_change_;
203  float center_;
205  bool init_ = false;
206 
207  public:
209  void init(float max_change_rate, float update_rate, float center);
210  bool update(float new_val, float *val);
211  };
212 
213  enum : uint8_t
214  {
220  NUM_LOW_PRIORITY_SENSORS
221  };
222 
224 
226 
227  float accel_[3] = {0, 0, 0};
228  float gyro_[3] = {0, 0, 0};
229 
230  bool calibrating_acc_flag_ = false;
231  bool calibrating_gyro_flag_ = false;
232  uint8_t next_sensor_to_update_ = BAROMETER;
233  void init_imu();
234  void calibrate_accel(void);
235  void calibrate_gyro(void);
236  void calibrate_baro(void);
237  void calibrate_diff_pressure(void);
238  void correct_imu(void);
239  void correct_mag(void);
240  void correct_baro(void);
241  void correct_diff_pressure(void);
242  bool update_imu(void);
243  void update_other_sensors(void);
244  void look_for_disabled_sensors(void);
245  uint32_t last_time_look_for_disarmed_sensors_ = 0;
246  uint32_t last_imu_update_ms_ = 0;
247 
250 
251  // IMU calibration
252  uint16_t gyro_calibration_count_ = 0;
253  turbomath::Vector gyro_sum_ = {0, 0, 0};
254  uint16_t accel_calibration_count_ = 0;
255  turbomath::Vector acc_sum_ = {0, 0, 0};
256  const turbomath::Vector gravity_ = {0.0f, 0.0f, 9.80665f};
257  float acc_temp_sum_ = 0.0f;
258  turbomath::Vector max_ = {-1000.0f, -1000.0f, -1000.0f};
259  turbomath::Vector min_ = {1000.0f, 1000.0f, 1000.0f};
260 
261  // Filtered IMU
264  uint64_t int_start_us_;
266 
267  // Baro Calibration
268  bool baro_calibrated_ = false;
269  float ground_pressure_ = 0.0f;
270  uint16_t baro_calibration_count_ = 0;
271  uint32_t last_baro_cal_iter_ms_ = 0;
272  float baro_calibration_mean_ = 0.0f;
273  float baro_calibration_var_ = 0.0f;
274 
275  // Diff Pressure Calibration
276  bool diff_pressure_calibrated_ = false;
277  uint16_t diff_pressure_calibration_count_ = 0;
278  uint32_t last_diff_pressure_cal_iter_ms_ = 0;
279  float diff_pressure_calibration_mean_ = 0.0f;
280  float diff_pressure_calibration_var_ = 0.0f;
281 
282  // Sensor Measurement Outlier Filters
286 
287 };
288 
289 } // namespace rosflight_firmware
290 
291 #pragma GCC diagnostic pop // End ignore missing field initializers in structs
292 
293 #endif // ROSFLIGHT_FIRMWARE_SENSORS_H
static volatile int16_t gyro[3]
Definition: drv_mpu6050.c:277
struct rosflight_firmware::GNSSData::@109 ecef
static const float DIFF_SAMPLE_RATE
Definition: sensors.h:191
static const float SONAR_SAMPLE_RATE
Definition: sensors.h:193
static const float DIFF_MAX_CHANGE_RATE
Definition: sensors.h:190
void init(const M_string &remappings)
bool sonar_present
Definition: multisense.c:44
static const float BARO_MAX_CALIBRATION_VARIANCE
Definition: sensors.h:196
OutlierFilter baro_outlier_filt_
Definition: sensors.h:283
static const float DIFF_PRESSURE_MAX_CALIBRATION_VARIANCE
Definition: sensors.h:197
OutlierFilter diff_outlier_filt_
Definition: sensors.h:284
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
uint64_t prev_imu_read_time_us_
Definition: sensors.h:265
static volatile int16_t accel[3]
Definition: drv_mpu6050.c:276
static const float BARO_SAMPLE_RATE
Definition: sensors.h:189
static const int SENSOR_CAL_CYCLES
Definition: sensors.h:195
rosflight_firmware::ROSflight * rosflight
bool should_send_imu_data(void)
Definition: sensors.h:178
static const float BARO_MAX_CHANGE_RATE
Definition: sensors.h:188
turbomath::Vector accel_int_
Definition: sensors.h:262
static volatile float data_[3]
Definition: drv_hmc5883l.c:75
turbomath::Vector gyro_int_
Definition: sensors.h:263
static const float SONAR_MAX_CHANGE_RATE
Definition: sensors.h:192
bool baro_present
Definition: multisense.c:42
const Data & data() const
Definition: sensors.h:160
static const int SENSOR_CAL_DELAY_CYCLES
Definition: sensors.h:194
bool mag_present
Definition: multisense.c:43
OutlierFilter sonar_outlier_filt_
Definition: sensors.h:285


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Wed Jul 3 2019 19:59:25