2012-03-11 12:31:38 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
2016-04-25 18:45:35 +02:00
|
|
|
* @addtogroup LibrePilotModules LibrePilot Modules
|
2012-03-11 12:31:38 +01:00
|
|
|
* @{
|
|
|
|
* @addtogroup PathPlanner Path Planner Module
|
|
|
|
* @brief Executes a series of waypoints
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file pathplanner.c
|
2016-04-25 18:45:35 +02:00
|
|
|
* @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016.
|
|
|
|
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2015.
|
2012-03-11 12:31:38 +01:00
|
|
|
* @brief Executes a series of waypoints
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "openpilot.h"
|
2012-04-10 09:51:50 +02:00
|
|
|
|
2014-02-02 19:54:17 +01:00
|
|
|
#include "callbackinfo.h"
|
2014-01-13 22:21:34 +01:00
|
|
|
#include "pathplan.h"
|
2012-03-11 12:31:38 +01:00
|
|
|
#include "flightstatus.h"
|
2013-05-18 19:36:45 +02:00
|
|
|
#include "airspeedstate.h"
|
2012-05-21 21:42:28 +02:00
|
|
|
#include "pathaction.h"
|
2012-04-10 09:51:50 +02:00
|
|
|
#include "pathdesired.h"
|
2012-05-21 21:42:28 +02:00
|
|
|
#include "pathstatus.h"
|
2013-05-18 19:36:45 +02:00
|
|
|
#include "positionstate.h"
|
|
|
|
#include "velocitystate.h"
|
2012-03-11 12:31:38 +01:00
|
|
|
#include "waypoint.h"
|
|
|
|
#include "waypointactive.h"
|
2014-02-09 19:33:29 +01:00
|
|
|
#include "flightmodesettings.h"
|
2015-03-20 11:42:20 +01:00
|
|
|
#include <systemsettings.h>
|
2013-05-02 23:31:14 +02:00
|
|
|
#include "paths.h"
|
2014-05-19 18:35:18 +02:00
|
|
|
#include "plans.h"
|
2015-03-20 11:42:20 +01:00
|
|
|
#include <sanitycheck.h>
|
|
|
|
#include <vtolpathfollowersettings.h>
|
2015-04-27 15:01:47 +02:00
|
|
|
#include <manualcontrolcommand.h>
|
2012-03-11 12:31:38 +01:00
|
|
|
|
|
|
|
// Private constants
|
2013-05-18 14:17:26 +02:00
|
|
|
#define STACK_SIZE_BYTES 1024
|
2014-01-11 20:03:36 +01:00
|
|
|
#define TASK_PRIORITY CALLBACK_TASK_NAVIGATION
|
2013-05-18 14:17:26 +02:00
|
|
|
#define MAX_QUEUE_SIZE 2
|
2014-01-11 20:03:36 +01:00
|
|
|
#define PATH_PLANNER_UPDATE_RATE_MS 100 // can be slow, since we listen to status updates as well
|
2012-03-11 12:31:38 +01:00
|
|
|
|
|
|
|
// Private types
|
|
|
|
|
2012-05-21 21:42:28 +02:00
|
|
|
// Private functions
|
2014-01-11 20:03:36 +01:00
|
|
|
static void pathPlannerTask();
|
|
|
|
static void commandUpdated(UAVObjEvent *ev);
|
|
|
|
static void statusUpdated(UAVObjEvent *ev);
|
|
|
|
static void updatePathDesired();
|
2012-05-21 21:42:28 +02:00
|
|
|
static void setWaypoint(uint16_t num);
|
|
|
|
|
2014-01-13 22:21:34 +01:00
|
|
|
static uint8_t checkPathPlan();
|
2012-05-21 21:42:28 +02:00
|
|
|
static uint8_t pathConditionCheck();
|
|
|
|
static uint8_t conditionNone();
|
|
|
|
static uint8_t conditionTimeOut();
|
|
|
|
static uint8_t conditionDistanceToTarget();
|
|
|
|
static uint8_t conditionLegRemaining();
|
2012-05-28 01:51:17 +02:00
|
|
|
static uint8_t conditionBelowError();
|
2012-05-21 21:42:28 +02:00
|
|
|
static uint8_t conditionAboveAltitude();
|
2012-05-28 01:51:17 +02:00
|
|
|
static uint8_t conditionAboveSpeed();
|
2012-05-21 21:42:28 +02:00
|
|
|
static uint8_t conditionPointingTowardsNext();
|
|
|
|
static uint8_t conditionPythonScript();
|
|
|
|
static uint8_t conditionImmediate();
|
2015-03-20 11:42:20 +01:00
|
|
|
static void SettingsUpdatedCb(__attribute__((unused)) UAVObjEvent *ev);
|
2012-05-21 21:42:28 +02:00
|
|
|
|
|
|
|
|
2012-03-11 12:31:38 +01:00
|
|
|
// Private variables
|
2014-01-11 20:03:36 +01:00
|
|
|
static DelayedCallbackInfo *pathPlannerHandle;
|
|
|
|
static DelayedCallbackInfo *pathDesiredUpdaterHandle;
|
2012-03-15 10:43:14 +01:00
|
|
|
static WaypointActiveData waypointActive;
|
|
|
|
static WaypointData waypoint;
|
2012-05-21 21:42:28 +02:00
|
|
|
static PathActionData pathAction;
|
|
|
|
static bool pathplanner_active = false;
|
2015-03-20 11:42:20 +01:00
|
|
|
static FrameType_t frameType;
|
|
|
|
static bool mode3D;
|
2012-03-11 12:31:38 +01:00
|
|
|
|
2015-03-20 11:42:20 +01:00
|
|
|
extern FrameType_t GetCurrentFrameType();
|
2012-05-10 02:36:44 +02:00
|
|
|
|
2012-03-11 12:31:38 +01:00
|
|
|
/**
|
|
|
|
* Module initialization
|
|
|
|
*/
|
|
|
|
int32_t PathPlannerStart()
|
|
|
|
{
|
2014-05-19 18:35:18 +02:00
|
|
|
plan_initialize();
|
2014-01-11 20:03:36 +01:00
|
|
|
// when the active waypoint changes, update pathDesired
|
|
|
|
WaypointConnectCallback(commandUpdated);
|
|
|
|
WaypointActiveConnectCallback(commandUpdated);
|
|
|
|
PathActionConnectCallback(commandUpdated);
|
|
|
|
PathStatusConnectCallback(statusUpdated);
|
2015-03-20 11:42:20 +01:00
|
|
|
SettingsUpdatedCb(NULL);
|
|
|
|
SystemSettingsConnectCallback(&SettingsUpdatedCb);
|
|
|
|
VtolPathFollowerSettingsConnectCallback(&SettingsUpdatedCb);
|
2012-03-11 12:31:38 +01:00
|
|
|
|
2014-01-11 20:03:36 +01:00
|
|
|
// Start main task callback
|
2014-02-02 22:08:16 +01:00
|
|
|
PIOS_CALLBACKSCHEDULER_Dispatch(pathPlannerHandle);
|
2012-03-11 12:31:38 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
return 0;
|
2012-03-11 12:31:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module initialization
|
|
|
|
*/
|
|
|
|
int32_t PathPlannerInitialize()
|
|
|
|
{
|
2014-01-13 22:21:34 +01:00
|
|
|
PathPlanInitialize();
|
2013-05-18 14:17:26 +02:00
|
|
|
PathActionInitialize();
|
|
|
|
PathStatusInitialize();
|
|
|
|
PathDesiredInitialize();
|
2013-05-18 19:36:45 +02:00
|
|
|
PositionStateInitialize();
|
|
|
|
AirspeedStateInitialize();
|
|
|
|
VelocityStateInitialize();
|
2013-05-18 14:17:26 +02:00
|
|
|
WaypointInitialize();
|
|
|
|
WaypointActiveInitialize();
|
|
|
|
|
2014-02-02 22:08:16 +01:00
|
|
|
pathPlannerHandle = PIOS_CALLBACKSCHEDULER_Create(&pathPlannerTask, CALLBACK_PRIORITY_REGULAR, TASK_PRIORITY, CALLBACKINFO_RUNNING_PATHPLANNER0, STACK_SIZE_BYTES);
|
|
|
|
pathDesiredUpdaterHandle = PIOS_CALLBACKSCHEDULER_Create(&updatePathDesired, CALLBACK_PRIORITY_CRITICAL, TASK_PRIORITY, CALLBACKINFO_RUNNING_PATHPLANNER1, STACK_SIZE_BYTES);
|
2014-01-11 20:03:36 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
return 0;
|
2012-03-11 12:31:38 +01:00
|
|
|
}
|
|
|
|
|
2013-06-04 05:37:40 +02:00
|
|
|
MODULE_INITCALL(PathPlannerInitialize, PathPlannerStart);
|
2012-03-11 12:31:38 +01:00
|
|
|
|
2015-03-20 11:42:20 +01:00
|
|
|
|
|
|
|
static void SettingsUpdatedCb(__attribute__((unused)) UAVObjEvent *ev)
|
|
|
|
{
|
2015-04-11 22:43:04 +02:00
|
|
|
VtolPathFollowerSettingsTreatCustomCraftAsOptions TreatCustomCraftAs;
|
2015-03-20 11:42:20 +01:00
|
|
|
|
|
|
|
VtolPathFollowerSettingsTreatCustomCraftAsGet(&TreatCustomCraftAs);
|
|
|
|
|
|
|
|
frameType = GetCurrentFrameType();
|
|
|
|
|
|
|
|
if (frameType == FRAME_TYPE_CUSTOM) {
|
|
|
|
switch (TreatCustomCraftAs) {
|
|
|
|
case VTOLPATHFOLLOWERSETTINGS_TREATCUSTOMCRAFTAS_FIXEDWING:
|
|
|
|
frameType = FRAME_TYPE_FIXED_WING;
|
|
|
|
break;
|
|
|
|
case VTOLPATHFOLLOWERSETTINGS_TREATCUSTOMCRAFTAS_VTOL:
|
|
|
|
frameType = FRAME_TYPE_MULTIROTOR;
|
|
|
|
break;
|
|
|
|
case VTOLPATHFOLLOWERSETTINGS_TREATCUSTOMCRAFTAS_GROUND:
|
|
|
|
frameType = FRAME_TYPE_GROUND;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-04-03 16:32:33 +02:00
|
|
|
|
|
|
|
switch (frameType) {
|
|
|
|
case FRAME_TYPE_GROUND:
|
|
|
|
mode3D = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mode3D = true;
|
|
|
|
}
|
2015-03-20 11:42:20 +01:00
|
|
|
}
|
|
|
|
|
2015-04-27 15:01:47 +02:00
|
|
|
#define AUTOTAKEOFF_THROTTLE_LIMIT_TO_ALLOW_TAKEOFF_START 0.3f
|
2015-03-20 11:42:20 +01:00
|
|
|
|
2012-03-11 12:31:38 +01:00
|
|
|
/**
|
|
|
|
* Module task
|
|
|
|
*/
|
2014-01-11 20:03:36 +01:00
|
|
|
static void pathPlannerTask()
|
2012-03-11 12:31:38 +01:00
|
|
|
{
|
2014-02-02 22:08:16 +01:00
|
|
|
PIOS_CALLBACKSCHEDULER_Schedule(pathPlannerHandle, PATH_PLANNER_UPDATE_RATE_MS, CALLBACK_UPDATEMODE_SOONER);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
bool endCondition = false;
|
|
|
|
|
2014-01-13 22:21:34 +01:00
|
|
|
// check path plan validity early to raise alarm
|
2014-01-12 21:04:31 +01:00
|
|
|
// even if not in guided mode
|
2014-01-13 22:21:34 +01:00
|
|
|
uint8_t validPathPlan = checkPathPlan();
|
2014-01-12 21:04:31 +01:00
|
|
|
|
2014-01-11 20:03:36 +01:00
|
|
|
FlightStatusData flightStatus;
|
|
|
|
FlightStatusGet(&flightStatus);
|
2014-02-16 19:07:31 +01:00
|
|
|
if (flightStatus.ControlChain.PathPlanner != FLIGHTSTATUS_CONTROLCHAIN_TRUE) {
|
2014-01-11 20:03:36 +01:00
|
|
|
pathplanner_active = false;
|
2016-06-28 23:20:39 +02:00
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_PATHPLAN);
|
2014-01-11 20:03:36 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2014-01-12 21:04:31 +01:00
|
|
|
PathDesiredData pathDesired;
|
|
|
|
PathDesiredGet(&pathDesired);
|
|
|
|
|
|
|
|
static uint8_t failsafeRTHset = 0;
|
2014-01-13 22:21:34 +01:00
|
|
|
if (!validPathPlan) {
|
2014-01-12 21:04:31 +01:00
|
|
|
pathplanner_active = false;
|
|
|
|
|
|
|
|
if (!failsafeRTHset) {
|
|
|
|
failsafeRTHset = 1;
|
2014-05-19 18:35:18 +02:00
|
|
|
plan_setup_positionHold();
|
2014-01-12 21:04:31 +01:00
|
|
|
}
|
2014-04-29 22:25:49 +02:00
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_PATHPLAN, SYSTEMALARMS_ALARM_CRITICAL);
|
2014-01-12 21:37:40 +01:00
|
|
|
|
|
|
|
return;
|
2014-01-12 21:04:31 +01:00
|
|
|
}
|
2015-03-20 11:42:20 +01:00
|
|
|
|
2014-01-12 21:04:31 +01:00
|
|
|
failsafeRTHset = 0;
|
2014-01-12 21:37:40 +01:00
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_PATHPLAN);
|
2014-01-12 21:04:31 +01:00
|
|
|
|
2014-01-11 20:03:36 +01:00
|
|
|
WaypointActiveGet(&waypointActive);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2015-04-27 15:01:47 +02:00
|
|
|
// with the introduction of takeoff, we allow for arming
|
|
|
|
// whilst in pathplanner mode. Previously it was just an assumption that
|
|
|
|
// a user never armed in pathplanner mode. This check allows a user to select
|
|
|
|
// pathplanner, to upload waypoints, and then arm in pathplanner.
|
|
|
|
if (!flightStatus.Armed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the transition from pathplanner to another flightmode back to pathplanner
|
|
|
|
// triggers a reset back to 0 index in the waypoint list
|
2014-01-11 20:03:36 +01:00
|
|
|
if (pathplanner_active == false) {
|
2015-05-23 22:17:32 +02:00
|
|
|
pathplanner_active = true;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2015-05-23 22:17:32 +02:00
|
|
|
FlightModeSettingsFlightModeChangeRestartsPathPlanOptions restart;
|
|
|
|
FlightModeSettingsFlightModeChangeRestartsPathPlanGet(&restart);
|
|
|
|
if (restart == FLIGHTMODESETTINGS_FLIGHTMODECHANGERESTARTSPATHPLAN_TRUE) {
|
|
|
|
setWaypoint(0);
|
|
|
|
return;
|
|
|
|
}
|
2014-01-11 20:03:36 +01:00
|
|
|
}
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2014-01-11 20:03:36 +01:00
|
|
|
WaypointInstGet(waypointActive.Index, &waypoint);
|
|
|
|
PathActionInstGet(waypoint.Action, &pathAction);
|
|
|
|
PathStatusData pathStatus;
|
|
|
|
PathStatusGet(&pathStatus);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2014-01-11 20:03:36 +01:00
|
|
|
// delay next step until path follower has acknowledged the path mode
|
|
|
|
if (pathStatus.UID != pathDesired.UID) {
|
|
|
|
return;
|
|
|
|
}
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2014-01-11 20:03:36 +01:00
|
|
|
// negative destinations DISABLE this feature
|
|
|
|
if (pathStatus.Status == PATHSTATUS_STATUS_CRITICAL && waypointActive.Index != pathAction.ErrorDestination && pathAction.ErrorDestination >= 0) {
|
|
|
|
setWaypoint(pathAction.ErrorDestination);
|
|
|
|
return;
|
|
|
|
}
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2015-04-27 15:01:47 +02:00
|
|
|
|
2014-01-11 20:03:36 +01:00
|
|
|
// check if condition has been met
|
|
|
|
endCondition = pathConditionCheck();
|
|
|
|
// decide what to do
|
|
|
|
switch (pathAction.Command) {
|
|
|
|
case PATHACTION_COMMAND_ONNOTCONDITIONNEXTWAYPOINT:
|
|
|
|
endCondition = !endCondition;
|
|
|
|
case PATHACTION_COMMAND_ONCONDITIONNEXTWAYPOINT:
|
|
|
|
if (endCondition) {
|
|
|
|
setWaypoint(waypointActive.Index + 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PATHACTION_COMMAND_ONNOTCONDITIONJUMPWAYPOINT:
|
|
|
|
endCondition = !endCondition;
|
|
|
|
case PATHACTION_COMMAND_ONCONDITIONJUMPWAYPOINT:
|
|
|
|
if (endCondition) {
|
|
|
|
if (pathAction.JumpDestination < 0) {
|
|
|
|
// waypoint ids <0 code relative jumps
|
|
|
|
setWaypoint(waypointActive.Index - pathAction.JumpDestination);
|
|
|
|
} else {
|
|
|
|
setWaypoint(pathAction.JumpDestination);
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
2014-01-11 20:03:36 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PATHACTION_COMMAND_IFCONDITIONJUMPWAYPOINTELSENEXTWAYPOINT:
|
|
|
|
if (endCondition) {
|
|
|
|
if (pathAction.JumpDestination < 0) {
|
|
|
|
// waypoint ids <0 code relative jumps
|
|
|
|
setWaypoint(waypointActive.Index - pathAction.JumpDestination);
|
2013-05-18 14:17:26 +02:00
|
|
|
} else {
|
2014-01-11 20:03:36 +01:00
|
|
|
setWaypoint(pathAction.JumpDestination);
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
2014-01-11 20:03:36 +01:00
|
|
|
} else {
|
|
|
|
setWaypoint(waypointActive.Index + 1);
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
2014-01-11 20:03:36 +01:00
|
|
|
break;
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
2012-03-15 10:33:32 +01:00
|
|
|
}
|
|
|
|
|
2015-04-27 15:01:47 +02:00
|
|
|
// callback function when waypoints changed in any way, update pathDesired
|
|
|
|
void updatePathDesired()
|
|
|
|
{
|
|
|
|
// only ever touch pathDesired if pathplanner is enabled
|
|
|
|
if (!pathplanner_active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find out current waypoint
|
|
|
|
WaypointActiveGet(&waypointActive);
|
|
|
|
WaypointInstGet(waypointActive.Index, &waypoint);
|
2015-05-04 13:14:54 +02:00
|
|
|
|
2015-04-27 15:01:47 +02:00
|
|
|
PathActionInstGet(waypoint.Action, &pathAction);
|
|
|
|
|
2015-05-04 13:14:54 +02:00
|
|
|
PathDesiredData pathDesired;
|
2015-05-23 22:17:32 +02:00
|
|
|
|
|
|
|
pathDesired.End.North = waypoint.Position.North;
|
|
|
|
pathDesired.End.East = waypoint.Position.East;
|
|
|
|
pathDesired.End.Down = waypoint.Position.Down;
|
|
|
|
pathDesired.EndingVelocity = waypoint.Velocity;
|
|
|
|
pathDesired.Mode = pathAction.Mode;
|
|
|
|
pathDesired.ModeParameters[0] = pathAction.ModeParameters[0];
|
|
|
|
pathDesired.ModeParameters[1] = pathAction.ModeParameters[1];
|
|
|
|
pathDesired.ModeParameters[2] = pathAction.ModeParameters[2];
|
|
|
|
pathDesired.ModeParameters[3] = pathAction.ModeParameters[3];
|
|
|
|
pathDesired.UID = waypointActive.Index;
|
|
|
|
|
|
|
|
|
|
|
|
if (waypointActive.Index == 0) {
|
|
|
|
PositionStateData positionState;
|
|
|
|
PositionStateGet(&positionState);
|
|
|
|
// First waypoint has itself as start point (used to be home position but that proved dangerous when looping)
|
|
|
|
|
|
|
|
/*pathDesired.Start[PATHDESIRED_START_NORTH] = waypoint.Position[WAYPOINT_POSITION_NORTH];
|
|
|
|
pathDesired.Start[PATHDESIRED_START_EAST] = waypoint.Position[WAYPOINT_POSITION_EAST];
|
|
|
|
pathDesired.Start[PATHDESIRED_START_DOWN] = waypoint.Position[WAYPOINT_POSITION_DOWN];*/
|
|
|
|
// note: if certain flightmodes need to override Start, End or mode parameters, that should happen within
|
|
|
|
// the pathfollower as needed. This also holds if Start is replaced by current position for takeoff and landing
|
|
|
|
pathDesired.Start.North = positionState.North;
|
|
|
|
pathDesired.Start.East = positionState.East;
|
|
|
|
pathDesired.Start.Down = positionState.Down;
|
|
|
|
pathDesired.StartingVelocity = pathDesired.EndingVelocity;
|
|
|
|
} else {
|
|
|
|
// Get previous waypoint as start point
|
|
|
|
WaypointData waypointPrev;
|
|
|
|
WaypointInstGet(waypointActive.Index - 1, &waypointPrev);
|
|
|
|
|
|
|
|
pathDesired.Start.North = waypointPrev.Position.North;
|
|
|
|
pathDesired.Start.East = waypointPrev.Position.East;
|
|
|
|
pathDesired.Start.Down = waypointPrev.Position.Down;
|
|
|
|
pathDesired.StartingVelocity = waypointPrev.Velocity;
|
2015-04-27 15:01:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PathDesiredSet(&pathDesired);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-14 20:49:19 +01:00
|
|
|
// safety checks for path plan integrity
|
2014-01-13 22:21:34 +01:00
|
|
|
static uint8_t checkPathPlan()
|
2014-01-12 21:04:31 +01:00
|
|
|
{
|
|
|
|
uint16_t i;
|
|
|
|
uint16_t waypointCount;
|
|
|
|
uint16_t actionCount;
|
2014-01-13 22:21:34 +01:00
|
|
|
uint8_t pathCrc;
|
|
|
|
PathPlanData pathPlan;
|
2014-01-14 22:46:01 +01:00
|
|
|
|
|
|
|
// WaypointData waypoint; // using global instead (?)
|
|
|
|
// PathActionData action; // using global instead (?)
|
2014-01-12 21:04:31 +01:00
|
|
|
|
2014-01-13 22:21:34 +01:00
|
|
|
PathPlanGet(&pathPlan);
|
2014-01-12 21:04:31 +01:00
|
|
|
|
2014-01-13 22:21:34 +01:00
|
|
|
waypointCount = pathPlan.WaypointCount;
|
2014-01-14 21:00:44 +01:00
|
|
|
if (waypointCount == 0) {
|
|
|
|
// an empty path plan is invalid
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-14 22:46:01 +01:00
|
|
|
actionCount = pathPlan.PathActionCount;
|
2014-01-12 21:04:31 +01:00
|
|
|
|
|
|
|
// check count consistency
|
|
|
|
if (waypointCount > UAVObjGetNumInstances(WaypointHandle())) {
|
2014-01-14 22:46:01 +01:00
|
|
|
// PIOS_DEBUGLOG_Printf("PathPlan : waypoint count error!");
|
2014-01-12 21:04:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (actionCount > UAVObjGetNumInstances(PathActionHandle())) {
|
2014-01-14 22:46:01 +01:00
|
|
|
// PIOS_DEBUGLOG_Printf("PathPlan : path action count error!");
|
2014-01-12 21:04:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check CRC
|
2014-01-13 22:21:34 +01:00
|
|
|
pathCrc = 0;
|
2014-01-12 21:04:31 +01:00
|
|
|
for (i = 0; i < waypointCount; i++) {
|
2014-01-13 22:21:34 +01:00
|
|
|
pathCrc = UAVObjUpdateCRC(WaypointHandle(), i, pathCrc);
|
2014-01-12 21:04:31 +01:00
|
|
|
}
|
|
|
|
for (i = 0; i < actionCount; i++) {
|
2014-01-13 22:21:34 +01:00
|
|
|
pathCrc = UAVObjUpdateCRC(PathActionHandle(), i, pathCrc);
|
2014-01-12 21:04:31 +01:00
|
|
|
}
|
2014-01-13 22:21:34 +01:00
|
|
|
if (pathCrc != pathPlan.Crc) {
|
2014-01-14 20:49:19 +01:00
|
|
|
// failed crc check
|
2014-01-14 22:46:01 +01:00
|
|
|
// PIOS_DEBUGLOG_Printf("PathPlan : bad CRC (%d / %d)!", pathCrc, pathPlan.Crc);
|
2014-01-12 21:04:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-14 21:00:44 +01:00
|
|
|
// waypoint consistency
|
|
|
|
for (i = 0; i < waypointCount; i++) {
|
|
|
|
WaypointInstGet(i, &waypoint);
|
|
|
|
if (waypoint.Action >= actionCount) {
|
|
|
|
// path action id is out of range
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// path action consistency
|
|
|
|
for (i = 0; i < actionCount; i++) {
|
|
|
|
PathActionInstGet(i, &pathAction);
|
|
|
|
if (pathAction.ErrorDestination >= waypointCount) {
|
|
|
|
// waypoint id is out of range
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (pathAction.JumpDestination >= waypointCount) {
|
|
|
|
// waypoint id is out of range
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-14 20:49:19 +01:00
|
|
|
// path plan passed checks
|
2014-01-12 21:04:31 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-11 20:03:36 +01:00
|
|
|
// callback function when status changed, issue execution of state machine
|
|
|
|
void commandUpdated(__attribute__((unused)) UAVObjEvent *ev)
|
|
|
|
{
|
2014-02-02 22:08:16 +01:00
|
|
|
PIOS_CALLBACKSCHEDULER_Dispatch(pathDesiredUpdaterHandle);
|
2014-01-11 20:03:36 +01:00
|
|
|
}
|
|
|
|
|
2012-05-21 21:42:28 +02:00
|
|
|
// callback function when waypoints changed in any way, update pathDesired
|
2014-01-11 20:03:36 +01:00
|
|
|
void statusUpdated(__attribute__((unused)) UAVObjEvent *ev)
|
|
|
|
{
|
2014-02-02 22:08:16 +01:00
|
|
|
PIOS_CALLBACKSCHEDULER_Dispatch(pathPlannerHandle);
|
2014-01-11 20:03:36 +01:00
|
|
|
}
|
|
|
|
|
2015-04-27 15:01:47 +02:00
|
|
|
|
2012-05-21 21:42:28 +02:00
|
|
|
// helper function to go to a specific waypoint
|
2013-05-18 14:17:26 +02:00
|
|
|
static void setWaypoint(uint16_t num)
|
|
|
|
{
|
2014-01-13 22:21:34 +01:00
|
|
|
PathPlanData pathPlan;
|
2014-01-12 15:20:22 +01:00
|
|
|
|
2014-01-13 22:21:34 +01:00
|
|
|
PathPlanGet(&pathPlan);
|
2014-01-12 19:42:12 +01:00
|
|
|
|
2014-01-13 22:21:34 +01:00
|
|
|
// here it is assumed that the path plan has been validated (waypoint count is consistent)
|
|
|
|
if (num >= pathPlan.WaypointCount) {
|
2014-01-12 19:42:12 +01:00
|
|
|
// path plans wrap around
|
2013-05-18 14:17:26 +02:00
|
|
|
num = 0;
|
|
|
|
}
|
2012-05-21 21:42:28 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
waypointActive.Index = num;
|
|
|
|
WaypointActiveSet(&waypointActive);
|
2012-05-21 21:42:28 +02:00
|
|
|
}
|
|
|
|
|
2014-01-12 15:20:22 +01:00
|
|
|
// execute the appropriate condition and report result
|
2013-05-18 14:17:26 +02:00
|
|
|
static uint8_t pathConditionCheck()
|
|
|
|
{
|
2014-01-12 15:20:22 +01:00
|
|
|
// i thought about a lookup table, but a switch is safer considering there could be invalid EndCondition ID's
|
2013-05-18 14:17:26 +02:00
|
|
|
switch (pathAction.EndCondition) {
|
|
|
|
case PATHACTION_ENDCONDITION_NONE:
|
|
|
|
return conditionNone();
|
|
|
|
|
|
|
|
break;
|
|
|
|
case PATHACTION_ENDCONDITION_TIMEOUT:
|
|
|
|
return conditionTimeOut();
|
|
|
|
|
|
|
|
break;
|
|
|
|
case PATHACTION_ENDCONDITION_DISTANCETOTARGET:
|
|
|
|
return conditionDistanceToTarget();
|
|
|
|
|
|
|
|
break;
|
|
|
|
case PATHACTION_ENDCONDITION_LEGREMAINING:
|
|
|
|
return conditionLegRemaining();
|
|
|
|
|
|
|
|
break;
|
|
|
|
case PATHACTION_ENDCONDITION_BELOWERROR:
|
|
|
|
return conditionBelowError();
|
|
|
|
|
|
|
|
break;
|
|
|
|
case PATHACTION_ENDCONDITION_ABOVEALTITUDE:
|
|
|
|
return conditionAboveAltitude();
|
|
|
|
|
|
|
|
break;
|
|
|
|
case PATHACTION_ENDCONDITION_ABOVESPEED:
|
|
|
|
return conditionAboveSpeed();
|
|
|
|
|
|
|
|
break;
|
|
|
|
case PATHACTION_ENDCONDITION_POINTINGTOWARDSNEXT:
|
|
|
|
return conditionPointingTowardsNext();
|
|
|
|
|
|
|
|
break;
|
|
|
|
case PATHACTION_ENDCONDITION_PYTHONSCRIPT:
|
|
|
|
return conditionPythonScript();
|
|
|
|
|
|
|
|
break;
|
|
|
|
case PATHACTION_ENDCONDITION_IMMEDIATE:
|
|
|
|
return conditionImmediate();
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// invalid endconditions are always true to prevent freezes
|
|
|
|
return true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2012-05-21 21:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* the None condition is always false */
|
2013-05-18 14:17:26 +02:00
|
|
|
static uint8_t conditionNone()
|
|
|
|
{
|
|
|
|
return false;
|
2012-05-21 21:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the Timeout condition measures time this waypoint is active
|
|
|
|
* Parameter 0: timeout in seconds
|
|
|
|
*/
|
2013-05-18 14:17:26 +02:00
|
|
|
static uint8_t conditionTimeOut()
|
|
|
|
{
|
|
|
|
static uint16_t toWaypoint;
|
|
|
|
static uint32_t toStarttime;
|
|
|
|
|
|
|
|
// reset timer if waypoint changed
|
|
|
|
if (waypointActive.Index != toWaypoint) {
|
|
|
|
toWaypoint = waypointActive.Index;
|
|
|
|
toStarttime = PIOS_DELAY_GetRaw();
|
|
|
|
}
|
|
|
|
if (PIOS_DELAY_DiffuS(toStarttime) >= 1e6f * pathAction.ConditionParameters[0]) {
|
|
|
|
// make sure we reinitialize even if the same waypoint comes twice
|
|
|
|
toWaypoint = 0xFFFF;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-05-21 21:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the DistanceToTarget measures distance to a waypoint
|
|
|
|
* returns true if closer
|
|
|
|
* Parameter 0: distance in meters
|
|
|
|
* Parameter 1: flag: 0=2d 1=3d
|
|
|
|
*/
|
2013-05-18 14:17:26 +02:00
|
|
|
static uint8_t conditionDistanceToTarget()
|
|
|
|
{
|
|
|
|
float distance;
|
2013-05-18 19:36:45 +02:00
|
|
|
PositionStateData positionState;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
PositionStateGet(&positionState);
|
2013-05-18 14:17:26 +02:00
|
|
|
if (pathAction.ConditionParameters[1] > 0.5f) {
|
2013-09-01 12:10:55 +02:00
|
|
|
distance = sqrtf(powf(waypoint.Position.North - positionState.North, 2)
|
|
|
|
+ powf(waypoint.Position.East - positionState.East, 2)
|
|
|
|
+ powf(waypoint.Position.Down - positionState.Down, 2));
|
2013-05-18 14:17:26 +02:00
|
|
|
} else {
|
2013-09-01 12:10:55 +02:00
|
|
|
distance = sqrtf(powf(waypoint.Position.North - positionState.North, 2)
|
|
|
|
+ powf(waypoint.Position.East - positionState.East, 2));
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (distance <= pathAction.ConditionParameters[0]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-05-21 21:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-05-18 14:17:26 +02:00
|
|
|
* the LegRemaining measures how far the pathfollower got on a linear path segment
|
2012-05-21 21:42:28 +02:00
|
|
|
* returns true if closer to destination (path more complete)
|
|
|
|
* Parameter 0: relative distance (0= complete, 1= just starting)
|
|
|
|
*/
|
2013-05-18 14:17:26 +02:00
|
|
|
static uint8_t conditionLegRemaining()
|
|
|
|
{
|
2015-04-17 15:06:02 +02:00
|
|
|
PathStatusData pathStatus;
|
2015-04-20 00:43:57 +02:00
|
|
|
|
2015-04-17 15:06:02 +02:00
|
|
|
PathStatusGet(&pathStatus);
|
2012-05-21 21:42:28 +02:00
|
|
|
|
2015-04-17 15:06:02 +02:00
|
|
|
if (pathStatus.fractional_progress >= (1.0f - pathAction.ConditionParameters[0])) {
|
2013-05-18 14:17:26 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-05-21 21:42:28 +02:00
|
|
|
}
|
|
|
|
|
2012-05-28 01:51:17 +02:00
|
|
|
/**
|
2013-05-18 14:17:26 +02:00
|
|
|
* the BelowError measures the error on a path segment
|
2012-05-28 01:51:17 +02:00
|
|
|
* returns true if error is below margin
|
|
|
|
* Parameter 0: error margin (in m)
|
|
|
|
*/
|
2013-05-18 14:17:26 +02:00
|
|
|
static uint8_t conditionBelowError()
|
|
|
|
{
|
|
|
|
PathDesiredData pathDesired;
|
2013-05-18 19:36:45 +02:00
|
|
|
PositionStateData positionState;
|
2012-05-28 01:51:17 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
PathDesiredGet(&pathDesired);
|
2013-05-18 19:36:45 +02:00
|
|
|
PositionStateGet(&positionState);
|
2012-05-28 01:51:17 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
float cur[3] = { positionState.North, positionState.East, positionState.Down };
|
2013-05-18 14:17:26 +02:00
|
|
|
struct path_status progress;
|
2012-05-28 01:51:17 +02:00
|
|
|
|
2014-08-15 17:56:57 +02:00
|
|
|
path_progress(&pathDesired,
|
2015-03-20 11:42:20 +01:00
|
|
|
cur, &progress, mode3D);
|
2013-05-18 14:17:26 +02:00
|
|
|
if (progress.error <= pathAction.ConditionParameters[0]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-05-28 01:51:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-21 21:42:28 +02:00
|
|
|
/**
|
2013-05-18 14:17:26 +02:00
|
|
|
* the AboveAltitude measures the flight altitude relative to home position
|
2012-05-21 21:42:28 +02:00
|
|
|
* returns true if above critical altitude
|
|
|
|
* WARNING! Altitudes are always negative (down coordinate)
|
|
|
|
* Parameter 0: altitude in meters (negative!)
|
|
|
|
*/
|
2013-05-18 14:17:26 +02:00
|
|
|
static uint8_t conditionAboveAltitude()
|
|
|
|
{
|
2013-05-18 19:36:45 +02:00
|
|
|
PositionStateData positionState;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
PositionStateGet(&positionState);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
if (positionState.Down <= pathAction.ConditionParameters[0]) {
|
2013-05-18 14:17:26 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-05-21 21:42:28 +02:00
|
|
|
}
|
|
|
|
|
2012-05-28 01:51:17 +02:00
|
|
|
/**
|
2013-05-18 14:17:26 +02:00
|
|
|
* the AboveSpeed measures the movement speed (3d)
|
2012-05-28 01:51:17 +02:00
|
|
|
* returns true if above critical speed
|
|
|
|
* Parameter 0: speed in m/s
|
|
|
|
* Parameter 1: flag: 0=groundspeed 1=airspeed
|
|
|
|
*/
|
2013-05-18 14:17:26 +02:00
|
|
|
static uint8_t conditionAboveSpeed()
|
|
|
|
{
|
2013-05-18 19:36:45 +02:00
|
|
|
VelocityStateData velocityState;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
VelocityStateGet(&velocityState);
|
|
|
|
float velocity = sqrtf(velocityState.North * velocityState.North + velocityState.East * velocityState.East + velocityState.Down * velocityState.Down);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// use airspeed if requested and available
|
|
|
|
if (pathAction.ConditionParameters[1] > 0.5f) {
|
2013-05-18 19:36:45 +02:00
|
|
|
AirspeedStateData airspeed;
|
|
|
|
AirspeedStateGet(&airspeed);
|
2013-05-18 14:17:26 +02:00
|
|
|
velocity = airspeed.CalibratedAirspeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (velocity >= pathAction.ConditionParameters[0]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-05-28 01:51:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-21 21:42:28 +02:00
|
|
|
/**
|
|
|
|
* the PointingTowardsNext measures the horizontal movement vector direction relative to the next waypoint
|
|
|
|
* regardless whether this waypoint will ever be active (Command could jump to another one on true)
|
|
|
|
* This is useful for curve segments where the craft should stop circling when facing a certain way (usually the next waypoint)
|
|
|
|
* returns true if within a certain angular margin
|
|
|
|
* Parameter 0: degrees variation allowed
|
|
|
|
*/
|
2013-05-18 14:17:26 +02:00
|
|
|
static uint8_t conditionPointingTowardsNext()
|
|
|
|
{
|
|
|
|
uint16_t nextWaypointId = waypointActive.Index + 1;
|
|
|
|
|
|
|
|
if (nextWaypointId >= UAVObjGetNumInstances(WaypointHandle())) {
|
|
|
|
nextWaypointId = 0;
|
|
|
|
}
|
|
|
|
WaypointData nextWaypoint;
|
|
|
|
WaypointInstGet(nextWaypointId, &nextWaypoint);
|
|
|
|
|
2013-09-01 12:10:55 +02:00
|
|
|
float angle1 = atan2f((nextWaypoint.Position.North - waypoint.Position.North), (nextWaypoint.Position.East - waypoint.Position.East));
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
VelocityStateData velocity;
|
|
|
|
VelocityStateGet(&velocity);
|
2013-05-18 14:17:26 +02:00
|
|
|
float angle2 = atan2f(velocity.North, velocity.East);
|
|
|
|
|
|
|
|
// calculate the absolute angular difference
|
|
|
|
angle1 = fabsf(RAD2DEG(angle1 - angle2));
|
|
|
|
while (angle1 > 360) {
|
|
|
|
angle1 -= 360;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (angle1 <= pathAction.ConditionParameters[0]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-05-21 21:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the PythonScript is supposed to read the output of a PyMite program running at the same time
|
|
|
|
* and return based on its output, likely read out through some to be defined UAVObject
|
|
|
|
* TODO XXX NOT YET IMPLEMENTED
|
|
|
|
* returns always true until implemented
|
|
|
|
* Parameter 0-3: defined by user script
|
|
|
|
*/
|
2013-05-18 14:17:26 +02:00
|
|
|
static uint8_t conditionPythonScript()
|
|
|
|
{
|
|
|
|
return true;
|
2012-05-21 21:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* the immediate condition is always true */
|
2013-05-18 14:17:26 +02:00
|
|
|
static uint8_t conditionImmediate()
|
|
|
|
{
|
|
|
|
return true;
|
2012-05-21 21:42:28 +02:00
|
|
|
}
|
|
|
|
|
2012-03-11 12:31:38 +01:00
|
|
|
/**
|
2012-03-15 18:28:21 +01:00
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|