123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483 |
- #include "AP_Proximity.h"
- #include "AP_Proximity_LightWareSF40C.h"
- #include "AP_Proximity_RPLidarA2.h"
- #include "AP_Proximity_TeraRangerTower.h"
- #include "AP_Proximity_TeraRangerTowerEvo.h"
- #include "AP_Proximity_RangeFinder.h"
- #include "AP_Proximity_MAV.h"
- #include "AP_Proximity_SITL.h"
- #include "AP_Proximity_MorseSITL.h"
- #include "AP_Proximity_AirSimSITL.h"
- #include <AP_AHRS/AP_AHRS.h>
- extern const AP_HAL::HAL &hal;
- const AP_Param::GroupInfo AP_Proximity::var_info[] = {
-
-
-
-
-
-
-
- AP_GROUPINFO("_TYPE", 1, AP_Proximity, _type[0], 0),
-
-
-
-
-
- AP_GROUPINFO("_ORIENT", 2, AP_Proximity, _orientation[0], 0),
-
-
-
-
-
-
- AP_GROUPINFO("_YAW_CORR", 3, AP_Proximity, _yaw_correction[0], 0),
-
-
-
-
-
-
- AP_GROUPINFO("_IGN_ANG1", 4, AP_Proximity, _ignore_angle_deg[0], 0),
-
-
-
-
-
-
- AP_GROUPINFO("_IGN_WID1", 5, AP_Proximity, _ignore_width_deg[0], 0),
-
-
-
-
-
-
- AP_GROUPINFO("_IGN_ANG2", 6, AP_Proximity, _ignore_angle_deg[1], 0),
-
-
-
-
-
-
- AP_GROUPINFO("_IGN_WID2", 7, AP_Proximity, _ignore_width_deg[1], 0),
-
-
-
-
-
-
- AP_GROUPINFO("_IGN_ANG3", 8, AP_Proximity, _ignore_angle_deg[2], 0),
-
-
-
-
-
-
- AP_GROUPINFO("_IGN_WID3", 9, AP_Proximity, _ignore_width_deg[2], 0),
-
-
-
-
-
-
- AP_GROUPINFO("_IGN_ANG4", 10, AP_Proximity, _ignore_angle_deg[3], 0),
-
-
-
-
-
-
- AP_GROUPINFO("_IGN_WID4", 11, AP_Proximity, _ignore_width_deg[3], 0),
-
-
-
-
-
-
- AP_GROUPINFO("_IGN_ANG5", 12, AP_Proximity, _ignore_angle_deg[4], 0),
-
-
-
-
-
-
- AP_GROUPINFO("_IGN_WID5", 13, AP_Proximity, _ignore_width_deg[4], 0),
-
-
-
-
-
-
- AP_GROUPINFO("_IGN_ANG6", 14, AP_Proximity, _ignore_angle_deg[5], 0),
-
-
-
-
-
-
- AP_GROUPINFO("_IGN_WID6", 15, AP_Proximity, _ignore_width_deg[5], 0),
- #if PROXIMITY_MAX_INSTANCES > 1
-
-
-
-
-
-
- AP_GROUPINFO("2_TYPE", 16, AP_Proximity, _type[1], 0),
-
-
-
-
-
- AP_GROUPINFO("2_ORIENT", 17, AP_Proximity, _orientation[1], 0),
-
-
-
-
-
-
- AP_GROUPINFO("2_YAW_CORR", 18, AP_Proximity, _yaw_correction[1], 0),
- #endif
- AP_GROUPEND
- };
- AP_Proximity::AP_Proximity(AP_SerialManager &_serial_manager) :
- serial_manager(_serial_manager)
- {
- AP_Param::setup_object_defaults(this, var_info);
- #if CONFIG_HAL_BOARD == HAL_BOARD_SITL
- if (_singleton != nullptr) {
- AP_HAL::panic("AP_Proximity must be singleton");
- }
- #endif
- _singleton = this;
- }
- void AP_Proximity::init(void)
- {
- if (num_instances != 0) {
-
- return;
- }
- for (uint8_t i=0; i<PROXIMITY_MAX_INSTANCES; i++) {
- detect_instance(i);
- if (drivers[i] != nullptr) {
-
-
- num_instances = i+1;
- }
-
- state[i].status = Proximity_NotConnected;
- }
- }
- void AP_Proximity::update(void)
- {
- for (uint8_t i=0; i<num_instances; i++) {
- if (drivers[i] != nullptr) {
- if (_type[i] == Proximity_Type_None) {
-
- state[i].status = Proximity_NotConnected;
- continue;
- }
- drivers[i]->update();
- }
- }
-
- for (int8_t i=num_instances-1; i>=0; i--) {
- if (drivers[i] != nullptr && (state[i].status == Proximity_Good)) {
- primary_instance = i;
- }
- }
- }
- uint8_t AP_Proximity::get_orientation(uint8_t instance) const
- {
- if (instance >= PROXIMITY_MAX_INSTANCES) {
- return 0;
- }
- return _orientation[instance].get();
- }
- int16_t AP_Proximity::get_yaw_correction(uint8_t instance) const
- {
- if (instance >= PROXIMITY_MAX_INSTANCES) {
- return 0;
- }
- return _yaw_correction[instance].get();
- }
- AP_Proximity::Proximity_Status AP_Proximity::get_status(uint8_t instance) const
- {
-
- if (instance >= num_instances) {
- return Proximity_NotConnected;
- }
- return state[instance].status;
- }
- AP_Proximity::Proximity_Status AP_Proximity::get_status() const
- {
- return get_status(primary_instance);
- }
- void AP_Proximity::handle_msg(const mavlink_message_t &msg)
- {
- for (uint8_t i=0; i<num_instances; i++) {
- if ((drivers[i] != nullptr) && (_type[i] != Proximity_Type_None)) {
- drivers[i]->handle_msg(msg);
- }
- }
- }
- void AP_Proximity::detect_instance(uint8_t instance)
- {
- uint8_t type = _type[instance];
- if (type == Proximity_Type_SF40C) {
- if (AP_Proximity_LightWareSF40C::detect(serial_manager)) {
- state[instance].instance = instance;
- drivers[instance] = new AP_Proximity_LightWareSF40C(*this, state[instance], serial_manager);
- return;
- }
- }
- if (type == Proximity_Type_RPLidarA2) {
- if (AP_Proximity_RPLidarA2::detect(serial_manager)) {
- state[instance].instance = instance;
- drivers[instance] = new AP_Proximity_RPLidarA2(*this, state[instance], serial_manager);
- return;
- }
- }
- if (type == Proximity_Type_MAV) {
- state[instance].instance = instance;
- drivers[instance] = new AP_Proximity_MAV(*this, state[instance]);
- return;
- }
- if (type == Proximity_Type_TRTOWER) {
- if (AP_Proximity_TeraRangerTower::detect(serial_manager)) {
- state[instance].instance = instance;
- drivers[instance] = new AP_Proximity_TeraRangerTower(*this, state[instance], serial_manager);
- return;
- }
- }
- if (type == Proximity_Type_TRTOWEREVO) {
- if (AP_Proximity_TeraRangerTowerEvo::detect(serial_manager)) {
- state[instance].instance = instance;
- drivers[instance] = new AP_Proximity_TeraRangerTowerEvo(*this, state[instance], serial_manager);
- return;
- }
- }
- if (type == Proximity_Type_RangeFinder) {
- state[instance].instance = instance;
- drivers[instance] = new AP_Proximity_RangeFinder(*this, state[instance]);
- return;
- }
- #if CONFIG_HAL_BOARD == HAL_BOARD_SITL
- if (type == Proximity_Type_SITL) {
- state[instance].instance = instance;
- drivers[instance] = new AP_Proximity_SITL(*this, state[instance]);
- return;
- }
- if (type == Proximity_Type_MorseSITL) {
- state[instance].instance = instance;
- drivers[instance] = new AP_Proximity_MorseSITL(*this, state[instance]);
- return;
- }
- if (type == Proximity_Type_AirSimSITL) {
- state[instance].instance = instance;
- drivers[instance] = new AP_Proximity_AirSimSITL(*this, state[instance]);
- return;
- }
- #endif
- }
- bool AP_Proximity::get_horizontal_distance(uint8_t instance, float angle_deg, float &distance) const
- {
- if ((drivers[instance] == nullptr) || (_type[instance] == Proximity_Type_None)) {
- return false;
- }
-
- return drivers[instance]->get_horizontal_distance(angle_deg, distance);
- }
- bool AP_Proximity::get_horizontal_distance(float angle_deg, float &distance) const
- {
- return get_horizontal_distance(primary_instance, angle_deg, distance);
- }
- bool AP_Proximity::get_horizontal_distances(Proximity_Distance_Array &prx_dist_array) const
- {
- if ((drivers[primary_instance] == nullptr) || (_type[primary_instance] == Proximity_Type_None)) {
- return false;
- }
-
- return drivers[primary_instance]->get_horizontal_distances(prx_dist_array);
- }
- const Vector2f* AP_Proximity::get_boundary_points(uint8_t instance, uint16_t& num_points) const
- {
- if ((drivers[instance] == nullptr) || (_type[instance] == Proximity_Type_None)) {
- num_points = 0;
- return nullptr;
- }
-
- return drivers[instance]->get_boundary_points(num_points);
- }
- const Vector2f* AP_Proximity::get_boundary_points(uint16_t& num_points) const
- {
- return get_boundary_points(primary_instance, num_points);
- }
- bool AP_Proximity::get_closest_object(float& angle_deg, float &distance) const
- {
- if ((drivers[primary_instance] == nullptr) || (_type[primary_instance] == Proximity_Type_None)) {
- return false;
- }
-
- return drivers[primary_instance]->get_closest_object(angle_deg, distance);
- }
- uint8_t AP_Proximity::get_object_count() const
- {
- if ((drivers[primary_instance] == nullptr) || (_type[primary_instance] == Proximity_Type_None)) {
- return 0;
- }
-
- return drivers[primary_instance]->get_object_count();
- }
- bool AP_Proximity::get_object_angle_and_distance(uint8_t object_number, float& angle_deg, float &distance) const
- {
- if ((drivers[primary_instance] == nullptr) || (_type[primary_instance] == Proximity_Type_None)) {
- return false;
- }
-
- return drivers[primary_instance]->get_object_angle_and_distance(object_number, angle_deg, distance);
- }
- float AP_Proximity::distance_max() const
- {
- if ((drivers[primary_instance] == nullptr) || (_type[primary_instance] == Proximity_Type_None)) {
- return 0.0f;
- }
-
- return drivers[primary_instance]->distance_max();
- }
- float AP_Proximity::distance_min() const
- {
- if ((drivers[primary_instance] == nullptr) || (_type[primary_instance] == Proximity_Type_None)) {
- return 0.0f;
- }
-
- return drivers[primary_instance]->distance_min();
- }
- bool AP_Proximity::get_upward_distance(uint8_t instance, float &distance) const
- {
- if ((drivers[instance] == nullptr) || (_type[instance] == Proximity_Type_None)) {
- return false;
- }
-
- return drivers[instance]->get_upward_distance(distance);
- }
- bool AP_Proximity::get_upward_distance(float &distance) const
- {
- return get_upward_distance(primary_instance, distance);
- }
- AP_Proximity::Proximity_Type AP_Proximity::get_type(uint8_t instance) const
- {
- if (instance < PROXIMITY_MAX_INSTANCES) {
- return (Proximity_Type)((uint8_t)_type[instance]);
- }
- return Proximity_Type_None;
- }
- bool AP_Proximity::sensor_present() const
- {
- return get_status() != Proximity_NotConnected;
- }
- bool AP_Proximity::sensor_enabled() const
- {
- return _type[primary_instance] != Proximity_Type_None;
- }
- bool AP_Proximity::sensor_failed() const
- {
- return get_status() != Proximity_Good;
- }
- AP_Proximity *AP_Proximity::_singleton;
- namespace AP {
- AP_Proximity *proximity()
- {
- return AP_Proximity::get_singleton();
- }
- }
|