Subversion Repositories FlightCtrl

Rev

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

Rev 1645 Rev 1646
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
 * For each A/D conversion cycle, each analog channel is sampled a number of times
68
 * For each A/D conversion cycle, each analog channel is sampled a number of times
69
 * (see array channelsForStates), and the results for each channel are summed.
69
 * (see array channelsForStates), and the results for each channel are summed.
70
 * Here are those for the gyros and the acc. meters. They are not zero-offset.
70
 * Here are those for the gyros and the acc. meters. They are not zero-offset.
71
 * 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
72
 * 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
73
 * the offsets with the DAC.
73
 * the offsets with the DAC.
74
 */
74
 */
75
volatile int16_t rawGyroSum[2], rawYawGyroSum;
75
volatile int16_t rawGyroSum[3];
76
volatile int16_t acc[2] = {0,0}, ZAcc = 0;
76
volatile int16_t acc[3];
77
volatile int16_t filteredAcc[2] = {0,0};
77
volatile int16_t filteredAcc[2]={0,0};
78
 
78
 
79
/*
79
/*
80
 * These 4 exported variables are zero-offset. The "PID" ones are used
80
 * These 4 exported variables are zero-offset. The "PID" ones are used
81
 * in the attitude control as rotation rates. The "ATT" ones are for
81
 * in the attitude control as rotation rates. The "ATT" ones are for
82
 * integration to angles.
82
 * integration to angles.
83
 */
83
 */
84
volatile int16_t gyro_PID[2];
84
volatile int16_t gyro_PID[2];
85
volatile int16_t gyro_ATT[2];
85
volatile int16_t gyro_ATT[2];
86
volatile int16_t gyroD[2];
86
volatile int16_t gyroD[2];
87
volatile int16_t yawGyro = 0;
87
volatile int16_t yawGyro;
88
 
88
 
89
/*
89
/*
90
 * 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
91
 * 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
92
 * to be centered on zero.
92
 * to be centered on zero.
93
 */
93
 */
94
volatile int16_t gyroOffset[2], yawGyroOffset;
94
volatile int16_t gyroOffset[3] = {
-
 
95
        512 * GYRO_SUMMATION_FACTOR_PITCHROLL,
-
 
96
        512 * GYRO_SUMMATION_FACTOR_PITCHROLL,
-
 
97
        512 * GYRO_SUMMATION_FACTOR_YAW
-
 
98
};
-
 
99
 
95
volatile int16_t accOffset[2], ZAccOffset;
100
volatile int16_t accOffset[3] = {
-
 
101
        512 * ACC_SUMMATION_FACTOR_PITCHROLL,
-
 
102
        512 * ACC_SUMMATION_FACTOR_PITCHROLL,
-
 
103
        512 * ACC_SUMMATION_FACTOR_Z
-
 
104
};
96
 
105
 
97
/*
106
/*
98
 * This allows some experimentation with the gyro filters.
107
 * This allows some experimentation with the gyro filters.
99
 * Should be replaced by #define's later on...
108
 * Should be replaced by #define's later on...
100
 */
109
 */
101
volatile uint8_t GYROS_FIRSTORDERFILTER;
110
volatile uint8_t GYROS_PID_FILTER;
102
volatile uint8_t GYROS_SECONDORDERFILTER;
111
volatile uint8_t GYROS_ATT_FILTER;
103
volatile uint8_t GYROS_DFILTER;
112
volatile uint8_t GYROS_D_FILTER;
104
volatile uint8_t ACC_FILTER;
113
volatile uint8_t ACC_FILTER;
105
 
114
 
106
/*
115
/*
107
 * Air pressure measurement.
116
 * Air pressure measurement.
108
 */
117
 */
109
#define MIN_RAWPRESSURE 200
118
#define MIN_RAWPRESSURE 200
110
#define MAX_RAWPRESSURE (1023-MIN_RAWPRESSURE)
119
#define MAX_RAWPRESSURE (1023-MIN_RAWPRESSURE)
111
volatile uint8_t rangewidth = 53;
120
volatile uint8_t rangewidth = 53;
112
volatile uint16_t rawAirPressure;
121
volatile uint16_t rawAirPressure;
113
volatile uint16_t filteredAirPressure;
122
volatile uint16_t filteredAirPressure;
114
 
123
 
115
/*
124
/*
116
 * Battery voltage, in units of: 1k/11k / 3V * 1024 = 31.03 per volt.
125
 * Battery voltage, in units of: 1k/11k / 3V * 1024 = 31.03 per volt.
117
 * That is divided by 3 below, for a final 10.34 per volt.
126
 * That is divided by 3 below, for a final 10.34 per volt.
118
 * So the initial value of 100 is for 9.7 volts.
127
 * So the initial value of 100 is for 9.7 volts.
119
 */
128
 */
120
volatile int16_t UBat = 100;
129
volatile int16_t UBat = 100;
121
 
130
 
122
/*
131
/*
123
 * Control and status.
132
 * Control and status.
124
 */
133
 */
125
volatile uint16_t ADCycleCount = 0;
134
volatile uint16_t ADCycleCount = 0;
126
volatile uint8_t analogDataReady = 1;
135
volatile uint8_t analogDataReady = 1;
127
 
136
 
128
/*
137
/*
129
 * Experiment: Measuring vibration-induced sensor noise.
138
 * Experiment: Measuring vibration-induced sensor noise.
130
 */
139
 */
131
volatile uint16_t gyroNoisePeak[2];
140
volatile uint16_t gyroNoisePeak[2];
132
volatile uint16_t accNoisePeak[2];
141
volatile uint16_t accNoisePeak[2];
133
 
142
 
134
// ADC channels
143
// ADC channels
135
#define AD_GYRO_YAW       0
144
#define AD_GYRO_YAW       0
136
#define AD_GYRO_ROLL      1
145
#define AD_GYRO_ROLL      1
137
#define AD_GYRO_PITCH     2
146
#define AD_GYRO_PITCH     2
138
#define AD_AIRPRESSURE    3
147
#define AD_AIRPRESSURE    3
139
#define AD_UBAT           4
148
#define AD_UBAT           4
140
#define AD_ACC_Z          5
149
#define AD_ACC_Z          5
141
#define AD_ACC_ROLL       6
150
#define AD_ACC_ROLL       6
142
#define AD_ACC_PITCH      7
151
#define AD_ACC_PITCH      7
143
 
152
 
144
/*
153
/*
145
 * Table of AD converter inputs for each state.
154
 * Table of AD converter inputs for each state.
146
 * The number of samples summed for each channel is equal to
155
 * The number of samples summed for each channel is equal to
147
 * the number of times the channel appears in the array.
156
 * the number of times the channel appears in the array.
148
 * The max. number of samples that can be taken in 2 ms is:
157
 * The max. number of samples that can be taken in 2 ms is:
149
 * 20e6 / 128 / 13 / (1/2e-3) = 24. Since the main control
158
 * 20e6 / 128 / 13 / (1/2e-3) = 24. Since the main control
150
 * loop needs a little time between reading AD values and
159
 * loop needs a little time between reading AD values and
151
 * re-enabling ADC, the real limit is (how much?) lower.
160
 * re-enabling ADC, the real limit is (how much?) lower.
152
 * The acc. sensor is sampled even if not used - or installed
161
 * The acc. sensor is sampled even if not used - or installed
153
 * at all. The cost is not significant.
162
 * at all. The cost is not significant.
154
 */
163
 */
155
 
164
 
156
const uint8_t channelsForStates[] PROGMEM = {
165
const uint8_t channelsForStates[] PROGMEM = {
157
  AD_GYRO_PITCH,
166
  AD_GYRO_PITCH,
158
  AD_GYRO_ROLL,
167
  AD_GYRO_ROLL,
159
  AD_GYRO_YAW,
168
  AD_GYRO_YAW,
160
 
169
 
161
  AD_ACC_PITCH,
170
  AD_ACC_PITCH,
162
  AD_ACC_ROLL,
171
  AD_ACC_ROLL,
163
  // AD_AIRPRESSURE,
172
  // AD_AIRPRESSURE,
164
 
173
 
165
  AD_GYRO_PITCH,
174
  AD_GYRO_PITCH,
166
  AD_GYRO_ROLL,
175
  AD_GYRO_ROLL,
167
  AD_ACC_Z,       // at 7, measure Z acc.
176
  AD_ACC_Z,       // at 7, measure Z acc.
168
 
177
 
169
  AD_GYRO_PITCH,
178
  AD_GYRO_PITCH,
170
  AD_GYRO_ROLL,
179
  AD_GYRO_ROLL,
171
  AD_GYRO_YAW,    // at 10, finish yaw gyro
180
  AD_GYRO_YAW,    // at 10, finish yaw gyro
172
 
181
 
173
  AD_ACC_PITCH,   // at 11, finish pitch axis acc.
182
  AD_ACC_PITCH,   // at 11, finish pitch axis acc.
174
  AD_ACC_ROLL,    // at 12, finish roll axis acc.
183
  AD_ACC_ROLL,    // at 12, finish roll axis acc.
175
  AD_AIRPRESSURE, // at 13, finish air pressure.
184
  AD_AIRPRESSURE, // at 13, finish air pressure.
176
 
185
 
177
  AD_GYRO_PITCH,  // at 14, finish pitch gyro
186
  AD_GYRO_PITCH,  // at 14, finish pitch gyro
178
  AD_GYRO_ROLL,   // at 15, finish roll gyro
187
  AD_GYRO_ROLL,   // at 15, finish roll gyro
179
  AD_UBAT         // at 16, measure battery.
188
  AD_UBAT         // at 16, measure battery.
180
};
189
};
181
 
190
 
182
// Feature removed. Could be reintroduced later - but should work for all gyro types then.
191
// Feature removed. Could be reintroduced later - but should work for all gyro types then.
183
// uint8_t GyroDefectPitch = 0, GyroDefectRoll = 0, GyroDefectYaw = 0;
192
// uint8_t GyroDefectPitch = 0, GyroDefectRoll = 0, GyroDefectYaw = 0;
184
 
193
 
185
void analog_init(void) {
194
void analog_init(void) {
186
  uint8_t sreg = SREG;
195
  uint8_t sreg = SREG;
187
  // disable all interrupts before reconfiguration
196
  // disable all interrupts before reconfiguration
188
  cli();
197
  cli();
189
 
198
 
190
  //ADC0 ... ADC7 is connected to PortA pin 0 ... 7
199
  //ADC0 ... ADC7 is connected to PortA pin 0 ... 7
191
  DDRA = 0x00;
200
  DDRA = 0x00;
192
  PORTA = 0x00;
201
  PORTA = 0x00;
193
  // Digital Input Disable Register 0
202
  // Digital Input Disable Register 0
194
  // Disable digital input buffer for analog adc_channel pins
203
  // Disable digital input buffer for analog adc_channel pins
195
  DIDR0 = 0xFF;
204
  DIDR0 = 0xFF;
196
  // external reference, adjust data to the right
205
  // external reference, adjust data to the right
197
  ADMUX &= ~((1 << REFS1)|(1 << REFS0)|(1 << ADLAR));
206
  ADMUX &= ~((1 << REFS1)|(1 << REFS0)|(1 << ADLAR));
198
  // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice)
207
  // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice)
199
  ADMUX = (ADMUX & 0xE0) | AD_GYRO_PITCH;
208
  ADMUX = (ADMUX & 0xE0) | AD_GYRO_PITCH;
200
  //Set ADC Control and Status Register A
209
  //Set ADC Control and Status Register A
201
  //Auto Trigger Enable, Prescaler Select Bits to Division Factor 128, i.e. ADC clock = SYSCKL/128 = 156.25 kHz
210
  //Auto Trigger Enable, Prescaler Select Bits to Division Factor 128, i.e. ADC clock = SYSCKL/128 = 156.25 kHz
202
  ADCSRA = (0<<ADEN)|(0<<ADSC)|(0<<ADATE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)|(0<<ADIE);
211
  ADCSRA = (0<<ADEN)|(0<<ADSC)|(0<<ADATE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)|(0<<ADIE);
203
  //Set ADC Control and Status Register B
212
  //Set ADC Control and Status Register B
204
  //Trigger Source to Free Running Mode
213
  //Trigger Source to Free Running Mode
205
  ADCSRB &= ~((1 << ADTS2)|(1 << ADTS1)|(1 << ADTS0));
214
  ADCSRB &= ~((1 << ADTS2)|(1 << ADTS1)|(1 << ADTS0));
206
  // Start AD conversion
215
  // Start AD conversion
207
  analog_start();
216
  analog_start();
208
  // restore global interrupt flags
217
  // restore global interrupt flags
209
  SREG = sreg;
218
  SREG = sreg;
210
}
219
}
211
 
220
 
212
void measureNoise(const int16_t sensor, volatile uint16_t* const noiseMeasurement, const uint8_t damping) {
221
void measureNoise(const int16_t sensor, volatile uint16_t* const noiseMeasurement, const uint8_t damping) {
213
  if (sensor > (int16_t)(*noiseMeasurement)) {
222
  if (sensor > (int16_t)(*noiseMeasurement)) {
214
    *noiseMeasurement = sensor;
223
    *noiseMeasurement = sensor;
215
  } else if (-sensor > (int16_t)(*noiseMeasurement)) {
224
  } else if (-sensor > (int16_t)(*noiseMeasurement)) {
216
    *noiseMeasurement = -sensor;
225
    *noiseMeasurement = -sensor;
217
  } else if (*noiseMeasurement > damping) {
226
  } else if (*noiseMeasurement > damping) {
218
    *noiseMeasurement -= damping;
227
    *noiseMeasurement -= damping;
219
  } else {
228
  } else {
220
    *noiseMeasurement = 0;
229
    *noiseMeasurement = 0;
221
  }
230
  }
222
}
231
}
223
 
232
 
224
uint16_t getAbsPressure(int advalue) {
233
uint16_t getAbsPressure(int advalue) {
225
  return (uint16_t)OCR0A * (uint16_t)rangewidth + advalue;
234
  return (uint16_t)OCR0A * (uint16_t)rangewidth + advalue;
226
}
235
}
227
 
236
 
228
uint16_t filterAirPressure(uint16_t rawpressure) {
237
uint16_t filterAirPressure(uint16_t rawpressure) {
229
  return rawpressure;
238
  return rawpressure;
230
}
239
}
231
 
240
 
232
/*****************************************************
241
/*****************************************************
233
 * Interrupt Service Routine for ADC            
242
 * Interrupt Service Routine for ADC            
234
 * Runs at 312.5 kHz or 3.2 µs. When all states are
243
 * Runs at 312.5 kHz or 3.2 µs. When all states are
235
 * processed the interrupt is disabled and further
244
 * processed the interrupt is disabled and further
236
 * AD conversions are stopped.
245
 * AD conversions are stopped.
237
 *****************************************************/
246
 *****************************************************/
238
ISR(ADC_vect) {
247
ISR(ADC_vect) {
239
  static uint8_t ad_channel = AD_GYRO_PITCH, state = 0;
248
  static uint8_t ad_channel = AD_GYRO_PITCH, state = 0;
240
  static uint16_t sensorInputs[8] = {0,0,0,0,0,0,0,0};
249
  static uint16_t sensorInputs[8] = {0,0,0,0,0,0,0,0};
241
  static uint8_t pressure_wait = 10;
250
  static uint8_t pressure_wait = 10;
242
  uint8_t i, axis;
251
  uint8_t i, axis;
243
  int16_t range;
252
  int16_t range;
244
 
253
 
245
  // for various filters...
254
  // for various filters...
246
  int16_t tempOffsetGyro, tempGyro;
255
  int16_t tempOffsetGyro, tempGyro;
247
 
256
 
248
  sensorInputs[ad_channel] += ADC;
257
  sensorInputs[ad_channel] += ADC;
249
 
258
 
250
  /*
259
  /*
251
   * Actually we don't need this "switch". We could do all the sampling into the
260
   * Actually we don't need this "switch". We could do all the sampling into the
252
   * sensorInputs array first, and all the processing after the last sample.
261
   * sensorInputs array first, and all the processing after the last sample.
253
   */
262
   */
254
  switch(state++) {
263
  switch(state++) {
255
  case 7: // Z acc      
264
  case 7: // Z acc      
256
#ifdef ACC_REVERSE_ZAXIS
265
  if (ACC_REVERSED[Z])
257
    ZAcc = -ZAccOffset - sensorInputs[AD_ACC_Z];
266
    acc[Z] = accOffset[Z] - sensorInputs[AD_ACC_Z];
258
#else
267
  else
259
    ZAcc = sensorInputs[AD_ACC_Z] - ZAccOffset;
268
    acc[Z] = sensorInputs[AD_ACC_Z] - accOffset[Z];
260
#endif
-
 
261
    break;
269
  break;
262
   
270
   
263
  case 10: // yaw gyro
271
  case 10: // yaw gyro
264
    rawYawGyroSum = sensorInputs[AD_GYRO_YAW];
272
    rawGyroSum[YAW] = sensorInputs[AD_GYRO_YAW];
265
#ifdef GYRO_REVERSE_YAW
273
    if (GYRO_REVERSED[YAW])
266
    yawGyro = rawYawGyroSum - yawGyroOffset;
274
      yawGyro = gyroOffset[YAW] - sensorInputs[AD_GYRO_YAW];
267
#else
275
    else
268
    yawGyro = yawGyroOffset - rawYawGyroSum; // negative is "default" (FC 1.0-1.3).
-
 
269
#endif
276
      yawGyro = sensorInputs[AD_GYRO_YAW] - gyroOffset[YAW];
270
    break;
277
    break;
271
   
278
   
272
  case 11: // pitch axis acc.
279
  case 11: // pitch axis acc.
273
#ifdef ACC_REVERSE_PITCHAXIS
280
    if (ACC_REVERSED[PITCH])
274
    acc[PITCH] = -accOffset[PITCH] - sensorInputs[AD_ACC_PITCH];
281
      acc[PITCH] = accOffset[PITCH] - sensorInputs[AD_ACC_PITCH];
275
#else
282
    else
276
    acc[PITCH] = sensorInputs[AD_ACC_PITCH] - accOffset[PITCH];
283
      acc[PITCH] = sensorInputs[AD_ACC_PITCH] - accOffset[PITCH];
277
#endif
-
 
278
    filteredAcc[PITCH] = (filteredAcc[PITCH] * (ACC_FILTER-1) + acc[PITCH]) / ACC_FILTER;
-
 
-
 
284
 
279
 
285
    filteredAcc[PITCH] = (filteredAcc[PITCH] * (ACC_FILTER-1) + acc[PITCH]) / ACC_FILTER;
280
    measureNoise(acc[PITCH], &accNoisePeak[PITCH], 1);
286
    measureNoise(acc[PITCH], &accNoisePeak[PITCH], 1);
281
    break;
287
    break;
282
   
288
   
283
  case 12: // roll axis acc.
289
  case 12: // roll axis acc.
284
#ifdef ACC_REVERSE_ROLLAXIS
290
    if (ACC_REVERSED[ROLL])
285
    acc[ROLL] = sensorInputs[AD_ACC_ROLL] - accOffset[ROLL];
291
      acc[ROLL] = accOffset[ROLL] - sensorInputs[AD_ACC_ROLL];
286
#else
292
    else
287
    acc[ROLL] = -accOffset[ROLL] - sensorInputs[AD_ACC_ROLL];
293
      acc[ROLL] = sensorInputs[AD_ACC_ROLL] - accOffset[ROLL];
288
#endif
-
 
289
    filteredAcc[ROLL] = (filteredAcc[ROLL] * (ACC_FILTER-1) + acc[ROLL]) / ACC_FILTER;
294
    filteredAcc[ROLL] = (filteredAcc[ROLL] * (ACC_FILTER-1) + acc[ROLL]) / ACC_FILTER;
290
    measureNoise(acc[ROLL], &accNoisePeak[ROLL], 1);
295
    measureNoise(acc[ROLL], &accNoisePeak[ROLL], 1);
291
    break;
296
    break;
292
 
297
 
293
  case 13: // air pressure
298
  case 13: // air pressure
294
    if (pressure_wait) {
299
    if (pressure_wait) {
295
      // A range switch was done recently. Wait for steadying.
300
      // A range switch was done recently. Wait for steadying.
296
      pressure_wait--;
301
      pressure_wait--;
297
      break;
302
      break;
298
    }
303
    }
299
    range = OCR0A;
304
    range = OCR0A;
300
    rawAirPressure = sensorInputs[AD_AIRPRESSURE];
305
    rawAirPressure = sensorInputs[AD_AIRPRESSURE];
301
    if (rawAirPressure < MIN_RAWPRESSURE) {
306
    if (rawAirPressure < MIN_RAWPRESSURE) {
302
      // value is too low, so decrease voltage on the op amp minus input, making the value higher.
307
      // value is too low, so decrease voltage on the op amp minus input, making the value higher.
303
      range -= (MAX_RAWPRESSURE - rawAirPressure) / rangewidth - 1;
308
      range -= (MAX_RAWPRESSURE - rawAirPressure) / rangewidth - 1;
304
      if (range < 0) range = 0;
309
      if (range < 0) range = 0;
305
      pressure_wait = (OCR0A - range) * 4;
310
      pressure_wait = (OCR0A - range) * 4;
306
      OCR0A = range;
311
      OCR0A = range;
307
    } else if (rawAirPressure > MAX_RAWPRESSURE) {
312
    } else if (rawAirPressure > MAX_RAWPRESSURE) {
308
      // value is too high, so increase voltage on the op amp minus input, making the value lower.
313
      // value is too high, so increase voltage on the op amp minus input, making the value lower.
309
      range += (rawAirPressure - MIN_RAWPRESSURE) / rangewidth - 1;
314
      range += (rawAirPressure - MIN_RAWPRESSURE) / rangewidth - 1;
310
      if (range > 254) range = 254;
315
      if (range > 254) range = 254;
311
      pressure_wait = (range - OCR0A) * 4;
316
      pressure_wait = (range - OCR0A) * 4;
312
      OCR0A = range;
317
      OCR0A = range;
313
    } else {
318
    } else {
314
      filteredAirPressure = filterAirPressure(getAbsPressure(rawAirPressure));
319
      filteredAirPressure = filterAirPressure(getAbsPressure(rawAirPressure));
315
    }
320
    }
316
   
321
   
317
    DebugOut.Analog[12] = range;
322
    DebugOut.Analog[13] = range;
318
    DebugOut.Analog[13] = rawAirPressure;
323
    DebugOut.Analog[14] = rawAirPressure;
319
    DebugOut.Analog[14] = filteredAirPressure;
324
    DebugOut.Analog[15] = filteredAirPressure;
320
    break;
325
    break;
321
 
326
 
322
  case 14:
327
  case 14:
323
  case 15: // pitch or roll gyro.
328
  case 15: // pitch or roll gyro.
324
    axis = state - 15;
329
    axis = state - 15;
325
    tempGyro = rawGyroSum[axis] = sensorInputs[AD_GYRO_PITCH - axis];
330
    tempGyro = rawGyroSum[axis] = sensorInputs[AD_GYRO_PITCH - axis];
326
        // DebugOut.Analog[6 + 3 * axis ] = tempGyro;
331
        // DebugOut.Analog[6 + 3 * axis ] = tempGyro;
327
    /*
332
    /*
328
     * Process the gyro data for the PID controller.
333
     * Process the gyro data for the PID controller.
329
     */
334
     */
330
    // 1) Extrapolate: Near the ends of the range, we boost the input significantly. This simulates a
335
    // 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.
336
    //    gyro with a wider range, and helps counter saturation at full control.
332
 
337
 
333
    if (staticParams.GlobalConfig & CFG_ROTARY_RATE_LIMITER) {
338
    if (staticParams.GlobalConfig & CFG_ROTARY_RATE_LIMITER) {
334
      if (tempGyro < SENSOR_MIN_PITCHROLL) {
339
      if (tempGyro < SENSOR_MIN_PITCHROLL) {
335
        tempGyro = tempGyro * EXTRAPOLATION_SLOPE - EXTRAPOLATION_LIMIT;
340
        tempGyro = tempGyro * EXTRAPOLATION_SLOPE - EXTRAPOLATION_LIMIT;
336
      }
341
      }
337
      else if (tempGyro > SENSOR_MAX_PITCHROLL) {
342
      else if (tempGyro > SENSOR_MAX_PITCHROLL) {
338
        tempGyro = (tempGyro - SENSOR_MAX_PITCHROLL) * EXTRAPOLATION_SLOPE + SENSOR_MAX_PITCHROLL;
343
        tempGyro = (tempGyro - SENSOR_MAX_PITCHROLL) * EXTRAPOLATION_SLOPE + SENSOR_MAX_PITCHROLL;
339
      }
344
      }
340
    }
345
    }
341
 
346
 
342
    // 2) Apply sign and offset, scale before filtering.
347
    // 2) Apply sign and offset, scale before filtering.
343
    if (GYROS_REVERSE[axis]) {
348
    if (GYRO_REVERSED[axis]) {
344
      tempOffsetGyro = (gyroOffset[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL;
349
      tempOffsetGyro = (gyroOffset[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL;
345
    } else {
350
    } else {
346
      tempOffsetGyro = (tempGyro - gyroOffset[axis]) * GYRO_FACTOR_PITCHROLL;
351
      tempOffsetGyro = (tempGyro - gyroOffset[axis]) * GYRO_FACTOR_PITCHROLL;
347
    }
352
    }
348
 
353
 
349
    // 3) Scale and filter.
354
    // 3) Scale and filter.
350
    tempOffsetGyro = (gyro_PID[axis] * (GYROS_PIDFILTER-1) + tempOffsetGyro) / GYROS_PIDFILTER;
355
    tempOffsetGyro = (gyro_PID[axis] * (GYROS_PID_FILTER-1) + tempOffsetGyro) / GYROS_PID_FILTER;
351
 
356
 
352
    // 4) Measure noise.
357
    // 4) Measure noise.
353
    measureNoise(tempOffsetGyro, &gyroNoisePeak[axis], GYRO_NOISE_MEASUREMENT_DAMPING);
358
    measureNoise(tempOffsetGyro, &gyroNoisePeak[axis], GYRO_NOISE_MEASUREMENT_DAMPING);
354
 
359
 
355
    // 5) Differential measurement. 
360
    // 5) Differential measurement. 
356
    gyroD[axis] = (gyroD[axis] * (GYROS_DFILTER-1) + (tempOffsetGyro - gyro_PID[axis])) / GYROS_DFILTER;
361
    gyroD[axis] = (gyroD[axis] * (GYROS_D_FILTER-1) + (tempOffsetGyro - gyro_PID[axis])) / GYROS_D_FILTER;
357
 
362
 
358
    // 6) Done.
363
    // 6) Done.
359
    gyro_PID[axis] = tempOffsetGyro;
364
    gyro_PID[axis] = tempOffsetGyro;
360
 
365
 
361
    /*
366
    /*
362
     * Now process the data for attitude angles.
367
     * Now process the data for attitude angles.
363
     */
368
     */
364
    tempGyro = rawGyroSum[axis];
369
    tempGyro = rawGyroSum[axis];
365
   
370
   
366
    // 1) Apply sign and offset, scale before filtering.
371
    // 1) Apply sign and offset, scale before filtering.
367
    if (GYROS_REVERSE[axis]) {
372
    if (GYRO_REVERSED[axis]) {
368
      tempOffsetGyro = (gyroOffset[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL;
373
      tempOffsetGyro = (gyroOffset[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL;
369
    } else {
374
    } else {
370
      tempOffsetGyro = (tempGyro - gyroOffset[axis]) * GYRO_FACTOR_PITCHROLL;
375
      tempOffsetGyro = (tempGyro - gyroOffset[axis]) * GYRO_FACTOR_PITCHROLL;
371
    }
376
    }
372
   
377
   
373
    // 2) Filter.
378
    // 2) Filter.
374
    gyro_ATT[axis] = (gyro_ATT[axis] * (GYROS_INTEGRALFILTER-1) + tempOffsetGyro) / GYROS_INTEGRALFILTER;
379
    gyro_ATT[axis] = (gyro_ATT[axis] * (GYROS_ATT_FILTER-1) + tempOffsetGyro) / GYROS_ATT_FILTER;
375
    break;
380
    break;
376
   
381
   
377
  case 16:
382
  case 16:
378
    // battery
383
    // battery
379
    UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4;
384
    UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4;
380
    analogDataReady = 1; // mark
385
    analogDataReady = 1; // mark
381
    ADCycleCount++;
386
    ADCycleCount++;
382
    // Stop the sampling. Cycle is over.
387
    // Stop the sampling. Cycle is over.
383
    state = 0;
388
    state = 0;
384
    for (i=0; i<8; i++) {
389
    for (i=0; i<8; i++) {
385
      sensorInputs[i] = 0;
390
      sensorInputs[i] = 0;
386
    }
391
    }
387
    break;
392
    break;
388
  default: {} // do nothing.
393
  default: {} // do nothing.
389
  }
394
  }
390
 
395
 
391
  // set up for next state.
396
  // set up for next state.
392
  ad_channel = pgm_read_byte(&channelsForStates[state]);
397
  ad_channel = pgm_read_byte(&channelsForStates[state]);
393
  // ad_channel = channelsForStates[state];
398
  // ad_channel = channelsForStates[state];
394
     
399
     
395
  // set adc muxer to next ad_channel
400
  // set adc muxer to next ad_channel
396
  ADMUX = (ADMUX & 0xE0) | ad_channel;
401
  ADMUX = (ADMUX & 0xE0) | ad_channel;
397
  // after full cycle stop further interrupts
402
  // after full cycle stop further interrupts
398
  if(state) analog_start();
403
  if(state) analog_start();
399
}
404
}
400
 
405
 
401
void analog_calibrate(void) {
406
void analog_calibrate(void) {
402
#define GYRO_OFFSET_CYCLES 32
407
#define GYRO_OFFSET_CYCLES 32
403
  uint8_t i;
408
  uint8_t i, axis;
404
  int32_t _pitchOffset = 0, _rollOffset = 0, _yawOffset = 0;
409
  int32_t deltaOffsets[3] = {0,0,0};
-
 
410
  int16_t filteredDelta;
405
 
411
 
406
  // Set the filters... to be removed again, once some good settings are found.
412
  // Set the filters... to be removed again, once some good settings are found.
407
  GYROS_FIRSTORDERFILTER = (dynamicParams.UserParams[4]   & 0b00000011)       + 1;
413
  GYROS_PID_FILTER = (dynamicParams.UserParams[4]   & 0b00000011)       + 1;
408
  GYROS_SECONDORDERFILTER = ((dynamicParams.UserParams[4] & 0b00001100) >> 2) + 1;
414
  GYROS_ATT_FILTER = ((dynamicParams.UserParams[4]  & 0b00001100) >> 2) + 1;
409
  GYROS_DFILTER = ((dynamicParams.UserParams[4]           & 0b00110000) >> 4) + 1;
415
  GYROS_D_FILTER = ((dynamicParams.UserParams[4]    & 0b00110000) >> 4) + 1;
410
  ACC_FILTER = ((dynamicParams.UserParams[4]              & 0b11000000) >> 6) + 1;
-
 
411
 
-
 
412
  gyroOffset[PITCH] = gyroOffset[ROLL] = yawGyroOffset = 0;
416
  ACC_FILTER = ((dynamicParams.UserParams[4]        & 0b11000000) >> 6) + 1;
413
 
417
 
414
  gyro_calibrate();
418
  gyro_calibrate();
415
 
419
 
416
  // determine gyro bias by averaging (requires that the copter does not rotate around any axis!)
420
  // determine gyro bias by averaging (requires that the copter does not rotate around any axis!)
417
  for(i=0; i < GYRO_OFFSET_CYCLES; i++) {
421
  for(i=0; i < GYRO_OFFSET_CYCLES; i++) {
418
    Delay_ms_Mess(10);
422
    Delay_ms_Mess(10);
419
    _pitchOffset += rawGyroSum[PITCH];
423
    for (axis=0; axis<=YAW; axis++) {
420
    _rollOffset  += rawGyroSum[ROLL];
424
      deltaOffsets[axis] += rawGyroSum[axis] - gyroOffset[axis];
421
    _yawOffset   += rawYawGyroSum;
425
    }
422
  }
426
  }
423
 
427
 
424
  gyroOffset[PITCH] = (_pitchOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES;
428
  for (axis=0; axis<=YAW; axis++) {
425
  gyroOffset[ROLL] = (_rollOffset  + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES;
429
    filteredDelta = (deltaOffsets[axis] + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES;
426
  yawGyroOffset   = (_yawOffset   + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES;
430
    gyroOffset[axis] += filteredDelta;
427
 
431
  }
428
  gyro_PID[PITCH] = gyro_PID[ROLL] = 0;
-
 
429
  gyro_ATT[PITCH] = gyro_ATT[ROLL] = 0;
-
 
430
 
432
 
431
  // Noise is relative to offset. So, reset noise measurements when
433
  // Noise is relative to offset. So, reset noise measurements when changing offsets.
432
  // changing offsets.
-
 
433
  gyroNoisePeak[PITCH] = gyroNoisePeak[ROLL] = 0;
434
  gyroNoisePeak[PITCH] = gyroNoisePeak[ROLL] = 0;
434
 
435
 
435
  accOffset[PITCH] = (int16_t)GetParamWord(PID_ACC_PITCH);
436
  accOffset[PITCH] = GetParamWord(PID_ACC_PITCH);
436
  accOffset[ROLL]  = (int16_t)GetParamWord(PID_ACC_ROLL);
437
  accOffset[ROLL]  = GetParamWord(PID_ACC_ROLL);
-
 
438
  accOffset[Z]     = GetParamWord(PID_ACC_Z);
-
 
439
 
437
  ZAccOffset       = (int16_t)GetParamWord(PID_ACC_TOP);
440
  Delay_ms_Mess(100);
438
}
441
}
439
 
442
 
440
/*
443
/*
441
 * Find acc. offsets for a neutral reading, and write them to EEPROM.
444
 * Find acc. offsets for a neutral reading, and write them to EEPROM.
442
 * Does not (!} update the local variables. This must be done with a
445
 * Does not (!} update the local variables. This must be done with a
443
 * call to analog_calibrate() - this always (?) is done by the caller
446
 * call to analog_calibrate() - this always (?) is done by the caller
444
 * anyway. There would be nothing wrong with updating the variables
447
 * anyway. There would be nothing wrong with updating the variables
445
 * directly from here, though.
448
 * directly from here, though.
446
 */
449
 */
447
void analog_calibrateAcc(void) {
450
void analog_calibrateAcc(void) {
448
#define ACC_OFFSET_CYCLES 10
451
#define ACC_OFFSET_CYCLES 10
449
  uint8_t i;
452
  uint8_t i, axis;
450
  int32_t _pitchAxisOffset = 0, _rollAxisOffset = 0, _ZAxisOffset = 0;
453
  int32_t deltaOffset[3] = {0,0,0};
-
 
454
  int16_t filteredDelta;
451
  // int16_t pressureDiff, savedRawAirPressure;
455
  // int16_t pressureDiff, savedRawAirPressure;
452
 
-
 
453
  accOffset[PITCH] = accOffset[ROLL] = ZAccOffset = 0;
-
 
454
 
456
 
455
  for(i=0; i < ACC_OFFSET_CYCLES; i++) {
457
  for(i=0; i < ACC_OFFSET_CYCLES; i++) {
456
    Delay_ms_Mess(10);
458
    Delay_ms_Mess(10);
457
    _pitchAxisOffset += acc[PITCH];
459
    for (axis=0; axis<=YAW; axis++) {
458
    _rollAxisOffset  += acc[ROLL];
460
          deltaOffset[axis] += acc[axis];
-
 
461
        }
-
 
462
  }
-
 
463
 
459
    _ZAxisOffset += ZAcc;
464
  for (axis=0; axis<=YAW; axis++) {
-
 
465
    filteredDelta = (deltaOffset[axis] + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES;
-
 
466
    accOffset[axis] += ACC_REVERSED[axis] ? -filteredDelta : filteredDelta;
460
  }
467
  }
461
 
468
 
462
  // Save ACC neutral settings to eeprom
469
  // Save ACC neutral settings to eeprom
463
  SetParamWord(PID_ACC_PITCH, (uint16_t)((_pitchAxisOffset + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES));
470
  SetParamWord(PID_ACC_PITCH, accOffset[PITCH]);
464
  SetParamWord(PID_ACC_ROLL, (uint16_t)((_rollAxisOffset  + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES));
471
  SetParamWord(PID_ACC_ROLL,  accOffset[ROLL]);
465
  SetParamWord(PID_ACC_TOP,  (uint16_t)((_ZAxisOffset     + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES));
472
  SetParamWord(PID_ACC_Z,     accOffset[Z]);
466
 
473
 
467
  // Noise is relative to offset. So, reset noise measurements when
474
  // Noise is relative to offset. So, reset noise measurements when
468
  // changing offsets.
475
  // changing offsets.
469
  accNoisePeak[PITCH] = accNoisePeak[ROLL] = 0;
476
  accNoisePeak[PITCH] = accNoisePeak[ROLL] = 0;
-
 
477
 
470
  // Setting offset values has an influence in the analog.c ISR
478
  // Setting offset values has an influence in the analog.c ISR
471
  // Therefore run measurement for 100ms to achive stable readings
479
  // Therefore run measurement for 100ms to achive stable readings
472
  // Delay_ms_Mess(100);
480
  Delay_ms_Mess(100);
473
 
481
 
474
  // Set the feedback so that air pressure ends up in the middle of the range.
482
  // Set the feedback so that air pressure ends up in the middle of the range.
475
  // (raw pressure high --> OCR0A also high...)
483
  // (raw pressure high --> OCR0A also high...)
476
  // OCR0A += (rawAirPressure - 512) / rangewidth;
484
  // OCR0A += (rawAirPressure - 512) / rangewidth;
477
  // Delay_ms_Mess(500);
485
  // Delay_ms_Mess(500);
478
 
486
 
479
  /*
487
  /*
480
    pressureDiff = 0;
488
    pressureDiff = 0;
481
    DebugOut.Analog[16] = rawAirPressure;
489
    DebugOut.Analog[16] = rawAirPressure;
482
 
490
 
483
    #define PRESSURE_CAL_CYCLE_COUNT 2
491
    #define PRESSURE_CAL_CYCLE_COUNT 2
484
    for (i=0; i<PRESSURE_CAL_CYCLE_COUNT; i++) {
492
    for (i=0; i<PRESSURE_CAL_CYCLE_COUNT; i++) {
485
    savedRawAirPressure = rawAirPressure;
493
    savedRawAirPressure = rawAirPressure;
486
    OCR0A++;
494
    OCR0A++;
487
    Delay_ms_Mess(200);
495
    Delay_ms_Mess(200);
488
    // raw pressure will decrease.
496
    // raw pressure will decrease.
489
    pressureDiff += (savedRawAirPressure - rawAirPressure);
497
    pressureDiff += (savedRawAirPressure - rawAirPressure);
490
 
498
 
491
    savedRawAirPressure = rawAirPressure;
499
    savedRawAirPressure = rawAirPressure;
492
    OCR0A--;
500
    OCR0A--;
493
    Delay_ms_Mess(200);
501
    Delay_ms_Mess(200);
494
    // raw pressure will increase.
502
    // raw pressure will increase.
495
    pressureDiff += (rawAirPressure - savedRawAirPressure);
503
    pressureDiff += (rawAirPressure - savedRawAirPressure);
496
    }
504
    }
497
 
505
 
498
    DebugOut.Analog[15] = rangewidth =
506
    DebugOut.Analog[16] = rangewidth =
499
    (pressureDiff + PRESSURE_CAL_CYCLE_COUNT * 2 - 1) / (PRESSURE_CAL_CYCLE_COUNT * 2);
507
    (pressureDiff + PRESSURE_CAL_CYCLE_COUNT * 2 - 1) / (PRESSURE_CAL_CYCLE_COUNT * 2);
500
  */
508
  */
501
}
509
}
502
 
510