2020-12-31 20:10:39 -04:00
|
|
|
#include "AP_RangeFinder_USD1_CAN.h"
|
2023-02-27 16:20:51 -04:00
|
|
|
#include <AP_BoardConfig/AP_BoardConfig.h>
|
2020-12-31 20:10:39 -04:00
|
|
|
|
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-02-27 16:20:51 -04:00
|
|
|
const AP_Param::GroupInfo AP_RangeFinder_USD1_CAN::var_info[] = {
|
|
|
|
|
|
|
|
// @Param: RECV_ID
|
|
|
|
// @DisplayName: CAN receive ID
|
|
|
|
// @Description: The receive ID of the CAN frames. A value of zero means all IDs are accepted.
|
|
|
|
// @Range: 0 65535
|
|
|
|
// @User: Advanced
|
|
|
|
AP_GROUPINFO("RECV_ID", 12, AP_RangeFinder_USD1_CAN, receive_id, 0),
|
|
|
|
|
|
|
|
AP_GROUPEND
|
|
|
|
};
|
|
|
|
|
|
|
|
USD1_MultiCAN *AP_RangeFinder_USD1_CAN::multican;
|
|
|
|
|
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) :
|
|
|
|
AP_RangeFinder_Backend(_state, _params)
|
|
|
|
{
|
2023-02-27 16:20:51 -04:00
|
|
|
if (multican == nullptr) {
|
|
|
|
multican = new USD1_MultiCAN();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
AP_Param::setup_object_defaults(this, var_info);
|
|
|
|
state.var_info = var_info;
|
2020-12-31 20:10:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// update state
|
|
|
|
void AP_RangeFinder_USD1_CAN::update(void)
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(_sem);
|
2021-10-18 00:20:53 -03:00
|
|
|
const uint32_t now = AP_HAL::millis();
|
|
|
|
if (_distance_count == 0 && now - state.last_reading_ms > 500) {
|
|
|
|
// no new data.
|
2020-12-31 20:10:39 -04:00
|
|
|
set_status(RangeFinder::Status::NoData);
|
2021-10-18 00:20:53 -03:00
|
|
|
} else if (_distance_count != 0) {
|
|
|
|
state.distance_m = _distance_sum / _distance_count;
|
|
|
|
state.last_reading_ms = AP_HAL::millis();
|
|
|
|
_distance_sum = 0;
|
|
|
|
_distance_count = 0;
|
2020-12-31 20:10:39 -04:00
|
|
|
update_status();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
if (receive_id != 0 && id != uint16_t(receive_id.get())) {
|
|
|
|
// incorrect receive ID
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (last_recv_id != -1 && id != last_recv_id) {
|
|
|
|
// changing ID
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
last_recv_id = id;
|
|
|
|
|
2021-10-18 00:20:53 -03:00
|
|
|
const uint16_t dist_cm = (frame.data[0]<<8) | frame.data[1];
|
|
|
|
_distance_sum += dist_cm * 0.01;
|
|
|
|
_distance_count++;
|
2023-02-27 16:20:51 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle frames from CANSensor, passing to the drivers
|
|
|
|
void USD1_MultiCAN::handle_frame(AP_HAL::CANFrame &frame)
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(sem);
|
|
|
|
for (auto *d = drivers; d; d=d->next) {
|
|
|
|
if (d->handle_frame(frame)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-12-31 20:10:39 -04:00
|
|
|
}
|
|
|
|
|
2022-03-12 06:37:29 -04:00
|
|
|
#endif // AP_RANGEFINDER_USD1_CAN_ENABLED
|