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
|
|
|
|
2012-11-07 06:03:30 -04:00
|
|
|
// update_navigation - checks for new GPS updates and invokes navigation routines
|
|
|
|
static void update_navigation()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2012-11-07 06:03:30 -04:00
|
|
|
static uint32_t nav_last_gps_update = 0; // the system time of the last gps update
|
|
|
|
static uint32_t nav_last_gps_time = 0; // the time according to the gps
|
|
|
|
bool pos_updated = false;
|
|
|
|
bool log_output = false;
|
2012-08-21 23:19:50 -03:00
|
|
|
|
2012-11-07 06:03:30 -04:00
|
|
|
// check for new gps data
|
|
|
|
if( g_gps->fix && g_gps->time != nav_last_gps_time ) {
|
2011-02-24 01:56:59 -04:00
|
|
|
|
2012-11-07 06:03:30 -04:00
|
|
|
// used to calculate speed in X and Y, iterms
|
|
|
|
// ------------------------------------------
|
|
|
|
dTnav = (float)(millis() - nav_last_gps_update)/ 1000.0;
|
|
|
|
nav_last_gps_update = millis();
|
|
|
|
|
|
|
|
// prevent runup from bad GPS
|
|
|
|
dTnav = min(dTnav, 1.0);
|
|
|
|
|
|
|
|
// save GPS time
|
|
|
|
nav_last_gps_time = g_gps->time;
|
|
|
|
|
|
|
|
// signal to run nav controllers
|
|
|
|
pos_updated = true;
|
|
|
|
|
|
|
|
// signal to create log entry
|
|
|
|
log_output = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if INERTIAL_NAV == ENABLED
|
|
|
|
// TO-DO: clean this up because inertial nav is overwriting the dTnav and pos_updated from above
|
|
|
|
// check for inertial nav updates
|
|
|
|
if( inertial_nav.position_ok() ) {
|
|
|
|
// 50hz
|
|
|
|
dTnav = 0.02; // To-Do: calculate the time from the mainloop or INS readings?
|
|
|
|
|
|
|
|
// signal to run nav controllers
|
|
|
|
pos_updated = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// calc various navigation values and run controllers if we've received a position update
|
|
|
|
if( pos_updated ) {
|
|
|
|
|
|
|
|
// calculate velocity
|
|
|
|
calc_velocity_and_position();
|
|
|
|
|
|
|
|
// calculate distance, angles to target
|
|
|
|
calc_distance_and_bearing();
|
|
|
|
|
|
|
|
// run navigation controllers
|
|
|
|
run_navigation_contollers();
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2012-11-07 06:03:30 -04:00
|
|
|
// Rotate the nav_lon and nav_lat vectors based on Yaw
|
|
|
|
calc_nav_pitch_roll();
|
2012-08-21 23:19:50 -03:00
|
|
|
|
2012-11-07 06:03:30 -04:00
|
|
|
// update log
|
|
|
|
if (log_output && (g.log_bitmask & MASK_LOG_NTUN) && motors.armed()) {
|
|
|
|
Log_Write_Nav_Tuning();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reduce nav outputs to zero if we have not received a gps update in 2 seconds
|
|
|
|
if( millis() - nav_last_gps_update > 2000 ) {
|
|
|
|
// after 12 reads we guess we may have lost GPS signal, stop navigating
|
|
|
|
// we have lost GPS signal for a moment. Reduce our error to avoid flyaways
|
|
|
|
auto_roll >>= 1;
|
|
|
|
auto_pitch >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//*******************************************************************************************************
|
|
|
|
// calc_velocity_and_filtered_position - velocity in lon and lat directions calculated from GPS position
|
|
|
|
// and accelerometer data
|
|
|
|
// lon_speed expressed in cm/s. positive numbers mean moving east
|
|
|
|
// lat_speed expressed in cm/s. positive numbers when moving north
|
|
|
|
// Note: we use gps locations directly to calculate velocity instead of asking gps for velocity because
|
|
|
|
// this is more accurate below 1.5m/s
|
|
|
|
// Note: even though the positions are projected using a lead filter, the velocities are calculated
|
|
|
|
// from the unaltered gps locations. We do not want noise from our lead filter affecting velocity
|
|
|
|
//*******************************************************************************************************
|
|
|
|
static void calc_velocity_and_position(){
|
|
|
|
static int32_t last_gps_longitude = 0;
|
|
|
|
static int32_t last_gps_latitude = 0;
|
2012-08-21 23:19:50 -03:00
|
|
|
|
|
|
|
// initialise last_longitude and last_latitude
|
2012-11-07 06:03:30 -04:00
|
|
|
if( last_gps_longitude == 0 && last_gps_latitude == 0 ) {
|
|
|
|
last_gps_longitude = g_gps->longitude;
|
|
|
|
last_gps_latitude = g_gps->latitude;
|
2012-08-21 23:19:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// this speed is ~ in cm because we are using 10^7 numbers from GPS
|
|
|
|
float tmp = 1.0/dTnav;
|
|
|
|
|
|
|
|
#if INERTIAL_NAV == ENABLED
|
2012-11-07 06:03:30 -04:00
|
|
|
if( inertial_nav.position_ok() ) {
|
|
|
|
// pull velocity from interial nav library
|
|
|
|
lon_speed = inertial_nav.get_longitude_velocity();
|
|
|
|
lat_speed = inertial_nav.get_latitude_velocity();
|
|
|
|
|
|
|
|
// pull position from interial nav library
|
|
|
|
current_loc.lng = inertial_nav.get_longitude();
|
|
|
|
current_loc.lat = inertial_nav.get_latitude();
|
|
|
|
}else{
|
|
|
|
// calculate velocity
|
|
|
|
lon_speed = (float)(g_gps->longitude - last_gps_longitude) * scaleLongDown * tmp;
|
|
|
|
lat_speed = (float)(g_gps->latitude - last_gps_latitude) * tmp;
|
|
|
|
|
|
|
|
// calculate position from gps + expected travel during gps_lag
|
|
|
|
current_loc.lng = xLeadFilter.get_position(g_gps->longitude, lon_speed, g_gps->get_lag());
|
|
|
|
current_loc.lat = yLeadFilter.get_position(g_gps->latitude, lat_speed, g_gps->get_lag());
|
|
|
|
}
|
2012-08-21 23:19:50 -03:00
|
|
|
#else
|
2012-11-07 06:03:30 -04:00
|
|
|
// calculate velocity
|
|
|
|
lon_speed = (float)(g_gps->longitude - last_gps_longitude) * scaleLongDown * tmp;
|
|
|
|
lat_speed = (float)(g_gps->latitude - last_gps_latitude) * tmp;
|
|
|
|
|
|
|
|
// calculate position from gps + expected travel during gps_lag
|
|
|
|
current_loc.lng = xLeadFilter.get_position(g_gps->longitude, lon_speed, g_gps->get_lag());
|
|
|
|
current_loc.lat = yLeadFilter.get_position(g_gps->latitude, lat_speed, g_gps->get_lag());
|
2012-08-21 23:19:50 -03:00
|
|
|
#endif
|
2012-11-07 06:03:30 -04:00
|
|
|
|
|
|
|
// store gps lat and lon values for next iteration
|
|
|
|
last_gps_longitude = g_gps->longitude;
|
|
|
|
last_gps_latitude = g_gps->latitude;
|
|
|
|
}
|
|
|
|
|
|
|
|
//****************************************************************
|
|
|
|
// Function that will calculate the desired direction to fly and distance
|
|
|
|
//****************************************************************
|
|
|
|
static void calc_distance_and_bearing()
|
|
|
|
{
|
|
|
|
// waypoint distance from plane in cm
|
|
|
|
// ---------------------------------------
|
|
|
|
wp_distance = get_distance_cm(¤t_loc, &next_WP);
|
|
|
|
home_distance = get_distance_cm(¤t_loc, &home);
|
|
|
|
|
|
|
|
// target_bearing is where we should be heading
|
|
|
|
// --------------------------------------------
|
|
|
|
target_bearing = get_bearing_cd(¤t_loc, &next_WP);
|
|
|
|
home_to_copter_bearing = get_bearing_cd(&home, ¤t_loc);
|
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
|
|
|
{
|
2012-08-21 23:19:50 -03:00
|
|
|
/*
|
|
|
|
* Becuase we are using lat and lon to do our distance errors here's a quick chart:
|
|
|
|
* 100 = 1m
|
|
|
|
* 1000 = 11m = 36 feet
|
|
|
|
* 1800 = 19.80m = 60 feet
|
|
|
|
* 3000 = 33m
|
|
|
|
* 10000 = 111m
|
|
|
|
*/
|
|
|
|
|
|
|
|
// X Error
|
|
|
|
long_error = (float)(next_loc->lng - current_loc.lng) * scaleLongDown; // 500 - 0 = 500 Go East
|
|
|
|
|
|
|
|
// 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-11-07 06:03:30 -04:00
|
|
|
// called after a GPS read
|
|
|
|
static void run_navigation_contollers()
|
|
|
|
{
|
|
|
|
// wp_distance is in CM
|
|
|
|
// --------------------
|
|
|
|
switch(control_mode) {
|
|
|
|
case AUTO:
|
|
|
|
// note: wp_control is handled by commands_logic
|
|
|
|
verify_commands();
|
|
|
|
|
|
|
|
// calculates desired Yaw
|
|
|
|
update_auto_yaw();
|
|
|
|
|
|
|
|
// calculates the desired Roll and Pitch
|
|
|
|
update_nav_wp();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GUIDED:
|
|
|
|
wp_control = WP_MODE;
|
|
|
|
// check if we are close to point > loiter
|
|
|
|
wp_verify_byte = 0;
|
|
|
|
verify_nav_wp();
|
|
|
|
|
|
|
|
if (wp_control == WP_MODE) {
|
|
|
|
update_auto_yaw();
|
|
|
|
} else {
|
|
|
|
set_mode(LOITER);
|
|
|
|
}
|
|
|
|
update_nav_wp();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RTL:
|
|
|
|
// have we reached the desired Altitude?
|
|
|
|
if(alt_change_flag <= REACHED_ALT) { // we are at or above the target alt
|
|
|
|
if(rtl_reached_alt == false) {
|
|
|
|
rtl_reached_alt = true;
|
|
|
|
do_RTL();
|
|
|
|
}
|
|
|
|
wp_control = WP_MODE;
|
|
|
|
// checks if we have made it to home
|
|
|
|
update_nav_RTL();
|
|
|
|
} else{
|
|
|
|
// we need to loiter until we are ready to come home
|
|
|
|
wp_control = LOITER_MODE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculates desired Yaw
|
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
|
|
|
update_auto_yaw();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// calculates the desired Roll and Pitch
|
|
|
|
update_nav_wp();
|
|
|
|
break;
|
|
|
|
|
|
|
|
// switch passthrough to LOITER
|
|
|
|
case LOITER:
|
|
|
|
case POSITION:
|
|
|
|
// This feature allows us to reposition the quad when the user lets
|
|
|
|
// go of the sticks
|
|
|
|
|
|
|
|
if((abs(g.rc_2.control_in) + abs(g.rc_1.control_in)) > 500) {
|
|
|
|
if(wp_distance > 500)
|
|
|
|
loiter_override = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow the user to take control temporarily,
|
|
|
|
if(loiter_override) {
|
|
|
|
// this sets the copter to not try and nav while we control it
|
|
|
|
wp_control = NO_NAV_MODE;
|
|
|
|
|
|
|
|
// reset LOITER to current position
|
|
|
|
next_WP.lat = current_loc.lat;
|
|
|
|
next_WP.lng = current_loc.lng;
|
|
|
|
|
|
|
|
if(g.rc_2.control_in == 0 && g.rc_1.control_in == 0) {
|
|
|
|
loiter_override = false;
|
|
|
|
wp_control = LOITER_MODE;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
wp_control = LOITER_MODE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(loiter_timer != 0) {
|
|
|
|
// If we have a safe approach alt set and we have been loitering for 20 seconds(default), begin approach
|
|
|
|
if((millis() - loiter_timer) > (uint32_t)g.auto_land_timeout.get()) {
|
|
|
|
// just to make sure we clear the timer
|
|
|
|
loiter_timer = 0;
|
|
|
|
if(g.rtl_approach_alt == 0) {
|
|
|
|
set_mode(LAND);
|
|
|
|
if(home_distance < 300) {
|
|
|
|
next_WP.lat = home.lat;
|
|
|
|
next_WP.lng = home.lng;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if(g.rtl_approach_alt < current_loc.alt) {
|
|
|
|
set_new_altitude(g.rtl_approach_alt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculates the desired Roll and Pitch
|
|
|
|
update_nav_wp();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LAND:
|
|
|
|
if(g.sonar_enabled)
|
|
|
|
verify_land_sonar();
|
|
|
|
else
|
|
|
|
verify_land_baro();
|
|
|
|
|
|
|
|
// calculates the desired Roll and Pitch
|
|
|
|
update_nav_wp();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CIRCLE:
|
|
|
|
wp_control = CIRCLE_MODE;
|
|
|
|
|
|
|
|
// calculates desired Yaw
|
|
|
|
update_auto_yaw();
|
|
|
|
update_nav_wp();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STABILIZE:
|
|
|
|
case TOY_A:
|
|
|
|
case TOY_M:
|
|
|
|
wp_control = NO_NAV_MODE;
|
|
|
|
update_nav_wp();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// are we in SIMPLE mode?
|
|
|
|
if(do_simple && g.super_simple) {
|
|
|
|
// get distance to home
|
|
|
|
if(home_distance > SUPER_SIMPLE_RADIUS) { // 10m from home
|
|
|
|
// we reset the angular offset to be a vector from home to the quad
|
|
|
|
initial_simple_bearing = home_to_copter_bearing;
|
|
|
|
//Serial.printf("ISB: %d\n", initial_simple_bearing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(yaw_mode == YAW_LOOK_AT_HOME) {
|
|
|
|
if(home_is_set) {
|
|
|
|
nav_yaw = get_bearing_cd(¤t_loc, &home);
|
|
|
|
} else {
|
|
|
|
nav_yaw = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_nav_RTL()
|
|
|
|
{
|
|
|
|
// Have we have reached Home?
|
|
|
|
if(wp_distance <= 200 || check_missed_wp()) {
|
|
|
|
// if loiter_timer value > 0, we are set to trigger auto_land or approach
|
|
|
|
set_mode(LOITER);
|
|
|
|
|
|
|
|
// just in case we arrive and we aren't at the lower RTL alt yet.
|
|
|
|
set_new_altitude(get_RTL_alt());
|
|
|
|
|
|
|
|
// force loitering above home
|
|
|
|
next_WP.lat = home.lat;
|
|
|
|
next_WP.lng = home.lng;
|
|
|
|
|
|
|
|
// If failsafe OR auto approach altitude is set
|
|
|
|
// we will go into automatic land, (g.rtl_approach_alt) is the lowest point
|
|
|
|
// -1 means disable feature
|
|
|
|
if(failsafe || g.rtl_approach_alt >= 0)
|
|
|
|
loiter_timer = millis();
|
|
|
|
else
|
|
|
|
loiter_timer = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool check_missed_wp()
|
|
|
|
{
|
|
|
|
int32_t temp;
|
|
|
|
temp = target_bearing - original_target_bearing;
|
|
|
|
temp = wrap_180(temp);
|
|
|
|
return (labs(temp) > 9000); // we passed the waypoint by 100 degrees
|
|
|
|
}
|
|
|
|
|
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-08-21 23:19:50 -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
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
// East / West
|
|
|
|
x_target_speed = g.pi_loiter_lon.get_p(x_error); // calculate desired speed from lon error
|
2012-04-11 11:52:25 -03:00
|
|
|
|
|
|
|
#if LOGGING_ENABLED == ENABLED
|
2012-08-21 23:19:50 -03:00
|
|
|
// 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);
|
|
|
|
}
|
2012-04-11 11:52:25 -03:00
|
|
|
#endif
|
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
// calculate rate error
|
2012-11-07 06:03:30 -04:00
|
|
|
x_rate_error = x_target_speed - lon_speed; // calc the speed error
|
2012-06-14 02:27:03 -03:00
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
p = g.pid_loiter_rate_lon.get_p(x_rate_error);
|
|
|
|
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-08-21 23:19:50 -03:00
|
|
|
// get rid of noise
|
2012-11-07 06:03:30 -04:00
|
|
|
if(abs(lon_speed) < 50) {
|
2012-08-21 23:19:50 -03:00
|
|
|
d = 0;
|
|
|
|
}
|
2012-04-11 11:52:25 -03:00
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
output = p + i + d;
|
2012-10-24 10:04:35 -03:00
|
|
|
nav_lon = constrain(output, -32000, 32000); // constraint to remove chance of overflow when adding int32_t to int16_t
|
2012-04-11 11:52:25 -03:00
|
|
|
|
|
|
|
#if LOGGING_ENABLED == ENABLED
|
2012-08-21 23:19:50 -03:00
|
|
|
// 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);
|
|
|
|
}
|
2012-04-11 11:52:25 -03:00
|
|
|
#endif
|
2012-02-24 02:01:44 -04:00
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
// North / South
|
|
|
|
y_target_speed = g.pi_loiter_lat.get_p(y_error); // calculate desired speed from lat error
|
2012-04-11 11:52:25 -03:00
|
|
|
|
|
|
|
#if LOGGING_ENABLED == ENABLED
|
2012-08-21 23:19:50 -03:00
|
|
|
// 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);
|
|
|
|
}
|
2012-04-11 11:52:25 -03:00
|
|
|
#endif
|
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
// calculate rate error
|
2012-11-07 06:03:30 -04:00
|
|
|
y_rate_error = y_target_speed - lat_speed; // calc the speed error
|
2012-06-14 02:27:03 -03:00
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
p = g.pid_loiter_rate_lat.get_p(y_rate_error);
|
|
|
|
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-08-21 23:19:50 -03:00
|
|
|
// get rid of noise
|
2012-11-07 06:03:30 -04:00
|
|
|
if(abs(lat_speed) < 50) {
|
2012-08-21 23:19:50 -03:00
|
|
|
d = 0;
|
|
|
|
}
|
2012-04-11 11:52:25 -03:00
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
output = p + i + d;
|
2012-10-24 10:04:35 -03:00
|
|
|
nav_lat = constrain(output, -32000, 32000); // constraint to remove chance of overflow when adding int32_t to int16_t
|
2012-04-11 11:52:25 -03:00
|
|
|
|
|
|
|
#if LOGGING_ENABLED == ENABLED
|
2012-08-21 23:19:50 -03:00
|
|
|
// 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);
|
|
|
|
}
|
2012-04-11 11:52:25 -03:00
|
|
|
#endif
|
2012-02-24 02:01:44 -04:00
|
|
|
|
2012-08-21 23:19:50 -03: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-08-21 23:19:50 -03:00
|
|
|
float temp, temp_x, temp_y;
|
|
|
|
|
|
|
|
// push us towards the original track
|
|
|
|
update_crosstrack();
|
|
|
|
|
|
|
|
int16_t cross_speed = crosstrack_error * -g.crosstrack_gain; // scale down crosstrack_error in cm
|
|
|
|
cross_speed = constrain(cross_speed, -150, 150);
|
|
|
|
|
|
|
|
// 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:
|
|
|
|
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;
|
|
|
|
|
|
|
|
// East / West
|
|
|
|
// calculate rate error
|
2012-11-07 06:03:30 -04:00
|
|
|
x_rate_error = x_target_speed - lon_speed;
|
2012-08-21 23:19:50 -03:00
|
|
|
|
|
|
|
x_rate_error = constrain(x_rate_error, -500, 500);
|
2012-10-28 00:24:52 -03:00
|
|
|
nav_lon = g.pid_nav_lon.get_pid(x_rate_error, dTnav);
|
2012-08-21 23:19:50 -03:00
|
|
|
int32_t tilt = (x_target_speed * x_target_speed * (int32_t)g.tilt_comp) / 10000;
|
|
|
|
|
|
|
|
if(x_target_speed < 0) tilt = -tilt;
|
|
|
|
nav_lon += tilt;
|
|
|
|
|
|
|
|
|
|
|
|
// North / South
|
|
|
|
// calculate rate error
|
2012-11-07 06:03:30 -04:00
|
|
|
y_rate_error = y_target_speed - lat_speed;
|
2012-08-21 23:19:50 -03:00
|
|
|
|
|
|
|
y_rate_error = constrain(y_rate_error, -500, 500); // added a rate error limit to keep pitching down to a minimum
|
2012-10-28 00:24:52 -03:00
|
|
|
nav_lat = g.pid_nav_lat.get_pid(y_rate_error, dTnav);
|
|
|
|
tilt = (y_target_speed * y_target_speed * (int32_t)g.tilt_comp) / 10000;
|
2012-08-21 23:19:50 -03:00
|
|
|
|
|
|
|
if(y_target_speed < 0) tilt = -tilt;
|
|
|
|
nav_lat += tilt;
|
|
|
|
|
|
|
|
// 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
|
2012-11-07 06:03:30 -04:00
|
|
|
static void calc_nav_pitch_roll()
|
2011-09-29 03:11:19 -03:00
|
|
|
{
|
2012-08-21 23:19:50 -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);
|
|
|
|
// rotate the vector
|
|
|
|
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
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
// flip pitch because forward is negative
|
|
|
|
auto_pitch = -auto_pitch;
|
2011-09-29 03:11:19 -03:00
|
|
|
}
|
|
|
|
|
2012-10-21 18:32:39 -03:00
|
|
|
static int16_t get_desired_speed(int16_t max_speed)
|
2011-09-24 21:40:29 -03:00
|
|
|
{
|
2012-10-27 18:13:24 -03:00
|
|
|
/*
|
|
|
|
Based on Equation by Bill Premerlani & Robert Lefebvre
|
|
|
|
(sq(V2)-sq(V1))/2 = A(X2-X1)
|
|
|
|
derives to:
|
|
|
|
V1 = sqrt(sq(V2) - 2*A*(X2-X1))
|
|
|
|
*/
|
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
if(fast_corner) {
|
2012-10-21 18:32:39 -03:00
|
|
|
// don't slow down
|
2012-08-21 23:19:50 -03:00
|
|
|
}else{
|
2012-10-27 18:13:24 -03:00
|
|
|
if(wp_distance < 20000){ // limit the size of numbers we're dealing with to avoide overflow
|
2012-10-21 18:32:39 -03:00
|
|
|
// go slower
|
2012-10-27 18:13:24 -03:00
|
|
|
int32_t temp = 2 * 100 * (int32_t)(wp_distance - g.waypoint_radius * 100);
|
|
|
|
int32_t s_min = WAYPOINT_SPEED_MIN;
|
|
|
|
temp += s_min * s_min;
|
|
|
|
max_speed = sqrt((float)temp);
|
|
|
|
max_speed = min(max_speed, g.waypoint_speed_max);
|
2012-10-21 18:32:39 -03:00
|
|
|
}
|
2012-08-21 23:19:50 -03:00
|
|
|
}
|
|
|
|
|
2012-10-21 18:32:39 -03:00
|
|
|
max_speed = min(max_speed, max_speed_old + (100 * dTnav));// limit going faster
|
|
|
|
max_speed = max(max_speed, WAYPOINT_SPEED_MIN); // don't go too slow
|
|
|
|
max_speed_old = max_speed;
|
2012-08-21 23:19:50 -03:00
|
|
|
return max_speed;
|
2012-01-04 02:49:40 -04:00
|
|
|
}
|
|
|
|
|
2012-10-21 18:37:42 -03:00
|
|
|
static void reset_desired_speed()
|
2012-10-21 18:32:39 -03:00
|
|
|
{
|
|
|
|
max_speed_old = 0;
|
|
|
|
}
|
|
|
|
|
2012-07-19 14:09:16 -03:00
|
|
|
static int16_t get_desired_climb_rate()
|
2012-07-19 02:49:34 -03:00
|
|
|
{
|
2012-08-21 23:19:50 -03:00
|
|
|
if(alt_change_flag == ASCENDING) {
|
|
|
|
return constrain(altitude_error / 4, 100, 180); // 180cm /s up, minimum is 100cm/s
|
2012-07-19 02:49:34 -03:00
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
}else if(alt_change_flag == DESCENDING) {
|
|
|
|
return constrain(altitude_error / 6, -100, -10); // -100cm /s down, max is -10cms
|
2012-07-19 02:49:34 -03:00
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
}else{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-07-19 02:49:34 -03:00
|
|
|
}
|
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
|
|
|
{
|
2012-08-21 23:19:50 -03:00
|
|
|
// Crosstrack Error
|
|
|
|
// ----------------
|
|
|
|
// 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-08-21 23:19:50 -03:00
|
|
|
// Next_WP alt is our target alt
|
|
|
|
// It changes based on climb rate
|
|
|
|
// until it reaches the target_altitude
|
2012-11-07 06:03:30 -04:00
|
|
|
|
|
|
|
#if INERTIAL_NAV == ENABLED
|
|
|
|
// use inertial nav for altitude error
|
|
|
|
return next_WP.alt - inertial_nav._position.z;
|
|
|
|
#else
|
2012-08-21 23:19:50 -03:00
|
|
|
return next_WP.alt - current_loc.alt;
|
2012-11-07 06:03:30 -04:00
|
|
|
#endif
|
2011-03-09 02:37:09 -04:00
|
|
|
}
|
|
|
|
|
2012-01-11 03:38:16 -04:00
|
|
|
static void clear_new_altitude()
|
|
|
|
{
|
2012-08-21 23:19:50 -03:00
|
|
|
alt_change_flag = REACHED_ALT;
|
2012-01-11 03:38:16 -04:00
|
|
|
}
|
|
|
|
|
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-21 23:19:50 -03:00
|
|
|
next_WP.alt = new_alt;
|
|
|
|
alt_change_flag = REACHED_ALT;
|
2012-03-10 16:42:22 -04:00
|
|
|
}
|
|
|
|
|
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-21 23:19:50 -03:00
|
|
|
next_WP.alt = new_alt;
|
2012-01-11 03:38:16 -04:00
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
if(next_WP.alt > current_loc.alt + 20) {
|
|
|
|
// we are below, going up
|
|
|
|
alt_change_flag = ASCENDING;
|
2012-08-09 20:53:05 -03:00
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
}else if(next_WP.alt < current_loc.alt - 20) {
|
|
|
|
// we are above, going down
|
|
|
|
alt_change_flag = DESCENDING;
|
2012-08-09 20:53:05 -03:00
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
}else{
|
|
|
|
// No Change
|
|
|
|
alt_change_flag = REACHED_ALT;
|
|
|
|
}
|
2012-01-11 03:38:16 -04:00
|
|
|
}
|
|
|
|
|
2012-08-09 20:53:05 -03:00
|
|
|
static void verify_altitude()
|
2012-01-11 03:38:16 -04:00
|
|
|
{
|
2012-08-21 23:19:50 -03:00
|
|
|
if(alt_change_flag == ASCENDING) {
|
|
|
|
// we are below, going up
|
|
|
|
if(current_loc.alt > next_WP.alt - 50) {
|
|
|
|
alt_change_flag = REACHED_ALT;
|
|
|
|
}
|
|
|
|
}else if (alt_change_flag == DESCENDING) {
|
|
|
|
// we are above, going down
|
|
|
|
if(current_loc.alt <= next_WP.alt + 50)
|
|
|
|
alt_change_flag = REACHED_ALT;
|
|
|
|
}
|
2012-01-11 03:38:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-07 18:24:32 -04:00
|
|
|
static int32_t wrap_360(int32_t error)
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2012-08-21 23:19:50 -03:00
|
|
|
if (error > 36000) error -= 36000;
|
|
|
|
if (error < 0) error += 36000;
|
|
|
|
return error;
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
2011-11-07 18:24:32 -04:00
|
|
|
static int32_t wrap_180(int32_t error)
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2012-08-21 23:19:50 -03:00
|
|
|
if (error > 18000) error -= 36000;
|
|
|
|
if (error < -18000) error += 36000;
|
|
|
|
return error;
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
2012-11-07 06:03:30 -04:00
|
|
|
static float wrap_360f(float angle_in_degrees)
|
|
|
|
{
|
|
|
|
if (angle_in_degrees > 36000) angle_in_degrees -= 36000;
|
|
|
|
if (angle_in_degrees < 0) angle_in_degrees += 36000;
|
|
|
|
return angle_in_degrees;
|
|
|
|
}
|
|
|
|
|
|
|
|
static float wrap_180f(float angle_in_degrees)
|
|
|
|
{
|
|
|
|
if (angle_in_degrees > 18000) angle_in_degrees -= 36000;
|
|
|
|
if (angle_in_degrees < -18000) angle_in_degrees += 36000;
|
|
|
|
return angle_in_degrees;
|
|
|
|
}
|
|
|
|
|
|
|
|
static float wrap_PI(float angle_in_radians)
|
|
|
|
{
|
|
|
|
if (angle_in_radians > M_PI) angle_in_radians -= 2.0*M_PI;
|
|
|
|
if (angle_in_radians < -M_PI) angle_in_radians += 2.0*M_PI;
|
|
|
|
return angle_in_radians;
|
|
|
|
}
|
|
|
|
|