CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Beacon/AP_Beacon_SITL.cpp
Views: 1798
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
AP_Beacon_SITL::AP_Beacon_SITL(AP_Beacon &frontend) :
45
AP_Beacon_Backend(frontend),
46
sitl(AP::sitl())
47
{
48
}
49
50
// return true if sensor is basically healthy (we are receiving data)
51
bool AP_Beacon_SITL::healthy()
52
{
53
// healthy if we have parsed a message within the past 300ms
54
return ((AP_HAL::millis() - last_update_ms) < AP_BEACON_TIMEOUT_MS);
55
}
56
57
// update the state of the sensor
58
void AP_Beacon_SITL::update(void)
59
{
60
uint32_t now = AP_HAL::millis();
61
if (now - last_update_ms < 10) {
62
return;
63
}
64
65
uint8_t beacon_id = next_beacon;
66
next_beacon = (next_beacon+1) % NUM_BEACONS;
67
68
// truth location of the flight vehicle
69
Location current_loc;
70
current_loc.lat = sitl->state.latitude * 1.0e7f;
71
current_loc.lng = sitl->state.longitude * 1.0e7f;
72
current_loc.alt = sitl->state.altitude * 1.0e2;
73
74
// where the beacon system origin is located
75
Location beacon_origin;
76
beacon_origin.lat = get_beacon_origin_lat() * 1.0e7f;
77
beacon_origin.lng = get_beacon_origin_lon() * 1.0e7f;
78
beacon_origin.alt = get_beacon_origin_alt() * 1.0e2;
79
80
// position of each beacon
81
Location beacon_loc = beacon_origin;
82
switch (beacon_id) {
83
case 0:
84
// NE corner
85
beacon_loc.offset(ORIGIN_OFFSET_NORTH + BEACON_SPACING_NORTH/2, ORIGIN_OFFSET_EAST + BEACON_SPACING_EAST/2);
86
break;
87
case 1:
88
// SE corner
89
beacon_loc.offset(ORIGIN_OFFSET_NORTH - BEACON_SPACING_NORTH/2, ORIGIN_OFFSET_EAST + BEACON_SPACING_EAST/2);
90
break;
91
case 2:
92
// SW corner
93
beacon_loc.offset(ORIGIN_OFFSET_NORTH - BEACON_SPACING_NORTH/2, ORIGIN_OFFSET_EAST - BEACON_SPACING_EAST/2);
94
break;
95
case 3:
96
// NW corner
97
beacon_loc.offset(ORIGIN_OFFSET_NORTH + BEACON_SPACING_NORTH/2, ORIGIN_OFFSET_EAST - BEACON_SPACING_EAST/2);
98
break;
99
}
100
101
const Vector2f beac_diff = beacon_origin.get_distance_NE(beacon_loc);
102
const Vector2f veh_diff = beacon_origin.get_distance_NE(current_loc);
103
104
Vector3f veh_pos3d(veh_diff.x, veh_diff.y, (beacon_origin.alt - current_loc.alt)*1.0e-2f);
105
Vector3f beac_pos3d(beac_diff.x, beac_diff.y, (beacon_loc.alt - beacon_origin.alt)*1.0e-2f);
106
Vector3f beac_veh_offset = veh_pos3d - beac_pos3d;
107
108
set_beacon_position(beacon_id, beac_pos3d);
109
set_beacon_distance(beacon_id, beac_veh_offset.length());
110
set_vehicle_position(veh_pos3d, 0.5f);
111
last_update_ms = now;
112
}
113
114
#endif // AP_BEACON_SITL_ENABLED
115
116