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
|
2013-05-18 19:36:45 +02:00
|
|
|
* PID loops on the @ref AttitudeDesired "Attitude Desired" and @ref AttitudeState "Attitude State"
|
2010-10-03 22:39:23 +02:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2013-05-02 23:31:14 +02:00
|
|
|
#include <openpilot.h>
|
2013-09-01 12:10:55 +02:00
|
|
|
#include <pios_struct_helper.h>
|
2010-10-03 22:39:23 +02:00
|
|
|
#include "stabilization.h"
|
2010-10-08 17:38:23 +02:00
|
|
|
#include "stabilizationsettings.h"
|
2013-12-10 21:07:56 +01:00
|
|
|
#include "stabilizationbank.h"
|
2013-12-11 14:56:00 +01:00
|
|
|
#include "stabilizationsettingsbank1.h"
|
|
|
|
#include "stabilizationsettingsbank2.h"
|
|
|
|
#include "stabilizationsettingsbank3.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"
|
2013-05-18 19:36:45 +02:00
|
|
|
#include "attitudestate.h"
|
2013-07-20 13:52:45 +02:00
|
|
|
#include "airspeedstate.h"
|
2013-05-18 19:36:45 +02:00
|
|
|
#include "gyrostate.h"
|
2011-05-03 18:04:44 +02:00
|
|
|
#include "flightstatus.h"
|
2014-01-15 19:53:14 +01:00
|
|
|
#include "manualcontrolsettings.h"
|
2011-05-03 18:04:44 +02:00
|
|
|
#include "manualcontrol.h" // Just to get a macro
|
2013-05-02 23:31:14 +02:00
|
|
|
#include "taskinfo.h"
|
2012-08-02 11:12:12 +02:00
|
|
|
|
|
|
|
// Math libraries
|
2011-03-02 02:25:34 +01:00
|
|
|
#include "CoordinateConversions.h"
|
2012-08-02 11:12:12 +02:00
|
|
|
#include "pid.h"
|
2012-08-03 19:15:57 +02:00
|
|
|
#include "sin_lookup.h"
|
2010-10-03 22:39:23 +02:00
|
|
|
|
2012-08-02 00:52:53 +02:00
|
|
|
// Includes for various stabilization algorithms
|
|
|
|
#include "relay_tuning.h"
|
2012-08-02 10:24:18 +02:00
|
|
|
#include "virtualflybar.h"
|
2010-10-03 22:39:23 +02:00
|
|
|
|
2013-03-28 17:29:26 +01:00
|
|
|
// Includes for various stabilization algorithms
|
|
|
|
#include "relay_tuning.h"
|
|
|
|
|
2010-10-03 22:39:23 +02:00
|
|
|
// Private constants
|
2013-05-18 14:17:26 +02:00
|
|
|
#define MAX_QUEUE_SIZE 1
|
2011-02-02 09:57:34 +01:00
|
|
|
|
|
|
|
#if defined(PIOS_STABILIZATION_STACK_SIZE)
|
2013-05-18 14:17:26 +02:00
|
|
|
#define STACK_SIZE_BYTES PIOS_STABILIZATION_STACK_SIZE
|
2011-02-02 09:57:34 +01:00
|
|
|
#else
|
2013-12-11 20:24:13 +01:00
|
|
|
#define STACK_SIZE_BYTES 790
|
2011-02-02 09:57:34 +01:00
|
|
|
#endif
|
|
|
|
|
2013-05-18 14:17:26 +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
|
|
|
|
2014-01-15 19:53:14 +01:00
|
|
|
// number of flight mode switch positions
|
|
|
|
#define NUM_FMS_POSITIONS 6
|
|
|
|
|
2013-12-16 19:42:24 +01:00
|
|
|
// The PID_RATE_ROLL set is used by Rate mode and the rate portion of Attitude mode
|
|
|
|
// The PID_RATE set is used by the attitude portion of Attitude mode
|
2013-12-18 11:01:16 +01:00
|
|
|
// The PID_RATEA_ROLL set is used by Rattitude mode because it needs to maintain
|
2013-12-16 19:42:24 +01:00
|
|
|
// - two independant rate PIDs because it does rate and attitude simultaneously
|
|
|
|
enum { PID_RATE_ROLL, PID_RATE_PITCH, PID_RATE_YAW, PID_ROLL, PID_PITCH, PID_YAW, PID_RATEA_ROLL, PID_RATEA_PITCH, PID_RATEA_YAW, PID_MAX };
|
2014-01-14 20:03:42 +01:00
|
|
|
enum { RATE_P, RATE_I, RATE_D, RATE_LIMIT, RATE_OFFSET };
|
|
|
|
enum { ATT_P, ATT_I, ATT_LIMIT, ATT_OFFSET };
|
2010-10-03 22:39:23 +02:00
|
|
|
|
|
|
|
// 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;
|
2013-05-18 14:17:26 +02:00
|
|
|
float axis_lock_accum[3] = { 0, 0, 0 };
|
|
|
|
uint8_t max_axis_lock = 0;
|
2011-06-25 17:38:37 +02:00
|
|
|
uint8_t max_axislock_rate = 0;
|
2013-05-18 14:17:26 +02:00
|
|
|
float weak_leveling_kp = 0;
|
2011-07-03 17:45:01 +02:00
|
|
|
uint8_t weak_leveling_max = 0;
|
2011-08-10 10:51:46 +02:00
|
|
|
bool lowThrottleZeroIntegral;
|
2012-11-28 20:52:13 +01:00
|
|
|
bool lowThrottleZeroAxis[MAX_AXES];
|
2012-02-21 03:47:54 +01:00
|
|
|
float vbar_decay = 0.991f;
|
2012-08-02 11:12:12 +02:00
|
|
|
struct pid pids[PID_MAX];
|
2014-01-10 20:32:44 +01:00
|
|
|
|
2014-01-14 20:03:42 +01:00
|
|
|
int flight_mode = -1;
|
2010-10-03 22:39:23 +02:00
|
|
|
|
2014-01-04 10:22:54 +01:00
|
|
|
static uint8_t rattitude_anti_windup;
|
2014-01-11 22:22:20 +01:00
|
|
|
static float cruise_control_min_throttle;
|
|
|
|
static float cruise_control_max_throttle;
|
|
|
|
static uint8_t cruise_control_max_angle;
|
|
|
|
static float cruise_control_max_power_factor;
|
|
|
|
static float cruise_control_power_trim;
|
|
|
|
static int8_t cruise_control_inverted_power_switch;
|
|
|
|
static float cruise_control_neutral_thrust;
|
2014-01-15 19:53:14 +01:00
|
|
|
static uint8_t cruise_control_flight_mode_switch_pos_enable[NUM_FMS_POSITIONS];
|
2010-10-03 22:39:23 +02:00
|
|
|
|
|
|
|
// Private functions
|
2013-05-18 14:17:26 +02:00
|
|
|
static void stabilizationTask(void *parameters);
|
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);
|
2013-05-18 14:17:26 +02:00
|
|
|
static void SettingsUpdatedCb(UAVObjEvent *ev);
|
2013-12-10 21:07:56 +01:00
|
|
|
static void BankUpdatedCb(UAVObjEvent *ev);
|
2013-12-11 14:56:00 +01:00
|
|
|
static void SettingsBankUpdatedCb(UAVObjEvent *ev);
|
2010-10-03 22:39:23 +02:00
|
|
|
|
2013-12-19 09:21:30 +01:00
|
|
|
static float stab_log2f(float x);
|
2014-01-04 10:22:54 +01:00
|
|
|
static float stab_powf(float x, uint8_t p);
|
2013-12-19 09:21:30 +01:00
|
|
|
|
2010-10-03 22:39:23 +02:00
|
|
|
/**
|
|
|
|
* Module initialization
|
|
|
|
*/
|
2011-06-20 07:35:40 +02:00
|
|
|
int32_t StabilizationStart()
|
2010-10-03 22:39:23 +02:00
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
// Initialize variables
|
|
|
|
// Create object queue
|
|
|
|
queue = xQueueCreate(MAX_QUEUE_SIZE, sizeof(UAVObjEvent));
|
|
|
|
|
|
|
|
// Listen for updates.
|
2013-05-18 19:36:45 +02:00
|
|
|
// AttitudeStateConnectQueue(queue);
|
|
|
|
GyroStateConnectQueue(queue);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
StabilizationSettingsConnectCallback(SettingsUpdatedCb);
|
|
|
|
SettingsUpdatedCb(StabilizationSettingsHandle());
|
|
|
|
|
2013-12-10 21:07:56 +01:00
|
|
|
StabilizationBankConnectCallback(BankUpdatedCb);
|
|
|
|
|
2013-12-11 14:56:00 +01:00
|
|
|
StabilizationSettingsBank1ConnectCallback(SettingsBankUpdatedCb);
|
|
|
|
StabilizationSettingsBank2ConnectCallback(SettingsBankUpdatedCb);
|
|
|
|
StabilizationSettingsBank3ConnectCallback(SettingsBankUpdatedCb);
|
|
|
|
|
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
// Start main task
|
|
|
|
xTaskCreate(stabilizationTask, (signed char *)"Stabilization", STACK_SIZE_BYTES / 4, NULL, TASK_PRIORITY, &taskHandle);
|
|
|
|
PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_STABILIZATION, taskHandle);
|
2013-05-15 05:20:52 +02:00
|
|
|
#ifdef PIOS_INCLUDE_WDG
|
2013-05-18 14:17:26 +02:00
|
|
|
PIOS_WDG_RegisterFlag(PIOS_WDG_STABILIZATION);
|
2013-05-15 05:20:52 +02:00
|
|
|
#endif
|
2013-05-18 14:17:26 +02:00
|
|
|
return 0;
|
2011-06-20 07:35:40 +02:00
|
|
|
}
|
|
|
|
|
2010-10-03 22:39:23 +02:00
|
|
|
/**
|
|
|
|
* Module initialization
|
|
|
|
*/
|
|
|
|
int32_t StabilizationInitialize()
|
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
// Initialize variables
|
|
|
|
StabilizationSettingsInitialize();
|
2013-12-11 13:05:05 +01:00
|
|
|
StabilizationBankInitialize();
|
2013-12-11 14:56:00 +01:00
|
|
|
StabilizationSettingsBank1Initialize();
|
|
|
|
StabilizationSettingsBank2Initialize();
|
|
|
|
StabilizationSettingsBank3Initialize();
|
2013-05-18 14:17:26 +02:00
|
|
|
ActuatorDesiredInitialize();
|
Final step: lot of small fixes, last commit in this commit series
This is the first cleanup pass through makefiles and pios.
Probably it is difficult to track changes due to the nature of them.
I would recommend to look at resulting files and compiled code instead.
NOTE: original branch was rebased and lot of conflicts were fixed on
the way. So do not expect that every commit in this series will be
buildable (unlike original branch). Only final result was tested.
The main goal was to remove as much duplication of code (and copy/paste
errors) as possible, moving common parts out of Makefiles. It still is
not perfect, and mostly no code changes made - Makefiles and #ifdefs only.
But please while testing make sure that all code works as before, and no
modules/options are missed by accident.
Brief list of changes:
- Moved common parts of Makefiles into the set of *.mk files.
- Changed method of passing common vars from top Makefile to lower ones.
- Some pios cleanup, mostly #ifdefs, and all pios_config.h files.
- Many obsolete files removed (for instance, AHRS files, op_config.h).
- Many obsolete or unused macros removed or fixed/renamed (ALL_DIGNOSTICS).
- Unified pios_config.h template. Please don't remove lines for board
configs, only comment/uncomment them. Adding new PIOS options, please
propagate them to all board files keeping the same order.
- Some formatting, spacing, indentation (no line endings change yet).
- Some cosmetic fixes (no more C:\X\Y\filename.c printings on Windows).
- Added some library.mk files to move libs into AR achives later.
- EntireFlash target now uses cross-platform python script to generate bin
files. So it works on all supported platforms: Linux, OSX, Windows.
- Top level packaging is completely rewritten. Now it is a part of top
Makefile. As such, all dependencies are checked and accounted, no
more 'make -j' problems should occur.
- Default GCS_BUILD_CONF is release now, may be changed if necessary
using 'make GCS_BUILD_CONF=debug gcs'.
- GCS build paths are separated into debug and release, so no more obj
file clashes. Packaging system supports only release builds.
- New target is introduced: 'clean_package'. Now 'make package' does not
clean build directory. Use clean_package instead for distributable builds.
- Targets like 'all', 'opfw_resource', etc now will print extra contex
in parallel builds too.
- If any of 'package', 'clean_package', 'opfw_resource' targets are given
on command line, GCS build will depend on the resource, so all fw_*.opfw
targets will be built and embedded into GCS. By default GCS does not
depend on resource, and will be built w/o firmware (unless the resource
files already exist and the Qt resource file is generated).
- fw_simposix (ELF executable) is now packaged for linux. Run'n'play!
- Make help is refined and is now up to date.
Still broken:
- UnitTests, should be fixed
- SimPosix: buildable, but should be reworked.
Next planned passes to do:
- toolchain bootstrapping and packaging (including windows - WIP)
- CMSIS/StdPeriph lib cleanup
- more PIOS cleanup
- move libs into AR archives to save build time
- sim targets refactir and cleanup
- move android-related directories under <top>/android
- unit test targets fix
- source code line ending changes (there are many different, were not changed)
- coding style
Merging this, please use --no-ff git option to make it the real commit point
Conflicts:
A lot of... :-)
2013-03-24 12:02:08 +01:00
|
|
|
#ifdef DIAG_RATEDESIRED
|
2013-05-18 14:17:26 +02:00
|
|
|
RateDesiredInitialize();
|
2013-07-20 13:52:45 +02:00
|
|
|
#endif
|
|
|
|
#ifdef REVOLUTION
|
|
|
|
AirspeedStateInitialize();
|
2011-06-18 18:59:02 +02:00
|
|
|
#endif
|
2013-05-18 14:17:26 +02:00
|
|
|
// Code required for relay tuning
|
|
|
|
sin_lookup_initalize();
|
|
|
|
RelayTuningSettingsInitialize();
|
|
|
|
RelayTuningInitialize();
|
2012-08-03 19:15:57 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
return 0;
|
2010-10-03 22:39:23 +02:00
|
|
|
}
|
|
|
|
|
2013-06-04 05:37:40 +02:00
|
|
|
MODULE_INITCALL(StabilizationInitialize, StabilizationStart);
|
2011-06-20 07:35:40 +02:00
|
|
|
|
2010-10-03 22:39:23 +02:00
|
|
|
/**
|
|
|
|
* Module task
|
|
|
|
*/
|
2013-05-18 14:17:26 +02:00
|
|
|
static void stabilizationTask(__attribute__((unused)) void *parameters)
|
2010-10-03 22:39:23 +02:00
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
UAVObjEvent ev;
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
uint32_t timeval = PIOS_DELAY_GetRaw();
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
ActuatorDesiredData actuatorDesired;
|
|
|
|
StabilizationDesiredData stabDesired;
|
|
|
|
RateDesiredData rateDesired;
|
2013-05-18 19:36:45 +02:00
|
|
|
AttitudeStateData attitudeState;
|
|
|
|
GyroStateData gyroStateData;
|
2013-05-18 14:17:26 +02:00
|
|
|
FlightStatusData flightStatus;
|
2013-12-10 21:07:56 +01:00
|
|
|
StabilizationBankData stabBank;
|
|
|
|
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2013-07-20 13:52:45 +02:00
|
|
|
#ifdef REVOLUTION
|
|
|
|
AirspeedStateData airspeedState;
|
|
|
|
#endif
|
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
SettingsUpdatedCb((UAVObjEvent *)NULL);
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
// Main task loop
|
|
|
|
ZeroPids();
|
|
|
|
while (1) {
|
|
|
|
float dT;
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
#ifdef PIOS_INCLUDE_WDG
|
|
|
|
PIOS_WDG_UpdateFlag(PIOS_WDG_STABILIZATION);
|
|
|
|
#endif
|
2011-07-03 17:45:01 +02:00
|
|
|
|
2013-12-19 09:21:30 +01:00
|
|
|
// Wait until the Gyro object is updated, if a timeout then go to failsafe
|
2013-05-18 14:17:26 +02:00
|
|
|
if (xQueueReceive(queue, &ev, FAILSAFE_TIMEOUT_MS / portTICK_RATE_MS) != pdTRUE) {
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_STABILIZATION, SYSTEMALARMS_ALARM_WARNING);
|
|
|
|
continue;
|
|
|
|
}
|
2011-07-03 17:45:01 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
dT = PIOS_DELAY_DiffuS(timeval) * 1.0e-6f;
|
|
|
|
timeval = PIOS_DELAY_GetRaw();
|
2013-03-28 17:29:26 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
FlightStatusGet(&flightStatus);
|
|
|
|
StabilizationDesiredGet(&stabDesired);
|
2013-05-18 19:36:45 +02:00
|
|
|
AttitudeStateGet(&attitudeState);
|
|
|
|
GyroStateGet(&gyroStateData);
|
2013-12-10 21:07:56 +01:00
|
|
|
StabilizationBankGet(&stabBank);
|
2013-05-18 14:17:26 +02:00
|
|
|
#ifdef DIAG_RATEDESIRED
|
|
|
|
RateDesiredGet(&rateDesired);
|
|
|
|
#endif
|
2013-12-10 21:10:01 +01:00
|
|
|
|
2014-01-14 20:03:42 +01:00
|
|
|
if (flight_mode != flightStatus.FlightMode) {
|
2013-12-10 21:10:01 +01:00
|
|
|
flight_mode = flightStatus.FlightMode;
|
2013-12-11 14:56:00 +01:00
|
|
|
SettingsBankUpdatedCb(NULL);
|
2013-12-10 21:10:01 +01:00
|
|
|
}
|
|
|
|
|
2013-07-20 13:52:45 +02:00
|
|
|
#ifdef REVOLUTION
|
|
|
|
float speedScaleFactor;
|
|
|
|
// Scale PID coefficients based on current airspeed estimation - needed for fixed wing planes
|
2013-07-20 14:52:22 +02:00
|
|
|
AirspeedStateGet(&airspeedState);
|
2013-07-20 16:59:00 +02:00
|
|
|
if (settings.ScaleToAirspeed < 0.1f || airspeedState.CalibratedAirspeed < 0.1f) {
|
2013-07-20 13:52:45 +02:00
|
|
|
// feature has been turned off
|
|
|
|
speedScaleFactor = 1.0f;
|
|
|
|
} else {
|
|
|
|
// scale the factor to be 1.0 at the specified airspeed (for example 10m/s) but scaled by 1/speed^2
|
|
|
|
speedScaleFactor = (settings.ScaleToAirspeed * settings.ScaleToAirspeed) / (airspeedState.CalibratedAirspeed * airspeedState.CalibratedAirspeed);
|
2013-09-01 12:10:55 +02:00
|
|
|
if (speedScaleFactor < settings.ScaleToAirspeedLimits.Min) {
|
|
|
|
speedScaleFactor = settings.ScaleToAirspeedLimits.Min;
|
2013-07-20 13:52:45 +02:00
|
|
|
}
|
2013-09-01 12:10:55 +02:00
|
|
|
if (speedScaleFactor > settings.ScaleToAirspeedLimits.Max) {
|
|
|
|
speedScaleFactor = settings.ScaleToAirspeedLimits.Max;
|
2013-07-20 13:52:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
const float speedScaleFactor = 1.0f;
|
|
|
|
#endif
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
#if defined(PIOS_QUATERNION_STABILIZATION)
|
|
|
|
// Quaternion calculation of error in each axis. Uses more memory.
|
|
|
|
float rpy_desired[3];
|
|
|
|
float q_desired[4];
|
|
|
|
float q_error[4];
|
|
|
|
float local_error[3];
|
|
|
|
|
|
|
|
// Essentially zero errors for anything in rate or none
|
2013-09-01 12:10:55 +02:00
|
|
|
if (stabDesired.StabilizationMode.Roll == STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE) {
|
2013-05-18 14:17:26 +02:00
|
|
|
rpy_desired[0] = stabDesired.Roll;
|
|
|
|
} else {
|
2013-05-18 19:36:45 +02:00
|
|
|
rpy_desired[0] = attitudeState.Roll;
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
|
|
|
|
2013-09-01 12:10:55 +02:00
|
|
|
if (stabDesired.StabilizationMode.Pitch == STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE) {
|
2013-05-18 14:17:26 +02:00
|
|
|
rpy_desired[1] = stabDesired.Pitch;
|
|
|
|
} else {
|
2013-05-18 19:36:45 +02:00
|
|
|
rpy_desired[1] = attitudeState.Pitch;
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
|
|
|
|
2013-09-01 12:10:55 +02:00
|
|
|
if (stabDesired.StabilizationMode.Yaw == STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE) {
|
2013-05-18 14:17:26 +02:00
|
|
|
rpy_desired[2] = stabDesired.Yaw;
|
|
|
|
} else {
|
2013-05-18 19:36:45 +02:00
|
|
|
rpy_desired[2] = attitudeState.Yaw;
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
RPY2Quaternion(rpy_desired, q_desired);
|
|
|
|
quat_inverse(q_desired);
|
2013-05-18 19:36:45 +02:00
|
|
|
quat_mult(q_desired, &attitudeState.q1, q_error);
|
2013-05-18 14:17:26 +02:00
|
|
|
quat_inverse(q_error);
|
|
|
|
Quaternion2RPY(q_error, local_error);
|
|
|
|
|
|
|
|
#else /* if defined(PIOS_QUATERNION_STABILIZATION) */
|
|
|
|
// Simpler algorithm for CC, less memory
|
2014-01-14 20:03:42 +01:00
|
|
|
float local_error[3] = { stabDesired.Roll - attitudeState.Roll,
|
2013-05-18 19:36:45 +02:00
|
|
|
stabDesired.Pitch - attitudeState.Pitch,
|
2014-01-14 20:03:42 +01:00
|
|
|
stabDesired.Yaw - attitudeState.Yaw };
|
2013-05-18 14:17:26 +02:00
|
|
|
// find shortest way
|
|
|
|
float modulo = fmodf(local_error[2] + 180.0f, 360.0f);
|
|
|
|
if (modulo < 0) {
|
|
|
|
local_error[2] = modulo + 180.0f;
|
|
|
|
} else {
|
|
|
|
local_error[2] = modulo - 180.0f;
|
|
|
|
}
|
|
|
|
#endif /* if defined(PIOS_QUATERNION_STABILIZATION) */
|
|
|
|
|
|
|
|
float gyro_filtered[3];
|
2013-05-18 19:36:45 +02:00
|
|
|
gyro_filtered[0] = gyro_filtered[0] * gyro_alpha + gyroStateData.x * (1 - gyro_alpha);
|
|
|
|
gyro_filtered[1] = gyro_filtered[1] * gyro_alpha + gyroStateData.y * (1 - gyro_alpha);
|
|
|
|
gyro_filtered[2] = gyro_filtered[2] * gyro_alpha + gyroStateData.z * (1 - gyro_alpha);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-12-16 04:02:06 +01:00
|
|
|
float *stabDesiredAxis = &stabDesired.Roll;
|
2013-05-18 14:17:26 +02:00
|
|
|
float *actuatorDesiredAxis = &actuatorDesired.Roll;
|
|
|
|
float *rateDesiredAxis = &rateDesired.Roll;
|
|
|
|
|
|
|
|
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:
|
|
|
|
for (uint8_t i = 0; i < MAX_AXES; i++) {
|
|
|
|
// Check whether this axis mode needs to be reinitialized
|
2013-09-01 12:10:55 +02:00
|
|
|
bool reinit = (cast_struct_to_array(stabDesired.StabilizationMode, stabDesired.StabilizationMode.Roll)[i] != previous_mode[i]);
|
|
|
|
previous_mode[i] = cast_struct_to_array(stabDesired.StabilizationMode, stabDesired.StabilizationMode.Roll)[i];
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Apply the selected control law
|
2013-09-01 12:10:55 +02:00
|
|
|
switch (cast_struct_to_array(stabDesired.StabilizationMode, stabDesired.StabilizationMode.Roll)[i]) {
|
2013-05-18 14:17:26 +02:00
|
|
|
case STABILIZATIONDESIRED_STABILIZATIONMODE_RATE:
|
|
|
|
if (reinit) {
|
|
|
|
pids[PID_RATE_ROLL + i].iAccumulator = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store to rate desired variable for storing to UAVO
|
2013-09-01 12:10:55 +02:00
|
|
|
rateDesiredAxis[i] =
|
2014-01-10 20:32:44 +01:00
|
|
|
bound(stabDesiredAxis[i], cast_struct_to_array(stabBank.ManualRate, stabBank.ManualRate.Roll)[i]);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Compute the inner loop
|
2013-08-11 15:12:17 +02:00
|
|
|
actuatorDesiredAxis[i] = pid_apply_setpoint(&pids[PID_RATE_ROLL + i], speedScaleFactor, rateDesiredAxis[i], gyro_filtered[i], dT);
|
2013-05-18 14:17:26 +02:00
|
|
|
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
|
2013-09-01 12:10:55 +02:00
|
|
|
rateDesiredAxis[i] = pid_apply(&pids[PID_ROLL + i], local_error[i], dT);
|
|
|
|
rateDesiredAxis[i] = bound(rateDesiredAxis[i],
|
2013-12-10 21:07:56 +01:00
|
|
|
cast_struct_to_array(stabBank.MaximumRate, stabBank.MaximumRate.Roll)[i]);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Compute the inner loop
|
2013-08-11 15:12:17 +02:00
|
|
|
actuatorDesiredAxis[i] = pid_apply_setpoint(&pids[PID_RATE_ROLL + i], speedScaleFactor, rateDesiredAxis[i], gyro_filtered[i], dT);
|
2013-05-18 14:17:26 +02:00
|
|
|
actuatorDesiredAxis[i] = bound(actuatorDesiredAxis[i], 1.0f);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2013-12-18 11:01:16 +01:00
|
|
|
case STABILIZATIONDESIRED_STABILIZATIONMODE_RATTITUDE:
|
2014-01-14 20:03:42 +01:00
|
|
|
// A parameterization from Attitude mode at center stick
|
|
|
|
// - to Rate mode at full stick
|
|
|
|
// This is done by parameterizing to use the rotation rate that Attitude mode
|
|
|
|
// - would use at center stick to using the rotation rate that Rate mode
|
|
|
|
// - would use at full stick in a weighted average sort of way.
|
2013-12-16 04:02:06 +01:00
|
|
|
{
|
|
|
|
if (reinit) {
|
2014-01-14 20:03:42 +01:00
|
|
|
pids[PID_ROLL + i].iAccumulator = 0;
|
|
|
|
pids[PID_RATE_ROLL + i].iAccumulator = 0;
|
2013-12-16 19:42:24 +01:00
|
|
|
pids[PID_RATEA_ROLL + i].iAccumulator = 0;
|
2013-12-16 04:02:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute what Rate mode would give for this stick angle's rate
|
2014-01-04 10:22:54 +01:00
|
|
|
// Save Rate's rate in a temp for later merging with Attitude's rate
|
2013-12-16 04:02:06 +01:00
|
|
|
float rateDesiredAxisRate;
|
|
|
|
rateDesiredAxisRate = bound(stabDesiredAxis[i], 1.0f)
|
2014-01-14 20:03:42 +01:00
|
|
|
* cast_struct_to_array(stabBank.ManualRate, stabBank.ManualRate.Roll)[i];
|
2013-12-16 04:02:06 +01:00
|
|
|
|
|
|
|
// Compute what Attitude mode would give for this stick angle's rate
|
|
|
|
|
|
|
|
// stabDesired for this mode is [-1.0f,+1.0f]
|
|
|
|
// - multiply by Attitude mode max angle to get desired angle
|
|
|
|
// - subtract off the actual angle to get the angle error
|
|
|
|
// This is what local_error[] holds for Attitude mode
|
|
|
|
float attitude_error = stabDesiredAxis[i]
|
2014-01-14 20:03:42 +01:00
|
|
|
* cast_struct_to_array(stabBank.RollMax, stabBank.RollMax)[i]
|
|
|
|
- cast_struct_to_array(attitudeState.Roll, attitudeState.Roll)[i];
|
2013-12-16 04:02:06 +01:00
|
|
|
|
|
|
|
// Compute the outer loop just like Attitude mode does
|
|
|
|
float rateDesiredAxisAttitude;
|
|
|
|
rateDesiredAxisAttitude = pid_apply(&pids[PID_ROLL + i], attitude_error, dT);
|
|
|
|
rateDesiredAxisAttitude = bound(rateDesiredAxisAttitude,
|
2014-01-10 20:32:44 +01:00
|
|
|
cast_struct_to_array(stabBank.MaximumRate,
|
|
|
|
stabBank.MaximumRate.Roll)[i]);
|
2013-12-16 04:02:06 +01:00
|
|
|
|
|
|
|
// Compute the weighted average rate desired
|
|
|
|
// Using max() rather than sqrt() for cpu speed;
|
|
|
|
// - this makes the stick region into a square;
|
|
|
|
// - this is a feature!
|
|
|
|
// - hold a roll angle and add just pitch without the stick sensitivity changing
|
|
|
|
// magnitude = sqrt(stabDesired.Roll*stabDesired.Roll + stabDesired.Pitch*stabDesired.Pitch);
|
|
|
|
float magnitude;
|
|
|
|
magnitude = fmaxf(fabsf(stabDesired.Roll), fabsf(stabDesired.Pitch));
|
2014-01-14 20:03:42 +01:00
|
|
|
rateDesiredAxis[i] = (1.0f - magnitude) * rateDesiredAxisAttitude
|
|
|
|
+ magnitude * rateDesiredAxisRate;
|
2013-12-16 04:02:06 +01:00
|
|
|
|
2013-12-16 19:42:24 +01:00
|
|
|
// Compute the inner loop for both Rate mode and Attitude mode
|
|
|
|
// actuatorDesiredAxis[i] is the weighted average of the two PIDs from the two rates
|
|
|
|
actuatorDesiredAxis[i] =
|
2014-01-14 20:03:42 +01:00
|
|
|
(1.0f - magnitude) * pid_apply_setpoint(&pids[PID_RATEA_ROLL + i], speedScaleFactor, rateDesiredAxis[i], gyro_filtered[i], dT)
|
|
|
|
+ magnitude * pid_apply_setpoint(&pids[PID_RATE_ROLL + i], speedScaleFactor, rateDesiredAxis[i], gyro_filtered[i], dT);
|
2013-12-16 04:02:06 +01:00
|
|
|
actuatorDesiredAxis[i] = bound(actuatorDesiredAxis[i], 1.0f);
|
|
|
|
|
2013-12-18 11:01:16 +01:00
|
|
|
// settings.RattitudeAntiWindup controls the iAccumulator zeroing
|
|
|
|
// - so both iAccs don't wind up too far;
|
|
|
|
// - nor do both iAccs get held too close to zero at mid stick
|
|
|
|
|
2013-12-19 09:21:30 +01:00
|
|
|
// I suspect that there would be windup without it
|
|
|
|
// - since rate and attitude fight each other here
|
2013-12-18 11:01:16 +01:00
|
|
|
// - rate trying to pull it over the top and attitude trying to pull it back down
|
|
|
|
|
|
|
|
// Wind-up increases linearly with cycles for a fixed error.
|
|
|
|
// We must never increase the iAcc or we risk oscillation.
|
|
|
|
|
|
|
|
// Use the powf() function to make two anti-windup curves
|
|
|
|
// - one for zeroing rate close to center stick
|
|
|
|
// - the other for zeroing attitude close to max stick
|
|
|
|
|
|
|
|
// the bigger the dT the more anti windup needed
|
|
|
|
// the bigger the PID[].i the more anti windup needed
|
|
|
|
// more anti windup is achieved with a lower powf() power
|
2014-01-04 10:22:54 +01:00
|
|
|
// a doubling of e.g. PID[].i should cause the runtime anti windup factor
|
2013-12-19 09:21:30 +01:00
|
|
|
// to get twice as far away from 1.0 (towards zero)
|
|
|
|
// e.g. from .90 to .80
|
2013-12-18 11:01:16 +01:00
|
|
|
|
2013-12-19 09:21:30 +01:00
|
|
|
// magic numbers
|
|
|
|
// these generate the inverted parabola like curves that go through [0,1] and [1,0]
|
2014-01-04 10:22:54 +01:00
|
|
|
// the higher the power, the closer the curve is to a straight line
|
|
|
|
// from [0,1] to [1,1] to [1,0] and the less anti windup is applied
|
|
|
|
|
|
|
|
// the UAVO RattitudeAntiWindup varies from 0 to 31
|
|
|
|
// 0 turns off anti windup
|
|
|
|
// 1 gives very little anti-windup because the power given to powf() is 31
|
|
|
|
// 31 gives a lot of anti-windup because the power given to powf() is 1
|
|
|
|
// the 32.1 is what does this
|
|
|
|
// the 7.966 and 17.668 cancel the default PID value and dT given to log2f()
|
|
|
|
// if these are non-default, tweaking is thus done so the user doesn't have to readjust
|
|
|
|
// the default value of 10 for UAVO RattitudeAntiWindup gives a power of 22
|
2014-01-14 20:03:42 +01:00
|
|
|
// these calculations are for magnitude = 0.5, so 22 corresponds to the number of bits
|
|
|
|
// used in the mantissa of the float
|
|
|
|
// i.e. 1.0-(0.5^22) almost underflows
|
2013-12-18 11:01:16 +01:00
|
|
|
|
|
|
|
// This may only be useful for aircraft with large Ki values and limits
|
|
|
|
if (dT > 0.0f && rattitude_anti_windup > 0.0f) {
|
|
|
|
float factor;
|
|
|
|
|
|
|
|
// At magnitudes close to one, the Attitude accumulators gets zeroed
|
2014-01-14 20:03:42 +01:00
|
|
|
if (pids[PID_ROLL + i].i > 0.0f) {
|
|
|
|
factor = 1.0f - stab_powf(magnitude, ((uint8_t)(32.1f - 7.966f - stab_log2f(dT * pids[PID_ROLL + i].i))) - rattitude_anti_windup);
|
|
|
|
pids[PID_ROLL + i].iAccumulator *= factor;
|
2013-12-18 11:01:16 +01:00
|
|
|
}
|
2014-01-14 20:03:42 +01:00
|
|
|
if (pids[PID_RATEA_ROLL + i].i > 0.0f) {
|
|
|
|
factor = 1.0f - stab_powf(magnitude, ((uint8_t)(32.1f - 17.668f - stab_log2f(dT * pids[PID_RATEA_ROLL + i].i))) - rattitude_anti_windup);
|
|
|
|
pids[PID_RATEA_ROLL + i].iAccumulator *= factor;
|
2013-12-18 11:01:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// At magnitudes close to zero, the Rate accumulator gets zeroed
|
2014-01-14 20:03:42 +01:00
|
|
|
if (pids[PID_RATE_ROLL + i].i > 0.0f) {
|
|
|
|
factor = 1.0f - stab_powf(1.0f - magnitude, ((uint8_t)(32.1f - 17.668f - stab_log2f(dT * pids[PID_RATE_ROLL + i].i))) - rattitude_anti_windup);
|
|
|
|
pids[PID_RATE_ROLL + i].iAccumulator *= factor;
|
2013-12-18 11:01:16 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-16 04:02:06 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
case STABILIZATIONDESIRED_STABILIZATIONMODE_VIRTUALBAR:
|
|
|
|
|
|
|
|
// Store for debugging output
|
2013-12-16 04:02:06 +01:00
|
|
|
rateDesiredAxis[i] = stabDesiredAxis[i];
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Run a virtual flybar stabilization algorithm on this axis
|
|
|
|
stabilization_virtual_flybar(gyro_filtered[i], rateDesiredAxis[i], &actuatorDesiredAxis[i], dT, reinit, i, &settings);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STABILIZATIONDESIRED_STABILIZATIONMODE_WEAKLEVELING:
|
2014-01-14 20:03:42 +01:00
|
|
|
// FIXME: local_error[] is rate - attitude for Weak Leveling
|
|
|
|
// The only ramifications are:
|
|
|
|
// Weak Leveling Kp is off by a factor of 3 to 12 and may need a different default in GCS
|
|
|
|
// Changing Rate mode max rate currently requires a change to Kp
|
|
|
|
// That would be changed to Attitude mode max angle affecting Kp
|
|
|
|
// Also does not take dT into account
|
2013-05-18 14:17:26 +02:00
|
|
|
{
|
|
|
|
if (reinit) {
|
|
|
|
pids[PID_RATE_ROLL + i].iAccumulator = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float weak_leveling = local_error[i] * weak_leveling_kp;
|
|
|
|
weak_leveling = bound(weak_leveling, weak_leveling_max);
|
|
|
|
|
|
|
|
// Compute desired rate as input biased towards leveling
|
2013-12-16 04:02:06 +01:00
|
|
|
rateDesiredAxis[i] = stabDesiredAxis[i] + weak_leveling;
|
2013-08-11 15:12:17 +02:00
|
|
|
actuatorDesiredAxis[i] = pid_apply_setpoint(&pids[PID_RATE_ROLL + i], speedScaleFactor, rateDesiredAxis[i], gyro_filtered[i], dT);
|
2013-05-18 14:17:26 +02:00
|
|
|
actuatorDesiredAxis[i] = bound(actuatorDesiredAxis[i], 1.0f);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case STABILIZATIONDESIRED_STABILIZATIONMODE_AXISLOCK:
|
|
|
|
if (reinit) {
|
|
|
|
pids[PID_RATE_ROLL + i].iAccumulator = 0;
|
|
|
|
}
|
|
|
|
|
2013-12-16 04:02:06 +01:00
|
|
|
if (fabsf(stabDesiredAxis[i]) > max_axislock_rate) {
|
2013-05-18 14:17:26 +02:00
|
|
|
// While getting strong commands act like rate mode
|
2013-12-16 04:02:06 +01:00
|
|
|
rateDesiredAxis[i] = stabDesiredAxis[i];
|
2013-05-18 14:17:26 +02:00
|
|
|
axis_lock_accum[i] = 0;
|
|
|
|
} else {
|
|
|
|
// For weaker commands or no command simply attitude lock (almost) on no gyro change
|
2013-12-16 04:02:06 +01:00
|
|
|
axis_lock_accum[i] += (stabDesiredAxis[i] - gyro_filtered[i]) * dT;
|
2013-05-18 14:17:26 +02:00
|
|
|
axis_lock_accum[i] = bound(axis_lock_accum[i], max_axis_lock);
|
|
|
|
rateDesiredAxis[i] = pid_apply(&pids[PID_ROLL + i], axis_lock_accum[i], dT);
|
|
|
|
}
|
2011-09-11 19:27:17 +02:00
|
|
|
|
2013-09-01 12:10:55 +02:00
|
|
|
rateDesiredAxis[i] = bound(rateDesiredAxis[i],
|
2013-12-10 21:07:56 +01:00
|
|
|
cast_struct_to_array(stabBank.ManualRate, stabBank.ManualRate.Roll)[i]);
|
2011-06-23 01:51:25 +02:00
|
|
|
|
2013-08-11 15:12:17 +02:00
|
|
|
actuatorDesiredAxis[i] = pid_apply_setpoint(&pids[PID_RATE_ROLL + i], speedScaleFactor, rateDesiredAxis[i], gyro_filtered[i], dT);
|
2013-05-18 14:17:26 +02:00
|
|
|
actuatorDesiredAxis[i] = bound(actuatorDesiredAxis[i], 1.0f);
|
2011-09-11 19:27:17 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
break;
|
2011-06-23 01:51:25 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
case STABILIZATIONDESIRED_STABILIZATIONMODE_RELAYRATE:
|
|
|
|
// Store to rate desired variable for storing to UAVO
|
2013-12-16 04:02:06 +01:00
|
|
|
rateDesiredAxis[i] = bound(stabDesiredAxis[i],
|
2013-12-10 21:07:56 +01:00
|
|
|
cast_struct_to_array(stabBank.ManualRate, stabBank.ManualRate.Roll)[i]);
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
// Run the relay controller which also estimates the oscillation parameters
|
|
|
|
stabilization_relay_rate(rateDesiredAxis[i] - gyro_filtered[i], &actuatorDesiredAxis[i], i, reinit);
|
|
|
|
actuatorDesiredAxis[i] = bound(actuatorDesiredAxis[i], 1.0f);
|
2012-02-22 01:02:37 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
break;
|
2012-02-20 14:29:43 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
case STABILIZATIONDESIRED_STABILIZATIONMODE_RELAYATTITUDE:
|
|
|
|
if (reinit) {
|
|
|
|
pids[PID_ROLL + i].iAccumulator = 0;
|
|
|
|
}
|
2012-02-20 14:29:43 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
// Compute the outer loop like attitude mode
|
|
|
|
rateDesiredAxis[i] = pid_apply(&pids[PID_ROLL + i], local_error[i], dT);
|
2013-09-01 12:10:55 +02:00
|
|
|
rateDesiredAxis[i] = bound(rateDesiredAxis[i],
|
2013-12-10 21:07:56 +01:00
|
|
|
cast_struct_to_array(stabBank.MaximumRate, stabBank.MaximumRate.Roll)[i]);
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
// Run the relay controller which also estimates the oscillation parameters
|
|
|
|
stabilization_relay_rate(rateDesiredAxis[i] - gyro_filtered[i], &actuatorDesiredAxis[i], i, reinit);
|
|
|
|
actuatorDesiredAxis[i] = bound(actuatorDesiredAxis[i], 1.0f);
|
2012-07-31 03:55:43 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
break;
|
2011-06-23 01:51:25 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
case STABILIZATIONDESIRED_STABILIZATIONMODE_NONE:
|
2013-12-16 04:02:06 +01:00
|
|
|
actuatorDesiredAxis[i] = bound(stabDesiredAxis[i], 1.0f);
|
2013-05-18 14:17:26 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-06-23 01:51:25 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
if (settings.VbarPiroComp == STABILIZATIONSETTINGS_VBARPIROCOMP_TRUE) {
|
|
|
|
stabilization_virtual_flybar_pirocomp(gyro_filtered[2], dT);
|
|
|
|
}
|
2012-07-31 03:55:43 +02:00
|
|
|
|
Final step: lot of small fixes, last commit in this commit series
This is the first cleanup pass through makefiles and pios.
Probably it is difficult to track changes due to the nature of them.
I would recommend to look at resulting files and compiled code instead.
NOTE: original branch was rebased and lot of conflicts were fixed on
the way. So do not expect that every commit in this series will be
buildable (unlike original branch). Only final result was tested.
The main goal was to remove as much duplication of code (and copy/paste
errors) as possible, moving common parts out of Makefiles. It still is
not perfect, and mostly no code changes made - Makefiles and #ifdefs only.
But please while testing make sure that all code works as before, and no
modules/options are missed by accident.
Brief list of changes:
- Moved common parts of Makefiles into the set of *.mk files.
- Changed method of passing common vars from top Makefile to lower ones.
- Some pios cleanup, mostly #ifdefs, and all pios_config.h files.
- Many obsolete files removed (for instance, AHRS files, op_config.h).
- Many obsolete or unused macros removed or fixed/renamed (ALL_DIGNOSTICS).
- Unified pios_config.h template. Please don't remove lines for board
configs, only comment/uncomment them. Adding new PIOS options, please
propagate them to all board files keeping the same order.
- Some formatting, spacing, indentation (no line endings change yet).
- Some cosmetic fixes (no more C:\X\Y\filename.c printings on Windows).
- Added some library.mk files to move libs into AR achives later.
- EntireFlash target now uses cross-platform python script to generate bin
files. So it works on all supported platforms: Linux, OSX, Windows.
- Top level packaging is completely rewritten. Now it is a part of top
Makefile. As such, all dependencies are checked and accounted, no
more 'make -j' problems should occur.
- Default GCS_BUILD_CONF is release now, may be changed if necessary
using 'make GCS_BUILD_CONF=debug gcs'.
- GCS build paths are separated into debug and release, so no more obj
file clashes. Packaging system supports only release builds.
- New target is introduced: 'clean_package'. Now 'make package' does not
clean build directory. Use clean_package instead for distributable builds.
- Targets like 'all', 'opfw_resource', etc now will print extra contex
in parallel builds too.
- If any of 'package', 'clean_package', 'opfw_resource' targets are given
on command line, GCS build will depend on the resource, so all fw_*.opfw
targets will be built and embedded into GCS. By default GCS does not
depend on resource, and will be built w/o firmware (unless the resource
files already exist and the Qt resource file is generated).
- fw_simposix (ELF executable) is now packaged for linux. Run'n'play!
- Make help is refined and is now up to date.
Still broken:
- UnitTests, should be fixed
- SimPosix: buildable, but should be reworked.
Next planned passes to do:
- toolchain bootstrapping and packaging (including windows - WIP)
- CMSIS/StdPeriph lib cleanup
- more PIOS cleanup
- move libs into AR archives to save build time
- sim targets refactir and cleanup
- move android-related directories under <top>/android
- unit test targets fix
- source code line ending changes (there are many different, were not changed)
- coding style
Merging this, please use --no-ff git option to make it the real commit point
Conflicts:
A lot of... :-)
2013-03-24 12:02:08 +01:00
|
|
|
#ifdef DIAG_RATEDESIRED
|
2013-05-18 14:17:26 +02:00
|
|
|
RateDesiredSet(&rateDesired);
|
2012-07-31 03:55:43 +02:00
|
|
|
#endif
|
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
// Save dT
|
|
|
|
actuatorDesired.UpdateTime = dT * 1000;
|
|
|
|
actuatorDesired.Throttle = stabDesired.Throttle;
|
|
|
|
|
|
|
|
// Suppress desired output while disarmed or throttle low, for configured axis
|
|
|
|
if (flightStatus.Armed != FLIGHTSTATUS_ARMED_ARMED || stabDesired.Throttle < 0) {
|
|
|
|
if (lowThrottleZeroAxis[ROLL]) {
|
|
|
|
actuatorDesired.Roll = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lowThrottleZeroAxis[PITCH]) {
|
|
|
|
actuatorDesired.Pitch = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lowThrottleZeroAxis[YAW]) {
|
|
|
|
actuatorDesired.Yaw = 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-11 22:22:20 +01:00
|
|
|
// modify throttle according to 1/cos(bank angle)
|
|
|
|
// to maintain same altitdue with changing bank angle
|
|
|
|
// but without manually adjusting throttle
|
|
|
|
// do it here and all the various flight modes (e.g. Altitude Hold) can use it
|
2014-01-15 19:53:14 +01:00
|
|
|
uint8_t flight_mode_switch_position;
|
|
|
|
ManualControlCommandFlightModeSwitchPositionGet(&flight_mode_switch_position);
|
|
|
|
if (flight_mode_switch_position < NUM_FMS_POSITIONS
|
|
|
|
&& cruise_control_flight_mode_switch_pos_enable[flight_mode_switch_position] != (uint8_t) 0
|
|
|
|
&& cruise_control_max_power_factor > 0.0001f) {
|
2014-01-11 22:22:20 +01:00
|
|
|
static uint8_t toggle;
|
|
|
|
static float factor;
|
|
|
|
float angle;
|
|
|
|
// get attitude state and calculate angle
|
|
|
|
// do it every 8th iteration to save CPU
|
|
|
|
if ((toggle++ & 7) == 0) {
|
|
|
|
// spherical right triangle
|
|
|
|
// 0 <= acosf() <= Pi
|
|
|
|
angle = RAD2DEG(acosf(cos_lookup_deg(attitudeState.Roll) * cos_lookup_deg(attitudeState.Pitch)));
|
|
|
|
// if past the cutoff angle (60 to 180 (180 means never))
|
|
|
|
if (angle > cruise_control_max_angle) {
|
|
|
|
// -1 reversed collective, 0 zero power, or 1 normal power
|
|
|
|
// these are all unboosted
|
|
|
|
factor = cruise_control_inverted_power_switch;
|
|
|
|
} else {
|
|
|
|
// avoid singularity
|
|
|
|
if (angle > 89.999f && angle < 90.001f) {
|
|
|
|
factor = cruise_control_max_power_factor;
|
|
|
|
} else {
|
|
|
|
factor = 1.0f / fabsf(cos_lookup_deg(angle));
|
|
|
|
if (factor > cruise_control_max_power_factor) {
|
|
|
|
factor = cruise_control_max_power_factor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// factor in the power trim, no effect at 1.0, linear effect increases with factor
|
|
|
|
factor = (factor - 1.0f) * cruise_control_power_trim + 1.0f;
|
|
|
|
// if inverted and they want negative boost
|
|
|
|
if (angle > 90.0f && cruise_control_inverted_power_switch == (int8_t) -1) {
|
|
|
|
factor = -factor;
|
|
|
|
// as long as throttle is getting reversed
|
|
|
|
// we may as well do pitch and yaw for a complete "invert switch"
|
|
|
|
actuatorDesired.Pitch = -actuatorDesired.Pitch;
|
|
|
|
actuatorDesired.Yaw = -actuatorDesired.Yaw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// also don't adjust throttle if <= 0, leaves neg alone and zero throttle stops motors
|
|
|
|
if (actuatorDesired.Throttle > cruise_control_min_throttle)
|
|
|
|
{
|
|
|
|
// quad example factor of 2 at hover power of 40%: (0.4 - 0.0) * 2.0 + 0.0 = 0.8
|
|
|
|
// CP heli example factor of 2 at hover stick of 60%: (0.6 - 0.5) * 2.0 + 0.5 = 0.7
|
|
|
|
actuatorDesired.Throttle = (actuatorDesired.Throttle - cruise_control_neutral_thrust) * factor + cruise_control_neutral_thrust;
|
|
|
|
if (actuatorDesired.Throttle > cruise_control_max_throttle) {
|
|
|
|
actuatorDesired.Throttle = cruise_control_max_throttle;
|
|
|
|
} else if (actuatorDesired.Throttle < cruise_control_min_throttle) {
|
|
|
|
actuatorDesired.Throttle = cruise_control_min_throttle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
if (PARSE_FLIGHT_MODE(flightStatus.FlightMode) != FLIGHTMODE_MANUAL) {
|
|
|
|
ActuatorDesiredSet(&actuatorDesired);
|
|
|
|
} else {
|
|
|
|
// Force all axes to reinitialize when engaged
|
|
|
|
for (uint8_t i = 0; i < MAX_AXES; i++) {
|
|
|
|
previous_mode[i] = 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flightStatus.Armed != FLIGHTSTATUS_ARMED_ARMED ||
|
|
|
|
(lowThrottleZeroIntegral && stabDesired.Throttle < 0)) {
|
|
|
|
// Force all axes to reinitialize when engaged
|
|
|
|
for (uint8_t i = 0; i < MAX_AXES; i++) {
|
|
|
|
previous_mode[i] = 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
/**
|
|
|
|
* Clear the accumulators and derivatives for all the axes
|
|
|
|
*/
|
2010-10-03 22:39:23 +02:00
|
|
|
static void ZeroPids(void)
|
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
for (uint32_t i = 0; i < PID_MAX; i++) {
|
|
|
|
pid_zero(&pids[i]);
|
|
|
|
}
|
2012-08-02 11:12:12 +02:00
|
|
|
|
2012-09-10 10:10:26 +02:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
for (uint8_t i = 0; i < 3; i++) {
|
|
|
|
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
|
|
|
{
|
2013-05-18 14:17:26 +02:00
|
|
|
if (val < -range) {
|
2014-01-11 22:22:20 +01:00
|
|
|
return -range;
|
2013-05-18 14:17:26 +02:00
|
|
|
} else if (val > range) {
|
2014-01-11 22:22:20 +01:00
|
|
|
return range;
|
2013-05-18 14:17:26 +02:00
|
|
|
}
|
|
|
|
return val;
|
2010-10-03 22:39:23 +02:00
|
|
|
}
|
|
|
|
|
2014-01-04 10:22:54 +01:00
|
|
|
|
|
|
|
// x small (0.0 < x < .01) so interpolation of fractional part is reasonable
|
2013-12-19 09:21:30 +01:00
|
|
|
static float stab_log2f(float x)
|
|
|
|
{
|
2014-01-14 20:03:42 +01:00
|
|
|
union {
|
|
|
|
volatile float f;
|
|
|
|
volatile uint32_t i;
|
|
|
|
volatile unsigned char c[4];
|
|
|
|
} __attribute__((packed)) u1, u2;
|
|
|
|
|
|
|
|
u2.f = u1.f = x;
|
|
|
|
u1.i <<= 1;
|
|
|
|
u2.i &= 0xff800000;
|
|
|
|
|
|
|
|
// get and unbias the exponent, add in a linear interpolation of the fractional part
|
|
|
|
return (float)(u1.c[3] - 127) + (x / u2.f) - 1.0f;
|
2014-01-04 10:22:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 0<=x<=1, 0<=p<=31
|
|
|
|
static float stab_powf(float x, uint8_t p)
|
|
|
|
{
|
2014-01-14 20:03:42 +01:00
|
|
|
float retval = 1.0f;
|
2014-01-04 10:22:54 +01:00
|
|
|
|
2014-01-14 20:03:42 +01:00
|
|
|
while (p) {
|
|
|
|
if (p & 1) {
|
|
|
|
retval *= x;
|
|
|
|
}
|
|
|
|
x *= x;
|
|
|
|
p >>= 1;
|
2013-12-19 09:21:30 +01:00
|
|
|
}
|
2014-01-04 10:22:54 +01:00
|
|
|
|
2014-01-14 20:03:42 +01:00
|
|
|
return retval;
|
2013-12-19 09:21:30 +01:00
|
|
|
}
|
|
|
|
|
2014-01-04 10:22:54 +01:00
|
|
|
|
2013-12-11 14:56:00 +01:00
|
|
|
static void SettingsBankUpdatedCb(__attribute__((unused)) UAVObjEvent *ev)
|
2010-10-03 22:39:23 +02:00
|
|
|
{
|
2013-12-10 21:10:01 +01:00
|
|
|
StabilizationBankData bank, oldBank;
|
2014-01-14 20:03:42 +01:00
|
|
|
|
2013-12-11 14:56:00 +01:00
|
|
|
StabilizationBankGet(&oldBank);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2014-01-14 20:03:42 +01:00
|
|
|
if (flight_mode < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-10 20:58:05 +01:00
|
|
|
|
2014-01-14 20:03:42 +01:00
|
|
|
switch (cast_struct_to_array(settings.FlightModeMap, settings.FlightModeMap.Stabilized1)[flight_mode]) {
|
2013-12-11 14:56:00 +01:00
|
|
|
case 0:
|
2014-01-14 20:03:42 +01:00
|
|
|
StabilizationSettingsBank1Get((StabilizationSettingsBank1Data *)&bank);
|
2013-12-11 14:56:00 +01:00
|
|
|
break;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-12-11 14:56:00 +01:00
|
|
|
case 1:
|
2014-01-14 20:03:42 +01:00
|
|
|
StabilizationSettingsBank2Get((StabilizationSettingsBank2Data *)&bank);
|
2013-12-11 14:56:00 +01:00
|
|
|
break;
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-12-11 14:56:00 +01:00
|
|
|
case 2:
|
2014-01-14 20:03:42 +01:00
|
|
|
StabilizationSettingsBank3Get((StabilizationSettingsBank3Data *)&bank);
|
2013-12-11 14:56:00 +01:00
|
|
|
break;
|
2013-12-10 20:58:05 +01:00
|
|
|
|
2013-12-11 14:56:00 +01:00
|
|
|
default:
|
2013-12-11 17:31:22 +01:00
|
|
|
memset(&bank, 0, sizeof(StabilizationBankDataPacked));
|
2014-01-14 20:03:42 +01:00
|
|
|
// return; //bank number is invalid. All we can do is ignore it.
|
2013-12-11 14:56:00 +01:00
|
|
|
}
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2014-01-14 20:03:42 +01:00
|
|
|
// Need to do this to prevent an infinite loop
|
|
|
|
if (memcmp(&oldBank, &bank, sizeof(StabilizationBankDataPacked)) != 0) {
|
2013-12-10 21:10:01 +01:00
|
|
|
StabilizationBankSet(&bank);
|
|
|
|
}
|
2013-12-10 21:07:56 +01:00
|
|
|
}
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-12-10 21:07:56 +01:00
|
|
|
static void BankUpdatedCb(__attribute__((unused)) UAVObjEvent *ev)
|
|
|
|
{
|
2013-12-11 17:31:22 +01:00
|
|
|
StabilizationBankData bank;
|
2014-01-14 20:03:42 +01:00
|
|
|
|
2013-12-10 21:07:56 +01:00
|
|
|
StabilizationBankGet(&bank);
|
|
|
|
|
2014-01-14 20:03:42 +01:00
|
|
|
// this code will be needed if any other modules alter stabilizationbank
|
2013-12-11 17:31:22 +01:00
|
|
|
/*
|
|
|
|
StabilizationBankData curBank;
|
2013-12-11 14:56:00 +01:00
|
|
|
if(flight_mode < 0) return;
|
2013-12-10 21:07:56 +01:00
|
|
|
|
2013-12-11 14:56:00 +01:00
|
|
|
switch(cast_struct_to_array(settings.FlightModeMap, settings.FlightModeMap.Stabilized1)[flight_mode])
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
StabilizationSettingsBank1Get((StabilizationSettingsBank1Data *) &curBank);
|
|
|
|
if(memcmp(&curBank, &bank, sizeof(StabilizationBankDataPacked)) != 0)
|
|
|
|
{
|
|
|
|
StabilizationSettingsBank1Set((StabilizationSettingsBank1Data *) &bank);
|
|
|
|
}
|
|
|
|
break;
|
2013-12-10 21:07:56 +01:00
|
|
|
|
2013-12-11 14:56:00 +01:00
|
|
|
case 1:
|
|
|
|
StabilizationSettingsBank2Get((StabilizationSettingsBank2Data *) &curBank);
|
|
|
|
if(memcmp(&curBank, &bank, sizeof(StabilizationBankDataPacked)) != 0)
|
|
|
|
{
|
|
|
|
StabilizationSettingsBank2Set((StabilizationSettingsBank2Data *) &bank);
|
|
|
|
}
|
|
|
|
break;
|
2013-12-10 21:07:56 +01:00
|
|
|
|
2013-12-11 14:56:00 +01:00
|
|
|
case 2:
|
|
|
|
StabilizationSettingsBank3Get((StabilizationSettingsBank3Data *) &curBank);
|
|
|
|
if(memcmp(&curBank, &bank, sizeof(StabilizationBankDataPacked)) != 0)
|
|
|
|
{
|
|
|
|
StabilizationSettingsBank3Set((StabilizationSettingsBank3Data *) &bank);
|
|
|
|
}
|
|
|
|
break;
|
2013-12-10 21:07:56 +01:00
|
|
|
|
2013-12-11 14:56:00 +01:00
|
|
|
default:
|
|
|
|
return; //invalid bank number
|
2013-12-10 21:07:56 +01:00
|
|
|
}
|
2014-01-14 20:03:42 +01:00
|
|
|
*/
|
2013-12-11 17:31:22 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Set the roll rate PID constants
|
2013-12-10 21:07:56 +01:00
|
|
|
pid_configure(&pids[PID_RATE_ROLL], bank.RollRatePID.Kp,
|
|
|
|
bank.RollRatePID.Ki,
|
|
|
|
bank.RollRatePID.Kd,
|
|
|
|
bank.RollRatePID.ILimit);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Set the pitch rate PID constants
|
2013-12-10 21:07:56 +01:00
|
|
|
pid_configure(&pids[PID_RATE_PITCH], bank.PitchRatePID.Kp,
|
|
|
|
bank.PitchRatePID.Ki,
|
|
|
|
bank.PitchRatePID.Kd,
|
|
|
|
bank.PitchRatePID.ILimit);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Set the yaw rate PID constants
|
2013-12-10 21:07:56 +01:00
|
|
|
pid_configure(&pids[PID_RATE_YAW], bank.YawRatePID.Kp,
|
|
|
|
bank.YawRatePID.Ki,
|
|
|
|
bank.YawRatePID.Kd,
|
|
|
|
bank.YawRatePID.ILimit);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Set the roll attitude PI constants
|
2013-12-10 21:07:56 +01:00
|
|
|
pid_configure(&pids[PID_ROLL], bank.RollPI.Kp,
|
|
|
|
bank.RollPI.Ki,
|
2013-12-16 19:42:24 +01:00
|
|
|
0,
|
2013-12-10 21:07:56 +01:00
|
|
|
bank.RollPI.ILimit);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Set the pitch attitude PI constants
|
2013-12-10 21:07:56 +01:00
|
|
|
pid_configure(&pids[PID_PITCH], bank.PitchPI.Kp,
|
|
|
|
bank.PitchPI.Ki,
|
2013-12-16 19:42:24 +01:00
|
|
|
0,
|
2013-12-10 21:07:56 +01:00
|
|
|
bank.PitchPI.ILimit);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
|
|
|
// Set the yaw attitude PI constants
|
2013-12-10 21:07:56 +01:00
|
|
|
pid_configure(&pids[PID_YAW], bank.YawPI.Kp,
|
|
|
|
bank.YawPI.Ki,
|
2013-12-16 19:42:24 +01:00
|
|
|
0,
|
2013-12-10 21:07:56 +01:00
|
|
|
bank.YawPI.ILimit);
|
2013-05-18 14:17:26 +02:00
|
|
|
|
2013-12-18 11:01:16 +01:00
|
|
|
// Set the Rattitude roll rate PID constants
|
2013-12-16 19:42:24 +01:00
|
|
|
pid_configure(&pids[PID_RATEA_ROLL],
|
2014-01-10 20:32:44 +01:00
|
|
|
bank.RollRatePID.Kp,
|
|
|
|
bank.RollRatePID.Ki,
|
|
|
|
bank.RollRatePID.Kd,
|
|
|
|
bank.RollRatePID.ILimit);
|
2013-12-16 19:42:24 +01:00
|
|
|
|
2013-12-18 11:01:16 +01:00
|
|
|
// Set the Rattitude pitch rate PID constants
|
2013-12-16 19:42:24 +01:00
|
|
|
pid_configure(&pids[PID_RATEA_PITCH],
|
2014-01-10 20:32:44 +01:00
|
|
|
bank.PitchRatePID.Kp,
|
|
|
|
bank.PitchRatePID.Ki,
|
|
|
|
bank.PitchRatePID.Kd,
|
|
|
|
bank.PitchRatePID.ILimit);
|
2013-12-16 19:42:24 +01:00
|
|
|
|
2013-12-18 11:01:16 +01:00
|
|
|
// Set the Rattitude yaw rate PID constants
|
2013-12-16 19:42:24 +01:00
|
|
|
pid_configure(&pids[PID_RATEA_YAW],
|
2014-01-10 20:32:44 +01:00
|
|
|
bank.YawRatePID.Kp,
|
|
|
|
bank.YawRatePID.Ki,
|
|
|
|
bank.YawRatePID.Kd,
|
|
|
|
bank.YawRatePID.ILimit);
|
2014-01-10 23:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void SettingsUpdatedCb(__attribute__((unused)) UAVObjEvent *ev)
|
|
|
|
{
|
|
|
|
StabilizationSettingsGet(&settings);
|
2013-12-16 19:42:24 +01:00
|
|
|
|
2013-05-18 14:17:26 +02:00
|
|
|
// Set up the derivative term
|
|
|
|
pid_configure_derivative(settings.DerivativeCutoff, settings.DerivativeGamma);
|
|
|
|
|
|
|
|
// Maximum deviation to accumulate for axis lock
|
|
|
|
max_axis_lock = settings.MaxAxisLock;
|
|
|
|
max_axislock_rate = settings.MaxAxisLockRate;
|
|
|
|
|
|
|
|
// Settings for weak leveling
|
|
|
|
weak_leveling_kp = settings.WeakLevelingKp;
|
|
|
|
weak_leveling_max = settings.MaxWeakLevelingRate;
|
|
|
|
|
|
|
|
// Whether to zero the PID integrals while throttle is low
|
|
|
|
lowThrottleZeroIntegral = settings.LowThrottleZeroIntegral == STABILIZATIONSETTINGS_LOWTHROTTLEZEROINTEGRAL_TRUE;
|
|
|
|
|
|
|
|
// Whether to suppress (zero) the StabilizationDesired output for each axis while disarmed or throttle is low
|
2013-09-01 12:10:55 +02:00
|
|
|
lowThrottleZeroAxis[ROLL] = settings.LowThrottleZeroAxis.Roll == STABILIZATIONSETTINGS_LOWTHROTTLEZEROAXIS_TRUE;
|
|
|
|
lowThrottleZeroAxis[PITCH] = settings.LowThrottleZeroAxis.Pitch == STABILIZATIONSETTINGS_LOWTHROTTLEZEROAXIS_TRUE;
|
|
|
|
lowThrottleZeroAxis[YAW] = settings.LowThrottleZeroAxis.Yaw == STABILIZATIONSETTINGS_LOWTHROTTLEZEROAXIS_TRUE;
|
2013-05-18 14:17:26 +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
|
|
|
|
// based on a time (in ms) rather than a fixed multiplier. The error between
|
|
|
|
// update rates on OP (~300 Hz) and CC (~475 Hz) is negligible for this
|
|
|
|
// calculation
|
|
|
|
const float fakeDt = 0.0025f;
|
|
|
|
if (settings.GyroTau < 0.0001f) {
|
|
|
|
gyro_alpha = 0; // not trusting this to resolve to 0
|
|
|
|
} else {
|
|
|
|
gyro_alpha = expf(-fakeDt / settings.GyroTau);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute time constant for vbar decay term based on a tau
|
2014-01-14 20:03:42 +01:00
|
|
|
vbar_decay = expf(-fakeDt / settings.VbarTau);
|
2014-01-10 23:16:50 +01:00
|
|
|
|
2014-01-14 20:03:42 +01:00
|
|
|
// force flight mode update
|
2014-01-10 23:16:50 +01:00
|
|
|
flight_mode = -1;
|
2013-12-18 11:01:16 +01:00
|
|
|
|
|
|
|
// Rattitude flight mode anti-windup factor
|
2014-01-04 10:22:54 +01:00
|
|
|
rattitude_anti_windup = settings.RattitudeAntiWindup;
|
2014-01-11 22:22:20 +01:00
|
|
|
|
|
|
|
cruise_control_min_throttle = (float) settings.CruiseControlMinThrottle / 100.0f;
|
|
|
|
cruise_control_max_throttle = (float) settings.CruiseControlMaxThrottle / 100.0f;
|
|
|
|
cruise_control_max_angle = settings.CruiseControlMaxAngle;
|
|
|
|
cruise_control_max_power_factor = settings.CruiseControlMaxPowerFactor;
|
|
|
|
cruise_control_power_trim = settings.CruiseControlPowerTrim / 100.0f;
|
|
|
|
cruise_control_inverted_power_switch = settings.CruiseControlInvertedPowerSwitch;
|
|
|
|
cruise_control_neutral_thrust = (float) settings.CruiseControlNeutralThrust / 100.0f;
|
2014-01-15 19:53:14 +01:00
|
|
|
|
|
|
|
memcpy(
|
|
|
|
cruise_control_flight_mode_switch_pos_enable,
|
|
|
|
settings.CruiseControlFlightModeSwitchPosEnable,
|
|
|
|
sizeof(cruise_control_flight_mode_switch_pos_enable));
|
2010-10-03 22:39:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-02 02:25:30 +01:00
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|