PX4 Firmware
PX4 Autopilot Software http://px4.io
sih.hpp
Go to the documentation of this file.
1 /****************************************************************************
2 *
3 * Copyright (c) 2019 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 
36 #include <px4_platform_common/module.h>
37 #include <px4_platform_common/module_params.h>
38 #include <px4_platform_common/posix.h>
39 
40 #include <matrix/matrix/math.hpp> // matrix, vectors, dcm, quaterions
41 #include <conversion/rotation.h> // math::radians,
42 #include <ecl/geo/geo.h> // to get the physical constants
43 #include <drivers/drv_hrt.h> // to get the real time
48 #include <perf/perf_counter.h>
49 #include <uORB/Subscription.hpp>
51 #include <uORB/topics/vehicle_angular_velocity.h> // to publish groundtruth
52 #include <uORB/topics/vehicle_attitude.h> // to publish groundtruth
53 #include <uORB/topics/vehicle_global_position.h> // to publish groundtruth
55 
56 extern "C" __EXPORT int sih_main(int argc, char *argv[]);
57 
58 class Sih : public ModuleBase<Sih>, public ModuleParams
59 {
60 public:
61  Sih();
62 
63  virtual ~Sih() = default;
64 
65  /** @see ModuleBase */
66  static int task_spawn(int argc, char *argv[]);
67 
68  /** @see ModuleBase */
69  static Sih *instantiate(int argc, char *argv[]);
70 
71  /** @see ModuleBase */
72  static int custom_command(int argc, char *argv[]);
73 
74  /** @see ModuleBase */
75  static int print_usage(const char *reason = nullptr);
76 
77  /** @see ModuleBase::run() */
78  void run() override;
79 
80  /** @see ModuleBase::print_status() */
81  int print_status() override;
82 
83  static float generate_wgn(); // generate white Gaussian noise sample
84 
85  // generate white Gaussian noise sample as a 3D vector with specified std
86  static matrix::Vector3f noiseGauss3f(float stdx, float stdy, float stdz);
87 
88  // timer called periodically to post the semaphore
89  static void timer_callback(void *sem);
90 
91 private:
92 
93  /**
94  * Check for parameter changes and update them if needed.
95  * @param parameter_update_sub uorb subscription to parameter_update
96  * @param force for a parameter update
97  */
99  void parameters_updated();
100 
101  // simulated sensor instances
102  PX4Accelerometer _px4_accel{ 1311244, ORB_PRIO_DEFAULT, ROTATION_NONE }; // 1311244: DRV_ACC_DEVTYPE_ACCELSIM, BUS: 1, ADDR: 1, TYPE: SIMULATION
103  PX4Gyroscope _px4_gyro{ 2294028, ORB_PRIO_DEFAULT, ROTATION_NONE }; // 2294028: DRV_GYR_DEVTYPE_GYROSIM, BUS: 1, ADDR: 2, TYPE: SIMULATION
104  PX4Magnetometer _px4_mag{ 197388, ORB_PRIO_DEFAULT, ROTATION_NONE }; // 197388: DRV_MAG_DEVTYPE_MAGSIM, BUS: 3, ADDR: 1, TYPE: SIMULATION
105  PX4Barometer _px4_baro{ 6620172, ORB_PRIO_DEFAULT }; // 6620172: DRV_BARO_DEVTYPE_BAROSIM, BUS: 1, ADDR: 4, TYPE: SIMULATION
106 
107  // to publish the gps position
110 
111  // angular velocity groundtruth
114 
115  // attitude groundtruth
118 
119  // global position groundtruth
122 
125 
126  // hard constants
127  static constexpr uint16_t NB_MOTORS = 4;
128  static constexpr float T1_C = 15.0f; // ground temperature in celcius
129  static constexpr float T1_K = T1_C - CONSTANTS_ABSOLUTE_NULL_CELSIUS; // ground temperature in Kelvin
130  static constexpr float TEMP_GRADIENT = -6.5f / 1000.0f; // temperature gradient in degrees per metre
131  static constexpr hrt_abstime LOOP_INTERVAL = 4000; // 4ms => 250 Hz real-time
132 
133  void init_variables();
134  void init_sensors();
135  void read_motors();
137  void equations_of_motion();
139  void send_IMU();
140  void send_gps();
141  void publish_sih();
142  void inner_loop();
143 
146 
147  px4_sem_t _data_semaphore;
148 
154  float _dt; // sampling time [s]
155  bool _grounded{true};// whether the vehicle is on the ground
156 
157  matrix::Vector3f _T_B; // thrust force in body frame [N]
158  matrix::Vector3f _Fa_I; // aerodynamic force in inertial frame [N]
159  matrix::Vector3f _Mt_B; // thruster moments in the body frame [Nm]
160  matrix::Vector3f _Ma_B; // aerodynamic moments in the body frame [Nm]
161  matrix::Vector3f _p_I; // inertial position [m]
162  matrix::Vector3f _v_I; // inertial velocity [m/s]
163  matrix::Vector3f _v_B; // body frame velocity [m/s]
164  matrix::Vector3f _p_I_dot; // inertial position differential
165  matrix::Vector3f _v_I_dot; // inertial velocity differential
166  matrix::Quatf _q; // quaternion attitude
167  matrix::Dcmf _C_IB; // body to inertial transformation
168  matrix::Vector3f _w_B; // body rates in body frame [rad/s]
169  matrix::Quatf _q_dot; // quaternion differential
170  matrix::Vector3f _w_B_dot; // body rates differential
171  float _u[NB_MOTORS]; // thruster signals
172 
173 
174  // sensors reconstruction
182  float _baro_p_mBar; // reconstructed (simulated) pressure in mBar
183  float _baro_temp_c; // reconstructed (simulated) barometer temperature in celcius
184 
185  // parameters
188  matrix::Vector3f _W_I; // weight of the vehicle in inertial frame [N]
189  matrix::Matrix3f _I; // vehicle inertia matrix
190  matrix::Matrix3f _Im1; // inverse of the intertia matrix
191  matrix::Vector3f _mu_I; // NED magnetic field in inertial frame [G]
192 
193  // parameters defined in sih_params.c
194  DEFINE_PARAMETERS(
195  (ParamFloat<px4::params::SIH_MASS>) _sih_mass,
196  (ParamFloat<px4::params::SIH_IXX>) _sih_ixx,
197  (ParamFloat<px4::params::SIH_IYY>) _sih_iyy,
198  (ParamFloat<px4::params::SIH_IZZ>) _sih_izz,
199  (ParamFloat<px4::params::SIH_IXY>) _sih_ixy,
200  (ParamFloat<px4::params::SIH_IXZ>) _sih_ixz,
201  (ParamFloat<px4::params::SIH_IYZ>) _sih_iyz,
202  (ParamFloat<px4::params::SIH_T_MAX>) _sih_t_max,
203  (ParamFloat<px4::params::SIH_Q_MAX>) _sih_q_max,
204  (ParamFloat<px4::params::SIH_L_ROLL>) _sih_l_roll,
205  (ParamFloat<px4::params::SIH_L_PITCH>) _sih_l_pitch,
206  (ParamFloat<px4::params::SIH_KDV>) _sih_kdv,
207  (ParamFloat<px4::params::SIH_KDW>) _sih_kdw,
208  (ParamInt<px4::params::SIH_LOC_LAT0>) _sih_lat0,
209  (ParamInt<px4::params::SIH_LOC_LON0>) _sih_lon0,
210  (ParamFloat<px4::params::SIH_LOC_H0>) _sih_h0,
211  (ParamFloat<px4::params::SIH_LOC_MU_X>) _sih_mu_x,
212  (ParamFloat<px4::params::SIH_LOC_MU_Y>) _sih_mu_y,
213  (ParamFloat<px4::params::SIH_LOC_MU_Z>) _sih_mu_z
214  )
215 };
uORB::Subscription _parameter_update_sub
Definition: sih.hpp:123
float _gps_alt_noiseless
Definition: sih.hpp:181
matrix::Matrix3f _I
Definition: sih.hpp:189
perf_counter_t _sampling_perf
Definition: sih.hpp:145
static int task_spawn(int argc, char *argv[])
Definition: sih.cpp:72
static constexpr float CONSTANTS_ABSOLUTE_NULL_CELSIUS
Definition: geo.h:59
void send_gps()
Definition: sih.cpp:383
float _L_ROLL
Definition: sih.hpp:186
hrt_abstime _now
Definition: sih.hpp:153
matrix::Vector3f _Mt_B
Definition: sih.hpp:159
float _u[NB_MOTORS]
Definition: sih.hpp:171
Definition of geo / math functions to perform geodesic calculations.
void equations_of_motion()
Definition: sih.cpp:286
static float generate_wgn()
Definition: sih.cpp:454
static constexpr uint16_t NB_MOTORS
Definition: sih.hpp:127
matrix::Vector3f _acc
Definition: sih.hpp:175
float _KDW
Definition: sih.hpp:186
matrix::Vector3f _w_B_dot
Definition: sih.hpp:170
matrix::Quatf _q
Definition: sih.hpp:166
void inner_loop()
Definition: sih.cpp:154
vehicle_global_position_s _gpos_gt
Definition: sih.hpp:120
Definition: I2C.hpp:51
static matrix::Vector3f noiseGauss3f(float stdx, float stdy, float stdz)
Definition: sih.cpp:484
orb_advert_t _att_gt_pub
Definition: sih.hpp:117
float _T_MAX
Definition: sih.hpp:186
matrix::Vector3f _T_B
Definition: sih.hpp:157
static constexpr float T1_C
Definition: sih.hpp:128
Sih()
Definition: sih.cpp:100
matrix::Vector3f _W_I
Definition: sih.hpp:188
matrix::Matrix3f _Im1
Definition: sih.hpp:190
int print_status() override
Definition: sih.cpp:60
Vector rotation library.
Quaternion class.
Definition: Dcm.hpp:24
High-resolution timer with callouts and timekeeping.
orb_advert_t _vehicle_angular_velocity_gt_pub
Definition: sih.hpp:113
float _MASS
Definition: sih.hpp:186
matrix::Vector3f _w_B
Definition: sih.hpp:168
matrix::Vector3f _mu_I
Definition: sih.hpp:191
float _H0
Definition: sih.hpp:186
static int print_usage(const char *reason=nullptr)
Definition: sih.cpp:494
__EXPORT int sih_main(int argc, char *argv[])
Definition: sih.cpp:489
float _dt
Definition: sih.hpp:154
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
double _COS_LAT0
Definition: sih.hpp:187
PX4Accelerometer _px4_accel
Definition: sih.hpp:102
void run() override
Definition: sih.cpp:107
PX4Gyroscope _px4_gyro
Definition: sih.hpp:103
vehicle_attitude_s _att_gt
Definition: sih.hpp:116
static constexpr hrt_abstime LOOP_INTERVAL
Definition: sih.hpp:131
void publish_sih()
Definition: sih.cpp:407
Header common to all counters.
double _gps_lat_noiseless
Definition: sih.hpp:179
bool _grounded
Definition: sih.hpp:155
hrt_abstime _last_run
Definition: sih.hpp:150
void reconstruct_sensors_signals()
Definition: sih.cpp:322
PX4Magnetometer _px4_mag
Definition: sih.hpp:104
static constexpr float TEMP_GRADIENT
Definition: sih.hpp:130
int _actuator_out_sub
Definition: sih.hpp:124
matrix::Vector3f _p_I
Definition: sih.hpp:161
void send_IMU()
Definition: sih.cpp:350
matrix::Vector3f _v_I_dot
Definition: sih.hpp:165
static constexpr float T1_K
Definition: sih.hpp:129
float _gps_alt
Definition: sih.hpp:181
float _baro_p_mBar
Definition: sih.hpp:182
orb_advert_t _gpos_gt_pub
Definition: sih.hpp:121
void read_motors()
Definition: sih.cpp:256
static Sih * instantiate(int argc, char *argv[])
Definition: sih.cpp:89
double _gps_lon_noiseless
Definition: sih.hpp:180
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
double _LAT0
Definition: sih.hpp:187
virtual ~Sih()=default
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
px4_sem_t _data_semaphore
Definition: sih.hpp:147
matrix::Vector3f _p_I_dot
Definition: sih.hpp:164
float _L_PITCH
Definition: sih.hpp:186
matrix::Vector3f _Fa_I
Definition: sih.hpp:158
matrix::Quatf _q_dot
Definition: sih.hpp:169
perf_counter_t _loop_perf
Definition: sih.hpp:144
matrix::Vector3f _gyro
Definition: sih.hpp:177
void init_variables()
Definition: sih.cpp:229
void parameters_updated()
Definition: sih.cpp:200
hrt_abstime _gps_time
Definition: sih.hpp:151
void parameters_update_poll()
Check for parameter changes and update them if needed.
Definition: sih.cpp:185
hrt_call _timer_call
Definition: sih.hpp:149
float _Q_MAX
Definition: sih.hpp:186
matrix::Vector3f _gps_vel
Definition: sih.hpp:178
float _KDV
Definition: sih.hpp:186
float _baro_temp_c
Definition: sih.hpp:183
Definition: sih.hpp:58
Callout record.
Definition: drv_hrt.h:72
void generate_force_and_torques()
Definition: sih.cpp:274
matrix::Vector3f _v_I
Definition: sih.hpp:162
static int custom_command(int argc, char *argv[])
Definition: sih.cpp:66
matrix::Vector3f _Ma_B
Definition: sih.hpp:160
matrix::Vector3f _v_B
Definition: sih.hpp:163
hrt_abstime _serial_time
Definition: sih.hpp:152
matrix::Dcmf _C_IB
Definition: sih.hpp:167
double _gps_lat
Definition: sih.hpp:179
orb_advert_t _vehicle_gps_pos_pub
Definition: sih.hpp:109
vehicle_gps_position_s _vehicle_gps_pos
Definition: sih.hpp:108
matrix::Vector3f _mag
Definition: sih.hpp:176
double _LON0
Definition: sih.hpp:187
vehicle_angular_velocity_s _vehicle_angular_velocity_gt
Definition: sih.hpp:112
double _gps_lon
Definition: sih.hpp:180
static void timer_callback(void *sem)
Definition: sih.cpp:148
Performance measuring tools.
void init_sensors()
Definition: sih.cpp:241
PX4Barometer _px4_baro
Definition: sih.hpp:105