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
|
|
|
|
//****************************************************************
|
2012-05-17 14:57:00 -03:00
|
|
|
static void navigate()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2012-03-20 04:13:38 -03:00
|
|
|
// waypoint distance from plane in cm
|
2012-01-06 14:20:59 -04:00
|
|
|
// ---------------------------------------
|
2012-08-14 18:08:33 -03:00
|
|
|
wp_distance = get_distance_cm(&filtered_loc, &next_WP);
|
|
|
|
home_distance = get_distance_cm(&filtered_loc, &home);
|
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-08-16 19:38:25 -03:00
|
|
|
target_bearing = get_bearing_cd(&filtered_loc, &next_WP);
|
2012-08-07 03:04:02 -03:00
|
|
|
home_to_copter_bearing = get_bearing_cd(&home, ¤t_loc);
|
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);
|
2012-08-16 05:51:09 -03:00
|
|
|
return (labs(temp) > 9000); // we passed the waypoint by 100 degrees
|
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(){
|
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-06-26 02:18:57 -03:00
|
|
|
// called after GPS read
|
|
|
|
// offset calculation of GPS speed:
|
|
|
|
// used for estimations below 1.5m/s
|
2011-12-29 02:51:25 -04:00
|
|
|
// y_GPS_speed positve = Up
|
|
|
|
// x_GPS_speed positve = Right
|
|
|
|
|
2012-03-18 00:08:45 -03:00
|
|
|
// initialise last_longitude and last_latitude
|
|
|
|
if( last_longitude == 0 && last_latitude == 0 ) {
|
|
|
|
last_longitude = g_gps->longitude;
|
|
|
|
last_latitude = g_gps->latitude;
|
|
|
|
}
|
|
|
|
|
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-03-16 18:09:59 -03:00
|
|
|
x_actual_speed = (float)(g_gps->longitude - last_longitude) * scaleLongDown * tmp;
|
|
|
|
y_actual_speed = (float)(g_gps->latitude - last_latitude) * tmp;
|
|
|
|
|
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
|
|
|
|
2012-07-03 21:16:14 -03:00
|
|
|
/*if(g_gps->ground_speed > 150){
|
2012-06-26 02:18:57 -03:00
|
|
|
float temp = radians((float)g_gps->ground_course/100.0);
|
|
|
|
x_actual_speed = (float)g_gps->ground_speed * sin(temp);
|
|
|
|
y_actual_speed = (float)g_gps->ground_speed * cos(temp);
|
2012-07-03 21:16:14 -03:00
|
|
|
}*/
|
|
|
|
|
2012-06-26 02:18:57 -03:00
|
|
|
|
2012-06-14 02:27:03 -03:00
|
|
|
#if INERTIAL_NAV == ENABLED
|
|
|
|
// inertial_nav
|
|
|
|
xy_error_correction();
|
2012-08-14 18:08:33 -03:00
|
|
|
filtered_loc.lng = xLeadFilter.get_position(g_gps->longitude, accels_velocity.x);
|
|
|
|
filtered_loc.lat = yLeadFilter.get_position(g_gps->latitude, accels_velocity.y);
|
2012-07-18 15:17:12 -03:00
|
|
|
#else
|
2012-08-14 18:08:33 -03:00
|
|
|
filtered_loc.lng = xLeadFilter.get_position(g_gps->longitude, x_actual_speed);
|
|
|
|
filtered_loc.lat = yLeadFilter.get_position(g_gps->latitude, y_actual_speed);
|
2012-07-18 15:17:12 -03:00
|
|
|
#endif
|
2012-04-19 01:06:15 -03: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
|
|
|
{
|
|
|
|
/*
|
|
|
|
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
|
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-04-19 01:06:15 -03:00
|
|
|
#define NAV_RATE_ERR_MAX 250
|
2012-08-16 08:04:46 -03:00
|
|
|
static void calc_loiter(int16_t x_error, int16_t y_error)
|
2011-05-16 01:59:06 -03:00
|
|
|
{
|
2012-04-11 11:52:25 -03:00
|
|
|
int32_t p,i,d; // used to capture pid values for logging
|
|
|
|
int32_t output;
|
|
|
|
int32_t x_target_speed, y_target_speed;
|
2012-02-24 02:01:44 -04:00
|
|
|
|
|
|
|
// East / West
|
2012-04-11 11:52:25 -03:00
|
|
|
x_target_speed = g.pi_loiter_lon.get_p(x_error); // calculate desired speed from lon error
|
|
|
|
|
|
|
|
#if LOGGING_ENABLED == ENABLED
|
|
|
|
// log output if PID logging is on and we are tuning the yaw
|
|
|
|
if( g.log_bitmask & MASK_LOG_PID && (g.radio_tuning == CH6_LOITER_KP || g.radio_tuning == CH6_LOITER_KI) ) {
|
|
|
|
Log_Write_PID(CH6_LOITER_KP, x_error, x_target_speed, 0, 0, x_target_speed, tuning_value);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-06-14 02:27:03 -03:00
|
|
|
|
|
|
|
// calculate rate error
|
|
|
|
#if INERTIAL_NAV == ENABLED
|
|
|
|
x_rate_error = x_target_speed - accels_velocity.x; // calc the speed error
|
|
|
|
#else
|
2012-04-19 12:16:29 -03:00
|
|
|
x_rate_error = x_target_speed - x_actual_speed; // calc the speed error
|
2012-06-14 02:27:03 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2012-04-11 11:52:25 -03:00
|
|
|
p = g.pid_loiter_rate_lon.get_p(x_rate_error);
|
2012-05-16 02:14:23 -03:00
|
|
|
i = g.pid_loiter_rate_lon.get_i(x_rate_error + x_error, dTnav);
|
|
|
|
d = g.pid_loiter_rate_lon.get_d(x_error, dTnav);
|
|
|
|
d = constrain(d, -2000, 2000);
|
2012-04-11 11:52:25 -03:00
|
|
|
|
2012-05-16 02:14:23 -03:00
|
|
|
// get rid of noise
|
|
|
|
if(abs(x_actual_speed) < 50){
|
|
|
|
d = 0;
|
|
|
|
}
|
2012-04-11 11:52:25 -03:00
|
|
|
|
|
|
|
output = p + i + d;
|
2012-07-19 02:49:34 -03:00
|
|
|
nav_lon = constrain(output, -3200, 3200);
|
2012-04-11 11:52:25 -03:00
|
|
|
|
|
|
|
#if LOGGING_ENABLED == ENABLED
|
|
|
|
// log output if PID logging is on and we are tuning the yaw
|
|
|
|
if( g.log_bitmask & MASK_LOG_PID && (g.radio_tuning == CH6_LOITER_RATE_KP || g.radio_tuning == CH6_LOITER_RATE_KI || g.radio_tuning == CH6_LOITER_RATE_KD) ) {
|
|
|
|
Log_Write_PID(CH6_LOITER_RATE_KP, x_rate_error, p, i, d, nav_lon, tuning_value);
|
|
|
|
}
|
|
|
|
#endif
|
2012-02-24 02:01:44 -04:00
|
|
|
|
|
|
|
// North / South
|
2012-04-11 11:52:25 -03:00
|
|
|
y_target_speed = g.pi_loiter_lat.get_p(y_error); // calculate desired speed from lat error
|
|
|
|
|
|
|
|
#if LOGGING_ENABLED == ENABLED
|
|
|
|
// log output if PID logging is on and we are tuning the yaw
|
|
|
|
if( g.log_bitmask & MASK_LOG_PID && (g.radio_tuning == CH6_LOITER_KP || g.radio_tuning == CH6_LOITER_KI) ) {
|
|
|
|
Log_Write_PID(CH6_LOITER_KP+100, y_error, y_target_speed, 0, 0, y_target_speed, tuning_value);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-06-14 02:27:03 -03:00
|
|
|
// calculate rate error
|
|
|
|
#if INERTIAL_NAV == ENABLED
|
|
|
|
y_rate_error = y_target_speed - accels_velocity.y; // calc the speed error
|
|
|
|
#else
|
|
|
|
y_rate_error = y_target_speed - y_actual_speed; // calc the speed error
|
|
|
|
#endif
|
|
|
|
|
2012-04-11 11:52:25 -03:00
|
|
|
p = g.pid_loiter_rate_lat.get_p(y_rate_error);
|
2012-05-16 02:14:23 -03:00
|
|
|
i = g.pid_loiter_rate_lat.get_i(y_rate_error + y_error, dTnav);
|
|
|
|
d = g.pid_loiter_rate_lat.get_d(y_error, dTnav);
|
|
|
|
d = constrain(d, -2000, 2000);
|
2012-04-11 11:52:25 -03:00
|
|
|
|
2012-05-16 02:14:23 -03:00
|
|
|
// get rid of noise
|
|
|
|
if(abs(y_actual_speed) < 50){
|
|
|
|
d = 0;
|
|
|
|
}
|
2012-04-11 11:52:25 -03:00
|
|
|
|
|
|
|
output = p + i + d;
|
2012-07-19 02:49:34 -03:00
|
|
|
nav_lat = constrain(output, -3200, 3200);
|
2012-04-11 11:52:25 -03:00
|
|
|
|
|
|
|
#if LOGGING_ENABLED == ENABLED
|
|
|
|
// log output if PID logging is on and we are tuning the yaw
|
|
|
|
if( g.log_bitmask & MASK_LOG_PID && (g.radio_tuning == CH6_LOITER_RATE_KP || g.radio_tuning == CH6_LOITER_RATE_KI || g.radio_tuning == CH6_LOITER_RATE_KD) ) {
|
|
|
|
Log_Write_PID(CH6_LOITER_RATE_KP+100, y_rate_error, p, i, d, nav_lat, tuning_value);
|
|
|
|
}
|
|
|
|
#endif
|
2012-02-24 02:01:44 -04:00
|
|
|
|
|
|
|
// 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());
|
2011-03-14 03:04:07 -03:00
|
|
|
}
|
|
|
|
|
2012-07-09 17:10:12 -03:00
|
|
|
static void calc_nav_rate(int16_t max_speed)
|
2012-01-29 02:00:05 -04:00
|
|
|
{
|
2012-06-26 02:18:57 -03:00
|
|
|
float temp, temp_x, temp_y;
|
|
|
|
|
2012-01-29 02:00:05 -04:00
|
|
|
// push us towards the original track
|
|
|
|
update_crosstrack();
|
|
|
|
|
2012-06-26 02:18:57 -03:00
|
|
|
int16_t cross_speed = crosstrack_error * -g.crosstrack_gain; // scale down crosstrack_error in cm
|
2012-08-16 19:38:25 -03:00
|
|
|
cross_speed = constrain(cross_speed, -150, 150);
|
2012-06-26 02:18:57 -03:00
|
|
|
|
|
|
|
// rotate by 90 to deal with trig functions
|
|
|
|
temp = (9000l - target_bearing) * RADX100;
|
|
|
|
temp_x = cos(temp);
|
|
|
|
temp_y = sin(temp);
|
|
|
|
|
|
|
|
// rotate desired spped vector:
|
2012-07-09 17:10:12 -03:00
|
|
|
int32_t x_target_speed = max_speed * temp_x - cross_speed * temp_y;
|
|
|
|
int32_t y_target_speed = cross_speed * temp_x + max_speed * temp_y;
|
2012-02-24 02:01:44 -04:00
|
|
|
|
|
|
|
// East / West
|
2012-07-18 15:17:12 -03:00
|
|
|
// calculate rate error
|
|
|
|
#if INERTIAL_NAV == ENABLED
|
2012-07-19 02:49:34 -03:00
|
|
|
x_rate_error = x_target_speed - accels_velocity.x;
|
2012-07-18 15:17:12 -03:00
|
|
|
#else
|
2012-07-19 02:49:34 -03:00
|
|
|
x_rate_error = x_target_speed - x_actual_speed;
|
2012-07-18 15:17:12 -03:00
|
|
|
#endif
|
|
|
|
|
2012-08-18 04:02:30 -03:00
|
|
|
x_rate_error = constrain(x_rate_error, -500, 500);
|
2012-02-24 02:01:44 -04:00
|
|
|
nav_lon = g.pid_nav_lon.get_pid(x_rate_error, dTnav);
|
2012-07-09 17:10:12 -03:00
|
|
|
int32_t tilt = (x_target_speed * x_target_speed * (int32_t)g.tilt_comp) / 10000;
|
2012-06-29 18:39:36 -03:00
|
|
|
|
2012-07-09 17:10:12 -03:00
|
|
|
if(x_target_speed < 0) tilt = -tilt;
|
|
|
|
nav_lon += tilt;
|
2012-07-19 02:49:34 -03:00
|
|
|
nav_lon = constrain(nav_lon, -3200, 3200);
|
2012-02-24 02:01:44 -04:00
|
|
|
|
2012-06-29 18:39:36 -03:00
|
|
|
|
2012-02-24 02:01:44 -04:00
|
|
|
// North / South
|
2012-07-18 15:17:12 -03:00
|
|
|
// calculate rate error
|
|
|
|
#if INERTIAL_NAV == ENABLED
|
2012-07-19 02:49:34 -03:00
|
|
|
y_rate_error = y_target_speed - accels_velocity.y;
|
2012-07-18 15:17:12 -03:00
|
|
|
#else
|
2012-07-19 02:49:34 -03:00
|
|
|
y_rate_error = y_target_speed - y_actual_speed;
|
2012-07-18 15:17:12 -03:00
|
|
|
#endif
|
|
|
|
|
2012-08-18 04:02:30 -03:00
|
|
|
y_rate_error = constrain(y_rate_error, -500, 500); // added a rate error limit to keep pitching down to a minimum
|
2012-02-24 02:01:44 -04:00
|
|
|
nav_lat = g.pid_nav_lat.get_pid(y_rate_error, dTnav);
|
2012-07-09 17:10:12 -03:00
|
|
|
tilt = (y_target_speed * y_target_speed * (int32_t)g.tilt_comp) / 10000;
|
2012-06-29 18:39:36 -03:00
|
|
|
|
2012-07-09 17:10:12 -03:00
|
|
|
if(y_target_speed < 0) tilt = -tilt;
|
|
|
|
nav_lat += tilt;
|
2012-07-19 02:49:34 -03:00
|
|
|
nav_lat = constrain(nav_lat, -3200, 3200);
|
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-06-26 02:18:57 -03:00
|
|
|
|
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-07-19 02:49:34 -03:00
|
|
|
static int16_t get_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-08-09 20:53:05 -03:00
|
|
|
if(fast_corner){
|
2012-08-10 14:02:32 -03:00
|
|
|
waypoint_radius = g.waypoint_radius * 2;
|
2012-08-09 20:53:05 -03:00
|
|
|
//max_speed = max_speed;
|
2012-02-19 17:13:21 -04:00
|
|
|
}else{
|
2012-08-10 14:02:32 -03:00
|
|
|
waypoint_radius = g.waypoint_radius;
|
2012-08-09 20:53:05 -03:00
|
|
|
max_speed = min(max_speed, (wp_distance - g.waypoint_radius) / 3);
|
2012-02-19 17:13:21 -04:00
|
|
|
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-07-10 21:16:57 -03: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;
|
|
|
|
}
|
|
|
|
|
2012-07-19 14:09:16 -03:00
|
|
|
static int16_t get_desired_climb_rate()
|
2012-07-19 02:49:34 -03:00
|
|
|
{
|
|
|
|
if(alt_change_flag == ASCENDING){
|
2012-07-19 21:53:49 -03:00
|
|
|
return constrain(altitude_error / 4, 100, 180); // 180cm /s up, minimum is 100cm/s
|
2012-07-19 02:49:34 -03:00
|
|
|
|
|
|
|
}else if(alt_change_flag == DESCENDING){
|
2012-07-19 21:53:49 -03:00
|
|
|
return constrain(altitude_error / 6, -100, -10); // -100cm /s down, max is -10cms
|
2012-07-19 02:49:34 -03:00
|
|
|
|
|
|
|
}else{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
// ----------------
|
2012-06-26 02:18:57 -03:00
|
|
|
// If we are too far off or too close we don't do track following
|
|
|
|
float temp = (target_bearing - original_target_bearing) * RADX100;
|
|
|
|
crosstrack_error = sin(temp) * wp_distance; // Meters we are off track line
|
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
|
2012-08-09 20:53:05 -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-08-18 09:46:48 -03:00
|
|
|
static void force_new_altitude(int32_t new_alt)
|
2012-03-10 16:42:22 -04:00
|
|
|
{
|
2012-08-18 09:46:48 -03:00
|
|
|
next_WP.alt = new_alt;
|
2012-03-10 16:42:22 -04:00
|
|
|
alt_change_flag = REACHED_ALT;
|
|
|
|
}
|
|
|
|
|
2012-08-18 09:46:48 -03:00
|
|
|
static void set_new_altitude(int32_t new_alt)
|
2012-01-11 03:38:16 -04:00
|
|
|
{
|
2012-08-18 09:46:48 -03:00
|
|
|
next_WP.alt = new_alt;
|
2012-01-11 03:38:16 -04:00
|
|
|
|
2012-08-09 20:53:05 -03:00
|
|
|
if(next_WP.alt > current_loc.alt + 20){
|
2012-01-11 03:38:16 -04:00
|
|
|
// we are below, going up
|
|
|
|
alt_change_flag = ASCENDING;
|
2012-08-09 20:53:05 -03:00
|
|
|
|
|
|
|
}else if(next_WP.alt < current_loc.alt - 20){
|
2012-01-11 03:38:16 -04:00
|
|
|
// we are above, going down
|
|
|
|
alt_change_flag = DESCENDING;
|
2012-08-09 20:53:05 -03:00
|
|
|
|
2012-01-11 03:38:16 -04:00
|
|
|
}else{
|
|
|
|
// No Change
|
|
|
|
alt_change_flag = REACHED_ALT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-09 20:53:05 -03:00
|
|
|
static void verify_altitude()
|
2012-01-11 03:38:16 -04:00
|
|
|
{
|
|
|
|
if(alt_change_flag == ASCENDING){
|
|
|
|
// we are below, going up
|
2012-08-09 20:53:05 -03:00
|
|
|
if(current_loc.alt > next_WP.alt - 50){
|
2012-01-11 03:38:16 -04:00
|
|
|
alt_change_flag = REACHED_ALT;
|
|
|
|
}
|
|
|
|
}else if (alt_change_flag == DESCENDING){
|
|
|
|
// we are above, going down
|
2012-08-09 20:53:05 -03:00
|
|
|
if(current_loc.alt <= next_WP.alt + 50)
|
2012-01-11 03:38:16 -04:00
|
|
|
alt_change_flag = REACHED_ALT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|