Subversion Repositories FlightCtrl

Rev

Rev 1980 | Rev 1988 | 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
3
// + Nur für den privaten Gebrauch
4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
1963 - 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.
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
1963 - 17
// + auf anderen Webseiten oder Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + eindeutig als Ursprung verlinkt und genannt werden
1612 dongfang 19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
21
// + Benutzung auf eigene Gefahr
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
25
// + mit unserer Zustimmung zulässig
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];
1960 - 61
paramset_t staticParams;
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
 
70
// MK flags. TODO: Replace by enum. State machine.
71
uint16_t isFlying = 0;
72
volatile uint8_t MKFlags = 0;
73
 
1612 dongfang 74
/************************************************************************
75
 * Map the parameter to pot values                                    
1775 - 76
 * Replacing this code by the code below saved almost 1 kbyte.
1612 dongfang 77
 ************************************************************************/
1775 - 78
 
1612 dongfang 79
void configuration_applyVariablesToParams(void) {
80
  uint8_t i;
1986 - 81
 
82
  debugOut.analog[20] = variables[0];
83
 
84
#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;}
85
#define SET_POT(b,a) {if (a>=255-VARIABLE_COUNT) b=variables[a+VARIABLE_COUNT-255]; else b=a;}
1960 - 86
  SET_POT_MM(dynamicParams.gyroP, staticParams.gyroP, 5, 200);
87
  SET_POT(dynamicParams.gyroI, staticParams.gyroI);
88
  SET_POT(dynamicParams.gyroD, staticParams.gyroD);
89
  SET_POT(dynamicParams.IFactor, staticParams.IFactor);
90
  SET_POT(dynamicParams.yawIFactor, staticParams.yawIFactor);
91
  SET_POT(dynamicParams.compassYawEffect,staticParams.compassYawEffect);
92
 
93
  SET_POT(dynamicParams.externalControl, staticParams.externalControl);
94
  SET_POT(dynamicParams.axisCoupling1, staticParams.axisCoupling1);
95
  SET_POT(dynamicParams.axisCoupling2, staticParams.axisCoupling2);
96
  SET_POT(dynamicParams.axisCouplingYawCorrection, staticParams.axisCouplingYawCorrection);
97
  SET_POT(dynamicParams.dynamicStability,staticParams.dynamicStability);
1980 - 98
  SET_POT(dynamicParams.maxAccVector,staticParams.maxAccVector);
1960 - 99
 
100
  SET_POT_MM(dynamicParams.heightP, staticParams.heightP,0,100);
101
  SET_POT_MM(dynamicParams.heightD, staticParams.heightD,0,100);
102
  SET_POT(dynamicParams.heightSetting,staticParams.heightSetting);
103
  SET_POT(dynamicParams.attitudeControl,staticParams.attitudeControl);
104
 
105
  for (i=0; i<sizeof(staticParams.userParams); i++) {
106
    SET_POT(dynamicParams.userParams[i],staticParams.userParams[i]);
1612 dongfang 107
  }
1805 - 108
 
1960 - 109
  SET_POT(dynamicParams.servoManualControl[0], staticParams.servoConfigurations[0].manualControl);
110
  SET_POT(dynamicParams.servoManualControl[1], staticParams.servoConfigurations[1].manualControl);
111
 
112
  SET_POT_MM(dynamicParams.output0Timing, staticParams.outputFlash[0].timing,1,255);
113
  SET_POT_MM(dynamicParams.output1Timing, staticParams.outputFlash[1].timing,1,255);
1612 dongfang 114
}
115
 
1775 - 116
const XLATION XLATIONS[] = {
1961 - 117
  {offsetof(paramset_t, heightSetting), offsetof(dynamicParam_t, heightSetting)},
1775 - 118
};
119
 
120
const MMXLATION MMXLATIONS[] = {
1961 - 121
  {offsetof(paramset_t, heightD), offsetof(dynamicParam_t, heightD),0,100},
1775 - 122
};
123
 
124
uint8_t configuration_applyVariableToParam(uint8_t src, uint8_t min, uint8_t max) {
125
  uint8_t result;
126
  if (src>=251) result = variables[src-251];
127
  else result = src;
128
  if (result < min) result = min;
129
  else if (result > max) result = max;
130
  return result;
131
}
132
 
133
void configuration_applyVariablesToParams_dead(void) {
134
  uint8_t i, src;
135
  uint8_t* pointerToTgt;
136
  for(i=0; i<sizeof(XLATIONS)/sizeof(XLATION); i++) {
137
    src = *((uint8_t*)(&staticParams + XLATIONS[i].sourceIdx));
138
    pointerToTgt = (uint8_t*)(&dynamicParams + XLATIONS[i].targetIdx);
139
    if (src < 255) {
140
      *pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
141
    }
142
  }
143
 
144
  for(i=0; i<sizeof(MMXLATIONS)/sizeof(MMXLATION); i++) {
145
    src = *((uint8_t*)(&staticParams + MMXLATIONS[i].sourceIdx));
146
    pointerToTgt = (uint8_t*)(&dynamicParams + XLATIONS[i].targetIdx);
147
    if (src < 255) {
148
      *pointerToTgt = configuration_applyVariableToParam(src, MMXLATIONS[i].min, MMXLATIONS[i].max);
149
    }
150
  }
151
 
1960 - 152
  for (i=0; i<sizeof(staticParams.userParams); i++) {
153
    src = *((uint8_t*)(&staticParams + offsetof(paramset_t, userParams) + i));
154
    pointerToTgt = (uint8_t*)(&dynamicParams + offsetof(dynamicParam_t, userParams) + i);
1775 - 155
    if (src < 255) {
156
      *pointerToTgt = configuration_applyVariableToParam(src, 0, 255);
157
    }    
158
  }
159
}
160
 
1612 dongfang 161
uint8_t getCPUType(void) {   // works only after reset or power on when the registers have default values
162
  uint8_t CPUType = ATMEGA644;
1961 - 163
  //if((UCSR1A == 0x20) && (UCSR1C == 0x06)) CPUType = ATMEGA644P;  // initial Values for 644P after reset
1612 dongfang 164
  return CPUType;
165
}
166
 
167
/*
168
 * Automatic detection of hardware components is not supported in this development-oriented
169
 * FC firmware. It would go against the point of it: To enable alternative hardware
170
 * configurations with otherwise unsupported components. Instead, one should write
171
 * custom code + adjust constants for the new hardware, and include the relevant code
172
 * from the makefile.
173
 * However - we still do detect the board release. Reason: Otherwise it would be too
174
 * tedious to have to modify the code for how to turn on and off LEDs when deploying
175
 * on different HW version....
176
 */
177
 
178
uint8_t getBoardRelease(void) {
1960 - 179
  uint8_t boardRelease = 13;
1612 dongfang 180
  // the board release is coded via the pull up or down the 2 status LED
181
 
182
  PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate
183
  DDRB  &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input
184
 
185
  _delay_loop_2(1000); // make some delay
186
 
187
  switch( PINB & ((1<<PINB1)|(1<<PINB0))) {
188
    case 0x00:
1960 - 189
      boardRelease = 10; // 1.0
1612 dongfang 190
      break;
191
    case 0x01:
1960 - 192
      boardRelease = 11; // 1.1 or 1.2
1612 dongfang 193
      break;
194
    case 0x02:
1960 - 195
      boardRelease = 20; // 2.0
1612 dongfang 196
      break;
197
    case 0x03:
1960 - 198
      boardRelease = 13; // 1.3
1612 dongfang 199
      break;
200
    default:
201
      break;
202
    }
203
  // set LED ports as output
204
  DDRB |= (1<<DDB1)|(1<<DDB0);
1964 - 205
  RED_OFF;
1612 dongfang 206
  GRN_OFF;
1960 - 207
  return boardRelease;
1612 dongfang 208
}
1960 - 209
 
210
void setOtherDefaults(void) {
211
  // Height Control
212
  staticParams.heightP = 10;
213
  staticParams.heightD = 30;
214
  staticParams.heightSetting = 251;
1961 - 215
  staticParams.heightControlMaxThrottleChange = 10;
1960 - 216
  staticParams.heightSlewRate = 4;
217
 
218
  // Control
219
  staticParams.stickP = 8;
220
  staticParams.stickD = 12;
221
  staticParams.stickYawP = 12;
222
  staticParams.stickThrottleD = 12;
223
  staticParams.minThrottle = 8;
224
  staticParams.maxThrottle = 230;
225
  staticParams.externalControl = 0;
226
 
227
  // IMU
228
  staticParams.gyroPIDFilterConstant = 1;
229
  staticParams.gyroATTFilterConstant = 1;
230
  staticParams.gyroDFilterConstant = 1;
1977 - 231
  staticParams.accFilterConstant = 10;
1960 - 232
 
233
  staticParams.gyroP = 60;
234
  staticParams.gyroI = 80;
235
  staticParams.gyroD = 4;
236
 
1961 - 237
  // set by gyro-specific code: gyro_setDefaults().
1960 - 238
  // staticParams.zerothOrderCorrection = 
239
  // staticParams.driftCompDivider = 
240
  // staticParams.driftCompLimit = 
241
 
242
  staticParams.axisCoupling1 = 90;
243
  staticParams.axisCoupling2 = 67;
244
  staticParams.axisCouplingYawCorrection = 0;
245
  staticParams.dynamicStability = 50;
1986 - 246
  staticParams.maxAccVector = 10;
1977 - 247
  staticParams.IFactor = 32;
248
  staticParams.yawIFactor = 100;  
1960 - 249
  staticParams.compassYawEffect = 128;
250
 
251
  // Servos
1980 - 252
  staticParams.servoCount = 7;
253
  staticParams.servoManualMaxSpeed = 10;
1960 - 254
  for (uint8_t i=0; i<2; i++) {
255
    staticParams.servoConfigurations[i].manualControl = 128;
1980 - 256
    staticParams.servoConfigurations[i].stabilizationFactor = 0;
1977 - 257
    staticParams.servoConfigurations[i].minValue = 32;
258
    staticParams.servoConfigurations[i].maxValue = 224;
1960 - 259
    staticParams.servoConfigurations[i].flags = 0;
260
  }
1980 - 261
 
1960 - 262
  // Battery warning and emergency flight
263
  staticParams.batteryVoltageWarning = 101;  // 3.7 each for 3S
264
  staticParams.emergencyThrottle = 35;
265
  staticParams.emergencyFlightDuration = 30;
266
 
267
  // Outputs
1961 - 268
  staticParams.outputFlash[0].bitmask = 1; //0b01011111;
1960 - 269
  staticParams.outputFlash[0].timing = 15;
1961 - 270
  staticParams.outputFlash[1].bitmask = 3; //0b11110011;
1960 - 271
  staticParams.outputFlash[1].timing = 15;
272
 
1986 - 273
  staticParams.outputDebugMask = 8;
274
  staticParams.outputFlags   = 16|8|4;
1960 - 275
}
276
 
277
/***************************************************/
278
/*    Default Values for parameter set 1           */
279
/***************************************************/
280
void paramSet_default(uint8_t setnumber) {
281
  setOtherDefaults();
1971 - 282
  gyro_setDefaultParameters();
1960 - 283
 
284
  for (uint8_t i=0; i<8; i++) {
285
    staticParams.userParams[i] = 0;
286
  }
287
 
288
  staticParams.bitConfig =
1961 - 289
    CFG_GYRO_SATURATION_PREVENTION | CFG_HEADING_HOLD;
1960 - 290
 
291
  memcpy(staticParams.name, "Default\0", 6);
292
}
293
 
294
/***************************************************/
295
/*    Default Values for Mixer Table               */
296
/***************************************************/
297
void mixerMatrix_default(void) { // Quadro 
298
  uint8_t i;
299
  // mixerMatric.revision = EEMIXER_REVISION;
300
  // clear mixer table (but preset throttle)
301
  for (i = 0; i < 16; i++) {
302
    mixerMatrix.motor[i][MIX_THROTTLE] = i < 4 ? 64 : 0;
303
    mixerMatrix.motor[i][MIX_PITCH] = 0;
304
    mixerMatrix.motor[i][MIX_ROLL] = 0;
305
    mixerMatrix.motor[i][MIX_YAW] = 0;
306
  }
307
  // default = Quadro
308
  mixerMatrix.motor[0][MIX_PITCH] = +64;
309
  mixerMatrix.motor[0][MIX_YAW] = +64;
310
  mixerMatrix.motor[1][MIX_PITCH] = -64;
311
  mixerMatrix.motor[1][MIX_YAW] = +64;
312
  mixerMatrix.motor[2][MIX_ROLL] = -64;
313
  mixerMatrix.motor[2][MIX_YAW] = -64;
314
  mixerMatrix.motor[3][MIX_ROLL] = +64;
315
  mixerMatrix.motor[3][MIX_YAW] = -64;
316
  memcpy(mixerMatrix.name, "Quadro\0", 7);
317
}
318
 
1968 - 319
/***************************************************/
320
/*    Default Values for R/C Channels              */
321
/***************************************************/
1960 - 322
void channelMap_default(void) {
1968 - 323
  channelMap.channels[CH_PITCH]    = 1;
324
  channelMap.channels[CH_ROLL]     = 0;
325
  channelMap.channels[CH_THROTTLE] = 2;
326
  channelMap.channels[CH_YAW]      = 3;
327
  channelMap.channels[CH_POTS + 0] = 4;
328
  channelMap.channels[CH_POTS + 1] = 5;
329
  channelMap.channels[CH_POTS + 2] = 6;
330
  channelMap.channels[CH_POTS + 3] = 7;
1960 - 331
}