Subversion Repositories FlightCtrl

Rev

Rev 1635 | Rev 1821 | 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
1623 - 3
// + Nur für den privaten Gebrauch
1612 dongfang 4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1623 - 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.
1623 - 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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1623 - 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
1623 - 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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1623 - 20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
1612 dongfang 21
// + Benutzung auf eigene Gefahr
1623 - 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
1623 - 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)
35
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
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
1623 - 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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1623 - 52
 
1612 dongfang 53
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
54
// + Contant Values
55
// + 0-250 -> normale Values
56
// + 251 -> Poti1
57
// + 252 -> Poti2
58
// + 253 -> Poti3
59
// + 254 -> Poti4
60
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
61
 
62
#ifndef EEMEM
63
#define EEMEM __attribute__ ((section (".eeprom")))
64
#endif
65
 
66
#include <avr/eeprom.h>
67
#include <string.h>
68
#include "eeprom.h"
69
#include "printf_P.h"
70
#include "output.h"
71
// TODO: Get rid of these. They have nothing to do with eeprom.
72
#include "flight.h"
73
#include "rc.h"
74
#include "sensors.h"
75
 
76
// byte array in eeprom
77
uint8_t EEPromArray[E2END+1] EEMEM;
78
 
79
paramset_t      staticParams;
80
MixerTable_t    Mixer;
81
 
82
/*
83
 * Default for your own experiments here, so you don't have to reset them
84
 * from MK-Tool all the time.
85
 */
86
void setDefaultUserParams(void) {
87
  uint8_t i;
88
  for (i=0; i<sizeof(staticParams.UserParams1); i++) {
89
    staticParams.UserParams1[i] = 0;
90
  }
91
  for (i=0; i<sizeof(staticParams.UserParams2); i++) {
92
    staticParams.UserParams2[i] = 0;
93
  }
1635 - 94
  /*
95
   * While we are still using userparams for flight parameters, do set
96
   * some safe & meaningful default values.
97
   */
1646 - 98
  staticParams.UserParams1[3] = 10;                     // Throttle stick D=10
99
  staticParams.UserParams2[0] = 0b11010101; // All gyro filter constants 2; acc. 4
100
  staticParams.UserParams2[1] = 2;                      // H&I motor smoothing.
101
  staticParams.UserParams2[2] = 120;            // Yaw I factor
102
  staticParams.UserParams2[3] = 100;            // Max Z acceleration for acc. correction of angles.                    
1612 dongfang 103
}
104
 
105
void setOtherDefaults(void) {
1614 dongfang 106
/* Channel assignments were changed to the normal:
107
 * Aileron/roll=1, elevator/pitch=2, throttle=3, yaw/rudder=4
108
 */
109
  staticParams.ChannelAssignment[CH_PITCH]    = 2;
110
  staticParams.ChannelAssignment[CH_ROLL]     = 1;
111
  staticParams.ChannelAssignment[CH_THROTTLE] = 3;
112
  staticParams.ChannelAssignment[CH_YAW]      = 4;
113
  staticParams.ChannelAssignment[CH_POTS+0]   = 5;
114
  staticParams.ChannelAssignment[CH_POTS+1]   = 6;
115
  staticParams.ChannelAssignment[CH_POTS+2]   = 7;
116
  staticParams.ChannelAssignment[CH_POTS+3]   = 8;
1612 dongfang 117
  staticParams.GlobalConfig = CFG_AXIS_COUPLING_ACTIVE | CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE;//CFG_HEIGHT_CONTROL | CFG_HEIGHT_SWITCH | CFG_COMPASS_FIX;
118
  staticParams.HeightMinGas = 30;
119
  staticParams.MaxHeight     = 251;
120
  staticParams.HeightP      = 10;
121
  staticParams.HeightD  = 30;
122
  staticParams.Height_ACC_Effect = 30;
123
  staticParams.Height_Gain = 4;
124
  staticParams.StickP = 12;
125
  staticParams.StickD = 16;
126
  staticParams.StickYawP = 12;
1615 dongfang 127
  staticParams.MinThrottle = 8;
128
  staticParams.MaxThrottle = 230;
1612 dongfang 129
  staticParams.CompassYawEffect = 128;
130
  staticParams.GyroP = 80;
131
  staticParams.GyroI = 100;
132
  staticParams.LowVoltageWarning = 94;
133
  staticParams.EmergencyGas = 35;
134
  staticParams.EmergencyGasDuration = 30;
1646 - 135
  staticParams.Unused0 = 0;
1612 dongfang 136
  staticParams.IFactor = 32;
137
  staticParams.ServoPitchControl = 100;
138
  staticParams.ServoPitchComp = 40;
139
  staticParams.ServoPitchCompInvert = 0;
140
  staticParams.ServoPitchMin = 50;
141
  staticParams.ServoPitchMax = 150;
142
  staticParams.ServoRefresh = 5;
143
  staticParams.LoopGasLimit = 50;
144
  staticParams.LoopThreshold = 90;
145
  staticParams.LoopHysteresis = 50;
146
  staticParams.BitConfig = 0;
147
  staticParams.AxisCoupling1 = 90;
148
  staticParams.AxisCoupling2 = 67;
149
  staticParams.AxisCouplingYawCorrection = 0;
150
  staticParams.DynamicStability = 50;
151
  staticParams.J16Bitmask = 95;
152
  staticParams.J17Bitmask = 243;
153
  staticParams.J16Timing = 15;
154
  staticParams.J17Timing = 15;
155
  staticParams.NaviGpsModeControl = 253;
156
  staticParams.NaviGpsGain = 100;
157
  staticParams.NaviGpsP = 90;
158
  staticParams.NaviGpsI = 90;
159
  staticParams.NaviGpsD = 90;
160
  staticParams.NaviGpsPLimit = 75;
161
  staticParams.NaviGpsILimit = 75;
162
  staticParams.NaviGpsDLimit = 75;
163
  staticParams.NaviGpsACC = 0;
164
  staticParams.NaviGpsMinSat = 6;
165
  staticParams.NaviStickThreshold = 8;
166
  staticParams.NaviWindCorrection = 90;
167
  staticParams.NaviSpeedCompensation = 30;
168
  staticParams.NaviOperatingRadius = 100;
169
  staticParams.NaviAngleLimitation = 100;
170
  staticParams.NaviPHLoginTime = 4;
171
}
172
 
173
/***************************************************/
174
/*    Default Values for parameter set 1           */
175
/***************************************************/
176
void ParamSet_DefaultSet1(void) { // sport
1646 - 177
  setOtherDefaults();
1612 dongfang 178
  gyro_setDefaults();
179
  setDefaultUserParams();
180
  memcpy(staticParams.Name, "Sport\0",6);
181
}
182
 
183
/***************************************************/
184
/*    Default Values for parameter set 2           */
185
/***************************************************/
186
void ParamSet_DefaultSet2(void) { // normal
1646 - 187
  setOtherDefaults();
1612 dongfang 188
  gyro_setDefaults();
189
  setDefaultUserParams();
190
  staticParams.Height_Gain = 3;
191
  staticParams.J16Timing = 20;
192
  staticParams.J17Timing = 20;
193
  memcpy(staticParams.Name, "Normal\0", 7);
194
}
195
 
196
/***************************************************/
197
/*    Default Values for parameter set 3           */
198
/***************************************************/
199
void ParamSet_DefaultSet3(void) { // beginner
1646 - 200
  setOtherDefaults();
1612 dongfang 201
  gyro_setDefaults();
202
  setDefaultUserParams();
203
  staticParams.GlobalConfig = CFG_ROTARY_RATE_LIMITER | CFG_AXIS_COUPLING_ACTIVE | CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE;//CFG_HEIGHT_CONTROL | CFG_HEIGHT_SWITCH | CFG_COMPASS_FIX;
204
  staticParams.Height_Gain = 3;
205
  staticParams.EmergencyGasDuration = 20;
206
  staticParams.AxisCouplingYawCorrection = 70;
207
  staticParams.J16Timing = 30;
208
  staticParams.J17Timing = 30;
209
  memcpy(staticParams.Name, "Beginner\0", 9);
210
}
211
 
212
/***************************************************/
213
/*       Read Parameter from EEPROM as byte        */
214
/***************************************************/
215
uint8_t GetParamByte(uint16_t param_id) {
216
  return eeprom_read_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id]);
217
}
218
 
219
/***************************************************/
220
/*       Write Parameter to EEPROM as byte         */
221
/***************************************************/
222
void SetParamByte(uint16_t param_id, uint8_t value) {
223
  eeprom_write_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
224
}
225
 
226
/***************************************************/
227
/*       Read Parameter from EEPROM as word        */
228
/***************************************************/
229
uint16_t GetParamWord(uint16_t param_id) {
230
  return eeprom_read_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id]);
231
}
232
 
233
/***************************************************/
234
/*       Write Parameter to EEPROM as word         */
235
/***************************************************/
236
void SetParamWord(uint16_t param_id, uint16_t value) {
237
  eeprom_write_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
238
}
239
 
240
/***************************************************/
241
/*       Read Parameter Set from EEPROM            */
242
/***************************************************/
243
// number [1..5]
244
void ParamSet_ReadFromEEProm(uint8_t setnumber) {
245
  if((1 > setnumber) || (setnumber > 5)) setnumber = 3;
246
  eeprom_read_block((uint8_t *) &staticParams.ChannelAssignment[0], &EEPromArray[EEPROM_ADR_PARAMSET_BEGIN + PARAMSET_STRUCT_LEN * (setnumber - 1)], PARAMSET_STRUCT_LEN);
247
  output_init();
248
}
249
 
250
/***************************************************/
251
/*        Write Parameter Set to EEPROM            */
252
/***************************************************/
253
// number [1..5]
254
void ParamSet_WriteToEEProm(uint8_t setnumber) {
255
  if(setnumber > 5) setnumber = 5;
256
  if(setnumber < 1) return;
257
  eeprom_write_block((uint8_t *) &staticParams.ChannelAssignment[0], &EEPromArray[EEPROM_ADR_PARAMSET_BEGIN + PARAMSET_STRUCT_LEN * (setnumber - 1)], PARAMSET_STRUCT_LEN);
258
  eeprom_write_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAMSET_LENGTH], PARAMSET_STRUCT_LEN);
259
  eeprom_write_block( &staticParams.ChannelAssignment[0], &EEPromArray[EEPROM_ADR_CHANNELS], 8); // backup the first 8 bytes that is the rc channel mapping
260
  // set this parameter set to active set
261
  setActiveParamSet(setnumber);
262
  output_init();
263
}
264
 
265
 
266
/***************************************************/
267
/*       Get active parameter set                  */
268
/***************************************************/
269
uint8_t getActiveParamSet(void) {
270
  uint8_t setnumber;
271
  setnumber = eeprom_read_byte(&EEPromArray[PID_ACTIVE_SET]);
272
  if(setnumber > 5) {
273
    setnumber = 3;
274
    eeprom_write_byte(&EEPromArray[PID_ACTIVE_SET], setnumber);
275
  }
276
  return(setnumber);
277
}
278
 
279
/***************************************************/
280
/*       Set active parameter set                  */
281
/***************************************************/
282
void setActiveParamSet(uint8_t setnumber) {
283
  if(setnumber > 5) setnumber = 5;
284
  if(setnumber < 1) setnumber = 1;
285
  eeprom_write_byte(&EEPromArray[PID_ACTIVE_SET], setnumber);
286
}
287
 
288
/***************************************************/
289
/*          Read MixerTable from EEPROM            */
290
/***************************************************/
291
uint8_t MixerTable_ReadFromEEProm(void) {
292
  if(eeprom_read_byte(&EEPromArray[EEPROM_ADR_MIXER_TABLE]) == EEMIXER_REVISION) {
293
    eeprom_read_block((uint8_t *) &Mixer, &EEPromArray[EEPROM_ADR_MIXER_TABLE], sizeof(Mixer));
294
    return 1;
295
  }
296
  else return 0;
297
}
298
 
299
/***************************************************/
300
/*          Write Mixer Table to EEPROM            */
301
/***************************************************/
302
uint8_t MixerTable_WriteToEEProm(void) {
303
  if(Mixer.Revision == EEMIXER_REVISION) {
304
    eeprom_write_block((uint8_t *) &Mixer, &EEPromArray[EEPROM_ADR_MIXER_TABLE], sizeof(Mixer));
305
    return 1;
306
  }
307
  else return 0;
308
}
309
 
310
/***************************************************/
311
/*    Default Values for Mixer Table               */
312
/***************************************************/
313
void MixerTable_Default(void) { // Quadro 
314
  uint8_t i;
315
  Mixer.Revision = EEMIXER_REVISION;
316
  // clear mixer table (but preset throttle)
317
  for(i = 0; i < 16; i++) {
318
    Mixer.Motor[i][MIX_THROTTLE] = i < 4 ? 64 : 0;
319
    Mixer.Motor[i][MIX_PITCH] = 0;
320
    Mixer.Motor[i][MIX_ROLL] = 0;
321
    Mixer.Motor[i][MIX_YAW]  = 0;
322
  }
323
  // default = Quadro
324
  Mixer.Motor[0][MIX_PITCH] = +64;                                 Mixer.Motor[0][MIX_YAW] = +64;
325
  Mixer.Motor[1][MIX_PITCH] = -64;                                 Mixer.Motor[1][MIX_YAW] = +64;
326
  Mixer.Motor[2][MIX_ROLL] = -64; Mixer.Motor[2][MIX_YAW] = -64;
327
  Mixer.Motor[3][MIX_ROLL] = +64; Mixer.Motor[3][MIX_YAW] = -64;
328
  memcpy(Mixer.Name, "Quadro\0", 7);
329
}
330
 
331
/***************************************************/
332
/*       Initialize EEPROM Parameter Sets          */
333
/***************************************************/
334
void ParamSet_Init(void) {
335
  uint8_t Channel_Backup  = 0, i;
336
  // parameter version  check
337
  if(eeprom_read_byte(&EEPromArray[PID_PARAM_REVISION]) != EEPARAM_REVISION) {
338
    // if version check faild
339
    printf("\n\rInit Parameter in EEPROM");
340
    eeprom_write_byte(&EEPromArray[EEPROM_ADR_MIXER_TABLE], 0xFF); // reset also mixer table
341
    // check if channel mapping backup is valid
342
    if(         (eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+0]) < 12)
343
                &&  (eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+1]) < 12)
344
                &&  (eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+2]) < 12)
345
                &&  (eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+3]) < 12)
346
                )
347
      {
348
        Channel_Backup = 1;
349
      }
350
    // fill all 5 parameter settings
351
    for (i=1; i<6; i++) {
352
      switch(i) {
353
      case 1:
354
        ParamSet_DefaultSet1(); // Fill staticParams Structure to default parameter set 1 (Sport)
355
        break;
356
      case 2:
357
        ParamSet_DefaultSet2(); // Kamera
358
        break;
359
      case 3:
360
        ParamSet_DefaultSet3(); // Beginner
361
        break;
362
      default:
363
        ParamSet_DefaultSet2(); // Kamera
364
        break;
365
      }
366
      if(Channel_Backup) { // if we have a rc channel mapping backup in eeprom
367
        // restore it
368
        staticParams.ChannelAssignment[0] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+0]);
369
        staticParams.ChannelAssignment[1] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+1]);
370
        staticParams.ChannelAssignment[2] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+2]);
371
        staticParams.ChannelAssignment[3] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+3]);
372
        staticParams.ChannelAssignment[4] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+4]);
373
        staticParams.ChannelAssignment[5] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+5]);
374
        staticParams.ChannelAssignment[6] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+6]);
375
        staticParams.ChannelAssignment[7] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+7]);
376
      }
377
      ParamSet_WriteToEEProm(i);
378
    }
379
    // default-Setting is parameter set 3
380
    setActiveParamSet(1);
381
    // update version info
382
    SetParamByte(PID_PARAM_REVISION, EEPARAM_REVISION);
383
  }
384
  // read active parameter set to staticParams stucture
385
  ParamSet_ReadFromEEProm(getActiveParamSet());
386
  printf("\n\rUsing Parameter Set %d", getActiveParamSet());
387
 
388
  // load mixer table
389
  if(!MixerTable_ReadFromEEProm()) {
390
    printf("\n\rGenerating default Mixer Table");
391
    MixerTable_Default(); // Quadro
392
    MixerTable_WriteToEEProm();
393
  }
394
  // determine motornumber
395
  RequiredMotors = 0;
396
  for(i = 0; i < 16; i++) {
397
    if(Mixer.Motor[i][MIX_THROTTLE] > 0) RequiredMotors++;
398
  }
399
 
400
  printf("\n\rMixer-Config: '%s' (%u Motors)",Mixer.Name, RequiredMotors);
401
  printf("\n\r==============================");
402
}