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
|
|
|
|
|
|
|
//****************************************************************
|
|
|
|
// Function that will calculate the desired direction to fly and distance
|
|
|
|
//****************************************************************
|
2011-07-17 07:32:00 -03:00
|
|
|
static void navigate()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
|
|
|
// 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-03-02 22:32:50 -04:00
|
|
|
wp_distance = get_distance(¤t_loc, &next_WP);
|
2011-01-11 17:15:08 -04:00
|
|
|
|
2011-02-24 01:56:59 -04:00
|
|
|
if (wp_distance < 0){
|
2011-07-16 18:53:47 -03:00
|
|
|
//gcs.send_text_P(SEVERITY_HIGH,PSTR("<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-04-25 02:12:59 -03:00
|
|
|
}
|
2011-02-24 01:56:59 -04:00
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static bool check_missed_wp()
|
2011-04-25 02:12:59 -03:00
|
|
|
{
|
|
|
|
long temp = target_bearing - saved_target_bearing;
|
|
|
|
temp = wrap_180(temp);
|
|
|
|
return (abs(temp) > 10000); //we pased the waypoint by 10 °
|
2011-01-11 17:15:08 -04:00
|
|
|
}
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static int
|
2011-07-10 21:47:08 -03:00
|
|
|
get_nav_throttle(long error)
|
|
|
|
{
|
|
|
|
int throttle;
|
|
|
|
|
2011-07-16 18:53:47 -03:00
|
|
|
// limit error to 4 meters to prevent I term run up
|
2011-07-30 17:42:54 -03:00
|
|
|
error = constrain(error, -800,800);
|
2011-07-16 18:53:47 -03:00
|
|
|
|
2011-07-10 21:47:08 -03:00
|
|
|
throttle = g.pid_throttle.get_pid(error, delta_ms_medium_loop, 1.0);
|
|
|
|
throttle = g.throttle_cruise + constrain(throttle, -80, 80);
|
2011-07-16 18:53:47 -03:00
|
|
|
|
|
|
|
// failed experiment
|
2011-07-10 21:47:08 -03:00
|
|
|
//int tem = alt_hold_velocity();
|
|
|
|
//throttle -= tem;
|
|
|
|
|
|
|
|
return throttle;
|
|
|
|
}
|
|
|
|
|
2011-05-27 15:21:55 -03:00
|
|
|
#define DIST_ERROR_MAX 1800
|
2011-07-17 07:32:00 -03:00
|
|
|
static void calc_loiter_nav()
|
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
|
2011-05-20 02:23:12 -03:00
|
|
|
1000 = 11m = 36 feet
|
2011-04-16 17:44:23 -03:00
|
|
|
1800 = 19.80m = 60 feet
|
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-04-25 02:12:59 -03:00
|
|
|
|
2011-04-16 17:44:23 -03:00
|
|
|
// X ROLL
|
|
|
|
long_error = (float)(next_WP.lng - current_loc.lng) * scaleLongDown; // 500 - 0 = 500 roll EAST
|
|
|
|
|
|
|
|
// Y PITCH
|
|
|
|
lat_error = current_loc.lat - next_WP.lat; // 0 - 500 = -500 pitch NORTH
|
2011-01-12 21:31:05 -04:00
|
|
|
|
2011-07-02 19:44:59 -03:00
|
|
|
// constrain input, not output to let I term ramp up and do it's job again wind
|
|
|
|
long_error = constrain(long_error, -loiter_error_max, loiter_error_max); // +- 20m max error
|
|
|
|
lat_error = constrain(lat_error, -loiter_error_max, loiter_error_max); // +- 20m max error
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-05-20 02:23:12 -03:00
|
|
|
nav_lon = g.pid_nav_lon.get_pid(long_error, dTnav, 1.0); // X 700 * 2.5 = 1750,
|
|
|
|
nav_lat = g.pid_nav_lat.get_pid(lat_error, dTnav, 1.0); // Y invert lat (for pitch)
|
2011-05-16 01:59:06 -03:00
|
|
|
}
|
2011-04-16 17:44:23 -03:00
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static void calc_loiter_output()
|
2011-05-16 01:59:06 -03:00
|
|
|
{
|
2011-02-17 05:36:33 -04:00
|
|
|
// rotate the vector
|
2011-04-16 17:44:23 -03:00
|
|
|
nav_roll = (float)nav_lon * sin_yaw_y - (float)nav_lat * -cos_yaw_x;
|
|
|
|
// BAD
|
|
|
|
//NORTH -1000 * 1 - 1000 * 0 = -1000 // roll left
|
|
|
|
//WEST -1000 * 0 - 1000 * -1 = 1000 // roll right - Backwards
|
|
|
|
//EAST -1000 * 0 - 1000 * 1 = -1000 // roll left - Backwards
|
|
|
|
//SOUTH -1000 * -1 - 1000 * 0 = 1000 // roll right
|
|
|
|
|
|
|
|
// GOOD
|
|
|
|
//NORTH -1000 * 1 - 1000 * 0 = -1000 // roll left
|
|
|
|
//WEST -1000 * 0 - 1000 * 1 = -1000 // roll right
|
|
|
|
//EAST -1000 * 0 - 1000 * -1 = 1000 // roll left
|
|
|
|
//SOUTH -1000 * -1 - 1000 * 0 = 1000 // roll right
|
|
|
|
|
|
|
|
nav_pitch = ((float)nav_lon * -cos_yaw_x + (float)nav_lat * sin_yaw_y);
|
|
|
|
// BAD
|
|
|
|
//NORTH -1000 * 0 + 1000 * 1 = 1000 // pitch back
|
|
|
|
//WEST -1000 * -1 + 1000 * 0 = 1000 // pitch back - Backwards
|
|
|
|
//EAST -1000 * 1 + 1000 * 0 = -1000 // pitch forward - Backwards
|
|
|
|
//SOUTH -1000 * 0 + 1000 * -1 = -1000 // pitch forward
|
|
|
|
|
|
|
|
// GOOD
|
|
|
|
//NORTH -1000 * 0 + 1000 * 1 = 1000 // pitch back
|
|
|
|
//WEST -1000 * 1 + 1000 * 0 = -1000 // pitch forward
|
|
|
|
//EAST -1000 * -1 + 1000 * 0 = 1000 // pitch back
|
|
|
|
//SOUTH -1000 * 0 + 1000 * -1 = -1000 // pitch forward
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static void calc_simple_nav()
|
2011-03-14 03:04:07 -03:00
|
|
|
{
|
2011-04-25 02:12:59 -03:00
|
|
|
// no dampening here in SIMPLE mode
|
2011-07-02 19:44:59 -03:00
|
|
|
nav_lat = constrain((wp_distance * 100), -4500, 4500); // +- 20m max error
|
2011-04-25 02:12:59 -03:00
|
|
|
// Scale response by kP
|
2011-07-02 19:44:59 -03:00
|
|
|
//nav_lat *= g.pid_nav_lat.kP(); // 1800 * 2 = 3600 or 36°
|
2011-04-25 02:12:59 -03:00
|
|
|
}
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static void calc_nav_output()
|
2011-04-27 02:08:24 -03:00
|
|
|
{
|
|
|
|
// get the sin and cos of the bearing error - rotated 90°
|
2011-06-27 14:01:53 -03:00
|
|
|
float sin_nav_y = sin(radians((float)(9000 - bearing_error) / 100));
|
|
|
|
float cos_nav_x = cos(radians((float)(bearing_error - 9000) / 100));
|
2011-04-27 02:08:24 -03:00
|
|
|
|
|
|
|
// rotate the vector
|
2011-08-08 13:54:56 -03:00
|
|
|
//nav_roll = (float)nav_lat * cos_nav_x;
|
|
|
|
//nav_pitch = -(float)nav_lat * sin_nav_y;
|
|
|
|
nav_roll = (float)nav_lon * sin_nav_y - (float)nav_lat * -cos_nav_x;
|
|
|
|
nav_pitch = (float)nav_lon * cos_nav_x - (float)nav_lat * sin_nav_y;
|
2011-04-27 02:08:24 -03:00
|
|
|
}
|
2011-04-30 13:01:57 -03:00
|
|
|
|
2011-04-25 02:12:59 -03:00
|
|
|
// called after we get GPS read
|
2011-07-30 17:42:54 -03:00
|
|
|
static void calc_rate_nav(int speed)
|
2011-04-25 02:12:59 -03:00
|
|
|
{
|
|
|
|
// which direction are we moving?
|
2011-08-08 13:54:56 -03:00
|
|
|
long heading_error = nav_bearing - g_gps->ground_course;
|
|
|
|
heading_error = wrap_180(heading_error);
|
2011-04-25 02:12:59 -03:00
|
|
|
|
2011-05-16 01:59:06 -03:00
|
|
|
// calc the cos of the error to tell how fast we are moving towards the target in cm
|
2011-08-08 13:54:56 -03:00
|
|
|
int targetspeed = (float)g_gps->ground_speed * cos(radians((float)heading_error/100));
|
|
|
|
|
|
|
|
// calc the sin of the error to tell how fast we are moving laterally to the target in cm
|
|
|
|
int lateralspeed = (float)g_gps->ground_speed * sin(radians((float)heading_error/100));
|
|
|
|
//targetspeed = max(targetspeed, 0);
|
2011-05-05 02:19:45 -03:00
|
|
|
|
2011-07-10 21:47:08 -03:00
|
|
|
// Reduce speed on RTL
|
2011-07-30 17:42:54 -03:00
|
|
|
if(control_mode == RTL){
|
|
|
|
int tmp = min(wp_distance, 80) * 50;
|
|
|
|
waypoint_speed = min(tmp, speed);
|
|
|
|
waypoint_speed = max(waypoint_speed, 50);
|
|
|
|
}else{
|
|
|
|
int tmp = min(wp_distance, 200) * 90;
|
|
|
|
waypoint_speed = min(tmp, speed);
|
|
|
|
waypoint_speed = max(waypoint_speed, 50);
|
|
|
|
//waypoint_speed = g.waypoint_speed_max.get();
|
|
|
|
}
|
2011-07-10 21:47:08 -03:00
|
|
|
|
2011-08-08 13:54:56 -03:00
|
|
|
int error = constrain(waypoint_speed - targetspeed, -1000, 1000);
|
2011-04-25 02:12:59 -03:00
|
|
|
// Scale response by kP
|
2011-07-30 17:42:54 -03:00
|
|
|
nav_lat += g.pid_nav_wp.get_pid(error, dTnav, 1.0);
|
|
|
|
nav_lat >>= 1; // divide by two for smooting
|
2011-05-18 20:38:24 -03:00
|
|
|
|
2011-08-08 13:54:56 -03:00
|
|
|
nav_lon += lateralspeed * 2; // 2 is our fake PID gain
|
|
|
|
nav_lon >>= 1;
|
|
|
|
|
|
|
|
//Serial.printf("dTnav: %ld, gs: %d, err: %d, int: %d, pitch: %ld", dTnav, targetspeed, error, (int)g.pid_nav_wp.get_integrator(), (long)nav_lat);
|
2011-04-25 02:12:59 -03:00
|
|
|
|
|
|
|
// limit our output
|
2011-07-31 16:57:35 -03:00
|
|
|
nav_lat = constrain(nav_lat, -3500, 3500); // +- max error
|
2011-03-14 03:04:07 -03:00
|
|
|
}
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static void calc_bearing_error()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2011-07-30 17:42:54 -03:00
|
|
|
// 83 99 Yaw = -16
|
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-07-17 07:32:00 -03:00
|
|
|
static void calc_altitude_error()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2011-03-09 02:37:09 -04:00
|
|
|
altitude_error = next_WP.alt - current_loc.alt;
|
|
|
|
}
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static void calc_altitude_smoothing_error()
|
2011-03-09 02:37:09 -04:00
|
|
|
{
|
|
|
|
// limit climb rates - we draw a straight line between first location and edge of waypoint_radius
|
2011-07-21 20:36:22 -03:00
|
|
|
target_altitude = next_WP.alt - ((float)(wp_distance * (next_WP.alt - prev_WP.alt)) / (float)(wp_totalDistance - g.waypoint_radius));
|
2011-03-05 01:12:16 -04:00
|
|
|
|
2011-03-09 02:37:09 -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);
|
2011-03-05 00:56:58 -04:00
|
|
|
}else{
|
2011-03-09 02:37:09 -04:00
|
|
|
target_altitude = constrain(target_altitude, prev_WP.alt, next_WP.alt);
|
2011-01-21 01:28:01 -04:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2011-07-30 17:42:54 -03:00
|
|
|
static void update_loiter()
|
|
|
|
{
|
|
|
|
float power;
|
|
|
|
|
|
|
|
if(wp_distance <= g.loiter_radius){
|
|
|
|
power = float(wp_distance) / float(g.loiter_radius);
|
|
|
|
power = constrain(power, 0.5, 1);
|
|
|
|
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.5, 1); //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
|
|
|
|
|
|
|
|
}
|
|
|
|
nav_bearing = wrap_360(nav_bearing);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static long wrap_360(long error)
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
|
|
|
if (error > 36000) error -= 36000;
|
|
|
|
if (error < 0) error += 36000;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static long wrap_180(long error)
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
|
|
|
if (error > 18000) error -= 36000;
|
|
|
|
if (error < -18000) error += 36000;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static void update_crosstrack(void)
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
|
|
|
// Crosstrack Error
|
|
|
|
// ----------------
|
2011-05-31 02:29:06 -03:00
|
|
|
if (cross_track_test() < 9000) { // If we are too far off or too close we don't do track following
|
2011-08-08 13:54:56 -03:00
|
|
|
// Meters we are off track line
|
|
|
|
crosstrack_error = sin(radians((target_bearing - crosstrack_bearing) / (float)100)) * (float)wp_distance;
|
|
|
|
|
|
|
|
// take meters * 100 to get adjustment to nav_bearing
|
2011-07-10 21:47:08 -03:00
|
|
|
long xtrack = g.pid_crosstrack.get_pid(crosstrack_error, dTnav, 1.0) * 100;
|
|
|
|
nav_bearing += constrain(xtrack, -g.crosstrack_entry_angle.get(), g.crosstrack_entry_angle.get());
|
2010-12-19 12:40:33 -04:00
|
|
|
nav_bearing = wrap_360(nav_bearing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static long cross_track_test()
|
2011-04-21 20:07:31 -03:00
|
|
|
{
|
|
|
|
long temp = target_bearing - crosstrack_bearing;
|
|
|
|
temp = wrap_180(temp);
|
|
|
|
return abs(temp);
|
|
|
|
}
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static void reset_crosstrack()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
|
|
|
crosstrack_bearing = get_bearing(¤t_loc, &next_WP); // Used for track following
|
|
|
|
}
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static 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;
|
|
|
|
}
|
|
|
|
|
2011-03-26 03:35:52 -03:00
|
|
|
// distance is returned in meters
|
2011-07-17 07:32:00 -03:00
|
|
|
static long get_distance(struct Location *loc1, struct Location *loc2)
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2011-03-15 02:54:48 -03:00
|
|
|
//if(loc1->lat == 0 || loc1->lng == 0)
|
|
|
|
// return -1;
|
|
|
|
//if(loc2->lat == 0 || loc2->lng == 0)
|
|
|
|
// return -1;
|
2010-12-19 12:40:33 -04:00
|
|
|
float dlat = (float)(loc2->lat - loc1->lat);
|
|
|
|
float dlong = ((float)(loc2->lng - loc1->lng)) * scaleLongDown;
|
|
|
|
return sqrt(sq(dlat) + sq(dlong)) * .01113195;
|
|
|
|
}
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static long get_alt_distance(struct Location *loc1, struct Location *loc2)
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
|
|
|
return abs(loc1->alt - loc2->alt);
|
|
|
|
}
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static long get_bearing(struct Location *loc1, struct Location *loc2)
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|