2020-12-31 20:10:39 -04:00
|
|
|
#include "AP_RangeFinder_USD1_CAN.h"
|
|
|
|
|
2022-03-12 06:37:29 -04:00
|
|
|
#if AP_RANGEFINDER_USD1_CAN_ENABLED
|
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2020-12-31 20:10:39 -04:00
|
|
|
|
2023-06-19 14:09:46 -03:00
|
|
|
RangeFinder_MultiCAN *AP_RangeFinder_USD1_CAN::multican;
|
2023-02-27 16:20:51 -04:00
|
|
|
|
2020-12-31 20:10:39 -04:00
|
|
|
/*
|
|
|
|
constructor
|
|
|
|
*/
|
|
|
|
AP_RangeFinder_USD1_CAN::AP_RangeFinder_USD1_CAN(RangeFinder::RangeFinder_State &_state, AP_RangeFinder_Params &_params) :
|
2023-06-19 14:09:46 -03:00
|
|
|
AP_RangeFinder_Backend_CAN(_state, _params)
|
2020-12-31 20:10:39 -04:00
|
|
|
{
|
2023-02-27 16:20:51 -04:00
|
|
|
if (multican == nullptr) {
|
2023-06-19 14:09:46 -03:00
|
|
|
multican = new RangeFinder_MultiCAN(AP_CAN::Protocol::USD1, "USD1 MultiCAN");
|
2023-02-27 16:20:51 -04:00
|
|
|
if (multican == nullptr) {
|
|
|
|
AP_BoardConfig::allocation_error("USD1_CAN");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// add to linked list of drivers
|
|
|
|
WITH_SEMAPHORE(multican->sem);
|
|
|
|
auto *prev = multican->drivers;
|
|
|
|
next = prev;
|
|
|
|
multican->drivers = this;
|
|
|
|
}
|
2020-12-31 20:10:39 -04:00
|
|
|
}
|
|
|
|
|
2021-10-18 00:20:53 -03:00
|
|
|
// handler for incoming frames. These come in at 100Hz
|
2023-02-27 16:20:51 -04:00
|
|
|
bool AP_RangeFinder_USD1_CAN::handle_frame(AP_HAL::CANFrame &frame)
|
2020-12-31 20:10:39 -04:00
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(_sem);
|
2023-02-27 16:20:51 -04:00
|
|
|
const uint16_t id = frame.id & AP_HAL::CANFrame::MaskStdID;
|
2023-06-19 14:09:46 -03:00
|
|
|
|
|
|
|
if (!is_correct_id(id)) {
|
2023-02-27 16:20:51 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-18 00:20:53 -03:00
|
|
|
const uint16_t dist_cm = (frame.data[0]<<8) | frame.data[1];
|
2023-07-19 05:00:46 -03:00
|
|
|
accumulate_distance_m(dist_cm * 0.01);
|
2023-02-27 16:20:51 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-12 06:37:29 -04:00
|
|
|
#endif // AP_RANGEFINDER_USD1_CAN_ENABLED
|