Subversion Repositories FlightCtrl

Rev

Rev 2164 | 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"
2189 - 9
#include "debug.h"
1977 - 10
 
1961 - 11
int16_t variables[VARIABLE_COUNT];
2039 - 12
ParamSet_t staticParams;
2156 - 13
ChannelMap_t channelMap;
2189 - 14
OutputMixer_t outputMixer;
2092 - 15
IMUConfig_t IMUConfig;
2059 - 16
volatile DynamicParams_t dynamicParams;
1969 - 17
 
2088 - 18
uint8_t CPUType;
19
uint8_t boardRelease;
1963 - 20
 
2018 - 21
VersionInfo_t versionInfo;
22
 
1963 - 23
// MK flags. TODO: Replace by enum. State machine.
2189 - 24
uint16_t isFlying;
25
volatile uint8_t MKFlags;
1963 - 26
 
2059 - 27
const MMXLATION XLATIONS[] = {
2189 - 28
{offsetof(ParamSet_t, flightMode), offsetof(DynamicParams_t, flightMode),0,255},
29
 
2059 - 30
{offsetof(ParamSet_t, levelCorrection[0]), offsetof(DynamicParams_t, levelCorrection[0]),0,255},
31
{offsetof(ParamSet_t, levelCorrection[1]), offsetof(DynamicParams_t, levelCorrection[1]),0,255},
2189 - 32
 
33
{offsetof(ParamSet_t, attGyroP), offsetof(DynamicParams_t, attGyroP),0,250},
34
{offsetof(ParamSet_t, attGyroI), offsetof(DynamicParams_t, attGyroI),0,250},
35
{offsetof(ParamSet_t, attGyroD), offsetof(DynamicParams_t, attGyroD),0,250},
36
 
37
{offsetof(ParamSet_t, rateGyroP), offsetof(DynamicParams_t, rateGyroP),0,250},
38
{offsetof(ParamSet_t, rateGyroI), offsetof(DynamicParams_t, rateGyroI),0,250},
39
{offsetof(ParamSet_t, rateGyroD), offsetof(DynamicParams_t, rateGyroD),0,250},
40
 
41
{offsetof(ParamSet_t, yawGyroP), offsetof(DynamicParams_t, yawGyroP),0,250},
42
{offsetof(ParamSet_t, yawGyroI), offsetof(DynamicParams_t, yawGyroI),0,250},
43
{offsetof(ParamSet_t, yawGyroD), offsetof(DynamicParams_t, yawGyroD),0,250},
44
 
2059 - 45
{offsetof(ParamSet_t, attitudeControl), offsetof(DynamicParams_t, attitudeControl),0,255},
46
{offsetof(ParamSet_t, externalControl), offsetof(DynamicParams_t, externalControl),0,255},
47
{offsetof(ParamSet_t, heightP), offsetof(DynamicParams_t, heightP),0,255},
48
{offsetof(ParamSet_t, heightI), offsetof(DynamicParams_t, heightI),0,255},
49
{offsetof(ParamSet_t, heightD), offsetof(DynamicParams_t, heightD),0,255},
50
{offsetof(ParamSet_t, heightSetting), offsetof(DynamicParams_t, heightSetting),0,255},
51
{offsetof(ParamSet_t, servoConfigurations[0].manualControl), offsetof(DynamicParams_t, servoManualControl[0]),0,255},
52
{offsetof(ParamSet_t, servoConfigurations[1].manualControl), offsetof(DynamicParams_t, servoManualControl[1]),0,255},
53
{offsetof(ParamSet_t, outputFlash[0].timing), offsetof(DynamicParams_t, output0Timing),0,255},
54
{offsetof(ParamSet_t, outputFlash[1].timing), offsetof(DynamicParams_t, output1Timing),0,255},
55
{offsetof(ParamSet_t, naviMode), offsetof(DynamicParams_t, naviMode),0,255}};
1775 - 56
 
57
uint8_t configuration_applyVariableToParam(uint8_t src, uint8_t min, uint8_t max) {
58
  uint8_t result;
2059 - 59
  if (src>=(256-VARIABLE_COUNT)) result = variables[src-(256-VARIABLE_COUNT)];
1775 - 60
  else result = src;
61
  if (result < min) result = min;
62
  else if (result > max) result = max;
63
  return result;
64
}
65
 
2189 - 66
void configuration_fixVariableParams(uint8_t variableNumber) {
67
  // If variable value is to great, limit it.
68
  uint8_t value = variables[variableNumber];
69
  if (value >= 256-VARIABLE_COUNT)
70
    value = 256-VARIABLE_COUNT - 1;
71
  for (uint8_t i=0; i<sizeof(DynamicParams_t); i++) {
72
    uint8_t* pointerToParam =(uint8_t*)(&dynamicParams + i);
73
    if (*pointerToParam == 256-VARIABLE_COUNT + variableNumber) {
74
       *pointerToParam = value;
75
    }
76
  }
77
}
78
 
2059 - 79
void configuration_applyVariablesToParams(void) {
1775 - 80
  uint8_t i, src;
81
  uint8_t* pointerToTgt;
2059 - 82
 
83
  for(i=0; i<sizeof(XLATIONS)/sizeof(MMXLATION); i++) {
84
    src = *((uint8_t*)(&staticParams) + XLATIONS[i].sourceIdx);
85
    pointerToTgt = (uint8_t*)(&dynamicParams) + XLATIONS[i].targetIdx;
86
    *pointerToTgt = configuration_applyVariableToParam(src, XLATIONS[i].min, XLATIONS[i].max);
1775 - 87
  }
88
 
2059 - 89
  // User parameters are always variable.
1960 - 90
  for (i=0; i<sizeof(staticParams.userParams); i++) {
2059 - 91
    src = *((uint8_t*)(&staticParams) + offsetof(ParamSet_t, userParams) + i);
92
    pointerToTgt = (uint8_t*)(&dynamicParams) + offsetof(DynamicParams_t, userParams) + i;
93
    *pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
1775 - 94
  }
95
}
96
 
2088 - 97
void setCPUType(void) {   // works only after reset or power on when the registers have default values
2160 - 98
#if (MCU_TYPE==atmega644)
99
        CPUType=ATMEGA644;
100
#else
2017 - 101
  if((UCSR1A == 0x20) && (UCSR1C == 0x06)) CPUType = ATMEGA644P;  // initial Values for 644P after reset
2088 - 102
  else CPUType = ATMEGA644;
2160 - 103
#endif
1612 dongfang 104
}
105
 
106
/*
107
 * Automatic detection of hardware components is not supported in this development-oriented
108
 * FC firmware. It would go against the point of it: To enable alternative hardware
109
 * configurations with otherwise unsupported components. Instead, one should write
110
 * custom code + adjust constants for the new hardware, and include the relevant code
111
 * from the makefile.
112
 * However - we still do detect the board release. Reason: Otherwise it would be too
113
 * tedious to have to modify the code for how to turn on and off LEDs when deploying
114
 * on different HW version....
115
 */
2088 - 116
void setBoardRelease(void) {
1612 dongfang 117
  // the board release is coded via the pull up or down the 2 status LED
118
 
119
  PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate
120
  DDRB  &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input
121
 
122
  _delay_loop_2(1000); // make some delay
123
 
124
  switch( PINB & ((1<<PINB1)|(1<<PINB0))) {
125
    case 0x00:
1960 - 126
      boardRelease = 10; // 1.0
1612 dongfang 127
      break;
128
    case 0x01:
1960 - 129
      boardRelease = 11; // 1.1 or 1.2
1612 dongfang 130
      break;
131
    case 0x02:
1960 - 132
      boardRelease = 20; // 2.0
1612 dongfang 133
      break;
134
    case 0x03:
1960 - 135
      boardRelease = 13; // 1.3
1612 dongfang 136
      break;
137
    default:
138
      break;
139
    }
140
  // set LED ports as output
141
  DDRB |= (1<<DDB1)|(1<<DDB0);
1964 - 142
  RED_OFF;
1612 dongfang 143
  GRN_OFF;
144
}
1960 - 145
 
2189 - 146
void configuration_setNormalFlightMode(void) {
147
  FlightMode_t flight_flightMode;
148
  if (dynamicParams.flightMode < 255/3)
149
    flight_flightMode = FM_RETURN_TO_LEVEL;
150
  else if (dynamicParams.flightMode < 255*2/3)
151
    flight_flightMode = FM_HEADING_HOLD;
152
  else
153
    flight_flightMode = FM_RATE;
154
  flight_setMode(flight_flightMode);
155
  debugOut.analog[20] = flight_flightMode;
2059 - 156
}
157
 
2189 - 158
void configuration_setFailsafeFlightMode(void) {
159
  flight_setMode(FM_RETURN_TO_LEVEL);
2059 - 160
}
161
 
162
// Called after a change in configuration parameters, as a hook for modules to take over changes.
163
void configuration_paramSetDidChange(void) {
164
  // This should be OK to do here as long as we don't change parameters during emergency flight. We don't.
2189 - 165
  // No longer necessary.
166
  // configuration_setNormalFlightMode();
167
  flight_setParameters();
168
  output_setParameters();
2059 - 169
  // Immediately load changes to output, and also signal the paramset change.
170
  output_init();
171
}
172
 
1960 - 173
void setOtherDefaults(void) {
174
  // Height Control
2035 - 175
  staticParams.airpressureFilterConstant = 8;
2189 - 176
  staticParams.airpressureWindowLength = 8;
177
  staticParams.airpressureDWindowLength = 24;
178
 
2026 - 179
  staticParams.airpressureAccZCorrection = 128+56;
1960 - 180
  staticParams.heightP = 10;
181
  staticParams.heightD = 30;
182
  staticParams.heightSetting = 251;
1961 - 183
  staticParams.heightControlMaxThrottleChange = 10;
1960 - 184
 
185
  // Control
2189 - 186
  staticParams.flightMode = 248;
1960 - 187
  staticParams.stickP = 8;
2189 - 188
  staticParams.stickD = 0;
189
  staticParams.stickYawP = 8;
190
  staticParams.stickThrottleD = 4;
191
 
1960 - 192
  staticParams.minThrottle = 8;
193
  staticParams.maxThrottle = 230;
2189 - 194
 
1960 - 195
  staticParams.externalControl = 0;
2158 - 196
  staticParams.attitudeControl = 0;
2189 - 197
  staticParams.dynamicStability = 80; // 0 means: Regulation only by decreasing throttle. 200: Only by increasing.
1960 - 198
 
2189 - 199
  staticParams.attGyroP = PID_NORMAL_VALUE;
200
  staticParams.attGyroI = PID_NORMAL_VALUE;
201
  staticParams.attGyroIMax = 0;
202
  staticParams.attGyroD = PID_NORMAL_VALUE * 3/2;
203
 
204
  staticParams.rateGyroP = PID_NORMAL_VALUE;
205
  staticParams.rateGyroI = PID_NORMAL_VALUE;
206
  staticParams.rateGyroIMax = 0;
207
  staticParams.rateGyroD = PID_NORMAL_VALUE;
208
 
209
  staticParams.yawGyroP = PID_NORMAL_VALUE;
210
  staticParams.yawGyroI = PID_NORMAL_VALUE;
211
  staticParams.yawGyroD = PID_NORMAL_VALUE;
1960 - 212
 
2189 - 213
  staticParams.compassMode = 0;
2051 - 214
  staticParams.compassYawCorrection = 64;
2189 - 215
  staticParams.compassP = PID_NORMAL_VALUE;
216
  staticParams.levelCorrection[0] = 249; // var1
217
  staticParams.levelCorrection[1] = 250; // var2
1960 - 218
 
219
  // Servos
2189 - 220
  staticParams.servoCount = 8;
1980 - 221
  staticParams.servoManualMaxSpeed = 10;
1960 - 222
  for (uint8_t i=0; i<2; i++) {
223
    staticParams.servoConfigurations[i].manualControl = 128;
2189 - 224
    staticParams.servoConfigurations[i].stabilizationFactor = 100;
1960 - 225
    staticParams.servoConfigurations[i].flags = 0;
226
  }
1980 - 227
 
1960 - 228
  // Battery warning and emergency flight
229
  staticParams.batteryVoltageWarning = 101;  // 3.7 each for 3S
230
  staticParams.emergencyThrottle = 35;
231
  staticParams.emergencyFlightDuration = 30;
232
 
233
  // Outputs
1961 - 234
  staticParams.outputFlash[0].bitmask = 1; //0b01011111;
1960 - 235
  staticParams.outputFlash[0].timing = 15;
1961 - 236
  staticParams.outputFlash[1].bitmask = 3; //0b11110011;
1960 - 237
  staticParams.outputFlash[1].timing = 15;
238
 
2189 - 239
  staticParams.outputDebugMask = DEBUG_INVERTED;
240
  staticParams.outputFlags   = /* OUTPUTFLAGS_FLASH_0_AT_BEEP | OUTPUTFLAGS_FLASH_1_AT_BEEP | */ OUTPUTFLAGS_USE_ONBOARD_LEDS;
2048 - 241
 
2058 - 242
  staticParams.naviMode = 0; // free.
2092 - 243
 
244
  staticParams.heightControlMaxIntegralIn = 125;
245
  staticParams.heightControlMaxIntegralOut = 75;
246
  staticParams.heightControlMaxThrottleChange = 75;
247
  staticParams.heightControlTestOscPeriod = 0;
248
  staticParams.heightControlTestOscAmplitude = 0;
1960 - 249
}
250
 
251
/***************************************************/
252
/*    Default Values for parameter set 1           */
253
/***************************************************/
254
void paramSet_default(uint8_t setnumber) {
255
  setOtherDefaults();
256
 
257
  for (uint8_t i=0; i<8; i++) {
2092 - 258
    staticParams.userParams[i] = i;
1960 - 259
  }
260
 
2189 - 261
  staticParams.bitConfig = 0;//CFG_GYRO_SATURATION_PREVENTION;
1960 - 262
 
263
  memcpy(staticParams.name, "Default\0", 6);
264
}
265
 
2092 - 266
void IMUConfig_default(void) {
267
  IMUConfig.gyroPIDFilterConstant = 1;
2095 - 268
  IMUConfig.gyroDWindowLength = 3;
2158 - 269
  // IMUConfig.gyroDFilterConstant = 1;
2092 - 270
  IMUConfig.accFilterConstant = 10;
2189 - 271
 
272
 
2092 - 273
  gyro_setDefaultParameters();
274
}
275
 
1960 - 276
/***************************************************/
277
/*    Default Values for Mixer Table               */
278
/***************************************************/
2189 - 279
void outputMixer_default(void) { // Quadro
1960 - 280
  uint8_t i;
281
  // clear mixer table (but preset throttle)
2189 - 282
  for (i = 0; i < NUM_OUTPUTS; i++) {
283
    outputMixer[i].flightControls[MIXER_SOURCE_THROTTLE] = i < 4 ? (1<<LOG_MOTOR_MIXER_UNIT) : 0;
284
    outputMixer[i].flightControls[MIXER_SOURCE_PITCH] = 0;
285
    outputMixer[i].flightControls[MIXER_SOURCE_ROLL] = 0;
286
    outputMixer[i].flightControls[MIXER_SOURCE_YAW] = 0;
287
    outputMixer[i].oppositeMotor = (uint8_t)-1;
288
    outputMixer[i].auxSource = (uint8_t)-1;
289
    outputMixer[i].outputType = i < 4 ? OUTPUT_TYPE_MOTOR : OUTPUT_TYPE_UNDEFINED;
290
    outputMixer[i].minValue = i < 4 ? 1   : 128;
291
    outputMixer[i].maxValue = i < 4 ? 255 : 128;
1960 - 292
  }
2160 - 293
  // default = Quadro+
2189 - 294
  outputMixer[0].flightControls[MIXER_SOURCE_PITCH] = -(1<<LOG_MOTOR_MIXER_UNIT);
295
  outputMixer[0].flightControls[MIXER_SOURCE_YAW] = +(1<<LOG_MOTOR_MIXER_UNIT);
296
  outputMixer[0].oppositeMotor = 1;
2020 - 297
 
2189 - 298
  outputMixer[1].flightControls[MIXER_SOURCE_PITCH] = +(1<<LOG_MOTOR_MIXER_UNIT);
299
  outputMixer[1].flightControls[MIXER_SOURCE_YAW] = +(1<<LOG_MOTOR_MIXER_UNIT);
300
  outputMixer[1].oppositeMotor = 0;
301
 
302
  outputMixer[2].flightControls[MIXER_SOURCE_ROLL] = +(1<<LOG_MOTOR_MIXER_UNIT);
303
  outputMixer[2].flightControls[MIXER_SOURCE_YAW] = -(1<<LOG_MOTOR_MIXER_UNIT);
304
  outputMixer[2].oppositeMotor = 3;
2158 - 305
 
2189 - 306
  outputMixer[3].flightControls[MIXER_SOURCE_ROLL] = -(1<<LOG_MOTOR_MIXER_UNIT);
307
  outputMixer[3].flightControls[MIXER_SOURCE_YAW] = -(1<<LOG_MOTOR_MIXER_UNIT);
308
  outputMixer[3].oppositeMotor = 2;
2158 - 309
 
2189 - 310
  outputMixer[8].outputType = OUTPUT_TYPE_SERVO;
311
  outputMixer[8].auxSource = MIXER_SOURCE_AUX_RCCHANNEL;
312
  outputMixer[8].minValue = 40;
313
  outputMixer[8].maxValue = 256-40;
2158 - 314
 
2189 - 315
  outputMixer[9].outputType = OUTPUT_TYPE_SERVO;
316
  outputMixer[9].auxSource = MIXER_SOURCE_AUX_RCCHANNEL+1;
317
  outputMixer[9].minValue = 10;
318
  outputMixer[9].maxValue = 256-10;
319
 
320
  /*
321
  // default = Quadro X
322
  motorMixer.matrix[0][MIX_PITCH] = +(1<<LOG_MOTOR_MIXER_SCALER);
323
  motorMixer.matrix[0][MIX_ROLL] = +(1<<LOG_MOTOR_MIXER_SCALER);
324
  motorMixer.matrix[0][MIX_YAW] = +(1<<LOG_MOTOR_MIXER_SCALER);
2160 - 325
  motorMixer.matrix[0][MIX_OPPOSITE_MOTOR] = 1;
2020 - 326
 
2189 - 327
  motorMixer.matrix[1][MIX_PITCH] = -(1<<LOG_MOTOR_MIXER_SCALER);
328
  motorMixer.matrix[1][MIX_ROLL] = -(1<<LOG_MOTOR_MIXER_SCALER);
329
  motorMixer.matrix[1][MIX_YAW] = +(1<<LOG_MOTOR_MIXER_SCALER);
2160 - 330
  motorMixer.matrix[1][MIX_OPPOSITE_MOTOR] = 0;
2020 - 331
 
2189 - 332
  motorMixer.matrix[2][MIX_PITCH] = +(1<<LOG_MOTOR_MIXER_SCALER);
333
  motorMixer.matrix[2][MIX_ROLL] = -(1<<LOG_MOTOR_MIXER_SCALER);
334
  motorMixer.matrix[2][MIX_YAW] = -(1<<LOG_MOTOR_MIXER_SCALER);
2160 - 335
  motorMixer.matrix[2][MIX_OPPOSITE_MOTOR] = 3;
2020 - 336
 
2189 - 337
  motorMixer.matrix[3][MIX_PITCH] = -(1<<LOG_MOTOR_MIXER_SCALER);
338
  motorMixer.matrix[3][MIX_ROLL] = +(1<<LOG_MOTOR_MIXER_SCALER);
339
  motorMixer.matrix[3][MIX_YAW] = -(1<<LOG_MOTOR_MIXER_SCALER);
2160 - 340
  motorMixer.matrix[3][MIX_OPPOSITE_MOTOR] = 2;
2158 - 341
 
342
  memcpy(motorMixer.name, "Quadro X\0", 9);
2189 - 343
*/
1960 - 344
}
345
 
1968 - 346
/***************************************************/
347
/*    Default Values for R/C Channels              */
348
/***************************************************/
1960 - 349
void channelMap_default(void) {
2164 - 350
  channelMap.RCPolarity = 0;
1968 - 351
  channelMap.channels[CH_PITCH]    = 1;
352
  channelMap.channels[CH_ROLL]     = 0;
353
  channelMap.channels[CH_THROTTLE] = 2;
354
  channelMap.channels[CH_YAW]      = 3;
2189 - 355
  channelMap.channels[CH_VARIABLES + 0] = 4;
356
  channelMap.channels[CH_VARIABLES + 1] = 5;
357
  channelMap.channels[CH_VARIABLES + 2] = 6;
358
  channelMap.channels[CH_VARIABLES + 3] = 7;
1960 - 359
}