2011-09-16 22:36:44 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup OpenPilotModules OpenPilot Modules
|
|
|
|
* @{
|
|
|
|
* @addtogroup TxPIDModule TxPID Module
|
|
|
|
* @brief Optional module to tune PID settings using R/C transmitter.
|
|
|
|
* Updates PID settings in RAM in real-time using configured Accessory channels as controllers.
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file txpid.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2011.
|
|
|
|
* @brief Optional module to tune PID settings using R/C transmitter.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output object: StabilizationSettings
|
|
|
|
*
|
|
|
|
* This module will periodically update values of stabilization PID settings
|
|
|
|
* depending on configured input control channels. New values of stabilization
|
|
|
|
* settings are not saved to flash, but updated in RAM. It is expected that the
|
|
|
|
* module will be enabled only for tuning. When desired values are found, they
|
|
|
|
* can be read via GCS and saved permanently. Then this module should be
|
|
|
|
* disabled again.
|
|
|
|
*
|
|
|
|
* UAVObjects are automatically generated by the UAVObjectGenerator from
|
|
|
|
* the object definition XML file.
|
|
|
|
*
|
|
|
|
* Modules have no API, all communication to other modules is done through UAVObjects.
|
|
|
|
* However modules may use the API exposed by shared libraries.
|
|
|
|
* See the OpenPilot wiki for more details.
|
|
|
|
* http://wiki.openpilot.org/display/Doc/OpenPilot+Architecture
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "openpilot.h"
|
2013-09-01 12:10:55 +02:00
|
|
|
#include <pios_struct_helper.h>
|
2011-09-16 22:36:44 +02:00
|
|
|
#include "txpidsettings.h"
|
|
|
|
#include "accessorydesired.h"
|
2011-09-18 13:38:50 +02:00
|
|
|
#include "manualcontrolcommand.h"
|
2011-09-16 22:36:44 +02:00
|
|
|
#include "stabilizationsettings.h"
|
2013-12-11 12:56:15 +01:00
|
|
|
#include "stabilizationbank.h"
|
2013-12-11 17:29:20 +01:00
|
|
|
#include "stabilizationsettingsbank1.h"
|
|
|
|
#include "stabilizationsettingsbank2.h"
|
|
|
|
#include "stabilizationsettingsbank3.h"
|
2011-09-19 15:16:28 +02:00
|
|
|
#include "flightstatus.h"
|
2011-11-13 15:25:58 +01:00
|
|
|
#include "hwsettings.h"
|
2011-09-16 22:36:44 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Configuration
|
|
|
|
//
|
2013-05-19 16:37:30 +02:00
|
|
|
#define SAMPLE_PERIOD_MS 200
|
|
|
|
#define TELEMETRY_UPDATE_PERIOD_MS 0 // 0 = update on change (default)
|
2011-09-16 22:36:44 +02:00
|
|
|
|
|
|
|
// Sanity checks
|
|
|
|
#if (TXPIDSETTINGS_PIDS_NUMELEM != TXPIDSETTINGS_INPUTS_NUMELEM) || \
|
2011-09-18 13:38:50 +02:00
|
|
|
(TXPIDSETTINGS_PIDS_NUMELEM != TXPIDSETTINGS_MINPID_NUMELEM) || \
|
|
|
|
(TXPIDSETTINGS_PIDS_NUMELEM != TXPIDSETTINGS_MAXPID_NUMELEM)
|
2011-09-16 22:36:44 +02:00
|
|
|
#error Invalid TxPID UAVObject definition (inconsistent number of field elements)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Private types
|
|
|
|
|
|
|
|
// Private variables
|
|
|
|
|
|
|
|
// Private functions
|
2013-05-19 16:37:30 +02:00
|
|
|
static void updatePIDs(UAVObjEvent *ev);
|
2011-09-16 22:36:44 +02:00
|
|
|
static uint8_t update(float *var, float val);
|
2014-07-11 23:11:45 +02:00
|
|
|
static uint8_t updateResponsiveness(uint8_t *var, float val);
|
2011-09-18 13:38:50 +02:00
|
|
|
static float scale(float val, float inMin, float inMax, float outMin, float outMax);
|
2011-09-16 22:36:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise the module, called on startup
|
|
|
|
* \returns 0 on success or -1 if initialisation failed
|
|
|
|
*/
|
|
|
|
int32_t TxPIDInitialize(void)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
bool txPIDEnabled;
|
2013-09-01 13:23:20 +02:00
|
|
|
HwSettingsOptionalModulesData optionalModules;
|
2011-11-13 15:25:58 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
HwSettingsInitialize();
|
2013-09-01 13:23:20 +02:00
|
|
|
HwSettingsOptionalModulesGet(&optionalModules);
|
2011-11-13 15:25:58 +01:00
|
|
|
|
2013-09-01 13:23:20 +02:00
|
|
|
if (optionalModules.TxPID == HWSETTINGS_OPTIONALMODULES_ENABLED) {
|
2013-05-19 16:37:30 +02:00
|
|
|
txPIDEnabled = true;
|
|
|
|
} else {
|
|
|
|
txPIDEnabled = false;
|
|
|
|
}
|
2011-11-13 15:25:58 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (txPIDEnabled) {
|
|
|
|
TxPIDSettingsInitialize();
|
|
|
|
AccessoryDesiredInitialize();
|
2011-11-13 15:25:58 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjEvent ev = {
|
|
|
|
.obj = AccessoryDesiredHandle(),
|
|
|
|
.instId = 0,
|
|
|
|
.event = 0,
|
2014-02-18 01:04:43 +01:00
|
|
|
.lowPriority = false,
|
2013-05-19 16:37:30 +02:00
|
|
|
};
|
|
|
|
EventPeriodicCallbackCreate(&ev, updatePIDs, SAMPLE_PERIOD_MS / portTICK_RATE_MS);
|
2011-09-16 22:36:44 +02:00
|
|
|
|
|
|
|
#if (TELEMETRY_UPDATE_PERIOD_MS != 0)
|
2013-05-19 16:37:30 +02:00
|
|
|
// Change StabilizationSettings update rate from OnChange to periodic
|
|
|
|
// to prevent telemetry link flooding with frequent updates in case of
|
|
|
|
// control channel jitter.
|
|
|
|
// Warning: saving to flash with this code active will change the
|
|
|
|
// StabilizationSettings update rate permanently. Use Metadata via
|
|
|
|
// browser to reset to defaults (telemetryAcked=true, OnChange).
|
|
|
|
UAVObjMetadata metadata;
|
|
|
|
StabilizationSettingsInitialize();
|
|
|
|
StabilizationSettingsGetMetadata(&metadata);
|
|
|
|
metadata.telemetryAcked = 0;
|
|
|
|
metadata.telemetryUpdateMode = UPDATEMODE_PERIODIC;
|
|
|
|
metadata.telemetryUpdatePeriod = TELEMETRY_UPDATE_PERIOD_MS;
|
|
|
|
StabilizationSettingsSetMetadata(&metadata);
|
2011-09-16 22:36:44 +02:00
|
|
|
#endif
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2011-09-16 22:36:44 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return -1;
|
2011-11-13 15:25:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* stub: module has no module thread */
|
|
|
|
int32_t TxPIDStart(void)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
return 0;
|
2011-09-16 22:36:44 +02:00
|
|
|
}
|
|
|
|
|
2013-06-04 05:37:40 +02:00
|
|
|
MODULE_INITCALL(TxPIDInitialize, TxPIDStart);
|
2011-11-13 15:25:58 +01:00
|
|
|
|
2011-09-16 22:36:44 +02:00
|
|
|
/**
|
|
|
|
* Update PIDs callback function
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
static void updatePIDs(UAVObjEvent *ev)
|
2011-09-16 22:36:44 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (ev->obj != AccessoryDesiredHandle()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TxPIDSettingsData inst;
|
|
|
|
TxPIDSettingsGet(&inst);
|
|
|
|
|
|
|
|
if (inst.UpdateMode == TXPIDSETTINGS_UPDATEMODE_NEVER) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t armed;
|
|
|
|
FlightStatusArmedGet(&armed);
|
|
|
|
if ((inst.UpdateMode == TXPIDSETTINGS_UPDATEMODE_WHENARMED) &&
|
|
|
|
(armed == FLIGHTSTATUS_ARMED_DISARMED)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-11 12:56:15 +01:00
|
|
|
StabilizationBankData bank;
|
2014-01-14 20:03:42 +01:00
|
|
|
switch (inst.BankNumber) {
|
2013-12-11 17:29:20 +01:00
|
|
|
case 0:
|
2014-01-14 20:03:42 +01:00
|
|
|
StabilizationSettingsBank1Get((StabilizationSettingsBank1Data *)&bank);
|
2013-12-11 17:29:20 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2014-01-14 20:03:42 +01:00
|
|
|
StabilizationSettingsBank2Get((StabilizationSettingsBank2Data *)&bank);
|
2013-12-11 17:29:20 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2014-01-14 20:03:42 +01:00
|
|
|
StabilizationSettingsBank2Get((StabilizationSettingsBank2Data *)&bank);
|
2013-12-11 17:29:20 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
StabilizationSettingsData stab;
|
|
|
|
StabilizationSettingsGet(&stab);
|
|
|
|
AccessoryDesiredData accessory;
|
|
|
|
|
2013-12-11 12:56:15 +01:00
|
|
|
uint8_t needsUpdateBank = 0;
|
|
|
|
uint8_t needsUpdateStab = 0;
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
// Loop through every enabled instance
|
|
|
|
for (uint8_t i = 0; i < TXPIDSETTINGS_PIDS_NUMELEM; i++) {
|
2013-09-01 12:10:55 +02:00
|
|
|
if (cast_struct_to_array(inst.PIDs, inst.PIDs.Instance1)[i] != TXPIDSETTINGS_PIDS_DISABLED) {
|
2013-05-19 16:37:30 +02:00
|
|
|
float value;
|
2013-09-01 12:10:55 +02:00
|
|
|
if (cast_struct_to_array(inst.Inputs, inst.Inputs.Instance1)[i] == TXPIDSETTINGS_INPUTS_THROTTLE) {
|
2013-05-19 16:37:30 +02:00
|
|
|
ManualControlCommandThrottleGet(&value);
|
|
|
|
value = scale(value,
|
2013-09-01 12:10:55 +02:00
|
|
|
inst.ThrottleRange.Min,
|
|
|
|
inst.ThrottleRange.Max,
|
|
|
|
cast_struct_to_array(inst.MinPID, inst.MinPID.Instance1)[i],
|
|
|
|
cast_struct_to_array(inst.MaxPID, inst.MaxPID.Instance1)[i]);
|
|
|
|
} else if (AccessoryDesiredInstGet(
|
|
|
|
cast_struct_to_array(inst.Inputs, inst.Inputs.Instance1)[i] - TXPIDSETTINGS_INPUTS_ACCESSORY0,
|
|
|
|
&accessory) == 0) {
|
|
|
|
value = scale(accessory.AccessoryVal, -1.0f, 1.0f,
|
|
|
|
cast_struct_to_array(inst.MinPID, inst.MinPID.Instance1)[i],
|
|
|
|
cast_struct_to_array(inst.MaxPID, inst.MaxPID.Instance1)[i]);
|
2013-05-19 16:37:30 +02:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-09-01 12:10:55 +02:00
|
|
|
switch (cast_struct_to_array(inst.PIDs, inst.PIDs.Instance1)[i]) {
|
2013-05-19 16:37:30 +02:00
|
|
|
case TXPIDSETTINGS_PIDS_ROLLRATEKP:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.RollRatePID.Kp, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_ROLLRATEKI:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.RollRatePID.Ki, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_ROLLRATEKD:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.RollRatePID.Kd, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_ROLLRATEILIMIT:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.RollRatePID.ILimit, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_ROLLATTITUDEKP:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.RollPI.Kp, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_ROLLATTITUDEKI:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.RollPI.Ki, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_ROLLATTITUDEILIMIT:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.RollPI.ILimit, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
2014-07-11 23:11:45 +02:00
|
|
|
case TXPIDSETTINGS_PIDS_ROLLATTITUDERESP:
|
|
|
|
needsUpdateBank |= updateResponsiveness(&bank.RollMax, value);
|
|
|
|
break;
|
2013-05-19 16:37:30 +02:00
|
|
|
case TXPIDSETTINGS_PIDS_PITCHRATEKP:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.PitchRatePID.Kp, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_PITCHRATEKI:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.PitchRatePID.Ki, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_PITCHRATEKD:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.PitchRatePID.Kd, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_PITCHRATEILIMIT:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.PitchRatePID.ILimit, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_PITCHATTITUDEKP:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.PitchPI.Kp, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_PITCHATTITUDEKI:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.PitchPI.Ki, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_PITCHATTITUDEILIMIT:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.PitchPI.ILimit, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
2014-07-11 23:11:45 +02:00
|
|
|
case TXPIDSETTINGS_PIDS_PITCHATTITUDERESP:
|
|
|
|
needsUpdateBank |= updateResponsiveness(&bank.PitchMax, value);
|
|
|
|
break;
|
2013-05-19 16:37:30 +02:00
|
|
|
case TXPIDSETTINGS_PIDS_ROLLPITCHRATEKP:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.RollRatePID.Kp, value);
|
|
|
|
needsUpdateBank |= update(&bank.PitchRatePID.Kp, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_ROLLPITCHRATEKI:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.RollRatePID.Ki, value);
|
|
|
|
needsUpdateBank |= update(&bank.PitchRatePID.Ki, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_ROLLPITCHRATEKD:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.RollRatePID.Kd, value);
|
|
|
|
needsUpdateBank |= update(&bank.PitchRatePID.Kd, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_ROLLPITCHRATEILIMIT:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.RollRatePID.ILimit, value);
|
|
|
|
needsUpdateBank |= update(&bank.PitchRatePID.ILimit, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_ROLLPITCHATTITUDEKP:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.RollPI.Kp, value);
|
|
|
|
needsUpdateBank |= update(&bank.PitchPI.Kp, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_ROLLPITCHATTITUDEKI:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.RollPI.Ki, value);
|
|
|
|
needsUpdateBank |= update(&bank.PitchPI.Ki, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_ROLLPITCHATTITUDEILIMIT:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.RollPI.ILimit, value);
|
|
|
|
needsUpdateBank |= update(&bank.PitchPI.ILimit, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
2014-07-11 23:11:45 +02:00
|
|
|
case TXPIDSETTINGS_PIDS_ROLLPITCHATTITUDERESP:
|
|
|
|
needsUpdateBank |= updateResponsiveness(&bank.RollMax, value);
|
|
|
|
needsUpdateBank |= updateResponsiveness(&bank.PitchMax, value);
|
|
|
|
break;
|
2013-05-19 16:37:30 +02:00
|
|
|
case TXPIDSETTINGS_PIDS_YAWRATEKP:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.YawRatePID.Kp, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_YAWRATEKI:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.YawRatePID.Ki, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_YAWRATEKD:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.YawRatePID.Kd, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_YAWRATEILIMIT:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.YawRatePID.ILimit, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_YAWATTITUDEKP:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.YawPI.Kp, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_YAWATTITUDEKI:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.YawPI.Ki, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TXPIDSETTINGS_PIDS_YAWATTITUDEILIMIT:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateBank |= update(&bank.YawPI.ILimit, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
2014-07-11 23:11:45 +02:00
|
|
|
case TXPIDSETTINGS_PIDS_YAWATTITUDERESP:
|
|
|
|
needsUpdateBank |= updateResponsiveness(&bank.YawMax, value);
|
|
|
|
break;
|
2013-05-19 16:37:30 +02:00
|
|
|
case TXPIDSETTINGS_PIDS_GYROTAU:
|
2013-12-11 12:56:15 +01:00
|
|
|
needsUpdateStab |= update(&stab.GyroTau, value);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PIOS_Assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-11 12:56:15 +01:00
|
|
|
if (needsUpdateStab) {
|
2013-05-19 16:37:30 +02:00
|
|
|
StabilizationSettingsSet(&stab);
|
|
|
|
}
|
2013-12-11 12:56:15 +01:00
|
|
|
if (needsUpdateBank) {
|
2014-01-14 20:03:42 +01:00
|
|
|
switch (inst.BankNumber) {
|
2013-12-11 17:29:20 +01:00
|
|
|
case 0:
|
2014-01-14 20:03:42 +01:00
|
|
|
StabilizationSettingsBank1Set((StabilizationSettingsBank1Data *)&bank);
|
2013-12-11 17:29:20 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2014-01-14 20:03:42 +01:00
|
|
|
StabilizationSettingsBank2Set((StabilizationSettingsBank2Data *)&bank);
|
2013-12-11 17:29:20 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2014-03-07 22:04:14 +01:00
|
|
|
StabilizationSettingsBank3Set((StabilizationSettingsBank3Data *)&bank);
|
2013-12-11 17:29:20 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2013-12-11 12:56:15 +01:00
|
|
|
}
|
2011-09-16 22:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-09-18 13:38:50 +02:00
|
|
|
* Scales input val from [inMin..inMax] range to [outMin..outMax].
|
|
|
|
* If val is out of input range (inMin <= inMax), it will be bound.
|
|
|
|
* (outMin > outMax) is ok, in that case output will be decreasing.
|
|
|
|
*
|
2011-09-16 22:36:44 +02:00
|
|
|
* \returns scaled value
|
|
|
|
*/
|
2011-09-18 13:38:50 +02:00
|
|
|
static float scale(float val, float inMin, float inMax, float outMin, float outMax)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
// bound input value
|
|
|
|
if (val > inMax) {
|
|
|
|
val = inMax;
|
|
|
|
}
|
|
|
|
if (val < inMin) {
|
|
|
|
val = inMin;
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalize input value to [0..1]
|
|
|
|
if (inMax <= inMin) {
|
|
|
|
val = 0.0f;
|
|
|
|
} else {
|
|
|
|
val = (val - inMin) / (inMax - inMin);
|
|
|
|
}
|
|
|
|
|
|
|
|
// update output bounds
|
|
|
|
if (outMin > outMax) {
|
|
|
|
float t = outMin;
|
|
|
|
outMin = outMax;
|
|
|
|
outMax = t;
|
|
|
|
val = 1.0f - val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (outMax - outMin) * val + outMin;
|
2011-09-16 22:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-09-18 13:38:50 +02:00
|
|
|
* Updates var using val if needed.
|
|
|
|
* \returns 1 if updated, 0 otherwise
|
2011-09-16 22:36:44 +02:00
|
|
|
*/
|
2011-09-18 13:38:50 +02:00
|
|
|
static uint8_t update(float *var, float val)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
/* FIXME: this is not an entirely correct way
|
|
|
|
* to check if the two floating point
|
|
|
|
* numbers are 'not equal'.
|
|
|
|
* Epsilon of 1e-9 is probably okay for the range
|
|
|
|
* of numbers we see here*/
|
|
|
|
if (fabsf(*var - val) > 1e-9f) {
|
|
|
|
*var = val;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2011-09-16 22:36:44 +02:00
|
|
|
}
|
|
|
|
|
2014-07-11 23:11:45 +02:00
|
|
|
/**
|
|
|
|
* Updates var using val if needed.
|
|
|
|
* \returns 1 if updated, 0 otherwise
|
|
|
|
*/
|
|
|
|
static uint8_t updateResponsiveness(uint8_t *var, float val)
|
|
|
|
{
|
|
|
|
// Just floor it for now.
|
|
|
|
uint8_t roundedVal = (uint8_t)val;
|
|
|
|
|
|
|
|
if (*var != roundedVal) {
|
|
|
|
*var = roundedVal;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-16 22:36:44 +02:00
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* @}
|
|
|
|
*/
|
2011-09-16 22:36:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|