Subversion Repositories FlightCtrl

Rev

Rev 2109 | Rev 2122 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include <util/delay.h>
#include <stddef.h>
#include <string.h>
#include "configuration.h"
#include "rc.h"
#include "output.h"
#include "flight.h"

int16_t variables[VARIABLE_COUNT];
ParamSet_t staticParams;
ChannelMap_t channelMap;
IMUConfig_t IMUConfig;
volatile DynamicParams_t dynamicParams;

VersionInfo_t versionInfo;

FlightMode_t currentFlightMode = FLIGHT_MODE_MANUAL;
// updated by considering airspeed..
volatile uint16_t isFlying = 0;

const MMXLATION XLATIONS[] = {
{offsetof(ParamSet_t, externalControl), offsetof(DynamicParams_t, externalControl),0,255},

{offsetof(ParamSet_t, gyroPID[0].P), offsetof(DynamicParams_t, gyroPID[0].P),0,255},
{offsetof(ParamSet_t, gyroPID[0].I), offsetof(DynamicParams_t, gyroPID[0].I),0,255},
{offsetof(ParamSet_t, gyroPID[0].D), offsetof(DynamicParams_t, gyroPID[0].D),0,255},

{offsetof(ParamSet_t, gyroPID[1].P), offsetof(DynamicParams_t, gyroPID[1].P),0,255},
{offsetof(ParamSet_t, gyroPID[1].I), offsetof(DynamicParams_t, gyroPID[1].I),0,255},
{offsetof(ParamSet_t, gyroPID[1].D), offsetof(DynamicParams_t, gyroPID[1].D),0,255},

{offsetof(ParamSet_t, gyroPID[2].P), offsetof(DynamicParams_t, gyroPID[2].P),0,255},
{offsetof(ParamSet_t, gyroPID[2].I), offsetof(DynamicParams_t, gyroPID[2].I),0,255},
{offsetof(ParamSet_t, gyroPID[2].D), offsetof(DynamicParams_t, gyroPID[2].D),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},
};

uint8_t configuration_applyVariableToParam(uint8_t src, uint8_t min, uint8_t max) {
  uint8_t result;
  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;
  return result;
}

void configuration_applyVariablesToParams(void) {
  uint8_t i, src;
  uint8_t* pointerToTgt;

  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);
  }

  // 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(DynamicParams_t, userParams) + i;
    *pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
  }
}

void configuration_setFlightParameters(uint8_t newFlightMode) {
        currentFlightMode = newFlightMode;
        flight_updateFlightParametersToFlightMode();
}

// 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_setFlightParameters(currentFlightMode);
  // Immediately load changes to output, and also signal the paramset change.
  output_init();
}

void setOtherDefaults(void) {
  // Control
  staticParams.externalControl = 0;
  staticParams.IFactor = 52;

  staticParams.airspeedCorrection = 100;
  staticParams.isFlyingThreshold = 100;

  // Servos
  staticParams.servoCount = 7;
  staticParams.servoManualMaxSpeed = 10;
  for (uint8_t i=0; i<2; i++) {
    staticParams.servoConfigurations[i].manualControl = 128;
    staticParams.servoConfigurations[i].stabilizationFactor = 0;
    staticParams.servoConfigurations[i].minValue = 32;
    staticParams.servoConfigurations[i].maxValue = 224;
    staticParams.servoConfigurations[i].flags = 0;
  }
 
  // Battery warning and emergency flight
  staticParams.batteryVoltageWarning = 101;  // 3.7 each for 3S
  staticParams.emergencyThrottle = 35;
  staticParams.emergencyFlightDuration = 30;

  for (uint8_t i=0; i<3; i++) {
          staticParams.gyroPID[i].P = 80;
          staticParams.gyroPID[i].I = 80;
          staticParams.gyroPID[i].D = 40;
          staticParams.gyroPID[i].iMax = 45;
  }

  staticParams.stickIElevator = 80;
  staticParams.stickIAilerons = 120;
  staticParams.stickIRudder = 40;

  // Outputs
  staticParams.outputFlash[0].bitmask = 1; //0b01011111;
  staticParams.outputFlash[0].timing = 15;
  staticParams.outputFlash[1].bitmask = 3; //0b11110011;
  staticParams.outputFlash[1].timing = 15;

  staticParams.outputDebugMask = 8;
  staticParams.outputFlags   = OUTPUTFLAGS_FLASH_0_AT_BEEP | OUTPUTFLAGS_FLASH_1_AT_BEEP | OUTPUTFLAGS_USE_ONBOARD_LEDS;
}

/***************************************************/
/*    Default Values for parameter set 1           */
/***************************************************/
void paramSet_default(uint8_t setnumber) {
  setOtherDefaults();
 
  for (uint8_t i=0; i<8; i++) {
    staticParams.userParams[i] = i;
  }

  staticParams.bitConfig =
    CFG_GYRO_SATURATION_PREVENTION;

  memcpy(staticParams.name, "Default\0", 6);
}

void IMUConfig_default(void) {
  IMUConfig.gyroPIDFilterConstant = 1;
  IMUConfig.gyroDFilterConstant = 1;
  IMUConfig.rateTolerance = 120;
  IMUConfig.gyroDWindowLength = 3;
  IMUConfig.gyroQuadrant = 0;
  IMUConfig.imuReversedFlags = 0;
}

/***************************************************/
/*    Default Values for R/C Channels              */
/***************************************************/
void channelMap_default(void) {
        channelMap.trim = 0;
  channelMap.channels[CH_ELEVATOR] = 1;
  channelMap.channels[CH_AILERONS] = 0;
  channelMap.channels[CH_THROTTLE] = 2;
  channelMap.channels[CH_RUDDER]   = 3;
  channelMap.channels[CH_POTS + 0] = 4;
  channelMap.channels[CH_POTS + 1] = 5;
  channelMap.channels[CH_POTS + 2] = 6;
  channelMap.channels[CH_POTS + 3] = 7;
}