Subversion Repositories FlightCtrl

Rev

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

#ifndef _CONFIGURATION_H
#define _CONFIGURATION_H

#include <inttypes.h>
#include <avr/io.h>

#define MAX_CHANNELS 8

// bitmask for VersionInfo_t.HardwareError[0]
#define FC_ERROR0_GYRO_PITCH    0x01
#define FC_ERROR0_GYRO_ROLL     0x02
#define FC_ERROR0_GYRO_YAW      0x04
#define FC_ERROR0_ACC_X         0x08
#define FC_ERROR0_ACC_Y         0x10
#define FC_ERROR0_ACC_Z         0x20
#define FC_ERROR0_PRESSURE      0x40
#define FC_ERROR1_RES0          0x80
// bitmask for VersionInfo_t.HardwareError[1]
#define FC_ERROR1_I2C           0x01
#define FC_ERROR1_BL_MISSING    0x02
#define FC_ERROR1_SPI_RX        0x04
#define FC_ERROR1_PPM           0x08
#define FC_ERROR1_MIXER         0x10
#define FC_ERROR1_RES1          0x20
#define FC_ERROR1_RES2          0x40
#define FC_ERROR1_RES3          0x80

typedef struct {
  uint8_t gyroQuadrant;
  uint8_t accQuadrant;
  uint8_t imuReversedFlags;

  uint8_t gyroPIDFilterConstant;
  uint8_t gyroDWindowLength;
  uint8_t gyroDFilterConstant;
  uint8_t accFilterConstant;

  uint8_t zerothOrderCorrection;
  uint8_t rateTolerance;

  uint8_t gyroActivityDamping;
  uint8_t driftCompDivider; // 1/k  (Koppel_ACC_Wirkung)
  uint8_t driftCompLimit;   // limit for gyrodrift compensation
} IMUConfig_t;

extern IMUConfig_t IMUConfig;

typedef struct {
        uint8_t P;
        uint8_t I;
        uint8_t D;
} PID_t;

typedef struct {
        uint8_t P;
        uint8_t I;
        uint8_t D;
        uint8_t iMax;
} PIDIM_t;

typedef enum {
  FLIGHT_MODE_NONE,
  FLIGHT_MODE_MANUAL,
  FLIGHT_MODE_RATE,
  FLIGHT_MODE_ANGLES
} FlightMode_t;

typedef struct {
    uint8_t SWMajor;
    uint8_t SWMinor;
    uint8_t protoMajor;
    uint8_t protoMinor;
    uint8_t SWPatch;
    uint8_t hardwareErrors[5];
}__attribute__((packed)) VersionInfo_t;

extern VersionInfo_t versionInfo;

typedef struct {
  // IMU stuff:
  PID_t gyroPID[3];

  // Control
  /* P */uint8_t externalControl;

  // Output and servo
  /*PMM*/uint8_t output0Timing;
  /*PMM*/uint8_t output1Timing;

  /* P */uint8_t userParams[8];
} DynamicParams_t;

extern volatile DynamicParams_t dynamicParams;

typedef struct {
  uint8_t sourceIdx, targetIdx;
  uint8_t min, max;
} MMXLATION;

typedef struct {
  uint8_t RCPolarity; // 1=positive, 0=negative. Use positive with Futaba receiver, negative with FrSky.
  uint8_t HWTrim;
  uint8_t variableOffset;
  uint8_t channels[MAX_CHANNELS];
} ChannelMap_t;
extern ChannelMap_t channelMap;

// With fixed wing, we need some way to trim the plane. This is done during a trim flight without gyros activated,
// and then save in a succeeding gyro calibration command.
typedef struct {
  int16_t trim[MAX_CHANNELS];
} RCTrim_t;
extern RCTrim_t rcTrim;

typedef struct {
  int16_t offsets[3];
} sensorOffset_t;

typedef struct {
  uint8_t reverse;
  int8_t minValue;
  int8_t maxValue;
} Servo_t;

#define SERVO_STABILIZATION_REVERSE 1

typedef struct {
  uint8_t bitmask;
  uint8_t timing;
} output_flash_t;

// values above 250 representing poti1 to poti4
typedef struct {
  // Global bitflags
  uint8_t bitConfig;  // see upper defines for bitcoding

  // Control
  PIDIM_t gyroPID[3];

  uint8_t stickIElevator;
  uint8_t stickIAilerons;
  uint8_t stickIRudder;

  uint8_t externalControl; // for serial Control

  uint8_t IFactor;

  uint8_t batteryWarningVoltage;

  // Airspeed
  uint8_t airspeedCorrection;
  uint8_t isFlyingThreshold;

  // Servos
  uint8_t servoCount;
  Servo_t servos[4];

  // Outputs
  output_flash_t outputFlash[2];
  uint8_t outputDebugMask;
  uint8_t outputFlags;

  // User params
  uint8_t userParams[8];

  // Name
  char name[12];
} ParamSet_t;

extern ParamSet_t staticParams;

// bit mask for staticParams.bitConfig
#define CFG_GYRO_SATURATION_PREVENTION  (1<<0)
#define CFG_USE_AIRSPEED_PID            (1<<1)

#define IMU_REVERSE_GYRO_PR             (1<<0)
#define IMU_REVERSE_GYRO_YAW            (1<<1)
#define IMU_REVERSE_ACC_XY              (1<<2)
#define IMU_REVERSE_ACC_Z               (1<<3)

#define ATMEGA644   0
#define ATMEGA644P  1
#define SYSCLK F_CPU

// Not really a part of configuration, but LEDs and HW s test are the same.
#define RED_OFF   {PORTB &=~(1<<PORTB4);}
#define RED_ON    {PORTB |= (1<<PORTB4);}
#define RED_FLASH PORTB ^= (1<<PORTB4)
#define GRN_OFF   {PORTB &=~(1<<PORTB5);}
#define GRN_ON    {PORTB |= (1<<PORTB5);}
#define GRN_FLASH PORTB ^= (1<<PORTB5)

// Mixer table
#define MIX_THROTTLE    0
#define MIX_PITCH   1
#define MIX_ROLL    2
#define MIX_YAW     3

#define VARIABLE_COUNT 8

extern int16_t variables[VARIABLE_COUNT]; // The "Poti"s.
extern uint8_t CPUType;

extern volatile uint16_t isFlying;
extern FlightMode_t currentFlightMode;

void IMUConfig_default(void);
void channelMap_default(void);
void rcTrim_setZero(void);
void paramSet_default(uint8_t setnumber);

void configuration_setFlightParameters(uint8_t newFlightMode);
void configuration_applyVariablesToParams(void);

// Called after a change in configuration parameters, as a hook for modules to take over changes.
void configuration_paramSetDidChange(void);
#endif // _CONFIGURATION_H