2017-04-02 11:56:15 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
2020-05-31 09:04:56 -03:00
|
|
|
#if HAL_ENABLE_LIBUAVCAN_DRIVERS
|
2017-04-02 11:56:15 -03:00
|
|
|
|
|
|
|
#include "AP_Compass_UAVCAN.h"
|
|
|
|
|
2020-05-31 09:04:56 -03:00
|
|
|
#include <AP_CANManager/AP_CANManager.h>
|
2018-07-20 08:18:00 -03:00
|
|
|
#include <AP_UAVCAN/AP_UAVCAN.h>
|
|
|
|
|
|
|
|
#include <uavcan/equipment/ahrs/MagneticFieldStrength.hpp>
|
|
|
|
#include <uavcan/equipment/ahrs/MagneticFieldStrength2.hpp>
|
2017-04-02 11:56:15 -03:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2020-05-31 09:04:56 -03:00
|
|
|
#define LOG_TAG "COMPASS"
|
2018-07-20 08:18:00 -03:00
|
|
|
// Frontend Registry Binders
|
|
|
|
UC_REGISTRY_BINDER(MagCb, uavcan::equipment::ahrs::MagneticFieldStrength);
|
|
|
|
UC_REGISTRY_BINDER(Mag2Cb, uavcan::equipment::ahrs::MagneticFieldStrength2);
|
|
|
|
|
|
|
|
AP_Compass_UAVCAN::DetectedModules AP_Compass_UAVCAN::_detected_modules[] = {0};
|
2018-10-11 20:35:03 -03:00
|
|
|
HAL_Semaphore AP_Compass_UAVCAN::_sem_registry;
|
2018-07-20 08:18:00 -03:00
|
|
|
|
2020-07-20 18:25:08 -03:00
|
|
|
AP_Compass_UAVCAN::AP_Compass_UAVCAN(AP_UAVCAN* ap_uavcan, uint8_t node_id, uint8_t sensor_id, uint32_t devid)
|
2018-08-06 19:21:27 -03:00
|
|
|
: _ap_uavcan(ap_uavcan)
|
|
|
|
, _node_id(node_id)
|
|
|
|
, _sensor_id(sensor_id)
|
2020-07-20 18:25:08 -03:00
|
|
|
, _devid(devid)
|
2018-08-06 19:21:27 -03:00
|
|
|
{
|
|
|
|
}
|
2018-07-20 08:18:00 -03:00
|
|
|
|
|
|
|
void AP_Compass_UAVCAN::subscribe_msgs(AP_UAVCAN* ap_uavcan)
|
2017-04-02 11:56:15 -03:00
|
|
|
{
|
2018-07-20 08:18:00 -03:00
|
|
|
if (ap_uavcan == nullptr) {
|
2018-03-09 04:32:16 -04:00
|
|
|
return;
|
|
|
|
}
|
2018-07-18 03:27:37 -03:00
|
|
|
|
2018-07-20 08:18:00 -03:00
|
|
|
auto* node = ap_uavcan->get_node();
|
|
|
|
|
|
|
|
uavcan::Subscriber<uavcan::equipment::ahrs::MagneticFieldStrength, MagCb> *mag_listener;
|
|
|
|
mag_listener = new uavcan::Subscriber<uavcan::equipment::ahrs::MagneticFieldStrength, MagCb>(*node);
|
|
|
|
const int mag_listener_res = mag_listener->start(MagCb(ap_uavcan, &handle_magnetic_field));
|
|
|
|
if (mag_listener_res < 0) {
|
|
|
|
AP_HAL::panic("UAVCAN Mag subscriber start problem\n\r");
|
2018-03-09 04:32:16 -04:00
|
|
|
return;
|
2017-04-02 11:56:15 -03:00
|
|
|
}
|
2018-07-18 03:27:37 -03:00
|
|
|
|
2018-07-20 08:18:00 -03:00
|
|
|
uavcan::Subscriber<uavcan::equipment::ahrs::MagneticFieldStrength2, Mag2Cb> *mag2_listener;
|
|
|
|
mag2_listener = new uavcan::Subscriber<uavcan::equipment::ahrs::MagneticFieldStrength2, Mag2Cb>(*node);
|
|
|
|
const int mag2_listener_res = mag2_listener->start(Mag2Cb(ap_uavcan, &handle_magnetic_field_2));
|
|
|
|
if (mag2_listener_res < 0) {
|
|
|
|
AP_HAL::panic("UAVCAN Mag subscriber start problem\n\r");
|
|
|
|
return;
|
|
|
|
}
|
2017-05-06 06:13:19 -03:00
|
|
|
}
|
2017-04-02 11:56:15 -03:00
|
|
|
|
2019-11-20 03:18:10 -04:00
|
|
|
AP_Compass_Backend* AP_Compass_UAVCAN::probe(uint8_t index)
|
2018-07-20 08:18:00 -03:00
|
|
|
{
|
|
|
|
AP_Compass_UAVCAN* driver = nullptr;
|
2019-11-20 03:18:10 -04:00
|
|
|
if (!_detected_modules[index].driver && _detected_modules[index].ap_uavcan) {
|
|
|
|
WITH_SEMAPHORE(_sem_registry);
|
|
|
|
// Register new Compass mode to a backend
|
2020-07-20 18:25:08 -03:00
|
|
|
driver = new AP_Compass_UAVCAN(_detected_modules[index].ap_uavcan, _detected_modules[index].node_id, _detected_modules[index].sensor_id, _detected_modules[index].devid);
|
2019-11-20 03:18:10 -04:00
|
|
|
if (driver) {
|
|
|
|
if (!driver->init()) {
|
|
|
|
delete driver;
|
|
|
|
return nullptr;
|
2018-07-20 08:18:00 -03:00
|
|
|
}
|
2019-11-20 03:18:10 -04:00
|
|
|
_detected_modules[index].driver = driver;
|
2020-05-31 09:04:56 -03:00
|
|
|
AP::can().log_text(AP_CANManager::LOG_INFO,
|
|
|
|
LOG_TAG,
|
2019-11-20 03:18:10 -04:00
|
|
|
"Found Mag Node %d on Bus %d Sensor ID %d\n",
|
|
|
|
_detected_modules[index].node_id,
|
|
|
|
_detected_modules[index].ap_uavcan->get_driver_index(),
|
|
|
|
_detected_modules[index].sensor_id);
|
2017-05-06 06:13:19 -03:00
|
|
|
}
|
|
|
|
}
|
2018-07-20 08:18:00 -03:00
|
|
|
return driver;
|
|
|
|
}
|
2017-05-06 06:13:19 -03:00
|
|
|
|
2019-11-20 03:18:10 -04:00
|
|
|
bool AP_Compass_UAVCAN::init()
|
2018-07-20 08:18:00 -03:00
|
|
|
{
|
2019-11-20 03:18:10 -04:00
|
|
|
// Adding 1 is necessary to allow backward compatibilty, where this field was set as 1 by default
|
2020-07-20 18:25:08 -03:00
|
|
|
if (!register_compass(_devid, _instance)) {
|
2019-11-20 03:18:10 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-08-29 03:47:07 -03:00
|
|
|
|
2020-07-20 18:25:08 -03:00
|
|
|
set_dev_id(_instance, _devid);
|
2018-07-20 08:18:00 -03:00
|
|
|
set_external(_instance, true);
|
2022-05-01 19:21:51 -03:00
|
|
|
set_rotation(_instance, ROTATION_NONE);
|
2018-07-20 08:18:00 -03:00
|
|
|
|
2020-05-31 09:04:56 -03:00
|
|
|
AP::can().log_text(AP_CANManager::LOG_INFO, LOG_TAG, "AP_Compass_UAVCAN loaded\n\r");
|
2019-11-20 03:18:10 -04:00
|
|
|
return true;
|
2017-04-02 11:56:15 -03:00
|
|
|
}
|
|
|
|
|
2018-07-20 08:18:00 -03:00
|
|
|
AP_Compass_UAVCAN* AP_Compass_UAVCAN::get_uavcan_backend(AP_UAVCAN* ap_uavcan, uint8_t node_id, uint8_t sensor_id)
|
2017-04-02 11:56:15 -03:00
|
|
|
{
|
2018-03-11 06:50:49 -03:00
|
|
|
if (ap_uavcan == nullptr) {
|
2018-07-20 08:18:00 -03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
for (uint8_t i=0; i<COMPASS_MAX_BACKEND; i++) {
|
|
|
|
if (_detected_modules[i].driver &&
|
|
|
|
_detected_modules[i].ap_uavcan == ap_uavcan &&
|
|
|
|
_detected_modules[i].node_id == node_id &&
|
|
|
|
_detected_modules[i].sensor_id == sensor_id) {
|
|
|
|
return _detected_modules[i].driver;
|
|
|
|
}
|
2017-04-02 11:56:15 -03:00
|
|
|
}
|
2018-03-11 06:50:49 -03:00
|
|
|
|
2018-07-20 08:18:00 -03:00
|
|
|
bool already_detected = false;
|
|
|
|
// Check if there's an empty spot for possible registeration
|
|
|
|
for (uint8_t i = 0; i < COMPASS_MAX_BACKEND; i++) {
|
|
|
|
if (_detected_modules[i].ap_uavcan == ap_uavcan &&
|
|
|
|
_detected_modules[i].node_id == node_id &&
|
|
|
|
_detected_modules[i].sensor_id == sensor_id) {
|
|
|
|
// Already Detected
|
|
|
|
already_detected = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!already_detected) {
|
|
|
|
for (uint8_t i = 0; i < COMPASS_MAX_BACKEND; i++) {
|
|
|
|
if (nullptr == _detected_modules[i].ap_uavcan) {
|
|
|
|
_detected_modules[i].ap_uavcan = ap_uavcan;
|
|
|
|
_detected_modules[i].node_id = node_id;
|
|
|
|
_detected_modules[i].sensor_id = sensor_id;
|
2020-07-20 18:25:08 -03:00
|
|
|
_detected_modules[i].devid = AP_HAL::Device::make_bus_id(AP_HAL::Device::BUS_TYPE_UAVCAN,
|
|
|
|
ap_uavcan->get_driver_index(),
|
|
|
|
node_id,
|
|
|
|
sensor_id + 1); // we use sensor_id as devtype
|
2018-07-20 08:18:00 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-20 03:18:10 -04:00
|
|
|
|
|
|
|
struct DetectedModules tempslot;
|
|
|
|
// Sort based on the node_id, larger values first
|
|
|
|
// we do this, so that we have repeatable compass
|
|
|
|
// registration, especially in cases of extraneous
|
|
|
|
// CAN compass is connected.
|
|
|
|
for (uint8_t i = 1; i < COMPASS_MAX_BACKEND; i++) {
|
|
|
|
for (uint8_t j = i; j > 0; j--) {
|
|
|
|
if (_detected_modules[j].node_id > _detected_modules[j-1].node_id) {
|
|
|
|
tempslot = _detected_modules[j];
|
|
|
|
_detected_modules[j] = _detected_modules[j-1];
|
|
|
|
_detected_modules[j-1] = tempslot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 08:18:00 -03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-03-11 06:50:49 -03:00
|
|
|
|
2018-07-20 08:18:00 -03:00
|
|
|
void AP_Compass_UAVCAN::handle_mag_msg(const Vector3f &mag)
|
|
|
|
{
|
|
|
|
Vector3f raw_field = mag * 1000.0;
|
2018-03-11 06:50:49 -03:00
|
|
|
|
2018-10-18 21:25:47 -03:00
|
|
|
accumulate_sample(raw_field, _instance);
|
2018-07-20 08:18:00 -03:00
|
|
|
}
|
2018-03-11 06:50:49 -03:00
|
|
|
|
2018-07-20 08:18:00 -03:00
|
|
|
void AP_Compass_UAVCAN::handle_magnetic_field(AP_UAVCAN* ap_uavcan, uint8_t node_id, const MagCb &cb)
|
|
|
|
{
|
2019-02-14 21:43:56 -04:00
|
|
|
WITH_SEMAPHORE(_sem_registry);
|
|
|
|
|
2019-02-14 21:45:14 -04:00
|
|
|
Vector3f mag_vector;
|
|
|
|
AP_Compass_UAVCAN* driver = get_uavcan_backend(ap_uavcan, node_id, 0);
|
|
|
|
if (driver != nullptr) {
|
|
|
|
mag_vector[0] = cb.msg->magnetic_field_ga[0];
|
|
|
|
mag_vector[1] = cb.msg->magnetic_field_ga[1];
|
|
|
|
mag_vector[2] = cb.msg->magnetic_field_ga[2];
|
|
|
|
driver->handle_mag_msg(mag_vector);
|
|
|
|
}
|
2018-07-20 08:18:00 -03:00
|
|
|
}
|
2018-07-18 03:27:37 -03:00
|
|
|
|
2018-07-20 08:18:00 -03:00
|
|
|
void AP_Compass_UAVCAN::handle_magnetic_field_2(AP_UAVCAN* ap_uavcan, uint8_t node_id, const Mag2Cb &cb)
|
|
|
|
{
|
2019-02-14 21:43:56 -04:00
|
|
|
WITH_SEMAPHORE(_sem_registry);
|
|
|
|
|
2019-02-14 21:45:14 -04:00
|
|
|
Vector3f mag_vector;
|
|
|
|
uint8_t sensor_id = cb.msg->sensor_id;
|
|
|
|
AP_Compass_UAVCAN* driver = get_uavcan_backend(ap_uavcan, node_id, sensor_id);
|
|
|
|
if (driver != nullptr) {
|
|
|
|
mag_vector[0] = cb.msg->magnetic_field_ga[0];
|
|
|
|
mag_vector[1] = cb.msg->magnetic_field_ga[1];
|
|
|
|
mag_vector[2] = cb.msg->magnetic_field_ga[2];
|
|
|
|
driver->handle_mag_msg(mag_vector);
|
|
|
|
}
|
2017-04-02 11:56:15 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AP_Compass_UAVCAN::read(void)
|
|
|
|
{
|
2018-10-18 21:25:47 -03:00
|
|
|
drain_accumulated_samples(_instance);
|
2017-04-02 11:56:15 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|