2011-09-13 07:25:03 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file guidance.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
2013-05-18 14:17:26 +02:00
|
|
|
* @brief This module compared @ref PositionActuatl to @ref ActiveWaypoint
|
2011-09-13 07:25:03 +02:00
|
|
|
* 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
|
2013-05-18 19:36:45 +02:00
|
|
|
* Input object: PositionState
|
2011-09-13 07:25:03 +02:00
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-05-02 23:31:14 +02:00
|
|
|
#include <openpilot.h>
|
|
|
|
|
2012-02-09 06:02:29 +01:00
|
|
|
#include <math.h>
|
2013-12-07 23:16:22 +01:00
|
|
|
#include <pid.h>
|
2013-05-03 01:15:13 +02:00
|
|
|
#include <CoordinateConversions.h>
|
2013-05-18 19:36:45 +02:00
|
|
|
#include <attitudestate.h>
|
2013-05-03 01:15:13 +02:00
|
|
|
#include <altitudeholdsettings.h>
|
2013-05-18 14:17:26 +02:00
|
|
|
#include <altitudeholddesired.h> // object that will be updated by the module
|
2013-12-08 12:23:16 +01:00
|
|
|
#include <altitudeholdstatus.h>
|
2013-05-03 01:15:13 +02:00
|
|
|
#include <flightstatus.h>
|
|
|
|
#include <stabilizationdesired.h>
|
2013-05-18 19:36:45 +02:00
|
|
|
#include <accelstate.h>
|
2013-05-03 01:15:13 +02:00
|
|
|
#include <pios_constants.h>
|
2013-07-14 22:20:22 +02:00
|
|
|
#include <velocitystate.h>
|
|
|
|
#include <positionstate.h>
|
2011-09-13 07:25:03 +02:00
|
|
|
// Private constants
|
2013-12-07 23:16:22 +01:00
|
|
|
|
2013-12-07 23:31:26 +01:00
|
|
|
#define CALLBACK_PRIORITY CALLBACK_PRIORITY_LOW
|
|
|
|
#define CBTASK_PRIORITY CALLBACK_TASK_FLIGHTCONTROL
|
2013-12-07 23:16:22 +01:00
|
|
|
|
2013-07-17 10:38:44 +02:00
|
|
|
#define STACK_SIZE_BYTES 1024
|
2013-07-19 13:28:48 +02:00
|
|
|
#define DESIRED_UPDATE_RATE_MS 100 // milliseconds
|
2011-09-13 07:25:03 +02:00
|
|
|
// Private types
|
|
|
|
|
|
|
|
// Private variables
|
2013-12-07 23:16:22 +01:00
|
|
|
static DelayedCallbackInfo *altitudeHoldCBInfo;
|
2012-02-08 16:42:10 +01:00
|
|
|
static AltitudeHoldSettingsData altitudeHoldSettings;
|
2013-12-07 23:16:22 +01:00
|
|
|
static struct pid accelpid;
|
|
|
|
static float accelStateDown;
|
|
|
|
|
2011-09-13 07:25:03 +02:00
|
|
|
|
|
|
|
// Private functions
|
2013-12-07 23:16:22 +01:00
|
|
|
static void altitudeHoldTask(void);
|
2013-05-18 14:17:26 +02:00
|
|
|
static void SettingsUpdatedCb(UAVObjEvent *ev);
|
2013-12-07 23:16:22 +01:00
|
|
|
static void AccelStateUpdatedCb(UAVObjEvent *ev);
|
2011-09-13 07:25:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise the module, called on startup
|
|
|
|
* \returns 0 on success or -1 if initialisation failed
|
|
|
|
*/
|
|
|
|
int32_t AltitudeHoldStart()
|
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
// Start main task
|
2013-12-07 23:16:22 +01:00
|
|
|
SettingsUpdatedCb(NULL);
|
|
|
|
DelayedCallbackDispatch(altitudeHoldCBInfo);
|
2011-09-13 07:25:03 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
return 0;
|
2011-09-13 07:25:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise the module, called on startup
|
|
|
|
* \returns 0 on success or -1 if initialisation failed
|
|
|
|
*/
|
|
|
|
int32_t AltitudeHoldInitialize()
|
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
AltitudeHoldSettingsInitialize();
|
|
|
|
AltitudeHoldDesiredInitialize();
|
2013-12-08 12:23:16 +01:00
|
|
|
AltitudeHoldStatusInitialize();
|
2011-09-13 07:25:03 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
// Create object queue
|
2012-02-08 16:42:10 +01:00
|
|
|
|
2013-12-07 23:16:22 +01:00
|
|
|
altitudeHoldCBInfo = DelayedCallbackCreate(&altitudeHoldTask, CALLBACK_PRIORITY, CBTASK_PRIORITY, STACK_SIZE_BYTES);
|
2013-05-18 14:17:26 +02:00
|
|
|
AltitudeHoldSettingsConnectCallback(&SettingsUpdatedCb);
|
2013-12-07 23:16:22 +01:00
|
|
|
AccelStateConnectCallback(&AccelStateUpdatedCb);
|
2012-02-08 16:42:10 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
return 0;
|
2011-09-13 07:25:03 +02:00
|
|
|
}
|
2013-06-04 05:37:40 +02:00
|
|
|
MODULE_INITCALL(AltitudeHoldInitialize, AltitudeHoldStart);
|
2011-09-13 07:25:03 +02:00
|
|
|
|
2012-02-12 07:35:19 +01:00
|
|
|
|
2011-09-13 07:25:03 +02:00
|
|
|
/**
|
|
|
|
* Module thread, should not return.
|
|
|
|
*/
|
2013-12-07 23:16:22 +01:00
|
|
|
static void altitudeHoldTask(void)
|
2011-09-13 07:25:03 +02:00
|
|
|
{
|
2013-12-07 23:31:26 +01:00
|
|
|
static float startThrottle = 0.5f;
|
|
|
|
|
|
|
|
// make sure we run only when we are supposed to run
|
|
|
|
FlightStatusData flightStatus;
|
|
|
|
|
|
|
|
FlightStatusGet(&flightStatus);
|
|
|
|
switch (flightStatus.FlightMode) {
|
|
|
|
case FLIGHTSTATUS_FLIGHTMODE_ALTITUDEHOLD:
|
|
|
|
case FLIGHTSTATUS_FLIGHTMODE_ALTITUDEVARIO:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pid_zero(&accelpid);
|
|
|
|
StabilizationDesiredThrottleGet(&startThrottle);
|
|
|
|
DelayedCallbackSchedule(altitudeHoldCBInfo, DESIRED_UPDATE_RATE_MS, CALLBACK_UPDATEMODE_SOONER);
|
|
|
|
return;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-12-08 12:23:16 +01:00
|
|
|
AltitudeHoldStatusData altitudeHoldStatus;
|
|
|
|
AltitudeHoldStatusGet(&altitudeHoldStatus);
|
|
|
|
|
2013-12-07 23:31:26 +01:00
|
|
|
// do the actual control loop(s)
|
|
|
|
AltitudeHoldDesiredData altitudeHoldDesired;
|
|
|
|
AltitudeHoldDesiredGet(&altitudeHoldDesired);
|
|
|
|
float positionStateDown;
|
|
|
|
PositionStateDownGet(&positionStateDown);
|
|
|
|
float velocityStateDown;
|
|
|
|
VelocityStateDownGet(&velocityStateDown);
|
|
|
|
|
|
|
|
// altitude control loop
|
2013-12-08 13:07:26 +01:00
|
|
|
altitudeHoldStatus.VelocityDesired = altitudeHoldSettings.AltitudeP * (positionStateDown - altitudeHoldDesired.Altitude) + altitudeHoldDesired.Velocity;
|
2013-12-07 23:31:26 +01:00
|
|
|
|
|
|
|
// velocity control loop
|
2013-12-08 13:07:26 +01:00
|
|
|
altitudeHoldStatus.AccelerationDesired = altitudeHoldSettings.VelocityP * (velocityStateDown - altitudeHoldStatus.VelocityDesired) - 9.81f;
|
2013-12-07 23:31:26 +01:00
|
|
|
|
2013-12-08 13:06:28 +01:00
|
|
|
altitudeHoldStatus.AccelerationFiltered = accelStateDown;
|
|
|
|
|
2013-12-08 12:23:16 +01:00
|
|
|
AltitudeHoldStatusSet(&altitudeHoldStatus);
|
2013-12-07 23:31:26 +01:00
|
|
|
|
|
|
|
// compensate acceleration by rotation
|
|
|
|
// explanation: Rbe[2][2] is the Down component of a 0,0,1 vector rotated by Attitude.Q
|
|
|
|
// It is 1.0 for no rotation, 0.0 for a 90 degrees roll or pitch and -1.0 for a 180 degrees flipped rotation
|
|
|
|
// multiplying with 1/Rbe[2][2] therefore is the acceleration/thrust required to overcome gravity and achieve the wanted vertical
|
|
|
|
// acceleration at the current tilt angle.
|
|
|
|
// around 90 degrees rotation this is infinite (since no possible acceleration would get us up or down) so we set the error to zero to keep
|
|
|
|
// integrals from winding in any direction
|
|
|
|
|
|
|
|
AttitudeStateData attitudeState;
|
|
|
|
AttitudeStateGet(&attitudeState);
|
|
|
|
float Rbe[3][3];
|
|
|
|
Quaternion2R(&attitudeState.q1, Rbe);
|
|
|
|
|
2013-12-08 12:23:16 +01:00
|
|
|
float rotatedAccelDesired = altitudeHoldStatus.AccelerationDesired;
|
|
|
|
#if 0
|
2013-12-07 23:31:26 +01:00
|
|
|
if (fabsf(Rbe[2][2]) > 1e-3f) {
|
|
|
|
rotatedAccelDesired /= Rbe[2][2];
|
|
|
|
} else {
|
|
|
|
rotatedAccelDesired = accelStateDown;
|
|
|
|
}
|
2013-12-08 12:23:16 +01:00
|
|
|
#endif
|
2013-12-07 23:31:26 +01:00
|
|
|
|
|
|
|
// acceleration control loop
|
2013-12-08 10:44:17 +01:00
|
|
|
float throttle = startThrottle - pid_apply_setpoint(&accelpid, 1.0f, rotatedAccelDesired, accelStateDown, 1000 / DESIRED_UPDATE_RATE_MS);
|
2013-12-07 23:31:26 +01:00
|
|
|
|
|
|
|
if (throttle >= 1.0f) {
|
|
|
|
throttle = 1.0f;
|
|
|
|
}
|
|
|
|
if (throttle <= 0.0f) {
|
|
|
|
throttle = 0.0f;
|
|
|
|
}
|
|
|
|
StabilizationDesiredData stab;
|
|
|
|
StabilizationDesiredGet(&stab);
|
|
|
|
stab.Roll = altitudeHoldDesired.Roll;
|
|
|
|
stab.Pitch = altitudeHoldDesired.Pitch;
|
|
|
|
stab.Yaw = altitudeHoldDesired.Yaw;
|
|
|
|
stab.Throttle = throttle;
|
|
|
|
stab.StabilizationMode.Roll = STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE;
|
|
|
|
stab.StabilizationMode.Pitch = STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE;
|
|
|
|
stab.StabilizationMode.Yaw = STABILIZATIONDESIRED_STABILIZATIONMODE_AXISLOCK;
|
|
|
|
|
|
|
|
StabilizationDesiredSet(&stab);
|
|
|
|
|
|
|
|
DelayedCallbackSchedule(altitudeHoldCBInfo, DESIRED_UPDATE_RATE_MS, CALLBACK_UPDATEMODE_SOONER);
|
2013-12-07 23:16:22 +01:00
|
|
|
}
|
2013-06-10 23:48:08 +02:00
|
|
|
|
2013-12-07 23:16:22 +01:00
|
|
|
static void AccelStateUpdatedCb(__attribute__((unused)) UAVObjEvent *ev)
|
|
|
|
{
|
2013-12-07 23:31:26 +01:00
|
|
|
float down;
|
|
|
|
|
|
|
|
AccelStatezGet(&down);
|
|
|
|
accelStateDown = down * altitudeHoldSettings.AccelAlpha + accelStateDown * (1.0f - altitudeHoldSettings.AccelAlpha);
|
2011-09-13 07:25:03 +02:00
|
|
|
}
|
2012-01-03 00:19:14 +01:00
|
|
|
|
2013-05-05 09:02:24 +02:00
|
|
|
static void SettingsUpdatedCb(__attribute__((unused)) UAVObjEvent *ev)
|
2012-01-03 00:19:14 +01:00
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
AltitudeHoldSettingsGet(&altitudeHoldSettings);
|
2013-12-07 23:16:22 +01:00
|
|
|
pid_configure(&accelpid, altitudeHoldSettings.AccelPI.Kp, altitudeHoldSettings.AccelPI.Kp, 0, altitudeHoldSettings.AccelPI.Ilimit);
|
|
|
|
pid_zero(&accelpid);
|
2013-12-07 23:31:26 +01:00
|
|
|
accelStateDown = 0.0f;
|
2012-01-03 00:19:14 +01:00
|
|
|
}
|