2014-07-23 08:02:50 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
handle vehicle <-> GCS communications for terrain library
|
|
|
|
*/
|
|
|
|
|
2019-07-09 07:22:07 -03:00
|
|
|
#include "AP_Terrain.h"
|
|
|
|
|
|
|
|
#include <AP_AHRS/AP_AHRS.h>
|
2015-08-11 03:28:46 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
#include <GCS_MAVLink/GCS_MAVLink.h>
|
|
|
|
#include <GCS_MAVLink/GCS.h>
|
2014-07-23 08:02:50 -03:00
|
|
|
|
2014-07-24 18:56:55 -03:00
|
|
|
#if AP_TERRAIN_AVAILABLE
|
2014-07-23 08:02:50 -03:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
/*
|
|
|
|
request any missing 4x4 grids from a block, given a grid_cache
|
|
|
|
*/
|
|
|
|
bool AP_Terrain::request_missing(mavlink_channel_t chan, struct grid_cache &gcache)
|
|
|
|
{
|
|
|
|
struct grid_block &grid = gcache.grid;
|
|
|
|
|
2020-05-01 22:52:00 -03:00
|
|
|
if (options.get() & uint16_t(Options::DisableDownload)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-06 03:17:39 -03:00
|
|
|
if (grid.spacing != grid_spacing) {
|
|
|
|
// an invalid grid
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-23 08:02:50 -03:00
|
|
|
// see if we are waiting for disk read
|
|
|
|
if (gcache.state == GRID_CACHE_DISKWAIT) {
|
2016-09-16 22:43:32 -03:00
|
|
|
// don't request data from the GCS till we know it's not on disk
|
2014-07-23 08:02:50 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// see if it is fully populated
|
|
|
|
if ((grid.bitmap & bitmap_mask) == bitmap_mask) {
|
|
|
|
// it is fully populated, nothing to do
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-05 01:11:13 -03:00
|
|
|
if (!HAVE_PAYLOAD_SPACE(chan, TERRAIN_REQUEST)) {
|
2014-07-24 22:40:56 -03:00
|
|
|
// not enough buffer space
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-23 08:02:50 -03:00
|
|
|
/*
|
|
|
|
ask the GCS to send a set of 4x4 grids
|
|
|
|
*/
|
|
|
|
mavlink_msg_terrain_request_send(chan, grid.lat, grid.lon, grid_spacing, bitmap_mask & ~grid.bitmap);
|
2015-11-19 23:14:56 -04:00
|
|
|
last_request_time_ms[chan] = AP_HAL::millis();
|
2014-07-23 08:02:50 -03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
request any missing 4x4 grids from a block
|
|
|
|
*/
|
|
|
|
bool AP_Terrain::request_missing(mavlink_channel_t chan, const struct grid_info &info)
|
|
|
|
{
|
|
|
|
// find the grid
|
2014-07-24 20:52:20 -03:00
|
|
|
struct grid_cache &gcache = find_grid_cache(info);
|
2014-07-23 08:02:50 -03:00
|
|
|
return request_missing(chan, gcache);
|
|
|
|
}
|
|
|
|
|
2020-06-18 20:15:23 -03:00
|
|
|
/*
|
|
|
|
send any pending cache requests
|
|
|
|
*/
|
|
|
|
bool AP_Terrain::send_cache_request(mavlink_channel_t chan)
|
|
|
|
{
|
|
|
|
for (uint16_t i=0; i<cache_size; i++) {
|
|
|
|
if (cache[i].state >= GRID_CACHE_VALID) {
|
|
|
|
if (request_missing(chan, cache[i])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-23 08:02:50 -03:00
|
|
|
/*
|
|
|
|
send any pending terrain request to the GCS
|
|
|
|
*/
|
|
|
|
void AP_Terrain::send_request(mavlink_channel_t chan)
|
|
|
|
{
|
2018-06-28 18:57:49 -03:00
|
|
|
if (!allocate()) {
|
2014-07-23 08:02:50 -03:00
|
|
|
// not enabled
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// see if we need to schedule some disk IO
|
|
|
|
schedule_disk_io();
|
|
|
|
|
|
|
|
Location loc;
|
2022-01-20 19:42:41 -04:00
|
|
|
if (!AP::ahrs().get_location(loc)) {
|
2020-06-18 20:15:23 -03:00
|
|
|
// we don't know where we are. Send a report and request any cached blocks.
|
|
|
|
// this allows for download of mission items when we have no GPS lock
|
|
|
|
loc = {};
|
|
|
|
send_terrain_report(chan, loc, true);
|
|
|
|
send_cache_request(chan);
|
2014-07-23 08:02:50 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-24 22:40:56 -03:00
|
|
|
// always send a terrain report
|
2014-08-05 22:43:37 -03:00
|
|
|
send_terrain_report(chan, loc, true);
|
2014-07-24 22:40:56 -03:00
|
|
|
|
|
|
|
// did we request recently?
|
2015-11-19 23:14:56 -04:00
|
|
|
if (AP_HAL::millis() - last_request_time_ms[chan] < 2000) {
|
2014-07-24 22:40:56 -03:00
|
|
|
// too soon to request again
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-23 08:02:50 -03:00
|
|
|
// request any missing 4x4 blocks in the current grid
|
|
|
|
struct grid_info info;
|
|
|
|
calculate_grid_info(loc, info);
|
|
|
|
|
|
|
|
if (request_missing(chan, info)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// also request a larger set of up to 9 grids
|
|
|
|
for (int8_t x=-1; x<=1; x++) {
|
|
|
|
for (int8_t y=-1; y<=1; y++) {
|
|
|
|
Location loc2 = loc;
|
2019-02-24 20:16:28 -04:00
|
|
|
loc2.offset(x*TERRAIN_GRID_BLOCK_SIZE_X*0.7f*grid_spacing,
|
|
|
|
y*TERRAIN_GRID_BLOCK_SIZE_Y*0.7f*grid_spacing);
|
2014-07-23 08:02:50 -03:00
|
|
|
struct grid_info info2;
|
|
|
|
calculate_grid_info(loc2, info2);
|
|
|
|
if (request_missing(chan, info2)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check cache blocks that may have been setup by a TERRAIN_CHECK
|
2020-06-18 20:15:23 -03:00
|
|
|
if (send_cache_request(chan)) {
|
|
|
|
return;
|
2014-07-23 08:02:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// request the current loc last to ensure it has highest last
|
|
|
|
// access time
|
|
|
|
if (request_missing(chan, info)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
count bits in a uint64_t
|
|
|
|
*/
|
2019-11-06 22:02:23 -04:00
|
|
|
uint8_t AP_Terrain::bitcount64(uint64_t b) const
|
2014-07-23 08:02:50 -03:00
|
|
|
{
|
|
|
|
return __builtin_popcount((unsigned)(b&0xFFFFFFFF)) + __builtin_popcount((unsigned)(b>>32));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
get some statistics for TERRAIN_REPORT
|
|
|
|
*/
|
2019-11-06 22:02:23 -04:00
|
|
|
void AP_Terrain::get_statistics(uint16_t &pending, uint16_t &loaded) const
|
2014-07-23 08:02:50 -03:00
|
|
|
{
|
|
|
|
pending = 0;
|
|
|
|
loaded = 0;
|
2015-09-22 19:31:17 -03:00
|
|
|
for (uint16_t i=0; i<cache_size; i++) {
|
2014-08-06 03:17:39 -03:00
|
|
|
if (cache[i].grid.spacing != grid_spacing) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-07-23 08:02:50 -03:00
|
|
|
if (cache[i].state == GRID_CACHE_INVALID) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uint8_t maskbits = TERRAIN_GRID_BLOCK_MUL_X*TERRAIN_GRID_BLOCK_MUL_Y;
|
|
|
|
if (cache[i].state == GRID_CACHE_DISKWAIT) {
|
|
|
|
pending += maskbits;
|
|
|
|
continue;
|
|
|
|
}
|
2014-08-06 03:17:39 -03:00
|
|
|
if (cache[i].state == GRID_CACHE_DIRTY) {
|
|
|
|
// count dirty grids as a pending, so we know where there
|
|
|
|
// are disk writes pending
|
|
|
|
pending++;
|
|
|
|
}
|
2014-07-23 08:02:50 -03:00
|
|
|
uint8_t bitcount = bitcount64(cache[i].grid.bitmap);
|
|
|
|
pending += maskbits - bitcount;
|
|
|
|
loaded += bitcount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
handle terrain messages from GCS
|
|
|
|
*/
|
2019-04-30 07:22:49 -03:00
|
|
|
void AP_Terrain::handle_data(mavlink_channel_t chan, const mavlink_message_t &msg)
|
2014-07-23 08:02:50 -03:00
|
|
|
{
|
2019-04-30 07:22:49 -03:00
|
|
|
if (msg.msgid == MAVLINK_MSG_ID_TERRAIN_DATA) {
|
2014-07-23 08:02:50 -03:00
|
|
|
handle_terrain_data(msg);
|
2019-04-30 07:22:49 -03:00
|
|
|
} else if (msg.msgid == MAVLINK_MSG_ID_TERRAIN_CHECK) {
|
2014-07-23 08:02:50 -03:00
|
|
|
handle_terrain_check(chan, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
send a TERRAIN_REPORT for a location
|
|
|
|
*/
|
2014-08-05 22:43:37 -03:00
|
|
|
void AP_Terrain::send_terrain_report(mavlink_channel_t chan, const Location &loc, bool extrapolate)
|
2014-07-23 08:02:50 -03:00
|
|
|
{
|
2014-07-24 22:40:56 -03:00
|
|
|
float terrain_height = 0;
|
|
|
|
float home_terrain_height = 0;
|
2014-07-23 08:02:50 -03:00
|
|
|
uint16_t spacing = 0;
|
2014-07-24 22:40:56 -03:00
|
|
|
Location current_loc;
|
2018-11-07 04:28:27 -04:00
|
|
|
const AP_AHRS &ahrs = AP::ahrs();
|
2022-01-20 19:42:41 -04:00
|
|
|
if (ahrs.get_location(current_loc) &&
|
2022-01-31 00:35:06 -04:00
|
|
|
height_amsl(ahrs.get_home(), home_terrain_height) &&
|
|
|
|
height_amsl(loc, terrain_height)) {
|
2014-07-24 22:40:56 -03:00
|
|
|
// non-zero spacing indicates we have data
|
2014-07-23 08:02:50 -03:00
|
|
|
spacing = grid_spacing;
|
2014-08-05 22:43:37 -03:00
|
|
|
} else if (extrapolate && have_current_loc_height) {
|
|
|
|
// show the extrapolated height, so logs show what height is
|
|
|
|
// being used for navigation
|
|
|
|
terrain_height = last_current_loc_height;
|
2020-06-18 20:15:23 -03:00
|
|
|
} else {
|
|
|
|
// report terrain height if we can, but can't give current_height
|
2022-01-31 00:35:06 -04:00
|
|
|
height_amsl(loc, terrain_height);
|
2014-07-23 08:02:50 -03:00
|
|
|
}
|
|
|
|
uint16_t pending, loaded;
|
|
|
|
get_statistics(pending, loaded);
|
2014-07-24 22:40:56 -03:00
|
|
|
|
2022-01-29 19:47:03 -04:00
|
|
|
float current_height = 0.0f;
|
2014-08-05 22:43:37 -03:00
|
|
|
if (spacing == 0 && !(extrapolate && have_current_loc_height)) {
|
2014-07-25 00:44:36 -03:00
|
|
|
current_height = 0;
|
2022-01-29 19:47:03 -04:00
|
|
|
} else if (!current_loc.is_zero()) {
|
2022-01-29 05:48:28 -04:00
|
|
|
int32_t height_above_home_cm = 0;
|
|
|
|
UNUSED_RESULT(current_loc.get_alt_cm(Location::AltFrame::ABOVE_HOME, height_above_home_cm));
|
|
|
|
current_height = height_above_home_cm * 0.01f; // cm -> m
|
2014-07-24 22:40:56 -03:00
|
|
|
}
|
|
|
|
current_height += home_terrain_height - terrain_height;
|
|
|
|
|
2016-04-05 01:11:13 -03:00
|
|
|
if (HAVE_PAYLOAD_SPACE(chan, TERRAIN_REPORT)) {
|
2014-07-24 22:40:56 -03:00
|
|
|
mavlink_msg_terrain_report_send(chan, loc.lat, loc.lng, spacing,
|
|
|
|
terrain_height, current_height,
|
|
|
|
pending, loaded);
|
2014-07-23 08:02:50 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
handle TERRAIN_CHECK messages from GCS
|
|
|
|
*/
|
2019-04-30 07:22:49 -03:00
|
|
|
void AP_Terrain::handle_terrain_check(mavlink_channel_t chan, const mavlink_message_t &msg)
|
2014-07-23 08:02:50 -03:00
|
|
|
{
|
|
|
|
mavlink_terrain_check_t packet;
|
2019-04-30 07:22:49 -03:00
|
|
|
mavlink_msg_terrain_check_decode(&msg, &packet);
|
2014-07-23 08:02:50 -03:00
|
|
|
Location loc;
|
|
|
|
loc.lat = packet.lat;
|
|
|
|
loc.lng = packet.lon;
|
2014-08-05 22:43:37 -03:00
|
|
|
send_terrain_report(chan, loc, false);
|
2014-07-23 08:02:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
handle TERRAIN_DATA messages from GCS
|
|
|
|
*/
|
2019-04-30 07:22:49 -03:00
|
|
|
void AP_Terrain::handle_terrain_data(const mavlink_message_t &msg)
|
2014-07-23 08:02:50 -03:00
|
|
|
{
|
|
|
|
mavlink_terrain_data_t packet;
|
2019-04-30 07:22:49 -03:00
|
|
|
mavlink_msg_terrain_data_decode(&msg, &packet);
|
2014-07-23 08:02:50 -03:00
|
|
|
|
|
|
|
uint16_t i;
|
2015-09-22 19:31:17 -03:00
|
|
|
for (i=0; i<cache_size; i++) {
|
2020-05-01 22:52:00 -03:00
|
|
|
if (TERRAIN_LATLON_EQUAL(cache[i].grid.lat,packet.lat) &&
|
|
|
|
TERRAIN_LATLON_EQUAL(cache[i].grid.lon,packet.lon) &&
|
2014-07-23 08:02:50 -03:00
|
|
|
cache[i].grid.spacing == packet.grid_spacing &&
|
2014-08-06 03:17:39 -03:00
|
|
|
grid_spacing == packet.grid_spacing &&
|
2014-07-23 08:02:50 -03:00
|
|
|
packet.gridbit < 56) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-09-22 19:31:17 -03:00
|
|
|
if (i == cache_size) {
|
2014-07-23 08:02:50 -03:00
|
|
|
// we don't have that grid, ignore data
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
struct grid_cache &gcache = cache[i];
|
|
|
|
struct grid_block &grid = gcache.grid;
|
|
|
|
uint8_t idx_x = (packet.gridbit / TERRAIN_GRID_BLOCK_MUL_Y) * TERRAIN_GRID_MAVLINK_SIZE;
|
|
|
|
uint8_t idx_y = (packet.gridbit % TERRAIN_GRID_BLOCK_MUL_Y) * TERRAIN_GRID_MAVLINK_SIZE;
|
|
|
|
ASSERT_RANGE(idx_x,0,(TERRAIN_GRID_BLOCK_MUL_X-1)*TERRAIN_GRID_MAVLINK_SIZE);
|
|
|
|
ASSERT_RANGE(idx_y,0,(TERRAIN_GRID_BLOCK_MUL_Y-1)*TERRAIN_GRID_MAVLINK_SIZE);
|
|
|
|
for (uint8_t x=0; x<TERRAIN_GRID_MAVLINK_SIZE; x++) {
|
|
|
|
for (uint8_t y=0; y<TERRAIN_GRID_MAVLINK_SIZE; y++) {
|
|
|
|
grid.height[idx_x+x][idx_y+y] = packet.data[x*TERRAIN_GRID_MAVLINK_SIZE+y];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gcache.grid.bitmap |= ((uint64_t)1) << packet.gridbit;
|
|
|
|
|
|
|
|
// mark dirty for disk IO
|
|
|
|
gcache.state = GRID_CACHE_DIRTY;
|
|
|
|
|
|
|
|
#if TERRAIN_DEBUG
|
|
|
|
hal.console->printf("Filled bit %u idx_x=%u idx_y=%u\n",
|
|
|
|
(unsigned)packet.gridbit, (unsigned)idx_x, (unsigned)idx_y);
|
|
|
|
if (gcache.grid.bitmap == bitmap_mask) {
|
|
|
|
hal.console->printf("--lat=%12.7f --lon=%12.7f %u\n",
|
|
|
|
grid.lat*1.0e-7f,
|
|
|
|
grid.lon*1.0e-7f,
|
|
|
|
grid.height[0][0]);
|
|
|
|
Location loc2;
|
|
|
|
loc2.lat = grid.lat;
|
|
|
|
loc2.lng = grid.lon;
|
2019-02-24 20:16:28 -04:00
|
|
|
loc2.offset(28*grid_spacing, 32*grid_spacing);
|
2014-07-23 08:02:50 -03:00
|
|
|
hal.console->printf("--lat=%12.7f --lon=%12.7f %u\n",
|
|
|
|
loc2.lat*1.0e-7f,
|
|
|
|
loc2.lng*1.0e-7f,
|
|
|
|
grid.height[27][31]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// see if we need to schedule some disk IO
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-24 18:56:55 -03:00
|
|
|
#endif // AP_TERRAIN_AVAILABLE
|