2018-09-05 03:45:01 -03:00
|
|
|
#include "AP_Airspeed_UAVCAN.h"
|
|
|
|
|
2022-05-04 05:13:29 -03:00
|
|
|
#if AP_AIRSPEED_UAVCAN_ENABLED
|
|
|
|
|
2020-05-31 09:01:32 -03:00
|
|
|
#include <AP_CANManager/AP_CANManager.h>
|
2018-09-05 03:45:01 -03:00
|
|
|
#include <AP_UAVCAN/AP_UAVCAN.h>
|
|
|
|
|
|
|
|
#include <uavcan/equipment/air_data/RawAirData.hpp>
|
2022-10-17 05:35:08 -03:00
|
|
|
#if AP_AIRSPEED_HYGROMETER_ENABLE
|
|
|
|
#include <dronecan/sensors/hygrometer/Hygrometer.hpp>
|
|
|
|
#endif
|
2018-09-05 03:45:01 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2020-05-31 09:01:32 -03:00
|
|
|
#define LOG_TAG "AirSpeed"
|
2018-09-05 03:45:01 -03:00
|
|
|
|
2022-10-17 05:35:08 -03:00
|
|
|
// Frontend Registry Binders
|
2018-09-05 03:45:01 -03:00
|
|
|
UC_REGISTRY_BINDER(AirspeedCb, uavcan::equipment::air_data::RawAirData);
|
|
|
|
|
2022-10-17 05:35:08 -03:00
|
|
|
#if AP_AIRSPEED_HYGROMETER_ENABLE
|
|
|
|
UC_REGISTRY_BINDER(HygrometerCb, dronecan::sensors::hygrometer::Hygrometer);
|
|
|
|
#endif
|
|
|
|
|
2022-06-22 05:20:30 -03:00
|
|
|
AP_Airspeed_UAVCAN::DetectedModules AP_Airspeed_UAVCAN::_detected_modules[];
|
2018-10-11 20:35:03 -03:00
|
|
|
HAL_Semaphore AP_Airspeed_UAVCAN::_sem_registry;
|
2018-09-05 03:45:01 -03:00
|
|
|
|
|
|
|
// constructor
|
|
|
|
AP_Airspeed_UAVCAN::AP_Airspeed_UAVCAN(AP_Airspeed &_frontend, uint8_t _instance) :
|
|
|
|
AP_Airspeed_Backend(_frontend, _instance)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void AP_Airspeed_UAVCAN::subscribe_msgs(AP_UAVCAN* ap_uavcan)
|
|
|
|
{
|
|
|
|
if (ap_uavcan == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* node = ap_uavcan->get_node();
|
|
|
|
|
|
|
|
uavcan::Subscriber<uavcan::equipment::air_data::RawAirData, AirspeedCb> *airspeed_listener;
|
|
|
|
airspeed_listener = new uavcan::Subscriber<uavcan::equipment::air_data::RawAirData, AirspeedCb>(*node);
|
|
|
|
|
|
|
|
const int airspeed_listener_res = airspeed_listener->start(AirspeedCb(ap_uavcan, &handle_airspeed));
|
|
|
|
if (airspeed_listener_res < 0) {
|
2022-10-17 05:35:08 -03:00
|
|
|
AP_HAL::panic("DroneCAN Airspeed subscriber error \n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#if AP_AIRSPEED_HYGROMETER_ENABLE
|
|
|
|
uavcan::Subscriber<dronecan::sensors::hygrometer::Hygrometer, HygrometerCb> *hygrometer_listener;
|
|
|
|
hygrometer_listener = new uavcan::Subscriber<dronecan::sensors::hygrometer::Hygrometer, HygrometerCb>(*node);
|
|
|
|
const int hygrometer_listener_res = hygrometer_listener->start(HygrometerCb(ap_uavcan, &handle_hygrometer));
|
|
|
|
if (hygrometer_listener_res < 0) {
|
|
|
|
AP_HAL::panic("DroneCAN Hygrometer subscriber error\n");
|
2018-09-05 03:45:01 -03:00
|
|
|
}
|
2022-10-17 05:35:08 -03:00
|
|
|
#endif
|
2018-09-05 03:45:01 -03:00
|
|
|
}
|
|
|
|
|
2022-10-06 19:28:48 -03:00
|
|
|
AP_Airspeed_Backend* AP_Airspeed_UAVCAN::probe(AP_Airspeed &_frontend, uint8_t _instance, uint32_t previous_devid)
|
2018-09-05 03:45:01 -03:00
|
|
|
{
|
2019-02-14 21:40:06 -04:00
|
|
|
WITH_SEMAPHORE(_sem_registry);
|
2018-09-05 03:45:01 -03:00
|
|
|
|
|
|
|
AP_Airspeed_UAVCAN* backend = nullptr;
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < AIRSPEED_MAX_SENSORS; i++) {
|
|
|
|
if (_detected_modules[i].driver == nullptr && _detected_modules[i].ap_uavcan != nullptr) {
|
2022-10-06 19:28:48 -03:00
|
|
|
const auto bus_id = AP_HAL::Device::make_bus_id(AP_HAL::Device::BUS_TYPE_UAVCAN,
|
|
|
|
_detected_modules[i].ap_uavcan->get_driver_index(),
|
|
|
|
_detected_modules[i].node_id, 0);
|
|
|
|
if (previous_devid != 0 && previous_devid != bus_id) {
|
|
|
|
// match with previous ID only
|
|
|
|
continue;
|
|
|
|
}
|
2018-09-05 03:45:01 -03:00
|
|
|
backend = new AP_Airspeed_UAVCAN(_frontend, _instance);
|
|
|
|
if (backend == nullptr) {
|
2022-10-06 19:28:48 -03:00
|
|
|
AP::can().log_text(AP_CANManager::LOG_INFO,
|
|
|
|
LOG_TAG,
|
|
|
|
"Failed register UAVCAN Airspeed Node %d on Bus %d\n",
|
|
|
|
_detected_modules[i].node_id,
|
|
|
|
_detected_modules[i].ap_uavcan->get_driver_index());
|
2018-09-05 03:45:01 -03:00
|
|
|
} else {
|
|
|
|
_detected_modules[i].driver = backend;
|
2022-10-06 19:28:48 -03:00
|
|
|
AP::can().log_text(AP_CANManager::LOG_INFO,
|
|
|
|
LOG_TAG,
|
|
|
|
"Registered UAVCAN Airspeed Node %d on Bus %d\n",
|
|
|
|
_detected_modules[i].node_id,
|
|
|
|
_detected_modules[i].ap_uavcan->get_driver_index());
|
|
|
|
backend->set_bus_id(bus_id);
|
2018-09-05 03:45:01 -03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
AP_Airspeed_UAVCAN* AP_Airspeed_UAVCAN::get_uavcan_backend(AP_UAVCAN* ap_uavcan, uint8_t node_id)
|
|
|
|
{
|
|
|
|
if (ap_uavcan == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < AIRSPEED_MAX_SENSORS; i++) {
|
|
|
|
if (_detected_modules[i].driver != nullptr &&
|
|
|
|
_detected_modules[i].ap_uavcan == ap_uavcan &&
|
|
|
|
_detected_modules[i].node_id == node_id ) {
|
|
|
|
return _detected_modules[i].driver;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool detected = false;
|
|
|
|
for (uint8_t i = 0; i < AIRSPEED_MAX_SENSORS; i++) {
|
|
|
|
if (_detected_modules[i].ap_uavcan == ap_uavcan && _detected_modules[i].node_id == node_id) {
|
|
|
|
// detected
|
|
|
|
detected = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!detected) {
|
|
|
|
for (uint8_t i = 0; i < AIRSPEED_MAX_SENSORS; i++) {
|
|
|
|
if (_detected_modules[i].ap_uavcan == nullptr) {
|
|
|
|
_detected_modules[i].ap_uavcan = ap_uavcan;
|
|
|
|
_detected_modules[i].node_id = node_id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_Airspeed_UAVCAN::handle_airspeed(AP_UAVCAN* ap_uavcan, uint8_t node_id, const AirspeedCb &cb)
|
|
|
|
{
|
2019-02-14 21:40:06 -04:00
|
|
|
WITH_SEMAPHORE(_sem_registry);
|
|
|
|
|
2019-02-14 21:41:02 -04:00
|
|
|
AP_Airspeed_UAVCAN* driver = get_uavcan_backend(ap_uavcan, node_id);
|
2018-09-05 03:45:01 -03:00
|
|
|
|
2019-02-14 21:41:02 -04:00
|
|
|
if (driver != nullptr) {
|
|
|
|
WITH_SEMAPHORE(driver->_sem_airspeed);
|
|
|
|
driver->_pressure = cb.msg->differential_pressure;
|
2020-12-07 23:04:39 -04:00
|
|
|
if (!isnan(cb.msg->static_air_temperature) &&
|
|
|
|
cb.msg->static_air_temperature > 0) {
|
2022-01-12 08:03:23 -04:00
|
|
|
driver->_temperature = KELVIN_TO_C(cb.msg->static_air_temperature);
|
2019-10-03 08:02:07 -03:00
|
|
|
driver->_have_temperature = true;
|
|
|
|
}
|
2019-02-14 21:41:02 -04:00
|
|
|
driver->_last_sample_time_ms = AP_HAL::millis();
|
|
|
|
}
|
2018-09-05 03:45:01 -03:00
|
|
|
}
|
|
|
|
|
2022-10-17 05:35:08 -03:00
|
|
|
#if AP_AIRSPEED_HYGROMETER_ENABLE
|
|
|
|
void AP_Airspeed_UAVCAN::handle_hygrometer(AP_UAVCAN* ap_uavcan, uint8_t node_id, const HygrometerCb &cb)
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(_sem_registry);
|
|
|
|
|
|
|
|
AP_Airspeed_UAVCAN* driver = get_uavcan_backend(ap_uavcan, node_id);
|
|
|
|
|
|
|
|
if (driver != nullptr) {
|
|
|
|
WITH_SEMAPHORE(driver->_sem_airspeed);
|
|
|
|
driver->_hygrometer.temperature = KELVIN_TO_C(cb.msg->temperature);
|
|
|
|
driver->_hygrometer.humidity = cb.msg->humidity;
|
|
|
|
driver->_hygrometer.last_sample_ms = AP_HAL::millis();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // AP_AIRSPEED_HYGROMETER_ENABLE
|
|
|
|
|
2018-09-05 03:45:01 -03:00
|
|
|
bool AP_Airspeed_UAVCAN::init()
|
|
|
|
{
|
|
|
|
// always returns true
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_Airspeed_UAVCAN::get_differential_pressure(float &pressure)
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(_sem_airspeed);
|
|
|
|
|
|
|
|
if ((AP_HAL::millis() - _last_sample_time_ms) > 100) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pressure = _pressure;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_Airspeed_UAVCAN::get_temperature(float &temperature)
|
|
|
|
{
|
2019-10-03 08:02:07 -03:00
|
|
|
if (!_have_temperature) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-05 03:45:01 -03:00
|
|
|
WITH_SEMAPHORE(_sem_airspeed);
|
|
|
|
|
|
|
|
if ((AP_HAL::millis() - _last_sample_time_ms) > 100) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-28 14:38:08 -03:00
|
|
|
temperature = _temperature;
|
2018-09-05 03:45:01 -03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-17 05:35:08 -03:00
|
|
|
#if AP_AIRSPEED_HYGROMETER_ENABLE
|
|
|
|
/*
|
|
|
|
return hygrometer data if available
|
|
|
|
*/
|
|
|
|
bool AP_Airspeed_UAVCAN::get_hygrometer(uint32_t &last_sample_ms, float &temperature, float &humidity)
|
|
|
|
{
|
|
|
|
if (_hygrometer.last_sample_ms == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
WITH_SEMAPHORE(_sem_airspeed);
|
|
|
|
last_sample_ms = _hygrometer.last_sample_ms;
|
|
|
|
temperature = _hygrometer.temperature;
|
|
|
|
humidity = _hygrometer.humidity;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif // AP_AIRSPEED_HYGROMETER_ENABLE
|
|
|
|
|
2022-05-04 05:13:29 -03:00
|
|
|
#endif // AP_AIRSPEED_UAVCAN_ENABLED
|