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-09-20 02:24:57 -03:00
|
|
|
static byte navigate()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2012-01-06 14:20:59 -04:00
|
|
|
// waypoint distance from plane in meters
|
|
|
|
// ---------------------------------------
|
2012-01-29 02:00:05 -04:00
|
|
|
wp_distance = get_distance(¤t_loc, &next_WP);
|
|
|
|
home_distance = get_distance(¤t_loc, &home);
|
2011-01-11 17:15:08 -04:00
|
|
|
|
2011-02-24 01:56:59 -04:00
|
|
|
if (wp_distance < 0){
|
2012-01-29 02:00:05 -04:00
|
|
|
// something went very wrong
|
2011-09-20 02:24:57 -03:00
|
|
|
return 0;
|
2011-01-11 17:15:08 -04:00
|
|
|
}
|
|
|
|
|
2011-02-17 05:36:33 -04:00
|
|
|
// target_bearing is where we should be heading
|
2011-01-11 17:15:08 -04:00
|
|
|
// --------------------------------------------
|
2012-01-04 02:49:40 -04:00
|
|
|
target_bearing = get_bearing(¤t_loc, &next_WP);
|
2011-12-09 19:34:20 -04:00
|
|
|
home_to_copter_bearing = get_bearing(&home, ¤t_loc);
|
2011-12-23 18:40:54 -04:00
|
|
|
|
2011-12-31 03:47:44 -04:00
|
|
|
// nav_bearing will includes xtrac correction
|
|
|
|
// ------------------------------------------
|
|
|
|
nav_bearing = target_bearing;
|
|
|
|
|
2011-09-20 02:24:57 -03:00
|
|
|
return 1;
|
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
|
|
|
{
|
2012-01-04 02:49:40 -04:00
|
|
|
int32_t temp;
|
|
|
|
temp = target_bearing - original_target_bearing;
|
|
|
|
temp = wrap_180(temp);
|
2011-11-28 18:22:05 -04:00
|
|
|
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-08-14 15:18:32 -03:00
|
|
|
// ------------------------------
|
|
|
|
|
2011-12-23 18:40:54 -04:00
|
|
|
static void calc_XY_velocity(){
|
|
|
|
// offset calculation of GPS speed:
|
|
|
|
// used for estimations below 1.5m/s
|
|
|
|
// our GPS is about 1m per
|
2011-12-25 19:44:27 -04:00
|
|
|
static int32_t last_longitude = 0;
|
2012-01-12 02:32:05 -04:00
|
|
|
static int32_t last_latitude = 0;
|
2011-12-23 18:40:54 -04:00
|
|
|
|
2012-01-31 00:58:19 -04:00
|
|
|
static int16_t x_speed_old = 0;
|
|
|
|
static int16_t y_speed_old = 0;
|
2012-01-13 02:26:15 -04:00
|
|
|
|
2011-12-29 02:51:25 -04:00
|
|
|
// y_GPS_speed positve = Up
|
|
|
|
// x_GPS_speed positve = Right
|
|
|
|
|
2011-12-29 21:32:08 -04:00
|
|
|
// this speed is ~ in cm because we are using 10^7 numbers from GPS
|
|
|
|
float tmp = 1.0/dTnav;
|
|
|
|
|
2012-01-13 02:26:15 -04:00
|
|
|
// straightforward approach:
|
2012-01-13 16:48:02 -04:00
|
|
|
///*
|
2012-03-12 03:21:33 -03:00
|
|
|
x_actual_speed = x_speed_old + (float)(g_gps->longitude - last_longitude) * scaleLongDown * tmp;
|
2012-01-31 00:58:19 -04:00
|
|
|
y_actual_speed = y_speed_old + (float)(g_gps->latitude - last_latitude) * tmp;
|
|
|
|
|
2012-03-12 03:21:33 -03:00
|
|
|
x_actual_speed = x_actual_speed >> 1;
|
|
|
|
y_actual_speed = y_actual_speed >> 1;
|
2012-01-31 00:58:19 -04:00
|
|
|
|
|
|
|
x_speed_old = x_actual_speed;
|
|
|
|
y_speed_old = y_actual_speed;
|
2012-01-12 02:32:05 -04:00
|
|
|
|
2012-01-13 16:48:02 -04:00
|
|
|
/*
|
2012-01-13 02:26:15 -04:00
|
|
|
// Ryan Beall's forward estimator:
|
2012-02-26 15:15:47 -04:00
|
|
|
int16_t x_speed_new = (float)(g_gps->longitude - last_longitude) * scaleLongDown* tmp;
|
2012-01-13 02:26:15 -04:00
|
|
|
int16_t y_speed_new = (float)(g_gps->latitude - last_latitude) * tmp;
|
2011-12-29 21:32:08 -04:00
|
|
|
|
2012-01-29 21:10:52 -04:00
|
|
|
x_actual_speed = x_speed_new + (x_speed_new - x_speed_old);
|
|
|
|
y_actual_speed = y_speed_new + (y_speed_new - y_speed_old);
|
2011-12-31 03:47:44 -04:00
|
|
|
|
2012-01-13 02:26:15 -04:00
|
|
|
x_speed_old = x_speed_new;
|
|
|
|
y_speed_old = y_speed_new;
|
2012-01-13 16:48:02 -04:00
|
|
|
*/
|
2011-12-25 19:44:27 -04:00
|
|
|
|
|
|
|
last_longitude = g_gps->longitude;
|
2012-01-12 02:32:05 -04:00
|
|
|
last_latitude = g_gps->latitude;
|
2011-12-23 18:40:54 -04:00
|
|
|
}
|
|
|
|
|
2011-09-08 22:59:42 -03:00
|
|
|
static void calc_location_error(struct Location *next_loc)
|
2011-01-11 17:15:08 -04:00
|
|
|
{
|
2012-03-12 03:21:33 -03:00
|
|
|
static int16_t last_lon_error = 0;
|
|
|
|
static int16_t last_lat_error = 0;
|
|
|
|
|
|
|
|
static int16_t last_lon_d = 0;
|
|
|
|
static int16_t last_lat_d = 0;
|
|
|
|
|
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
|
2011-02-17 05:36:33 -04:00
|
|
|
*/
|
2011-04-25 02:12:59 -03:00
|
|
|
|
2011-12-29 02:51:25 -04:00
|
|
|
// X Error
|
|
|
|
long_error = (float)(next_loc->lng - current_loc.lng) * scaleLongDown; // 500 - 0 = 500 Go East
|
2011-04-16 17:44:23 -03:00
|
|
|
|
2011-12-29 02:51:25 -04:00
|
|
|
// Y Error
|
|
|
|
lat_error = next_loc->lat - current_loc.lat; // 500 - 0 = 500 Go North
|
2012-03-12 03:21:33 -03:00
|
|
|
|
|
|
|
int16_t tmp;
|
|
|
|
|
|
|
|
tmp = (long_error - last_lon_error);
|
|
|
|
if(abs(abs(tmp) -last_lon_d) > 15) tmp = x_rate_d;
|
|
|
|
x_rate_d = lon_rate_d_filter.apply(tmp);
|
|
|
|
last_lon_d = abs(tmp);
|
|
|
|
|
|
|
|
tmp = (lat_error - last_lat_error);
|
|
|
|
if(abs(abs(tmp) -last_lat_d) > 15) tmp = y_rate_d;
|
|
|
|
//if(abs(tmp) > 80) tmp = y_rate_d;
|
|
|
|
y_rate_d = lat_rate_d_filter.apply(tmp);
|
|
|
|
last_lat_d = abs(tmp);
|
|
|
|
|
|
|
|
int16_t t22 = x_rate_d * (g.pid_loiter_rate_lon.kD() / dTnav);
|
|
|
|
int16_t raww = (long_error - last_lon_error);
|
|
|
|
|
|
|
|
//Serial.printf("XX, %d, %d, %d\n", raww, x_rate_d, t22);
|
|
|
|
|
|
|
|
last_lon_error = long_error;
|
|
|
|
last_lat_error = lat_error;
|
2011-05-16 01:59:06 -03:00
|
|
|
}
|
2011-04-16 17:44:23 -03:00
|
|
|
|
2012-01-31 00:58:19 -04:00
|
|
|
#define NAV_ERR_MAX 600
|
2012-01-22 16:37:32 -04:00
|
|
|
static void calc_loiter(int x_error, int y_error)
|
2011-05-16 01:59:06 -03:00
|
|
|
{
|
2012-02-24 02:01:44 -04:00
|
|
|
int16_t x_target_speed, y_target_speed;
|
2012-03-10 12:27:58 -04:00
|
|
|
//int16_t x_iterm, y_iterm;
|
2012-02-24 02:01:44 -04:00
|
|
|
|
|
|
|
// East / West
|
|
|
|
x_target_speed = g.pi_loiter_lon.get_p(x_error); // not contstrained yet
|
2012-02-26 15:15:47 -04:00
|
|
|
//x_target_speed = constrain(x_target_speed, -250, 250); // limit to 2.5m/s travel speed
|
2012-02-24 02:01:44 -04:00
|
|
|
x_rate_error = x_target_speed - x_actual_speed; // calc the speed error
|
2012-03-12 03:21:33 -03:00
|
|
|
nav_lon = g.pid_loiter_rate_lon.get_pi(x_rate_error, dTnav);
|
|
|
|
nav_lon -= x_rate_d * (g.pid_loiter_rate_lon.kD() / dTnav);
|
2012-02-24 02:01:44 -04:00
|
|
|
nav_lon = constrain(nav_lon, -3000, 3000); // 30°
|
|
|
|
|
|
|
|
// North / South
|
|
|
|
y_target_speed = g.pi_loiter_lat.get_p(y_error);
|
2012-02-26 15:15:47 -04:00
|
|
|
//y_target_speed = constrain(y_target_speed, -250, 250);
|
2012-02-24 02:01:44 -04:00
|
|
|
y_rate_error = y_target_speed - y_actual_speed;
|
2012-03-12 03:21:33 -03:00
|
|
|
nav_lat = g.pid_loiter_rate_lat.get_pi(y_rate_error, dTnav);
|
|
|
|
nav_lat -= y_rate_d * (g.pid_loiter_rate_lat.kD() / dTnav);
|
2012-02-24 02:01:44 -04:00
|
|
|
nav_lat = constrain(nav_lat, -3000, 3000); // 30°
|
|
|
|
|
|
|
|
// copy over I term to Nav_Rate
|
|
|
|
g.pid_nav_lon.set_integrator(g.pid_loiter_rate_lon.get_integrator());
|
|
|
|
g.pid_nav_lat.set_integrator(g.pid_loiter_rate_lat.get_integrator());
|
|
|
|
|
2012-02-26 15:15:47 -04:00
|
|
|
|
2012-02-24 02:01:44 -04:00
|
|
|
// Wind I term based on location error,
|
2012-01-31 00:58:19 -04:00
|
|
|
// limit windup
|
2012-02-24 02:01:44 -04:00
|
|
|
/*
|
|
|
|
x_error = constrain(x_error, -NAV_ERR_MAX, NAV_ERR_MAX);
|
|
|
|
y_error = constrain(y_error, -NAV_ERR_MAX, NAV_ERR_MAX);
|
|
|
|
x_iterm = g.pi_loiter_lon.get_i(x_error, dTnav);
|
|
|
|
y_iterm = g.pi_loiter_lat.get_i(y_error, dTnav);
|
|
|
|
nav_lat = nav_lat + y_iterm;
|
|
|
|
nav_lon = nav_lon + x_iterm;
|
|
|
|
*/
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-12-31 03:47:44 -04:00
|
|
|
/*
|
2011-12-29 02:51:25 -04:00
|
|
|
int8_t ttt = 1.0/dTnav;
|
|
|
|
int16_t t2 = g.pi_nav_lat.get_integrator();
|
2011-12-30 01:35:01 -04:00
|
|
|
|
|
|
|
// 1 2 3 4 5 6 7 8 9 10
|
|
|
|
Serial.printf("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n",
|
2011-12-29 02:51:25 -04:00
|
|
|
wp_distance, //1
|
2011-12-30 01:35:01 -04:00
|
|
|
y_error, //2
|
|
|
|
y_GPS_speed, //3
|
2011-12-31 03:47:44 -04:00
|
|
|
|
2011-12-30 01:35:01 -04:00
|
|
|
y_actual_speed, //4 ;
|
|
|
|
y_target_speed, //5
|
|
|
|
y_rate_error, //6
|
|
|
|
nav_lat_p, //7
|
|
|
|
nav_lat, //8
|
|
|
|
y_iterm, //9
|
|
|
|
t2); //10
|
2011-12-29 02:51:25 -04:00
|
|
|
//*/
|
|
|
|
|
|
|
|
/*
|
2012-01-29 02:00:05 -04:00
|
|
|
int16_t t1 = g.pid_nav_lon.get_integrator(); // X
|
2011-12-29 02:51:25 -04:00
|
|
|
Serial.printf("%d, %1.4f, %d, %d, %d, %d, %d, %d, %d, %d\n",
|
|
|
|
wp_distance, //1
|
|
|
|
dTnav, //2
|
|
|
|
x_error, //3
|
|
|
|
x_GPS_speed, //4
|
|
|
|
x_actual_speed, //5
|
|
|
|
x_target_speed, //6
|
|
|
|
x_rate_error, //7
|
|
|
|
nav_lat, //8
|
|
|
|
x_iterm, //9
|
|
|
|
t1); //10
|
2011-12-25 19:44:27 -04:00
|
|
|
//*/
|
2011-03-14 03:04:07 -03:00
|
|
|
}
|
|
|
|
|
2012-01-29 02:00:05 -04:00
|
|
|
static void calc_nav_rate(int max_speed)
|
|
|
|
{
|
|
|
|
// push us towards the original track
|
|
|
|
update_crosstrack();
|
|
|
|
|
|
|
|
// nav_bearing includes crosstrack
|
2012-02-24 02:01:44 -04:00
|
|
|
float temp = (9000l - nav_bearing) * RADX100;
|
|
|
|
|
|
|
|
// East / West
|
|
|
|
x_rate_error = (cos(temp) * max_speed) - x_actual_speed; // 413
|
|
|
|
x_rate_error = constrain(x_rate_error, -1000, 1000);
|
|
|
|
nav_lon = g.pid_nav_lon.get_pid(x_rate_error, dTnav);
|
|
|
|
nav_lon = constrain(nav_lon, -3000, 3000);
|
|
|
|
|
|
|
|
// North / South
|
|
|
|
y_rate_error = (sin(temp) * max_speed) - y_actual_speed; // 413
|
|
|
|
y_rate_error = constrain(y_rate_error, -1000, 1000); // added a rate error limit to keep pitching down to a minimum
|
|
|
|
nav_lat = g.pid_nav_lat.get_pid(y_rate_error, dTnav);
|
|
|
|
nav_lat = constrain(nav_lat, -3000, 3000);
|
2012-01-29 02:00:05 -04:00
|
|
|
|
2012-02-24 02:01:44 -04:00
|
|
|
// copy over I term to Loiter_Rate
|
|
|
|
g.pid_loiter_rate_lon.set_integrator(g.pid_nav_lon.get_integrator());
|
|
|
|
g.pid_loiter_rate_lat.set_integrator(g.pid_nav_lat.get_integrator());
|
2012-01-29 02:00:05 -04:00
|
|
|
|
2012-02-24 02:01:44 -04:00
|
|
|
//int16_t x_iterm = g.pi_loiter_lon.get_i(x_rate_error, dTnav);
|
|
|
|
//int16_t y_iterm = g.pi_loiter_lat.get_i(y_rate_error, dTnav);
|
2012-01-29 02:00:05 -04:00
|
|
|
|
2012-02-24 02:01:44 -04:00
|
|
|
//nav_lon = nav_lon + x_iterm;
|
|
|
|
//nav_lat = nav_lat + y_iterm;
|
2012-02-15 13:10:07 -04:00
|
|
|
|
2012-01-29 02:00:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Serial.printf("max_sp %d,\t x_sp %d, y_sp %d,\t x_re: %d, y_re: %d, \tnav_lon: %d, nav_lat: %d, Xi:%d, Yi:%d, \t XE %d \n",
|
|
|
|
max_speed,
|
|
|
|
x_actual_speed,
|
|
|
|
y_actual_speed,
|
|
|
|
x_rate_error,
|
|
|
|
y_rate_error,
|
|
|
|
nav_lon,
|
|
|
|
nav_lat,
|
|
|
|
x_iterm,
|
|
|
|
y_iterm,
|
|
|
|
crosstrack_error);
|
|
|
|
//*/
|
|
|
|
|
|
|
|
// nav_lat and nav_lon will be rotated to the angle of the quad in calc_nav_pitch_roll()
|
|
|
|
|
|
|
|
/*Serial.printf("max_speed: %d, xspeed: %d, yspeed: %d, x_re: %d, y_re: %d, nav_lon: %ld, nav_lat: %ld ",
|
|
|
|
max_speed,
|
|
|
|
x_actual_speed,
|
|
|
|
y_actual_speed,
|
|
|
|
x_rate_error,
|
|
|
|
y_rate_error,
|
|
|
|
nav_lon,
|
|
|
|
nav_lat);*/
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-15 13:10:07 -04:00
|
|
|
/*static void calc_nav_lon(int rate)
|
2012-01-29 02:00:05 -04:00
|
|
|
{
|
|
|
|
nav_lon = g.pid_nav_lon.get_pid(rate, dTnav);
|
|
|
|
nav_lon = constrain(nav_lon, -3000, 3000);
|
|
|
|
}
|
2011-12-29 02:51:25 -04:00
|
|
|
|
2012-01-29 02:00:05 -04:00
|
|
|
static void calc_nav_lat(int rate)
|
|
|
|
{
|
2012-01-30 03:02:00 -04:00
|
|
|
nav_lat = g.pid_nav_lat.get_pid(rate, dTnav);
|
2012-01-29 02:00:05 -04:00
|
|
|
nav_lat = constrain(nav_lat, -3000, 3000);
|
|
|
|
}
|
2012-02-15 13:10:07 -04:00
|
|
|
*/
|
2011-12-29 02:51:25 -04:00
|
|
|
|
2012-02-09 13:29:40 -04:00
|
|
|
//static int16_t get_corrected_angle(int16_t desired_rate, int16_t rate_out)
|
|
|
|
/*{
|
2012-01-29 02:00:05 -04:00
|
|
|
int16_t tt = desired_rate;
|
|
|
|
// scale down the desired rate and square it
|
|
|
|
desired_rate = desired_rate / 20;
|
|
|
|
desired_rate = desired_rate * desired_rate;
|
|
|
|
int16_t tmp = 0;
|
|
|
|
|
|
|
|
if (tt > 0){
|
|
|
|
tmp = rate_out + (rate_out - desired_rate);
|
|
|
|
tmp = max(tmp, rate_out);
|
|
|
|
}else if (tt < 0){
|
|
|
|
tmp = rate_out + (rate_out + desired_rate);
|
|
|
|
tmp = min(tmp, rate_out);
|
|
|
|
}
|
|
|
|
//Serial.printf("rate:%d, norm:%d, out:%d \n", tt, rate_out, tmp);
|
|
|
|
return tmp;
|
2012-02-09 13:29:40 -04:00
|
|
|
}*/
|
2011-10-27 16:37:33 -03:00
|
|
|
|
2012-01-29 02:00:05 -04:00
|
|
|
//wp_distance,ttt, y_error, y_GPS_speed, y_actual_speed, y_target_speed, y_rate_error, nav_lat, y_iterm, t2
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-23 18:40:54 -04:00
|
|
|
// this calculation rotates our World frame of reference to the copter's frame of reference
|
|
|
|
// We use the DCM's matrix to precalculate these trig values at 50hz
|
2011-09-29 03:11:19 -03:00
|
|
|
static void calc_loiter_pitch_roll()
|
|
|
|
{
|
2011-10-27 16:37:33 -03:00
|
|
|
//Serial.printf("ys %ld, cx %1.4f, _cx %1.4f | sy %1.4f, _sy %1.4f\n", dcm.yaw_sensor, cos_yaw_x, _cos_yaw_x, sin_yaw_y, _sin_yaw_y);
|
2011-09-29 03:11:19 -03:00
|
|
|
// rotate the vector
|
2012-02-15 13:10:07 -04:00
|
|
|
auto_roll = (float)nav_lon * sin_yaw_y - (float)nav_lat * cos_yaw_x;
|
|
|
|
auto_pitch = (float)nav_lon * cos_yaw_x + (float)nav_lat * sin_yaw_y;
|
2011-09-29 03:11:19 -03:00
|
|
|
|
|
|
|
// flip pitch because forward is negative
|
2012-02-15 13:10:07 -04:00
|
|
|
auto_pitch = -auto_pitch;
|
2011-09-29 03:11:19 -03:00
|
|
|
}
|
|
|
|
|
2012-02-19 17:13:21 -04:00
|
|
|
static int16_t calc_desired_speed(int16_t max_speed, bool _slow)
|
2011-09-24 21:40:29 -03:00
|
|
|
{
|
2011-09-25 01:49:45 -03:00
|
|
|
/*
|
2012-01-21 15:58:32 -04:00
|
|
|
|< WP Radius
|
2011-11-09 16:06:37 -04:00
|
|
|
0 1 2 3 4 5 6 7 8m
|
2011-09-25 01:49:45 -03:00
|
|
|
...|...|...|...|...|...|...|...|
|
2011-11-09 16:06:37 -04:00
|
|
|
100 | 200 300 400cm/s
|
|
|
|
| +|+
|
|
|
|
|< we should slow to 1.5 m/s as we hit the target
|
2011-09-25 01:49:45 -03:00
|
|
|
*/
|
2011-11-09 16:06:37 -04:00
|
|
|
|
2012-01-04 02:49:40 -04:00
|
|
|
// max_speed is default 600 or 6m/s
|
2012-02-19 17:13:21 -04:00
|
|
|
if(_slow){
|
|
|
|
max_speed = min(max_speed, wp_distance / 2);
|
|
|
|
max_speed = max(max_speed, 0);
|
|
|
|
}else{
|
|
|
|
max_speed = min(max_speed, wp_distance);
|
|
|
|
max_speed = max(max_speed, WAYPOINT_SPEED_MIN); // go at least 100cm/s
|
|
|
|
}
|
2011-09-24 21:40:29 -03:00
|
|
|
|
2011-10-03 14:37:56 -03:00
|
|
|
// limit the ramp up of the speed
|
2011-11-09 16:06:37 -04:00
|
|
|
// waypoint_speed_gov is reset to 0 at each new WP command
|
2012-01-22 02:12:57 -04:00
|
|
|
if(max_speed > waypoint_speed_gov){
|
2012-01-21 15:58:32 -04:00
|
|
|
waypoint_speed_gov += (int)(100.0 * dTnav); // increase at .5/ms
|
2012-01-22 02:12:57 -04:00
|
|
|
max_speed = waypoint_speed_gov;
|
2011-10-03 14:37:56 -03:00
|
|
|
}
|
|
|
|
|
2012-01-04 02:49:40 -04:00
|
|
|
return max_speed;
|
|
|
|
}
|
|
|
|
|
2011-12-31 03:47:44 -04:00
|
|
|
|
2011-11-10 03:30:16 -04:00
|
|
|
static void update_crosstrack(void)
|
2011-11-10 02:56:09 -04:00
|
|
|
{
|
|
|
|
// Crosstrack Error
|
|
|
|
// ----------------
|
2011-12-31 03:47:44 -04:00
|
|
|
if (abs(wrap_180(target_bearing - original_target_bearing)) < 4500) { // If we are too far off or too close we don't do track following
|
2011-11-19 15:08:03 -04:00
|
|
|
float temp = (target_bearing - original_target_bearing) * RADX100;
|
2011-12-23 18:40:54 -04:00
|
|
|
crosstrack_error = sin(temp) * (wp_distance * g.crosstrack_gain); // Meters we are off track line
|
2011-12-31 03:47:44 -04:00
|
|
|
nav_bearing = target_bearing + constrain(crosstrack_error, -3000, 3000);
|
|
|
|
nav_bearing = wrap_360(nav_bearing);
|
|
|
|
}else{
|
|
|
|
nav_bearing = target_bearing;
|
2011-11-10 02:56:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-07 18:24:32 -04:00
|
|
|
static int32_t get_altitude_error()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2012-01-11 03:38:16 -04:00
|
|
|
// Next_WP alt is our target alt
|
|
|
|
// It changes based on climb rate
|
|
|
|
// until it reaches the target_altitude
|
2011-09-04 21:15:36 -03:00
|
|
|
return next_WP.alt - current_loc.alt;
|
2011-03-09 02:37:09 -04:00
|
|
|
}
|
|
|
|
|
2012-01-11 03:38:16 -04:00
|
|
|
static void clear_new_altitude()
|
|
|
|
{
|
|
|
|
alt_change_flag = REACHED_ALT;
|
|
|
|
}
|
|
|
|
|
2012-03-10 16:42:22 -04:00
|
|
|
static void force_new_altitude(int32_t _new_alt)
|
|
|
|
{
|
|
|
|
next_WP.alt = _new_alt;
|
|
|
|
target_altitude = _new_alt;
|
|
|
|
alt_change_flag = REACHED_ALT;
|
|
|
|
}
|
|
|
|
|
2012-01-11 03:38:16 -04:00
|
|
|
static void set_new_altitude(int32_t _new_alt)
|
|
|
|
{
|
2012-02-19 16:40:08 -04:00
|
|
|
if(_new_alt == current_loc.alt){
|
2012-03-10 16:42:22 -04:00
|
|
|
force_new_altitude(_new_alt);
|
2012-02-19 16:40:08 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-10 16:42:22 -04:00
|
|
|
// We start at the current location altitude and gradually change alt
|
2012-01-11 03:38:16 -04:00
|
|
|
next_WP.alt = current_loc.alt;
|
|
|
|
|
|
|
|
// for calculating the delta time
|
|
|
|
alt_change_timer = millis();
|
|
|
|
|
|
|
|
// save the target altitude
|
|
|
|
target_altitude = _new_alt;
|
|
|
|
|
|
|
|
// reset our altitude integrator
|
|
|
|
alt_change = 0;
|
|
|
|
|
|
|
|
// save the original altitude
|
|
|
|
original_altitude = current_loc.alt;
|
|
|
|
|
|
|
|
// to decide if we have reached the target altitude
|
|
|
|
if(target_altitude > original_altitude){
|
|
|
|
// we are below, going up
|
|
|
|
alt_change_flag = ASCENDING;
|
2012-01-20 14:11:18 -04:00
|
|
|
//Serial.printf("go up\n");
|
2012-01-11 03:38:16 -04:00
|
|
|
}else if(target_altitude < original_altitude){
|
|
|
|
// we are above, going down
|
|
|
|
alt_change_flag = DESCENDING;
|
2012-01-20 14:11:18 -04:00
|
|
|
//Serial.printf("go down\n");
|
2012-01-11 03:38:16 -04:00
|
|
|
}else{
|
|
|
|
// No Change
|
|
|
|
alt_change_flag = REACHED_ALT;
|
2012-01-20 14:11:18 -04:00
|
|
|
//Serial.printf("reached alt\n");
|
2012-01-11 03:38:16 -04:00
|
|
|
}
|
|
|
|
//Serial.printf("new alt: %d Org alt: %d\n", target_altitude, original_altitude);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t get_new_altitude()
|
|
|
|
{
|
|
|
|
// returns a new next_WP.alt
|
|
|
|
|
|
|
|
if(alt_change_flag == ASCENDING){
|
|
|
|
// we are below, going up
|
|
|
|
if(current_loc.alt >= target_altitude){
|
|
|
|
alt_change_flag = REACHED_ALT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we shouldn't command past our target
|
|
|
|
if(next_WP.alt >= target_altitude){
|
|
|
|
return target_altitude;
|
|
|
|
}
|
|
|
|
}else if (alt_change_flag == DESCENDING){
|
|
|
|
// we are above, going down
|
|
|
|
if(current_loc.alt <= target_altitude)
|
|
|
|
alt_change_flag = REACHED_ALT;
|
|
|
|
|
|
|
|
// we shouldn't command past our target
|
|
|
|
if(next_WP.alt <= target_altitude){
|
|
|
|
return target_altitude;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we have reached our target altitude, return the target alt
|
|
|
|
if(alt_change_flag == REACHED_ALT){
|
|
|
|
return target_altitude;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t diff = abs(next_WP.alt - target_altitude);
|
2012-03-10 16:43:28 -04:00
|
|
|
// scale is how we generate a desired rate from the elapsed time
|
|
|
|
// a smaller scale means faster rates
|
2012-01-29 02:00:05 -04:00
|
|
|
int8_t _scale = 4;
|
2012-01-11 03:38:16 -04:00
|
|
|
|
|
|
|
if (next_WP.alt < target_altitude){
|
|
|
|
// we are below the target alt
|
|
|
|
if(diff < 200){
|
|
|
|
_scale = 4;
|
2012-03-10 16:43:01 -04:00
|
|
|
} else {
|
|
|
|
_scale = 3;
|
2012-01-11 03:38:16 -04:00
|
|
|
}
|
|
|
|
}else {
|
2012-01-13 02:26:15 -04:00
|
|
|
// we are above the target, going down
|
2012-01-29 02:00:05 -04:00
|
|
|
if(diff < 400){
|
2012-01-11 03:38:16 -04:00
|
|
|
_scale = 5;
|
|
|
|
}
|
2012-01-29 02:00:05 -04:00
|
|
|
if(diff < 100){
|
|
|
|
_scale = 6;
|
|
|
|
}
|
2012-01-11 03:38:16 -04:00
|
|
|
}
|
|
|
|
|
2012-01-29 21:10:52 -04:00
|
|
|
// we use the elapsed time as our altitude offset
|
|
|
|
// 1000 = 1 sec
|
|
|
|
// 1000 >> 4 = 64cm/s descent by default
|
2012-01-11 03:38:16 -04:00
|
|
|
int32_t change = (millis() - alt_change_timer) >> _scale;
|
|
|
|
|
|
|
|
if(alt_change_flag == ASCENDING){
|
|
|
|
alt_change += change;
|
|
|
|
}else{
|
|
|
|
alt_change -= change;
|
|
|
|
}
|
|
|
|
// for generating delta time
|
|
|
|
alt_change_timer = millis();
|
|
|
|
|
|
|
|
return original_altitude + alt_change;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-07 18:24:32 -04:00
|
|
|
static int32_t wrap_360(int32_t error)
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
|
|
|
if (error > 36000) error -= 36000;
|
|
|
|
if (error < 0) error += 36000;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2011-11-07 18:24:32 -04:00
|
|
|
static int32_t wrap_180(int32_t error)
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
|
|
|
if (error > 18000) error -= 36000;
|
|
|
|
if (error < -18000) error += 36000;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2011-11-27 23:11:44 -04:00
|
|
|
/*
|
|
|
|
//static int32_t 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-10-15 17:09:04 -03:00
|
|
|
*/
|
2012-01-22 02:12:57 -04:00
|
|
|
|
2011-03-26 03:35:52 -03:00
|
|
|
// distance is returned in meters
|
2011-11-07 18:24:32 -04:00
|
|
|
static int32_t get_distance(struct Location *loc1, struct Location *loc2)
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
|
|
|
float dlat = (float)(loc2->lat - loc1->lat);
|
|
|
|
float dlong = ((float)(loc2->lng - loc1->lng)) * scaleLongDown;
|
2012-01-22 02:12:57 -04:00
|
|
|
dlong = sqrt(sq(dlat) + sq(dlong)) * 1.113195;
|
|
|
|
return dlong;
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
2011-10-15 17:09:04 -03:00
|
|
|
/*
|
2011-12-29 02:51:25 -04:00
|
|
|
//static int32_t get_alt_distance(struct Location *loc1, struct Location *loc2)
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
|
|
|
return abs(loc1->alt - loc2->alt);
|
|
|
|
}
|
2011-10-15 17:09:04 -03:00
|
|
|
*/
|
2011-11-07 18:24:32 -04:00
|
|
|
static int32_t get_bearing(struct Location *loc1, struct Location *loc2)
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2011-11-07 18:24:32 -04:00
|
|
|
int32_t off_x = loc2->lng - loc1->lng;
|
|
|
|
int32_t off_y = (loc2->lat - loc1->lat) * scaleLongUp;
|
|
|
|
int32_t bearing = 9000 + atan2(-off_y, off_x) * 5729.57795;
|
2010-12-19 12:40:33 -04:00
|
|
|
if (bearing < 0) bearing += 36000;
|
|
|
|
return bearing;
|
|
|
|
}
|