2013-09-22 01:45:24 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
/*
|
|
|
|
* rally point support
|
|
|
|
* Michael Day, September 2013
|
|
|
|
*/
|
|
|
|
|
|
|
|
//get a rally point
|
2013-10-02 23:20:54 -03:00
|
|
|
static bool get_rally_point_with_index(unsigned i, RallyLocation &ret)
|
2013-09-22 01:45:24 -03:00
|
|
|
{
|
2013-10-02 23:20:54 -03:00
|
|
|
if (i >= (unsigned)g.rally_total) {
|
|
|
|
return false;
|
2013-09-22 01:45:24 -03:00
|
|
|
}
|
2013-10-02 23:20:54 -03:00
|
|
|
hal.storage->read_block(&ret, RALLY_START_BYTE + (i * sizeof(RallyLocation)), sizeof(RallyLocation));
|
|
|
|
if (ret.lat == 0 && ret.lng == 0) {
|
|
|
|
// sanity check ...
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2013-09-22 01:45:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
//save a rally point
|
2013-10-02 23:20:54 -03:00
|
|
|
static bool set_rally_point_with_index(unsigned i, const RallyLocation &rallyLoc)
|
2013-09-22 01:45:24 -03:00
|
|
|
{
|
2013-10-02 23:20:54 -03:00
|
|
|
if (i >= (unsigned) g.rally_total) {
|
2013-09-22 01:45:24 -03:00
|
|
|
//not allowed
|
2013-10-02 23:20:54 -03:00
|
|
|
return false;
|
2013-09-22 01:45:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= MAX_RALLYPOINTS) {
|
|
|
|
//also not allowed
|
2013-10-02 23:20:54 -03:00
|
|
|
return false;
|
2013-09-22 01:45:24 -03:00
|
|
|
}
|
2013-10-02 23:20:54 -03:00
|
|
|
hal.storage->write_block(RALLY_START_BYTE + (i * sizeof(RallyLocation)), &rallyLoc, sizeof(RallyLocation));
|
|
|
|
return true;
|
2013-09-22 01:45:24 -03:00
|
|
|
}
|
|
|
|
|
2013-10-02 23:20:54 -03:00
|
|
|
// 'best' means 'closest to Location loc' for now.
|
|
|
|
static bool find_best_rally_point(const Location &myloc, const Location &homeloc, RallyLocation &ret)
|
|
|
|
{
|
|
|
|
float min_dis = -1;
|
2013-09-22 01:45:24 -03:00
|
|
|
|
2013-10-02 23:20:54 -03:00
|
|
|
for (unsigned i = 0; i < (unsigned) g.rally_total; i++) {
|
|
|
|
RallyLocation next_rally;
|
|
|
|
if (!get_rally_point_with_index(i, next_rally)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Location rally_loc = rally_location_to_location(next_rally, homeloc);
|
|
|
|
float dis = get_distance(myloc, rally_loc);
|
2013-09-22 01:45:24 -03:00
|
|
|
|
2013-10-02 23:20:54 -03:00
|
|
|
if (dis < min_dis || min_dis < 0) {
|
2013-09-22 01:45:24 -03:00
|
|
|
min_dis = dis;
|
|
|
|
ret = next_rally;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-06 00:54:50 -03:00
|
|
|
if (g.rally_limit_km > 0 && min_dis > g.rally_limit_km*1000.0f &&
|
2013-10-06 00:51:03 -03:00
|
|
|
get_distance(myloc, homeloc) < min_dis) {
|
|
|
|
// return false, which makes home be used instead
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-02 23:20:54 -03:00
|
|
|
return min_dis >= 0;
|
2013-09-22 01:45:24 -03:00
|
|
|
}
|
|
|
|
|
2013-10-02 23:20:54 -03:00
|
|
|
// translate a RallyLocation to a Location
|
2014-03-03 09:50:45 -04:00
|
|
|
static Location rally_location_to_location(const RallyLocation &r_loc, const Location &homeloc)
|
2013-10-02 23:20:54 -03:00
|
|
|
{
|
2013-12-30 18:40:24 -04:00
|
|
|
Location ret = {};
|
2014-03-03 09:50:45 -04:00
|
|
|
ret.flags.relative_alt = true;
|
2013-09-22 01:45:24 -03:00
|
|
|
|
|
|
|
//Currently can't do true AGL on the APM. Relative altitudes are
|
|
|
|
//relative to HOME point's altitude. Terrain on the board is inbound
|
|
|
|
//for the PX4, though. This line will need to be updated when that happens:
|
2013-10-14 02:38:40 -03:00
|
|
|
ret.alt = (r_loc.alt*100UL) + homeloc.alt;
|
2013-09-22 01:45:24 -03:00
|
|
|
|
|
|
|
ret.lat = r_loc.lat;
|
|
|
|
ret.lng = r_loc.lng;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2013-10-05 10:07:32 -03:00
|
|
|
|
|
|
|
// return best RTL location from current position
|
2014-03-03 09:50:45 -04:00
|
|
|
static AP_Mission::Mission_Command rally_find_best_cmd(const Location &myloc, const Location &homeloc)
|
2013-10-05 10:07:32 -03:00
|
|
|
{
|
2013-12-30 18:40:24 -04:00
|
|
|
RallyLocation ral_loc = {};
|
2014-03-03 09:50:45 -04:00
|
|
|
AP_Mission::Mission_Command ret = {};
|
|
|
|
ret.id = MAV_CMD_NAV_LOITER_UNLIM;
|
2014-01-03 07:33:40 -04:00
|
|
|
if (find_best_rally_point(myloc, home, ral_loc)) {
|
2013-10-05 10:07:32 -03:00
|
|
|
//we have setup Rally points: use them instead of Home for RTL
|
2014-03-03 09:50:45 -04:00
|
|
|
ret.content.location = rally_location_to_location(ral_loc, home);
|
2013-10-05 10:07:32 -03:00
|
|
|
} else {
|
2014-03-03 09:50:45 -04:00
|
|
|
ret.content.location = homeloc;
|
2013-10-05 10:07:32 -03:00
|
|
|
// Altitude to hold over home
|
2014-03-03 09:50:45 -04:00
|
|
|
ret.content.location.alt = read_alt_to_hold();
|
2013-10-05 10:07:32 -03:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|