2010-09-27 09:28:34 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
2012-05-08 07:25:59 +02:00
|
|
|
* @file vtolpathfollower.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
|
2013-05-18 19:36:45 +02:00
|
|
|
* @brief This module compared @ref PositionState to @ref PathDesired
|
2012-06-06 16:12:36 +02:00
|
|
|
* and sets @ref Stabilization. It only does this when the FlightMode field
|
|
|
|
* of @ref FlightStatus is PathPlanner or RTH.
|
2010-09-27 09:28:34 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-06-06 16:12:36 +02:00
|
|
|
* Input object: FlightStatus
|
|
|
|
* Input object: PathDesired
|
2013-05-18 19:36:45 +02:00
|
|
|
* Input object: PositionState
|
2012-06-06 16:12:36 +02:00
|
|
|
* Output object: StabilizationDesired
|
2010-09-27 09:28:34 +02:00
|
|
|
*
|
2013-05-18 14:17:26 +02:00
|
|
|
* This module will periodically update the value of the @ref StabilizationDesired object based on
|
2013-05-18 19:36:45 +02:00
|
|
|
* @ref PathDesired and @PositionState when the Flight Mode selected in @FlightStatus is supported
|
2012-06-06 16:12:36 +02:00
|
|
|
* by this module. Otherwise another module (e.g. @ref ManualControlCommand) is expected to be
|
|
|
|
* writing to @ref StabilizationDesired.
|
2010-09-27 09:28:34 +02:00
|
|
|
*
|
|
|
|
* The module executes in its own thread in this example.
|
|
|
|
*
|
|
|
|
* Modules have no API, all communication to other modules is done through UAVObjects.
|
|
|
|
* However modules may use the API exposed by shared libraries.
|
|
|
|
* See the OpenPilot wiki for more details.
|
|
|
|
* http://www.openpilot.org/OpenPilot_Application_Architecture
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-05-02 23:31:14 +02:00
|
|
|
#include <openpilot.h>
|
2012-04-10 05:39:41 +02:00
|
|
|
|
2012-05-08 07:25:59 +02:00
|
|
|
#include "vtolpathfollower.h"
|
2013-05-02 23:31:14 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
#include "accelstate.h"
|
|
|
|
#include "attitudestate.h"
|
2012-06-06 08:33:30 +02:00
|
|
|
#include "hwsettings.h"
|
2013-05-18 14:17:26 +02:00
|
|
|
#include "pathdesired.h" // object that will be updated by the module
|
2013-05-18 19:36:45 +02:00
|
|
|
#include "positionstate.h"
|
2011-03-02 02:25:44 +01:00
|
|
|
#include "manualcontrol.h"
|
2011-05-09 18:37:06 +02:00
|
|
|
#include "flightstatus.h"
|
2012-05-21 11:25:57 +02:00
|
|
|
#include "pathstatus.h"
|
2013-05-22 21:45:06 +02:00
|
|
|
#include "gpsvelocitysensor.h"
|
|
|
|
#include "gpspositionsensor.h"
|
2012-11-06 10:13:09 +01:00
|
|
|
#include "homelocation.h"
|
2012-05-24 11:09:53 +02:00
|
|
|
#include "vtolpathfollowersettings.h"
|
2011-01-14 01:51:54 +01:00
|
|
|
#include "nedaccel.h"
|
2011-03-02 02:25:27 +01:00
|
|
|
#include "stabilizationdesired.h"
|
2010-09-27 09:28:34 +02:00
|
|
|
#include "stabilizationsettings.h"
|
|
|
|
#include "systemsettings.h"
|
|
|
|
#include "velocitydesired.h"
|
2013-05-18 19:36:45 +02:00
|
|
|
#include "velocitystate.h"
|
2013-05-02 23:31:14 +02:00
|
|
|
#include "taskinfo.h"
|
|
|
|
|
|
|
|
#include "paths.h"
|
2011-01-14 01:51:54 +01:00
|
|
|
#include "CoordinateConversions.h"
|
2010-09-27 09:28:34 +02:00
|
|
|
|
2013-03-28 17:29:26 +01:00
|
|
|
#include "cameradesired.h"
|
|
|
|
#include "poilearnsettings.h"
|
|
|
|
#include "poilocation.h"
|
|
|
|
#include "accessorydesired.h"
|
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
// Private constants
|
2013-05-18 14:17:26 +02:00
|
|
|
#define MAX_QUEUE_SIZE 4
|
2012-03-10 07:51:57 +01:00
|
|
|
#define STACK_SIZE_BYTES 1548
|
2013-05-18 14:17:26 +02:00
|
|
|
#define TASK_PRIORITY (tskIDLE_PRIORITY + 2)
|
2012-05-08 09:28:19 +02:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
// Private types
|
|
|
|
|
|
|
|
// Private variables
|
2012-05-08 07:25:59 +02:00
|
|
|
static xTaskHandle pathfollowerTaskHandle;
|
2013-03-28 17:29:26 +01:00
|
|
|
static PathStatusData pathStatus;
|
2012-05-24 11:09:53 +02:00
|
|
|
static VtolPathFollowerSettingsData vtolpathfollowerSettings;
|
2013-04-14 11:23:47 +02:00
|
|
|
static float poiRadius;
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
// Private functions
|
2012-05-08 07:25:59 +02:00
|
|
|
static void vtolPathFollowerTask(void *parameters);
|
2013-05-18 14:17:26 +02:00
|
|
|
static void SettingsUpdatedCb(UAVObjEvent *ev);
|
2012-04-10 05:39:41 +02:00
|
|
|
static void updateNedAccel();
|
2013-03-28 17:29:26 +01:00
|
|
|
static void updatePOIBearing();
|
2012-04-10 05:39:41 +02:00
|
|
|
static void updatePathVelocity();
|
2012-05-08 09:28:19 +02:00
|
|
|
static void updateEndpointVelocity();
|
2013-05-18 14:17:26 +02:00
|
|
|
static void updateFixedAttitude(float *attitude);
|
2013-03-28 17:29:26 +01:00
|
|
|
static void updateVtolDesiredAttitude(bool yaw_attitude);
|
2012-05-08 09:28:19 +02:00
|
|
|
static float bound(float val, float min, float max);
|
2012-06-06 08:33:30 +02:00
|
|
|
static bool vtolpathfollower_enabled;
|
2013-05-18 14:17:26 +02:00
|
|
|
static void accessoryUpdated(UAVObjEvent *ev);
|
2012-04-10 05:39:41 +02:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
/**
|
|
|
|
* Initialise the module, called on startup
|
|
|
|
* \returns 0 on success or -1 if initialisation failed
|
|
|
|
*/
|
2012-05-08 07:25:59 +02:00
|
|
|
int32_t VtolPathFollowerStart()
|
2011-06-20 07:35:40 +02:00
|
|
|
{
|
2013-04-14 11:23:47 +02:00
|
|
|
if (vtolpathfollower_enabled) {
|
|
|
|
// Start main task
|
2013-05-06 11:41:14 +02:00
|
|
|
xTaskCreate(vtolPathFollowerTask, (signed char *)"VtolPathFollower", STACK_SIZE_BYTES / 4, NULL, TASK_PRIORITY, &pathfollowerTaskHandle);
|
|
|
|
PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_PATHFOLLOWER, pathfollowerTaskHandle);
|
2013-04-14 11:23:47 +02:00
|
|
|
}
|
2011-06-20 07:35:40 +02:00
|
|
|
|
2013-04-14 11:23:47 +02:00
|
|
|
return 0;
|
2011-06-20 07:35:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise the module, called on startup
|
|
|
|
* \returns 0 on success or -1 if initialisation failed
|
|
|
|
*/
|
2012-05-08 07:25:59 +02:00
|
|
|
int32_t VtolPathFollowerInitialize()
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
2013-04-14 11:23:47 +02:00
|
|
|
uint8_t optionalModules[HWSETTINGS_OPTIONALMODULES_NUMELEM];
|
|
|
|
|
|
|
|
HwSettingsOptionalModulesGet(optionalModules);
|
|
|
|
|
|
|
|
if (optionalModules[HWSETTINGS_OPTIONALMODULES_VTOLPATHFOLLOWER] == HWSETTINGS_OPTIONALMODULES_ENABLED) {
|
|
|
|
VtolPathFollowerSettingsInitialize();
|
|
|
|
NedAccelInitialize();
|
|
|
|
PathDesiredInitialize();
|
|
|
|
PathStatusInitialize();
|
|
|
|
VelocityDesiredInitialize();
|
|
|
|
CameraDesiredInitialize();
|
|
|
|
AccessoryDesiredInitialize();
|
|
|
|
PoiLearnSettingsInitialize();
|
|
|
|
PoiLocationInitialize();
|
|
|
|
vtolpathfollower_enabled = true;
|
|
|
|
} else {
|
|
|
|
vtolpathfollower_enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
2012-05-08 09:28:19 +02:00
|
|
|
|
2013-06-04 05:37:40 +02:00
|
|
|
MODULE_INITCALL(VtolPathFollowerInitialize, VtolPathFollowerStart);
|
2010-09-27 09:28:34 +02:00
|
|
|
|
2011-03-26 12:40:33 +01:00
|
|
|
static float northVelIntegral = 0;
|
2013-05-18 14:17:26 +02:00
|
|
|
static float eastVelIntegral = 0;
|
|
|
|
static float downVelIntegral = 0;
|
2011-03-26 12:40:33 +01:00
|
|
|
|
|
|
|
static float northPosIntegral = 0;
|
2013-05-18 14:17:26 +02:00
|
|
|
static float eastPosIntegral = 0;
|
|
|
|
static float downPosIntegral = 0;
|
2011-03-26 12:40:33 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
static float throttleOffset = 0;
|
2010-09-27 09:28:34 +02:00
|
|
|
/**
|
|
|
|
* Module thread, should not return.
|
|
|
|
*/
|
2013-05-05 09:02:24 +02:00
|
|
|
static void vtolPathFollowerTask(__attribute__((unused)) void *parameters)
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
2013-04-14 11:23:47 +02:00
|
|
|
SystemSettingsData systemSettings;
|
|
|
|
FlightStatusData flightStatus;
|
|
|
|
|
|
|
|
portTickType lastUpdateTime;
|
|
|
|
|
|
|
|
VtolPathFollowerSettingsConnectCallback(SettingsUpdatedCb);
|
|
|
|
AccessoryDesiredConnectCallback(accessoryUpdated);
|
|
|
|
|
|
|
|
VtolPathFollowerSettingsGet(&vtolpathfollowerSettings);
|
|
|
|
|
|
|
|
// Main task loop
|
|
|
|
lastUpdateTime = xTaskGetTickCount();
|
|
|
|
while (1) {
|
|
|
|
// Conditions when this runs:
|
|
|
|
// 1. Must have VTOL type airframe
|
|
|
|
// 2. Flight mode is PositionHold and PathDesired.Mode is Endpoint OR
|
2013-05-18 14:17:26 +02:00
|
|
|
// FlightMode is PathPlanner and PathDesired.Mode is Endpoint or Path
|
2013-04-14 11:23:47 +02:00
|
|
|
|
|
|
|
SystemSettingsGet(&systemSettings);
|
|
|
|
if ((systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_VTOL) && (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_QUADP)
|
2013-05-18 14:17:26 +02:00
|
|
|
&& (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_OCTOCOAXX) && (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_QUADX)
|
|
|
|
&& (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_HEXA) && (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_HEXAX)
|
|
|
|
&& (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_HEXACOAX) && (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_OCTO)
|
|
|
|
&& (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_OCTOV) && (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_OCTOCOAXP)
|
|
|
|
&& (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_TRI)) {
|
2013-04-14 11:23:47 +02:00
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_GUIDANCE, SYSTEMALARMS_ALARM_WARNING);
|
|
|
|
vTaskDelay(1000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continue collecting data if not enough time
|
|
|
|
vTaskDelayUntil(&lastUpdateTime, vtolpathfollowerSettings.UpdatePeriod / portTICK_RATE_MS);
|
|
|
|
|
|
|
|
// Convert the accels into the NED frame
|
|
|
|
updateNedAccel();
|
|
|
|
|
|
|
|
FlightStatusGet(&flightStatus);
|
|
|
|
PathStatusGet(&pathStatus);
|
|
|
|
PathDesiredData pathDesired;
|
|
|
|
PathDesiredGet(&pathDesired);
|
|
|
|
|
|
|
|
// Check the combinations of flightmode and pathdesired mode
|
|
|
|
switch (flightStatus.FlightMode) {
|
2013-05-18 14:17:26 +02:00
|
|
|
case FLIGHTSTATUS_FLIGHTMODE_LAND:
|
|
|
|
case FLIGHTSTATUS_FLIGHTMODE_POSITIONHOLD:
|
|
|
|
case FLIGHTSTATUS_FLIGHTMODE_RETURNTOBASE:
|
|
|
|
if (pathDesired.Mode == PATHDESIRED_MODE_FLYENDPOINT) {
|
|
|
|
updateEndpointVelocity();
|
|
|
|
updateVtolDesiredAttitude(false);
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_GUIDANCE, SYSTEMALARMS_ALARM_OK);
|
|
|
|
} else {
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_GUIDANCE, SYSTEMALARMS_ALARM_ERROR);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FLIGHTSTATUS_FLIGHTMODE_PATHPLANNER:
|
|
|
|
pathStatus.UID = pathDesired.UID;
|
|
|
|
pathStatus.Status = PATHSTATUS_STATUS_INPROGRESS;
|
|
|
|
switch (pathDesired.Mode) {
|
|
|
|
// TODO: Make updateVtolDesiredAttitude and velocity report success and update PATHSTATUS_STATUS accordingly
|
|
|
|
case PATHDESIRED_MODE_FLYENDPOINT:
|
|
|
|
case PATHDESIRED_MODE_FLYVECTOR:
|
|
|
|
case PATHDESIRED_MODE_FLYCIRCLERIGHT:
|
|
|
|
case PATHDESIRED_MODE_FLYCIRCLELEFT:
|
|
|
|
updatePathVelocity();
|
|
|
|
updateVtolDesiredAttitude(false);
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_GUIDANCE, SYSTEMALARMS_ALARM_OK);
|
2013-04-14 11:23:47 +02:00
|
|
|
break;
|
2013-05-18 14:17:26 +02:00
|
|
|
case PATHDESIRED_MODE_FIXEDATTITUDE:
|
|
|
|
updateFixedAttitude(pathDesired.ModeParameters);
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_GUIDANCE, SYSTEMALARMS_ALARM_OK);
|
2013-04-14 11:23:47 +02:00
|
|
|
break;
|
2013-05-18 14:17:26 +02:00
|
|
|
case PATHDESIRED_MODE_DISARMALARM:
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_GUIDANCE, SYSTEMALARMS_ALARM_CRITICAL);
|
2013-04-14 11:23:47 +02:00
|
|
|
break;
|
|
|
|
default:
|
2013-05-18 14:17:26 +02:00
|
|
|
pathStatus.Status = PATHSTATUS_STATUS_CRITICAL;
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_GUIDANCE, SYSTEMALARMS_ALARM_ERROR);
|
2013-04-14 11:23:47 +02:00
|
|
|
break;
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
|
|
|
PathStatusSet(&pathStatus);
|
|
|
|
break;
|
|
|
|
case FLIGHTSTATUS_FLIGHTMODE_POI:
|
|
|
|
if (pathDesired.Mode == PATHDESIRED_MODE_FLYENDPOINT) {
|
|
|
|
updateEndpointVelocity();
|
|
|
|
updateVtolDesiredAttitude(true);
|
|
|
|
updatePOIBearing();
|
|
|
|
} else {
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_GUIDANCE, SYSTEMALARMS_ALARM_ERROR);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Be cleaner and get rid of global variables
|
|
|
|
northVelIntegral = 0;
|
|
|
|
eastVelIntegral = 0;
|
|
|
|
downVelIntegral = 0;
|
|
|
|
northPosIntegral = 0;
|
|
|
|
eastPosIntegral = 0;
|
|
|
|
downPosIntegral = 0;
|
|
|
|
|
|
|
|
// Track throttle before engaging this mode. Cheap system ident
|
|
|
|
StabilizationDesiredData stabDesired;
|
|
|
|
StabilizationDesiredGet(&stabDesired);
|
|
|
|
throttleOffset = stabDesired.Throttle;
|
|
|
|
|
|
|
|
break;
|
2013-04-14 11:23:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_GUIDANCE);
|
|
|
|
}
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
2013-03-28 17:29:26 +01:00
|
|
|
/**
|
|
|
|
* Compute bearing and elevation between current position and POI
|
|
|
|
*/
|
|
|
|
static void updatePOIBearing()
|
|
|
|
{
|
2013-05-04 03:42:44 +02:00
|
|
|
const float DEADBAND_HIGH = 0.10f;
|
2013-05-18 14:17:26 +02:00
|
|
|
const float DEADBAND_LOW = -0.10f;
|
2013-04-14 11:23:47 +02:00
|
|
|
float dT = vtolpathfollowerSettings.UpdatePeriod / 1000.0f;
|
|
|
|
|
|
|
|
PathDesiredData pathDesired;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-04-14 11:23:47 +02:00
|
|
|
PathDesiredGet(&pathDesired);
|
2013-05-18 19:36:45 +02:00
|
|
|
PositionStateData positionState;
|
|
|
|
PositionStateGet(&positionState);
|
2013-04-14 11:23:47 +02:00
|
|
|
CameraDesiredData cameraDesired;
|
|
|
|
CameraDesiredGet(&cameraDesired);
|
|
|
|
StabilizationDesiredData stabDesired;
|
|
|
|
StabilizationDesiredGet(&stabDesired);
|
|
|
|
PoiLocationData poi;
|
|
|
|
PoiLocationGet(&poi);
|
|
|
|
|
|
|
|
float dLoc[3];
|
|
|
|
float yaw = 0;
|
2013-05-04 03:42:44 +02:00
|
|
|
/*float elevation = 0;*/
|
2013-04-14 11:23:47 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
dLoc[0] = positionState.North - poi.North;
|
|
|
|
dLoc[1] = positionState.East - poi.East;
|
|
|
|
dLoc[2] = positionState.Down - poi.Down;
|
2013-04-14 11:23:47 +02:00
|
|
|
|
|
|
|
if (dLoc[1] < 0) {
|
2013-05-18 14:17:26 +02:00
|
|
|
yaw = RAD2DEG(atan2f(dLoc[1], dLoc[0])) + 180.0f;
|
2013-04-14 11:23:47 +02:00
|
|
|
} else {
|
2013-05-18 14:17:26 +02:00
|
|
|
yaw = RAD2DEG(atan2f(dLoc[1], dLoc[0])) - 180.0f;
|
2013-04-14 11:23:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// distance
|
2013-05-04 03:42:44 +02:00
|
|
|
float distance = sqrtf(powf(dLoc[0], 2.0f) + powf(dLoc[1], 2.0f));
|
2013-04-14 11:23:47 +02:00
|
|
|
|
|
|
|
ManualControlCommandData manualControlData;
|
|
|
|
ManualControlCommandGet(&manualControlData);
|
|
|
|
|
|
|
|
float changeRadius = 0;
|
|
|
|
// Move closer or further, radially
|
|
|
|
if (manualControlData.Pitch > DEADBAND_HIGH) {
|
|
|
|
changeRadius = (manualControlData.Pitch - DEADBAND_HIGH) * dT * 100.0f;
|
|
|
|
} else if (manualControlData.Pitch < DEADBAND_LOW) {
|
|
|
|
changeRadius = (manualControlData.Pitch - DEADBAND_LOW) * dT * 100.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// move along circular path
|
|
|
|
float pathAngle = 0;
|
|
|
|
if (manualControlData.Roll > DEADBAND_HIGH) {
|
|
|
|
pathAngle = -(manualControlData.Roll - DEADBAND_HIGH) * dT * 300.0f;
|
|
|
|
} else if (manualControlData.Roll < DEADBAND_LOW) {
|
|
|
|
pathAngle = -(manualControlData.Roll - DEADBAND_LOW) * dT * 300.0f;
|
|
|
|
} else if (manualControlData.Roll >= DEADBAND_LOW && manualControlData.Roll <= DEADBAND_HIGH) {
|
|
|
|
// change radius only when not circling
|
|
|
|
poiRadius = distance + changeRadius;
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't try to move any closer
|
|
|
|
if (poiRadius >= 3.0f || changeRadius > 0) {
|
2013-05-04 03:42:44 +02:00
|
|
|
if (fabsf(pathAngle) > 0.0f || fabsf(changeRadius) > 0.0f) {
|
2013-04-27 15:30:02 +02:00
|
|
|
pathDesired.End[PATHDESIRED_END_NORTH] = poi.North + (poiRadius * cosf(DEG2RAD(pathAngle + yaw - 180.0f)));
|
2013-05-18 14:17:26 +02:00
|
|
|
pathDesired.End[PATHDESIRED_END_EAST] = poi.East + (poiRadius * sinf(DEG2RAD(pathAngle + yaw - 180.0f)));
|
2013-05-04 03:42:44 +02:00
|
|
|
pathDesired.StartingVelocity = 1.0f;
|
2013-05-18 14:17:26 +02:00
|
|
|
pathDesired.EndingVelocity = 0.0f;
|
2013-04-14 11:23:47 +02:00
|
|
|
pathDesired.Mode = PATHDESIRED_MODE_FLYENDPOINT;
|
|
|
|
PathDesiredSet(&pathDesired);
|
|
|
|
}
|
|
|
|
}
|
2013-05-18 14:17:26 +02:00
|
|
|
// not above
|
2013-04-14 11:23:47 +02:00
|
|
|
if (distance >= 3.0f) {
|
2013-05-18 14:17:26 +02:00
|
|
|
// You can feed this into camerastabilization
|
2013-05-04 03:42:44 +02:00
|
|
|
/*elevation = RAD2DEG(atan2f(dLoc[2],distance));*/
|
2013-04-14 11:23:47 +02:00
|
|
|
|
|
|
|
stabDesired.Yaw = yaw + (pathAngle / 2.0f);
|
|
|
|
stabDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_YAW] = STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE;
|
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
// cameraDesired.Yaw=yaw;
|
|
|
|
// cameraDesired.PitchOrServo2=elevation;
|
2013-04-14 11:23:47 +02:00
|
|
|
|
|
|
|
CameraDesiredSet(&cameraDesired);
|
|
|
|
StabilizationDesiredSet(&stabDesired);
|
|
|
|
}
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
2012-04-10 05:39:41 +02:00
|
|
|
/**
|
|
|
|
* Compute desired velocity from the current position and path
|
|
|
|
*
|
2013-05-18 19:36:45 +02:00
|
|
|
* Takes in @ref PositionState and compares it to @ref PathDesired
|
2012-04-10 05:39:41 +02:00
|
|
|
* and computes @ref VelocityDesired
|
|
|
|
*/
|
|
|
|
static void updatePathVelocity()
|
|
|
|
{
|
2013-04-14 11:23:47 +02:00
|
|
|
float dT = vtolpathfollowerSettings.UpdatePeriod / 1000.0f;
|
|
|
|
float downCommand;
|
|
|
|
|
|
|
|
PathDesiredData pathDesired;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-04-14 11:23:47 +02:00
|
|
|
PathDesiredGet(&pathDesired);
|
2013-05-18 19:36:45 +02:00
|
|
|
PositionStateData positionState;
|
|
|
|
PositionStateGet(&positionState);
|
2013-04-14 11:23:47 +02:00
|
|
|
|
|
|
|
float cur[3] =
|
2013-05-18 19:36:45 +02:00
|
|
|
{ positionState.North, positionState.East, positionState.Down };
|
2013-04-14 11:23:47 +02:00
|
|
|
struct path_status progress;
|
|
|
|
|
|
|
|
path_progress(pathDesired.Start, pathDesired.End, cur, &progress, pathDesired.Mode);
|
|
|
|
|
|
|
|
float groundspeed;
|
|
|
|
switch (pathDesired.Mode) {
|
2013-05-18 14:17:26 +02:00
|
|
|
case PATHDESIRED_MODE_FLYCIRCLERIGHT:
|
|
|
|
case PATHDESIRED_MODE_DRIVECIRCLERIGHT:
|
|
|
|
case PATHDESIRED_MODE_FLYCIRCLELEFT:
|
|
|
|
case PATHDESIRED_MODE_DRIVECIRCLELEFT:
|
|
|
|
groundspeed = pathDesired.EndingVelocity;
|
|
|
|
break;
|
|
|
|
case PATHDESIRED_MODE_FLYENDPOINT:
|
|
|
|
case PATHDESIRED_MODE_DRIVEENDPOINT:
|
|
|
|
groundspeed = pathDesired.EndingVelocity - pathDesired.EndingVelocity * bound(progress.fractional_progress, 0, 1);
|
|
|
|
if (progress.fractional_progress > 1) {
|
|
|
|
groundspeed = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PATHDESIRED_MODE_FLYVECTOR:
|
|
|
|
case PATHDESIRED_MODE_DRIVEVECTOR:
|
|
|
|
default:
|
|
|
|
groundspeed = pathDesired.StartingVelocity
|
|
|
|
+ (pathDesired.EndingVelocity - pathDesired.StartingVelocity) * bound(progress.fractional_progress, 0, 1);
|
|
|
|
if (progress.fractional_progress > 1) {
|
|
|
|
groundspeed = 0;
|
|
|
|
}
|
|
|
|
break;
|
2013-04-14 11:23:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VelocityDesiredData velocityDesired;
|
|
|
|
velocityDesired.North = progress.path_direction[0] * groundspeed;
|
2013-05-18 14:17:26 +02:00
|
|
|
velocityDesired.East = progress.path_direction[1] * groundspeed;
|
2013-04-14 11:23:47 +02:00
|
|
|
|
|
|
|
float error_speed = progress.error * vtolpathfollowerSettings.HorizontalPosPI[VTOLPATHFOLLOWERSETTINGS_HORIZONTALPOSPI_KP];
|
|
|
|
float correction_velocity[2] =
|
|
|
|
{ progress.correction_direction[0] * error_speed, progress.correction_direction[1] * error_speed };
|
|
|
|
|
|
|
|
float total_vel = sqrtf(powf(correction_velocity[0], 2) + powf(correction_velocity[1], 2));
|
2013-05-18 14:17:26 +02:00
|
|
|
float scale = 1;
|
|
|
|
if (total_vel > vtolpathfollowerSettings.HorizontalVelMax) {
|
2013-04-14 11:23:47 +02:00
|
|
|
scale = vtolpathfollowerSettings.HorizontalVelMax / total_vel;
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
2013-04-14 11:23:47 +02:00
|
|
|
|
|
|
|
velocityDesired.North += progress.correction_direction[0] * error_speed * scale;
|
2013-05-18 14:17:26 +02:00
|
|
|
velocityDesired.East += progress.correction_direction[1] * error_speed * scale;
|
2013-04-14 11:23:47 +02:00
|
|
|
|
|
|
|
float altitudeSetpoint = pathDesired.Start[2] + (pathDesired.End[2] - pathDesired.Start[2]) * bound(progress.fractional_progress, 0, 1);
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
float downError = altitudeSetpoint - positionState.Down;
|
2013-04-14 11:23:47 +02:00
|
|
|
downPosIntegral = bound(downPosIntegral + downError * dT * vtolpathfollowerSettings.VerticalPosPI[VTOLPATHFOLLOWERSETTINGS_VERTICALPOSPI_KI],
|
2013-05-18 14:17:26 +02:00
|
|
|
-vtolpathfollowerSettings.VerticalPosPI[VTOLPATHFOLLOWERSETTINGS_VERTICALPOSPI_ILIMIT],
|
|
|
|
vtolpathfollowerSettings.VerticalPosPI[VTOLPATHFOLLOWERSETTINGS_VERTICALPOSPI_ILIMIT]);
|
|
|
|
downCommand = (downError * vtolpathfollowerSettings.VerticalPosPI[VTOLPATHFOLLOWERSETTINGS_VERTICALPOSPI_KP] + downPosIntegral);
|
2013-04-14 11:23:47 +02:00
|
|
|
velocityDesired.Down = bound(downCommand, -vtolpathfollowerSettings.VerticalVelMax, vtolpathfollowerSettings.VerticalVelMax);
|
|
|
|
|
|
|
|
// update pathstatus
|
2013-05-18 14:17:26 +02:00
|
|
|
pathStatus.error = progress.error;
|
2013-04-14 11:23:47 +02:00
|
|
|
pathStatus.fractional_progress = progress.fractional_progress;
|
|
|
|
|
|
|
|
VelocityDesiredSet(&velocityDesired);
|
2012-04-10 05:39:41 +02:00
|
|
|
}
|
|
|
|
|
2011-01-14 01:51:57 +01:00
|
|
|
/**
|
|
|
|
* Compute desired velocity from the current position
|
|
|
|
*
|
2013-05-18 19:36:45 +02:00
|
|
|
* Takes in @ref PositionState and compares it to @ref PositionDesired
|
2011-01-14 01:51:57 +01:00
|
|
|
* and computes @ref VelocityDesired
|
|
|
|
*/
|
2012-05-08 09:28:19 +02:00
|
|
|
void updateEndpointVelocity()
|
2010-10-04 04:01:34 +02:00
|
|
|
{
|
2013-04-14 11:23:47 +02:00
|
|
|
float dT = vtolpathfollowerSettings.UpdatePeriod / 1000.0f;
|
|
|
|
|
|
|
|
PathDesiredData pathDesired;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-04-14 11:23:47 +02:00
|
|
|
PathDesiredGet(&pathDesired);
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
PositionStateData positionState;
|
2013-04-14 11:23:47 +02:00
|
|
|
VelocityDesiredData velocityDesired;
|
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
PositionStateGet(&positionState);
|
2013-04-14 11:23:47 +02:00
|
|
|
VelocityDesiredGet(&velocityDesired);
|
|
|
|
|
|
|
|
float northError;
|
|
|
|
float eastError;
|
|
|
|
float downError;
|
|
|
|
float northCommand;
|
|
|
|
float eastCommand;
|
|
|
|
float downCommand;
|
|
|
|
|
|
|
|
float northPos = 0, eastPos = 0, downPos = 0;
|
|
|
|
switch (vtolpathfollowerSettings.PositionSource) {
|
2013-05-18 14:17:26 +02:00
|
|
|
case VTOLPATHFOLLOWERSETTINGS_POSITIONSOURCE_EKF:
|
2013-05-18 19:36:45 +02:00
|
|
|
northPos = positionState.North;
|
|
|
|
eastPos = positionState.East;
|
|
|
|
downPos = positionState.Down;
|
2013-05-18 14:17:26 +02:00
|
|
|
break;
|
|
|
|
case VTOLPATHFOLLOWERSETTINGS_POSITIONSOURCE_GPSPOS:
|
|
|
|
{
|
|
|
|
// this used to work with the NEDposition UAVObject
|
|
|
|
// however this UAVObject has been removed
|
2013-05-22 21:45:06 +02:00
|
|
|
GPSPositionSensorData gpsPosition;
|
|
|
|
GPSPositionSensorGet(&gpsPosition);
|
2013-05-18 14:17:26 +02:00
|
|
|
HomeLocationData homeLocation;
|
|
|
|
HomeLocationGet(&homeLocation);
|
|
|
|
float lat = DEG2RAD(homeLocation.Latitude / 10.0e6f);
|
|
|
|
float alt = homeLocation.Altitude;
|
|
|
|
float T[3] = { alt + 6.378137E6f,
|
|
|
|
cosf(lat) * (alt + 6.378137E6f),
|
|
|
|
-1.0f };
|
|
|
|
float NED[3] = { T[0] * (DEG2RAD((gpsPosition.Latitude - homeLocation.Latitude) / 10.0e6f)),
|
|
|
|
T[1] * (DEG2RAD((gpsPosition.Longitude - homeLocation.Longitude) / 10.0e6f)),
|
|
|
|
T[2] * ((gpsPosition.Altitude + gpsPosition.GeoidSeparation - homeLocation.Altitude)) };
|
|
|
|
|
|
|
|
northPos = NED[0];
|
|
|
|
eastPos = NED[1];
|
|
|
|
downPos = NED[2];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PIOS_Assert(0);
|
|
|
|
break;
|
2013-04-14 11:23:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute desired north command
|
|
|
|
northError = pathDesired.End[PATHDESIRED_END_NORTH] - northPos;
|
|
|
|
northPosIntegral = bound(northPosIntegral + northError * dT * vtolpathfollowerSettings.HorizontalPosPI[VTOLPATHFOLLOWERSETTINGS_HORIZONTALPOSPI_KI],
|
2013-05-18 14:17:26 +02:00
|
|
|
-vtolpathfollowerSettings.HorizontalPosPI[VTOLPATHFOLLOWERSETTINGS_HORIZONTALPOSPI_ILIMIT],
|
|
|
|
vtolpathfollowerSettings.HorizontalPosPI[VTOLPATHFOLLOWERSETTINGS_HORIZONTALPOSPI_ILIMIT]);
|
|
|
|
northCommand = (northError * vtolpathfollowerSettings.HorizontalPosPI[VTOLPATHFOLLOWERSETTINGS_HORIZONTALPOSPI_KP] + northPosIntegral);
|
2013-04-14 11:23:47 +02:00
|
|
|
|
|
|
|
eastError = pathDesired.End[PATHDESIRED_END_EAST] - eastPos;
|
2013-05-18 14:17:26 +02:00
|
|
|
eastPosIntegral = bound(eastPosIntegral + eastError * dT * vtolpathfollowerSettings.HorizontalPosPI[VTOLPATHFOLLOWERSETTINGS_HORIZONTALPOSPI_KI],
|
|
|
|
-vtolpathfollowerSettings.HorizontalPosPI[VTOLPATHFOLLOWERSETTINGS_HORIZONTALPOSPI_ILIMIT],
|
|
|
|
vtolpathfollowerSettings.HorizontalPosPI[VTOLPATHFOLLOWERSETTINGS_HORIZONTALPOSPI_ILIMIT]);
|
2013-04-14 11:23:47 +02:00
|
|
|
eastCommand = (eastError * vtolpathfollowerSettings.HorizontalPosPI[VTOLPATHFOLLOWERSETTINGS_HORIZONTALPOSPI_KP] + eastPosIntegral);
|
|
|
|
|
|
|
|
// Limit the maximum velocity
|
|
|
|
float total_vel = sqrtf(powf(northCommand, 2) + powf(eastCommand, 2));
|
2013-05-18 14:17:26 +02:00
|
|
|
float scale = 1;
|
|
|
|
if (total_vel > vtolpathfollowerSettings.HorizontalVelMax) {
|
2013-04-14 11:23:47 +02:00
|
|
|
scale = vtolpathfollowerSettings.HorizontalVelMax / total_vel;
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
2013-04-14 11:23:47 +02:00
|
|
|
|
|
|
|
velocityDesired.North = northCommand * scale;
|
2013-05-18 14:17:26 +02:00
|
|
|
velocityDesired.East = eastCommand * scale;
|
2013-04-14 11:23:47 +02:00
|
|
|
|
|
|
|
downError = pathDesired.End[PATHDESIRED_END_DOWN] - downPos;
|
|
|
|
downPosIntegral = bound(downPosIntegral + downError * dT * vtolpathfollowerSettings.VerticalPosPI[VTOLPATHFOLLOWERSETTINGS_VERTICALPOSPI_KI],
|
2013-05-18 14:17:26 +02:00
|
|
|
-vtolpathfollowerSettings.VerticalPosPI[VTOLPATHFOLLOWERSETTINGS_VERTICALPOSPI_ILIMIT],
|
|
|
|
vtolpathfollowerSettings.VerticalPosPI[VTOLPATHFOLLOWERSETTINGS_VERTICALPOSPI_ILIMIT]);
|
|
|
|
downCommand = (downError * vtolpathfollowerSettings.VerticalPosPI[VTOLPATHFOLLOWERSETTINGS_VERTICALPOSPI_KP] + downPosIntegral);
|
2013-04-14 11:23:47 +02:00
|
|
|
velocityDesired.Down = bound(downCommand, -vtolpathfollowerSettings.VerticalVelMax, vtolpathfollowerSettings.VerticalVelMax);
|
|
|
|
|
|
|
|
VelocityDesiredSet(&velocityDesired);
|
2010-10-04 04:01:34 +02:00
|
|
|
}
|
|
|
|
|
2012-05-21 11:25:57 +02:00
|
|
|
/**
|
|
|
|
* Compute desired attitude from a fixed preset
|
|
|
|
*
|
|
|
|
*/
|
2013-05-18 14:17:26 +02:00
|
|
|
static void updateFixedAttitude(float *attitude)
|
2012-05-21 11:25:57 +02:00
|
|
|
{
|
2013-04-14 11:23:47 +02:00
|
|
|
StabilizationDesiredData stabDesired;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-04-14 11:23:47 +02:00
|
|
|
StabilizationDesiredGet(&stabDesired);
|
2013-05-18 14:17:26 +02:00
|
|
|
stabDesired.Roll = attitude[0];
|
|
|
|
stabDesired.Pitch = attitude[1];
|
|
|
|
stabDesired.Yaw = attitude[2];
|
2013-04-14 11:23:47 +02:00
|
|
|
stabDesired.Throttle = attitude[3];
|
2013-05-18 14:17:26 +02:00
|
|
|
stabDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_ROLL] = STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE;
|
2013-04-14 11:23:47 +02:00
|
|
|
stabDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_PITCH] = STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE;
|
2013-05-18 14:17:26 +02:00
|
|
|
stabDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_YAW] = STABILIZATIONDESIRED_STABILIZATIONMODE_AXISLOCK;
|
2013-04-14 11:23:47 +02:00
|
|
|
StabilizationDesiredSet(&stabDesired);
|
2012-05-21 11:25:57 +02:00
|
|
|
}
|
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
/**
|
2011-01-14 01:51:57 +01:00
|
|
|
* Compute desired attitude from the desired velocity
|
|
|
|
*
|
2013-05-18 19:36:45 +02:00
|
|
|
* Takes in @ref NedState which has the acceleration in the
|
2013-05-18 14:17:26 +02:00
|
|
|
* NED frame as the feedback term and then compares the
|
2013-05-18 19:36:45 +02:00
|
|
|
* @ref VelocityState against the @ref VelocityDesired
|
2010-09-27 09:28:34 +02:00
|
|
|
*/
|
2013-03-28 17:29:26 +01:00
|
|
|
static void updateVtolDesiredAttitude(bool yaw_attitude)
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
2013-04-14 11:23:47 +02:00
|
|
|
float dT = vtolpathfollowerSettings.UpdatePeriod / 1000.0f;
|
|
|
|
|
|
|
|
VelocityDesiredData velocityDesired;
|
2013-05-18 19:36:45 +02:00
|
|
|
VelocityStateData velocityState;
|
2013-04-14 11:23:47 +02:00
|
|
|
StabilizationDesiredData stabDesired;
|
2013-05-18 19:36:45 +02:00
|
|
|
AttitudeStateData attitudeState;
|
2013-04-14 11:23:47 +02:00
|
|
|
NedAccelData nedAccel;
|
|
|
|
StabilizationSettingsData stabSettings;
|
|
|
|
SystemSettingsData systemSettings;
|
|
|
|
|
|
|
|
float northError;
|
|
|
|
float northCommand;
|
|
|
|
|
|
|
|
float eastError;
|
|
|
|
float eastCommand;
|
|
|
|
|
|
|
|
float downError;
|
|
|
|
float downCommand;
|
|
|
|
|
|
|
|
SystemSettingsGet(&systemSettings);
|
2013-05-18 19:36:45 +02:00
|
|
|
VelocityStateGet(&velocityState);
|
2013-04-14 11:23:47 +02:00
|
|
|
VelocityDesiredGet(&velocityDesired);
|
|
|
|
StabilizationDesiredGet(&stabDesired);
|
|
|
|
VelocityDesiredGet(&velocityDesired);
|
2013-05-18 19:36:45 +02:00
|
|
|
AttitudeStateGet(&attitudeState);
|
2013-04-14 11:23:47 +02:00
|
|
|
StabilizationSettingsGet(&stabSettings);
|
|
|
|
NedAccelGet(&nedAccel);
|
|
|
|
|
|
|
|
float northVel = 0, eastVel = 0, downVel = 0;
|
|
|
|
switch (vtolpathfollowerSettings.VelocitySource) {
|
2013-05-18 14:17:26 +02:00
|
|
|
case VTOLPATHFOLLOWERSETTINGS_VELOCITYSOURCE_EKF:
|
2013-05-18 19:36:45 +02:00
|
|
|
northVel = velocityState.North;
|
|
|
|
eastVel = velocityState.East;
|
|
|
|
downVel = velocityState.Down;
|
2013-05-18 14:17:26 +02:00
|
|
|
break;
|
|
|
|
case VTOLPATHFOLLOWERSETTINGS_VELOCITYSOURCE_NEDVEL:
|
|
|
|
{
|
2013-05-22 21:45:06 +02:00
|
|
|
GPSVelocitySensorData gpsVelocity;
|
|
|
|
GPSVelocitySensorGet(&gpsVelocity);
|
2013-05-18 14:17:26 +02:00
|
|
|
northVel = gpsVelocity.North;
|
|
|
|
eastVel = gpsVelocity.East;
|
|
|
|
downVel = gpsVelocity.Down;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VTOLPATHFOLLOWERSETTINGS_VELOCITYSOURCE_GPSPOS:
|
|
|
|
{
|
2013-05-22 21:45:06 +02:00
|
|
|
GPSPositionSensorData gpsPosition;
|
|
|
|
GPSPositionSensorGet(&gpsPosition);
|
2013-05-18 14:17:26 +02:00
|
|
|
northVel = gpsPosition.Groundspeed * cosf(DEG2RAD(gpsPosition.Heading));
|
|
|
|
eastVel = gpsPosition.Groundspeed * sinf(DEG2RAD(gpsPosition.Heading));
|
2013-05-18 19:36:45 +02:00
|
|
|
downVel = velocityState.Down;
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PIOS_Assert(0);
|
|
|
|
break;
|
2013-04-14 11:23:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Testing code - refactor into manual control command
|
|
|
|
ManualControlCommandData manualControlData;
|
|
|
|
ManualControlCommandGet(&manualControlData);
|
|
|
|
|
|
|
|
// Compute desired north command
|
|
|
|
northError = velocityDesired.North - northVel;
|
|
|
|
northVelIntegral = bound(northVelIntegral + northError * dT * vtolpathfollowerSettings.HorizontalVelPID[VTOLPATHFOLLOWERSETTINGS_HORIZONTALVELPID_KI],
|
2013-05-18 14:17:26 +02:00
|
|
|
-vtolpathfollowerSettings.HorizontalVelPID[VTOLPATHFOLLOWERSETTINGS_HORIZONTALVELPID_ILIMIT],
|
|
|
|
vtolpathfollowerSettings.HorizontalVelPID[VTOLPATHFOLLOWERSETTINGS_HORIZONTALVELPID_ILIMIT]);
|
|
|
|
northCommand = (northError * vtolpathfollowerSettings.HorizontalVelPID[VTOLPATHFOLLOWERSETTINGS_HORIZONTALVELPID_KP] + northVelIntegral
|
|
|
|
- nedAccel.North * vtolpathfollowerSettings.HorizontalVelPID[VTOLPATHFOLLOWERSETTINGS_HORIZONTALVELPID_KD]
|
|
|
|
+ velocityDesired.North * vtolpathfollowerSettings.VelocityFeedforward);
|
2013-04-14 11:23:47 +02:00
|
|
|
|
|
|
|
// Compute desired east command
|
|
|
|
eastError = velocityDesired.East - eastVel;
|
|
|
|
eastVelIntegral = bound(eastVelIntegral + eastError * dT * vtolpathfollowerSettings.HorizontalVelPID[VTOLPATHFOLLOWERSETTINGS_HORIZONTALVELPID_KI],
|
2013-05-18 14:17:26 +02:00
|
|
|
-vtolpathfollowerSettings.HorizontalVelPID[VTOLPATHFOLLOWERSETTINGS_HORIZONTALVELPID_ILIMIT],
|
|
|
|
vtolpathfollowerSettings.HorizontalVelPID[VTOLPATHFOLLOWERSETTINGS_HORIZONTALVELPID_ILIMIT]);
|
|
|
|
eastCommand = (eastError * vtolpathfollowerSettings.HorizontalVelPID[VTOLPATHFOLLOWERSETTINGS_HORIZONTALVELPID_KP] + eastVelIntegral
|
|
|
|
- nedAccel.East * vtolpathfollowerSettings.HorizontalVelPID[VTOLPATHFOLLOWERSETTINGS_HORIZONTALVELPID_KD]
|
|
|
|
+ velocityDesired.East * vtolpathfollowerSettings.VelocityFeedforward);
|
2013-04-14 11:23:47 +02:00
|
|
|
|
|
|
|
// Compute desired down command
|
|
|
|
downError = velocityDesired.Down - downVel;
|
|
|
|
// Must flip this sign
|
|
|
|
downError = -downError;
|
|
|
|
downVelIntegral = bound(downVelIntegral + downError * dT * vtolpathfollowerSettings.VerticalVelPID[VTOLPATHFOLLOWERSETTINGS_VERTICALVELPID_KI],
|
2013-05-18 14:17:26 +02:00
|
|
|
-vtolpathfollowerSettings.VerticalVelPID[VTOLPATHFOLLOWERSETTINGS_VERTICALVELPID_ILIMIT],
|
|
|
|
vtolpathfollowerSettings.VerticalVelPID[VTOLPATHFOLLOWERSETTINGS_VERTICALVELPID_ILIMIT]);
|
|
|
|
downCommand = (downError * vtolpathfollowerSettings.VerticalVelPID[VTOLPATHFOLLOWERSETTINGS_VERTICALVELPID_KP] + downVelIntegral
|
|
|
|
- nedAccel.Down * vtolpathfollowerSettings.VerticalVelPID[VTOLPATHFOLLOWERSETTINGS_VERTICALVELPID_KD]);
|
2013-04-14 11:23:47 +02:00
|
|
|
|
|
|
|
stabDesired.Throttle = bound(downCommand + throttleOffset, 0, 1);
|
|
|
|
|
|
|
|
// Project the north and east command signals into the pitch and roll based on yaw. For this to behave well the
|
|
|
|
// craft should move similarly for 5 deg roll versus 5 deg pitch
|
2013-05-18 19:36:45 +02:00
|
|
|
stabDesired.Pitch = bound(-northCommand * cosf(DEG2RAD(attitudeState.Yaw)) +
|
|
|
|
-eastCommand * sinf(DEG2RAD(attitudeState.Yaw)),
|
2013-05-18 14:17:26 +02:00
|
|
|
-vtolpathfollowerSettings.MaxRollPitch, vtolpathfollowerSettings.MaxRollPitch);
|
2013-05-18 19:36:45 +02:00
|
|
|
stabDesired.Roll = bound(-northCommand * sinf(DEG2RAD(attitudeState.Yaw)) +
|
|
|
|
eastCommand * cosf(DEG2RAD(attitudeState.Yaw)),
|
2013-05-18 14:17:26 +02:00
|
|
|
-vtolpathfollowerSettings.MaxRollPitch, vtolpathfollowerSettings.MaxRollPitch);
|
2013-04-14 11:23:47 +02:00
|
|
|
|
|
|
|
if (vtolpathfollowerSettings.ThrottleControl == VTOLPATHFOLLOWERSETTINGS_THROTTLECONTROL_FALSE) {
|
|
|
|
// For now override throttle with manual control. Disable at your risk, quad goes to China.
|
|
|
|
ManualControlCommandData manualControl;
|
|
|
|
ManualControlCommandGet(&manualControl);
|
|
|
|
stabDesired.Throttle = manualControl.Throttle;
|
|
|
|
}
|
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
stabDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_ROLL] = STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE;
|
2013-04-14 11:23:47 +02:00
|
|
|
stabDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_PITCH] = STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE;
|
|
|
|
if (yaw_attitude) {
|
|
|
|
stabDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_YAW] = STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE;
|
|
|
|
} else {
|
|
|
|
stabDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_YAW] = STABILIZATIONDESIRED_STABILIZATIONMODE_AXISLOCK;
|
|
|
|
stabDesired.Yaw = stabSettings.MaximumRate[STABILIZATIONSETTINGS_MAXIMUMRATE_YAW] * manualControlData.Yaw;
|
|
|
|
}
|
|
|
|
StabilizationDesiredSet(&stabDesired);
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
2012-04-10 05:39:41 +02:00
|
|
|
/**
|
|
|
|
* Keep a running filtered version of the acceleration in the NED frame
|
|
|
|
*/
|
|
|
|
static void updateNedAccel()
|
|
|
|
{
|
2013-04-14 11:23:47 +02:00
|
|
|
float accel[3];
|
|
|
|
float q[4];
|
|
|
|
float Rbe[3][3];
|
|
|
|
float accel_ned[3];
|
|
|
|
|
|
|
|
// Collect downsampled attitude data
|
2013-05-18 19:36:45 +02:00
|
|
|
AccelStateData accelState;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-05-18 19:36:45 +02:00
|
|
|
AccelStateGet(&accelState);
|
|
|
|
accel[0] = accelState.x;
|
|
|
|
accel[1] = accelState.y;
|
|
|
|
accel[2] = accelState.z;
|
2013-04-14 11:23:47 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
// rotate avg accels into earth frame and store it
|
2013-05-18 19:36:45 +02:00
|
|
|
AttitudeStateData attitudeState;
|
|
|
|
AttitudeStateGet(&attitudeState);
|
|
|
|
q[0] = attitudeState.q1;
|
|
|
|
q[1] = attitudeState.q2;
|
|
|
|
q[2] = attitudeState.q3;
|
|
|
|
q[3] = attitudeState.q4;
|
2013-04-14 11:23:47 +02:00
|
|
|
Quaternion2R(q, Rbe);
|
|
|
|
for (uint8_t i = 0; i < 3; i++) {
|
|
|
|
accel_ned[i] = 0;
|
2013-05-18 14:17:26 +02:00
|
|
|
for (uint8_t j = 0; j < 3; j++) {
|
2013-04-14 11:23:47 +02:00
|
|
|
accel_ned[i] += Rbe[j][i] * accel[j];
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
2013-04-14 11:23:47 +02:00
|
|
|
}
|
|
|
|
accel_ned[2] += 9.81f;
|
|
|
|
|
|
|
|
NedAccelData accelData;
|
|
|
|
NedAccelGet(&accelData);
|
|
|
|
accelData.North = accel_ned[0];
|
2013-05-18 14:17:26 +02:00
|
|
|
accelData.East = accel_ned[1];
|
|
|
|
accelData.Down = accel_ned[2];
|
2013-04-14 11:23:47 +02:00
|
|
|
NedAccelSet(&accelData);
|
2012-04-10 05:39:41 +02:00
|
|
|
}
|
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
/**
|
|
|
|
* Bound input value between limits
|
|
|
|
*/
|
|
|
|
static float bound(float val, float min, float max)
|
|
|
|
{
|
2013-04-14 11:23:47 +02:00
|
|
|
if (val < min) {
|
|
|
|
val = min;
|
|
|
|
} else if (val > max) {
|
|
|
|
val = max;
|
|
|
|
}
|
|
|
|
return val;
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
2012-05-08 09:28:19 +02:00
|
|
|
|
2013-05-05 09:02:24 +02:00
|
|
|
static void SettingsUpdatedCb(__attribute__((unused)) UAVObjEvent *ev)
|
2012-05-08 09:28:19 +02:00
|
|
|
{
|
2013-04-14 11:23:47 +02:00
|
|
|
VtolPathFollowerSettingsGet(&vtolpathfollowerSettings);
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
2012-05-08 09:28:19 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
static void accessoryUpdated(UAVObjEvent *ev)
|
2012-05-08 09:28:19 +02:00
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
if (ev->obj != AccessoryDesiredHandle()) {
|
2013-04-14 11:23:47 +02:00
|
|
|
return;
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
2013-04-14 11:23:47 +02:00
|
|
|
|
|
|
|
AccessoryDesiredData accessory;
|
|
|
|
PoiLearnSettingsData poiLearn;
|
|
|
|
PoiLearnSettingsGet(&poiLearn);
|
|
|
|
|
|
|
|
if (poiLearn.Input != POILEARNSETTINGS_INPUT_NONE) {
|
|
|
|
if (AccessoryDesiredInstGet(poiLearn.Input - POILEARNSETTINGS_INPUT_ACCESSORY0, &accessory) == 0) {
|
|
|
|
if (accessory.AccessoryVal < -0.5f) {
|
2013-05-18 19:36:45 +02:00
|
|
|
PositionStateData positionState;
|
|
|
|
PositionStateGet(&positionState);
|
2013-04-14 11:23:47 +02:00
|
|
|
PoiLocationData poi;
|
|
|
|
PoiLocationGet(&poi);
|
2013-05-18 19:36:45 +02:00
|
|
|
poi.North = positionState.North;
|
|
|
|
poi.East = positionState.East;
|
|
|
|
poi.Down = positionState.Down;
|
2013-04-14 11:23:47 +02:00
|
|
|
PoiLocationSet(&poi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-08 09:28:19 +02:00
|
|
|
}
|