2017-04-02 11:56:03 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
|
|
|
#if HAL_WITH_UAVCAN
|
|
|
|
|
|
|
|
#include "AP_Baro_UAVCAN.h"
|
|
|
|
#include <AP_BoardConfig/AP_BoardConfig.h>
|
2017-05-06 06:13:05 -03:00
|
|
|
#include <AP_BoardConfig/AP_BoardConfig_CAN.h>
|
2017-04-02 11:56:03 -03:00
|
|
|
|
2018-02-01 15:13:54 -04:00
|
|
|
#if HAL_OS_POSIX_IO
|
2017-04-02 11:56:03 -03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2018-02-01 15:13:54 -04:00
|
|
|
#endif
|
2017-04-02 11:56:03 -03:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2017-05-06 06:13:05 -03:00
|
|
|
#define debug_baro_uavcan(level, fmt, args...) do { if ((level) <= AP_BoardConfig_CAN::get_can_debug()) { printf(fmt, ##args); }} while (0)
|
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)
|
|
|
|
{
|
2017-05-06 06:13:05 -03:00
|
|
|
_sem_baro = hal.util->new_semaphore();
|
|
|
|
}
|
2017-04-02 11:56:03 -03:00
|
|
|
|
2017-05-06 06:13:05 -03:00
|
|
|
AP_Baro_UAVCAN::~AP_Baro_UAVCAN()
|
|
|
|
{
|
2018-03-09 04:31:42 -04:00
|
|
|
if (!_initialized) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-11 06:51:17 -03:00
|
|
|
|
|
|
|
AP_UAVCAN *ap_uavcan = AP_UAVCAN::get_uavcan(_manager);
|
2018-03-09 04:31:42 -04:00
|
|
|
if (ap_uavcan == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-11 06:51:17 -03:00
|
|
|
|
2018-03-09 04:31:42 -04:00
|
|
|
ap_uavcan->remove_baro_listener(this);
|
2018-03-11 06:51:17 -03:00
|
|
|
delete _sem_baro;
|
|
|
|
|
2018-03-09 04:31:42 -04:00
|
|
|
debug_baro_uavcan(2, "AP_Baro_UAVCAN destructed\n\r");
|
2017-04-02 11:56:03 -03:00
|
|
|
}
|
|
|
|
|
2017-05-06 06:13:05 -03:00
|
|
|
AP_Baro_Backend *AP_Baro_UAVCAN::probe(AP_Baro &baro)
|
2017-04-02 11:56:03 -03:00
|
|
|
{
|
2018-03-09 04:31:42 -04:00
|
|
|
|
|
|
|
if (AP_BoardConfig_CAN::get_can_num_ifaces() == 0) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
AP_Baro_UAVCAN *sensor;
|
|
|
|
for (uint8_t i = 0; i < MAX_NUMBER_OF_CAN_DRIVERS; i++) {
|
2018-03-11 06:51:17 -03:00
|
|
|
AP_UAVCAN *ap_uavcan = AP_UAVCAN::get_uavcan(i);
|
|
|
|
if (ap_uavcan == nullptr) {
|
2018-03-09 04:31:42 -04:00
|
|
|
continue;
|
|
|
|
}
|
2018-03-11 06:51:17 -03:00
|
|
|
|
|
|
|
uint8_t freebaro = ap_uavcan->find_smallest_free_baro_node();
|
2018-03-09 04:31:42 -04:00
|
|
|
if (freebaro == UINT8_MAX) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
sensor = new AP_Baro_UAVCAN(baro);
|
|
|
|
if (sensor->register_uavcan_baro(i, freebaro)) {
|
|
|
|
debug_baro_uavcan(2, "AP_Baro_UAVCAN probed, drv: %d, node: %d\n\r", i, freebaro);
|
|
|
|
return sensor;
|
|
|
|
} else {
|
|
|
|
delete sensor;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Read the sensor
|
|
|
|
void AP_Baro_UAVCAN::update(void)
|
|
|
|
{
|
2017-05-02 02:57:59 -03:00
|
|
|
if (_sem_baro->take(HAL_SEMAPHORE_BLOCK_FOREVER)) {
|
2017-04-02 11:56:03 -03:00
|
|
|
_copy_to_frontend(_instance, _pressure, _temperature);
|
|
|
|
|
|
|
|
_frontend.set_external_temperature(_temperature);
|
|
|
|
_sem_baro->give();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_Baro_UAVCAN::handle_baro_msg(float pressure, float temperature)
|
|
|
|
{
|
2017-05-02 02:57:59 -03:00
|
|
|
if (_sem_baro->take(HAL_SEMAPHORE_BLOCK_FOREVER)) {
|
2017-04-02 11:56:03 -03:00
|
|
|
_pressure = pressure;
|
2018-05-10 21:00:32 -03:00
|
|
|
_temperature = temperature - C_TO_KELVIN;
|
2017-04-02 11:56:03 -03:00
|
|
|
_last_timestamp = AP_HAL::micros64();
|
|
|
|
_sem_baro->give();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-06 06:13:05 -03:00
|
|
|
bool AP_Baro_UAVCAN::register_uavcan_baro(uint8_t mgr, uint8_t node)
|
|
|
|
{
|
2018-03-11 06:51:17 -03:00
|
|
|
AP_UAVCAN *ap_uavcan = AP_UAVCAN::get_uavcan(mgr);
|
2018-03-09 04:31:42 -04:00
|
|
|
if (ap_uavcan == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_manager = mgr;
|
2017-05-06 06:13:05 -03:00
|
|
|
|
2018-03-09 04:31:42 -04:00
|
|
|
if (ap_uavcan->register_baro_listener_to_node(this, node)) {
|
|
|
|
_instance = _frontend.register_sensor();
|
|
|
|
debug_baro_uavcan(2, "AP_Baro_UAVCAN loaded\n\r");
|
2017-05-06 06:13:05 -03:00
|
|
|
|
2018-03-09 04:31:42 -04:00
|
|
|
_initialized = true;
|
2017-05-06 06:13:05 -03:00
|
|
|
|
2018-03-09 04:31:42 -04:00
|
|
|
return true;
|
2017-05-06 06:13:05 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-02 11:56:03 -03:00
|
|
|
#endif // HAL_WITH_UAVCAN
|
|
|
|
|