Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/AP_Periph/rangefinder.cpp
Views: 1798
#include "AP_Periph.h"12#ifdef HAL_PERIPH_ENABLE_RANGEFINDER34/*5rangefinder support6*/78#include <dronecan_msgs.h>910#ifndef AP_PERIPH_PROBE_CONTINUOUS11#define AP_PERIPH_PROBE_CONTINUOUS 012#endif1314extern const AP_HAL::HAL &hal;1516/*17update CAN rangefinder18*/19void AP_Periph_FW::can_rangefinder_update(void)20{21if (rangefinder.get_type(0) == RangeFinder::Type::NONE) {22return;23}24#if AP_PERIPH_PROBE_CONTINUOUS25// We only allow continuous probing for rangefinders while vehicle is disarmed. Probing is currently inefficient and leads to longer loop times.26if ((rangefinder.num_sensors() == 0) && !hal.util->get_soft_armed() && option_is_set(PeriphOptions::PROBE_CONTINUOUS)) {27uint32_t now = AP_HAL::millis();28static uint32_t last_probe_ms;29if (now - last_probe_ms >= 1000) {30last_probe_ms = now;31rangefinder.init(ROTATION_NONE);32}33}34#endif35uint32_t now = AP_HAL::millis();36if (g.rangefinder_max_rate > 0 &&37now - last_rangefinder_update_ms < uint32_t(1000/g.rangefinder_max_rate)) {38// limit to max rate39return;40}41last_rangefinder_update_ms = now;4243// update all rangefinder instances44rangefinder.update();4546// cycle through each rangefinder instance to find one to send47// equipment.range_sensor only uses 3 CAN frames so we just send all available sensor measurements.48for (uint8_t i = 0; i <= rangefinder.num_sensors(); i++) {4950if (rangefinder.get_type(i) == RangeFinder::Type::NONE) {51continue;52}5354AP_RangeFinder_Backend *backend = rangefinder.get_backend(i);55if (backend == nullptr) {56continue;57}5859RangeFinder::Status status = backend->status();60if (status <= RangeFinder::Status::NoData) {61// don't send any data for this instance62continue;63}6465const uint32_t sample_ms = backend->last_reading_ms();66if (last_rangefinder_sample_ms[i] == sample_ms) {67// don't same the same reading again68continue;69}70last_rangefinder_sample_ms[i] = sample_ms;7172uavcan_equipment_range_sensor_Measurement pkt {};73pkt.sensor_id = rangefinder.get_address(i);7475switch (status) {76case RangeFinder::Status::OutOfRangeLow:77pkt.reading_type = UAVCAN_EQUIPMENT_RANGE_SENSOR_MEASUREMENT_READING_TYPE_TOO_CLOSE;78break;79case RangeFinder::Status::OutOfRangeHigh:80pkt.reading_type = UAVCAN_EQUIPMENT_RANGE_SENSOR_MEASUREMENT_READING_TYPE_TOO_FAR;81break;82case RangeFinder::Status::Good:83pkt.reading_type = UAVCAN_EQUIPMENT_RANGE_SENSOR_MEASUREMENT_READING_TYPE_VALID_RANGE;84break;85default:86pkt.reading_type = UAVCAN_EQUIPMENT_RANGE_SENSOR_MEASUREMENT_READING_TYPE_UNDEFINED;87break;88}8990switch (backend->get_mav_distance_sensor_type()) {91case MAV_DISTANCE_SENSOR_LASER:92pkt.sensor_type = UAVCAN_EQUIPMENT_RANGE_SENSOR_MEASUREMENT_SENSOR_TYPE_LIDAR;93break;94case MAV_DISTANCE_SENSOR_ULTRASOUND:95pkt.sensor_type = UAVCAN_EQUIPMENT_RANGE_SENSOR_MEASUREMENT_SENSOR_TYPE_SONAR;96break;97case MAV_DISTANCE_SENSOR_RADAR:98pkt.sensor_type = UAVCAN_EQUIPMENT_RANGE_SENSOR_MEASUREMENT_SENSOR_TYPE_RADAR;99break;100default:101pkt.sensor_type = UAVCAN_EQUIPMENT_RANGE_SENSOR_MEASUREMENT_SENSOR_TYPE_UNDEFINED;102break;103}104105float dist_m = backend->distance();106pkt.range = dist_m;107108uint8_t buffer[UAVCAN_EQUIPMENT_RANGE_SENSOR_MEASUREMENT_MAX_SIZE];109uint16_t total_size = uavcan_equipment_range_sensor_Measurement_encode(&pkt, buffer, !periph.canfdout());110111canard_broadcast(UAVCAN_EQUIPMENT_RANGE_SENSOR_MEASUREMENT_SIGNATURE,112UAVCAN_EQUIPMENT_RANGE_SENSOR_MEASUREMENT_ID,113CANARD_TRANSFER_PRIORITY_LOW,114&buffer[0],115total_size);116117}118}119120#endif // HAL_PERIPH_ENABLE_RANGEFINDER121122123