2012-04-10 05:39:41 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file paths.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
|
2013-05-19 16:37:30 +02:00
|
|
|
* @brief Library path manipulation
|
2012-04-10 05:39:41 +02:00
|
|
|
*
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2013-05-04 03:42:44 +02:00
|
|
|
#include <pios.h>
|
|
|
|
#include <pios_math.h>
|
|
|
|
|
2012-04-10 05:39:41 +02:00
|
|
|
#include "paths.h"
|
|
|
|
|
2012-05-28 01:51:17 +02:00
|
|
|
#include "uavobjectmanager.h" // <--.
|
2013-05-19 16:37:30 +02:00
|
|
|
#include "pathdesired.h" // <-- needed only for correct ENUM macro usage with path modes (PATHDESIRED_MODE_xxx,
|
2012-05-28 01:51:17 +02:00
|
|
|
// no direct UAVObject usage allowed in this file
|
|
|
|
|
|
|
|
// private functions
|
2013-05-19 16:37:30 +02:00
|
|
|
static void path_endpoint(float *start_point, float *end_point, float *cur_point, struct path_status *status);
|
|
|
|
static void path_vector(float *start_point, float *end_point, float *cur_point, struct path_status *status);
|
|
|
|
static void path_circle(float *start_point, float *end_point, float *cur_point, struct path_status *status, bool clockwise);
|
2012-05-28 01:51:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Compute progress along path and deviation from it
|
|
|
|
* @param[in] start_point Starting point
|
|
|
|
* @param[in] end_point Ending point
|
|
|
|
* @param[in] cur_point Current location
|
|
|
|
* @param[in] mode Path following mode
|
|
|
|
* @param[out] status Structure containing progress along path and deviation
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
void path_progress(float *start_point, float *end_point, float *cur_point, struct path_status *status, uint8_t mode)
|
2012-05-28 01:51:17 +02:00
|
|
|
{
|
2013-04-14 11:23:47 +02:00
|
|
|
switch (mode) {
|
2013-05-19 16:37:30 +02:00
|
|
|
case PATHDESIRED_MODE_FLYVECTOR:
|
|
|
|
case PATHDESIRED_MODE_DRIVEVECTOR:
|
|
|
|
return path_vector(start_point, end_point, cur_point, status);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case PATHDESIRED_MODE_FLYCIRCLERIGHT:
|
|
|
|
case PATHDESIRED_MODE_DRIVECIRCLERIGHT:
|
|
|
|
return path_circle(start_point, end_point, cur_point, status, 1);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case PATHDESIRED_MODE_FLYCIRCLELEFT:
|
|
|
|
case PATHDESIRED_MODE_DRIVECIRCLELEFT:
|
|
|
|
return path_circle(start_point, end_point, cur_point, status, 0);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case PATHDESIRED_MODE_FLYENDPOINT:
|
|
|
|
case PATHDESIRED_MODE_DRIVEENDPOINT:
|
|
|
|
default:
|
|
|
|
// use the endpoint as default failsafe if called in unknown modes
|
|
|
|
return path_endpoint(start_point, end_point, cur_point, status);
|
|
|
|
|
|
|
|
break;
|
2013-04-14 11:23:47 +02:00
|
|
|
}
|
2012-05-28 01:51:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Compute progress towards endpoint. Deviation equals distance
|
|
|
|
* @param[in] start_point Starting point
|
|
|
|
* @param[in] end_point Ending point
|
|
|
|
* @param[in] cur_point Current location
|
|
|
|
* @param[out] status Structure containing progress along path and deviation
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
static void path_endpoint(float *start_point, float *end_point, float *cur_point, struct path_status *status)
|
2012-05-28 01:51:17 +02:00
|
|
|
{
|
2013-04-14 11:23:47 +02:00
|
|
|
float path_north, path_east, diff_north, diff_east;
|
|
|
|
float dist_path, dist_diff;
|
2012-05-28 01:51:17 +02:00
|
|
|
|
2013-04-14 11:23:47 +02:00
|
|
|
// we do not correct in this mode
|
|
|
|
status->correction_direction[0] = status->correction_direction[1] = 0;
|
2012-05-28 01:51:17 +02:00
|
|
|
|
2013-04-14 11:23:47 +02:00
|
|
|
// Distance to go
|
|
|
|
path_north = end_point[0] - start_point[0];
|
2013-05-19 16:37:30 +02:00
|
|
|
path_east = end_point[1] - start_point[1];
|
2012-05-28 01:51:17 +02:00
|
|
|
|
2013-04-14 11:23:47 +02:00
|
|
|
// Current progress location relative to end
|
|
|
|
diff_north = end_point[0] - cur_point[0];
|
2013-05-19 16:37:30 +02:00
|
|
|
diff_east = end_point[1] - cur_point[1];
|
2012-05-28 01:51:17 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
dist_diff = sqrtf(diff_north * diff_north + diff_east * diff_east);
|
|
|
|
dist_path = sqrtf(path_north * path_north + path_east * path_east);
|
2012-05-28 01:51:17 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (dist_diff < 1e-6f) {
|
2013-04-14 11:23:47 +02:00
|
|
|
status->fractional_progress = 1;
|
|
|
|
status->error = 0;
|
2013-05-19 16:37:30 +02:00
|
|
|
status->path_direction[0] = status->path_direction[1] = 0;
|
2013-04-14 11:23:47 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-05-28 01:51:17 +02:00
|
|
|
|
2013-04-14 11:23:47 +02:00
|
|
|
status->fractional_progress = 1 - dist_diff / (1 + dist_path);
|
|
|
|
status->error = dist_diff;
|
2012-05-28 01:51:17 +02:00
|
|
|
|
2013-04-14 11:23:47 +02:00
|
|
|
// Compute direction to travel
|
|
|
|
status->path_direction[0] = diff_north / dist_diff;
|
|
|
|
status->path_direction[1] = diff_east / dist_diff;
|
2012-05-28 01:51:17 +02:00
|
|
|
}
|
|
|
|
|
2012-04-10 05:39:41 +02:00
|
|
|
/**
|
|
|
|
* @brief Compute progress along path and deviation from it
|
|
|
|
* @param[in] start_point Starting point
|
|
|
|
* @param[in] end_point Ending point
|
|
|
|
* @param[in] cur_point Current location
|
|
|
|
* @param[out] status Structure containing progress along path and deviation
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
static void path_vector(float *start_point, float *end_point, float *cur_point, struct path_status *status)
|
2012-04-10 05:39:41 +02:00
|
|
|
{
|
2013-04-14 11:23:47 +02:00
|
|
|
float path_north, path_east, diff_north, diff_east;
|
|
|
|
float dist_path;
|
|
|
|
float dot;
|
|
|
|
float normal[2];
|
2012-04-10 05:39:41 +02:00
|
|
|
|
2013-04-14 11:23:47 +02:00
|
|
|
// Distance to go
|
|
|
|
path_north = end_point[0] - start_point[0];
|
2013-05-19 16:37:30 +02:00
|
|
|
path_east = end_point[1] - start_point[1];
|
2012-04-10 05:39:41 +02:00
|
|
|
|
2013-04-14 11:23:47 +02:00
|
|
|
// Current progress location relative to start
|
|
|
|
diff_north = cur_point[0] - start_point[0];
|
2013-05-19 16:37:30 +02:00
|
|
|
diff_east = cur_point[1] - start_point[1];
|
2012-04-10 05:39:41 +02:00
|
|
|
|
2013-04-14 11:23:47 +02:00
|
|
|
dot = path_north * diff_north + path_east * diff_east;
|
2013-05-19 16:37:30 +02:00
|
|
|
dist_path = sqrtf(path_north * path_north + path_east * path_east);
|
2012-04-10 05:39:41 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (dist_path < 1e-6f) {
|
2013-04-14 11:23:47 +02:00
|
|
|
// if the path is too short, we cannot determine vector direction.
|
|
|
|
// Fly towards the endpoint to prevent flying away,
|
|
|
|
// but assume progress=1 either way.
|
|
|
|
path_endpoint(start_point, end_point, cur_point, status);
|
|
|
|
status->fractional_progress = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the normal to the path
|
2013-05-19 16:37:30 +02:00
|
|
|
normal[0] = -path_east / dist_path;
|
|
|
|
normal[1] = path_north / dist_path;
|
2013-04-14 11:23:47 +02:00
|
|
|
|
|
|
|
status->fractional_progress = dot / (dist_path * dist_path);
|
|
|
|
status->error = normal[0] * diff_north + normal[1] * diff_east;
|
|
|
|
|
|
|
|
// Compute direction to correct error
|
|
|
|
status->correction_direction[0] = (status->error > 0) ? -normal[0] : normal[0];
|
|
|
|
status->correction_direction[1] = (status->error > 0) ? -normal[1] : normal[1];
|
|
|
|
|
|
|
|
// Now just want magnitude of error
|
|
|
|
status->error = fabs(status->error);
|
|
|
|
|
|
|
|
// Compute direction to travel
|
|
|
|
status->path_direction[0] = path_north / dist_path;
|
|
|
|
status->path_direction[1] = path_east / dist_path;
|
2012-04-10 05:39:41 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 15:55:52 +02:00
|
|
|
/**
|
|
|
|
* @brief Compute progress along circular path and deviation from it
|
|
|
|
* @param[in] start_point Starting point
|
|
|
|
* @param[in] end_point Center point
|
|
|
|
* @param[in] cur_point Current location
|
|
|
|
* @param[out] status Structure containing progress along path and deviation
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
static void path_circle(float *start_point, float *end_point, float *cur_point, struct path_status *status, bool clockwise)
|
2012-05-26 15:55:52 +02:00
|
|
|
{
|
2013-04-14 11:23:47 +02:00
|
|
|
float radius_north, radius_east, diff_north, diff_east;
|
|
|
|
float radius, cradius;
|
|
|
|
float normal[2];
|
|
|
|
float progress;
|
|
|
|
float a_diff, a_radius;
|
2012-05-26 15:55:52 +02:00
|
|
|
|
2013-04-14 11:23:47 +02:00
|
|
|
// Radius
|
|
|
|
radius_north = end_point[0] - start_point[0];
|
2013-05-19 16:37:30 +02:00
|
|
|
radius_east = end_point[1] - start_point[1];
|
2012-05-26 15:55:52 +02:00
|
|
|
|
2013-04-14 11:23:47 +02:00
|
|
|
// Current location relative to center
|
2013-05-19 16:37:30 +02:00
|
|
|
diff_north = cur_point[0] - end_point[0];
|
|
|
|
diff_east = cur_point[1] - end_point[1];
|
2012-05-26 15:55:52 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
radius = sqrtf(powf(radius_north, 2) + powf(radius_east, 2));
|
2013-04-14 11:23:47 +02:00
|
|
|
cradius = sqrtf(powf(diff_north, 2) + powf(diff_east, 2));
|
2012-05-26 15:55:52 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (cradius < 1e-6f) {
|
2013-04-14 11:23:47 +02:00
|
|
|
// cradius is zero, just fly somewhere and make sure correction is still a normal
|
2013-05-19 16:37:30 +02:00
|
|
|
status->fractional_progress = 1;
|
2013-04-14 11:23:47 +02:00
|
|
|
status->error = radius;
|
|
|
|
status->correction_direction[0] = 0;
|
|
|
|
status->correction_direction[1] = 1;
|
|
|
|
status->path_direction[0] = 1;
|
|
|
|
status->path_direction[1] = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clockwise) {
|
|
|
|
// Compute the normal to the radius clockwise
|
|
|
|
normal[0] = -diff_east / cradius;
|
|
|
|
normal[1] = diff_north / cradius;
|
|
|
|
} else {
|
|
|
|
// Compute the normal to the radius counter clockwise
|
|
|
|
normal[0] = diff_east / cradius;
|
|
|
|
normal[1] = -diff_north / cradius;
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalize progress to 0..1
|
2013-05-19 16:37:30 +02:00
|
|
|
a_diff = atan2f(diff_north, diff_east);
|
2013-04-14 11:23:47 +02:00
|
|
|
a_radius = atan2f(radius_north, radius_east);
|
|
|
|
|
|
|
|
if (a_diff < 0) {
|
2013-05-04 03:42:44 +02:00
|
|
|
a_diff += 2.0f * M_PI_F;
|
2013-04-14 11:23:47 +02:00
|
|
|
}
|
|
|
|
if (a_radius < 0) {
|
2013-05-04 03:42:44 +02:00
|
|
|
a_radius += 2.0f * M_PI_F;
|
2013-04-14 11:23:47 +02:00
|
|
|
}
|
|
|
|
|
2013-05-04 03:42:44 +02:00
|
|
|
progress = (a_diff - a_radius + M_PI_F) / (2.0f * M_PI_F);
|
2013-04-14 11:23:47 +02:00
|
|
|
|
|
|
|
if (progress < 0) {
|
2013-05-04 03:42:44 +02:00
|
|
|
progress += 1.0f;
|
|
|
|
} else if (progress >= 1.0f) {
|
|
|
|
progress -= 1.0f;
|
2013-04-14 11:23:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (clockwise) {
|
|
|
|
progress = 1 - progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
status->fractional_progress = progress;
|
|
|
|
|
|
|
|
// error is current radius minus wanted radius - positive if too close
|
|
|
|
status->error = radius - cradius;
|
|
|
|
|
|
|
|
// Compute direction to correct error
|
|
|
|
status->correction_direction[0] = (status->error > 0 ? 1 : -1) * diff_north / cradius;
|
|
|
|
status->correction_direction[1] = (status->error > 0 ? 1 : -1) * diff_east / cradius;
|
|
|
|
|
|
|
|
// Compute direction to travel
|
|
|
|
status->path_direction[0] = normal[0];
|
|
|
|
status->path_direction[1] = normal[1];
|
|
|
|
|
|
|
|
status->error = fabs(status->error);
|
2012-05-26 15:55:52 +02:00
|
|
|
}
|