2010-10-03 22:39:23 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup OpenPilotModules OpenPilot Modules
|
|
|
|
* @{
|
|
|
|
* @addtogroup StabilizationModule Stabilization Module
|
|
|
|
* @brief Stabilization PID loops in an airframe type independent manner
|
|
|
|
* @note This object updates the @ref ActuatorDesired "Actuator Desired" based on the
|
|
|
|
* PID loops on the @ref AttitudeDesired "Attitude Desired" and @ref AttitudeActual "Attitude Actual"
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file stabilization.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief Attitude stabilization module.
|
|
|
|
*
|
|
|
|
* @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"
|
|
|
|
#include "stabilization.h"
|
2010-10-08 17:38:23 +02:00
|
|
|
#include "stabilizationsettings.h"
|
2010-10-03 22:39:23 +02:00
|
|
|
#include "actuatordesired.h"
|
2010-10-24 22:00:02 +02:00
|
|
|
#include "ratedesired.h"
|
2012-07-30 04:08:46 +02:00
|
|
|
#include "relaytuning.h"
|
2012-07-29 22:21:14 +02:00
|
|
|
#include "relaytuningsettings.h"
|
2011-03-02 02:25:27 +01:00
|
|
|
#include "stabilizationdesired.h"
|
2010-10-03 22:39:23 +02:00
|
|
|
#include "attitudeactual.h"
|
2011-12-12 20:28:04 +01:00
|
|
|
#include "gyros.h"
|
2011-05-03 18:04:44 +02:00
|
|
|
#include "flightstatus.h"
|
|
|
|
#include "manualcontrol.h" // Just to get a macro
|
2011-03-02 02:25:34 +01:00
|
|
|
#include "CoordinateConversions.h"
|
2010-10-03 22:39:23 +02:00
|
|
|
|
|
|
|
// Private constants
|
2011-01-14 01:51:54 +01:00
|
|
|
#define MAX_QUEUE_SIZE 1
|
2011-02-02 09:57:34 +01:00
|
|
|
|
|
|
|
#if defined(PIOS_STABILIZATION_STACK_SIZE)
|
|
|
|
#define STACK_SIZE_BYTES PIOS_STABILIZATION_STACK_SIZE
|
|
|
|
#else
|
2011-01-09 01:03:19 +01:00
|
|
|
#define STACK_SIZE_BYTES 724
|
2011-02-02 09:57:34 +01:00
|
|
|
#endif
|
|
|
|
|
2010-10-03 22:39:23 +02:00
|
|
|
#define TASK_PRIORITY (tskIDLE_PRIORITY+4)
|
2010-10-24 22:00:05 +02:00
|
|
|
#define FAILSAFE_TIMEOUT_MS 30
|
2010-10-03 22:39:23 +02:00
|
|
|
|
2012-02-21 03:47:54 +01:00
|
|
|
enum {PID_RATE_ROLL, PID_RATE_PITCH, PID_RATE_YAW, PID_ROLL, PID_PITCH, PID_YAW, PID_VBAR_ROLL, PID_VBAR_PITCH, PID_VBAR_YAW, PID_MAX};
|
2010-10-03 22:39:23 +02:00
|
|
|
|
|
|
|
enum {ROLL,PITCH,YAW,MAX_AXES};
|
|
|
|
|
|
|
|
|
|
|
|
// Private types
|
|
|
|
typedef struct {
|
|
|
|
float p;
|
|
|
|
float i;
|
|
|
|
float d;
|
|
|
|
float iLim;
|
|
|
|
float iAccumulator;
|
|
|
|
float lastErr;
|
|
|
|
} pid_type;
|
|
|
|
|
|
|
|
// Private variables
|
|
|
|
static xTaskHandle taskHandle;
|
2010-10-08 17:38:23 +02:00
|
|
|
static StabilizationSettingsData settings;
|
2010-10-03 22:39:23 +02:00
|
|
|
static xQueueHandle queue;
|
2011-06-22 02:49:20 +02:00
|
|
|
float gyro_alpha = 0;
|
|
|
|
float gyro_filtered[3] = {0,0,0};
|
2011-06-24 08:57:12 +02:00
|
|
|
float axis_lock_accum[3] = {0,0,0};
|
2012-05-26 19:05:04 +02:00
|
|
|
float vbar_sensitivity[3] = {1, 1, 1};
|
2011-06-24 08:57:12 +02:00
|
|
|
uint8_t max_axis_lock = 0;
|
2011-06-25 17:38:37 +02:00
|
|
|
uint8_t max_axislock_rate = 0;
|
2011-07-03 17:45:01 +02:00
|
|
|
float weak_leveling_kp = 0;
|
|
|
|
uint8_t weak_leveling_max = 0;
|
2011-08-10 10:51:46 +02:00
|
|
|
bool lowThrottleZeroIntegral;
|
2012-02-20 14:29:43 +01:00
|
|
|
float vbar_integral[3] = {0, 0, 0};
|
2012-02-21 03:47:54 +01:00
|
|
|
float vbar_decay = 0.991f;
|
2010-10-03 22:39:23 +02:00
|
|
|
pid_type pids[PID_MAX];
|
2012-05-26 17:38:03 +02:00
|
|
|
int8_t vbar_gyros_suppress;
|
2012-02-22 01:02:37 +01:00
|
|
|
bool vbar_piro_comp = false;
|
2010-10-03 22:39:23 +02:00
|
|
|
|
2012-07-30 22:20:18 +02:00
|
|
|
// TODO: Move this to flash
|
|
|
|
static float sin_lookup[360];
|
|
|
|
|
2010-10-03 22:39:23 +02:00
|
|
|
// Private functions
|
|
|
|
static void stabilizationTask(void* parameters);
|
2012-02-20 14:29:43 +01:00
|
|
|
static float ApplyPid(pid_type * pid, const float err, float dT);
|
2012-07-31 03:55:43 +02:00
|
|
|
static float bound(float val, float range);
|
2010-10-03 22:39:23 +02:00
|
|
|
static void ZeroPids(void);
|
|
|
|
static void SettingsUpdatedCb(UAVObjEvent * ev);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module initialization
|
|
|
|
*/
|
2011-06-20 07:35:40 +02:00
|
|
|
int32_t StabilizationStart()
|
2010-10-03 22:39:23 +02:00
|
|
|
{
|
|
|
|
// Initialize variables
|
2012-05-22 22:19:31 +02:00
|
|
|
// Create object queue
|
|
|
|
queue = xQueueCreate(MAX_QUEUE_SIZE, sizeof(UAVObjEvent));
|
|
|
|
|
2012-07-30 22:20:18 +02:00
|
|
|
for(uint32_t i = 0; i < 360; i++)
|
|
|
|
sin_lookup[i] = sinf((float)i * 2 * M_PI / 360.0f);
|
|
|
|
|
2012-05-22 22:19:31 +02:00
|
|
|
// Listen for updates.
|
|
|
|
// AttitudeActualConnectQueue(queue);
|
|
|
|
GyrosConnectQueue(queue);
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2012-05-22 22:19:31 +02:00
|
|
|
StabilizationSettingsConnectCallback(SettingsUpdatedCb);
|
|
|
|
SettingsUpdatedCb(StabilizationSettingsHandle());
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2011-06-20 07:35:40 +02:00
|
|
|
// Start main task
|
|
|
|
xTaskCreate(stabilizationTask, (signed char*)"Stabilization", STACK_SIZE_BYTES/4, NULL, TASK_PRIORITY, &taskHandle);
|
|
|
|
TaskMonitorAdd(TASKINFO_RUNNING_STABILIZATION, taskHandle);
|
|
|
|
PIOS_WDG_RegisterFlag(PIOS_WDG_STABILIZATION);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-03 22:39:23 +02:00
|
|
|
/**
|
|
|
|
* Module initialization
|
|
|
|
*/
|
|
|
|
int32_t StabilizationInitialize()
|
|
|
|
{
|
|
|
|
// Initialize variables
|
2011-06-18 18:59:02 +02:00
|
|
|
StabilizationSettingsInitialize();
|
|
|
|
ActuatorDesiredInitialize();
|
2012-07-30 04:08:46 +02:00
|
|
|
RelayTuningSettingsInitialize();
|
|
|
|
RelayTuningInitialize();
|
2011-06-18 18:59:02 +02:00
|
|
|
#if defined(DIAGNOSTICS)
|
|
|
|
RateDesiredInitialize();
|
|
|
|
#endif
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2010-10-03 22:39:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-13 05:44:32 +02:00
|
|
|
MODULE_INITCALL(StabilizationInitialize, StabilizationStart)
|
2011-06-20 07:35:40 +02:00
|
|
|
|
2010-10-03 22:39:23 +02:00
|
|
|
/**
|
|
|
|
* Module task
|
|
|
|
*/
|
|
|
|
static void stabilizationTask(void* parameters)
|
|
|
|
{
|
|
|
|
UAVObjEvent ev;
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2012-01-25 16:56:07 +01:00
|
|
|
uint32_t timeval = PIOS_DELAY_GetRaw();
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2010-10-03 22:39:23 +02:00
|
|
|
ActuatorDesiredData actuatorDesired;
|
2011-03-02 02:25:27 +01:00
|
|
|
StabilizationDesiredData stabDesired;
|
2010-10-24 22:00:02 +02:00
|
|
|
RateDesiredData rateDesired;
|
2010-10-03 22:39:23 +02:00
|
|
|
AttitudeActualData attitudeActual;
|
2011-12-12 20:28:04 +01:00
|
|
|
GyrosData gyrosData;
|
2011-05-03 18:04:44 +02:00
|
|
|
FlightStatusData flightStatus;
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2010-10-08 17:38:23 +02:00
|
|
|
SettingsUpdatedCb((UAVObjEvent *) NULL);
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2010-10-03 22:39:23 +02:00
|
|
|
// Main task loop
|
|
|
|
ZeroPids();
|
|
|
|
while(1) {
|
2012-02-20 14:29:43 +01:00
|
|
|
float dT;
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2011-01-10 01:16:30 +01:00
|
|
|
PIOS_WDG_UpdateFlag(PIOS_WDG_STABILIZATION);
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2010-12-20 17:38:17 +01:00
|
|
|
// Wait until the AttitudeRaw object is updated, if a timeout then go to failsafe
|
2010-10-03 22:39:23 +02:00
|
|
|
if ( xQueueReceive(queue, &ev, FAILSAFE_TIMEOUT_MS / portTICK_RATE_MS) != pdTRUE )
|
|
|
|
{
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_STABILIZATION,SYSTEMALARMS_ALARM_WARNING);
|
2010-11-23 18:13:42 +01:00
|
|
|
continue;
|
2011-06-23 01:51:25 +02:00
|
|
|
}
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2012-01-25 16:56:07 +01:00
|
|
|
dT = PIOS_DELAY_DiffuS(timeval) * 1.0e-6f;
|
|
|
|
timeval = PIOS_DELAY_GetRaw();
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2011-05-03 18:04:44 +02:00
|
|
|
FlightStatusGet(&flightStatus);
|
2011-03-02 02:25:27 +01:00
|
|
|
StabilizationDesiredGet(&stabDesired);
|
2010-10-03 22:39:23 +02:00
|
|
|
AttitudeActualGet(&attitudeActual);
|
2011-12-12 20:28:04 +01:00
|
|
|
GyrosGet(&gyrosData);
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2011-06-17 16:50:10 +02:00
|
|
|
#if defined(DIAGNOSTICS)
|
|
|
|
RateDesiredGet(&rateDesired);
|
|
|
|
#endif
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2011-03-02 02:25:34 +01:00
|
|
|
#if defined(PIOS_QUATERNION_STABILIZATION)
|
|
|
|
// Quaternion calculation of error in each axis. Uses more memory.
|
2011-03-02 02:25:38 +01:00
|
|
|
float rpy_desired[3];
|
2011-03-02 02:25:34 +01:00
|
|
|
float q_desired[4];
|
|
|
|
float q_error[4];
|
|
|
|
float local_error[3];
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2011-03-02 02:25:38 +01:00
|
|
|
// Essentially zero errors for anything in rate or none
|
|
|
|
if(stabDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_ROLL] == STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE)
|
2011-06-23 01:51:25 +02:00
|
|
|
rpy_desired[0] = stabDesired.Roll;
|
|
|
|
else
|
2011-03-02 02:25:38 +01:00
|
|
|
rpy_desired[0] = attitudeActual.Roll;
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2011-03-02 02:25:38 +01:00
|
|
|
if(stabDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_PITCH] == STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE)
|
|
|
|
rpy_desired[1] = stabDesired.Pitch;
|
|
|
|
else
|
|
|
|
rpy_desired[1] = attitudeActual.Pitch;
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2011-03-02 02:25:38 +01:00
|
|
|
if(stabDesired.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_YAW] == STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE)
|
|
|
|
rpy_desired[2] = stabDesired.Yaw;
|
|
|
|
else
|
|
|
|
rpy_desired[2] = attitudeActual.Yaw;
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2011-03-02 02:25:38 +01:00
|
|
|
RPY2Quaternion(rpy_desired, q_desired);
|
2011-03-02 02:25:34 +01:00
|
|
|
quat_inverse(q_desired);
|
2011-03-02 02:25:38 +01:00
|
|
|
quat_mult(q_desired, &attitudeActual.q1, q_error);
|
2011-03-02 02:25:34 +01:00
|
|
|
quat_inverse(q_error);
|
|
|
|
Quaternion2RPY(q_error, local_error);
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2011-03-02 02:25:34 +01:00
|
|
|
#else
|
|
|
|
// Simpler algorithm for CC, less memory
|
|
|
|
float local_error[3] = {stabDesired.Roll - attitudeActual.Roll,
|
|
|
|
stabDesired.Pitch - attitudeActual.Pitch,
|
|
|
|
stabDesired.Yaw - attitudeActual.Yaw};
|
2012-03-26 04:16:54 +02:00
|
|
|
local_error[2] = fmodf(local_error[2] + 180, 360) - 180;
|
2011-03-02 02:25:34 +01:00
|
|
|
#endif
|
2011-06-23 01:51:25 +02:00
|
|
|
|
2011-12-12 20:28:04 +01:00
|
|
|
gyro_filtered[0] = gyro_filtered[0] * gyro_alpha + gyrosData.x * (1 - gyro_alpha);
|
|
|
|
gyro_filtered[1] = gyro_filtered[1] * gyro_alpha + gyrosData.y * (1 - gyro_alpha);
|
|
|
|
gyro_filtered[2] = gyro_filtered[2] * gyro_alpha + gyrosData.z * (1 - gyro_alpha);
|
2011-06-23 01:51:25 +02:00
|
|
|
|
2011-03-02 02:25:27 +01:00
|
|
|
float *attitudeDesiredAxis = &stabDesired.Roll;
|
2010-10-05 16:51:33 +02:00
|
|
|
float *actuatorDesiredAxis = &actuatorDesired.Roll;
|
2010-10-24 22:00:02 +02:00
|
|
|
float *rateDesiredAxis = &rateDesired.Roll;
|
2011-06-23 01:51:25 +02:00
|
|
|
|
2012-07-31 03:55:43 +02:00
|
|
|
ActuatorDesiredGet(&actuatorDesired);
|
|
|
|
|
|
|
|
// A flag to track which stabilization mode each axis is in
|
|
|
|
static uint8_t previous_mode[MAX_AXES] = {255,255,255};
|
|
|
|
bool error = false;
|
|
|
|
|
|
|
|
//Run the selected stabilization algorithm on each axis:
|
2011-06-24 08:57:12 +02:00
|
|
|
for(uint8_t i=0; i< MAX_AXES; i++)
|
2010-10-05 16:51:33 +02:00
|
|
|
{
|
2012-07-31 03:55:43 +02:00
|
|
|
// Check whether this axis mode needs to be reinitialized
|
|
|
|
bool reinit = (stabDesired.StabilizationMode[i] == previous_mode[i]);
|
|
|
|
previous_mode[i] = stabDesired.StabilizationMode[i];
|
|
|
|
|
|
|
|
// Apply the selected control law
|
2011-06-24 08:57:12 +02:00
|
|
|
switch(stabDesired.StabilizationMode[i])
|
2010-10-05 16:51:33 +02:00
|
|
|
{
|
2011-03-02 02:25:30 +01:00
|
|
|
case STABILIZATIONDESIRED_STABILIZATIONMODE_RATE:
|
2012-07-31 03:55:43 +02:00
|
|
|
if(reinit)
|
|
|
|
pids[PID_RATE_ROLL + i].iAccumulator = 0;
|
|
|
|
|
|
|
|
// Store to rate desired variable for storing to UAVO
|
|
|
|
rateDesiredAxis[i] = bound(attitudeDesiredAxis[i], settings.ManualRate[i]);
|
|
|
|
|
|
|
|
// Compute the inner loop
|
|
|
|
actuatorDesiredAxis[i] = ApplyPid(&pids[PID_RATE_ROLL + i], rateDesiredAxis[i] - gyro_filtered[i], dT);
|
|
|
|
actuatorDesiredAxis[i] = bound(actuatorDesiredAxis[i],1.0f);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE:
|
|
|
|
if(reinit) {
|
|
|
|
pids[PID_ROLL + i].iAccumulator = 0;
|
|
|
|
pids[PID_RATE_ROLL + i].iAccumulator = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the outer loop
|
|
|
|
rateDesiredAxis[i] = ApplyPid(&pids[PID_ROLL + i], local_error[i], dT);
|
|
|
|
rateDesiredAxis[i] = bound(rateDesiredAxis[i], settings.MaximumRate[i]);
|
|
|
|
|
|
|
|
// Compute the inner loop
|
|
|
|
actuatorDesiredAxis[i] = ApplyPid(&pids[PID_RATE_ROLL + i], rateDesiredAxis[i] - gyro_filtered[i], dT);
|
|
|
|
actuatorDesiredAxis[i] = bound(actuatorDesiredAxis[i],1.0f);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2012-05-26 17:39:35 +02:00
|
|
|
case STABILIZATIONDESIRED_STABILIZATIONMODE_VIRTUALBAR:
|
2012-07-31 03:55:43 +02:00
|
|
|
{
|
|
|
|
if(reinit)
|
|
|
|
vbar_integral[i] = 0;
|
|
|
|
|
2011-06-24 08:57:12 +02:00
|
|
|
rateDesiredAxis[i] = attitudeDesiredAxis[i];
|
2011-09-11 23:33:38 +02:00
|
|
|
|
2012-07-31 03:55:43 +02:00
|
|
|
// Track the angle of the virtual flybar which includes a slow decay
|
|
|
|
vbar_integral[i] = vbar_integral[i] * vbar_decay + gyro_filtered[i] * dT;
|
|
|
|
vbar_integral[i] = bound(vbar_integral[i], settings.VbarMaxAngle);
|
|
|
|
|
|
|
|
// Command signal can indicate how much to disregard the gyro feedback (fast flips)
|
|
|
|
float gyro_gain = 1.0f;
|
|
|
|
if (vbar_gyros_suppress > 0) {
|
|
|
|
gyro_gain = (1.0f - fabs(rateDesiredAxis[i]) * vbar_gyros_suppress / 100.0f);
|
|
|
|
gyro_gain = (gyro_gain < 0) ? 0 : gyro_gain;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command signal is composed of stick input added to the gyro and virtual flybar
|
|
|
|
actuatorDesiredAxis[i] = rateDesiredAxis[i] * vbar_sensitivity[i] -
|
|
|
|
gyro_gain * (vbar_integral[i] * pids[PID_VBAR_ROLL + i].i +
|
|
|
|
gyro_filtered[i] * pids[PID_VBAR_ROLL + i].p);
|
2011-06-23 01:51:25 +02:00
|
|
|
|
2012-07-31 03:55:43 +02:00
|
|
|
actuatorDesiredAxis[i] = bound(actuatorDesiredAxis[i],1.0f);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2011-07-03 17:45:01 +02:00
|
|
|
case STABILIZATIONDESIRED_STABILIZATIONMODE_WEAKLEVELING:
|
|
|
|
{
|
2012-07-31 03:55:43 +02:00
|
|
|
if (reinit)
|
|
|
|
pids[PID_RATE_ROLL + i].iAccumulator = 0;
|
2011-07-03 17:45:01 +02:00
|
|
|
|
2012-07-31 03:55:43 +02:00
|
|
|
float weak_leveling = local_error[i] * weak_leveling_kp;
|
|
|
|
weak_leveling = bound(weak_leveling, weak_leveling_max);
|
2011-07-03 17:45:01 +02:00
|
|
|
|
2012-07-31 03:55:43 +02:00
|
|
|
// Compute desired rate as input biased towards leveling
|
2011-07-03 17:45:01 +02:00
|
|
|
rateDesiredAxis[i] = attitudeDesiredAxis[i] + weak_leveling;
|
2012-07-31 03:55:43 +02:00
|
|
|
actuatorDesiredAxis[i] = ApplyPid(&pids[PID_RATE_ROLL + i], rateDesiredAxis[i] - gyro_filtered[i], dT);
|
|
|
|
actuatorDesiredAxis[i] = bound(actuatorDesiredAxis[i],1.0f);
|
2011-07-03 17:45:01 +02:00
|
|
|
|
2011-03-02 02:25:30 +01:00
|
|
|
break;
|
2011-07-03 17:45:01 +02:00
|
|
|
}
|
2012-07-31 16:49:17 +02:00
|
|
|
case STABILIZATIONDESIRED_STABILIZATIONMODE_RELAY:
|
|
|
|
case STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE:
|
|
|
|
rateDesiredAxis[i] = ApplyPid(&pids[PID_ROLL + i], local_error[i], dT);
|
|
|
|
|
|
|
|
if(rateDesiredAxis[i] > settings.MaximumRate[i])
|
|
|
|
rateDesiredAxis[i] = settings.MaximumRate[i];
|
|
|
|
else if(rateDesiredAxis[i] < -settings.MaximumRate[i])
|
|
|
|
rateDesiredAxis[i] = -settings.MaximumRate[i];
|
|
|
|
|
|
|
|
|
|
|
|
axis_lock_accum[i] = 0;
|
|
|
|
break;
|
2011-06-24 08:57:12 +02:00
|
|
|
|
|
|
|
case STABILIZATIONDESIRED_STABILIZATIONMODE_AXISLOCK:
|
2012-07-31 03:55:43 +02:00
|
|
|
if (reinit)
|
|
|
|
pids[PID_RATE_ROLL + i].iAccumulator = 0;
|
|
|
|
|
2011-06-25 17:38:37 +02:00
|
|
|
if(fabs(attitudeDesiredAxis[i]) > max_axislock_rate) {
|
|
|
|
// While getting strong commands act like rate mode
|
|
|
|
rateDesiredAxis[i] = attitudeDesiredAxis[i];
|
|
|
|
axis_lock_accum[i] = 0;
|
|
|
|
} else {
|
|
|
|
// For weaker commands or no command simply attitude lock (almost) on no gyro change
|
|
|
|
axis_lock_accum[i] += (attitudeDesiredAxis[i] - gyro_filtered[i]) * dT;
|
2012-07-31 03:55:43 +02:00
|
|
|
axis_lock_accum[i] = bound(axis_lock_accum[i], max_axis_lock);
|
2012-02-20 14:29:43 +01:00
|
|
|
rateDesiredAxis[i] = ApplyPid(&pids[PID_ROLL + i], axis_lock_accum[i], dT);
|
2011-06-25 17:38:37 +02:00
|
|
|
}
|
2011-09-11 19:27:17 +02:00
|
|
|
|
2012-07-31 03:55:43 +02:00
|
|
|
rateDesiredAxis[i] = bound(rateDesiredAxis[i], settings.MaximumRate[i]);
|
2011-06-23 01:51:25 +02:00
|
|
|
|
2012-07-31 03:55:43 +02:00
|
|
|
actuatorDesiredAxis[i] = ApplyPid(&pids[PID_RATE_ROLL + i], rateDesiredAxis[i] - gyro_filtered[i], dT);
|
|
|
|
actuatorDesiredAxis[i] = bound(actuatorDesiredAxis[i],1.0f);
|
2012-02-22 01:02:37 +01:00
|
|
|
|
2012-07-31 03:55:43 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case STABILIZATIONDESIRED_STABILIZATIONMODE_RELAYATTITUDE:
|
2012-07-29 22:21:14 +02:00
|
|
|
{
|
2012-07-31 03:55:43 +02:00
|
|
|
RelayTuningData relay;
|
|
|
|
RelayTuningGet(&relay);
|
|
|
|
|
|
|
|
static bool rateRelayRunning[MAX_AXES];
|
|
|
|
|
|
|
|
// On first run initialize estimates to something reasonable
|
|
|
|
if(reinit) {
|
|
|
|
pids[PID_ROLL + i].iAccumulator = 0;
|
|
|
|
rateRelayRunning[i] = false;
|
|
|
|
relay.Period[i] = 200;
|
|
|
|
relay.Gain[i] = 0;
|
|
|
|
}
|
|
|
|
// Replace the rate PID with a relay to measure the critical properties of this axis
|
|
|
|
// i.e. period and gain
|
|
|
|
|
|
|
|
// Compute the outer loop
|
|
|
|
rateDesiredAxis[i] = ApplyPid(&pids[PID_ROLL + i], local_error[i], dT);
|
|
|
|
rateDesiredAxis[i] = bound(rateDesiredAxis[i], settings.MaximumRate[i]);
|
|
|
|
|
|
|
|
// Store to rate desired variable for storing to UAVO
|
|
|
|
rateDesiredAxis[i] = bound(attitudeDesiredAxis[i], settings.ManualRate[i]);
|
|
|
|
|
2012-07-30 04:08:46 +02:00
|
|
|
RelayTuningSettingsData relaySettings;
|
|
|
|
RelayTuningSettingsGet(&relaySettings);
|
2012-07-29 22:21:14 +02:00
|
|
|
float error = rateDesiredAxis[i] - gyro_filtered[i];
|
2012-07-30 04:08:46 +02:00
|
|
|
float command = error > 0 ? relaySettings.Amplitude : -relaySettings.Amplitude;
|
2012-07-31 03:55:43 +02:00
|
|
|
actuatorDesiredAxis[i] = bound(command,1.0f);
|
2012-07-30 04:08:46 +02:00
|
|
|
|
|
|
|
static bool high = false;
|
|
|
|
static portTickType lastHighTime;
|
|
|
|
static portTickType lastLowTime;
|
|
|
|
portTickType thisTime = xTaskGetTickCount();
|
|
|
|
|
2012-07-30 22:20:18 +02:00
|
|
|
static float accum_sin, accum_cos;
|
2012-07-30 04:08:46 +02:00
|
|
|
static uint32_t accumulated = 0;
|
|
|
|
|
|
|
|
const uint16_t DEGLITCH_TIME = 20; // ms
|
|
|
|
const float AMPLITUDE_ALPHA = 0.95;
|
|
|
|
const float PERIOD_ALPHA = 0.95;
|
|
|
|
|
|
|
|
// Make sure the period can't go below limit
|
|
|
|
if (relay.Period[i] < DEGLITCH_TIME)
|
|
|
|
relay.Period[i] = DEGLITCH_TIME;
|
|
|
|
|
|
|
|
// Project the error onto a sine and cosine of the same frequency
|
|
|
|
// to accumulate the average amplitude
|
|
|
|
float dT = thisTime - lastHighTime;
|
2012-07-30 22:20:18 +02:00
|
|
|
uint32_t phase = 360 * dT / relay.Period[i];
|
|
|
|
if(phase >= 360)
|
|
|
|
phase = 1;
|
|
|
|
accum_sin += sin_lookup[phase] * error;
|
|
|
|
accum_cos += sin_lookup[(phase + 90) % 360] * error;
|
2012-07-30 04:08:46 +02:00
|
|
|
accumulated ++;
|
|
|
|
|
|
|
|
// Make susre we've had enough time since last transition then check for a change in the output
|
|
|
|
bool hysteresis = (high ? (thisTime - lastHighTime) : (thisTime - lastLowTime)) > DEGLITCH_TIME;
|
|
|
|
if ( !high && hysteresis && error > 0 ){ /* RISE DETECTED */
|
2012-07-30 22:20:18 +02:00
|
|
|
float this_amplitude = 2 * sqrtf(accum_sin*accum_sin + accum_cos*accum_cos) / accumulated;
|
2012-07-31 03:55:43 +02:00
|
|
|
float this_gain = this_amplitude / relaySettings.Amplitude;
|
|
|
|
|
2012-07-30 04:08:46 +02:00
|
|
|
accumulated = 0;
|
|
|
|
accum_sin = 0;
|
2012-07-30 22:20:18 +02:00
|
|
|
accum_cos = 0;
|
2012-07-30 04:08:46 +02:00
|
|
|
|
2012-07-30 22:20:18 +02:00
|
|
|
if(rateRelayRunning[i] == false) {
|
|
|
|
rateRelayRunning[i] = true;
|
|
|
|
relay.Period[i] = 200;
|
2012-07-31 03:55:43 +02:00
|
|
|
relay.Gain[i] = 0;
|
2012-07-30 22:20:18 +02:00
|
|
|
} else {
|
|
|
|
// Low pass filter each amplitude and period
|
2012-07-31 03:55:43 +02:00
|
|
|
relay.Gain[i] = relay.Gain[i] * AMPLITUDE_ALPHA + this_gain * (1 - AMPLITUDE_ALPHA);
|
2012-07-30 22:20:18 +02:00
|
|
|
relay.Period[i] = relay.Period[i] * PERIOD_ALPHA + dT * (1 - PERIOD_ALPHA);
|
|
|
|
}
|
2012-07-30 04:08:46 +02:00
|
|
|
lastHighTime = thisTime;
|
|
|
|
high = true;
|
|
|
|
RelayTuningSet(&relay);
|
|
|
|
} else if ( high && hysteresis && error < 0 ) { /* FALL DETECTED */
|
|
|
|
lastLowTime = thisTime;
|
|
|
|
high = false;
|
|
|
|
}
|
|
|
|
|
2012-07-29 22:21:14 +02:00
|
|
|
break;
|
|
|
|
}
|
2012-07-31 03:55:43 +02:00
|
|
|
case STABILIZATIONDESIRED_STABILIZATIONMODE_RELAYRATE:
|
2010-10-05 16:51:33 +02:00
|
|
|
{
|
2012-07-31 03:55:43 +02:00
|
|
|
RelayTuningData relay;
|
|
|
|
RelayTuningGet(&relay);
|
2012-02-20 14:29:43 +01:00
|
|
|
|
2012-07-31 03:55:43 +02:00
|
|
|
static bool rateRelayRunning[MAX_AXES];
|
|
|
|
|
|
|
|
// On first run initialize estimates to something reasonable
|
|
|
|
if(reinit) {
|
|
|
|
pids[PID_ROLL + i].iAccumulator = 0;
|
|
|
|
rateRelayRunning[i] = false;
|
|
|
|
relay.Period[i] = 200;
|
|
|
|
relay.Gain[i] = 0;
|
2012-02-21 21:23:54 +01:00
|
|
|
}
|
2012-02-20 14:29:43 +01:00
|
|
|
|
2012-07-31 03:55:43 +02:00
|
|
|
// Replace the rate PID with a relay to measure the critical properties of this axis
|
|
|
|
// i.e. period and gain
|
|
|
|
|
|
|
|
// Store to rate desired variable for storing to UAVO
|
|
|
|
rateDesiredAxis[i] = bound(attitudeDesiredAxis[i], settings.ManualRate[i]);
|
|
|
|
|
|
|
|
RelayTuningSettingsData relaySettings;
|
|
|
|
RelayTuningSettingsGet(&relaySettings);
|
|
|
|
float error = rateDesiredAxis[i] - gyro_filtered[i];
|
|
|
|
float command = error > 0 ? relaySettings.Amplitude : -relaySettings.Amplitude;
|
|
|
|
actuatorDesiredAxis[i] = bound(command,1.0);
|
|
|
|
|
|
|
|
static bool high = false;
|
|
|
|
static portTickType lastHighTime;
|
|
|
|
static portTickType lastLowTime;
|
|
|
|
portTickType thisTime = xTaskGetTickCount();
|
|
|
|
|
|
|
|
static float accum_sin, accum_cos;
|
|
|
|
static uint32_t accumulated = 0;
|
|
|
|
|
|
|
|
const uint16_t DEGLITCH_TIME = 20; // ms
|
|
|
|
const float AMPLITUDE_ALPHA = 0.95;
|
|
|
|
const float PERIOD_ALPHA = 0.95;
|
|
|
|
|
|
|
|
// Make sure the period can't go below limit
|
|
|
|
if (relay.Period[i] < DEGLITCH_TIME)
|
|
|
|
relay.Period[i] = DEGLITCH_TIME;
|
|
|
|
|
|
|
|
// Project the error onto a sine and cosine of the same frequency
|
|
|
|
// to accumulate the average amplitude
|
|
|
|
float dT = thisTime - lastHighTime;
|
|
|
|
uint32_t phase = 360 * dT / relay.Period[i];
|
|
|
|
if(phase >= 360)
|
|
|
|
phase = 1;
|
|
|
|
accum_sin += sin_lookup[phase] * error;
|
|
|
|
accum_cos += sin_lookup[(phase + 90) % 360] * error;
|
|
|
|
accumulated ++;
|
|
|
|
|
|
|
|
// Make susre we've had enough time since last transition then check for a change in the output
|
|
|
|
bool hysteresis = (high ? (thisTime - lastHighTime) : (thisTime - lastLowTime)) > DEGLITCH_TIME;
|
|
|
|
if ( !high && hysteresis && error > 0 ){ /* RISE DETECTED */
|
|
|
|
float this_amplitude = 2 * sqrtf(accum_sin*accum_sin + accum_cos*accum_cos) / accumulated;
|
|
|
|
float this_gain = this_amplitude / relaySettings.Amplitude;
|
|
|
|
|
|
|
|
accumulated = 0;
|
|
|
|
accum_sin = 0;
|
|
|
|
accum_cos = 0;
|
|
|
|
|
|
|
|
if(rateRelayRunning[i] == false) {
|
|
|
|
rateRelayRunning[i] = true;
|
|
|
|
relay.Period[i] = 200;
|
|
|
|
relay.Gain[i] = 0;
|
|
|
|
} else {
|
|
|
|
// Low pass filter each amplitude and period
|
|
|
|
relay.Gain[i] = relay.Gain[i] * AMPLITUDE_ALPHA + this_gain * (1 - AMPLITUDE_ALPHA);
|
|
|
|
relay.Period[i] = relay.Period[i] * PERIOD_ALPHA + dT * (1 - PERIOD_ALPHA);
|
|
|
|
}
|
|
|
|
lastHighTime = thisTime;
|
|
|
|
high = true;
|
|
|
|
RelayTuningSet(&relay);
|
|
|
|
} else if ( high && hysteresis && error < 0 ) { /* FALL DETECTED */
|
|
|
|
lastLowTime = thisTime;
|
|
|
|
high = false;
|
|
|
|
}
|
2011-03-02 02:25:27 +01:00
|
|
|
}
|
|
|
|
break;
|
2011-06-23 01:51:25 +02:00
|
|
|
|
2012-07-31 03:55:43 +02:00
|
|
|
case STABILIZATIONDESIRED_STABILIZATIONMODE_NONE:
|
|
|
|
actuatorDesiredAxis[i] = bound(attitudeDesiredAxis[i],1.0f);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = true;
|
|
|
|
break;
|
2010-10-05 16:51:33 +02:00
|
|
|
}
|
2010-10-03 22:39:23 +02:00
|
|
|
}
|
2011-06-23 01:51:25 +02:00
|
|
|
|
2012-07-31 03:55:43 +02:00
|
|
|
// Piro compensation rotates the virtual flybar by yaw step to keep it
|
|
|
|
// rotated to external world
|
|
|
|
if (vbar_piro_comp) {
|
|
|
|
const float F_PI = 3.14159f;
|
|
|
|
float cy = cosf(gyro_filtered[2] / 180.0f * F_PI * dT);
|
|
|
|
float sy = sinf(gyro_filtered[2] / 180.0f * F_PI * dT);
|
|
|
|
|
|
|
|
float vbar_pitch = cy * vbar_integral[1] - sy * vbar_integral[0];
|
|
|
|
float vbar_roll = sy * vbar_integral[1] + cy * vbar_integral[0];
|
|
|
|
|
|
|
|
vbar_integral[1] = vbar_pitch;
|
|
|
|
vbar_integral[0] = vbar_roll;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(DIAGNOSTICS)
|
|
|
|
RateDesiredSet(&rateDesired);
|
|
|
|
#endif
|
|
|
|
|
2010-10-08 17:38:28 +02:00
|
|
|
// Save dT
|
|
|
|
actuatorDesired.UpdateTime = dT * 1000;
|
2012-07-31 03:55:43 +02:00
|
|
|
actuatorDesired.Throttle = stabDesired.Throttle;
|
2011-06-23 01:51:25 +02:00
|
|
|
|
2012-07-31 03:55:43 +02:00
|
|
|
if(PARSE_FLIGHT_MODE(flightStatus.FlightMode) != FLIGHTMODE_MANUAL) {
|
2010-10-03 22:39:23 +02:00
|
|
|
ActuatorDesiredSet(&actuatorDesired);
|
2012-07-31 03:55:43 +02:00
|
|
|
} else {
|
|
|
|
// Force all axes to reinitialize when engaged
|
|
|
|
for(uint8_t i=0; i< MAX_AXES; i++)
|
|
|
|
previous_mode[i] = 255;
|
2010-10-03 22:39:23 +02:00
|
|
|
}
|
2011-06-23 01:51:25 +02:00
|
|
|
|
2011-05-03 18:04:44 +02:00
|
|
|
if(flightStatus.Armed != FLIGHTSTATUS_ARMED_ARMED ||
|
2012-07-31 03:55:43 +02:00
|
|
|
(lowThrottleZeroIntegral && stabDesired.Throttle < 0))
|
2010-10-03 22:39:23 +02:00
|
|
|
{
|
2012-07-31 03:55:43 +02:00
|
|
|
// Force all axes to reinitialize when engaged
|
|
|
|
for(uint8_t i=0; i< MAX_AXES; i++)
|
|
|
|
previous_mode[i] = 255;
|
2010-10-03 22:39:23 +02:00
|
|
|
}
|
2011-06-23 01:51:25 +02:00
|
|
|
|
2012-07-31 03:55:43 +02:00
|
|
|
// Clear or set alarms. Done like this to prevent toggline each cycle
|
|
|
|
// and hammering system alarms
|
|
|
|
if (error)
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_STABILIZATION,SYSTEMALARMS_ALARM_ERROR);
|
|
|
|
else
|
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_STABILIZATION);
|
2010-10-03 22:39:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-31 03:55:43 +02:00
|
|
|
/**
|
|
|
|
* Update one of the PID structures with the input error and timestep
|
|
|
|
* @param pid Pointer to the PID structure
|
|
|
|
* @param[in] err The error on for this controller
|
|
|
|
* @param[in] dT The time step since the last update
|
|
|
|
*/
|
2012-02-20 14:29:43 +01:00
|
|
|
float ApplyPid(pid_type * pid, const float err, float dT)
|
2010-10-03 22:39:23 +02:00
|
|
|
{
|
|
|
|
float diff = (err - pid->lastErr);
|
|
|
|
pid->lastErr = err;
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2011-06-23 01:51:25 +02:00
|
|
|
// Scale up accumulator by 1000 while computing to avoid losing precision
|
2011-11-26 22:52:25 +01:00
|
|
|
pid->iAccumulator += err * (pid->i * dT * 1000.0f);
|
2012-07-31 03:55:43 +02:00
|
|
|
pid->iAccumulator = bound(pid->iAccumulator, pid->iLim * 1000.0f);
|
2011-11-26 22:52:25 +01:00
|
|
|
return ((err * pid->p) + pid->iAccumulator / 1000.0f + (diff * pid->d / dT));
|
2010-10-03 22:39:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-31 03:55:43 +02:00
|
|
|
/**
|
|
|
|
* Clear the accumulators and derivatives for all the axes
|
|
|
|
*/
|
2010-10-03 22:39:23 +02:00
|
|
|
static void ZeroPids(void)
|
|
|
|
{
|
2010-10-08 17:38:23 +02:00
|
|
|
for(int8_t ct = 0; ct < PID_MAX; ct++) {
|
2011-11-26 22:52:25 +01:00
|
|
|
pids[ct].iAccumulator = 0.0f;
|
|
|
|
pids[ct].lastErr = 0.0f;
|
2010-10-03 22:39:23 +02:00
|
|
|
}
|
2011-06-24 08:57:12 +02:00
|
|
|
for(uint8_t i = 0; i < 3; i++)
|
2011-11-26 22:52:25 +01:00
|
|
|
axis_lock_accum[i] = 0.0f;
|
2010-10-03 22:39:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bound input value between limits
|
|
|
|
*/
|
2012-07-31 03:55:43 +02:00
|
|
|
static float bound(float val, float range)
|
2010-10-03 22:39:23 +02:00
|
|
|
{
|
2012-07-31 03:55:43 +02:00
|
|
|
if(val < -range) {
|
|
|
|
val = -range;
|
|
|
|
} else if(val > range) {
|
|
|
|
val = range;
|
2010-10-03 22:39:23 +02:00
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SettingsUpdatedCb(UAVObjEvent * ev)
|
|
|
|
{
|
2010-10-08 17:38:23 +02:00
|
|
|
StabilizationSettingsGet(&settings);
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2011-06-23 02:06:39 +02:00
|
|
|
// Set the roll rate PID constants
|
2012-02-21 03:47:54 +01:00
|
|
|
pids[PID_RATE_ROLL].p = settings.RollRatePID[STABILIZATIONSETTINGS_ROLLRATEPID_KP];
|
|
|
|
pids[PID_RATE_ROLL].i = settings.RollRatePID[STABILIZATIONSETTINGS_ROLLRATEPID_KI];
|
|
|
|
pids[PID_RATE_ROLL].d = settings.RollRatePID[STABILIZATIONSETTINGS_ROLLRATEPID_KD];
|
|
|
|
pids[PID_RATE_ROLL].iLim = settings.RollRatePID[STABILIZATIONSETTINGS_ROLLRATEPID_ILIMIT];
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2011-06-23 02:06:39 +02:00
|
|
|
// Set the pitch rate PID constants
|
2012-02-21 03:47:54 +01:00
|
|
|
pids[PID_RATE_PITCH].p = settings.PitchRatePID[STABILIZATIONSETTINGS_PITCHRATEPID_KP];
|
|
|
|
pids[PID_RATE_PITCH].i = settings.PitchRatePID[STABILIZATIONSETTINGS_PITCHRATEPID_KI];
|
|
|
|
pids[PID_RATE_PITCH].d = settings.PitchRatePID[STABILIZATIONSETTINGS_PITCHRATEPID_KD];
|
|
|
|
pids[PID_RATE_PITCH].iLim = settings.PitchRatePID[STABILIZATIONSETTINGS_PITCHRATEPID_ILIMIT];
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2011-06-23 02:06:39 +02:00
|
|
|
// Set the yaw rate PID constants
|
2012-02-21 03:47:54 +01:00
|
|
|
pids[PID_RATE_YAW].p = settings.YawRatePID[STABILIZATIONSETTINGS_YAWRATEPID_KP];
|
|
|
|
pids[PID_RATE_YAW].i = settings.YawRatePID[STABILIZATIONSETTINGS_YAWRATEPID_KI];
|
|
|
|
pids[PID_RATE_YAW].d = settings.YawRatePID[STABILIZATIONSETTINGS_YAWRATEPID_KD];
|
|
|
|
pids[PID_RATE_YAW].iLim = settings.YawRatePID[STABILIZATIONSETTINGS_YAWRATEPID_ILIMIT];
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2011-06-23 02:06:39 +02:00
|
|
|
// Set the roll attitude PI constants
|
2012-02-21 03:47:54 +01:00
|
|
|
pids[PID_ROLL].p = settings.RollPI[STABILIZATIONSETTINGS_ROLLPI_KP];
|
|
|
|
pids[PID_ROLL].i = settings.RollPI[STABILIZATIONSETTINGS_ROLLPI_KI];
|
|
|
|
pids[PID_ROLL].iLim = settings.RollPI[STABILIZATIONSETTINGS_ROLLPI_ILIMIT];
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2011-06-23 02:06:39 +02:00
|
|
|
// Set the pitch attitude PI constants
|
2012-02-21 03:47:54 +01:00
|
|
|
pids[PID_PITCH].p = settings.PitchPI[STABILIZATIONSETTINGS_PITCHPI_KP];
|
|
|
|
pids[PID_PITCH].i = settings.PitchPI[STABILIZATIONSETTINGS_PITCHPI_KI];
|
|
|
|
pids[PID_PITCH].iLim = settings.PitchPI[STABILIZATIONSETTINGS_PITCHPI_ILIMIT];
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2012-02-21 03:47:54 +01:00
|
|
|
// Set the yaw attitude PI constants
|
|
|
|
pids[PID_YAW].p = settings.YawPI[STABILIZATIONSETTINGS_YAWPI_KP];
|
|
|
|
pids[PID_YAW].i = settings.YawPI[STABILIZATIONSETTINGS_YAWPI_KI];
|
|
|
|
pids[PID_YAW].iLim = settings.YawPI[STABILIZATIONSETTINGS_YAWPI_ILIMIT];
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2012-02-21 03:47:54 +01:00
|
|
|
// Set the roll attitude PI constants
|
|
|
|
pids[PID_VBAR_ROLL].p = settings.VbarRollPI[STABILIZATIONSETTINGS_VBARROLLPI_KP];
|
|
|
|
pids[PID_VBAR_ROLL].i = settings.VbarRollPI[STABILIZATIONSETTINGS_VBARROLLPI_KI];
|
|
|
|
|
|
|
|
// Set the pitch attitude PI constants
|
|
|
|
pids[PID_VBAR_PITCH].p = settings.VbarPitchPI[STABILIZATIONSETTINGS_VBARPITCHPI_KP];
|
|
|
|
pids[PID_VBAR_PITCH].i = settings.VbarPitchPI[STABILIZATIONSETTINGS_VBARPITCHPI_KI];
|
|
|
|
|
2011-06-23 02:06:39 +02:00
|
|
|
// Set the yaw attitude PI constants
|
2012-02-21 03:47:54 +01:00
|
|
|
pids[PID_VBAR_YAW].p = settings.VbarYawPI[STABILIZATIONSETTINGS_VBARYAWPI_KP];
|
|
|
|
pids[PID_VBAR_YAW].i = settings.VbarYawPI[STABILIZATIONSETTINGS_VBARYAWPI_KI];
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2012-05-26 19:05:04 +02:00
|
|
|
// Need to store the vbar sensitivity
|
|
|
|
vbar_sensitivity[0] = settings.VbarSensitivity[0];
|
|
|
|
vbar_sensitivity[1] = settings.VbarSensitivity[1];
|
|
|
|
vbar_sensitivity[2] = settings.VbarSensitivity[2];
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2011-06-24 08:57:12 +02:00
|
|
|
// Maximum deviation to accumulate for axis lock
|
|
|
|
max_axis_lock = settings.MaxAxisLock;
|
2011-06-25 17:38:37 +02:00
|
|
|
max_axislock_rate = settings.MaxAxisLockRate;
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2011-07-03 17:45:01 +02:00
|
|
|
// Settings for weak leveling
|
|
|
|
weak_leveling_kp = settings.WeakLevelingKp;
|
|
|
|
weak_leveling_max = settings.MaxWeakLevelingRate;
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2011-08-10 10:51:46 +02:00
|
|
|
// Whether to zero the PID integrals while throttle is low
|
|
|
|
lowThrottleZeroIntegral = settings.LowThrottleZeroIntegral == STABILIZATIONSETTINGS_LOWTHROTTLEZEROINTEGRAL_TRUE;
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2011-06-22 02:49:20 +02:00
|
|
|
// The dT has some jitter iteration to iteration that we don't want to
|
|
|
|
// make thie result unpredictable. Still, it's nicer to specify the constant
|
2011-06-23 01:51:25 +02:00
|
|
|
// based on a time (in ms) rather than a fixed multiplier. The error between
|
2011-06-22 02:49:20 +02:00
|
|
|
// update rates on OP (~300 Hz) and CC (~475 Hz) is negligible for this
|
|
|
|
// calculation
|
|
|
|
const float fakeDt = 0.0025;
|
|
|
|
if(settings.GyroTau < 0.0001)
|
|
|
|
gyro_alpha = 0; // not trusting this to resolve to 0
|
|
|
|
else
|
2012-01-03 00:19:14 +01:00
|
|
|
gyro_alpha = expf(-fakeDt / settings.GyroTau);
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2012-02-21 03:47:54 +01:00
|
|
|
// Compute time constant for vbar decay term based on a tau
|
|
|
|
vbar_decay = expf(-fakeDt / settings.VbarTau);
|
2012-05-26 17:38:03 +02:00
|
|
|
vbar_gyros_suppress = settings.VbarGyroSuppress;
|
2012-02-22 01:02:37 +01:00
|
|
|
vbar_piro_comp = settings.VbarPiroComp == STABILIZATIONSETTINGS_VBARPIROCOMP_TRUE;
|
2010-10-03 22:39:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2011-03-02 02:25:30 +01:00
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|
2012-07-31 03:55:43 +02:00
|
|
|
|