Subversion Repositories FlightCtrl

Rev

Rev 1635 | Rev 1646 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1635 Rev 1645
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
2
// + Copyright (c) 04.2007 Holger Buss
3
// + Nur für den privaten Gebrauch
3
// + Nur für den privaten Gebrauch
4
// + www.MikroKopter.com
4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
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.
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
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
11
// + Verkauf von Luftbildaufnahmen, usw.
11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
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
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
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"
17
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + eindeutig als Ursprung verlinkt werden
18
// + eindeutig als Ursprung verlinkt werden
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
21
// + Benutzung auf eigene Gefahr
21
// + Benutzung auf eigene Gefahr
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
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
25
// + mit unserer Zustimmung zulässig
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
30
// + this list of conditions and the following disclaimer.
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
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.
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
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
34
// +     for non-commercial use (directly or indirectly)
34
// +     for non-commercial use (directly or indirectly)
35
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
35
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
36
// +     with our written permission
36
// +     with our written permission
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
38
// +     clearly linked as origin
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
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"
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
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
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)
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
48
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49
// +  POSSIBILITY OF SUCH DAMAGE.
49
// +  POSSIBILITY OF SUCH DAMAGE.
50
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
50
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
51
#include <avr/io.h>
51
#include <avr/io.h>
52
#include <avr/interrupt.h>
52
#include <avr/interrupt.h>
53
#include <avr/pgmspace.h>
53
#include <avr/pgmspace.h>
54
#include "analog.h"
54
#include "analog.h"
55
 
55
 
56
#include "sensors.h"
56
#include "sensors.h"
57
 
57
 
58
// for Delay functions
58
// for Delay functions
59
#include "timer0.h"
59
#include "timer0.h"
60
 
60
 
61
// For DebugOut
61
// For DebugOut
62
#include "uart0.h"
62
#include "uart0.h"
63
 
63
 
64
// For reading and writing acc. meter offsets.
64
// For reading and writing acc. meter offsets.
65
#include "eeprom.h"
65
#include "eeprom.h"
66
 
66
 
67
/*
67
/*
68
 * Arrays could have been used for the 2 * 3 axes, but despite some repetition,
-
 
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
68
 * For each A/D conversion cycle, each analog channel is sampled a number of times
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
69
 * (see array channelsForStates), and the results for each channel are summed.
74
 * acc. meters. They are not zero-offset.
70
 * Here are those for the gyros and the acc. meters. They are not zero-offset.
75
 * They are exported in the analog.h file - but please do not use them! The only
71
 * 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
72
 * reason for the export is that the ENC-03_FC1.3 modules needs them for calibrating
77
 * the offsets with the DAC.
73
 * the offsets with the DAC.
78
 */
74
 */
79
volatile int16_t rawPitchGyroSum, rawRollGyroSum, rawYawGyroSum;
75
volatile int16_t rawGyroSum[2], rawYawGyroSum;
80
volatile int16_t pitchAxisAcc = 0, rollAxisAcc = 0, ZAxisAcc = 0;
76
volatile int16_t acc[2] = {0,0}, ZAcc = 0;
81
volatile int16_t filteredPitchAxisAcc = 0, filteredRollAxisAcc = 0;
77
volatile int16_t filteredAcc[2] = {0,0};
82
 
-
 
83
// that float one - "Top" - is missing.
-
 
84
 
78
 
85
/*
79
/*
86
 * These 4 exported variables are zero-offset. The "filtered" ones are
80
 * These 4 exported variables are zero-offset. The "PID" ones are used
87
 * (if configured to with the GYROS_SECONDORDERFILTER define) low pass
81
 * in the attitude control as rotation rates. The "ATT" ones are for
88
 * filtered versions of the other 2.
-
 
89
 * They are derived from the "raw" values above, by zero-offsetting.
82
 * integration to angles.
90
 */
83
 */
91
volatile int16_t hiResPitchGyro = 0, hiResRollGyro = 0;
84
volatile int16_t gyro_PID[2];
92
volatile int16_t filteredHiResPitchGyro = 0, filteredHiResRollGyro = 0;
85
volatile int16_t gyro_ATT[2];
93
volatile int16_t pitchGyroD = 0, rollGyroD = 0;
86
volatile int16_t gyroD[2];
94
volatile int16_t yawGyro = 0;
87
volatile int16_t yawGyro = 0;
95
 
88
 
96
/*
89
/*
97
 * Offset values. These are the raw gyro and acc. meter sums when the copter is
90
 * 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
91
 * standing still. They are used for adjusting the gyro and acc. meter values
99
 * to be zero when the copter stands still.
92
 * to be centered on zero.
100
 */
93
 */
101
volatile int16_t pitchOffset, rollOffset, yawOffset;
94
volatile int16_t gyroOffset[2], yawGyroOffset;
102
volatile int16_t pitchAxisAccOffset, rollAxisAccOffset, ZAxisAccOffset;
95
volatile int16_t accOffset[2], ZAccOffset;
103
 
96
 
104
/*
97
/*
105
 * This allows some experimentation with the gyro filters.
98
 * This allows some experimentation with the gyro filters.
106
 * Should be replaced by #define's later on...
99
 * Should be replaced by #define's later on...
107
 */
100
 */
108
volatile uint8_t GYROS_FIRSTORDERFILTER;
101
volatile uint8_t GYROS_FIRSTORDERFILTER;
109
volatile uint8_t GYROS_SECONDORDERFILTER;
102
volatile uint8_t GYROS_SECONDORDERFILTER;
110
volatile uint8_t GYROS_DFILTER;
103
volatile uint8_t GYROS_DFILTER;
111
volatile uint8_t ACC_FILTER;
104
volatile uint8_t ACC_FILTER;
-
 
105
 
112
 
106
/*
-
 
107
 * Air pressure measurement.
113
// Air pressure (no support right now).
108
 */
114
// volatile int32_t AirPressure = 32000;
109
#define MIN_RAWPRESSURE 200
115
// volatile uint8_t average_pressure = 0;
110
#define MAX_RAWPRESSURE (1023-MIN_RAWPRESSURE)
116
// volatile int16_t StartAirPressure;
111
volatile uint8_t rangewidth = 53;
117
// volatile uint16_t ReadingAirPressure = 1023;
112
volatile uint16_t rawAirPressure;
118
// volatile int16_t HeightD = 0;
113
volatile uint16_t filteredAirPressure;
119
 
114
 
120
/*
115
/*
121
 * Battery voltage, in units of: 1k/11k / 3V * 1024 = 31.03 per volt.
116
 * 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.
117
 * 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.
118
 * So the initial value of 100 is for 9.7 volts.
124
 */
119
 */
125
volatile int16_t UBat = 100;
120
volatile int16_t UBat = 100;
126
 
-
 
127
volatile int16_t filteredAirPressure;
-
 
128
 
121
 
129
/*
122
/*
130
 * Control and status.
123
 * Control and status.
131
 */
124
 */
132
volatile uint16_t ADCycleCount = 0;
125
volatile uint16_t ADCycleCount = 0;
133
volatile uint8_t analogDataReady = 1;
126
volatile uint8_t analogDataReady = 1;
134
 
127
 
135
/*
128
/*
136
 * Experiment: Measuring vibration-induced sensor noise.
129
 * Experiment: Measuring vibration-induced sensor noise.
137
 */
130
 */
138
volatile uint16_t pitchGyroNoisePeak, rollGyroNoisePeak;
131
volatile uint16_t gyroNoisePeak[2];
139
volatile uint16_t pitchAccNoisePeak, rollAccNoisePeak;
132
volatile uint16_t accNoisePeak[2];
140
 
133
 
141
// ADC channels
134
// ADC channels
142
#define AD_GYRO_YAW       0
135
#define AD_GYRO_YAW       0
143
#define AD_GYRO_ROLL      1
136
#define AD_GYRO_ROLL      1
144
#define AD_GYRO_PITCH     2
137
#define AD_GYRO_PITCH     2
145
#define AD_AIRPRESSURE    3
138
#define AD_AIRPRESSURE    3
146
#define AD_UBAT           4
139
#define AD_UBAT           4
147
#define AD_ACC_Z          5
140
#define AD_ACC_Z          5
148
#define AD_ACC_ROLL       6
141
#define AD_ACC_ROLL       6
149
#define AD_ACC_PITCH      7
142
#define AD_ACC_PITCH      7
150
 
143
 
151
/*
144
/*
152
 * Table of AD converter inputs for each state.
145
 * Table of AD converter inputs for each state.
153
 * The number of samples summed for each channel is equal to
146
 * The number of samples summed for each channel is equal to
154
 * the number of times the channel appears in the array.
147
 * the number of times the channel appears in the array.
155
 * The max. number of samples that can be taken in 2 ms is:
148
 * 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
149
 * 20e6 / 128 / 13 / (1/2e-3) = 24. Since the main control
157
 * loop needs a little time between reading AD values and
150
 * loop needs a little time between reading AD values and
158
 * re-enabling ADC, the real limit is (how much?) lower.
151
 * re-enabling ADC, the real limit is (how much?) lower.
159
 * The acc. sensor is sampled even if not used - or installed
152
 * The acc. sensor is sampled even if not used - or installed
160
 * at all. The cost is not significant.
153
 * at all. The cost is not significant.
161
 */
154
 */
162
 
155
 
163
const uint8_t channelsForStates[] PROGMEM = {
156
const uint8_t channelsForStates[] PROGMEM = {
164
  AD_GYRO_PITCH,
157
  AD_GYRO_PITCH,
165
  AD_GYRO_ROLL,
158
  AD_GYRO_ROLL,
166
  AD_GYRO_YAW,
159
  AD_GYRO_YAW,
167
 
160
 
168
  AD_ACC_PITCH,
161
  AD_ACC_PITCH,
169
  AD_ACC_ROLL,
162
  AD_ACC_ROLL,
170
  // AD_AIRPRESSURE,
163
  // AD_AIRPRESSURE,
171
 
164
 
172
  AD_GYRO_PITCH,
165
  AD_GYRO_PITCH,
173
  AD_GYRO_ROLL,
166
  AD_GYRO_ROLL,
174
  AD_ACC_Z,       // at 7, measure Z acc.
167
  AD_ACC_Z,       // at 7, measure Z acc.
175
 
168
 
176
  AD_GYRO_PITCH,
169
  AD_GYRO_PITCH,
177
  AD_GYRO_ROLL,
170
  AD_GYRO_ROLL,
178
  AD_GYRO_YAW,    // at 10, finish yaw gyro
171
  AD_GYRO_YAW,    // at 10, finish yaw gyro
179
 
172
 
180
  AD_ACC_PITCH,   // at 11, finish pitch axis acc.
173
  AD_ACC_PITCH,   // at 11, finish pitch axis acc.
181
  AD_ACC_ROLL,    // at 12, finish roll axis acc.
174
  AD_ACC_ROLL,    // at 12, finish roll axis acc.
182
  AD_AIRPRESSURE, // at 13, finish air pressure.
175
  AD_AIRPRESSURE, // at 13, finish air pressure.
183
 
176
 
184
  AD_GYRO_PITCH,  // at 14, finish pitch gyro
177
  AD_GYRO_PITCH,  // at 14, finish pitch gyro
185
  AD_GYRO_ROLL,   // at 15, finish roll gyro
178
  AD_GYRO_ROLL,   // at 15, finish roll gyro
186
  AD_UBAT         // at 16, measure battery.
179
  AD_UBAT         // at 16, measure battery.
187
};
180
};
188
 
181
 
189
// Feature removed. Could be reintroduced later - but should work for all gyro types then.
182
// Feature removed. Could be reintroduced later - but should work for all gyro types then.
190
// uint8_t GyroDefectPitch = 0, GyroDefectRoll = 0, GyroDefectYaw = 0;
183
// uint8_t GyroDefectPitch = 0, GyroDefectRoll = 0, GyroDefectYaw = 0;
191
 
184
 
192
void analog_init(void) {
185
void analog_init(void) {
193
  uint8_t sreg = SREG;
186
  uint8_t sreg = SREG;
194
  // disable all interrupts before reconfiguration
187
  // disable all interrupts before reconfiguration
195
  cli();
188
  cli();
196
 
189
 
197
  //ADC0 ... ADC7 is connected to PortA pin 0 ... 7
190
  //ADC0 ... ADC7 is connected to PortA pin 0 ... 7
198
  DDRA = 0x00;
191
  DDRA = 0x00;
199
  PORTA = 0x00;
192
  PORTA = 0x00;
200
  // Digital Input Disable Register 0
193
  // Digital Input Disable Register 0
201
  // Disable digital input buffer for analog adc_channel pins
194
  // Disable digital input buffer for analog adc_channel pins
202
  DIDR0 = 0xFF;
195
  DIDR0 = 0xFF;
203
  // external reference, adjust data to the right
196
  // external reference, adjust data to the right
204
  ADMUX &= ~((1 << REFS1)|(1 << REFS0)|(1 << ADLAR));
197
  ADMUX &= ~((1 << REFS1)|(1 << REFS0)|(1 << ADLAR));
205
  // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice)
198
  // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice)
206
  ADMUX = (ADMUX & 0xE0) | AD_GYRO_PITCH;
199
  ADMUX = (ADMUX & 0xE0) | AD_GYRO_PITCH;
207
  //Set ADC Control and Status Register A
200
  //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
201
  //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);
202
  ADCSRA = (0<<ADEN)|(0<<ADSC)|(0<<ADATE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)|(0<<ADIE);
210
  //Set ADC Control and Status Register B
203
  //Set ADC Control and Status Register B
211
  //Trigger Source to Free Running Mode
204
  //Trigger Source to Free Running Mode
212
  ADCSRB &= ~((1 << ADTS2)|(1 << ADTS1)|(1 << ADTS0));
205
  ADCSRB &= ~((1 << ADTS2)|(1 << ADTS1)|(1 << ADTS0));
213
  // Start AD conversion
206
  // Start AD conversion
214
  analog_start();
207
  analog_start();
215
  // restore global interrupt flags
208
  // restore global interrupt flags
216
  SREG = sreg;
209
  SREG = sreg;
217
}
210
}
218
 
211
 
219
void measureNoise(const int16_t sensor, volatile uint16_t* const noiseMeasurement, const uint8_t damping) {
212
void measureNoise(const int16_t sensor, volatile uint16_t* const noiseMeasurement, const uint8_t damping) {
220
  if (sensor > (int16_t)(*noiseMeasurement)) {
213
  if (sensor > (int16_t)(*noiseMeasurement)) {
221
    *noiseMeasurement = sensor;
214
    *noiseMeasurement = sensor;
222
  } else if (-sensor > (int16_t)(*noiseMeasurement)) {
215
  } else if (-sensor > (int16_t)(*noiseMeasurement)) {
223
    *noiseMeasurement = -sensor;
216
    *noiseMeasurement = -sensor;
224
  } else if (*noiseMeasurement > damping) {
217
  } else if (*noiseMeasurement > damping) {
225
    *noiseMeasurement -= damping;
218
    *noiseMeasurement -= damping;
226
  } else {
219
  } else {
227
    *noiseMeasurement = 0;
220
    *noiseMeasurement = 0;
228
  }
221
  }
229
}
222
}
230
 
-
 
231
 
-
 
232
#define ADCENTER (1023/2)
-
 
233
#define HALFRANGE 400
-
 
234
uint8_t stepsize = 53;
-
 
235
 
223
 
236
uint16_t getAbsPressure(int advalue) {
224
uint16_t getAbsPressure(int advalue) {
237
  return (uint16_t)OCR0A * (uint16_t)stepsize + advalue;
225
  return (uint16_t)OCR0A * (uint16_t)rangewidth + advalue;
238
}
226
}
239
 
227
 
240
uint16_t filterAirPressure(uint16_t rawpressure) {
228
uint16_t filterAirPressure(uint16_t rawpressure) {
241
  return rawpressure;
229
  return rawpressure;
242
}
230
}
243
 
231
 
244
/*****************************************************/
232
/*****************************************************
245
/*     Interrupt Service Routine for ADC             */
-
 
246
/*****************************************************/
233
 * Interrupt Service Routine for ADC            
247
// Runs at 312.5 kHz or 3.2 µs
234
 * Runs at 312.5 kHz or 3.2 µs. When all states are
248
// When all states are processed the interrupt is disabled
235
 * processed the interrupt is disabled and further
249
// and the update of further AD conversions is stopped.
-
 
-
 
236
 * AD conversions are stopped.
250
 
237
 *****************************************************/
251
ISR(ADC_vect) {
238
ISR(ADC_vect) {
252
  static uint8_t ad_channel = AD_GYRO_PITCH, state = 0;
239
  static uint8_t ad_channel = AD_GYRO_PITCH, state = 0;
253
  static uint16_t sensorInputs[8] = {0,0,0,0,0,0,0,0};
240
  static uint16_t sensorInputs[8] = {0,0,0,0,0,0,0,0};
254
 
-
 
-
 
241
  static uint8_t pressure_wait = 10;
255
  uint8_t i;
242
  uint8_t i, axis;
256
  int16_t step = OCR0A;
243
  int16_t range;
257
 
244
 
258
  // for various filters...
245
  // for various filters...
259
  static int16_t pitchGyroFilter, rollGyroFilter, tempOffsetGyro;
246
  int16_t tempOffsetGyro, tempGyro;
260
 
247
 
261
  sensorInputs[ad_channel] += ADC;
248
  sensorInputs[ad_channel] += ADC;
262
 
249
 
263
  /*
250
  /*
264
   * Actually we don't need this "switch". We could do all the sampling into the
251
   * 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.
252
   * sensorInputs array first, and all the processing after the last sample.
266
   */
253
   */
267
  switch(state++) {
254
  switch(state++) {
268
  case 7: // Z acc      
255
  case 7: // Z acc      
269
#ifdef ACC_REVERSE_ZAXIS
256
#ifdef ACC_REVERSE_ZAXIS
270
    ZAxisAcc = -ZAxisAccOffset - sensorInputs[AD_ACC_Z];
257
    ZAcc = -ZAccOffset - sensorInputs[AD_ACC_Z];
271
#else
258
#else
272
    ZAxisAcc = sensorInputs[AD_ACC_Z] - ZAxisAccOffset;
259
    ZAcc = sensorInputs[AD_ACC_Z] - ZAccOffset;
273
#endif
260
#endif
274
    break;
261
    break;
275
   
262
   
276
  case 10: // yaw gyro
263
  case 10: // yaw gyro
277
    rawYawGyroSum = sensorInputs[AD_GYRO_YAW];
264
    rawYawGyroSum = sensorInputs[AD_GYRO_YAW];
278
#ifdef GYRO_REVERSE_YAW
265
#ifdef GYRO_REVERSE_YAW
279
    yawGyro = rawYawGyroSum - yawOffset;
266
    yawGyro = rawYawGyroSum - yawGyroOffset;
280
#else
267
#else
281
    yawGyro = yawOffset - rawYawGyroSum; // negative is "default" (FC 1.0-1.3).
268
    yawGyro = yawGyroOffset - rawYawGyroSum; // negative is "default" (FC 1.0-1.3).
282
#endif
269
#endif
283
    break;
270
    break;
284
   
271
   
285
  case 11: // pitch axis acc.
272
  case 11: // pitch axis acc.
286
#ifdef ACC_REVERSE_PITCHAXIS
273
#ifdef ACC_REVERSE_PITCHAXIS
287
    pitchAxisAcc = -pitchAxisAccOffset - sensorInputs[AD_ACC_PITCH];
274
    acc[PITCH] = -accOffset[PITCH] - sensorInputs[AD_ACC_PITCH];
288
#else
275
#else
289
    pitchAxisAcc = sensorInputs[AD_ACC_PITCH] - pitchAxisAccOffset;
276
    acc[PITCH] = sensorInputs[AD_ACC_PITCH] - accOffset[PITCH];
290
#endif
277
#endif
291
    filteredPitchAxisAcc = (filteredPitchAxisAcc * (ACC_FILTER-1) + pitchAxisAcc) / ACC_FILTER;
278
    filteredAcc[PITCH] = (filteredAcc[PITCH] * (ACC_FILTER-1) + acc[PITCH]) / ACC_FILTER;
292
 
279
 
293
    measureNoise(pitchAxisAcc, &pitchAccNoisePeak, 1);
280
    measureNoise(acc[PITCH], &accNoisePeak[PITCH], 1);
294
    break;
281
    break;
295
   
282
   
296
  case 12: // roll axis acc.
283
  case 12: // roll axis acc.
297
#ifdef ACC_REVERSE_ROLLAXIS
284
#ifdef ACC_REVERSE_ROLLAXIS
298
    rollAxisAcc = sensorInputs[AD_ACC_ROLL] - rollAxisAccOffset;
285
    acc[ROLL] = sensorInputs[AD_ACC_ROLL] - accOffset[ROLL];
299
#else
286
#else
300
    rollAxisAcc = -rollAxisAccOffset - sensorInputs[AD_ACC_ROLL];
287
    acc[ROLL] = -accOffset[ROLL] - sensorInputs[AD_ACC_ROLL];
301
#endif
288
#endif
302
    filteredRollAxisAcc = (filteredRollAxisAcc * (ACC_FILTER-1) + rollAxisAcc) / ACC_FILTER;
289
    filteredAcc[ROLL] = (filteredAcc[ROLL] * (ACC_FILTER-1) + acc[ROLL]) / ACC_FILTER;
303
    measureNoise(rollAxisAcc, &rollAccNoisePeak, 1);
290
    measureNoise(acc[ROLL], &accNoisePeak[ROLL], 1);
304
    break;
291
    break;
305
   
292
 
306
  case 13: // air pressure
293
  case 13: // air pressure
-
 
294
    if (pressure_wait) {
-
 
295
      // A range switch was done recently. Wait for steadying.
-
 
296
      pressure_wait--;
-
 
297
      break;
-
 
298
    }
-
 
299
    range = OCR0A;
307
    if (sensorInputs[AD_AIRPRESSURE] < ADCENTER-HALFRANGE) {
300
    rawAirPressure = sensorInputs[AD_AIRPRESSURE];
-
 
301
    if (rawAirPressure < MIN_RAWPRESSURE) {
308
      // value is too low, so decrease voltage on the op amp minus input, making the value higher.
302
      // 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);
303
      range -= (MAX_RAWPRESSURE - rawAirPressure) / rangewidth - 1;
310
      if (step<0) step = 0;
304
      if (range < 0) range = 0;
-
 
305
      pressure_wait = (OCR0A - range) * 4;
311
      OCR0A = step;
306
      OCR0A = range;
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) {
307
    } else if (rawAirPressure > MAX_RAWPRESSURE) {
314
      // value is too high, so increase voltage on the op amp minus input, making the value lower.
308
      // 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);
309
      range += (rawAirPressure - MIN_RAWPRESSURE) / rangewidth - 1;
316
      if (step>254) step = 254;
310
      if (range > 254) range = 254;
-
 
311
      pressure_wait = (range - OCR0A) * 4;
317
      OCR0A = step;
312
      OCR0A = range;
318
      // wait = ... (calculate something here .. calculate at what time the R/C filter is to within one sample off)
-
 
319
    } else {
313
    } else {
320
      filteredAirPressure = filterAirPressure(getAbsPressure(sensorInputs[AD_AIRPRESSURE]));
314
      filteredAirPressure = filterAirPressure(getAbsPressure(rawAirPressure));
321
    }
315
    }
-
 
316
   
-
 
317
    DebugOut.Analog[12] = range;
-
 
318
    DebugOut.Analog[13] = rawAirPressure;
-
 
319
    DebugOut.Analog[14] = filteredAirPressure;
322
    break;
320
    break;
-
 
321
 
323
 
322
  case 14:
-
 
323
  case 15: // pitch or roll gyro.
324
  case 14: // pitch gyro
324
    axis = state - 15;
-
 
325
    tempGyro = rawGyroSum[axis] = sensorInputs[AD_GYRO_PITCH - axis];
-
 
326
        // DebugOut.Analog[6 + 3 * axis ] = tempGyro;
-
 
327
    /*
-
 
328
     * Process the gyro data for the PID controller.
325
    rawPitchGyroSum = sensorInputs[AD_GYRO_PITCH];
329
     */
326
    // Filter already before offsetting. The offsetting resolution improvement obtained by divding by
330
    // 1) Extrapolate: Near the ends of the range, we boost the input significantly. This simulates a
-
 
331
    //    gyro with a wider range, and helps counter saturation at full control.
-
 
332
 
-
 
333
    if (staticParams.GlobalConfig & CFG_ROTARY_RATE_LIMITER) {
-
 
334
      if (tempGyro < SENSOR_MIN_PITCHROLL) {
-
 
335
        tempGyro = tempGyro * EXTRAPOLATION_SLOPE - EXTRAPOLATION_LIMIT;
-
 
336
      }
327
    // GYROS_FIRSTORDERFILTER _after_ offsetting is too small to be worth pursuing.
337
      else if (tempGyro > SENSOR_MAX_PITCHROLL) {
328
    pitchGyroFilter = (pitchGyroFilter * (GYROS_FIRSTORDERFILTER-1) + rawPitchGyroSum * GYRO_FACTOR_PITCHROLL) / GYROS_FIRSTORDERFILTER;
338
        tempGyro = (tempGyro - SENSOR_MAX_PITCHROLL) * EXTRAPOLATION_SLOPE + SENSOR_MAX_PITCHROLL;
-
 
339
      }
-
 
340
    }
-
 
341
 
329
    // Offset to 0.
342
    // 2) Apply sign and offset, scale before filtering.
330
#ifdef GYROS_REVERSE_PITCH
343
    if (GYROS_REVERSE[axis]) {
331
    tempOffsetGyro = pitchOffset - pitchGyroFilter;
344
      tempOffsetGyro = (gyroOffset[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL;
332
#else
345
    } else {
333
    tempOffsetGyro = pitchGyroFilter - pitchOffset;
346
      tempOffsetGyro = (tempGyro - gyroOffset[axis]) * GYRO_FACTOR_PITCHROLL;
-
 
347
    }
334
#endif
348
 
335
    // Calculate the delta from last shot and filter it.
349
    // 3) Scale and filter.
-
 
350
    tempOffsetGyro = (gyro_PID[axis] * (GYROS_PIDFILTER-1) + tempOffsetGyro) / GYROS_PIDFILTER;
336
    pitchGyroD = (pitchGyroD * (GYROS_DFILTER-1) + (tempOffsetGyro - hiResPitchGyro)) / GYROS_DFILTER;
351
 
337
    // How we can overwrite the last value. This value is used for the D part of the PID controller.
352
    // 4) Measure noise.
-
 
353
    measureNoise(tempOffsetGyro, &gyroNoisePeak[axis], GYRO_NOISE_MEASUREMENT_DAMPING);
338
    hiResPitchGyro = tempOffsetGyro;
354
 
339
    // Filter a little more. This value is used in integration to angles.
355
    // 5) Differential measurement. 
340
    filteredHiResPitchGyro = (filteredHiResPitchGyro * (GYROS_SECONDORDERFILTER-1) + hiResPitchGyro) / GYROS_SECONDORDERFILTER;
-
 
-
 
356
    gyroD[axis] = (gyroD[axis] * (GYROS_DFILTER-1) + (tempOffsetGyro - gyro_PID[axis])) / GYROS_DFILTER;
341
    measureNoise(hiResPitchGyro, &pitchGyroNoisePeak, GYRO_NOISE_MEASUREMENT_DAMPING);
357
 
-
 
358
    // 6) Done.
342
    break;
359
    gyro_PID[axis] = tempOffsetGyro;
-
 
360
 
343
   
361
    /*
-
 
362
     * Now process the data for attitude angles.
344
  case 15: // Roll gyro. Works the same as pitch.
363
     */
-
 
364
    tempGyro = rawGyroSum[axis];
345
    rawRollGyroSum = sensorInputs[AD_GYRO_ROLL];
365
   
346
    rollGyroFilter = (rollGyroFilter * (GYROS_FIRSTORDERFILTER-1) + rawRollGyroSum * GYRO_FACTOR_PITCHROLL) / GYROS_FIRSTORDERFILTER;
366
    // 1) Apply sign and offset, scale before filtering.
347
#ifdef GYRO_REVERSE_ROLL
367
    if (GYROS_REVERSE[axis]) {
348
    tempOffsetGyro = rollOffset - rollGyroFilter;
368
      tempOffsetGyro = (gyroOffset[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL;
349
#else
369
    } else {
350
    tempOffsetGyro = rollGyroFilter - rollOffset;
370
      tempOffsetGyro = (tempGyro - gyroOffset[axis]) * GYRO_FACTOR_PITCHROLL;
351
#endif
-
 
-
 
371
    }
352
    rollGyroD = (rollGyroD * (GYROS_DFILTER-1) + (tempOffsetGyro - hiResRollGyro)) / GYROS_DFILTER;
372
   
353
    hiResRollGyro = tempOffsetGyro;
373
    // 2) Filter.
354
    filteredHiResRollGyro = (filteredHiResRollGyro * (GYROS_SECONDORDERFILTER-1) + hiResRollGyro) / GYROS_SECONDORDERFILTER;
-
 
355
    measureNoise(hiResRollGyro, &rollGyroNoisePeak, GYRO_NOISE_MEASUREMENT_DAMPING);
374
    gyro_ATT[axis] = (gyro_ATT[axis] * (GYROS_INTEGRALFILTER-1) + tempOffsetGyro) / GYROS_INTEGRALFILTER;
356
    break;
375
    break;
357
   
376
   
358
  case 16:
377
  case 16:
359
    // battery
378
    // battery
360
    UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4;
379
    UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4;
361
    analogDataReady = 1; // mark
380
    analogDataReady = 1; // mark
362
    ADCycleCount++;
381
    ADCycleCount++;
363
    // Stop the sampling. Cycle is over.
382
    // Stop the sampling. Cycle is over.
364
    state = 0;
383
    state = 0;
365
    for (i=0; i<8; i++) {
384
    for (i=0; i<8; i++) {
366
      sensorInputs[i] = 0;
385
      sensorInputs[i] = 0;
367
    }
386
    }
368
    break;
387
    break;
369
  default: {} // do nothing.
388
  default: {} // do nothing.
370
  }
389
  }
371
 
390
 
372
  // set up for next state.
391
  // set up for next state.
373
  ad_channel = pgm_read_byte(&channelsForStates[state]);
392
  ad_channel = pgm_read_byte(&channelsForStates[state]);
374
  // ad_channel = channelsForStates[state];
393
  // ad_channel = channelsForStates[state];
375
     
394
     
376
  // set adc muxer to next ad_channel
395
  // set adc muxer to next ad_channel
377
  ADMUX = (ADMUX & 0xE0) | ad_channel;
396
  ADMUX = (ADMUX & 0xE0) | ad_channel;
378
  // after full cycle stop further interrupts
397
  // after full cycle stop further interrupts
379
  if(state) analog_start();
398
  if(state) analog_start();
380
}
399
}
381
 
400
 
382
void analog_calibrate(void) {
401
void analog_calibrate(void) {
383
#define GYRO_OFFSET_CYCLES 32
402
#define GYRO_OFFSET_CYCLES 32
384
  uint8_t i;
403
  uint8_t i;
385
  int32_t _pitchOffset = 0, _rollOffset = 0, _yawOffset = 0;
404
  int32_t _pitchOffset = 0, _rollOffset = 0, _yawOffset = 0;
386
 
405
 
387
  // Set the filters... to be removed again, once some good settings are found.
406
  // Set the filters... to be removed again, once some good settings are found.
388
  GYROS_FIRSTORDERFILTER = (dynamicParams.UserParams[4]   & 0b00000011)       + 1;
407
  GYROS_FIRSTORDERFILTER = (dynamicParams.UserParams[4]   & 0b00000011)       + 1;
389
  GYROS_SECONDORDERFILTER = ((dynamicParams.UserParams[4] & 0b00001100) >> 2) + 1;
408
  GYROS_SECONDORDERFILTER = ((dynamicParams.UserParams[4] & 0b00001100) >> 2) + 1;
390
  GYROS_DFILTER = ((dynamicParams.UserParams[4]           & 0b00110000) >> 4) + 1;
409
  GYROS_DFILTER = ((dynamicParams.UserParams[4]           & 0b00110000) >> 4) + 1;
391
  ACC_FILTER = ((dynamicParams.UserParams[4]              & 0b11000000) >> 6) + 1;
410
  ACC_FILTER = ((dynamicParams.UserParams[4]              & 0b11000000) >> 6) + 1;
392
 
411
 
393
  pitchOffset = rollOffset = yawOffset = 0;
412
  gyroOffset[PITCH] = gyroOffset[ROLL] = yawGyroOffset = 0;
394
 
413
 
395
  gyro_calibrate();
414
  gyro_calibrate();
396
 
415
 
397
  // determine gyro bias by averaging (requires that the copter does not rotate around any axis!)
416
  // determine gyro bias by averaging (requires that the copter does not rotate around any axis!)
398
  for(i=0; i < GYRO_OFFSET_CYCLES; i++) {
417
  for(i=0; i < GYRO_OFFSET_CYCLES; i++) {
399
    Delay_ms_Mess(10);
418
    Delay_ms_Mess(10);
400
    _pitchOffset += rawPitchGyroSum * GYRO_FACTOR_PITCHROLL;
419
    _pitchOffset += rawGyroSum[PITCH];
401
    _rollOffset  += rawRollGyroSum * GYRO_FACTOR_PITCHROLL;
420
    _rollOffset  += rawGyroSum[ROLL];
402
    _yawOffset   += rawYawGyroSum;
421
    _yawOffset   += rawYawGyroSum;
403
  }
422
  }
404
 
423
 
405
  pitchOffset = (_pitchOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES;
424
  gyroOffset[PITCH] = (_pitchOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES;
406
  rollOffset  = (_rollOffset  + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES;
425
  gyroOffset[ROLL] = (_rollOffset  + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES;
407
  yawOffset   = (_yawOffset   + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES;
426
  yawGyroOffset   = (_yawOffset   + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES;
408
 
-
 
409
  filteredHiResPitchGyro = filteredHiResRollGyro = 0;
-
 
410
 
427
 
411
  pitchAxisAccOffset = (int16_t)GetParamWord(PID_ACC_NICK);
-
 
412
  rollAxisAccOffset  = (int16_t)GetParamWord(PID_ACC_ROLL);
428
  gyro_PID[PITCH] = gyro_PID[ROLL] = 0;
413
  ZAxisAccOffset     = (int16_t)GetParamWord(PID_ACC_TOP);
429
  gyro_ATT[PITCH] = gyro_ATT[ROLL] = 0;
414
 
430
 
415
  // Noise is relative to offset. So, reset noise measurements when
431
  // Noise is relative to offset. So, reset noise measurements when
416
  // changing offsets.
432
  // changing offsets.
417
  pitchGyroNoisePeak = rollGyroNoisePeak = 0;
433
  gyroNoisePeak[PITCH] = gyroNoisePeak[ROLL] = 0;
418
 
434
 
419
  // Setting offset values has an influence in the analog.c ISR
435
  accOffset[PITCH] = (int16_t)GetParamWord(PID_ACC_PITCH);
420
  // Therefore run measurement for 100ms to achive stable readings
436
  accOffset[ROLL]  = (int16_t)GetParamWord(PID_ACC_ROLL);
421
  Delay_ms_Mess(100);
437
  ZAccOffset       = (int16_t)GetParamWord(PID_ACC_TOP);
422
}
438
}
423
 
439
 
424
/*
440
/*
425
 * Find acc. offsets for a neutral reading, and write them to EEPROM.
441
 * 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
442
 * Does not (!} update the local variables. This must be done with a
427
 * call to analog_calibrate() - this always (?) is done by the caller
443
 * call to analog_calibrate() - this always (?) is done by the caller
428
 * anyway. There would be nothing wrong with updating the variables
444
 * anyway. There would be nothing wrong with updating the variables
429
 * directly from here, though.
445
 * directly from here, though.
430
 */
446
 */
431
void analog_calibrateAcc(void) {
447
void analog_calibrateAcc(void) {
432
#define ACC_OFFSET_CYCLES 10
448
#define ACC_OFFSET_CYCLES 10
433
  uint8_t i;
449
  uint8_t i;
434
  int32_t _pitchAxisOffset = 0, _rollAxisOffset = 0, _ZAxisOffset = 0;
450
  int32_t _pitchAxisOffset = 0, _rollAxisOffset = 0, _ZAxisOffset = 0;
-
 
451
  // int16_t pressureDiff, savedRawAirPressure;
435
 
452
 
436
  pitchAxisAccOffset = rollAxisAccOffset = ZAxisAccOffset = 0;
453
  accOffset[PITCH] = accOffset[ROLL] = ZAccOffset = 0;
437
 
454
 
438
  for(i=0; i < ACC_OFFSET_CYCLES; i++) {
455
  for(i=0; i < ACC_OFFSET_CYCLES; i++) {
439
    Delay_ms_Mess(10);
456
    Delay_ms_Mess(10);
440
    _pitchAxisOffset += pitchAxisAcc;
457
    _pitchAxisOffset += acc[PITCH];
441
    _rollAxisOffset += rollAxisAcc;
458
    _rollAxisOffset  += acc[ROLL];
442
    _ZAxisOffset += ZAxisAcc;
459
    _ZAxisOffset += ZAcc;
443
  }
460
  }
444
 
461
 
445
  // Save ACC neutral settings to eeprom
462
  // Save ACC neutral settings to eeprom
446
  SetParamWord(PID_ACC_NICK, (uint16_t)((_pitchAxisOffset + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES));
463
  SetParamWord(PID_ACC_PITCH, (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));
464
  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));
465
  SetParamWord(PID_ACC_TOP,  (uint16_t)((_ZAxisOffset     + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES));
449
 
466
 
450
  // Noise is relative to offset. So, reset noise measurements when
467
  // Noise is relative to offset. So, reset noise measurements when
451
  // changing offsets.
468
  // changing offsets.
452
  pitchAccNoisePeak = rollAccNoisePeak = 0;
469
  accNoisePeak[PITCH] = accNoisePeak[ROLL] = 0;
-
 
470
  // Setting offset values has an influence in the analog.c ISR
-
 
471
  // Therefore run measurement for 100ms to achive stable readings
-
 
472
  // Delay_ms_Mess(100);
-
 
473
 
-
 
474
  // Set the feedback so that air pressure ends up in the middle of the range.
-
 
475
  // (raw pressure high --> OCR0A also high...)
-
 
476
  // OCR0A += (rawAirPressure - 512) / rangewidth;
-
 
477
  // Delay_ms_Mess(500);
-
 
478
 
-
 
479
  /*
-
 
480
    pressureDiff = 0;
-
 
481
    DebugOut.Analog[16] = rawAirPressure;
-
 
482
 
-
 
483
    #define PRESSURE_CAL_CYCLE_COUNT 2
-
 
484
    for (i=0; i<PRESSURE_CAL_CYCLE_COUNT; i++) {
-
 
485
    savedRawAirPressure = rawAirPressure;
-
 
486
    OCR0A++;
-
 
487
    Delay_ms_Mess(200);
-
 
488
    // raw pressure will decrease.
-
 
489
    pressureDiff += (savedRawAirPressure - rawAirPressure);
-
 
490
 
-
 
491
    savedRawAirPressure = rawAirPressure;
-
 
492
    OCR0A--;
-
 
493
    Delay_ms_Mess(200);
-
 
494
    // raw pressure will increase.
-
 
495
    pressureDiff += (rawAirPressure - savedRawAirPressure);
-
 
496
    }
-
 
497
 
-
 
498
    DebugOut.Analog[15] = rangewidth =
-
 
499
    (pressureDiff + PRESSURE_CAL_CYCLE_COUNT * 2 - 1) / (PRESSURE_CAL_CYCLE_COUNT * 2);
-
 
500
  */
453
}
501
}
454
 
502