Subversion Repositories FlightCtrl

Compare Revisions

Ignore whitespace Rev 2058 → Rev 2059

/branches/dongfang_FC_rewrite/configuration.c
56,12 → 56,13
#include "sensors.h"
#include "rc.h"
#include "output.h"
#include "flight.h"
 
int16_t variables[VARIABLE_COUNT];
ParamSet_t staticParams;
channelMap_t channelMap;
mixerMatrix_t mixerMatrix;
volatile dynamicParam_t dynamicParams;
volatile DynamicParams_t dynamicParams;
 
uint8_t CPUType = ATMEGA644;
uint8_t boardRelease = 13;
73,59 → 74,28
uint16_t isFlying = 0;
volatile uint8_t MKFlags = 0;
 
/************************************************************************
* Map the parameter to pot values
* Replacing this code by the code below saved almost 1 kbyte.
************************************************************************/
const MMXLATION XLATIONS[] = {
{offsetof(ParamSet_t, levelCorrection[0]), offsetof(DynamicParams_t, levelCorrection[0]),0,255},
{offsetof(ParamSet_t, levelCorrection[1]), offsetof(DynamicParams_t, levelCorrection[1]),0,255},
{offsetof(ParamSet_t, gyroP), offsetof(DynamicParams_t, gyroP),0,255},
{offsetof(ParamSet_t, gyroI), offsetof(DynamicParams_t, gyroI),0,255},
{offsetof(ParamSet_t, gyroD), offsetof(DynamicParams_t, gyroD),0,255},
{offsetof(ParamSet_t, attitudeControl), offsetof(DynamicParams_t, attitudeControl),0,255},
{offsetof(ParamSet_t, externalControl), offsetof(DynamicParams_t, externalControl),0,255},
{offsetof(ParamSet_t, dynamicStability), offsetof(DynamicParams_t, dynamicStability),0,255},
{offsetof(ParamSet_t, heightP), offsetof(DynamicParams_t, heightP),0,255},
{offsetof(ParamSet_t, heightI), offsetof(DynamicParams_t, heightI),0,255},
{offsetof(ParamSet_t, heightD), offsetof(DynamicParams_t, heightD),0,255},
{offsetof(ParamSet_t, heightSetting), offsetof(DynamicParams_t, heightSetting),0,255},
{offsetof(ParamSet_t, servoConfigurations[0].manualControl), offsetof(DynamicParams_t, servoManualControl[0]),0,255},
{offsetof(ParamSet_t, servoConfigurations[1].manualControl), offsetof(DynamicParams_t, servoManualControl[1]),0,255},
{offsetof(ParamSet_t, outputFlash[0].timing), offsetof(DynamicParams_t, output0Timing),0,255},
{offsetof(ParamSet_t, outputFlash[1].timing), offsetof(DynamicParams_t, output1Timing),0,255},
{offsetof(ParamSet_t, naviMode), offsetof(DynamicParams_t, naviMode),0,255}};
 
void configuration_applyVariablesToParams(void) {
uint8_t i;
 
#define SET_POT_MM(b,a,min,max) {if (a>=255-VARIABLE_COUNT) b=variables[a+VARIABLE_COUNT-255]; else b=a; if(b<=min) b=min; else if(b>=max) b=max;}
#define SET_POT(b,a) {if (a>=255-VARIABLE_COUNT) b=variables[a+VARIABLE_COUNT-255]; else b=a;}
SET_POT_MM(dynamicParams.gyroP, staticParams.gyroP, 5, 200);
SET_POT(dynamicParams.gyroI, staticParams.gyroI);
SET_POT(dynamicParams.gyroD, staticParams.gyroD);
//SET_POT(dynamicParams.compassControlHeading,staticParams.compassControlHeading);
 
SET_POT(dynamicParams.externalControl, staticParams.externalControl);
SET_POT(dynamicParams.dynamicStability,staticParams.dynamicStability);
SET_POT(dynamicParams.maxAccVector,staticParams.maxAccVector);
 
SET_POT(dynamicParams.heightP, staticParams.heightP);
SET_POT(dynamicParams.heightI, staticParams.heightI);
SET_POT(dynamicParams.heightD, staticParams.heightD);
SET_POT(dynamicParams.heightSetting,staticParams.heightSetting);
 
SET_POT(dynamicParams.attitudeControl,staticParams.attitudeControl);
 
SET_POT(dynamicParams.servoManualControl[0], staticParams.servoConfigurations[0].manualControl);
SET_POT(dynamicParams.servoManualControl[1], staticParams.servoConfigurations[1].manualControl);
 
SET_POT_MM(dynamicParams.output0Timing, staticParams.outputFlash[0].timing,1,255);
SET_POT_MM(dynamicParams.output1Timing, staticParams.outputFlash[1].timing,1,255);
 
SET_POT(dynamicParams.levelCorrection[0], staticParams.levelCorrection[0]);
SET_POT(dynamicParams.levelCorrection[1], staticParams.levelCorrection[1]);
 
SET_POT(dynamicParams.naviMode, staticParams.naviMode);
 
for (i=0; i<sizeof(staticParams.userParams); i++) {
SET_POT(dynamicParams.userParams[i],staticParams.userParams[i]);
}
}
 
const XLATION XLATIONS[] = {
{offsetof(ParamSet_t, heightSetting), offsetof(dynamicParam_t, heightSetting)},
};
 
const MMXLATION MMXLATIONS[] = {
{offsetof(ParamSet_t, heightD), offsetof(dynamicParam_t, heightD),0,100},
};
 
uint8_t configuration_applyVariableToParam(uint8_t src, uint8_t min, uint8_t max) {
uint8_t result;
if (src>=251) result = variables[src-251];
if (src>=(256-VARIABLE_COUNT)) result = variables[src-(256-VARIABLE_COUNT)];
else result = src;
if (result < min) result = min;
else if (result > max) result = max;
132,31 → 102,21
return result;
}
 
void configuration_applyVariablesToParams_dead(void) {
void configuration_applyVariablesToParams(void) {
uint8_t i, src;
uint8_t* pointerToTgt;
for(i=0; i<sizeof(XLATIONS)/sizeof(XLATION); i++) {
src = *((uint8_t*)(&staticParams + XLATIONS[i].sourceIdx));
pointerToTgt = (uint8_t*)(&dynamicParams + XLATIONS[i].targetIdx);
if (src < 255) {
*pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
}
 
for(i=0; i<sizeof(XLATIONS)/sizeof(MMXLATION); i++) {
src = *((uint8_t*)(&staticParams) + XLATIONS[i].sourceIdx);
pointerToTgt = (uint8_t*)(&dynamicParams) + XLATIONS[i].targetIdx;
*pointerToTgt = configuration_applyVariableToParam(src, XLATIONS[i].min, XLATIONS[i].max);
}
 
for(i=0; i<sizeof(MMXLATIONS)/sizeof(MMXLATION); i++) {
src = *((uint8_t*)(&staticParams + MMXLATIONS[i].sourceIdx));
pointerToTgt = (uint8_t*)(&dynamicParams + XLATIONS[i].targetIdx);
if (src < 255) {
*pointerToTgt = configuration_applyVariableToParam(src, MMXLATIONS[i].min, MMXLATIONS[i].max);
}
}
// User parameters are always variable.
for (i=0; i<sizeof(staticParams.userParams); i++) {
src = *((uint8_t*)(&staticParams + offsetof(ParamSet_t, userParams) + i));
pointerToTgt = (uint8_t*)(&dynamicParams + offsetof(dynamicParam_t, userParams) + i);
if (src < 255) {
*pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
}
src = *((uint8_t*)(&staticParams) + offsetof(ParamSet_t, userParams) + i);
pointerToTgt = (uint8_t*)(&dynamicParams) + offsetof(DynamicParams_t, userParams) + i;
*pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
}
}
 
176,7 → 136,6
* tedious to have to modify the code for how to turn on and off LEDs when deploying
* on different HW version....
*/
 
uint8_t getBoardRelease(void) {
uint8_t boardRelease = 13;
// the board release is coded via the pull up or down the 2 status LED
209,6 → 168,24
return boardRelease;
}
 
void configuration_setNormalFlightParameters(void) {
flight_setParameters(staticParams.IFactor, dynamicParams.gyroP,
staticParams.bitConfig & CFG_HEADING_HOLD ? 0 : dynamicParams.gyroI,
dynamicParams.gyroP, staticParams.yawIFactor);
}
 
void configuration_setFailsafeFlightParameters(void) {
flight_setParameters(0, 90, 120, 90, 120);
}
 
// Called after a change in configuration parameters, as a hook for modules to take over changes.
void configuration_paramSetDidChange(void) {
// This should be OK to do here as long as we don't change parameters during emergency flight. We don't.
configuration_setNormalFlightParameters();
// Immediately load changes to output, and also signal the paramset change.
output_init();
}
 
void setOtherDefaults(void) {
// Height Control
staticParams.airpressureFilterConstant = 8;
246,7 → 223,8
// staticParams.driftCompLimit =
staticParams.dynamicStability = 50;
staticParams.maxAccVector = 10;
staticParams.zerothOrderCorrectionAccTolerance = 60;
staticParams.zerothOrderCorrectionControlTolerance = 60;
staticParams.IFactor = 52;
staticParams.yawIFactor = 100;
staticParams.compassYawCorrection = 64;