2017-04-02 11:56:03 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
2020-05-31 09:02:33 -03:00
|
|
|
#if HAL_ENABLE_LIBUAVCAN_DRIVERS
|
2017-04-02 11:56:03 -03:00
|
|
|
|
|
|
|
#include "AP_Baro_UAVCAN.h"
|
|
|
|
|
2020-05-31 09:02:33 -03:00
|
|
|
#include <AP_CANManager/AP_CANManager.h>
|
2018-07-20 06:44:55 -03:00
|
|
|
#include <AP_UAVCAN/AP_UAVCAN.h>
|
|
|
|
|
|
|
|
#include <uavcan/equipment/air_data/StaticPressure.hpp>
|
|
|
|
#include <uavcan/equipment/air_data/StaticTemperature.hpp>
|
2017-04-02 11:56:03 -03:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2020-05-31 09:02:33 -03:00
|
|
|
#define LOG_TAG "Baro"
|
2017-04-02 11:56:03 -03:00
|
|
|
|
2018-07-20 06:44:55 -03:00
|
|
|
//UAVCAN Frontend Registry Binder
|
|
|
|
UC_REGISTRY_BINDER(PressureCb, uavcan::equipment::air_data::StaticPressure);
|
|
|
|
UC_REGISTRY_BINDER(TemperatureCb, uavcan::equipment::air_data::StaticTemperature);
|
|
|
|
|
|
|
|
AP_Baro_UAVCAN::DetectedModules AP_Baro_UAVCAN::_detected_modules[] = {0};
|
2018-10-11 20:35:03 -03:00
|
|
|
HAL_Semaphore AP_Baro_UAVCAN::_sem_registry;
|
2018-07-20 06:44:55 -03:00
|
|
|
|
2017-04-02 11:56:03 -03:00
|
|
|
/*
|
|
|
|
constructor - registers instance at top Baro driver
|
|
|
|
*/
|
|
|
|
AP_Baro_UAVCAN::AP_Baro_UAVCAN(AP_Baro &baro) :
|
|
|
|
AP_Baro_Backend(baro)
|
2018-07-20 06:44:55 -03:00
|
|
|
{}
|
2017-04-02 11:56:03 -03:00
|
|
|
|
2018-07-20 06:44:55 -03:00
|
|
|
void AP_Baro_UAVCAN::subscribe_msgs(AP_UAVCAN* ap_uavcan)
|
2017-05-06 06:13:05 -03:00
|
|
|
{
|
2018-07-20 06:44:55 -03:00
|
|
|
if (ap_uavcan == nullptr) {
|
2018-03-09 04:31:42 -04:00
|
|
|
return;
|
|
|
|
}
|
2018-07-18 03:27:37 -03:00
|
|
|
|
2018-07-20 06:44:55 -03:00
|
|
|
auto* node = ap_uavcan->get_node();
|
|
|
|
|
|
|
|
uavcan::Subscriber<uavcan::equipment::air_data::StaticPressure, PressureCb> *pressure_listener;
|
|
|
|
pressure_listener = new uavcan::Subscriber<uavcan::equipment::air_data::StaticPressure, PressureCb>(*node);
|
|
|
|
// Msg Handler
|
|
|
|
const int pressure_listener_res = pressure_listener->start(PressureCb(ap_uavcan, &handle_pressure));
|
|
|
|
if (pressure_listener_res < 0) {
|
|
|
|
AP_HAL::panic("UAVCAN Baro subscriber start problem\n\r");
|
2018-03-09 04:31:42 -04:00
|
|
|
}
|
2018-07-18 03:27:37 -03:00
|
|
|
|
2018-07-20 06:44:55 -03:00
|
|
|
uavcan::Subscriber<uavcan::equipment::air_data::StaticTemperature, TemperatureCb> *temperature_listener;
|
|
|
|
temperature_listener = new uavcan::Subscriber<uavcan::equipment::air_data::StaticTemperature, TemperatureCb>(*node);
|
|
|
|
// Msg Handler
|
|
|
|
const int temperature_listener_res = temperature_listener->start(TemperatureCb(ap_uavcan, &handle_temperature));
|
|
|
|
if (temperature_listener_res < 0) {
|
|
|
|
AP_HAL::panic("UAVCAN Baro subscriber start problem\n\r");
|
|
|
|
}
|
|
|
|
}
|
2018-07-18 03:27:37 -03:00
|
|
|
|
2018-07-20 06:44:55 -03:00
|
|
|
AP_Baro_Backend* AP_Baro_UAVCAN::probe(AP_Baro &baro)
|
|
|
|
{
|
2019-02-14 21:37:08 -04:00
|
|
|
WITH_SEMAPHORE(_sem_registry);
|
|
|
|
|
2018-07-20 06:44:55 -03:00
|
|
|
AP_Baro_UAVCAN* backend = nullptr;
|
|
|
|
for (uint8_t i = 0; i < BARO_MAX_DRIVERS; i++) {
|
|
|
|
if (_detected_modules[i].driver == nullptr && _detected_modules[i].ap_uavcan != nullptr) {
|
|
|
|
backend = new AP_Baro_UAVCAN(baro);
|
|
|
|
if (backend == nullptr) {
|
2020-05-31 09:02:33 -03:00
|
|
|
AP::can().log_text(AP_CANManager::LOG_ERROR,
|
|
|
|
LOG_TAG,
|
|
|
|
"Failed register UAVCAN Baro Node %d on Bus %d\n",
|
|
|
|
_detected_modules[i].node_id,
|
|
|
|
_detected_modules[i].ap_uavcan->get_driver_index());
|
2018-07-20 06:44:55 -03:00
|
|
|
} else {
|
|
|
|
_detected_modules[i].driver = backend;
|
2019-12-24 22:13:03 -04:00
|
|
|
backend->_pressure = 0;
|
|
|
|
backend->_pressure_count = 0;
|
2018-07-20 06:44:55 -03:00
|
|
|
backend->_ap_uavcan = _detected_modules[i].ap_uavcan;
|
|
|
|
backend->_node_id = _detected_modules[i].node_id;
|
2020-07-18 04:12:41 -03:00
|
|
|
|
|
|
|
backend->_instance = backend->_frontend.register_sensor();
|
|
|
|
backend->set_bus_id(backend->_instance, AP_HAL::Device::make_bus_id(AP_HAL::Device::BUS_TYPE_UAVCAN,
|
|
|
|
_detected_modules[i].ap_uavcan->get_driver_index(),
|
|
|
|
backend->_node_id, 0));
|
|
|
|
|
2020-05-31 09:02:33 -03:00
|
|
|
AP::can().log_text(AP_CANManager::LOG_INFO,
|
|
|
|
LOG_TAG,
|
|
|
|
"Registered UAVCAN Baro Node %d on Bus %d\n",
|
|
|
|
_detected_modules[i].node_id,
|
|
|
|
_detected_modules[i].ap_uavcan->get_driver_index());
|
2018-07-20 06:44:55 -03:00
|
|
|
}
|
|
|
|
break;
|
2018-03-09 04:31:42 -04:00
|
|
|
}
|
2018-07-20 06:44:55 -03:00
|
|
|
}
|
|
|
|
return backend;
|
|
|
|
}
|
2018-07-18 03:27:37 -03:00
|
|
|
|
2018-07-20 06:44:55 -03:00
|
|
|
AP_Baro_UAVCAN* AP_Baro_UAVCAN::get_uavcan_backend(AP_UAVCAN* ap_uavcan, uint8_t node_id, bool create_new)
|
|
|
|
{
|
|
|
|
if (ap_uavcan == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
for (uint8_t i = 0; i < BARO_MAX_DRIVERS; 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (create_new) {
|
|
|
|
bool already_detected = false;
|
|
|
|
//Check if there's an empty spot for possible registeration
|
|
|
|
for (uint8_t i = 0; i < BARO_MAX_DRIVERS; i++) {
|
|
|
|
if (_detected_modules[i].ap_uavcan == ap_uavcan && _detected_modules[i].node_id == node_id) {
|
|
|
|
//Already Detected
|
|
|
|
already_detected = true;
|
|
|
|
break;
|
|
|
|
}
|
2018-03-09 04:31:42 -04:00
|
|
|
}
|
2018-07-20 06:44:55 -03:00
|
|
|
if (!already_detected) {
|
|
|
|
for (uint8_t i = 0; i < BARO_MAX_DRIVERS; i++) {
|
|
|
|
if (_detected_modules[i].ap_uavcan == nullptr) {
|
|
|
|
_detected_modules[i].ap_uavcan = ap_uavcan;
|
|
|
|
_detected_modules[i].node_id = node_id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-04-02 11:56:03 -03:00
|
|
|
}
|
|
|
|
}
|
2017-05-06 06:13:05 -03:00
|
|
|
|
2018-03-09 04:31:42 -04:00
|
|
|
return nullptr;
|
2017-04-02 11:56:03 -03:00
|
|
|
}
|
|
|
|
|
2019-12-24 22:13:03 -04:00
|
|
|
|
|
|
|
void AP_Baro_UAVCAN::_update_and_wrap_accumulator(float *accum, float val, uint8_t *count, const uint8_t max_count)
|
2017-04-02 11:56:03 -03:00
|
|
|
{
|
2019-12-24 22:13:03 -04:00
|
|
|
*accum += val;
|
|
|
|
*count += 1;
|
|
|
|
if (*count == max_count) {
|
|
|
|
*count = max_count / 2;
|
|
|
|
*accum = *accum / 2;
|
|
|
|
}
|
|
|
|
}
|
2019-02-14 21:37:08 -04:00
|
|
|
|
2019-12-24 22:13:03 -04:00
|
|
|
void AP_Baro_UAVCAN::handle_pressure(AP_UAVCAN* ap_uavcan, uint8_t node_id, const PressureCb &cb)
|
|
|
|
{
|
|
|
|
AP_Baro_UAVCAN* driver;
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(_sem_registry);
|
|
|
|
driver = get_uavcan_backend(ap_uavcan, node_id, true);
|
|
|
|
if (driver == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-14 21:37:52 -04:00
|
|
|
}
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(driver->_sem_baro);
|
2019-12-24 22:13:03 -04:00
|
|
|
_update_and_wrap_accumulator(&driver->_pressure, cb.msg->static_pressure, &driver->_pressure_count, 32);
|
2019-02-14 21:37:52 -04:00
|
|
|
driver->new_pressure = true;
|
|
|
|
}
|
2017-04-02 11:56:03 -03:00
|
|
|
}
|
|
|
|
|
2018-07-20 06:44:55 -03:00
|
|
|
void AP_Baro_UAVCAN::handle_temperature(AP_UAVCAN* ap_uavcan, uint8_t node_id, const TemperatureCb &cb)
|
2017-04-02 11:56:03 -03:00
|
|
|
{
|
2019-12-24 22:13:03 -04:00
|
|
|
AP_Baro_UAVCAN* driver;
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(_sem_registry);
|
|
|
|
driver = get_uavcan_backend(ap_uavcan, node_id, false);
|
|
|
|
if (driver == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-14 21:37:52 -04:00
|
|
|
}
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(driver->_sem_baro);
|
|
|
|
driver->_temperature = cb.msg->static_temperature - C_TO_KELVIN;
|
|
|
|
}
|
2017-04-02 11:56:03 -03:00
|
|
|
}
|
|
|
|
|
2018-07-20 06:44:55 -03:00
|
|
|
// Read the sensor
|
|
|
|
void AP_Baro_UAVCAN::update(void)
|
2017-05-06 06:13:05 -03:00
|
|
|
{
|
2019-12-24 22:13:03 -04:00
|
|
|
float pressure = 0;
|
|
|
|
|
2018-07-20 06:44:55 -03:00
|
|
|
WITH_SEMAPHORE(_sem_baro);
|
|
|
|
if (new_pressure) {
|
2019-12-24 22:13:03 -04:00
|
|
|
if (_pressure_count != 0) {
|
|
|
|
pressure = _pressure / _pressure_count;
|
|
|
|
_pressure_count = 0;
|
|
|
|
_pressure = 0;
|
|
|
|
}
|
|
|
|
_copy_to_frontend(_instance, pressure, _temperature);
|
|
|
|
|
2017-05-06 06:13:05 -03:00
|
|
|
|
2018-07-20 06:44:55 -03:00
|
|
|
_frontend.set_external_temperature(_temperature);
|
|
|
|
new_pressure = false;
|
2017-05-06 06:13:05 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 09:02:33 -03:00
|
|
|
#endif // HAL_ENABLE_LIBUAVCAN_DRIVERS
|
2017-04-02 11:56:03 -03:00
|
|
|
|