Subversion Repositories FlightCtrl

Rev

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