Simulation Gazebo: Use Gazebo Airpressure sensor (#21176)

* Simulation Gazebo: Use Gazebo Airpressure sensor

Signed-off-by: Alejandro Hernández Cordero <ahcorde@gmail.com>

* Fixed build

Signed-off-by: Alejandro Hernández Cordero <ahcorde@gmail.com>

* Added feedback

Signed-off-by: Alejandro Hernández Cordero <ahcorde@gmail.com>

* make linters happy

Signed-off-by: Alejandro Hernández Cordero <ahcorde@gmail.com>

---------

Signed-off-by: Alejandro Hernández Cordero <ahcorde@gmail.com>
This commit is contained in:
Alejandro Hernández Cordero
2023-02-22 19:32:25 +01:00
committed by GitHub
parent 06dfd1726f
commit ea6814d258
10 changed files with 90 additions and 14 deletions
+42 -8
View File
@@ -157,12 +157,20 @@ int GZBridge::init()
std::string airpressure_topic = "/world/" + _world_name + "/model/" + _model_name +
"/link/airspeed_link/sensor/air_speed/air_speed";
if (!_node.Subscribe(airpressure_topic, &GZBridge::airpressureCallback, this)) {
if (!_node.Subscribe(airpressure_topic, &GZBridge::airspeedCallback, this)) {
PX4_ERR("failed to subscribe to %s", airpressure_topic.c_str());
return PX4_ERROR;
}
#endif
// Air pressure: /world/$WORLD/model/$MODEL/link/base_link/sensor/air_pressure_sensor/air_pressure
std::string air_pressure_topic = "/world/" + _world_name + "/model/" + _model_name +
"/link/base_link/sensor/air_pressure_sensor/air_pressure";
if (!_node.Subscribe(air_pressure_topic, &GZBridge::barometerCallback, this)) {
PX4_ERR("failed to subscribe to %s", air_pressure_topic.c_str());
return PX4_ERROR;
}
if (!_mixing_interface_esc.init(_model_name)) {
PX4_ERR("failed to init ESC output");
@@ -185,6 +193,7 @@ int GZBridge::task_spawn(int argc, char *argv[])
const char *model_pose = nullptr;
const char *model_sim = nullptr;
const char *px4_instance = nullptr;
std::string model_name_std;
bool error_flag = false;
@@ -248,7 +257,7 @@ int GZBridge::task_spawn(int argc, char *argv[])
}
} else if (!model_name) {
std::string model_name_std = std::string(model_sim) + "_" + std::string(px4_instance);
model_name_std = std::string(model_sim) + "_" + std::string(px4_instance);
model_name = model_name_std.c_str();
}
@@ -329,28 +338,53 @@ void GZBridge::clockCallback(const gz::msgs::Clock &clock)
pthread_mutex_unlock(&_node_mutex);
}
#if 0
void GZBridge::airpressureCallback(const gz::msgs::FluidPressure &air_pressure)
void GZBridge::barometerCallback(const gz::msgs::FluidPressure &air_pressure)
{
if (hrt_absolute_time() == 0) {
return;
}
pthread_mutex_lock(&_mutex);
pthread_mutex_lock(&_node_mutex);
const uint64_t time_us = (air_pressure.header().stamp().sec() * 1000000)
+ (air_pressure.header().stamp().nsec() / 1000);
double air_pressure_value = air_pressure.pressure();
// publish
sensor_baro_s sensor_baro{};
sensor_baro.timestamp_sample = time_us;
sensor_baro.device_id = 6620172; // 6620172: DRV_BARO_DEVTYPE_BAROSIM, BUS: 1, ADDR: 4, TYPE: SIMULATION
sensor_baro.pressure = air_pressure.pressure();
sensor_baro.temperature = this->_temperature;
sensor_baro.timestamp = hrt_absolute_time();
_sensor_baro_pub.publish(sensor_baro);
pthread_mutex_unlock(&_node_mutex);
}
#if 0
void GZBridge::airspeedCallback(const gz::msgs::AirSpeedSensor &air_speed)
{
if (hrt_absolute_time() == 0) {
return;
}
pthread_mutex_lock(&_node_mutex);
const uint64_t time_us = (air_speed.header().stamp().sec() * 1000000)
+ (air_speed.header().stamp().nsec() / 1000);
double air_speed_value = air_speed.diff_pressure();
differential_pressure_s report{};
report.timestamp_sample = time_us;
report.device_id = 1377548; // 1377548: DRV_DIFF_PRESS_DEVTYPE_SIM, BUS: 1, ADDR: 5, TYPE: SIMULATION
report.differential_pressure_pa = static_cast<float>(air_pressure_value); // hPa to Pa;
report.temperature = static_cast<float>(air_pressure.variance()) + CONSTANTS_ABSOLUTE_NULL_CELSIUS; // K to C
report.differential_pressure_pa = static_cast<float>(air_speed_value); // hPa to Pa;
report.temperature = static_cast<float>(air_speed.temperature()) + CONSTANTS_ABSOLUTE_NULL_CELSIUS; // K to C
report.timestamp = hrt_absolute_time();;
_differential_pressure_pub.publish(report);
this->_temperature = report.temperature;
pthread_mutex_unlock(&_node_mutex);
}
#endif