2011-03-19 07:20:11 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2013-02-18 01:58:24 -04:00
|
|
|
// run_nav_updates - top level call for the autopilot
|
|
|
|
// ensures calculations such as "distance to waypoint" are calculated before autopilot makes decisions
|
|
|
|
// To-Do - rename and move this function to make it's purpose more clear
|
2013-01-08 01:45:12 -04:00
|
|
|
static void run_nav_updates(void)
|
|
|
|
{
|
2013-02-18 01:58:24 -04:00
|
|
|
// fetch position from inertial navigation
|
|
|
|
calc_position();
|
2013-01-08 01:45:12 -04:00
|
|
|
|
2013-02-18 01:58:24 -04:00
|
|
|
// calculate distance and bearing for reporting and autopilot decisions
|
|
|
|
calc_distance_and_bearing();
|
2013-01-25 02:11:09 -04:00
|
|
|
|
2013-02-18 01:58:24 -04:00
|
|
|
// run autopilot to make high level decisions about control modes
|
|
|
|
run_autopilot();
|
|
|
|
}
|
|
|
|
|
|
|
|
// calc_position - get lat and lon positions from inertial nav library
|
|
|
|
static void calc_position(){
|
|
|
|
if( inertial_nav.position_ok() ) {
|
2013-01-25 02:11:09 -04:00
|
|
|
// pull position from interial nav library
|
|
|
|
current_loc.lng = inertial_nav.get_longitude();
|
|
|
|
current_loc.lat = inertial_nav.get_latitude();
|
|
|
|
}
|
2012-11-07 06:03:30 -04:00
|
|
|
}
|
|
|
|
|
2013-02-18 01:58:24 -04:00
|
|
|
// calc_distance_and_bearing - calculate distance and direction to waypoints for reporting and autopilot decisions
|
2012-11-07 06:03:30 -04:00
|
|
|
static void calc_distance_and_bearing()
|
|
|
|
{
|
2013-03-20 10:29:08 -03:00
|
|
|
Vector3f curr = inertial_nav.get_position();
|
2013-10-29 01:28:27 -03:00
|
|
|
|
2013-02-18 01:58:24 -04:00
|
|
|
// get target from loiter or wpinav controller
|
2014-02-03 03:21:52 -04:00
|
|
|
if (control_mode == LOITER || control_mode == CIRCLE) {
|
2014-01-19 10:35:55 -04:00
|
|
|
wp_distance = wp_nav.get_loiter_distance_to_target();
|
|
|
|
wp_bearing = wp_nav.get_loiter_bearing_to_target();
|
2014-02-03 03:21:52 -04:00
|
|
|
}else if (control_mode == AUTO) {
|
2014-01-19 10:35:55 -04:00
|
|
|
wp_distance = wp_nav.get_wp_distance_to_destination();
|
|
|
|
wp_bearing = wp_nav.get_wp_bearing_to_destination();
|
2013-01-28 02:59:55 -04:00
|
|
|
}else{
|
2013-03-20 10:29:08 -03:00
|
|
|
wp_distance = 0;
|
2013-01-28 02:59:55 -04:00
|
|
|
wp_bearing = 0;
|
|
|
|
}
|
2012-11-07 06:03:30 -04:00
|
|
|
|
2013-01-28 02:59:55 -04:00
|
|
|
// calculate home distance and bearing
|
2013-11-11 09:24:18 -04:00
|
|
|
if(GPS_ok()) {
|
2013-03-20 10:29:08 -03:00
|
|
|
home_distance = pythagorous2(curr.x, curr.y);
|
|
|
|
home_bearing = pv_get_bearing_cd(curr,Vector3f(0,0,0));
|
2012-12-08 01:23:32 -04:00
|
|
|
|
2013-01-28 02:59:55 -04:00
|
|
|
// update super simple bearing (if required) because it relies on home_bearing
|
2013-10-05 06:25:03 -03:00
|
|
|
update_super_simple_bearing(false);
|
2013-02-18 01:58:24 -04:00
|
|
|
}
|
2012-04-19 01:06:15 -03:00
|
|
|
}
|
|
|
|
|
2013-01-24 00:36:55 -04:00
|
|
|
// run_autopilot - highest level call to process mission commands
|
|
|
|
static void run_autopilot()
|
2012-11-07 06:03:30 -04:00
|
|
|
{
|
2014-02-03 03:21:52 -04:00
|
|
|
if (control_mode == AUTO) {
|
|
|
|
// load the next command if the command queues are empty
|
|
|
|
update_commands();
|
2013-04-24 08:59:49 -03:00
|
|
|
|
2014-02-03 03:21:52 -04:00
|
|
|
// process the active navigation and conditional commands
|
|
|
|
verify_commands();
|
2013-01-24 00:36:55 -04:00
|
|
|
}
|
2012-12-09 02:24:19 -04:00
|
|
|
}
|
|
|
|
|
2012-11-23 02:57:49 -04:00
|
|
|
// Keeps old data out of our calculation / logs
|
|
|
|
static void reset_nav_params(void)
|
|
|
|
{
|
|
|
|
// Will be set by new command
|
2012-12-08 01:23:32 -04:00
|
|
|
wp_bearing = 0;
|
2012-11-23 02:57:49 -04:00
|
|
|
|
|
|
|
// Will be set by new command
|
|
|
|
wp_distance = 0;
|
|
|
|
|
2013-02-18 01:58:24 -04:00
|
|
|
// Will be set by nav or loiter controllers
|
|
|
|
lon_error = 0;
|
2012-11-23 02:57:49 -04:00
|
|
|
lat_error = 0;
|
|
|
|
}
|