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/libraries/AP_Beacon/AP_Beacon_Backend.cpp
Views: 1798
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/1415#include "AP_Beacon_Backend.h"1617#if AP_BEACON_ENABLED1819// debug20#include <stdio.h>21#include <AP_SerialManager/AP_SerialManager.h>2223/*24base class constructor.25This incorporates initialisation as well.26*/27AP_Beacon_Backend::AP_Beacon_Backend(AP_Beacon &frontend) :28_frontend(frontend)29{30const AP_SerialManager &serialmanager = AP::serialmanager();31uart = serialmanager.find_serial(AP_SerialManager::SerialProtocol_Beacon, 0);32if (uart == nullptr) {33return;34}3536uart->begin(serialmanager.find_baudrate(AP_SerialManager::SerialProtocol_Beacon, 0));37}3839// set vehicle position40// pos should be in meters in NED frame from the beacon's local origin41// accuracy_estimate is also in meters42void AP_Beacon_Backend::set_vehicle_position(const Vector3f& pos, float accuracy_estimate)43{44_frontend.veh_pos_update_ms = AP_HAL::millis();45_frontend.veh_pos_accuracy = accuracy_estimate;46_frontend.veh_pos_ned = correct_for_orient_yaw(pos);47}4849// set individual beacon distance from vehicle in meters in NED frame50void AP_Beacon_Backend::set_beacon_distance(uint8_t beacon_instance, float distance)51{52// sanity check instance53if (beacon_instance >= AP_BEACON_MAX_BEACONS) {54return;55}5657// setup new beacon58if (beacon_instance >= _frontend.num_beacons) {59_frontend.num_beacons = beacon_instance+1;60}6162_frontend.beacon_state[beacon_instance].distance_update_ms = AP_HAL::millis();63_frontend.beacon_state[beacon_instance].distance = distance;64_frontend.beacon_state[beacon_instance].healthy = true;65}6667// set beacon's position68// pos should be in meters in NED from the beacon's local origin69void AP_Beacon_Backend::set_beacon_position(uint8_t beacon_instance, const Vector3f& pos)70{71// sanity check instance72if (beacon_instance >= AP_BEACON_MAX_BEACONS) {73return;74}7576// setup new beacon77if (beacon_instance >= _frontend.num_beacons) {78_frontend.num_beacons = beacon_instance+1;79}8081// set position after correcting yaw82_frontend.beacon_state[beacon_instance].position = correct_for_orient_yaw(pos);83}8485// rotate vector (meters) to correct for beacon system yaw orientation86Vector3f AP_Beacon_Backend::correct_for_orient_yaw(const Vector3f &vector)87{88// exit immediately if no correction89if (_frontend.orient_yaw == 0) {90return vector;91}9293// check for change in parameter value and update constants94if (orient_yaw_deg != _frontend.orient_yaw) {95_frontend.orient_yaw.set(wrap_180(_frontend.orient_yaw.get()));9697// calculate rotation constants98orient_yaw_deg = _frontend.orient_yaw;99orient_cos_yaw = cosf(radians(orient_yaw_deg));100orient_sin_yaw = sinf(radians(orient_yaw_deg));101}102103// rotate x,y by -orient_yaw104Vector3f vec_rotated;105vec_rotated.x = vector.x*orient_cos_yaw - vector.y*orient_sin_yaw;106vec_rotated.y = vector.x*orient_sin_yaw + vector.y*orient_cos_yaw;107vec_rotated.z = vector.z;108return vec_rotated;109}110111#endif // AP_BEACON_ENABLED112113114