2010-12-19 12:40:33 -04:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: t -*-
|
|
|
|
|
|
|
|
//****************************************************************
|
|
|
|
// Function that will calculate the desired direction to fly and distance
|
|
|
|
//****************************************************************
|
|
|
|
void navigate()
|
|
|
|
{
|
|
|
|
// do not navigate with corrupt data
|
|
|
|
// ---------------------------------
|
2011-02-24 01:56:59 -04:00
|
|
|
if (g_gps->fix == 0){
|
2011-02-17 05:36:33 -04:00
|
|
|
g_gps->new_data = false;
|
2010-12-19 12:40:33 -04:00
|
|
|
return;
|
|
|
|
}
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
if(next_WP.lat == 0){
|
|
|
|
return;
|
|
|
|
}
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-01-11 17:15:08 -04:00
|
|
|
// waypoint distance from plane
|
|
|
|
// ----------------------------
|
2011-02-24 01:56:59 -04:00
|
|
|
wp_distance = getDistance(¤t_loc, &next_WP);
|
2011-01-11 17:15:08 -04:00
|
|
|
|
2011-02-24 01:56:59 -04:00
|
|
|
if (wp_distance < 0){
|
|
|
|
gcs.send_text(SEVERITY_HIGH,"<navigate> WP error - distance < 0");
|
2011-01-11 17:15:08 -04:00
|
|
|
//Serial.println(wp_distance,DEC);
|
|
|
|
//print_current_waypoints();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-17 05:36:33 -04:00
|
|
|
// target_bearing is where we should be heading
|
2011-01-11 17:15:08 -04:00
|
|
|
// --------------------------------------------
|
|
|
|
target_bearing = get_bearing(¤t_loc, &next_WP);
|
|
|
|
|
2011-02-24 01:56:59 -04:00
|
|
|
// nav_bearing will includes xtrac correction
|
|
|
|
// ------------------------------------------
|
2011-01-11 17:15:08 -04:00
|
|
|
nav_bearing = target_bearing;
|
|
|
|
|
2011-02-24 01:56:59 -04:00
|
|
|
// check if we have missed the WP
|
|
|
|
loiter_delta = (target_bearing - old_target_bearing)/100;
|
|
|
|
|
|
|
|
// reset the old value
|
|
|
|
old_target_bearing = target_bearing;
|
|
|
|
|
|
|
|
// wrap values
|
|
|
|
if (loiter_delta > 180) loiter_delta -= 360;
|
|
|
|
if (loiter_delta < -180) loiter_delta += 360;
|
|
|
|
loiter_sum += abs(loiter_delta);
|
|
|
|
|
2011-01-11 17:15:08 -04:00
|
|
|
// control mode specific updates to nav_bearing
|
|
|
|
// --------------------------------------------
|
|
|
|
update_navigation();
|
|
|
|
}
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-02-06 03:02:51 -04:00
|
|
|
#define DIST_ERROR_MAX 1800
|
2011-01-11 17:15:08 -04:00
|
|
|
void calc_nav()
|
|
|
|
{
|
2011-02-06 03:02:51 -04:00
|
|
|
Vector2f yawvector;
|
2011-02-10 03:08:03 -04:00
|
|
|
Matrix3f temp;
|
2011-02-06 03:02:51 -04:00
|
|
|
|
2011-01-11 17:15:08 -04:00
|
|
|
/*
|
|
|
|
Becuase we are using lat and lon to do our distance errors here's a quick chart:
|
|
|
|
100 = 1m
|
|
|
|
1000 = 11m
|
2011-01-12 21:31:05 -04:00
|
|
|
3000 = 33m
|
2011-01-11 17:15:08 -04:00
|
|
|
10000 = 111m
|
|
|
|
pitch_max = 22° (2200)
|
2011-02-17 05:36:33 -04:00
|
|
|
*/
|
2011-01-12 21:31:05 -04:00
|
|
|
|
2011-02-10 03:08:03 -04:00
|
|
|
long_error = (float)(next_WP.lng - current_loc.lng) * scaleLongDown; // 50 - 30 = 20 pitch right
|
|
|
|
lat_error = next_WP.lat - current_loc.lat; // 50 - 30 = 20 pitch up
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-02-06 03:02:51 -04:00
|
|
|
long_error = constrain(long_error, -DIST_ERROR_MAX, DIST_ERROR_MAX); // +- 20m max error
|
|
|
|
lat_error = constrain(lat_error, -DIST_ERROR_MAX, DIST_ERROR_MAX); // +- 20m max error
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-02-10 03:08:03 -04:00
|
|
|
// Convert distance into ROLL X
|
2011-02-17 03:09:13 -04:00
|
|
|
nav_lon = long_error * g.pid_nav_lon.kP(); // 1800 * 2 = 3600 or 36°
|
|
|
|
//nav_lon = g.pid_nav_lon.get_pid(long_error, dTnav2, 1.0);
|
2011-02-06 03:02:51 -04:00
|
|
|
//nav_lon = constrain(nav_lon, -DIST_ERROR_MAX, DIST_ERROR_MAX); // Limit max command
|
2011-01-11 17:15:08 -04:00
|
|
|
|
2011-02-10 03:08:03 -04:00
|
|
|
// PITCH Y
|
2011-02-17 03:09:13 -04:00
|
|
|
//nav_lat = g.pid_nav_lat.get_pid(lat_error, dTnav2, 1.0);
|
|
|
|
nav_lat = lat_error * g.pid_nav_lat.kP(); // 1800 * 2 = 3600 or 36°
|
2011-02-06 03:02:51 -04:00
|
|
|
//nav_lat = constrain(nav_lat, -DIST_ERROR_MAX, DIST_ERROR_MAX); // Limit max command
|
2011-01-11 17:15:08 -04:00
|
|
|
|
2011-02-17 05:36:33 -04:00
|
|
|
// rotate the vector
|
2011-02-10 03:08:03 -04:00
|
|
|
nav_roll = (float)nav_lon * sin_yaw_y - (float)nav_lat * cos_yaw_x;
|
|
|
|
nav_pitch = -((float)nav_lon * cos_yaw_x + (float)nav_lat * sin_yaw_y);
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-02-25 01:33:39 -04:00
|
|
|
long pmax = g.pitch_max.get();
|
|
|
|
|
|
|
|
nav_roll = constrain(nav_roll, -pmax, pmax);
|
|
|
|
nav_pitch = constrain(nav_pitch, -pmax, pmax);
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void calc_bearing_error()
|
|
|
|
{
|
2011-01-25 01:53:36 -04:00
|
|
|
bearing_error = nav_bearing - dcm.yaw_sensor;
|
2010-12-19 12:40:33 -04:00
|
|
|
bearing_error = wrap_180(bearing_error);
|
|
|
|
}
|
|
|
|
|
2011-02-17 05:36:33 -04:00
|
|
|
void calc_altitude_error()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2011-01-21 01:28:01 -04:00
|
|
|
if(control_mode == AUTO && offset_altitude != 0) {
|
2011-02-17 03:25:31 -04:00
|
|
|
// limit climb rates - we draw a straight line between first location and edge of waypoint_radius
|
2011-02-17 05:36:33 -04:00
|
|
|
target_altitude = next_WP.alt - ((wp_distance * offset_altitude) / (wp_totalDistance - g.waypoint_radius));
|
|
|
|
|
2011-01-21 01:28:01 -04:00
|
|
|
// stay within a certain range
|
|
|
|
if(prev_WP.alt > next_WP.alt){
|
|
|
|
target_altitude = constrain(target_altitude, next_WP.alt, prev_WP.alt);
|
|
|
|
}else{
|
|
|
|
target_altitude = constrain(target_altitude, prev_WP.alt, next_WP.alt);
|
|
|
|
}
|
2011-02-24 01:56:59 -04:00
|
|
|
}else{
|
2011-01-21 01:28:01 -04:00
|
|
|
target_altitude = next_WP.alt;
|
|
|
|
}
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-01-21 01:28:01 -04:00
|
|
|
altitude_error = target_altitude - current_loc.alt;
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
long wrap_360(long error)
|
|
|
|
{
|
|
|
|
if (error > 36000) error -= 36000;
|
|
|
|
if (error < 0) error += 36000;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
long wrap_180(long error)
|
|
|
|
{
|
|
|
|
if (error > 18000) error -= 36000;
|
|
|
|
if (error < -18000) error += 36000;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_loiter()
|
|
|
|
{
|
2011-02-24 01:56:59 -04:00
|
|
|
float power;
|
|
|
|
|
|
|
|
if(wp_distance <= g.loiter_radius){
|
|
|
|
power = float(wp_distance) / float(g.loiter_radius);
|
|
|
|
nav_bearing += (int)(9000.0 * (2.0 + power));
|
|
|
|
|
|
|
|
}else if(wp_distance < (g.loiter_radius + LOITER_RANGE)){
|
|
|
|
power = -((float)(wp_distance - g.loiter_radius - LOITER_RANGE) / LOITER_RANGE);
|
|
|
|
power = constrain(power, 0, 1);
|
|
|
|
nav_bearing -= power * 9000;
|
|
|
|
|
|
|
|
}else{
|
|
|
|
update_crosstrack();
|
|
|
|
loiter_time = millis(); // keep start time for loiter updating till we get within LOITER_RANGE of orbit
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wp_distance < g.loiter_radius){
|
|
|
|
nav_bearing += 9000;
|
|
|
|
}else{
|
|
|
|
nav_bearing -= 100 * M_PI / 180 * asin(g.loiter_radius / wp_distance);
|
|
|
|
}
|
|
|
|
|
|
|
|
update_crosstrack;
|
|
|
|
nav_bearing = wrap_360(nav_bearing);
|
|
|
|
}
|
2010-12-19 12:40:33 -04:00
|
|
|
|
|
|
|
void update_crosstrack(void)
|
|
|
|
{
|
|
|
|
// Crosstrack Error
|
|
|
|
// ----------------
|
|
|
|
if (abs(target_bearing - crosstrack_bearing) < 4500) { // If we are too far off or too close we don't do track following
|
|
|
|
crosstrack_error = sin(radians((target_bearing - crosstrack_bearing) / 100)) * wp_distance; // Meters we are off track line
|
2011-02-17 05:36:33 -04:00
|
|
|
nav_bearing += constrain(crosstrack_error * g.crosstrack_gain, -g.crosstrack_entry_angle.get(), g.crosstrack_entry_angle.get());
|
2010-12-19 12:40:33 -04:00
|
|
|
nav_bearing = wrap_360(nav_bearing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_crosstrack()
|
|
|
|
{
|
|
|
|
crosstrack_bearing = get_bearing(¤t_loc, &next_WP); // Used for track following
|
|
|
|
}
|
|
|
|
|
2011-02-24 01:56:59 -04:00
|
|
|
long get_altitude_above_home(void)
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
|
|
|
// This is the altitude above the home location
|
|
|
|
// The GPS gives us altitude at Sea Level
|
|
|
|
// if you slope soar, you should see a negative number sometimes
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
return current_loc.alt - home.alt;
|
|
|
|
}
|
|
|
|
|
|
|
|
long getDistance(struct Location *loc1, struct Location *loc2)
|
|
|
|
{
|
2011-02-17 05:36:33 -04:00
|
|
|
if(loc1->lat == 0 || loc1->lng == 0)
|
2010-12-19 12:40:33 -04:00
|
|
|
return -1;
|
2011-02-17 05:36:33 -04:00
|
|
|
if(loc2->lat == 0 || loc2->lng == 0)
|
2010-12-19 12:40:33 -04:00
|
|
|
return -1;
|
|
|
|
float dlat = (float)(loc2->lat - loc1->lat);
|
|
|
|
float dlong = ((float)(loc2->lng - loc1->lng)) * scaleLongDown;
|
|
|
|
return sqrt(sq(dlat) + sq(dlong)) * .01113195;
|
|
|
|
}
|
|
|
|
|
|
|
|
long get_alt_distance(struct Location *loc1, struct Location *loc2)
|
|
|
|
{
|
|
|
|
return abs(loc1->alt - loc2->alt);
|
|
|
|
}
|
|
|
|
|
|
|
|
long get_bearing(struct Location *loc1, struct Location *loc2)
|
|
|
|
{
|
|
|
|
long off_x = loc2->lng - loc1->lng;
|
|
|
|
long off_y = (loc2->lat - loc1->lat) * scaleLongUp;
|
|
|
|
long bearing = 9000 + atan2(-off_y, off_x) * 5729.57795;
|
|
|
|
if (bearing < 0) bearing += 36000;
|
|
|
|
return bearing;
|
|
|
|
}
|