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
|
|
|
{
|
|
|
|
if(next_WP.lat == 0){
|
2011-09-20 02:24:57 -03:00
|
|
|
return 0;
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
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-10-11 06:12:37 -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();
|
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
|
|
|
// --------------------------------------------
|
|
|
|
target_bearing = get_bearing(¤t_loc, &next_WP);
|
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
|
|
|
{
|
2011-09-21 17:19:36 -03:00
|
|
|
long temp = target_bearing - original_target_bearing;
|
2011-04-25 02:12:59 -03:00
|
|
|
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-08-14 15:18:32 -03:00
|
|
|
// ------------------------------
|
|
|
|
|
|
|
|
// long_error, lat_error
|
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
|
|
|
|
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
|
2011-09-08 22:59:42 -03:00
|
|
|
long_error = (float)(next_loc->lng - current_loc.lng) * scaleLongDown; // 500 - 0 = 500 roll EAST
|
2011-04-16 17:44:23 -03:00
|
|
|
|
|
|
|
// Y PITCH
|
2011-09-08 22:59:42 -03:00
|
|
|
lat_error = next_loc->lat - current_loc.lat; // 0 - 500 = -500 pitch NORTH
|
2011-05-16 01:59:06 -03:00
|
|
|
}
|
2011-04-16 17:44:23 -03:00
|
|
|
|
2011-09-04 21:15:36 -03:00
|
|
|
#define NAV_ERR_MAX 400
|
2011-09-27 13:35:05 -03:00
|
|
|
static void calc_loiter(int x_error, int y_error)
|
2011-05-16 01:59:06 -03:00
|
|
|
{
|
2011-09-04 21:15:36 -03:00
|
|
|
x_error = constrain(x_error, -NAV_ERR_MAX, NAV_ERR_MAX);
|
|
|
|
y_error = constrain(y_error, -NAV_ERR_MAX, NAV_ERR_MAX);
|
2011-04-16 17:44:23 -03:00
|
|
|
|
2011-09-04 21:15:36 -03:00
|
|
|
int x_target_speed = g.pi_loiter_lon.get_pi(x_error, dTnav);
|
|
|
|
int y_target_speed = g.pi_loiter_lat.get_pi(y_error, dTnav);
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-09-04 21:15:36 -03:00
|
|
|
// find the rates:
|
2011-09-12 00:36:20 -03:00
|
|
|
float temp = radians((float)g_gps->ground_course/100.0);
|
|
|
|
|
2011-09-16 03:41:15 -03:00
|
|
|
#ifdef OPTFLOW_ENABLED
|
|
|
|
// calc the cos of the error to tell how fast we are moving towards the target in cm
|
|
|
|
if(g.optflow_enabled && current_loc.alt < 500 && g_gps->ground_speed < 150){
|
|
|
|
x_actual_speed = optflow.vlon * 10;
|
|
|
|
y_actual_speed = optflow.vlat * 10;
|
|
|
|
}else{
|
|
|
|
x_actual_speed = (float)g_gps->ground_speed * sin(temp);
|
|
|
|
y_actual_speed = (float)g_gps->ground_speed * cos(temp);
|
|
|
|
}
|
|
|
|
#else
|
2011-09-16 03:33:00 -03:00
|
|
|
x_actual_speed = (float)g_gps->ground_speed * sin(temp);
|
|
|
|
y_actual_speed = (float)g_gps->ground_speed * cos(temp);
|
2011-09-16 03:41:15 -03:00
|
|
|
#endif
|
2011-09-16 03:33:00 -03:00
|
|
|
|
2011-09-04 21:15:36 -03:00
|
|
|
y_rate_error = y_target_speed - y_actual_speed; // 413
|
2011-09-29 03:11:19 -03:00
|
|
|
y_rate_error = constrain(y_rate_error, -250, 250); // added a rate error limit to keep pitching down to a minimum
|
2011-09-30 03:27:23 -03:00
|
|
|
nav_lat = g.pi_nav_lat.get_pi(y_rate_error, dTnav);
|
|
|
|
nav_lat = constrain(nav_lat, -3500, 3500);
|
2011-08-08 13:54:56 -03:00
|
|
|
|
2011-09-04 21:15:36 -03:00
|
|
|
x_rate_error = x_target_speed - x_actual_speed;
|
2011-09-29 03:11:19 -03:00
|
|
|
x_rate_error = constrain(x_rate_error, -250, 250);
|
2011-09-30 03:27:23 -03:00
|
|
|
nav_lon = g.pi_nav_lon.get_pi(x_rate_error, dTnav);
|
|
|
|
nav_lon = constrain(nav_lon, -3500, 3500);
|
2011-03-14 03:04:07 -03:00
|
|
|
}
|
|
|
|
|
2011-09-29 03:11:19 -03:00
|
|
|
// nav_roll, nav_pitch
|
|
|
|
static void calc_loiter_pitch_roll()
|
|
|
|
{
|
2011-10-15 19:58:03 -03:00
|
|
|
|
|
|
|
float temp = radians((float)(9000 - (dcm.yaw_sensor))/100.0);
|
|
|
|
float _cos_yaw_x = cos(temp);
|
|
|
|
float _sin_yaw_y = sin(temp);
|
|
|
|
|
|
|
|
// Serial.printf("ys %ld, cyx %1.4f, _cyx %1.4f\n", dcm.yaw_sensor, cos_yaw_x, _cos_yaw_x);
|
|
|
|
|
2011-09-29 03:11:19 -03:00
|
|
|
// rotate the vector
|
2011-10-15 19:58:03 -03: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-09-29 03:11:19 -03:00
|
|
|
|
|
|
|
// flip pitch because forward is negative
|
|
|
|
nav_pitch = -nav_pitch;
|
|
|
|
}
|
|
|
|
|
2011-09-27 13:35:05 -03:00
|
|
|
static void calc_nav_rate(int max_speed)
|
2011-09-24 21:40:29 -03:00
|
|
|
{
|
2011-09-25 01:49:45 -03:00
|
|
|
/*
|
|
|
|
0 1 2 3 4 5 6 7 8
|
|
|
|
...|...|...|...|...|...|...|...|
|
|
|
|
100 200 300 400
|
|
|
|
+|+
|
|
|
|
*/
|
|
|
|
max_speed = min(max_speed, (wp_distance * 50));
|
2011-09-24 21:40:29 -03:00
|
|
|
|
2011-10-03 14:37:56 -03:00
|
|
|
// limit the ramp up of the speed
|
|
|
|
if(waypoint_speed_gov < max_speed){
|
2011-10-15 19:35:18 -03:00
|
|
|
|
|
|
|
waypoint_speed_gov += (int)(150.0 * dTnav); // increase at 1.5/ms
|
|
|
|
|
|
|
|
// go at least 1m/s
|
|
|
|
max_speed = max(100, waypoint_speed_gov);
|
|
|
|
// limit with governer
|
2011-10-03 14:37:56 -03:00
|
|
|
max_speed = min(max_speed, waypoint_speed_gov);
|
|
|
|
}
|
|
|
|
|
2011-09-24 21:40:29 -03:00
|
|
|
// XXX target_angle should be the original desired target angle!
|
|
|
|
float temp = radians((original_target_bearing - g_gps->ground_course)/100.0);
|
|
|
|
|
|
|
|
x_actual_speed = -sin(temp) * (float)g_gps->ground_speed;
|
|
|
|
x_rate_error = -x_actual_speed;
|
|
|
|
x_rate_error = constrain(x_rate_error, -800, 800);
|
|
|
|
nav_lon = constrain(g.pi_nav_lon.get_pi(x_rate_error, dTnav), -3500, 3500);
|
|
|
|
|
|
|
|
y_actual_speed = cos(temp) * (float)g_gps->ground_speed;
|
|
|
|
y_rate_error = max_speed - y_actual_speed; // 413
|
|
|
|
y_rate_error = constrain(y_rate_error, -800, 800); // added a rate error limit to keep pitching down to a minimum
|
|
|
|
nav_lat = constrain(g.pi_nav_lat.get_pi(y_rate_error, dTnav), -3500, 3500);
|
|
|
|
|
|
|
|
/*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);*/
|
|
|
|
}
|
|
|
|
|
|
|
|
// nav_roll, nav_pitch
|
2011-09-27 13:35:05 -03:00
|
|
|
static void calc_nav_pitch_roll()
|
2011-09-24 21:40:29 -03:00
|
|
|
{
|
|
|
|
float temp = radians((float)(9000 - (dcm.yaw_sensor - original_target_bearing))/100.0);
|
|
|
|
float _cos_yaw_x = cos(temp);
|
|
|
|
float _sin_yaw_y = sin(temp);
|
|
|
|
|
|
|
|
// rotate the vector
|
|
|
|
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;
|
|
|
|
|
|
|
|
// flip pitch because forward is negative
|
|
|
|
nav_pitch = -nav_pitch;
|
|
|
|
|
|
|
|
/*Serial.printf("_cos_yaw_x:%1.4f, _sin_yaw_y:%1.4f, nav_roll:%ld, nav_pitch:%ld\n",
|
|
|
|
_cos_yaw_x,
|
|
|
|
_sin_yaw_y,
|
|
|
|
nav_roll,
|
|
|
|
nav_pitch);*/
|
|
|
|
}
|
|
|
|
|
2011-09-04 21:15:36 -03:00
|
|
|
static long get_altitude_error()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2011-09-04 21:15:36 -03:00
|
|
|
return next_WP.alt - current_loc.alt;
|
2011-03-09 02:37:09 -04:00
|
|
|
}
|
|
|
|
|
2011-09-04 21:15:36 -03:00
|
|
|
static int get_loiter_angle()
|
2011-07-30 17:42:54 -03:00
|
|
|
{
|
|
|
|
float power;
|
2011-09-04 21:15:36 -03:00
|
|
|
int angle;
|
2011-07-30 17:42:54 -03:00
|
|
|
|
|
|
|
if(wp_distance <= g.loiter_radius){
|
|
|
|
power = float(wp_distance) / float(g.loiter_radius);
|
|
|
|
power = constrain(power, 0.5, 1);
|
2011-09-04 21:15:36 -03:00
|
|
|
angle = 90.0 * (2.0 + power);
|
2011-07-30 17:42:54 -03:00
|
|
|
}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);
|
2011-09-04 21:15:36 -03:00
|
|
|
angle = power * 90;
|
2011-07-30 17:42:54 -03:00
|
|
|
}
|
2011-09-04 21:15:36 -03:00
|
|
|
|
|
|
|
return angle;
|
2011-07-30 17:42:54 -03:00
|
|
|
}
|
|
|
|
|
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-09-12 00:36:20 -03:00
|
|
|
/*
|
2011-09-04 21:15:36 -03:00
|
|
|
static long get_crosstrack_correction(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-09-04 21:15:36 -03:00
|
|
|
|
2011-08-08 13:54:56 -03:00
|
|
|
// Meters we are off track line
|
2011-09-04 21:15:36 -03:00
|
|
|
float error = sin(radians((target_bearing - crosstrack_bearing) / (float)100)) * (float)wp_distance;
|
2011-08-08 13:54:56 -03:00
|
|
|
|
|
|
|
// take meters * 100 to get adjustment to nav_bearing
|
2011-09-04 21:15:36 -03:00
|
|
|
long _crosstrack_correction = g.pi_crosstrack.get_pi(error, dTnav) * 100;
|
|
|
|
|
|
|
|
// constrain answer to 30° to avoid overshoot
|
|
|
|
return constrain(_crosstrack_correction, -g.crosstrack_entry_angle.get(), g.crosstrack_entry_angle.get());
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
2011-09-04 22:16:02 -03:00
|
|
|
return 0;
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
2011-09-12 00:36:20 -03:00
|
|
|
*/
|
2011-09-12 00:40:05 -03:00
|
|
|
/*
|
2011-07-17 07:32:00 -03:00
|
|
|
static long cross_track_test()
|
2011-04-21 20:07:31 -03:00
|
|
|
{
|
2011-09-04 21:15:36 -03:00
|
|
|
long temp = wrap_180(target_bearing - crosstrack_bearing);
|
2011-04-21 20:07:31 -03:00
|
|
|
return abs(temp);
|
|
|
|
}
|
2011-09-12 00:40:05 -03:00
|
|
|
*/
|
|
|
|
/*
|
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-09-12 00:40:05 -03:00
|
|
|
*/
|
2011-10-15 17:09:04 -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-10-15 17:09:04 -03:00
|
|
|
*/
|
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-10-15 17:09:04 -03:00
|
|
|
/*
|
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-10-15 17:09:04 -03:00
|
|
|
*/
|
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;
|
|
|
|
}
|