Subversion Repositories FlightCtrl

Rev

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