Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
1612 dongfang 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
2035 - 3
// + Nur f�r den privaten Gebrauch
1612 dongfang 4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2035 - 6
// + Es gilt f�r das gesamte Projekt (Hardware, Software, Bin�rfiles, Sourcecode und Dokumentation),
7
// + dass eine Nutzung (auch auszugsweise) nur f�r den privaten und nicht-kommerziellen Gebrauch zul�ssig ist.
1612 dongfang 8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
2035 - 10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Best�ckung und Verkauf von Platinen oder Baus�tzen,
1612 dongfang 11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2035 - 13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder ver�ffentlicht,
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright m�ssen dann beiliegen
1612 dongfang 15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
2035 - 17
// + auf anderen Webseiten oder Medien ver�ffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
1963 - 18
// + eindeutig als Ursprung verlinkt und genannt werden
1612 dongfang 19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2035 - 20
// + Keine Gew�hr auf Fehlerfreiheit, Vollst�ndigkeit oder Funktion
1612 dongfang 21
// + Benutzung auf eigene Gefahr
2035 - 22
// + Wir �bernehmen keinerlei Haftung f�r direkte oder indirekte Personen- oder Sachsch�den
1612 dongfang 23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
2035 - 25
// + mit unserer Zustimmung zul�ssig
1612 dongfang 26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
30
// + this list of conditions and the following disclaimer.
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
32
// +     from this software without specific prior written permission.
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
34
// +     for non-commercial use (directly or indirectly)
1868 - 35
// +     Commercial use (for example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
1612 dongfang 36
// +     with our written permission
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1963 - 47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1612 dongfang 49
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50
// +  POSSIBILITY OF SUCH DAMAGE.
51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
52
#include <util/delay.h>
1775 - 53
#include <stddef.h>
1960 - 54
#include <string.h>
1612 dongfang 55
#include "configuration.h"
1960 - 56
#include "sensors.h"
57
#include "rc.h"
1977 - 58
#include "uart0.h"
59
 
1961 - 60
int16_t variables[VARIABLE_COUNT];
2039 - 61
ParamSet_t staticParams;
1960 - 62
channelMap_t channelMap;
63
mixerMatrix_t mixerMatrix;
1969 - 64
volatile dynamicParam_t dynamicParams;
65
 
1612 dongfang 66
uint8_t CPUType = ATMEGA644;
1960 - 67
uint8_t boardRelease = 13;
68
uint8_t requiredMotors;
1963 - 69
 
2018 - 70
VersionInfo_t versionInfo;
71
 
1963 - 72
// MK flags. TODO: Replace by enum. State machine.
73
uint16_t isFlying = 0;
74
volatile uint8_t MKFlags = 0;
75
 
1612 dongfang 76
/************************************************************************
77
 * Map the parameter to pot values                                    
1775 - 78
 * Replacing this code by the code below saved almost 1 kbyte.
1612 dongfang 79
 ************************************************************************/
1775 - 80
 
1612 dongfang 81
void configuration_applyVariablesToParams(void) {
82
  uint8_t i;
1986 - 83
 
84
  debugOut.analog[20] = variables[0];
85
 
86
#define SET_POT_MM(b,a,min,max) {if (a>=255-VARIABLE_COUNT) b=variables[a+VARIABLE_COUNT-255]; else b=a; if(b<=min) b=min; else if(b>=max) b=max;}
87
#define SET_POT(b,a) {if (a>=255-VARIABLE_COUNT) b=variables[a+VARIABLE_COUNT-255]; else b=a;}
1960 - 88
  SET_POT_MM(dynamicParams.gyroP, staticParams.gyroP, 5, 200);
89
  SET_POT(dynamicParams.gyroI, staticParams.gyroI);
90
  SET_POT(dynamicParams.gyroD, staticParams.gyroD);
91
  SET_POT(dynamicParams.compassYawEffect,staticParams.compassYawEffect);
92
 
93
  SET_POT(dynamicParams.externalControl, staticParams.externalControl);
94
  SET_POT(dynamicParams.dynamicStability,staticParams.dynamicStability);
1980 - 95
  SET_POT(dynamicParams.maxAccVector,staticParams.maxAccVector);
1960 - 96
 
2032 - 97
  SET_POT(dynamicParams.heightP, staticParams.heightP);
98
  SET_POT(dynamicParams.heightI, staticParams.heightI);
99
  SET_POT(dynamicParams.heightD, staticParams.heightD);
1960 - 100
  SET_POT(dynamicParams.heightSetting,staticParams.heightSetting);
2032 - 101
 
1960 - 102
  SET_POT(dynamicParams.attitudeControl,staticParams.attitudeControl);
103
 
104
  SET_POT(dynamicParams.servoManualControl[0], staticParams.servoConfigurations[0].manualControl);
105
  SET_POT(dynamicParams.servoManualControl[1], staticParams.servoConfigurations[1].manualControl);
106
 
107
  SET_POT_MM(dynamicParams.output0Timing, staticParams.outputFlash[0].timing,1,255);
108
  SET_POT_MM(dynamicParams.output1Timing, staticParams.outputFlash[1].timing,1,255);
1988 - 109
 
110
  SET_POT(dynamicParams.levelCorrection[0], staticParams.levelCorrection[0]);
111
  SET_POT(dynamicParams.levelCorrection[1], staticParams.levelCorrection[1]);
2032 - 112
 
2045 - 113
  SET_POT(dynamicParams.naviMode, staticParams.naviMode);
114
 
2032 - 115
  for (i=0; i<sizeof(staticParams.userParams); i++) {
116
    SET_POT(dynamicParams.userParams[i],staticParams.userParams[i]);
117
  }
1612 dongfang 118
}
119
 
1775 - 120
const XLATION XLATIONS[] = {
2039 - 121
  {offsetof(ParamSet_t, heightSetting), offsetof(dynamicParam_t, heightSetting)},
1775 - 122
};
123
 
124
const MMXLATION MMXLATIONS[] = {
2039 - 125
  {offsetof(ParamSet_t, heightD), offsetof(dynamicParam_t, heightD),0,100},
1775 - 126
};
127
 
128
uint8_t configuration_applyVariableToParam(uint8_t src, uint8_t min, uint8_t max) {
129
  uint8_t result;
130
  if (src>=251) result = variables[src-251];
131
  else result = src;
132
  if (result < min) result = min;
133
  else if (result > max) result = max;
134
  return result;
135
}
136
 
137
void configuration_applyVariablesToParams_dead(void) {
138
  uint8_t i, src;
139
  uint8_t* pointerToTgt;
140
  for(i=0; i<sizeof(XLATIONS)/sizeof(XLATION); i++) {
141
    src = *((uint8_t*)(&staticParams + XLATIONS[i].sourceIdx));
142
    pointerToTgt = (uint8_t*)(&dynamicParams + XLATIONS[i].targetIdx);
143
    if (src < 255) {
144
      *pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
145
    }
146
  }
147
 
148
  for(i=0; i<sizeof(MMXLATIONS)/sizeof(MMXLATION); i++) {
149
    src = *((uint8_t*)(&staticParams + MMXLATIONS[i].sourceIdx));
150
    pointerToTgt = (uint8_t*)(&dynamicParams + XLATIONS[i].targetIdx);
151
    if (src < 255) {
152
      *pointerToTgt = configuration_applyVariableToParam(src, MMXLATIONS[i].min, MMXLATIONS[i].max);
153
    }
154
  }
155
 
1960 - 156
  for (i=0; i<sizeof(staticParams.userParams); i++) {
2039 - 157
    src = *((uint8_t*)(&staticParams + offsetof(ParamSet_t, userParams) + i));
1960 - 158
    pointerToTgt = (uint8_t*)(&dynamicParams + offsetof(dynamicParam_t, userParams) + i);
1775 - 159
    if (src < 255) {
160
      *pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
161
    }    
162
  }
163
}
164
 
1612 dongfang 165
uint8_t getCPUType(void) {   // works only after reset or power on when the registers have default values
166
  uint8_t CPUType = ATMEGA644;
2017 - 167
  if((UCSR1A == 0x20) && (UCSR1C == 0x06)) CPUType = ATMEGA644P;  // initial Values for 644P after reset
1612 dongfang 168
  return CPUType;
169
}
170
 
171
/*
172
 * Automatic detection of hardware components is not supported in this development-oriented
173
 * FC firmware. It would go against the point of it: To enable alternative hardware
174
 * configurations with otherwise unsupported components. Instead, one should write
175
 * custom code + adjust constants for the new hardware, and include the relevant code
176
 * from the makefile.
177
 * However - we still do detect the board release. Reason: Otherwise it would be too
178
 * tedious to have to modify the code for how to turn on and off LEDs when deploying
179
 * on different HW version....
180
 */
181
 
182
uint8_t getBoardRelease(void) {
1960 - 183
  uint8_t boardRelease = 13;
1612 dongfang 184
  // the board release is coded via the pull up or down the 2 status LED
185
 
186
  PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate
187
  DDRB  &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input
188
 
189
  _delay_loop_2(1000); // make some delay
190
 
191
  switch( PINB & ((1<<PINB1)|(1<<PINB0))) {
192
    case 0x00:
1960 - 193
      boardRelease = 10; // 1.0
1612 dongfang 194
      break;
195
    case 0x01:
1960 - 196
      boardRelease = 11; // 1.1 or 1.2
1612 dongfang 197
      break;
198
    case 0x02:
1960 - 199
      boardRelease = 20; // 2.0
1612 dongfang 200
      break;
201
    case 0x03:
1960 - 202
      boardRelease = 13; // 1.3
1612 dongfang 203
      break;
204
    default:
205
      break;
206
    }
207
  // set LED ports as output
208
  DDRB |= (1<<DDB1)|(1<<DDB0);
1964 - 209
  RED_OFF;
1612 dongfang 210
  GRN_OFF;
1960 - 211
  return boardRelease;
1612 dongfang 212
}
1960 - 213
 
214
void setOtherDefaults(void) {
215
  // Height Control
2035 - 216
  staticParams.airpressureFilterConstant = 8;
2036 - 217
  //staticParams.airpressureWindowLength = 0;
2026 - 218
  staticParams.airpressureAccZCorrection = 128+56;
1960 - 219
  staticParams.heightP = 10;
220
  staticParams.heightD = 30;
221
  staticParams.heightSetting = 251;
1961 - 222
  staticParams.heightControlMaxThrottleChange = 10;
1960 - 223
  staticParams.heightSlewRate = 4;
224
 
225
  // Control
226
  staticParams.stickP = 8;
227
  staticParams.stickD = 12;
228
  staticParams.stickYawP = 12;
229
  staticParams.stickThrottleD = 12;
230
  staticParams.minThrottle = 8;
231
  staticParams.maxThrottle = 230;
232
  staticParams.externalControl = 0;
1988 - 233
  staticParams.motorSmoothing = 0;
1960 - 234
 
235
  // IMU
236
  staticParams.gyroPIDFilterConstant = 1;
2036 - 237
  staticParams.gyroATTFilterConstant = 1;
1960 - 238
  staticParams.gyroDFilterConstant = 1;
1977 - 239
  staticParams.accFilterConstant = 10;
1960 - 240
 
241
  staticParams.gyroP = 60;
242
  staticParams.gyroI = 80;
243
  staticParams.gyroD = 4;
244
 
1961 - 245
  // set by gyro-specific code: gyro_setDefaults().
1960 - 246
  // staticParams.zerothOrderCorrection = 
247
  // staticParams.driftCompDivider = 
248
  // staticParams.driftCompLimit = 
249
 
250
  staticParams.axisCoupling1 = 90;
251
  staticParams.axisCoupling2 = 67;
252
  staticParams.axisCouplingYawCorrection = 0;
253
  staticParams.dynamicStability = 50;
1986 - 254
  staticParams.maxAccVector = 10;
1977 - 255
  staticParams.IFactor = 32;
256
  staticParams.yawIFactor = 100;  
1960 - 257
  staticParams.compassYawEffect = 128;
1988 - 258
  staticParams.levelCorrection[0] = staticParams.levelCorrection[1] = 128;
1960 - 259
 
260
  // Servos
1980 - 261
  staticParams.servoCount = 7;
262
  staticParams.servoManualMaxSpeed = 10;
1960 - 263
  for (uint8_t i=0; i<2; i++) {
264
    staticParams.servoConfigurations[i].manualControl = 128;
1980 - 265
    staticParams.servoConfigurations[i].stabilizationFactor = 0;
1977 - 266
    staticParams.servoConfigurations[i].minValue = 32;
267
    staticParams.servoConfigurations[i].maxValue = 224;
1960 - 268
    staticParams.servoConfigurations[i].flags = 0;
269
  }
1980 - 270
 
1960 - 271
  // Battery warning and emergency flight
272
  staticParams.batteryVoltageWarning = 101;  // 3.7 each for 3S
273
  staticParams.emergencyThrottle = 35;
274
  staticParams.emergencyFlightDuration = 30;
275
 
276
  // Outputs
1961 - 277
  staticParams.outputFlash[0].bitmask = 1; //0b01011111;
1960 - 278
  staticParams.outputFlash[0].timing = 15;
1961 - 279
  staticParams.outputFlash[1].bitmask = 3; //0b11110011;
1960 - 280
  staticParams.outputFlash[1].timing = 15;
281
 
1986 - 282
  staticParams.outputDebugMask = 8;
283
  staticParams.outputFlags   = 16|8|4;
1960 - 284
}
285
 
286
/***************************************************/
287
/*    Default Values for parameter set 1           */
288
/***************************************************/
289
void paramSet_default(uint8_t setnumber) {
290
  setOtherDefaults();
1971 - 291
  gyro_setDefaultParameters();
1960 - 292
 
293
  for (uint8_t i=0; i<8; i++) {
294
    staticParams.userParams[i] = 0;
295
  }
296
 
297
  staticParams.bitConfig =
2044 - 298
    CFG_GYRO_SATURATION_PREVENTION | CFG_HEADING_HOLD | CFG_COMPASS_ACTIVE;
1960 - 299
 
300
  memcpy(staticParams.name, "Default\0", 6);
301
}
302
 
303
/***************************************************/
304
/*    Default Values for Mixer Table               */
305
/***************************************************/
306
void mixerMatrix_default(void) { // Quadro 
307
  uint8_t i;
308
  // mixerMatric.revision = EEMIXER_REVISION;
309
  // clear mixer table (but preset throttle)
310
  for (i = 0; i < 16; i++) {
311
    mixerMatrix.motor[i][MIX_THROTTLE] = i < 4 ? 64 : 0;
312
    mixerMatrix.motor[i][MIX_PITCH] = 0;
313
    mixerMatrix.motor[i][MIX_ROLL] = 0;
314
    mixerMatrix.motor[i][MIX_YAW] = 0;
315
  }
316
  // default = Quadro
317
  mixerMatrix.motor[0][MIX_PITCH] = +64;
318
  mixerMatrix.motor[0][MIX_YAW] = +64;
319
  mixerMatrix.motor[1][MIX_PITCH] = -64;
320
  mixerMatrix.motor[1][MIX_YAW] = +64;
321
  mixerMatrix.motor[2][MIX_ROLL] = -64;
322
  mixerMatrix.motor[2][MIX_YAW] = -64;
323
  mixerMatrix.motor[3][MIX_ROLL] = +64;
324
  mixerMatrix.motor[3][MIX_YAW] = -64;
325
  memcpy(mixerMatrix.name, "Quadro\0", 7);
2020 - 326
 
2026 - 327
  /*
2020 - 328
  // default = X
329
  mixerMatrix.motor[0][MIX_PITCH] = +45;
330
  mixerMatrix.motor[0][MIX_ROLL]  = +45;
331
  mixerMatrix.motor[0][MIX_YAW]   = +64;
332
 
333
  mixerMatrix.motor[1][MIX_PITCH] = -45;
334
  mixerMatrix.motor[1][MIX_ROLL]  = -45;
335
  mixerMatrix.motor[1][MIX_YAW]   = +64;
336
 
337
  mixerMatrix.motor[2][MIX_PITCH] = +45;
338
  mixerMatrix.motor[2][MIX_ROLL]  = -45;
339
  mixerMatrix.motor[2][MIX_YAW]   = -64;
340
 
341
  mixerMatrix.motor[3][MIX_PITCH] = -45;
342
  mixerMatrix.motor[3][MIX_ROLL]  = +45;
343
  mixerMatrix.motor[3][MIX_YAW]   = -64;
2026 - 344
  */
2020 - 345
 
346
  memcpy(mixerMatrix.name, "X\0", 7);
1960 - 347
}
348
 
1968 - 349
/***************************************************/
350
/*    Default Values for R/C Channels              */
351
/***************************************************/
1960 - 352
void channelMap_default(void) {
1968 - 353
  channelMap.channels[CH_PITCH]    = 1;
354
  channelMap.channels[CH_ROLL]     = 0;
355
  channelMap.channels[CH_THROTTLE] = 2;
356
  channelMap.channels[CH_YAW]      = 3;
357
  channelMap.channels[CH_POTS + 0] = 4;
358
  channelMap.channels[CH_POTS + 1] = 5;
359
  channelMap.channels[CH_POTS + 2] = 6;
360
  channelMap.channels[CH_POTS + 3] = 7;
1960 - 361
}