Subversion Repositories FlightCtrl

Rev

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