Subversion Repositories FlightCtrl

Rev

Rev 2158 | Rev 2160 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1612 dongfang 1
#include <util/delay.h>
1775 - 2
#include <stddef.h>
1960 - 3
#include <string.h>
1612 dongfang 4
#include "configuration.h"
1960 - 5
#include "sensors.h"
6
#include "rc.h"
2048 - 7
#include "output.h"
2059 - 8
#include "flight.h"
1977 - 9
 
1961 - 10
int16_t variables[VARIABLE_COUNT];
2039 - 11
ParamSet_t staticParams;
2156 - 12
ChannelMap_t channelMap;
2158 - 13
MotorMixer_t motorMixer;
2092 - 14
IMUConfig_t IMUConfig;
2059 - 15
volatile DynamicParams_t dynamicParams;
1969 - 16
 
2088 - 17
uint8_t CPUType;
18
uint8_t boardRelease;
1960 - 19
uint8_t requiredMotors;
1963 - 20
 
2018 - 21
VersionInfo_t versionInfo;
22
 
1963 - 23
// MK flags. TODO: Replace by enum. State machine.
24
uint16_t isFlying = 0;
25
volatile uint8_t MKFlags = 0;
26
 
2059 - 27
const MMXLATION XLATIONS[] = {
28
{offsetof(ParamSet_t, levelCorrection[0]), offsetof(DynamicParams_t, levelCorrection[0]),0,255},
29
{offsetof(ParamSet_t, levelCorrection[1]), offsetof(DynamicParams_t, levelCorrection[1]),0,255},
30
{offsetof(ParamSet_t, gyroP), offsetof(DynamicParams_t, gyroP),0,255},
31
{offsetof(ParamSet_t, gyroI), offsetof(DynamicParams_t, gyroI),0,255},
32
{offsetof(ParamSet_t, gyroD), offsetof(DynamicParams_t, gyroD),0,255},
33
{offsetof(ParamSet_t, attitudeControl), offsetof(DynamicParams_t, attitudeControl),0,255},
34
{offsetof(ParamSet_t, externalControl), offsetof(DynamicParams_t, externalControl),0,255},
35
{offsetof(ParamSet_t, dynamicStability), offsetof(DynamicParams_t, dynamicStability),0,255},
36
{offsetof(ParamSet_t, heightP), offsetof(DynamicParams_t, heightP),0,255},
37
{offsetof(ParamSet_t, heightI), offsetof(DynamicParams_t, heightI),0,255},
38
{offsetof(ParamSet_t, heightD), offsetof(DynamicParams_t, heightD),0,255},
39
{offsetof(ParamSet_t, heightSetting), offsetof(DynamicParams_t, heightSetting),0,255},
40
{offsetof(ParamSet_t, servoConfigurations[0].manualControl), offsetof(DynamicParams_t, servoManualControl[0]),0,255},
41
{offsetof(ParamSet_t, servoConfigurations[1].manualControl), offsetof(DynamicParams_t, servoManualControl[1]),0,255},
42
{offsetof(ParamSet_t, outputFlash[0].timing), offsetof(DynamicParams_t, output0Timing),0,255},
43
{offsetof(ParamSet_t, outputFlash[1].timing), offsetof(DynamicParams_t, output1Timing),0,255},
44
{offsetof(ParamSet_t, naviMode), offsetof(DynamicParams_t, naviMode),0,255}};
1775 - 45
 
46
uint8_t configuration_applyVariableToParam(uint8_t src, uint8_t min, uint8_t max) {
47
  uint8_t result;
2059 - 48
  if (src>=(256-VARIABLE_COUNT)) result = variables[src-(256-VARIABLE_COUNT)];
1775 - 49
  else result = src;
50
  if (result < min) result = min;
51
  else if (result > max) result = max;
52
  return result;
53
}
54
 
2059 - 55
void configuration_applyVariablesToParams(void) {
1775 - 56
  uint8_t i, src;
57
  uint8_t* pointerToTgt;
2059 - 58
 
59
  for(i=0; i<sizeof(XLATIONS)/sizeof(MMXLATION); i++) {
60
    src = *((uint8_t*)(&staticParams) + XLATIONS[i].sourceIdx);
61
    pointerToTgt = (uint8_t*)(&dynamicParams) + XLATIONS[i].targetIdx;
62
    *pointerToTgt = configuration_applyVariableToParam(src, XLATIONS[i].min, XLATIONS[i].max);
1775 - 63
  }
64
 
2059 - 65
  // User parameters are always variable.
1960 - 66
  for (i=0; i<sizeof(staticParams.userParams); i++) {
2059 - 67
    src = *((uint8_t*)(&staticParams) + offsetof(ParamSet_t, userParams) + i);
68
    pointerToTgt = (uint8_t*)(&dynamicParams) + offsetof(DynamicParams_t, userParams) + i;
69
    *pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
1775 - 70
  }
71
}
72
 
2088 - 73
void setCPUType(void) {   // works only after reset or power on when the registers have default values
2017 - 74
  if((UCSR1A == 0x20) && (UCSR1C == 0x06)) CPUType = ATMEGA644P;  // initial Values for 644P after reset
2088 - 75
  else CPUType = ATMEGA644;
1612 dongfang 76
}
77
 
78
/*
79
 * Automatic detection of hardware components is not supported in this development-oriented
80
 * FC firmware. It would go against the point of it: To enable alternative hardware
81
 * configurations with otherwise unsupported components. Instead, one should write
82
 * custom code + adjust constants for the new hardware, and include the relevant code
83
 * from the makefile.
84
 * However - we still do detect the board release. Reason: Otherwise it would be too
85
 * tedious to have to modify the code for how to turn on and off LEDs when deploying
86
 * on different HW version....
87
 */
2088 - 88
void setBoardRelease(void) {
1612 dongfang 89
  // the board release is coded via the pull up or down the 2 status LED
90
 
91
  PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate
92
  DDRB  &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input
93
 
94
  _delay_loop_2(1000); // make some delay
95
 
96
  switch( PINB & ((1<<PINB1)|(1<<PINB0))) {
97
    case 0x00:
1960 - 98
      boardRelease = 10; // 1.0
1612 dongfang 99
      break;
100
    case 0x01:
1960 - 101
      boardRelease = 11; // 1.1 or 1.2
1612 dongfang 102
      break;
103
    case 0x02:
1960 - 104
      boardRelease = 20; // 2.0
1612 dongfang 105
      break;
106
    case 0x03:
1960 - 107
      boardRelease = 13; // 1.3
1612 dongfang 108
      break;
109
    default:
110
      break;
111
    }
112
  // set LED ports as output
113
  DDRB |= (1<<DDB1)|(1<<DDB0);
1964 - 114
  RED_OFF;
1612 dongfang 115
  GRN_OFF;
116
}
1960 - 117
 
2059 - 118
void configuration_setNormalFlightParameters(void) {
119
  flight_setParameters(staticParams.IFactor, dynamicParams.gyroP,
120
      staticParams.bitConfig & CFG_HEADING_HOLD ? 0 : dynamicParams.gyroI,
121
      dynamicParams.gyroP, staticParams.yawIFactor);
122
}
123
 
124
void configuration_setFailsafeFlightParameters(void) {
125
  flight_setParameters(0, 90, 120, 90, 120);
126
}
127
 
128
// Called after a change in configuration parameters, as a hook for modules to take over changes.
129
void configuration_paramSetDidChange(void) {
130
  // This should be OK to do here as long as we don't change parameters during emergency flight. We don't.
131
  configuration_setNormalFlightParameters();
132
  // Immediately load changes to output, and also signal the paramset change.
133
  output_init();
134
}
135
 
1960 - 136
void setOtherDefaults(void) {
137
  // Height Control
2035 - 138
  staticParams.airpressureFilterConstant = 8;
2036 - 139
  //staticParams.airpressureWindowLength = 0;
2026 - 140
  staticParams.airpressureAccZCorrection = 128+56;
1960 - 141
  staticParams.heightP = 10;
142
  staticParams.heightD = 30;
143
  staticParams.heightSetting = 251;
1961 - 144
  staticParams.heightControlMaxThrottleChange = 10;
1960 - 145
 
146
  // Control
147
  staticParams.stickP = 8;
148
  staticParams.stickD = 12;
149
  staticParams.stickYawP = 12;
150
  staticParams.stickThrottleD = 12;
151
  staticParams.minThrottle = 8;
152
  staticParams.maxThrottle = 230;
153
  staticParams.externalControl = 0;
2158 - 154
  staticParams.attitudeControl = 0;
1988 - 155
  staticParams.motorSmoothing = 0;
1960 - 156
 
157
  staticParams.gyroP = 60;
158
  staticParams.gyroI = 80;
159
  staticParams.gyroD = 4;
160
 
1961 - 161
  // set by gyro-specific code: gyro_setDefaults().
1960 - 162
  // staticParams.zerothOrderCorrection = 
163
  // staticParams.driftCompDivider = 
164
  // staticParams.driftCompLimit = 
165
 
166
  staticParams.dynamicStability = 50;
2055 - 167
  staticParams.IFactor = 52;
1977 - 168
  staticParams.yawIFactor = 100;  
2051 - 169
  staticParams.compassYawCorrection = 64;
2052 - 170
  staticParams.compassP = 50;
1988 - 171
  staticParams.levelCorrection[0] = staticParams.levelCorrection[1] = 128;
1960 - 172
 
173
  // Servos
1980 - 174
  staticParams.servoCount = 7;
175
  staticParams.servoManualMaxSpeed = 10;
1960 - 176
  for (uint8_t i=0; i<2; i++) {
177
    staticParams.servoConfigurations[i].manualControl = 128;
1980 - 178
    staticParams.servoConfigurations[i].stabilizationFactor = 0;
1977 - 179
    staticParams.servoConfigurations[i].minValue = 32;
180
    staticParams.servoConfigurations[i].maxValue = 224;
1960 - 181
    staticParams.servoConfigurations[i].flags = 0;
182
  }
1980 - 183
 
1960 - 184
  // Battery warning and emergency flight
185
  staticParams.batteryVoltageWarning = 101;  // 3.7 each for 3S
186
  staticParams.emergencyThrottle = 35;
187
  staticParams.emergencyFlightDuration = 30;
188
 
189
  // Outputs
1961 - 190
  staticParams.outputFlash[0].bitmask = 1; //0b01011111;
1960 - 191
  staticParams.outputFlash[0].timing = 15;
1961 - 192
  staticParams.outputFlash[1].bitmask = 3; //0b11110011;
1960 - 193
  staticParams.outputFlash[1].timing = 15;
194
 
2158 - 195
  staticParams.outputDebugMask = DEBUG_ACC0THORDER;
2048 - 196
  staticParams.outputFlags   = OUTPUTFLAGS_FLASH_0_AT_BEEP | OUTPUTFLAGS_FLASH_1_AT_BEEP | OUTPUTFLAGS_USE_ONBOARD_LEDS;
197
 
2058 - 198
  staticParams.naviMode = 0; // free.
2092 - 199
  staticParams.airpressureWindowLength = 0;
200
  staticParams.airpressureDWindowLength = 24;
201
 
202
  staticParams.heightControlMaxIntegralIn = 125;
203
  staticParams.heightControlMaxIntegralOut = 75;
204
  staticParams.heightControlMaxThrottleChange = 75;
205
  staticParams.heightControlTestOscPeriod = 0;
206
  staticParams.heightControlTestOscAmplitude = 0;
1960 - 207
}
208
 
209
/***************************************************/
210
/*    Default Values for parameter set 1           */
211
/***************************************************/
212
void paramSet_default(uint8_t setnumber) {
213
  setOtherDefaults();
214
 
215
  for (uint8_t i=0; i<8; i++) {
2092 - 216
    staticParams.userParams[i] = i;
1960 - 217
  }
218
 
2158 - 219
  staticParams.bitConfig = CFG_GYRO_SATURATION_PREVENTION;
1960 - 220
 
221
  memcpy(staticParams.name, "Default\0", 6);
222
}
223
 
2092 - 224
void IMUConfig_default(void) {
225
  IMUConfig.gyroPIDFilterConstant = 1;
2095 - 226
  IMUConfig.gyroDWindowLength = 3;
2158 - 227
  // IMUConfig.gyroDFilterConstant = 1;
2092 - 228
  IMUConfig.accFilterConstant = 10;
229
  IMUConfig.rateTolerance = 120;
2095 - 230
  IMUConfig.gyroActivityDamping = 24;
2092 - 231
 
232
  gyro_setDefaultParameters();
233
}
234
 
1960 - 235
/***************************************************/
236
/*    Default Values for Mixer Table               */
237
/***************************************************/
2158 - 238
void motorMixer_default(void) { // Quadro
1960 - 239
  uint8_t i;
240
  // clear mixer table (but preset throttle)
2158 - 241
  for (i = 0; i < MAX_MOTORS; i++) {
242
    motorMixer.matrix[i][MIX_THROTTLE] = i < 4 ? 64 : 0;
243
    motorMixer.matrix[i][MIX_PITCH] = 0;
244
    motorMixer.matrix[i][MIX_ROLL] = 0;
245
    motorMixer.matrix[i][MIX_YAW] = 0;
2159 - 246
    motorMixer.matrix[i][MIX_OPPOSITE_MOTOR] = (uint8_t)-1;
1960 - 247
  }
248
  // default = Quadro
2158 - 249
  motorMixer.matrix[0][MIX_PITCH] = +64;
250
  motorMixer.matrix[0][MIX_YAW] = +64;
251
  motorMixer.matrix[0][MIX_OPPOSITE_MOTOR] = 1;
2020 - 252
 
2158 - 253
  motorMixer.matrix[1][MIX_PITCH] = -64;
254
  motorMixer.matrix[1][MIX_YAW] = +64;
255
  motorMixer.matrix[1][MIX_OPPOSITE_MOTOR] = 0;
256
 
257
  motorMixer.matrix[2][MIX_ROLL] = -64;
258
  motorMixer.matrix[2][MIX_YAW] = -64;
259
  motorMixer.matrix[2][MIX_OPPOSITE_MOTOR] = 3;
260
 
261
  motorMixer.matrix[3][MIX_ROLL] = +64;
262
  motorMixer.matrix[3][MIX_YAW] = -64;
263
  motorMixer.matrix[3][MIX_OPPOSITE_MOTOR] = 2;
264
 
265
  memcpy(motorMixer.name, "Quadro +\0", 9);
266
 
2026 - 267
  /*
2020 - 268
  // default = X
269
  mixerMatrix.motor[0][MIX_PITCH] = +45;
270
  mixerMatrix.motor[0][MIX_ROLL]  = +45;
271
  mixerMatrix.motor[0][MIX_YAW]   = +64;
272
 
273
  mixerMatrix.motor[1][MIX_PITCH] = -45;
274
  mixerMatrix.motor[1][MIX_ROLL]  = -45;
275
  mixerMatrix.motor[1][MIX_YAW]   = +64;
276
 
277
  mixerMatrix.motor[2][MIX_PITCH] = +45;
278
  mixerMatrix.motor[2][MIX_ROLL]  = -45;
279
  mixerMatrix.motor[2][MIX_YAW]   = -64;
280
 
281
  mixerMatrix.motor[3][MIX_PITCH] = -45;
282
  mixerMatrix.motor[3][MIX_ROLL]  = +45;
283
  mixerMatrix.motor[3][MIX_YAW]   = -64;
2158 - 284
 
285
  memcpy(motorMixer.name, "Quadro X\0", 9);
2026 - 286
  */
1960 - 287
}
288
 
1968 - 289
/***************************************************/
290
/*    Default Values for R/C Channels              */
291
/***************************************************/
1960 - 292
void channelMap_default(void) {
1968 - 293
  channelMap.channels[CH_PITCH]    = 1;
294
  channelMap.channels[CH_ROLL]     = 0;
295
  channelMap.channels[CH_THROTTLE] = 2;
296
  channelMap.channels[CH_YAW]      = 3;
297
  channelMap.channels[CH_POTS + 0] = 4;
298
  channelMap.channels[CH_POTS + 1] = 5;
299
  channelMap.channels[CH_POTS + 2] = 6;
300
  channelMap.channels[CH_POTS + 3] = 7;
1960 - 301
}