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.
|
2010-09-27 09:28:34 +02:00
|
|
|
* @brief This module compared @ref PositionActuatl to @ref ActiveWaypoint
|
|
|
|
* and sets @ref AttitudeDesired. It only does this when the FlightMode field
|
|
|
|
* of @ref ManualControlCommand is Auto.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Input object: ActiveWaypoint
|
|
|
|
* Input object: PositionActual
|
|
|
|
* Input object: ManualControlCommand
|
|
|
|
* Output object: AttitudeDesired
|
|
|
|
*
|
|
|
|
* This module will periodically update the value of the AttitudeDesired object.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "openpilot.h"
|
2012-04-10 05:39:41 +02:00
|
|
|
#include "paths.h"
|
|
|
|
|
2012-05-08 07:25:59 +02:00
|
|
|
#include "vtolpathfollower.h"
|
2012-02-29 20:48:31 +01:00
|
|
|
#include "accels.h"
|
2012-05-21 11:25:57 +02:00
|
|
|
#include "hwsettings.h"
|
2010-09-27 09:28:34 +02:00
|
|
|
#include "attitudeactual.h"
|
2012-05-08 09:28:19 +02:00
|
|
|
#include "pathdesired.h" // object that will be updated by the module
|
2010-09-27 09:28:34 +02:00
|
|
|
#include "positionactual.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"
|
2012-04-14 20:34:30 +02:00
|
|
|
#include "gpsvelocity.h"
|
|
|
|
#include "gpsposition.h"
|
2012-05-24 11:09:53 +02:00
|
|
|
#include "vtolpathfollowersettings.h"
|
2011-01-14 01:51:54 +01:00
|
|
|
#include "nedaccel.h"
|
2012-04-15 23:23:34 +02:00
|
|
|
#include "nedposition.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"
|
|
|
|
#include "velocityactual.h"
|
2011-01-14 01:51:54 +01:00
|
|
|
#include "CoordinateConversions.h"
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
// Private constants
|
2012-02-29 20:48:31 +01:00
|
|
|
#define MAX_QUEUE_SIZE 4
|
2012-03-10 07:51:57 +01:00
|
|
|
#define STACK_SIZE_BYTES 1548
|
2011-01-09 21:49:33 +01:00
|
|
|
#define TASK_PRIORITY (tskIDLE_PRIORITY+2)
|
2012-04-14 20:34:30 +02:00
|
|
|
#define F_PI 3.14159265358979323846f
|
2012-05-08 09:28:19 +02:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
// Private types
|
|
|
|
|
|
|
|
// Private variables
|
2012-05-21 11:25:57 +02:00
|
|
|
static bool followerEnabled = false;
|
2012-05-08 07:25:59 +02:00
|
|
|
static xTaskHandle pathfollowerTaskHandle;
|
2012-05-08 09:28:19 +02:00
|
|
|
static PathDesiredData pathDesired;
|
2012-05-24 11:09:53 +02:00
|
|
|
static VtolPathFollowerSettingsData vtolpathfollowerSettings;
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
// Private functions
|
2012-05-08 07:25:59 +02:00
|
|
|
static void vtolPathFollowerTask(void *parameters);
|
2012-05-08 09:28:19 +02:00
|
|
|
static void SettingsUpdatedCb(UAVObjEvent * ev);
|
2012-04-10 05:39:41 +02:00
|
|
|
static void updateNedAccel();
|
|
|
|
static void updatePathVelocity();
|
2012-05-08 09:28:19 +02:00
|
|
|
static void updateEndpointVelocity();
|
2012-05-24 18:19:52 +02:00
|
|
|
static void updateFixedAttitude(float* attitude);
|
2010-10-04 04:01:34 +02:00
|
|
|
static void updateVtolDesiredAttitude();
|
2012-05-08 09:28:19 +02:00
|
|
|
static float bound(float val, float min, float max);
|
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
|
|
|
{
|
2012-05-21 11:25:57 +02:00
|
|
|
if (followerEnabled) {
|
|
|
|
// Start main task
|
2012-05-24 18:19:52 +02:00
|
|
|
xTaskCreate(vtolPathFollowerTask, (signed char *)"PathFollower", STACK_SIZE_BYTES/4, NULL, TASK_PRIORITY, &pathfollowerTaskHandle);
|
2012-05-21 11:25:57 +02:00
|
|
|
TaskMonitorAdd(TASKINFO_RUNNING_PATHFOLLOWER, pathfollowerTaskHandle);
|
|
|
|
}
|
2011-06-20 07:35:40 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
2012-05-21 11:25:57 +02:00
|
|
|
HwSettingsInitialize();
|
|
|
|
uint8_t optionalModules[HWSETTINGS_OPTIONALMODULES_NUMELEM];
|
|
|
|
HwSettingsOptionalModulesGet(optionalModules);
|
2012-05-24 18:19:52 +02:00
|
|
|
if (optionalModules[HWSETTINGS_OPTIONALMODULES_VTOLPATHFOLLOWER] == HWSETTINGS_OPTIONALMODULES_ENABLED) {
|
2012-05-21 11:25:57 +02:00
|
|
|
followerEnabled = true;
|
2012-05-24 11:09:53 +02:00
|
|
|
VtolPathFollowerSettingsInitialize();
|
2012-05-21 11:25:57 +02:00
|
|
|
NedAccelInitialize();
|
|
|
|
PathDesiredInitialize();
|
|
|
|
PathStatusInitialize();
|
|
|
|
VelocityDesiredInitialize();
|
|
|
|
} else {
|
|
|
|
followerEnabled = false;
|
|
|
|
}
|
2011-01-14 01:51:54 +01:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2012-05-08 09:28:19 +02:00
|
|
|
|
2012-05-08 07:25:59 +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;
|
|
|
|
static float eastVelIntegral = 0;
|
|
|
|
static float downVelIntegral = 0;
|
|
|
|
|
|
|
|
static float northPosIntegral = 0;
|
|
|
|
static float eastPosIntegral = 0;
|
|
|
|
static float downPosIntegral = 0;
|
|
|
|
|
2012-05-08 10:12:14 +02:00
|
|
|
static float throttleOffset = 0;
|
2010-09-27 09:28:34 +02:00
|
|
|
/**
|
|
|
|
* Module thread, should not return.
|
|
|
|
*/
|
2012-05-08 07:25:59 +02:00
|
|
|
static void vtolPathFollowerTask(void *parameters)
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
|
|
|
SystemSettingsData systemSettings;
|
2011-05-09 18:37:06 +02:00
|
|
|
FlightStatusData flightStatus;
|
2012-05-21 11:25:57 +02:00
|
|
|
PathStatusData pathStatus;
|
2010-09-27 09:28:34 +02:00
|
|
|
|
2011-01-14 01:51:54 +01:00
|
|
|
portTickType lastUpdateTime;
|
2012-05-08 09:28:19 +02:00
|
|
|
|
2012-05-24 11:09:53 +02:00
|
|
|
VtolPathFollowerSettingsConnectCallback(SettingsUpdatedCb);
|
2012-05-08 09:28:19 +02:00
|
|
|
PathDesiredConnectCallback(SettingsUpdatedCb);
|
|
|
|
|
2012-05-24 11:09:53 +02:00
|
|
|
VtolPathFollowerSettingsGet(&vtolpathfollowerSettings);
|
2012-05-08 09:28:19 +02:00
|
|
|
PathDesiredGet(&pathDesired);
|
2011-01-14 01:51:54 +01:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
// Main task loop
|
2011-01-14 01:51:54 +01:00
|
|
|
lastUpdateTime = xTaskGetTickCount();
|
2010-09-27 09:28:34 +02:00
|
|
|
while (1) {
|
2012-05-07 19:52:34 +02:00
|
|
|
|
2012-05-08 09:28:19 +02:00
|
|
|
// Conditions when this runs:
|
|
|
|
// 1. Must have VTOL type airframe
|
|
|
|
// 2. Flight mode is PositionHold and PathDesired.Mode is Endpoint OR
|
|
|
|
// FlightMode is PathPlanner and PathDesired.Mode is Endpoint or Path
|
|
|
|
|
2012-05-07 19:52:34 +02:00
|
|
|
SystemSettingsGet(&systemSettings);
|
|
|
|
if ( (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_VTOL) &&
|
2012-05-08 08:41:31 +02:00
|
|
|
(systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_QUADP) &&
|
2012-05-07 19:52:34 +02:00
|
|
|
(systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_QUADP) &&
|
|
|
|
(systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_QUADX) &&
|
|
|
|
(systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_HEXA) &&
|
2012-05-08 08:41:31 +02:00
|
|
|
(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) )
|
2012-05-07 19:52:34 +02:00
|
|
|
{
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_GUIDANCE,SYSTEMALARMS_ALARM_WARNING);
|
|
|
|
vTaskDelay(1000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-01-14 01:51:54 +01:00
|
|
|
// Continue collecting data if not enough time
|
2012-05-24 11:09:53 +02:00
|
|
|
vTaskDelayUntil(&lastUpdateTime, vtolpathfollowerSettings.UpdatePeriod / portTICK_RATE_MS);
|
2012-04-10 05:39:41 +02:00
|
|
|
|
|
|
|
// Convert the accels into the NED frame
|
|
|
|
updateNedAccel();
|
2011-01-14 01:51:54 +01:00
|
|
|
|
2011-05-09 18:37:06 +02:00
|
|
|
FlightStatusGet(&flightStatus);
|
2012-05-21 11:25:57 +02:00
|
|
|
PathStatusGet(&pathStatus);
|
2012-04-10 05:39:41 +02:00
|
|
|
|
2012-05-08 09:28:19 +02:00
|
|
|
// Check the combinations of flightmode and pathdesired mode
|
2012-04-10 05:39:41 +02:00
|
|
|
switch(flightStatus.FlightMode) {
|
|
|
|
case FLIGHTSTATUS_FLIGHTMODE_POSITIONHOLD:
|
2012-05-24 18:19:52 +02:00
|
|
|
case FLIGHTSTATUS_FLIGHTMODE_RETURNTOBASE:
|
2012-05-21 11:25:57 +02:00
|
|
|
if (pathDesired.Mode == PATHDESIRED_MODE_FLYENDPOINT) {
|
2012-05-08 09:28:19 +02:00
|
|
|
updateEndpointVelocity();
|
|
|
|
updateVtolDesiredAttitude();
|
2012-05-21 11:25:57 +02:00
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_GUIDANCE,SYSTEMALARMS_ALARM_OK);
|
2012-05-08 09:28:19 +02:00
|
|
|
} else {
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_GUIDANCE,SYSTEMALARMS_ALARM_ERROR);
|
|
|
|
}
|
2012-04-10 05:39:41 +02:00
|
|
|
break;
|
|
|
|
case FLIGHTSTATUS_FLIGHTMODE_PATHPLANNER:
|
2012-05-21 11:25:57 +02:00
|
|
|
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:
|
|
|
|
updateEndpointVelocity();
|
|
|
|
updateVtolDesiredAttitude();
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_GUIDANCE,SYSTEMALARMS_ALARM_OK);
|
|
|
|
break;
|
|
|
|
case PATHDESIRED_MODE_FLYVECTOR:
|
2012-05-28 01:51:17 +02:00
|
|
|
case PATHDESIRED_MODE_FLYCIRCLERIGHT:
|
|
|
|
case PATHDESIRED_MODE_FLYCIRCLELEFT:
|
2012-05-21 11:25:57 +02:00
|
|
|
updatePathVelocity();
|
|
|
|
updateVtolDesiredAttitude();
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_GUIDANCE,SYSTEMALARMS_ALARM_OK);
|
|
|
|
break;
|
|
|
|
case PATHDESIRED_MODE_FIXEDATTITUDE:
|
2012-05-24 18:19:52 +02:00
|
|
|
updateFixedAttitude(pathDesired.ModeParameters);
|
2012-05-21 11:25:57 +02:00
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_GUIDANCE,SYSTEMALARMS_ALARM_OK);
|
|
|
|
break;
|
|
|
|
case PATHDESIRED_MODE_DISARMALARM:
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_GUIDANCE,SYSTEMALARMS_ALARM_CRITICAL);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pathStatus.Status = PATHSTATUS_STATUS_CRITICAL;
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_GUIDANCE,SYSTEMALARMS_ALARM_ERROR);
|
|
|
|
break;
|
2012-05-08 09:28:19 +02:00
|
|
|
}
|
2012-04-10 05:39:41 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Be cleaner and get rid of global variables
|
|
|
|
northVelIntegral = 0;
|
|
|
|
eastVelIntegral = 0;
|
|
|
|
downVelIntegral = 0;
|
|
|
|
northPosIntegral = 0;
|
|
|
|
eastPosIntegral = 0;
|
|
|
|
downPosIntegral = 0;
|
2012-05-08 10:12:14 +02:00
|
|
|
|
|
|
|
// Track throttle before engaging this mode. Cheap system ident
|
|
|
|
StabilizationDesiredData stabDesired;
|
|
|
|
StabilizationDesiredGet(&stabDesired);
|
|
|
|
throttleOffset = stabDesired.Throttle;
|
|
|
|
|
2012-04-10 05:39:41 +02:00
|
|
|
break;
|
2011-01-14 01:51:54 +01:00
|
|
|
}
|
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
|
|
|
|
*
|
|
|
|
* Takes in @ref PositionActual and compares it to @ref PathDesired
|
|
|
|
* and computes @ref VelocityDesired
|
|
|
|
*/
|
|
|
|
static void updatePathVelocity()
|
|
|
|
{
|
2012-05-24 11:09:53 +02:00
|
|
|
float dT = vtolpathfollowerSettings.UpdatePeriod / 1000.0f;
|
2012-04-10 08:59:26 +02:00
|
|
|
float downCommand;
|
|
|
|
|
2012-04-10 05:39:41 +02:00
|
|
|
PositionActualData positionActual;
|
|
|
|
PositionActualGet(&positionActual);
|
|
|
|
|
|
|
|
float cur[3] = {positionActual.North, positionActual.East, positionActual.Down};
|
|
|
|
struct path_status progress;
|
|
|
|
|
2012-05-28 01:51:17 +02:00
|
|
|
path_progress(pathDesired.Start, pathDesired.End, cur, &progress, pathDesired.Mode);
|
2012-04-10 05:39:41 +02:00
|
|
|
|
|
|
|
float groundspeed = pathDesired.StartingVelocity +
|
2012-05-28 01:51:17 +02:00
|
|
|
(pathDesired.EndingVelocity - pathDesired.StartingVelocity) * bound ( progress.fractional_progress,0,1);
|
2012-04-10 05:39:41 +02:00
|
|
|
if(progress.fractional_progress > 1)
|
|
|
|
groundspeed = 0;
|
|
|
|
|
|
|
|
VelocityDesiredData velocityDesired;
|
|
|
|
velocityDesired.North = progress.path_direction[0] * groundspeed;
|
|
|
|
velocityDesired.East = progress.path_direction[1] * groundspeed;
|
|
|
|
|
2012-05-24 23:28:13 +02:00
|
|
|
float error_speed = progress.error * vtolpathfollowerSettings.HorizontalPosPI[VTOLPATHFOLLOWERSETTINGS_HORIZONTALPOSPI_KP];
|
2012-04-10 05:39:41 +02:00
|
|
|
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));
|
|
|
|
float scale = 1;
|
2012-05-24 11:09:53 +02:00
|
|
|
if(total_vel > vtolpathfollowerSettings.HorizontalVelMax)
|
|
|
|
scale = vtolpathfollowerSettings.HorizontalVelMax / total_vel;
|
2012-04-10 05:39:41 +02:00
|
|
|
|
|
|
|
velocityDesired.North += progress.correction_direction[0] * error_speed * scale;
|
|
|
|
velocityDesired.East += progress.correction_direction[1] * error_speed * scale;
|
|
|
|
|
2012-04-10 08:59:26 +02:00
|
|
|
float altitudeSetpoint = pathDesired.Start[2] + (pathDesired.End[2] - pathDesired.Start[2]) *
|
|
|
|
bound(progress.fractional_progress,0,1);
|
|
|
|
|
|
|
|
float downError = altitudeSetpoint - positionActual.Down;
|
2012-05-24 23:28:13 +02:00
|
|
|
downPosIntegral = bound(downPosIntegral + downError * dT * vtolpathfollowerSettings.VerticalPosPI[VTOLPATHFOLLOWERSETTINGS_VERTICALPOSPI_KI],
|
|
|
|
-vtolpathfollowerSettings.VerticalPosPI[VTOLPATHFOLLOWERSETTINGS_VERTICALPOSPI_ILIMIT],
|
|
|
|
vtolpathfollowerSettings.VerticalPosPI[VTOLPATHFOLLOWERSETTINGS_VERTICALPOSPI_ILIMIT]);
|
|
|
|
downCommand = (downError * vtolpathfollowerSettings.VerticalPosPI[VTOLPATHFOLLOWERSETTINGS_VERTICALPOSPI_KP] + downPosIntegral);
|
2012-04-10 08:59:26 +02:00
|
|
|
velocityDesired.Down = bound(downCommand,
|
2012-05-24 11:09:53 +02:00
|
|
|
-vtolpathfollowerSettings.VerticalVelMax,
|
|
|
|
vtolpathfollowerSettings.VerticalVelMax);
|
2012-04-10 08:59:26 +02:00
|
|
|
|
2012-04-10 05:39:41 +02:00
|
|
|
VelocityDesiredSet(&velocityDesired);
|
|
|
|
}
|
|
|
|
|
2011-01-14 01:51:57 +01:00
|
|
|
/**
|
|
|
|
* Compute desired velocity from the current position
|
|
|
|
*
|
|
|
|
* Takes in @ref PositionActual and compares it to @ref PositionDesired
|
|
|
|
* and computes @ref VelocityDesired
|
|
|
|
*/
|
2012-05-08 09:28:19 +02:00
|
|
|
void updateEndpointVelocity()
|
2010-10-04 04:01:34 +02:00
|
|
|
{
|
2012-05-24 11:09:53 +02:00
|
|
|
float dT = vtolpathfollowerSettings.UpdatePeriod / 1000.0f;
|
2011-03-26 12:40:33 +01:00
|
|
|
|
2010-10-04 04:01:34 +02:00
|
|
|
PositionActualData positionActual;
|
|
|
|
VelocityDesiredData velocityDesired;
|
|
|
|
|
|
|
|
PositionActualGet(&positionActual);
|
|
|
|
VelocityDesiredGet(&velocityDesired);
|
|
|
|
|
2011-03-26 12:40:33 +01:00
|
|
|
float northError;
|
|
|
|
float eastError;
|
|
|
|
float downError;
|
|
|
|
float northCommand;
|
|
|
|
float eastCommand;
|
|
|
|
float downCommand;
|
|
|
|
|
2012-04-15 23:23:34 +02:00
|
|
|
float northPos = 0, eastPos = 0, downPos = 0;
|
2012-05-24 11:09:53 +02:00
|
|
|
switch (vtolpathfollowerSettings.PositionSource) {
|
2012-05-24 23:28:13 +02:00
|
|
|
case VTOLPATHFOLLOWERSETTINGS_POSITIONSOURCE_EKF:
|
2012-04-15 23:23:34 +02:00
|
|
|
northPos = positionActual.North;
|
|
|
|
eastPos = positionActual.East;
|
|
|
|
downPos = positionActual.Down;
|
|
|
|
break;
|
2012-05-24 23:28:13 +02:00
|
|
|
case VTOLPATHFOLLOWERSETTINGS_POSITIONSOURCE_GPSPOS:
|
2012-04-15 23:23:34 +02:00
|
|
|
{
|
|
|
|
NEDPositionData nedPosition;
|
|
|
|
NEDPositionGet(&nedPosition);
|
|
|
|
northPos = nedPosition.North;
|
|
|
|
eastPos = nedPosition.East;
|
|
|
|
downPos = nedPosition.Down;
|
|
|
|
}
|
2012-04-16 15:59:25 +02:00
|
|
|
break;
|
2012-04-15 23:23:34 +02:00
|
|
|
default:
|
|
|
|
PIOS_Assert(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-03-26 12:40:33 +01:00
|
|
|
// Compute desired north command
|
2012-05-08 09:28:19 +02:00
|
|
|
northError = pathDesired.End[PATHDESIRED_END_NORTH] - northPos;
|
2012-05-24 23:28:13 +02:00
|
|
|
northPosIntegral = bound(northPosIntegral + northError * dT * vtolpathfollowerSettings.HorizontalPosPI[VTOLPATHFOLLOWERSETTINGS_HORIZONTALPOSPI_KI],
|
|
|
|
-vtolpathfollowerSettings.HorizontalPosPI[VTOLPATHFOLLOWERSETTINGS_HORIZONTALPOSPI_ILIMIT],
|
|
|
|
vtolpathfollowerSettings.HorizontalPosPI[VTOLPATHFOLLOWERSETTINGS_HORIZONTALPOSPI_ILIMIT]);
|
|
|
|
northCommand = (northError * vtolpathfollowerSettings.HorizontalPosPI[VTOLPATHFOLLOWERSETTINGS_HORIZONTALPOSPI_KP] +
|
2011-03-26 12:40:33 +01:00
|
|
|
northPosIntegral);
|
2010-10-04 04:01:34 +02:00
|
|
|
|
2012-05-08 09:28:19 +02:00
|
|
|
eastError = pathDesired.End[PATHDESIRED_END_EAST] - eastPos;
|
2012-05-24 23:28:13 +02:00
|
|
|
eastPosIntegral = bound(eastPosIntegral + eastError * dT * vtolpathfollowerSettings.HorizontalPosPI[VTOLPATHFOLLOWERSETTINGS_HORIZONTALPOSPI_KI],
|
|
|
|
-vtolpathfollowerSettings.HorizontalPosPI[VTOLPATHFOLLOWERSETTINGS_HORIZONTALPOSPI_ILIMIT],
|
|
|
|
vtolpathfollowerSettings.HorizontalPosPI[VTOLPATHFOLLOWERSETTINGS_HORIZONTALPOSPI_ILIMIT]);
|
|
|
|
eastCommand = (eastError * vtolpathfollowerSettings.HorizontalPosPI[VTOLPATHFOLLOWERSETTINGS_HORIZONTALPOSPI_KP] +
|
2011-03-31 22:08:14 +02:00
|
|
|
eastPosIntegral);
|
2010-10-04 04:01:34 +02:00
|
|
|
|
2012-05-08 09:28:19 +02:00
|
|
|
// Limit the maximum velocity
|
2012-04-14 17:49:54 +02:00
|
|
|
float total_vel = sqrtf(powf(northCommand,2) + powf(eastCommand,2));
|
2012-03-15 10:25:37 +01:00
|
|
|
float scale = 1;
|
2012-05-24 11:09:53 +02:00
|
|
|
if(total_vel > vtolpathfollowerSettings.HorizontalVelMax)
|
|
|
|
scale = vtolpathfollowerSettings.HorizontalVelMax / total_vel;
|
2012-03-15 10:25:37 +01:00
|
|
|
|
|
|
|
velocityDesired.North = northCommand * scale;
|
|
|
|
velocityDesired.East = eastCommand * scale;
|
2011-03-26 12:40:33 +01:00
|
|
|
|
2012-05-08 09:28:19 +02:00
|
|
|
downError = pathDesired.End[PATHDESIRED_END_DOWN] - downPos;
|
2012-05-24 23:28:13 +02:00
|
|
|
downPosIntegral = bound(downPosIntegral + downError * dT * vtolpathfollowerSettings.VerticalPosPI[VTOLPATHFOLLOWERSETTINGS_VERTICALPOSPI_KI],
|
|
|
|
-vtolpathfollowerSettings.VerticalPosPI[VTOLPATHFOLLOWERSETTINGS_VERTICALPOSPI_ILIMIT],
|
|
|
|
vtolpathfollowerSettings.VerticalPosPI[VTOLPATHFOLLOWERSETTINGS_VERTICALPOSPI_ILIMIT]);
|
|
|
|
downCommand = (downError * vtolpathfollowerSettings.VerticalPosPI[VTOLPATHFOLLOWERSETTINGS_VERTICALPOSPI_KP] + downPosIntegral);
|
2011-03-26 12:40:33 +01:00
|
|
|
velocityDesired.Down = bound(downCommand,
|
2012-05-24 11:09:53 +02:00
|
|
|
-vtolpathfollowerSettings.VerticalVelMax,
|
|
|
|
vtolpathfollowerSettings.VerticalVelMax);
|
2010-10-04 04:01:34 +02:00
|
|
|
|
|
|
|
VelocityDesiredSet(&velocityDesired);
|
|
|
|
}
|
|
|
|
|
2012-05-21 11:25:57 +02:00
|
|
|
/**
|
|
|
|
* Compute desired attitude from a fixed preset
|
|
|
|
*
|
|
|
|
*/
|
2012-05-24 18:19:52 +02:00
|
|
|
static void updateFixedAttitude(float* attitude)
|
2012-05-21 11:25:57 +02:00
|
|
|
{
|
|
|
|
StabilizationDesiredData stabDesired;
|
|
|
|
StabilizationDesiredGet(&stabDesired);
|
|
|
|
stabDesired.Roll = attitude[0];
|
|
|
|
stabDesired.Pitch = attitude[1];
|
|
|
|
stabDesired.Yaw = attitude[2];
|
|
|
|
stabDesired.Throttle = attitude[3];
|
|
|
|
stabDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_ROLL] = STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE;
|
|
|
|
stabDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_PITCH] = STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE;
|
|
|
|
stabDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_YAW] = STABILIZATIONDESIRED_STABILIZATIONMODE_RATE;
|
|
|
|
StabilizationDesiredSet(&stabDesired);
|
|
|
|
}
|
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
/**
|
2011-01-14 01:51:57 +01:00
|
|
|
* Compute desired attitude from the desired velocity
|
|
|
|
*
|
|
|
|
* Takes in @ref NedActual which has the acceleration in the
|
|
|
|
* NED frame as the feedback term and then compares the
|
|
|
|
* @ref VelocityActual against the @ref VelocityDesired
|
2010-09-27 09:28:34 +02:00
|
|
|
*/
|
2010-10-04 04:01:34 +02:00
|
|
|
static void updateVtolDesiredAttitude()
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
2012-05-24 11:09:53 +02:00
|
|
|
float dT = vtolpathfollowerSettings.UpdatePeriod / 1000.0f;
|
2010-10-04 04:01:34 +02:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
VelocityDesiredData velocityDesired;
|
|
|
|
VelocityActualData velocityActual;
|
2011-03-02 02:25:27 +01:00
|
|
|
StabilizationDesiredData stabDesired;
|
2010-09-27 09:28:34 +02:00
|
|
|
AttitudeActualData attitudeActual;
|
2011-01-14 01:51:54 +01:00
|
|
|
NedAccelData nedAccel;
|
2012-05-24 11:09:53 +02:00
|
|
|
VtolPathFollowerSettingsData vtolpathfollowerSettings;
|
2010-09-27 09:28:34 +02:00
|
|
|
StabilizationSettingsData stabSettings;
|
|
|
|
SystemSettingsData systemSettings;
|
|
|
|
|
|
|
|
float northError;
|
|
|
|
float northCommand;
|
2010-11-12 17:57:13 +01:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
float eastError;
|
|
|
|
float eastCommand;
|
2010-11-12 17:57:13 +01:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
float downError;
|
2011-01-14 01:51:54 +01:00
|
|
|
float downCommand;
|
2012-05-08 09:28:19 +02:00
|
|
|
|
2010-10-04 04:01:34 +02:00
|
|
|
SystemSettingsGet(&systemSettings);
|
2012-05-24 11:09:53 +02:00
|
|
|
VtolPathFollowerSettingsGet(&vtolpathfollowerSettings);
|
2010-10-04 04:01:34 +02:00
|
|
|
|
|
|
|
VelocityActualGet(&velocityActual);
|
|
|
|
VelocityDesiredGet(&velocityDesired);
|
2011-03-02 02:25:27 +01:00
|
|
|
StabilizationDesiredGet(&stabDesired);
|
2010-10-04 04:01:34 +02:00
|
|
|
VelocityDesiredGet(&velocityDesired);
|
|
|
|
AttitudeActualGet(&attitudeActual);
|
|
|
|
StabilizationSettingsGet(&stabSettings);
|
2011-01-14 01:51:54 +01:00
|
|
|
NedAccelGet(&nedAccel);
|
2010-10-04 04:01:34 +02:00
|
|
|
|
2012-04-15 18:51:26 +02:00
|
|
|
float northVel = 0, eastVel = 0, downVel = 0;
|
2012-05-24 11:09:53 +02:00
|
|
|
switch (vtolpathfollowerSettings.VelocitySource) {
|
2012-05-24 23:28:13 +02:00
|
|
|
case VTOLPATHFOLLOWERSETTINGS_VELOCITYSOURCE_EKF:
|
2012-04-14 20:34:30 +02:00
|
|
|
northVel = velocityActual.North;
|
|
|
|
eastVel = velocityActual.East;
|
|
|
|
downVel = velocityActual.Down;
|
|
|
|
break;
|
2012-05-24 23:28:13 +02:00
|
|
|
case VTOLPATHFOLLOWERSETTINGS_VELOCITYSOURCE_NEDVEL:
|
2012-04-14 20:34:30 +02:00
|
|
|
{
|
|
|
|
GPSVelocityData gpsVelocity;
|
|
|
|
GPSVelocityGet(&gpsVelocity);
|
|
|
|
northVel = gpsVelocity.North;
|
|
|
|
eastVel = gpsVelocity.East;
|
|
|
|
downVel = gpsVelocity.Down;
|
|
|
|
}
|
2012-04-16 15:59:25 +02:00
|
|
|
break;
|
2012-05-24 23:28:13 +02:00
|
|
|
case VTOLPATHFOLLOWERSETTINGS_VELOCITYSOURCE_GPSPOS:
|
2012-04-14 20:34:30 +02:00
|
|
|
{
|
|
|
|
GPSPositionData gpsPosition;
|
|
|
|
GPSPositionGet(&gpsPosition);
|
|
|
|
northVel = gpsPosition.Groundspeed * cosf(gpsPosition.Heading * F_PI / 180.0f);
|
|
|
|
eastVel = gpsPosition.Groundspeed * sinf(gpsPosition.Heading * F_PI / 180.0f);
|
|
|
|
downVel = velocityActual.Down;
|
|
|
|
}
|
2012-04-16 15:59:25 +02:00
|
|
|
break;
|
2012-04-14 20:34:30 +02:00
|
|
|
default:
|
2012-04-15 18:51:26 +02:00
|
|
|
PIOS_Assert(0);
|
2012-04-14 20:34:30 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-03-25 21:17:31 +01:00
|
|
|
// Testing code - refactor into manual control command
|
|
|
|
ManualControlCommandData manualControlData;
|
|
|
|
ManualControlCommandGet(&manualControlData);
|
|
|
|
stabDesired.Yaw = stabSettings.MaximumRate[STABILIZATIONSETTINGS_MAXIMUMRATE_YAW] * manualControlData.Yaw;
|
2010-10-04 04:01:34 +02:00
|
|
|
|
2011-01-14 01:51:54 +01:00
|
|
|
// Compute desired north command
|
2012-04-14 20:34:30 +02:00
|
|
|
northError = velocityDesired.North - northVel;
|
2012-05-24 23:28:13 +02:00
|
|
|
northVelIntegral = bound(northVelIntegral + northError * dT * vtolpathfollowerSettings.HorizontalVelPID[VTOLPATHFOLLOWERSETTINGS_HORIZONTALVELPID_KI],
|
|
|
|
-vtolpathfollowerSettings.HorizontalVelPID[VTOLPATHFOLLOWERSETTINGS_HORIZONTALVELPID_ILIMIT],
|
|
|
|
vtolpathfollowerSettings.HorizontalVelPID[VTOLPATHFOLLOWERSETTINGS_HORIZONTALVELPID_ILIMIT]);
|
|
|
|
northCommand = (northError * vtolpathfollowerSettings.HorizontalVelPID[VTOLPATHFOLLOWERSETTINGS_HORIZONTALVELPID_KP] +
|
2011-03-26 12:40:33 +01:00
|
|
|
northVelIntegral -
|
2012-05-24 23:28:13 +02:00
|
|
|
nedAccel.North * vtolpathfollowerSettings.HorizontalVelPID[VTOLPATHFOLLOWERSETTINGS_HORIZONTALVELPID_KD] +
|
2012-05-24 11:09:53 +02:00
|
|
|
velocityDesired.North * vtolpathfollowerSettings.VelocityFeedforward);
|
2010-10-04 04:01:34 +02:00
|
|
|
|
2011-01-14 01:51:54 +01:00
|
|
|
// Compute desired east command
|
2012-04-14 20:34:30 +02:00
|
|
|
eastError = velocityDesired.East - eastVel;
|
2012-05-24 23:28:13 +02:00
|
|
|
eastVelIntegral = bound(eastVelIntegral + eastError * dT * vtolpathfollowerSettings.HorizontalVelPID[VTOLPATHFOLLOWERSETTINGS_HORIZONTALVELPID_KI],
|
|
|
|
-vtolpathfollowerSettings.HorizontalVelPID[VTOLPATHFOLLOWERSETTINGS_HORIZONTALVELPID_ILIMIT],
|
|
|
|
vtolpathfollowerSettings.HorizontalVelPID[VTOLPATHFOLLOWERSETTINGS_HORIZONTALVELPID_ILIMIT]);
|
|
|
|
eastCommand = (eastError * vtolpathfollowerSettings.HorizontalVelPID[VTOLPATHFOLLOWERSETTINGS_HORIZONTALVELPID_KP] +
|
2011-03-26 12:40:33 +01:00
|
|
|
eastVelIntegral -
|
2012-05-24 23:28:13 +02:00
|
|
|
nedAccel.East * vtolpathfollowerSettings.HorizontalVelPID[VTOLPATHFOLLOWERSETTINGS_HORIZONTALVELPID_KD] +
|
2012-05-24 11:09:53 +02:00
|
|
|
velocityDesired.East * vtolpathfollowerSettings.VelocityFeedforward);
|
2011-01-14 01:51:54 +01:00
|
|
|
|
|
|
|
// Compute desired down command
|
2012-04-14 20:34:30 +02:00
|
|
|
downError = velocityDesired.Down - downVel;
|
2012-03-15 03:52:45 +01:00
|
|
|
// Must flip this sign
|
|
|
|
downError = -downError;
|
2012-05-24 23:28:13 +02:00
|
|
|
downVelIntegral = bound(downVelIntegral + downError * dT * vtolpathfollowerSettings.VerticalVelPID[VTOLPATHFOLLOWERSETTINGS_VERTICALVELPID_KI],
|
|
|
|
-vtolpathfollowerSettings.VerticalVelPID[VTOLPATHFOLLOWERSETTINGS_VERTICALVELPID_ILIMIT],
|
|
|
|
vtolpathfollowerSettings.VerticalVelPID[VTOLPATHFOLLOWERSETTINGS_VERTICALVELPID_ILIMIT]);
|
|
|
|
downCommand = (downError * vtolpathfollowerSettings.VerticalVelPID[VTOLPATHFOLLOWERSETTINGS_VERTICALVELPID_KP] +
|
2011-03-26 12:40:33 +01:00
|
|
|
downVelIntegral -
|
2012-05-24 23:28:13 +02:00
|
|
|
nedAccel.Down * vtolpathfollowerSettings.VerticalVelPID[VTOLPATHFOLLOWERSETTINGS_VERTICALVELPID_KD]);
|
2011-01-14 01:51:54 +01:00
|
|
|
|
2012-05-08 10:12:14 +02:00
|
|
|
stabDesired.Throttle = bound(downCommand + throttleOffset, 0, 1);
|
2010-10-04 04:01:34 +02:00
|
|
|
|
|
|
|
// 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
|
2011-03-02 02:25:27 +01:00
|
|
|
stabDesired.Pitch = bound(-northCommand * cosf(attitudeActual.Yaw * M_PI / 180) +
|
2010-12-10 18:14:35 +01:00
|
|
|
-eastCommand * sinf(attitudeActual.Yaw * M_PI / 180),
|
2012-05-24 11:09:53 +02:00
|
|
|
-vtolpathfollowerSettings.MaxRollPitch, vtolpathfollowerSettings.MaxRollPitch);
|
2011-03-02 02:25:27 +01:00
|
|
|
stabDesired.Roll = bound(-northCommand * sinf(attitudeActual.Yaw * M_PI / 180) +
|
2010-10-04 04:01:34 +02:00
|
|
|
eastCommand * cosf(attitudeActual.Yaw * M_PI / 180),
|
2012-05-24 11:09:53 +02:00
|
|
|
-vtolpathfollowerSettings.MaxRollPitch, vtolpathfollowerSettings.MaxRollPitch);
|
2010-10-04 04:01:34 +02:00
|
|
|
|
2012-05-24 23:28:13 +02:00
|
|
|
if(vtolpathfollowerSettings.ThrottleControl == VTOLPATHFOLLOWERSETTINGS_THROTTLECONTROL_FALSE) {
|
2011-01-14 01:51:54 +01:00
|
|
|
// For now override throttle with manual control. Disable at your risk, quad goes to China.
|
|
|
|
ManualControlCommandData manualControl;
|
|
|
|
ManualControlCommandGet(&manualControl);
|
2011-03-02 02:25:27 +01:00
|
|
|
stabDesired.Throttle = manualControl.Throttle;
|
2011-01-14 01:51:54 +01:00
|
|
|
}
|
2010-10-04 04:01:34 +02:00
|
|
|
|
2011-03-02 02:25:27 +01:00
|
|
|
stabDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_ROLL] = STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE;
|
|
|
|
stabDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_PITCH] = STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE;
|
2011-03-25 21:17:31 +01:00
|
|
|
stabDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_YAW] = STABILIZATIONDESIRED_STABILIZATIONMODE_RATE;
|
2011-03-02 02:25:27 +01:00
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
float accel[3];
|
|
|
|
float q[4];
|
|
|
|
float Rbe[3][3];
|
|
|
|
float accel_ned[3];
|
|
|
|
|
|
|
|
// Collect downsampled attitude data
|
|
|
|
AccelsData accels;
|
|
|
|
AccelsGet(&accels);
|
|
|
|
accel[0] = accels.x;
|
|
|
|
accel[1] = accels.y;
|
|
|
|
accel[2] = accels.z;
|
|
|
|
|
|
|
|
//rotate avg accels into earth frame and store it
|
|
|
|
AttitudeActualData attitudeActual;
|
|
|
|
AttitudeActualGet(&attitudeActual);
|
|
|
|
q[0]=attitudeActual.q1;
|
|
|
|
q[1]=attitudeActual.q2;
|
|
|
|
q[2]=attitudeActual.q3;
|
|
|
|
q[3]=attitudeActual.q4;
|
|
|
|
Quaternion2R(q, Rbe);
|
|
|
|
for (uint8_t i=0; i<3; i++){
|
|
|
|
accel_ned[i]=0;
|
|
|
|
for (uint8_t j=0; j<3; j++)
|
|
|
|
accel_ned[i] += Rbe[j][i]*accel[j];
|
|
|
|
}
|
|
|
|
accel_ned[2] += 9.81f;
|
|
|
|
|
|
|
|
NedAccelData accelData;
|
|
|
|
NedAccelGet(&accelData);
|
|
|
|
accelData.North = accel_ned[0];
|
|
|
|
accelData.East = accel_ned[1];
|
|
|
|
accelData.Down = accel_ned[2];
|
|
|
|
NedAccelSet(&accelData);
|
|
|
|
}
|
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
/**
|
|
|
|
* Bound input value between limits
|
|
|
|
*/
|
|
|
|
static float bound(float val, float min, float max)
|
|
|
|
{
|
|
|
|
if (val < min) {
|
|
|
|
val = min;
|
|
|
|
} else if (val > max) {
|
|
|
|
val = max;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
2012-05-08 09:28:19 +02:00
|
|
|
|
|
|
|
static void SettingsUpdatedCb(UAVObjEvent * ev)
|
|
|
|
{
|
2012-05-24 11:09:53 +02:00
|
|
|
VtolPathFollowerSettingsGet(&vtolpathfollowerSettings);
|
2012-05-08 09:28:19 +02:00
|
|
|
PathDesiredGet(&pathDesired);
|
|
|
|
}
|
|
|
|
|