Subversion Repositories FlightCtrl

Rev

Rev 1634 | Rev 1645 | 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),
7
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
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
17
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + eindeutig als Ursprung verlinkt werden
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)
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
#include <avr/io.h>
52
#include <avr/interrupt.h>
53
#include <avr/pgmspace.h>
54
#include "analog.h"
55
 
56
#include "sensors.h"
57
 
58
// for Delay functions
59
#include "timer0.h"
60
 
61
// For DebugOut
62
#include "uart0.h"
63
 
64
// For reading and writing acc. meter offsets.
65
#include "eeprom.h"
66
 
67
/*
1635 - 68
 * Arrays could have been used for the 2 * 3 axes, but despite some repetition,
1612 dongfang 69
 * the code is easier to read without.
70
 *
71
 * For each A/D conversion cycle, each channel (eg. the yaw gyro, or the Z axis
72
 * accelerometer) is sampled a number of times (see array channelsForStates), and
73
 * the results for each channel are summed. Here are those for the gyros and the
74
 * acc. meters. They are not zero-offset.
75
 * They are exported in the analog.h file - but please do not use them! The only
76
 * reason for the export is that the ENC-03_FC1.3 modules needs them for calibrating
77
 * the offsets with the DAC.
78
 */
79
volatile int16_t rawPitchGyroSum, rawRollGyroSum, rawYawGyroSum;
80
volatile int16_t pitchAxisAcc = 0, rollAxisAcc = 0, ZAxisAcc = 0;
81
volatile int16_t filteredPitchAxisAcc = 0, filteredRollAxisAcc = 0;
82
 
83
// that float one - "Top" - is missing.
84
 
85
/*
86
 * These 4 exported variables are zero-offset. The "filtered" ones are
87
 * (if configured to with the GYROS_SECONDORDERFILTER define) low pass
88
 * filtered versions of the other 2.
89
 * They are derived from the "raw" values above, by zero-offsetting.
90
 */
91
volatile int16_t hiResPitchGyro = 0, hiResRollGyro = 0;
92
volatile int16_t filteredHiResPitchGyro = 0, filteredHiResRollGyro = 0;
93
volatile int16_t pitchGyroD = 0, rollGyroD = 0;
94
volatile int16_t yawGyro = 0;
95
 
96
/*
97
 * Offset values. These are the raw gyro and acc. meter sums when the copter is
98
 * standing still. They are used for adjusting the gyro and acc. meter values
99
 * to be zero when the copter stands still.
100
 */
101
volatile int16_t pitchOffset, rollOffset, yawOffset;
102
volatile int16_t pitchAxisAccOffset, rollAxisAccOffset, ZAxisAccOffset;
103
 
104
/*
105
 * This allows some experimentation with the gyro filters.
106
 * Should be replaced by #define's later on...
107
 */
108
volatile uint8_t GYROS_FIRSTORDERFILTER;
109
volatile uint8_t GYROS_SECONDORDERFILTER;
110
volatile uint8_t GYROS_DFILTER;
111
volatile uint8_t ACC_FILTER;
112
 
113
// Air pressure (no support right now).
114
// volatile int32_t AirPressure = 32000;
115
// volatile uint8_t average_pressure = 0;
116
// volatile int16_t StartAirPressure;
117
// volatile uint16_t ReadingAirPressure = 1023;
118
// volatile int16_t HeightD = 0;
119
 
120
/*
121
 * Battery voltage, in units of: 1k/11k / 3V * 1024 = 31.03 per volt.
122
 * That is divided by 3 below, for a final 10.34 per volt.
123
 * So the initial value of 100 is for 9.7 volts.
124
 */
125
volatile int16_t UBat = 100;
126
 
1634 - 127
volatile int16_t filteredAirPressure;
128
 
1612 dongfang 129
/*
130
 * Control and status.
131
 */
132
volatile uint16_t ADCycleCount = 0;
133
volatile uint8_t analogDataReady = 1;
134
 
135
/*
136
 * Experiment: Measuring vibration-induced sensor noise.
137
 */
138
volatile uint16_t pitchGyroNoisePeak, rollGyroNoisePeak;
139
volatile uint16_t pitchAccNoisePeak, rollAccNoisePeak;
140
 
141
// ADC channels
1634 - 142
#define AD_GYRO_YAW       0
143
#define AD_GYRO_ROLL      1
144
#define AD_GYRO_PITCH     2
145
#define AD_AIRPRESSURE    3
146
#define AD_UBAT           4
147
#define AD_ACC_Z          5
148
#define AD_ACC_ROLL       6
149
#define AD_ACC_PITCH      7
1612 dongfang 150
 
151
/*
152
 * Table of AD converter inputs for each state.
153
 * The number of samples summed for each channel is equal to
154
 * the number of times the channel appears in the array.
155
 * The max. number of samples that can be taken in 2 ms is:
156
 * 20e6 / 128 / 13 / (1/2e-3) = 24. Since the main control
157
 * loop needs a little time between reading AD values and
158
 * re-enabling ADC, the real limit is (how much?) lower.
159
 * The acc. sensor is sampled even if not used - or installed
160
 * at all. The cost is not significant.
161
 */
162
 
163
const uint8_t channelsForStates[] PROGMEM = {
164
  AD_GYRO_PITCH,
165
  AD_GYRO_ROLL,
166
  AD_GYRO_YAW,
167
 
1634 - 168
  AD_ACC_PITCH,
1612 dongfang 169
  AD_ACC_ROLL,
1634 - 170
  // AD_AIRPRESSURE,
1612 dongfang 171
 
172
  AD_GYRO_PITCH,
173
  AD_GYRO_ROLL,
1634 - 174
  AD_ACC_Z,       // at 7, measure Z acc.
1612 dongfang 175
 
176
  AD_GYRO_PITCH,
177
  AD_GYRO_ROLL,
178
  AD_GYRO_YAW,    // at 10, finish yaw gyro
179
 
180
  AD_ACC_PITCH,   // at 11, finish pitch axis acc.
181
  AD_ACC_ROLL,    // at 12, finish roll axis acc.
1634 - 182
  AD_AIRPRESSURE, // at 13, finish air pressure.
1612 dongfang 183
 
1634 - 184
  AD_GYRO_PITCH,  // at 14, finish pitch gyro
185
  AD_GYRO_ROLL,   // at 15, finish roll gyro
186
  AD_UBAT         // at 16, measure battery.
1612 dongfang 187
};
188
 
189
// Feature removed. Could be reintroduced later - but should work for all gyro types then.
190
// uint8_t GyroDefectPitch = 0, GyroDefectRoll = 0, GyroDefectYaw = 0;
191
 
192
void analog_init(void) {
193
  uint8_t sreg = SREG;
194
  // disable all interrupts before reconfiguration
195
  cli();
196
 
197
  //ADC0 ... ADC7 is connected to PortA pin 0 ... 7
198
  DDRA = 0x00;
199
  PORTA = 0x00;
200
  // Digital Input Disable Register 0
201
  // Disable digital input buffer for analog adc_channel pins
202
  DIDR0 = 0xFF;
203
  // external reference, adjust data to the right
204
  ADMUX &= ~((1 << REFS1)|(1 << REFS0)|(1 << ADLAR));
205
  // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice)
206
  ADMUX = (ADMUX & 0xE0) | AD_GYRO_PITCH;
207
  //Set ADC Control and Status Register A
208
  //Auto Trigger Enable, Prescaler Select Bits to Division Factor 128, i.e. ADC clock = SYSCKL/128 = 156.25 kHz
209
  ADCSRA = (0<<ADEN)|(0<<ADSC)|(0<<ADATE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)|(0<<ADIE);
210
  //Set ADC Control and Status Register B
211
  //Trigger Source to Free Running Mode
212
  ADCSRB &= ~((1 << ADTS2)|(1 << ADTS1)|(1 << ADTS0));
213
  // Start AD conversion
214
  analog_start();
215
  // restore global interrupt flags
216
  SREG = sreg;
217
}
218
 
219
void measureNoise(const int16_t sensor, volatile uint16_t* const noiseMeasurement, const uint8_t damping) {
220
  if (sensor > (int16_t)(*noiseMeasurement)) {
221
    *noiseMeasurement = sensor;
222
  } else if (-sensor > (int16_t)(*noiseMeasurement)) {
223
    *noiseMeasurement = -sensor;
224
  } else if (*noiseMeasurement > damping) {
225
    *noiseMeasurement -= damping;
226
  } else {
227
    *noiseMeasurement = 0;
228
  }
229
}
230
 
1634 - 231
 
232
#define ADCENTER (1023/2)
233
#define HALFRANGE 400
234
uint8_t stepsize = 53;
235
 
236
uint16_t getAbsPressure(int advalue) {
237
  return (uint16_t)OCR0A * (uint16_t)stepsize + advalue;
238
}
239
 
240
uint16_t filterAirPressure(uint16_t rawpressure) {
241
  return rawpressure;
242
}
243
 
1612 dongfang 244
/*****************************************************/
245
/*     Interrupt Service Routine for ADC             */
246
/*****************************************************/
247
// Runs at 312.5 kHz or 3.2 µs
248
// When all states are processed the interrupt is disabled
249
// and the update of further AD conversions is stopped.
250
 
251
ISR(ADC_vect) {
252
  static uint8_t ad_channel = AD_GYRO_PITCH, state = 0;
253
  static uint16_t sensorInputs[8] = {0,0,0,0,0,0,0,0};
254
 
255
  uint8_t i;
1634 - 256
  int16_t step = OCR0A;
257
 
1612 dongfang 258
  // for various filters...
259
  static int16_t pitchGyroFilter, rollGyroFilter, tempOffsetGyro;
260
 
261
  sensorInputs[ad_channel] += ADC;
262
 
263
  /*
264
   * Actually we don't need this "switch". We could do all the sampling into the
265
   * sensorInputs array first, and all the processing after the last sample.
266
   */
267
  switch(state++) {
268
  case 7: // Z acc      
269
#ifdef ACC_REVERSE_ZAXIS
270
    ZAxisAcc = -ZAxisAccOffset - sensorInputs[AD_ACC_Z];
271
#else
272
    ZAxisAcc = sensorInputs[AD_ACC_Z] - ZAxisAccOffset;
273
#endif
274
    break;
275
 
276
  case 10: // yaw gyro
277
    rawYawGyroSum = sensorInputs[AD_GYRO_YAW];
278
#ifdef GYRO_REVERSE_YAW
279
    yawGyro = rawYawGyroSum - yawOffset;
280
#else
281
    yawGyro = yawOffset - rawYawGyroSum; // negative is "default" (FC 1.0-1.3).
282
#endif
283
    break;
284
 
285
  case 11: // pitch axis acc.
286
#ifdef ACC_REVERSE_PITCHAXIS
287
    pitchAxisAcc = -pitchAxisAccOffset - sensorInputs[AD_ACC_PITCH];
288
#else
289
    pitchAxisAcc = sensorInputs[AD_ACC_PITCH] - pitchAxisAccOffset;
290
#endif
291
    filteredPitchAxisAcc = (filteredPitchAxisAcc * (ACC_FILTER-1) + pitchAxisAcc) / ACC_FILTER;
292
 
293
    measureNoise(pitchAxisAcc, &pitchAccNoisePeak, 1);
294
    break;
295
 
296
  case 12: // roll axis acc.
297
#ifdef ACC_REVERSE_ROLLAXIS
298
    rollAxisAcc = sensorInputs[AD_ACC_ROLL] - rollAxisAccOffset;
299
#else
300
    rollAxisAcc = -rollAxisAccOffset - sensorInputs[AD_ACC_ROLL];
301
#endif
302
    filteredRollAxisAcc = (filteredRollAxisAcc * (ACC_FILTER-1) + rollAxisAcc) / ACC_FILTER;
303
    measureNoise(rollAxisAcc, &rollAccNoisePeak, 1);
304
    break;
305
 
1634 - 306
  case 13: // air pressure
307
    if (sensorInputs[AD_AIRPRESSURE] < ADCENTER-HALFRANGE) {
308
      // value is too low, so decrease voltage on the op amp minus input, making the value higher.
309
      step -= ((HALFRANGE-sensorInputs[AD_AIRPRESSURE]) / stepsize + 1);
310
      if (step<0) step = 0;
311
      OCR0A = step;
312
      // wait = ... (calculate something here .. calculate at what time the R/C filter is to within one sample off)
313
    } else if (sensorInputs[AD_AIRPRESSURE] > ADCENTER+HALFRANGE) {
314
      // value is too high, so increase voltage on the op amp minus input, making the value lower.
315
      step += ((sensorInputs[AD_AIRPRESSURE] - HALFRANGE)/stepsize + 1);
316
      if (step>254) step = 254;
317
      OCR0A = step;
318
      // wait = ... (calculate something here .. calculate at what time the R/C filter is to within one sample off)
319
    } else {
320
      filteredAirPressure = filterAirPressure(getAbsPressure(sensorInputs[AD_AIRPRESSURE]));
321
    }
322
    break;
323
 
324
  case 14: // pitch gyro
1612 dongfang 325
    rawPitchGyroSum = sensorInputs[AD_GYRO_PITCH];
326
    // Filter already before offsetting. The offsetting resolution improvement obtained by divding by
327
    // GYROS_FIRSTORDERFILTER _after_ offsetting is too small to be worth pursuing.
328
    pitchGyroFilter = (pitchGyroFilter * (GYROS_FIRSTORDERFILTER-1) + rawPitchGyroSum * GYRO_FACTOR_PITCHROLL) / GYROS_FIRSTORDERFILTER;
329
    // Offset to 0.
330
#ifdef GYROS_REVERSE_PITCH
331
    tempOffsetGyro = pitchOffset - pitchGyroFilter;
332
#else
333
    tempOffsetGyro = pitchGyroFilter - pitchOffset;
334
#endif
335
    // Calculate the delta from last shot and filter it.
336
    pitchGyroD = (pitchGyroD * (GYROS_DFILTER-1) + (tempOffsetGyro - hiResPitchGyro)) / GYROS_DFILTER;
337
    // How we can overwrite the last value. This value is used for the D part of the PID controller.
338
    hiResPitchGyro = tempOffsetGyro;
339
    // Filter a little more. This value is used in integration to angles.
340
    filteredHiResPitchGyro = (filteredHiResPitchGyro * (GYROS_SECONDORDERFILTER-1) + hiResPitchGyro) / GYROS_SECONDORDERFILTER;
341
    measureNoise(hiResPitchGyro, &pitchGyroNoisePeak, GYRO_NOISE_MEASUREMENT_DAMPING);
342
    break;
343
 
1634 - 344
  case 15: // Roll gyro. Works the same as pitch.
1612 dongfang 345
    rawRollGyroSum = sensorInputs[AD_GYRO_ROLL];
346
    rollGyroFilter = (rollGyroFilter * (GYROS_FIRSTORDERFILTER-1) + rawRollGyroSum * GYRO_FACTOR_PITCHROLL) / GYROS_FIRSTORDERFILTER;
347
#ifdef GYRO_REVERSE_ROLL
348
    tempOffsetGyro = rollOffset - rollGyroFilter;
349
#else
350
    tempOffsetGyro = rollGyroFilter - rollOffset;
351
#endif
352
    rollGyroD = (rollGyroD * (GYROS_DFILTER-1) + (tempOffsetGyro - hiResRollGyro)) / GYROS_DFILTER;
353
    hiResRollGyro = tempOffsetGyro;
354
    filteredHiResRollGyro = (filteredHiResRollGyro * (GYROS_SECONDORDERFILTER-1) + hiResRollGyro) / GYROS_SECONDORDERFILTER;
355
    measureNoise(hiResRollGyro, &rollGyroNoisePeak, GYRO_NOISE_MEASUREMENT_DAMPING);
356
    break;
357
 
1634 - 358
  case 16:
1612 dongfang 359
    // battery
360
    UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4;
361
    analogDataReady = 1; // mark
362
    ADCycleCount++;
363
    // Stop the sampling. Cycle is over.
364
    state = 0;
365
    for (i=0; i<8; i++) {
366
      sensorInputs[i] = 0;
367
    }
368
    break;
369
  default: {} // do nothing.
370
  }
371
 
372
  // set up for next state.
373
  ad_channel = pgm_read_byte(&channelsForStates[state]);
374
  // ad_channel = channelsForStates[state];
375
 
376
  // set adc muxer to next ad_channel
377
  ADMUX = (ADMUX & 0xE0) | ad_channel;
378
  // after full cycle stop further interrupts
379
  if(state) analog_start();
380
}
381
 
382
void analog_calibrate(void) {
383
#define GYRO_OFFSET_CYCLES 32
384
  uint8_t i;
385
  int32_t _pitchOffset = 0, _rollOffset = 0, _yawOffset = 0;
386
 
387
  // Set the filters... to be removed again, once some good settings are found.
388
  GYROS_FIRSTORDERFILTER = (dynamicParams.UserParams[4]   & 0b00000011)       + 1;
389
  GYROS_SECONDORDERFILTER = ((dynamicParams.UserParams[4] & 0b00001100) >> 2) + 1;
390
  GYROS_DFILTER = ((dynamicParams.UserParams[4]           & 0b00110000) >> 4) + 1;
391
  ACC_FILTER = ((dynamicParams.UserParams[4]              & 0b11000000) >> 6) + 1;
392
 
393
  pitchOffset = rollOffset = yawOffset = 0;
394
 
395
  gyro_calibrate();
396
 
397
  // determine gyro bias by averaging (requires that the copter does not rotate around any axis!)
398
  for(i=0; i < GYRO_OFFSET_CYCLES; i++) {
399
    Delay_ms_Mess(10);
400
    _pitchOffset += rawPitchGyroSum * GYRO_FACTOR_PITCHROLL;
401
    _rollOffset  += rawRollGyroSum * GYRO_FACTOR_PITCHROLL;
402
    _yawOffset   += rawYawGyroSum;
403
  }
404
 
405
  pitchOffset = (_pitchOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES;
406
  rollOffset  = (_rollOffset  + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES;
407
  yawOffset   = (_yawOffset   + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES;
408
 
409
  filteredHiResPitchGyro = filteredHiResRollGyro = 0;
410
 
411
  pitchAxisAccOffset = (int16_t)GetParamWord(PID_ACC_NICK);
412
  rollAxisAccOffset  = (int16_t)GetParamWord(PID_ACC_ROLL);
413
  ZAxisAccOffset     = (int16_t)GetParamWord(PID_ACC_TOP);
414
 
415
  // Noise is relative to offset. So, reset noise measurements when
416
  // changing offsets.
417
  pitchGyroNoisePeak = rollGyroNoisePeak = 0;
418
 
419
  // Setting offset values has an influence in the analog.c ISR
420
  // Therefore run measurement for 100ms to achive stable readings
421
  Delay_ms_Mess(100);
422
}
423
 
424
/*
425
 * Find acc. offsets for a neutral reading, and write them to EEPROM.
426
 * Does not (!} update the local variables. This must be done with a
427
 * call to analog_calibrate() - this always (?) is done by the caller
428
 * anyway. There would be nothing wrong with updating the variables
429
 * directly from here, though.
430
 */
431
void analog_calibrateAcc(void) {
432
#define ACC_OFFSET_CYCLES 10
433
  uint8_t i;
434
  int32_t _pitchAxisOffset = 0, _rollAxisOffset = 0, _ZAxisOffset = 0;
435
 
436
  pitchAxisAccOffset = rollAxisAccOffset = ZAxisAccOffset = 0;
437
 
438
  for(i=0; i < ACC_OFFSET_CYCLES; i++) {
439
    Delay_ms_Mess(10);
440
    _pitchAxisOffset += pitchAxisAcc;
441
    _rollAxisOffset += rollAxisAcc;
442
    _ZAxisOffset += ZAxisAcc;
443
  }
444
 
445
  // Save ACC neutral settings to eeprom
446
  SetParamWord(PID_ACC_NICK, (uint16_t)((_pitchAxisOffset + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES));
447
  SetParamWord(PID_ACC_ROLL, (uint16_t)((_rollAxisOffset  + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES));
448
  SetParamWord(PID_ACC_TOP,  (uint16_t)((_ZAxisOffset     + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES));
449
 
450
  // Noise is relative to offset. So, reset noise measurements when
451
  // changing offsets.
452
  pitchAccNoisePeak = rollAccNoisePeak = 0;
453
}