simulator in hardware, new module added that allows to run a simulator in the hardware autopilot, for more documentation visit https://github.com/romain-chiap/PX4_SIH_QuadX

This commit is contained in:
romain
2019-02-06 10:57:12 -05:00
committed by Beat Küng
parent 4fe9ac9993
commit c09e9ec97f
7 changed files with 1232 additions and 0 deletions
+1
View File
@@ -75,6 +75,7 @@ px4_add_board(
mc_pos_control
navigator
sensors
sih
vmount
vtol_att_control
wind_estimator
+1
View File
@@ -107,6 +107,7 @@ set(msg_files
sensor_preflight.msg
sensor_selection.msg
servorail_status.msg
sih.msg
subsystem_info.msg
system_power.msg
task_stack_info.msg
+9
View File
@@ -0,0 +1,9 @@
# simulator in Hardware - Romain Chiappinelli - Jan 8, 2019
uint64 timestamp # time since system start (microseconds)
uint32 dt_us # simulator sampling time [us]
float32[3] euler_rpy # euler angles (roll-pitch-yaw) [deg]
float32[3] omega_b # body rates in body frame [rad/s]
float32[3] p_i_local # local inertial position [m]
float32[3] v_i # inertial velocity [m]
float32[4] u # motor signals [0;1]
+44
View File
@@ -0,0 +1,44 @@
############################################################################
#
# Copyright (c) 2015 PX4 Development Team. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name PX4 nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
px4_add_module(
MODULE modules__sih
MAIN sih
STACK_MAIN 1200
STACK_MAX 3500
COMPILE_FLAGS
SRCS
sih.cpp
DEPENDS
mathlib
)
File diff suppressed because it is too large Load Diff
+200
View File
@@ -0,0 +1,200 @@
/****************************************************************************
*
* Copyright (c) 2018 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#pragma once
#include <px4_module.h>
#include <px4_module_params.h>
#include <matrix/matrix/math.hpp> // matrix, vectors, dcm, quaterions
#include <conversion/rotation.h> // math::radians,
#include <ecl/geo/geo.h> // to get the physical constants
#include <drivers/drv_hrt.h> // to get the real time
#include <uORB/topics/parameter_update.h>
#include <uORB/topics/actuator_outputs.h>
#include <uORB/topics/sensor_baro.h>
#include <uORB/topics/sensor_gyro.h>
#include <uORB/topics/sensor_accel.h>
#include <uORB/topics/sensor_mag.h>
#include <uORB/topics/vehicle_gps_position.h>
#include <uORB/topics/sih.h>
using namespace matrix;
extern "C" __EXPORT int sih_main(int argc, char *argv[]);
class Sih : public ModuleBase<Sih>, public ModuleParams
{
public:
Sih(int example_param, bool example_flag);
virtual ~Sih() = default;
/** @see ModuleBase */
static int task_spawn(int argc, char *argv[]);
/** @see ModuleBase */
static Sih *instantiate(int argc, char *argv[]);
/** @see ModuleBase */
static int custom_command(int argc, char *argv[]);
/** @see ModuleBase */
static int print_usage(const char *reason = nullptr);
/** @see ModuleBase::run() */
void run() override;
/** @see ModuleBase::print_status() */
int print_status() override;
static float generate_wgn(); // generate white Gaussian noise sample
// generate white Gaussian noise sample as a 3D vector with specified std
static Vector3f noiseGauss3f(float stdx, float stdy, float stdz);
// static int pack_float(char* uart_msg, int index, void *value); // pack a float to a IEEE754
private:
/**
* Check for parameter changes and update them if needed.
* @param parameter_update_sub uorb subscription to parameter_update
* @param force for a parameter update
*/
void parameters_update_poll(int parameter_update_sub);
void parameters_updated();
uint8_t is_HIL_running(int vehicle_status_sub);
// to publish the simulator states
struct sih_s _sih {};
orb_advert_t _sih_pub{nullptr};
// to publish the sensor baro
struct sensor_baro_s _sensor_baro {};
orb_advert_t _sensor_baro_pub{nullptr};
// to publish the sensor mag
struct sensor_mag_s _sensor_mag {};
orb_advert_t _sensor_mag_pub{nullptr};
// to publish the sensor gyroscope
struct sensor_gyro_s _sensor_gyro {};
orb_advert_t _sensor_gyro_pub{nullptr};
// to publish the sensor accelerometer
struct sensor_accel_s _sensor_accel {};
orb_advert_t _sensor_accel_pub{nullptr};
// to publish the gps position
struct vehicle_gps_position_s _vehicle_gps_pos {};
orb_advert_t _vehicle_gps_pos_pub{nullptr};
// hard constants
static constexpr uint16_t NB_MOTORS = 4;
static constexpr float T1_C = 15.0f; // ground temperature in celcius
static constexpr float T1_K = T1_C - CONSTANTS_ABSOLUTE_NULL_CELSIUS; // ground temperature in Kelvin
static constexpr float TEMP_GRADIENT = -6.5f / 1000.0f; // temperature gradient in degrees per metre
static constexpr uint32_t BAUDS_RATE = 57600; // bauds rate of the serial port
void init_variables();
void init_sensors();
int init_serial_port();
void read_motors(const int actuator_out_sub);
void generate_force_and_torques();
void equations_of_motion();
void reconstruct_sensors_signals();
void send_IMU(hrt_abstime now);
void send_gps(hrt_abstime now);
void publish_sih();
void send_serial_msg(int serial_fd, int64_t t_ms);
float _dt; // sampling time [s]
char _uart_name[12] = "/dev/ttyS5/"; // serial port name
Vector3f _T_B; // thrust force in body frame [N]
Vector3f _Fa_I; // aerodynamic force in inertial frame [N]
Vector3f _Mt_B; // thruster moments in the body frame [Nm]
Vector3f _Ma_B; // aerodynamic moments in the body frame [Nm]
Vector3f _p_I; // inertial position [m]
Vector3f _v_I; // inertial velocity [m/s]
Vector3f _v_B; // body frame velocity [m/s]
Vector3f _p_I_dot; // inertial position differential
Vector3f _v_I_dot; // inertial velocity differential
Quatf _q; // quaternion attitude
Dcmf _C_IB; // body to inertial transformation
Vector3f _w_B; // body rates in body frame [rad/s]
Quatf _q_dot; // quaternion differential
Vector3f _w_B_dot; // body rates differential
float _u[NB_MOTORS]; // thruster signals
// sensors reconstruction
Vector3f _acc;
Vector3f _mag;
Vector3f _gyro;
Vector3f _gps_vel;
double _gps_lat, _gps_lat_noiseless;
double _gps_lon, _gps_lon_noiseless;
float _gps_alt, _gps_alt_noiseless;
float _baro_p_mBar; // reconstructed (simulated) pressure in mBar
float _baro_temp_c; // reconstructed (simulated) barometer temperature in celcius
// parameters
float _MASS, _T_MAX, _Q_MAX, _L_ROLL, _L_PITCH, _KDV, _KDW, _H0;
double _LAT0, _LON0, _COS_LAT0;
Vector3f _W_I; // weight of the vehicle in inertial frame [N]
Matrix3f _I; // vehicle inertia matrix
Matrix3f _Im1; // inverse of the intertia matrix
Vector3f _mu_I; // NED magnetic field in inertial frame [G]
// parameters defined in sih_params.c
DEFINE_PARAMETERS(
(ParamFloat<px4::params::SIH_MASS>) _sih_mass,
(ParamFloat<px4::params::SIH_IXX>) _sih_ixx,
(ParamFloat<px4::params::SIH_IYY>) _sih_iyy,
(ParamFloat<px4::params::SIH_IZZ>) _sih_izz,
(ParamFloat<px4::params::SIH_IXY>) _sih_ixy,
(ParamFloat<px4::params::SIH_IXZ>) _sih_ixz,
(ParamFloat<px4::params::SIH_IYZ>) _sih_iyz,
(ParamFloat<px4::params::SIH_T_MAX>) _sih_t_max,
(ParamFloat<px4::params::SIH_Q_MAX>) _sih_q_max,
(ParamFloat<px4::params::SIH_L_ROLL>) _sih_l_roll,
(ParamFloat<px4::params::SIH_L_PITCH>) _sih_l_pitch,
(ParamFloat<px4::params::SIH_KDV>) _sih_kdv,
(ParamFloat<px4::params::SIH_KDW>) _sih_kdw,
(ParamInt<px4::params::SIH_LAT0>) _sih_lat0,
(ParamInt<px4::params::SIH_LON0>) _sih_lon0,
(ParamFloat<px4::params::SIH_H0>) _sih_h0,
(ParamFloat<px4::params::SIH_MU_X>) _sih_mu_x,
(ParamFloat<px4::params::SIH_MU_Y>) _sih_mu_y,
(ParamFloat<px4::params::SIH_MU_Z>) _sih_mu_z
)
};
+345
View File
@@ -0,0 +1,345 @@
/****************************************************************************
*
* Copyright (c) 2013-2015 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file sih_params.c
* Parameters for quadcopter X simulator in hardware.
*
* @author Romain Chiappinelli <romain.chiap@gmail.com>
* February 2019
*/
/**
* Vehicle mass
*
* This value can be measured by weighting the quad on a scale.
*
* @unit kg
* @min 0.0
* @decimal 2
* @increment 0.1
* @group SIH
*/
PARAM_DEFINE_FLOAT(SIH_MASS, 1.0f);
/**
* Vehicle inertia about X axis
*
* The intertia is a 3 by 3 symmetric matrix.
* It represents the difficulty of the vehicle to modify its angular rate.
*
* @unit kg*m*m
* @min 0.0
* @decimal 3
* @increment 0.005
* @group SIH
*/
PARAM_DEFINE_FLOAT(SIH_IXX, 0.025f);
/**
* Vehicle inertia about Y axis
*
* The intertia is a 3 by 3 symmetric matrix.
* It represents the difficulty of the vehicle to modify its angular rate.
*
* @unit kg*m*m
* @min 0.0
* @decimal 3
* @increment 0.005
* @group SIH
*/
PARAM_DEFINE_FLOAT(SIH_IYY, 0.025f);
/**
* Vehicle inertia about Z axis
*
* The intertia is a 3 by 3 symmetric matrix.
* It represents the difficulty of the vehicle to modify its angular rate.
*
* @unit kg*m*m
* @min 0.0
* @decimal 3
* @increment 0.005
* @group SIH
*/
PARAM_DEFINE_FLOAT(SIH_IZZ, 0.030f);
/**
* Vehicle cross term inertia xy
*
* The intertia is a 3 by 3 symmetric matrix.
* This value can be set to 0 for a quad symmetric about its center of mass.
*
* @unit kg*m*m
* @decimal 3
* @increment 0.005
* @group SIH
*/
PARAM_DEFINE_FLOAT(SIH_IXY, 0.0f);
/**
* Vehicle cross term inertia xz
*
* The intertia is a 3 by 3 symmetric matrix.
* This value can be set to 0 for a quad symmetric about its center of mass.
*
* @unit kg*m*m
* @decimal 3
* @increment 0.005
* @group SIH
*/
PARAM_DEFINE_FLOAT(SIH_IXZ, 0.0f);
/**
* Vehicle cross term inertia yz
*
* The intertia is a 3 by 3 symmetric matrix.
* This value can be set to 0 for a quad symmetric about its center of mass.
*
* @unit kg*m*m
* @decimal 3
* @increment 0.005
* @group SIH
*/
PARAM_DEFINE_FLOAT(SIH_IYZ, 0.0f);
/**
* Max propeller thrust force
*
* This is the maximum force delivered by one propeller
* when the motor is running at full speed.
*
* This value is usually about 5 times the mass of the quadrotor.
*
* @unit N
* @min 0.0
* @decimal 2
* @increment 0.5
* @group SIH
*/
PARAM_DEFINE_FLOAT(SIH_T_MAX, 5.0f);
/**
* Max propeller torque
*
* This is the maximum torque delivered by one propeller
* when the motor is running at full speed.
*
* This value is usually about few percent of the maximum thrust force.
*
* @unit Nm
* @min 0.0
* @decimal 3
* @increment 0.05
* @group SIH
*/
PARAM_DEFINE_FLOAT(SIH_Q_MAX, 0.1f);
/**
* Roll arm length
*
* This is the arm length generating the rolling moment
*
* This value can be measured with a ruler.
* This corresponds to half the distance between the left and right motors.
*
* @unit m
* @min 0.0
* @decimal 2
* @increment 0.05
* @group SIH
*/
PARAM_DEFINE_FLOAT(SIH_L_ROLL, 0.2f);
/**
* Pitch arm length
*
* This is the arm length generating the pitching moment
*
* This value can be measured with a ruler.
* This corresponds to half the distance between the front and rear motors.
*
* @unit m
* @min 0.0
* @decimal 2
* @increment 0.05
* @group SIH
*/
PARAM_DEFINE_FLOAT(SIH_L_PITCH, 0.2f);
/**
* First order drag coefficient
*
* Physical coefficient representing the friction with air particules.
* The greater this value, the slower the quad will move.
*
* Drag force function of velocity: D=-KDV*V.
* The maximum freefall velocity can be computed as V=10*MASS/KDV [m/s]
*
* @unit N/(m/s)
* @min 0.0
* @decimal 2
* @increment 0.05
* @group SIH
*/
PARAM_DEFINE_FLOAT(SIH_KDV, 1.0f);
/**
* First order angular damper coefficient
*
* Physical coefficient representing the friction with air particules during rotations.
* The greater this value, the slower the quad will rotate.
*
* Aerodynamic moment function of body rate: Ma=-KDW*W_B.
* This value can be set to 0 if unknown.
*
* @unit Nm/(rad/s)
* @min 0.0
* @decimal 3
* @increment 0.005
* @group SIH
*/
PARAM_DEFINE_FLOAT(SIH_KDW, 0.025f);
/**
* Initial geodetic latitude
*
* This value represents the North-South location on Earth where the simulation begins.
* A value of 45 deg should be written 450000000.
*
* LAT0, LON0, H0, MU_X, MU_Y, and MU_Z should ideally be consistent among each others
* to represent a physical ground location on Earth.
*
* @unit 1e-7 deg
* @min -850000000
* @max 850000000
* @group SIH
*/
PARAM_DEFINE_INT32(SIH_LAT0, 454671160);
/**
* Initial geodetic longitude
*
* This value represents the East-West location on Earth where the simulation begins.
* A value of 45 deg should be written 450000000.
*
* LAT0, LON0, H0, MU_X, MU_Y, and MU_Z should ideally be consistent among each others
* to represent a physical ground location on Earth.
*
* @unit 1e-7 deg
* @min -1800000000
* @max 1800000000
* @group SIH
*/
PARAM_DEFINE_INT32(SIH_LON0, -737578370);
/**
* Initial AMSL ground altitude
*
* This value represents the Above Mean Sea Level (AMSL) altitude where the simulation begins.
*
* If using FlightGear as a visual animation,
* this value can be tweaked such that the vehicle lies on the ground at takeoff.
*
* LAT0, LON0, H0, MU_X, MU_Y, and MU_Z should ideally be consistent among each others
* to represent a physical ground location on Earth.
*
*
* @unit m
* @min -420.0
* @max 8848.0
* @decimal 2
* @increment 0.01
* @group SIH
*/
PARAM_DEFINE_FLOAT(SIH_H0, 32.34f);
/**
* North magnetic field at the initial location
*
* This value represents the North magnetic field at the initial location.
*
* A magnetic field calculator can be found on the NOAA website
* Note, the values need to be converted from nano Tesla to Gauss
*
* LAT0, LON0, H0, MU_X, MU_Y, and MU_Z should ideally be consistent among each others
* to represent a physical ground location on Earth.
*
* @unit Gauss
* @min -1.0
* @max 1.0
* @decimal 2
* @increment 0.001
* @group SIH
*/
PARAM_DEFINE_FLOAT(SIH_MU_X, 0.179f);
/**
* East magnetic field at the initial location
*
* This value represents the East magnetic field at the initial location.
*
* A magnetic field calculator can be found on the NOAA website
* Note, the values need to be converted from nano Tesla to Gauss
*
* LAT0, LON0, H0, MU_X, MU_Y, and MU_Z should ideally be consistent among each others
* to represent a physical ground location on Earth.
*
* @unit Gauss
* @min -1.0
* @max 1.0
* @decimal 2
* @increment 0.001
* @group SIH
*/
PARAM_DEFINE_FLOAT(SIH_MU_Y, -0.045f);
/**
* Down magnetic field at the initial location
*
* This value represents the Down magnetic field at the initial location.
*
* A magnetic field calculator can be found on the NOAA website
* Note, the values need to be converted from nano Tesla to Gauss
*
* LAT0, LON0, H0, MU_X, MU_Y, and MU_Z should ideally be consistent among each others
* to represent a physical ground location on Earth.
*
* @unit Gauss
* @min -1.0
* @max 1.0
* @decimal 2
* @increment 0.001
* @group SIH
*/
PARAM_DEFINE_FLOAT(SIH_MU_Z, 0.504f);