mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-11-25 16:51:16 +08:00
feat(simulation): enable airspeed sensor failure injection on gazebo too (#29210)
* feat(simulation): enable failure injection on gazebo sensor too * docs(failure_injection): remove added dot
This commit is contained in:
1 parent
5729cf1e3d
commit
e1e1122af5
9 files changed
+265
-74
No files matched your search
@@ -581,6 +581,7 @@ void GZBridge::airPressureCallback(const gz::msgs::FluidPressure &msg, uint8_t i
|
||||
void GZBridge::airspeedCallback(const gz::msgs::AirSpeed &msg)
|
||||
{
|
||||
const uint64_t timestamp = hrt_absolute_time();
|
||||
_failure_config.update();
|
||||
|
||||
device::Device::DeviceId id{};
|
||||
id.devid_s.bus_type = device::Device::DeviceBusType::DeviceBusType_SIMULATION;
|
||||
@@ -596,7 +597,10 @@ void GZBridge::airspeedCallback(const gz::msgs::AirSpeed &msg)
|
||||
_temperature = static_cast<float>(msg.temperature()) + atmosphere::kAbsoluteNullCelsius; // K to C
|
||||
report.temperature = _temperature;
|
||||
report.pitot_temperature = NAN;
|
||||
_differential_pressure_pub.publish(report);
|
||||
|
||||
if (failure_injection::process_airspeed(_failure_config, 0, report, _airspeed_failure)) {
|
||||
_differential_pressure_pub.publish(report);
|
||||
}
|
||||
}
|
||||
|
||||
void GZBridge::imuCallback(const gz::msgs::IMU &msg, uint8_t instance_index)
|
||||
|
||||
@@ -188,6 +188,7 @@ private:
|
||||
failure_injection::Config _failure_config;
|
||||
failure_injection::Stuck<sensor_gnss_s> _gnss_stuck[_MAX_GNSS_SENSORS];
|
||||
failure_injection::Stuck<vehicle_odometry_s> _visual_odometry_stuck{};
|
||||
failure_injection::AirspeedState _airspeed_failure;
|
||||
|
||||
GZMixingInterfaceESC _mixing_interface_esc{_node};
|
||||
GZMixingInterfaceServo _mixing_interface_servo{_node};
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
|
||||
#include <uORB/Publication.hpp>
|
||||
#include <uORB/Subscription.hpp>
|
||||
#include <uORB/topics/differential_pressure.h>
|
||||
#include <uORB/topics/failure_injection.h>
|
||||
#include <uORB/topics/sensor_gnss.h>
|
||||
#include <uORB/topics/vehicle_odometry.h>
|
||||
@@ -53,6 +54,11 @@ public:
|
||||
{
|
||||
bridge.odometryCallback(message);
|
||||
}
|
||||
|
||||
static void airspeedCallback(GZBridge &bridge, const gz::msgs::AirSpeed &message)
|
||||
{
|
||||
bridge.airspeedCallback(message);
|
||||
}
|
||||
};
|
||||
|
||||
namespace
|
||||
@@ -88,6 +94,14 @@ gz::msgs::OdometryWithCovariance odometry(double east, double north, double up)
|
||||
return message;
|
||||
}
|
||||
|
||||
gz::msgs::AirSpeed airSpeed(double diff_pressure)
|
||||
{
|
||||
gz::msgs::AirSpeed message;
|
||||
message.set_diff_pressure(diff_pressure);
|
||||
message.set_temperature(288.15);
|
||||
return message;
|
||||
}
|
||||
|
||||
failure_injection_s sensorFailure(uint8_t unit, uint8_t failure_type)
|
||||
{
|
||||
failure_injection_s config{};
|
||||
@@ -113,6 +127,11 @@ failure_injection_s vioFailure(uint8_t failure_type)
|
||||
return sensorFailure(failure_injection_s::FAILURE_UNIT_SENSOR_VIO, failure_type);
|
||||
}
|
||||
|
||||
failure_injection_s airspeedFailure(uint8_t failure_type)
|
||||
{
|
||||
return sensorFailure(failure_injection_s::FAILURE_UNIT_SENSOR_AIRSPEED, failure_type);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(GZBridge, AppliesGpsFailureInjectionAtNavSatPublication)
|
||||
@@ -205,3 +224,43 @@ TEST(GZBridge, AppliesVioFailureInjectionAtOdometryPublication)
|
||||
EXPECT_FLOAT_EQ(recovered.position[1], 21.f);
|
||||
EXPECT_FLOAT_EQ(recovered.position[2], -23.f);
|
||||
}
|
||||
|
||||
TEST(GZBridge, AppliesAirspeedFailureInjectionAtAirspeedPublication)
|
||||
{
|
||||
GZBridge bridge{"default", "x500"};
|
||||
uORB::Publication<failure_injection_s> failure_pub{ORB_ID(failure_injection)};
|
||||
uORB::Subscription differential_pressure_sub{ORB_ID(differential_pressure)};
|
||||
|
||||
GZBridgeTestPeer::airspeedCallback(bridge, airSpeed(100.0));
|
||||
|
||||
differential_pressure_s baseline{};
|
||||
ASSERT_TRUE(differential_pressure_sub.update(&baseline));
|
||||
EXPECT_FLOAT_EQ(baseline.differential_pressure_pa, 100.f);
|
||||
|
||||
ASSERT_TRUE(failure_pub.publish(airspeedFailure(failure_injection_s::FAILURE_TYPE_STUCK)));
|
||||
GZBridgeTestPeer::airspeedCallback(bridge, airSpeed(200.0));
|
||||
|
||||
differential_pressure_s stuck{};
|
||||
ASSERT_TRUE(differential_pressure_sub.update(&stuck));
|
||||
EXPECT_FLOAT_EQ(stuck.differential_pressure_pa, baseline.differential_pressure_pa);
|
||||
|
||||
// Wrong starts the pitot blockage ramp, so the first sample is still unscaled
|
||||
// (the ramp itself is covered by the process_airspeed() tests).
|
||||
ASSERT_TRUE(failure_pub.publish(airspeedFailure(failure_injection_s::FAILURE_TYPE_WRONG)));
|
||||
GZBridgeTestPeer::airspeedCallback(bridge, airSpeed(200.0));
|
||||
|
||||
differential_pressure_s wrong{};
|
||||
ASSERT_TRUE(differential_pressure_sub.update(&wrong));
|
||||
EXPECT_FLOAT_EQ(wrong.differential_pressure_pa, 200.f);
|
||||
|
||||
ASSERT_TRUE(failure_pub.publish(airspeedFailure(failure_injection_s::FAILURE_TYPE_OFF)));
|
||||
GZBridgeTestPeer::airspeedCallback(bridge, airSpeed(200.0));
|
||||
EXPECT_FALSE(differential_pressure_sub.updated());
|
||||
|
||||
ASSERT_TRUE(failure_pub.publish(airspeedFailure(failure_injection_s::FAILURE_TYPE_OK)));
|
||||
GZBridgeTestPeer::airspeedCallback(bridge, airSpeed(200.0));
|
||||
|
||||
differential_pressure_s recovered{};
|
||||
ASSERT_TRUE(differential_pressure_sub.update(&recovered));
|
||||
EXPECT_FLOAT_EQ(recovered.differential_pressure_pa, 200.f);
|
||||
}
|
||||
@@ -80,61 +80,46 @@ void SensorAirspeedSim::Run()
|
||||
if (_vehicle_local_position_sub.updated() && _vehicle_global_position_sub.updated()
|
||||
&& _vehicle_attitude_sub.updated()) {
|
||||
|
||||
updateFailureConfig();
|
||||
_failure_config.update();
|
||||
|
||||
if (!_airspeed_disconnected) {
|
||||
vehicle_local_position_s lpos{};
|
||||
_vehicle_local_position_sub.copy(&lpos);
|
||||
|
||||
vehicle_local_position_s lpos{};
|
||||
_vehicle_local_position_sub.copy(&lpos);
|
||||
vehicle_global_position_s gpos{};
|
||||
_vehicle_global_position_sub.copy(&gpos);
|
||||
|
||||
vehicle_global_position_s gpos{};
|
||||
_vehicle_global_position_sub.copy(&gpos);
|
||||
vehicle_attitude_s attitude{};
|
||||
_vehicle_attitude_sub.copy(&attitude);
|
||||
|
||||
vehicle_attitude_s attitude{};
|
||||
_vehicle_attitude_sub.copy(&attitude);
|
||||
Vector3f local_velocity = Vector3f{lpos.vx, lpos.vy, lpos.vz};
|
||||
Vector3f body_velocity = Dcmf{Quatf{attitude.q}} .transpose() * local_velocity;
|
||||
|
||||
Vector3f local_velocity = Vector3f{lpos.vx, lpos.vy, lpos.vz};
|
||||
Vector3f body_velocity = Dcmf{Quatf{attitude.q}} .transpose() * local_velocity;
|
||||
// device id
|
||||
device::Device::DeviceId device_id;
|
||||
device_id.devid_s.bus_type = device::Device::DeviceBusType::DeviceBusType_SIMULATION;
|
||||
device_id.devid_s.bus = 0;
|
||||
device_id.devid_s.address = 0;
|
||||
device_id.devid_s.devtype = DRV_DIFF_PRESS_DEVTYPE_SIM;
|
||||
|
||||
// device id
|
||||
device::Device::DeviceId device_id;
|
||||
device_id.devid_s.bus_type = device::Device::DeviceBusType::DeviceBusType_SIMULATION;
|
||||
device_id.devid_s.bus = 0;
|
||||
device_id.devid_s.address = 0;
|
||||
device_id.devid_s.devtype = DRV_DIFF_PRESS_DEVTYPE_SIM;
|
||||
const float alt_amsl = gpos.alt;
|
||||
const float temperature_local = TEMPERATURE_MSL - LAPSE_RATE * alt_amsl;
|
||||
const float density_ratio = powf(TEMPERATURE_MSL / temperature_local, 4.256f);
|
||||
const float air_density = AIR_DENSITY_MSL / density_ratio;
|
||||
|
||||
const float alt_amsl = gpos.alt;
|
||||
const float temperature_local = TEMPERATURE_MSL - LAPSE_RATE * alt_amsl;
|
||||
const float density_ratio = powf(TEMPERATURE_MSL / temperature_local, 4.256f);
|
||||
const float air_density = AIR_DENSITY_MSL / density_ratio;
|
||||
// calculate differential pressure + noise in hPa
|
||||
const float diff_pressure_noise = (float)math::generate_wgn() * 0.01f;
|
||||
float diff_pressure = sign(body_velocity(0)) * 0.005f * air_density * body_velocity(0) * body_velocity(
|
||||
0) + diff_pressure_noise;
|
||||
|
||||
if (!_airspeed_stuck) {
|
||||
// calculate differential pressure + noise in hPa
|
||||
const float diff_pressure_noise = (float)math::generate_wgn() * 0.01f;
|
||||
float diff_pressure = sign(body_velocity(0)) * 0.005f * air_density * body_velocity(0) * body_velocity(
|
||||
0) + diff_pressure_noise;
|
||||
differential_pressure_s differential_pressure{};
|
||||
// report.timestamp_sample = time;
|
||||
differential_pressure.device_id = 1377548; // 1377548: DRV_DIFF_PRESS_DEVTYPE_SIM, BUS: 1, ADDR: 5, TYPE: SIMULATION
|
||||
differential_pressure.differential_pressure_pa = diff_pressure * 100.0f; // hPa to Pa
|
||||
differential_pressure.temperature = temperature_local + ABSOLUTE_ZERO_C; // K to C
|
||||
differential_pressure.pitot_temperature = NAN;
|
||||
differential_pressure.timestamp = hrt_absolute_time();
|
||||
|
||||
// simulate pitot blockage: ramp down differential pressure over time
|
||||
const float blockage_fraction = 0.7f; // max blockage (fully ramped)
|
||||
const float airspeed_blockage_rampup_time = 30_s;
|
||||
|
||||
float airspeed_blockage_scale = 1.f;
|
||||
|
||||
if (_airspeed_blocked_timestamp > 0) {
|
||||
airspeed_blockage_scale = math::constrain(1.f - (hrt_absolute_time() - _airspeed_blocked_timestamp) /
|
||||
airspeed_blockage_rampup_time, 1.f - blockage_fraction, 1.f);
|
||||
}
|
||||
|
||||
_last_differential_pressure_pa = diff_pressure * 100.0f * airspeed_blockage_scale; // hPa to Pa
|
||||
}
|
||||
|
||||
differential_pressure_s differential_pressure{};
|
||||
// report.timestamp_sample = time;
|
||||
differential_pressure.device_id = 1377548; // 1377548: DRV_DIFF_PRESS_DEVTYPE_SIM, BUS: 1, ADDR: 5, TYPE: SIMULATION
|
||||
differential_pressure.differential_pressure_pa = _last_differential_pressure_pa;
|
||||
differential_pressure.temperature = temperature_local + ABSOLUTE_ZERO_C; // K to C
|
||||
differential_pressure.pitot_temperature = NAN;
|
||||
differential_pressure.timestamp = hrt_absolute_time();
|
||||
if (failure_injection::process_airspeed(_failure_config, 0, differential_pressure, _airspeed_failure)) {
|
||||
_differential_pressure_pub.publish(differential_pressure);
|
||||
}
|
||||
}
|
||||
@@ -142,26 +127,6 @@ void SensorAirspeedSim::Run()
|
||||
perf_end(_loop_perf);
|
||||
}
|
||||
|
||||
void SensorAirspeedSim::updateFailureConfig()
|
||||
{
|
||||
_failure_config.update();
|
||||
|
||||
const failure_injection::Mode mode = _failure_config.mode(failure_injection_s::FAILURE_UNIT_SENSOR_AIRSPEED, 1);
|
||||
|
||||
_airspeed_disconnected = (mode == failure_injection::Mode::Off);
|
||||
_airspeed_stuck = (mode == failure_injection::Mode::Stuck);
|
||||
|
||||
if (mode == failure_injection::Mode::Wrong) {
|
||||
// Simulate pitot blockage: start the ramp on the rising edge and keep it running.
|
||||
if (_airspeed_blocked_timestamp == 0) {
|
||||
_airspeed_blocked_timestamp = hrt_absolute_time();
|
||||
}
|
||||
|
||||
} else {
|
||||
_airspeed_blocked_timestamp = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int SensorAirspeedSim::task_spawn(int argc, char *argv[])
|
||||
{
|
||||
SensorAirspeedSim *instance = new SensorAirspeedSim();
|
||||
|
||||
@@ -80,8 +80,6 @@ public:
|
||||
|
||||
bool init();
|
||||
|
||||
void updateFailureConfig();
|
||||
|
||||
private:
|
||||
void Run() override;
|
||||
|
||||
@@ -94,10 +92,7 @@ private:
|
||||
|
||||
failure_injection::Config _failure_config;
|
||||
|
||||
bool _airspeed_disconnected{false};
|
||||
bool _airspeed_stuck{false};
|
||||
hrt_abstime _airspeed_blocked_timestamp{0};
|
||||
float _last_differential_pressure_pa{0.f};
|
||||
failure_injection::AirspeedState _airspeed_failure;
|
||||
|
||||
perf_counter_t _loop_perf{perf_alloc(PC_ELAPSED, MODULE_NAME": cycle")};
|
||||
};
|
||||
Reference in new issue
Block a user