Line data Source code
1 : /* 2 : This program is free software: you can redistribute it and/or modify 3 : it under the terms of the GNU General Public License as published by 4 : the Free Software Foundation, either version 3 of the License, or 5 : (at your option) any later version. 6 : 7 : This program is distributed in the hope that it will be useful, 8 : but WITHOUT ANY WARRANTY; without even the implied warranty of 9 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 : GNU General Public License for more details. 11 : 12 : You should have received a copy of the GNU General Public License 13 : along with this program. If not, see <http://www.gnu.org/licenses/>. 14 : */ 15 : 16 : #include "AP_Beacon_SITL.h" 17 : 18 : #if AP_BEACON_SITL_ENABLED 19 : 20 : #include <AP_HAL/AP_HAL.h> 21 : 22 : #include <stdio.h> 23 : 24 : extern const AP_HAL::HAL& hal; 25 : 26 : #define NUM_BEACONS 4 27 : /* 28 : * Define a rectangular pattern of beacons with the pattern centroid located at the beacon origin as defined by the following params: 29 : * 30 : * BCN_ALT - Height above the WGS-84 geoid (m) 31 : * BCN_LATITUDE - WGS-84 latitude (deg) 32 : * BCN_LONGITUDE - WGS-84 longitude (deg) 33 : * 34 : * The spacing between beacons in the North/South and East/West directions is defined by the following parameters: 35 : */ 36 : #define BEACON_SPACING_NORTH 10.0 37 : #define BEACON_SPACING_EAST 20.0 38 : 39 : // The centroid of the pattern can be moved using using the following parameters: 40 : #define ORIGIN_OFFSET_NORTH 2.5 // shifts beacon pattern centroid North (m) 41 : #define ORIGIN_OFFSET_EAST 5.0 // shifts beacon pattern centroid East (m) 42 : 43 : // constructor 44 4 : AP_Beacon_SITL::AP_Beacon_SITL(AP_Beacon &frontend) : 45 : AP_Beacon_Backend(frontend), 46 4 : sitl(AP::sitl()) 47 : { 48 4 : } 49 : 50 : // return true if sensor is basically healthy (we are receiving data) 51 3462 : bool AP_Beacon_SITL::healthy() 52 : { 53 : // healthy if we have parsed a message within the past 300ms 54 3462 : return ((AP_HAL::millis() - last_update_ms) < AP_BEACON_TIMEOUT_MS); 55 : } 56 : 57 : // update the state of the sensor 58 110249 : void AP_Beacon_SITL::update(void) 59 : { 60 110249 : uint32_t now = AP_HAL::millis(); 61 110249 : if (now - last_update_ms < 10) { 62 79652 : return; 63 : } 64 : 65 30597 : uint8_t beacon_id = next_beacon; 66 30597 : next_beacon = (next_beacon+1) % NUM_BEACONS; 67 : 68 : // truth location of the flight vehicle 69 : const Location current_loc { 70 30597 : int32_t(sitl->state.latitude * 1.0e7f), 71 30597 : int32_t(sitl->state.longitude * 1.0e7f), 72 30597 : int32_t(sitl->state.altitude * 1.0e2f), 73 : Location::AltFrame::ABSOLUTE 74 30597 : }; 75 : 76 : // where the beacon system origin is located 77 : const Location beacon_origin { 78 30597 : int32_t(get_beacon_origin_lat() * 1.0e7f), 79 61194 : int32_t(get_beacon_origin_lon() * 1.0e7f), 80 61194 : int32_t(get_beacon_origin_alt() * 1.0e2f), 81 : Location::AltFrame::ABSOLUTE 82 30597 : }; 83 : 84 : // position of each beacon 85 30597 : Location beacon_loc = beacon_origin; 86 30597 : switch (beacon_id) { 87 7651 : case 0: 88 : // NE corner 89 7651 : beacon_loc.offset(ORIGIN_OFFSET_NORTH + BEACON_SPACING_NORTH/2, ORIGIN_OFFSET_EAST + BEACON_SPACING_EAST/2); 90 7651 : break; 91 7651 : case 1: 92 : // SE corner 93 7651 : beacon_loc.offset(ORIGIN_OFFSET_NORTH - BEACON_SPACING_NORTH/2, ORIGIN_OFFSET_EAST + BEACON_SPACING_EAST/2); 94 7651 : break; 95 7648 : case 2: 96 : // SW corner 97 7648 : beacon_loc.offset(ORIGIN_OFFSET_NORTH - BEACON_SPACING_NORTH/2, ORIGIN_OFFSET_EAST - BEACON_SPACING_EAST/2); 98 7648 : break; 99 7647 : case 3: 100 : // NW corner 101 7647 : beacon_loc.offset(ORIGIN_OFFSET_NORTH + BEACON_SPACING_NORTH/2, ORIGIN_OFFSET_EAST - BEACON_SPACING_EAST/2); 102 7647 : break; 103 : } 104 : 105 30597 : const Vector2f beac_diff = beacon_origin.get_distance_NE(beacon_loc); 106 30597 : const Vector2f veh_diff = beacon_origin.get_distance_NE(current_loc); 107 : 108 30597 : Vector3f veh_pos3d(veh_diff.x, veh_diff.y, (beacon_origin.alt - current_loc.alt)*1.0e-2f); 109 30597 : Vector3f beac_pos3d(beac_diff.x, beac_diff.y, (beacon_loc.alt - beacon_origin.alt)*1.0e-2f); 110 30597 : Vector3f beac_veh_offset = veh_pos3d - beac_pos3d; 111 : 112 30597 : set_beacon_position(beacon_id, beac_pos3d); 113 30597 : set_beacon_distance(beacon_id, beac_veh_offset.length()); 114 30597 : set_vehicle_position(veh_pos3d, 0.5f); 115 30597 : last_update_ms = now; 116 : } 117 : 118 : #endif // AP_BEACON_SITL_ENABLED