Subversion Repositories FlightCtrl

Rev

Rev 1615 | Rev 1635 | 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
  }
94
}
95
 
96
void setOtherDefaults(void) {
1614 dongfang 97
/* Channel assignments were changed to the normal:
98
 * Aileron/roll=1, elevator/pitch=2, throttle=3, yaw/rudder=4
99
 */
100
  staticParams.ChannelAssignment[CH_PITCH]    = 2;
101
  staticParams.ChannelAssignment[CH_ROLL]     = 1;
102
  staticParams.ChannelAssignment[CH_THROTTLE] = 3;
103
  staticParams.ChannelAssignment[CH_YAW]      = 4;
104
  staticParams.ChannelAssignment[CH_POTS+0]   = 5;
105
  staticParams.ChannelAssignment[CH_POTS+1]   = 6;
106
  staticParams.ChannelAssignment[CH_POTS+2]   = 7;
107
  staticParams.ChannelAssignment[CH_POTS+3]   = 8;
1612 dongfang 108
  staticParams.GlobalConfig = CFG_AXIS_COUPLING_ACTIVE | CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE;//CFG_HEIGHT_CONTROL | CFG_HEIGHT_SWITCH | CFG_COMPASS_FIX;
109
  staticParams.HeightMinGas = 30;
110
  staticParams.MaxHeight     = 251;
111
  staticParams.HeightP      = 10;
112
  staticParams.HeightD  = 30;
113
  staticParams.Height_ACC_Effect = 30;
114
  staticParams.Height_Gain = 4;
115
  staticParams.StickP = 12;
116
  staticParams.StickD = 16;
117
  staticParams.StickYawP = 12;
1615 dongfang 118
  staticParams.MinThrottle = 8;
119
  staticParams.MaxThrottle = 230;
1612 dongfang 120
  staticParams.CompassYawEffect = 128;
121
  staticParams.GyroP = 80;
122
  staticParams.GyroI = 100;
123
  staticParams.LowVoltageWarning = 94;
124
  staticParams.EmergencyGas = 35;
125
  staticParams.EmergencyGasDuration = 30;
126
  staticParams.UfoArrangement = 0;
127
  staticParams.IFactor = 32;
128
  staticParams.ServoPitchControl = 100;
129
  staticParams.ServoPitchComp = 40;
130
  staticParams.ServoPitchCompInvert = 0;
131
  staticParams.ServoPitchMin = 50;
132
  staticParams.ServoPitchMax = 150;
133
  staticParams.ServoRefresh = 5;
134
  staticParams.LoopGasLimit = 50;
135
  staticParams.LoopThreshold = 90;
136
  staticParams.LoopHysteresis = 50;
137
  staticParams.BitConfig = 0;
138
  staticParams.AxisCoupling1 = 90;
139
  staticParams.AxisCoupling2 = 67;
140
  staticParams.AxisCouplingYawCorrection = 0;
141
  staticParams.GyroAccTrim = 2;
142
  staticParams.DynamicStability = 50;
143
  staticParams.J16Bitmask = 95;
144
  staticParams.J17Bitmask = 243;
145
  staticParams.J16Timing = 15;
146
  staticParams.J17Timing = 15;
147
  staticParams.NaviGpsModeControl = 253;
148
  staticParams.NaviGpsGain = 100;
149
  staticParams.NaviGpsP = 90;
150
  staticParams.NaviGpsI = 90;
151
  staticParams.NaviGpsD = 90;
152
  staticParams.NaviGpsPLimit = 75;
153
  staticParams.NaviGpsILimit = 75;
154
  staticParams.NaviGpsDLimit = 75;
155
  staticParams.NaviGpsACC = 0;
156
  staticParams.NaviGpsMinSat = 6;
157
  staticParams.NaviStickThreshold = 8;
158
  staticParams.NaviWindCorrection = 90;
159
  staticParams.NaviSpeedCompensation = 30;
160
  staticParams.NaviOperatingRadius = 100;
161
  staticParams.NaviAngleLimitation = 100;
162
  staticParams.NaviPHLoginTime = 4;
163
}
164
 
165
/***************************************************/
166
/*    Default Values for parameter set 1           */
167
/***************************************************/
168
void ParamSet_DefaultSet1(void) { // sport
169
  gyro_setDefaults();
170
  setDefaultUserParams();
171
  setOtherDefaults();
172
  memcpy(staticParams.Name, "Sport\0",6);
173
}
174
 
175
/***************************************************/
176
/*    Default Values for parameter set 2           */
177
/***************************************************/
178
void ParamSet_DefaultSet2(void) { // normal
179
  gyro_setDefaults();
180
  setDefaultUserParams();
181
  setOtherDefaults();
182
  staticParams.Height_Gain = 3;
183
  staticParams.GyroAccTrim = 32;
184
  staticParams.J16Timing = 20;
185
  staticParams.J17Timing = 20;
186
  memcpy(staticParams.Name, "Normal\0", 7);
187
}
188
 
189
/***************************************************/
190
/*    Default Values for parameter set 3           */
191
/***************************************************/
192
void ParamSet_DefaultSet3(void) { // beginner
193
  gyro_setDefaults();
194
  setDefaultUserParams();
195
  setOtherDefaults();
196
  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;
197
  staticParams.Height_Gain = 3;
198
  staticParams.EmergencyGasDuration = 20;
199
  staticParams.AxisCouplingYawCorrection = 70;
200
  staticParams.GyroAccTrim = 32;
201
  staticParams.J16Timing = 30;
202
  staticParams.J17Timing = 30;
203
  memcpy(staticParams.Name, "Beginner\0", 9);
204
}
205
 
206
/***************************************************/
207
/*       Read Parameter from EEPROM as byte        */
208
/***************************************************/
209
uint8_t GetParamByte(uint16_t param_id) {
210
  return eeprom_read_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id]);
211
}
212
 
213
/***************************************************/
214
/*       Write Parameter to EEPROM as byte         */
215
/***************************************************/
216
void SetParamByte(uint16_t param_id, uint8_t value) {
217
  eeprom_write_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
218
}
219
 
220
/***************************************************/
221
/*       Read Parameter from EEPROM as word        */
222
/***************************************************/
223
uint16_t GetParamWord(uint16_t param_id) {
224
  return eeprom_read_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id]);
225
}
226
 
227
/***************************************************/
228
/*       Write Parameter to EEPROM as word         */
229
/***************************************************/
230
void SetParamWord(uint16_t param_id, uint16_t value) {
231
  eeprom_write_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
232
}
233
 
234
/***************************************************/
235
/*       Read Parameter Set from EEPROM            */
236
/***************************************************/
237
// number [1..5]
238
void ParamSet_ReadFromEEProm(uint8_t setnumber) {
239
  if((1 > setnumber) || (setnumber > 5)) setnumber = 3;
240
  eeprom_read_block((uint8_t *) &staticParams.ChannelAssignment[0], &EEPromArray[EEPROM_ADR_PARAMSET_BEGIN + PARAMSET_STRUCT_LEN * (setnumber - 1)], PARAMSET_STRUCT_LEN);
241
  output_init();
242
}
243
 
244
/***************************************************/
245
/*        Write Parameter Set to EEPROM            */
246
/***************************************************/
247
// number [1..5]
248
void ParamSet_WriteToEEProm(uint8_t setnumber) {
249
  if(setnumber > 5) setnumber = 5;
250
  if(setnumber < 1) return;
251
  eeprom_write_block((uint8_t *) &staticParams.ChannelAssignment[0], &EEPromArray[EEPROM_ADR_PARAMSET_BEGIN + PARAMSET_STRUCT_LEN * (setnumber - 1)], PARAMSET_STRUCT_LEN);
252
  eeprom_write_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAMSET_LENGTH], PARAMSET_STRUCT_LEN);
253
  eeprom_write_block( &staticParams.ChannelAssignment[0], &EEPromArray[EEPROM_ADR_CHANNELS], 8); // backup the first 8 bytes that is the rc channel mapping
254
  // set this parameter set to active set
255
  setActiveParamSet(setnumber);
256
  output_init();
257
}
258
 
259
 
260
/***************************************************/
261
/*       Get active parameter set                  */
262
/***************************************************/
263
uint8_t getActiveParamSet(void) {
264
  uint8_t setnumber;
265
  setnumber = eeprom_read_byte(&EEPromArray[PID_ACTIVE_SET]);
266
  if(setnumber > 5) {
267
    setnumber = 3;
268
    eeprom_write_byte(&EEPromArray[PID_ACTIVE_SET], setnumber);
269
  }
270
  return(setnumber);
271
}
272
 
273
/***************************************************/
274
/*       Set active parameter set                  */
275
/***************************************************/
276
void setActiveParamSet(uint8_t setnumber) {
277
  if(setnumber > 5) setnumber = 5;
278
  if(setnumber < 1) setnumber = 1;
279
  eeprom_write_byte(&EEPromArray[PID_ACTIVE_SET], setnumber);
280
}
281
 
282
/***************************************************/
283
/*          Read MixerTable from EEPROM            */
284
/***************************************************/
285
uint8_t MixerTable_ReadFromEEProm(void) {
286
  if(eeprom_read_byte(&EEPromArray[EEPROM_ADR_MIXER_TABLE]) == EEMIXER_REVISION) {
287
    eeprom_read_block((uint8_t *) &Mixer, &EEPromArray[EEPROM_ADR_MIXER_TABLE], sizeof(Mixer));
288
    return 1;
289
  }
290
  else return 0;
291
}
292
 
293
/***************************************************/
294
/*          Write Mixer Table to EEPROM            */
295
/***************************************************/
296
uint8_t MixerTable_WriteToEEProm(void) {
297
  if(Mixer.Revision == EEMIXER_REVISION) {
298
    eeprom_write_block((uint8_t *) &Mixer, &EEPromArray[EEPROM_ADR_MIXER_TABLE], sizeof(Mixer));
299
    return 1;
300
  }
301
  else return 0;
302
}
303
 
304
/***************************************************/
305
/*    Default Values for Mixer Table               */
306
/***************************************************/
307
void MixerTable_Default(void) { // Quadro 
308
  uint8_t i;
309
  Mixer.Revision = EEMIXER_REVISION;
310
  // clear mixer table (but preset throttle)
311
  for(i = 0; i < 16; i++) {
312
    Mixer.Motor[i][MIX_THROTTLE] = i < 4 ? 64 : 0;
313
    Mixer.Motor[i][MIX_PITCH] = 0;
314
    Mixer.Motor[i][MIX_ROLL] = 0;
315
    Mixer.Motor[i][MIX_YAW]  = 0;
316
  }
317
  // default = Quadro
318
  Mixer.Motor[0][MIX_PITCH] = +64;                                 Mixer.Motor[0][MIX_YAW] = +64;
319
  Mixer.Motor[1][MIX_PITCH] = -64;                                 Mixer.Motor[1][MIX_YAW] = +64;
320
  Mixer.Motor[2][MIX_ROLL] = -64; Mixer.Motor[2][MIX_YAW] = -64;
321
  Mixer.Motor[3][MIX_ROLL] = +64; Mixer.Motor[3][MIX_YAW] = -64;
322
  memcpy(Mixer.Name, "Quadro\0", 7);
323
}
324
 
325
/***************************************************/
326
/*       Initialize EEPROM Parameter Sets          */
327
/***************************************************/
328
void ParamSet_Init(void) {
329
  uint8_t Channel_Backup  = 0, i;
330
  // parameter version  check
331
  if(eeprom_read_byte(&EEPromArray[PID_PARAM_REVISION]) != EEPARAM_REVISION) {
332
    // if version check faild
333
    printf("\n\rInit Parameter in EEPROM");
334
    eeprom_write_byte(&EEPromArray[EEPROM_ADR_MIXER_TABLE], 0xFF); // reset also mixer table
335
    // check if channel mapping backup is valid
336
    if(         (eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+0]) < 12)
337
                &&  (eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+1]) < 12)
338
                &&  (eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+2]) < 12)
339
                &&  (eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+3]) < 12)
340
                )
341
      {
342
        Channel_Backup = 1;
343
      }
344
    // fill all 5 parameter settings
345
    for (i=1; i<6; i++) {
346
      switch(i) {
347
      case 1:
348
        ParamSet_DefaultSet1(); // Fill staticParams Structure to default parameter set 1 (Sport)
349
        break;
350
      case 2:
351
        ParamSet_DefaultSet2(); // Kamera
352
        break;
353
      case 3:
354
        ParamSet_DefaultSet3(); // Beginner
355
        break;
356
      default:
357
        ParamSet_DefaultSet2(); // Kamera
358
        break;
359
      }
360
      if(Channel_Backup) { // if we have a rc channel mapping backup in eeprom
361
        // restore it
362
        staticParams.ChannelAssignment[0] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+0]);
363
        staticParams.ChannelAssignment[1] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+1]);
364
        staticParams.ChannelAssignment[2] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+2]);
365
        staticParams.ChannelAssignment[3] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+3]);
366
        staticParams.ChannelAssignment[4] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+4]);
367
        staticParams.ChannelAssignment[5] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+5]);
368
        staticParams.ChannelAssignment[6] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+6]);
369
        staticParams.ChannelAssignment[7] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+7]);
370
      }
371
      ParamSet_WriteToEEProm(i);
372
    }
373
    // default-Setting is parameter set 3
374
    setActiveParamSet(1);
375
    // update version info
376
    SetParamByte(PID_PARAM_REVISION, EEPARAM_REVISION);
377
  }
378
  // read active parameter set to staticParams stucture
379
  ParamSet_ReadFromEEProm(getActiveParamSet());
380
  printf("\n\rUsing Parameter Set %d", getActiveParamSet());
381
 
382
  // load mixer table
383
  if(!MixerTable_ReadFromEEProm()) {
384
    printf("\n\rGenerating default Mixer Table");
385
    MixerTable_Default(); // Quadro
386
    MixerTable_WriteToEEProm();
387
  }
388
  // determine motornumber
389
  RequiredMotors = 0;
390
  for(i = 0; i < 16; i++) {
391
    if(Mixer.Motor[i][MIX_THROTTLE] > 0) RequiredMotors++;
392
  }
393
 
394
  printf("\n\rMixer-Config: '%s' (%u Motors)",Mixer.Name, RequiredMotors);
395
  printf("\n\r==============================");
396
}