Skip to content

Commit 09eaef8

Browse files
committed
sih: move to PX4Accelerometer/PX4Gyroscope/PX4Magnetometer/PX4Barometer helpers
1 parent f432f74 commit 09eaef8

File tree

3 files changed

+43
-77
lines changed

3 files changed

+43
-77
lines changed

src/modules/sih/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@ px4_add_module(
4141
sih.cpp
4242
DEPENDS
4343
mathlib
44+
drivers_accelerometer
45+
drivers_barometer
46+
drivers_gyroscope
47+
drivers_magnetometer
4448
)

src/modules/sih/sih.cpp

+28-58
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include <px4_log.h>
4747

4848
#include <drivers/drv_pwm_output.h> // to get PWM flags
49+
#include <uORB/topics/actuator_outputs.h>
4950
#include <uORB/topics/vehicle_status.h> // to get the HIL status
5051

5152
#include <unistd.h>
@@ -283,28 +284,6 @@ void Sih::init_variables()
283284

284285
void Sih::init_sensors()
285286
{
286-
287-
_sensor_accel.device_id=1;
288-
_sensor_accel.error_count=0;
289-
_sensor_accel.integral_dt=0;
290-
_sensor_accel.temperature=T1_C;
291-
_sensor_accel.scaling=0.0f;
292-
293-
_sensor_gyro.device_id=1;
294-
_sensor_gyro.error_count=0;
295-
_sensor_gyro.integral_dt=0;
296-
_sensor_gyro.temperature=T1_C;
297-
_sensor_gyro.scaling=0.0f;
298-
299-
_sensor_mag.device_id=1;
300-
_sensor_mag.error_count=0;
301-
_sensor_mag.temperature=T1_C;
302-
_sensor_mag.scaling=0.0f;
303-
_sensor_mag.is_external=false;
304-
305-
_sensor_baro.error_count=0;
306-
_sensor_baro.device_id=1;
307-
308287
_vehicle_gps_pos.fix_type=3; // 3D fix
309288
_vehicle_gps_pos.satellites_used=8;
310289
_vehicle_gps_pos.heading=NAN;
@@ -410,44 +389,35 @@ void Sih::reconstruct_sensors_signals()
410389

411390
void Sih::send_IMU()
412391
{
413-
_sensor_accel.timestamp=_now;
414-
_sensor_accel.x=_acc(0);
415-
_sensor_accel.y=_acc(1);
416-
_sensor_accel.z=_acc(2);
417-
if (_sensor_accel_pub != nullptr) {
418-
orb_publish(ORB_ID(sensor_accel), _sensor_accel_pub, &_sensor_accel);
419-
} else {
420-
_sensor_accel_pub = orb_advertise(ORB_ID(sensor_accel), &_sensor_accel);
421-
}
392+
// gyro
393+
{
394+
static constexpr float scaling = 1000.0f;
395+
_px4_gyro.set_scale(1 / scaling);
396+
_px4_gyro.set_temperature(T1_C);
397+
_px4_gyro.update(_now, _gyro(0) * scaling, _gyro(1) * scaling, _gyro(2) * scaling);
398+
}
422399

423-
_sensor_gyro.timestamp=_now;
424-
_sensor_gyro.x=_gyro(0);
425-
_sensor_gyro.y=_gyro(1);
426-
_sensor_gyro.z=_gyro(2);
427-
if (_sensor_gyro_pub != nullptr) {
428-
orb_publish(ORB_ID(sensor_gyro), _sensor_gyro_pub, &_sensor_gyro);
429-
} else {
430-
_sensor_gyro_pub = orb_advertise(ORB_ID(sensor_gyro), &_sensor_gyro);
431-
}
400+
// accel
401+
{
402+
static constexpr float scaling = 1000.0f;
403+
_px4_accel.set_scale(1 / scaling);
404+
_px4_accel.set_temperature(T1_C);
405+
_px4_accel.update(_now, _acc(0) * scaling, _acc(1) * scaling, _acc(2) * scaling);
406+
}
432407

433-
_sensor_mag.timestamp=_now;
434-
_sensor_mag.x=_mag(0);
435-
_sensor_mag.y=_mag(1);
436-
_sensor_mag.z=_mag(2);
437-
if (_sensor_mag_pub != nullptr) {
438-
orb_publish(ORB_ID(sensor_mag), _sensor_mag_pub, &_sensor_mag);
439-
} else {
440-
_sensor_mag_pub = orb_advertise(ORB_ID(sensor_mag), &_sensor_mag);
441-
}
408+
// magnetometer
409+
{
410+
static constexpr float scaling = 1000.0f;
411+
_px4_mag.set_scale(1 / scaling);
412+
_px4_mag.set_temperature(T1_C);
413+
_px4_mag.update(_now, _mag(0) * scaling, _mag(1) * scaling, _mag(2) * scaling);
414+
}
442415

443-
_sensor_baro.timestamp=_now;
444-
_sensor_baro.pressure=_baro_p_mBar;
445-
_sensor_baro.temperature=_baro_temp_c;
446-
if (_sensor_baro_pub != nullptr) {
447-
orb_publish(ORB_ID(sensor_baro), _sensor_baro_pub, &_sensor_baro);
448-
} else {
449-
_sensor_baro_pub = orb_advertise(ORB_ID(sensor_baro), &_sensor_baro);
450-
}
416+
// baro
417+
{
418+
_px4_baro.set_temperature(_baro_temp_c);
419+
_px4_baro.update(_now, _baro_p_mBar);
420+
}
451421
}
452422

453423
void Sih::send_gps()
@@ -501,7 +471,7 @@ void Sih::publish_sih()
501471
} else {
502472
_att_gt_sub = orb_advertise(ORB_ID(vehicle_attitude_groundtruth), &_att_gt);
503473
}
504-
}
474+
}
505475

506476
float Sih::generate_wgn() // generate white Gaussian noise sample with std=1
507477
{

src/modules/sih/sih.hpp

+11-19
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,15 @@
4141
#include <conversion/rotation.h> // math::radians,
4242
#include <ecl/geo/geo.h> // to get the physical constants
4343
#include <drivers/drv_hrt.h> // to get the real time
44+
#include <lib/drivers/accelerometer/PX4Accelerometer.hpp>
45+
#include <lib/drivers/barometer/PX4Barometer.hpp>
46+
#include <lib/drivers/gyroscope/PX4Gyroscope.hpp>
47+
#include <lib/drivers/magnetometer/PX4Magnetometer.hpp>
4448
#include <perf/perf_counter.h>
45-
4649
#include <uORB/topics/parameter_update.h>
47-
#include <uORB/topics/actuator_outputs.h>
48-
#include <uORB/topics/sensor_baro.h>
49-
#include <uORB/topics/sensor_gyro.h>
50-
#include <uORB/topics/sensor_accel.h>
51-
#include <uORB/topics/sensor_mag.h>
52-
#include <uORB/topics/vehicle_gps_position.h>
5350
#include <uORB/topics/vehicle_global_position.h> // to publish groundtruth
5451
#include <uORB/topics/vehicle_attitude.h> // to publish groundtruth
52+
#include <uORB/topics/vehicle_gps_position.h>
5553

5654
extern "C" __EXPORT int sih_main(int argc, char *argv[]);
5755

@@ -98,18 +96,12 @@ class Sih : public ModuleBase<Sih>, public ModuleParams
9896
void parameters_update_poll();
9997
void parameters_updated();
10098

101-
// to publish the sensor baro
102-
struct sensor_baro_s _sensor_baro {};
103-
orb_advert_t _sensor_baro_pub{nullptr};
104-
// to publish the sensor mag
105-
struct sensor_mag_s _sensor_mag {};
106-
orb_advert_t _sensor_mag_pub{nullptr};
107-
// to publish the sensor gyroscope
108-
struct sensor_gyro_s _sensor_gyro {};
109-
orb_advert_t _sensor_gyro_pub{nullptr};
110-
// to publish the sensor accelerometer
111-
struct sensor_accel_s _sensor_accel {};
112-
orb_advert_t _sensor_accel_pub{nullptr};
99+
// simulated sensor instances
100+
PX4Accelerometer _px4_accel{ 1311244, ORB_PRIO_DEFAULT, ROTATION_NONE }; // 1311244: DRV_ACC_DEVTYPE_ACCELSIM, BUS: 1, ADDR: 1, TYPE: SIMULATION
101+
PX4Gyroscope _px4_gyro{ 2294028, ORB_PRIO_DEFAULT, ROTATION_NONE }; // 2294028: DRV_GYR_DEVTYPE_GYROSIM, BUS: 1, ADDR: 2, TYPE: SIMULATION
102+
PX4Magnetometer _px4_mag{ 197388, ORB_PRIO_DEFAULT, ROTATION_NONE }; // 197388: DRV_MAG_DEVTYPE_MAGSIM, BUS: 3, ADDR: 1, TYPE: SIMULATION
103+
PX4Barometer _px4_baro{ 6620172, ORB_PRIO_DEFAULT }; // 6620172: DRV_BARO_DEVTYPE_BAROSIM, BUS: 1, ADDR: 4, TYPE: SIMULATION
104+
113105
// to publish the gps position
114106
struct vehicle_gps_position_s _vehicle_gps_pos {};
115107
orb_advert_t _vehicle_gps_pos_pub{nullptr};

0 commit comments

Comments
 (0)