From 9c3812e3841f833aad7a6a139fe992509272f135 Mon Sep 17 00:00:00 2001 From: Randy Mackay Date: Wed, 29 May 2019 15:20:48 +0900 Subject: [PATCH] AP_Proximity: move copy_locations to backend also remove unused get_locations also fix comment for copy_locations method --- libraries/AP_Proximity/AP_Proximity.cpp | 64 ++----------------- libraries/AP_Proximity/AP_Proximity.h | 13 +--- .../AP_Proximity/AP_Proximity_Backend.cpp | 64 ++++++++++++++++++- libraries/AP_Proximity/AP_Proximity_Backend.h | 14 ++++ 4 files changed, 84 insertions(+), 71 deletions(-) diff --git a/libraries/AP_Proximity/AP_Proximity.cpp b/libraries/AP_Proximity/AP_Proximity.cpp index bf8035b435..8d09d4abf2 100644 --- a/libraries/AP_Proximity/AP_Proximity.cpp +++ b/libraries/AP_Proximity/AP_Proximity.cpp @@ -379,76 +379,24 @@ const Vector2f* AP_Proximity::get_boundary_points(uint16_t& num_points) const return get_boundary_points(primary_instance, num_points); } -const AP_Proximity::Proximity_Location* AP_Proximity::get_locations(uint16_t& location_count) const -{ - if (_location_count == 0) { - return nullptr; - } - - location_count = _location_count; - return _locations; -} - // copy location points around vehicle into a buffer owned by the caller // caller should provide the buff_size which is the maximum number of locations the buffer can hold (normally PROXIMITY_MAX_DIRECTION) // num_copied is updated with the number of locations copied into the buffer // returns true on success, false on failure (should only happen if there is a semaphore conflict) -bool AP_Proximity::copy_locations(Proximity_Location* buff, uint16_t buff_size, uint16_t& num_copied) +bool AP_Proximity::copy_locations(uint8_t instance, Proximity_Location* buff, uint16_t buff_size, uint16_t& num_copied) { - // sanity check buffer - if (buff == nullptr) { + if ((drivers[instance] == nullptr) || (_type[instance] == Proximity_Type_None)) { num_copied = 0; return false; } - WITH_SEMAPHORE(_rsem); - - // copy locations into caller's buffer - num_copied = MIN(_location_count, buff_size); - for (uint16_t i=0; icopy_locations(buff, buff_size, num_copied); } -void AP_Proximity::locations_update() +bool AP_Proximity::copy_locations(Proximity_Location* buff, uint16_t buff_size, uint16_t& num_copied) { - // exit immediately if primary sensor is disabled - if ((drivers[primary_instance] == nullptr) || (_type[primary_instance] == Proximity_Type_None)) { - return; - } - - WITH_SEMAPHORE(_rsem); - - // get number of objects, return early if none - uint8_t num_objects = get_object_count(); - if (num_objects == 0) { - _location_count = 0; - return; - } - - // get vehicle location and heading in degrees - Location my_loc; - if (!AP::ahrs().get_position(my_loc)) { - return; - } - const float veh_bearing = AP::ahrs().yaw_sensor * 0.01f; - - // convert offset from vehicle position to earth frame location - const uint32_t now_ms = AP_HAL::millis(); - _location_count = 0; - for (uint8_t i=0; i +#include +#include #include #include "AP_Proximity.h" #include "AP_Proximity_Backend.h" @@ -172,7 +174,7 @@ void AP_Proximity_Backend::init_boundary() _sector_edge_vector[sector].y = sinf(angle_rad) * 100.0f; _boundary_point[sector] = _sector_edge_vector[sector] * PROXIMITY_BOUNDARY_DIST_DEFAULT; } - frontend.locations_update(); + update_locations(); } // update boundary points used for object avoidance based on a single sector's distance changing @@ -227,7 +229,27 @@ void AP_Proximity_Backend::update_boundary_for_sector(uint8_t sector) if (!_distance_valid[prev_sector_ccw]) { _boundary_point[prev_sector_ccw] = _sector_edge_vector[prev_sector_ccw] * shortest_distance; } - frontend.locations_update(); + update_locations(); +} + +// copy location points around vehicle into a buffer owned by the caller +// caller should provide the buff_size which is the maximum number of locations the buffer can hold (normally PROXIMITY_MAX_DIRECTION) +// num_copied is updated with the number of locations copied into the buffer +// returns true on success, false on failure which should only happen if buff is nullptr +bool AP_Proximity_Backend::copy_locations(AP_Proximity::Proximity_Location* buff, uint16_t buff_size, uint16_t& num_copied) +{ + // sanity check buffer + if (buff == nullptr) { + num_copied = 0; + return false; + } + + WITH_SEMAPHORE(_rsem); + + // copy locations into caller's buffer + num_copied = MIN(_location_count, buff_size); + memcpy(buff, _locations, num_copied * sizeof(AP_Proximity::Proximity_Location)); + return true; } // set status and update valid count @@ -328,3 +350,41 @@ bool AP_Proximity_Backend::get_next_ignore_start_or_end(uint8_t start_or_end, in } return found; } + +void AP_Proximity_Backend::update_locations() +{ + WITH_SEMAPHORE(_rsem); + + // get number of objects, return early if none + const uint8_t num_objects = get_object_count(); + if (num_objects == 0) { + _location_count = 0; + return; + } + + // get vehicle location and heading in degrees + float veh_bearing; + Location my_loc; + { + WITH_SEMAPHORE(AP::ahrs().get_semaphore()); + if (!AP::ahrs().get_position(my_loc)) { + return; + } + veh_bearing = AP::ahrs().yaw_sensor * 0.01f; + } + + // convert offset from vehicle position to earth frame location + const uint32_t now_ms = AP_HAL::millis(); + _location_count = 0; + for (uint8_t i=0; i