Rev 1961 | Rev 1964 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1961 | Rev 1963 | ||
---|---|---|---|
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 und 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 Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
18 | // + eindeutig als Ursprung verlinkt werden |
18 | // + eindeutig als Ursprung verlinkt und genannt 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 example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
35 | // + Commercial use (for example: 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 |
|
47 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
48 | // + 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 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
49 | // + POSSIBILITY OF SUCH DAMAGE. |
50 | // + POSSIBILITY OF SUCH DAMAGE. |
50 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
51 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
51 | #include <avr/io.h> |
52 | #include <avr/io.h> |
52 | #include <avr/interrupt.h> |
53 | #include <avr/interrupt.h> |
53 | #include <avr/pgmspace.h> |
54 | #include <avr/pgmspace.h> |
54 | 55 | ||
55 | #include "analog.h" |
56 | #include "analog.h" |
56 | #include "attitude.h" |
57 | #include "attitude.h" |
57 | #include "sensors.h" |
58 | #include "sensors.h" |
58 | 59 | ||
59 | // for Delay functions |
60 | // for Delay functions |
60 | #include "timer0.h" |
61 | #include "timer0.h" |
61 | 62 | ||
62 | // For debugOut |
63 | // For debugOut |
63 | #include "uart0.h" |
64 | #include "uart0.h" |
64 | 65 | ||
65 | // For reading and writing acc. meter offsets. |
66 | // For reading and writing acc. meter offsets. |
66 | #include "eeprom.h" |
67 | #include "eeprom.h" |
67 | 68 | ||
68 | // For debugOut.digital |
69 | // For debugOut.digital |
69 | #include "output.h" |
70 | #include "output.h" |
70 | 71 | ||
71 | // set ADC enable & ADC Start Conversion & ADC Interrupt Enable bit |
72 | // set ADC enable & ADC Start Conversion & ADC Interrupt Enable bit |
72 | #define startADC() (ADCSRA |= (1<<ADEN)|(1<<ADSC)|(1<<ADIE)) |
73 | #define startADC() (ADCSRA |= (1<<ADEN)|(1<<ADSC)|(1<<ADIE)) |
73 | 74 | ||
74 | /* |
75 | /* |
75 | * For each A/D conversion cycle, each analog channel is sampled a number of times |
76 | * For each A/D conversion cycle, each analog channel is sampled a number of times |
76 | * (see array channelsForStates), and the results for each channel are summed. |
77 | * (see array channelsForStates), and the results for each channel are summed. |
77 | * Here are those for the gyros and the acc. meters. They are not zero-offset. |
78 | * Here are those for the gyros and the acc. meters. They are not zero-offset. |
78 | * They are exported in the analog.h file - but please do not use them! The only |
79 | * They are exported in the analog.h file - but please do not use them! The only |
79 | * reason for the export is that the ENC-03_FC1.3 modules needs them for calibrating |
80 | * reason for the export is that the ENC-03_FC1.3 modules needs them for calibrating |
80 | * the offsets with the DAC. |
81 | * the offsets with the DAC. |
81 | */ |
82 | */ |
82 | volatile uint16_t sensorInputs[8]; |
83 | volatile uint16_t sensorInputs[8]; |
83 | volatile int16_t rawGyroSum[3]; |
84 | volatile int16_t rawGyroSum[3]; |
84 | volatile int16_t acc[3]; |
85 | volatile int16_t acc[3]; |
85 | volatile int16_t filteredAcc[2] = { 0,0 }; |
86 | volatile int16_t filteredAcc[2] = { 0,0 }; |
86 | // volatile int32_t stronglyFilteredAcc[3] = { 0,0,0 }; |
87 | // volatile int32_t stronglyFilteredAcc[3] = { 0,0,0 }; |
87 | 88 | ||
88 | /* |
89 | /* |
89 | * These 4 exported variables are zero-offset. The "PID" ones are used |
90 | * These 4 exported variables are zero-offset. The "PID" ones are used |
90 | * in the attitude control as rotation rates. The "ATT" ones are for |
91 | * in the attitude control as rotation rates. The "ATT" ones are for |
91 | * integration to angles. |
92 | * integration to angles. |
92 | */ |
93 | */ |
93 | volatile int16_t gyro_PID[2]; |
94 | volatile int16_t gyro_PID[2]; |
94 | volatile int16_t gyro_ATT[2]; |
95 | volatile int16_t gyro_ATT[2]; |
95 | volatile int16_t gyroD[2]; |
96 | volatile int16_t gyroD[2]; |
96 | volatile int16_t yawGyro; |
97 | volatile int16_t yawGyro; |
97 | 98 | ||
98 | /* |
99 | /* |
99 | * Offset values. These are the raw gyro and acc. meter sums when the copter is |
100 | * Offset values. These are the raw gyro and acc. meter sums when the copter is |
100 | * standing still. They are used for adjusting the gyro and acc. meter values |
101 | * standing still. They are used for adjusting the gyro and acc. meter values |
101 | * to be centered on zero. |
102 | * to be centered on zero. |
102 | */ |
103 | */ |
103 | 104 | ||
104 | sensorOffset_t gyroOffset; |
105 | sensorOffset_t gyroOffset; |
105 | sensorOffset_t accOffset; |
106 | sensorOffset_t accOffset; |
106 | 107 | ||
107 | /* |
108 | /* |
108 | * This allows some experimentation with the gyro filters. |
109 | * This allows some experimentation with the gyro filters. |
109 | * Should be replaced by #define's later on... |
110 | * Should be replaced by #define's later on... |
110 | */ |
111 | */ |
111 | 112 | ||
112 | /* |
113 | /* |
113 | * Air pressure |
114 | * Air pressure |
114 | */ |
115 | */ |
115 | volatile uint8_t rangewidth = 106; |
116 | volatile uint8_t rangewidth = 106; |
116 | 117 | ||
117 | // Direct from sensor, irrespective of range. |
118 | // Direct from sensor, irrespective of range. |
118 | // volatile uint16_t rawAirPressure; |
119 | // volatile uint16_t rawAirPressure; |
119 | 120 | ||
120 | // Value of 2 samples, with range. |
121 | // Value of 2 samples, with range. |
121 | volatile uint16_t simpleAirPressure; |
122 | volatile uint16_t simpleAirPressure; |
122 | 123 | ||
123 | // Value of AIRPRESSURE_SUMMATION_FACTOR samples, with range, filtered. |
124 | // Value of AIRPRESSURE_SUMMATION_FACTOR samples, with range, filtered. |
124 | volatile int32_t filteredAirPressure; |
125 | volatile int32_t filteredAirPressure; |
125 | 126 | ||
126 | // Partial sum of AIRPRESSURE_SUMMATION_FACTOR samples. |
127 | // Partial sum of AIRPRESSURE_SUMMATION_FACTOR samples. |
127 | volatile int32_t airPressureSum; |
128 | volatile int32_t airPressureSum; |
128 | 129 | ||
129 | // The number of samples summed into airPressureSum so far. |
130 | // The number of samples summed into airPressureSum so far. |
130 | volatile uint8_t pressureMeasurementCount; |
131 | volatile uint8_t pressureMeasurementCount; |
131 | 132 | ||
132 | /* |
133 | /* |
133 | * Battery voltage, in units of: 1k/11k / 3V * 1024 = 31.03 per volt. |
134 | * Battery voltage, in units of: 1k/11k / 3V * 1024 = 31.03 per volt. |
134 | * That is divided by 3 below, for a final 10.34 per volt. |
135 | * That is divided by 3 below, for a final 10.34 per volt. |
135 | * So the initial value of 100 is for 9.7 volts. |
136 | * So the initial value of 100 is for 9.7 volts. |
136 | */ |
137 | */ |
137 | volatile int16_t UBat = 100; |
138 | volatile int16_t UBat = 100; |
138 | 139 | ||
139 | /* |
140 | /* |
140 | * Control and status. |
141 | * Control and status. |
141 | */ |
142 | */ |
142 | volatile uint16_t ADCycleCount = 0; |
143 | volatile uint16_t ADCycleCount = 0; |
143 | volatile uint8_t analogDataReady = 1; |
144 | volatile uint8_t analogDataReady = 1; |
144 | 145 | ||
145 | /* |
146 | /* |
146 | * Experiment: Measuring vibration-induced sensor noise. |
147 | * Experiment: Measuring vibration-induced sensor noise. |
147 | */ |
148 | */ |
148 | volatile uint16_t gyroNoisePeak[2]; |
149 | volatile uint16_t gyroNoisePeak[2]; |
149 | volatile uint16_t accNoisePeak[2]; |
150 | volatile uint16_t accNoisePeak[2]; |
150 | 151 | ||
151 | // ADC channels |
152 | // ADC channels |
152 | #define AD_GYRO_YAW 0 |
153 | #define AD_GYRO_YAW 0 |
153 | #define AD_GYRO_ROLL 1 |
154 | #define AD_GYRO_ROLL 1 |
154 | #define AD_GYRO_PITCH 2 |
155 | #define AD_GYRO_PITCH 2 |
155 | #define AD_AIRPRESSURE 3 |
156 | #define AD_AIRPRESSURE 3 |
156 | #define AD_UBAT 4 |
157 | #define AD_UBAT 4 |
157 | #define AD_ACC_Z 5 |
158 | #define AD_ACC_Z 5 |
158 | #define AD_ACC_ROLL 6 |
159 | #define AD_ACC_ROLL 6 |
159 | #define AD_ACC_PITCH 7 |
160 | #define AD_ACC_PITCH 7 |
160 | 161 | ||
161 | /* |
162 | /* |
162 | * Table of AD converter inputs for each state. |
163 | * Table of AD converter inputs for each state. |
163 | * The number of samples summed for each channel is equal to |
164 | * The number of samples summed for each channel is equal to |
164 | * the number of times the channel appears in the array. |
165 | * the number of times the channel appears in the array. |
165 | * The max. number of samples that can be taken in 2 ms is: |
166 | * The max. number of samples that can be taken in 2 ms is: |
166 | * 20e6 / 128 / 13 / (1/2e-3) = 24. Since the main control |
167 | * 20e6 / 128 / 13 / (1/2e-3) = 24. Since the main control |
167 | * loop needs a little time between reading AD values and |
168 | * loop needs a little time between reading AD values and |
168 | * re-enabling ADC, the real limit is (how much?) lower. |
169 | * re-enabling ADC, the real limit is (how much?) lower. |
169 | * The acc. sensor is sampled even if not used - or installed |
170 | * The acc. sensor is sampled even if not used - or installed |
170 | * at all. The cost is not significant. |
171 | * at all. The cost is not significant. |
171 | */ |
172 | */ |
172 | 173 | ||
173 | const uint8_t channelsForStates[] PROGMEM = { |
174 | const uint8_t channelsForStates[] PROGMEM = { |
174 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_GYRO_YAW, |
175 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_GYRO_YAW, |
175 | AD_ACC_PITCH, AD_ACC_ROLL, AD_AIRPRESSURE, |
176 | AD_ACC_PITCH, AD_ACC_ROLL, AD_AIRPRESSURE, |
176 | 177 | ||
177 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_ACC_Z, // at 8, measure Z acc. |
178 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_ACC_Z, // at 8, measure Z acc. |
178 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_GYRO_YAW, // at 11, finish yaw gyro |
179 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_GYRO_YAW, // at 11, finish yaw gyro |
179 | 180 | ||
180 | AD_ACC_PITCH, // at 12, finish pitch axis acc. |
181 | AD_ACC_PITCH, // at 12, finish pitch axis acc. |
181 | AD_ACC_ROLL, // at 13, finish roll axis acc. |
182 | AD_ACC_ROLL, // at 13, finish roll axis acc. |
182 | AD_AIRPRESSURE, // at 14, finish air pressure. |
183 | AD_AIRPRESSURE, // at 14, finish air pressure. |
183 | 184 | ||
184 | AD_GYRO_PITCH, // at 15, finish pitch gyro |
185 | AD_GYRO_PITCH, // at 15, finish pitch gyro |
185 | AD_GYRO_ROLL, // at 16, finish roll gyro |
186 | AD_GYRO_ROLL, // at 16, finish roll gyro |
186 | AD_UBAT // at 17, measure battery. |
187 | AD_UBAT // at 17, measure battery. |
187 | }; |
188 | }; |
188 | 189 | ||
189 | // Feature removed. Could be reintroduced later - but should work for all gyro types then. |
190 | // Feature removed. Could be reintroduced later - but should work for all gyro types then. |
190 | // uint8_t GyroDefectPitch = 0, GyroDefectRoll = 0, GyroDefectYaw = 0; |
191 | // uint8_t GyroDefectPitch = 0, GyroDefectRoll = 0, GyroDefectYaw = 0; |
191 | 192 | ||
192 | void analog_init(void) { |
193 | void analog_init(void) { |
193 | uint8_t sreg = SREG; |
194 | uint8_t sreg = SREG; |
194 | // disable all interrupts before reconfiguration |
195 | // disable all interrupts before reconfiguration |
195 | cli(); |
196 | cli(); |
196 | 197 | ||
197 | //ADC0 ... ADC7 is connected to PortA pin 0 ... 7 |
198 | //ADC0 ... ADC7 is connected to PortA pin 0 ... 7 |
198 | DDRA = 0x00; |
199 | DDRA = 0x00; |
199 | PORTA = 0x00; |
200 | PORTA = 0x00; |
200 | // Digital Input Disable Register 0 |
201 | // Digital Input Disable Register 0 |
201 | // Disable digital input buffer for analog adc_channel pins |
202 | // Disable digital input buffer for analog adc_channel pins |
202 | DIDR0 = 0xFF; |
203 | DIDR0 = 0xFF; |
203 | // external reference, adjust data to the right |
204 | // external reference, adjust data to the right |
204 | ADMUX &= ~((1<<REFS1)|(1<<REFS0)|(1<<ADLAR)); |
205 | ADMUX &= ~((1<<REFS1)|(1<<REFS0)|(1<<ADLAR)); |
205 | // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice) |
206 | // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice) |
206 | ADMUX = (ADMUX & 0xE0) | channelsForStates[0]; |
207 | ADMUX = (ADMUX & 0xE0) | channelsForStates[0]; |
207 | //Set ADC Control and Status Register A |
208 | //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 | //Auto Trigger Enable, Prescaler Select Bits to Division Factor 128, i.e. ADC clock = SYSCKL/128 = 156.25 kHz |
209 | ADCSRA = (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); |
210 | ADCSRA = (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); |
210 | //Set ADC Control and Status Register B |
211 | //Set ADC Control and Status Register B |
211 | //Trigger Source to Free Running Mode |
212 | //Trigger Source to Free Running Mode |
212 | ADCSRB &= ~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)); |
213 | ADCSRB &= ~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)); |
213 | 214 | ||
214 | startAnalogConversionCycle(); |
215 | startAnalogConversionCycle(); |
215 | 216 | ||
216 | // restore global interrupt flags |
217 | // restore global interrupt flags |
217 | SREG = sreg; |
218 | SREG = sreg; |
218 | } |
219 | } |
219 | 220 | ||
220 | void measureNoise(const int16_t sensor, |
221 | void measureNoise(const int16_t sensor, |
221 | volatile uint16_t* const noiseMeasurement, const uint8_t damping) { |
222 | volatile uint16_t* const noiseMeasurement, const uint8_t damping) { |
222 | if (sensor > (int16_t) (*noiseMeasurement)) { |
223 | if (sensor > (int16_t) (*noiseMeasurement)) { |
223 | *noiseMeasurement = sensor; |
224 | *noiseMeasurement = sensor; |
224 | } else if (-sensor > (int16_t) (*noiseMeasurement)) { |
225 | } else if (-sensor > (int16_t) (*noiseMeasurement)) { |
225 | *noiseMeasurement = -sensor; |
226 | *noiseMeasurement = -sensor; |
226 | } else if (*noiseMeasurement > damping) { |
227 | } else if (*noiseMeasurement > damping) { |
227 | *noiseMeasurement -= damping; |
228 | *noiseMeasurement -= damping; |
228 | } else { |
229 | } else { |
229 | *noiseMeasurement = 0; |
230 | *noiseMeasurement = 0; |
230 | } |
231 | } |
231 | } |
232 | } |
232 | 233 | ||
233 | /* |
234 | /* |
234 | * Min.: 0 |
235 | * Min.: 0 |
235 | * Max: About 106 * 240 + 2047 = 27487; it is OK with just a 16 bit type. |
236 | * Max: About 106 * 240 + 2047 = 27487; it is OK with just a 16 bit type. |
236 | */ |
237 | */ |
237 | uint16_t getSimplePressure(int advalue) { |
238 | uint16_t getSimplePressure(int advalue) { |
238 | return (uint16_t) OCR0A * (uint16_t) rangewidth + advalue; |
239 | return (uint16_t) OCR0A * (uint16_t) rangewidth + advalue; |
239 | } |
240 | } |
240 | 241 | ||
241 | void startAnalogConversionCycle(void) { |
242 | void startAnalogConversionCycle(void) { |
242 | analogDataReady = 0; |
243 | analogDataReady = 0; |
243 | // Stop the sampling. Cycle is over. |
244 | // Stop the sampling. Cycle is over. |
244 | for (uint8_t i = 0; i < 8; i++) { |
245 | for (uint8_t i = 0; i < 8; i++) { |
245 | sensorInputs[i] = 0; |
246 | sensorInputs[i] = 0; |
246 | } |
247 | } |
247 | ADMUX = (ADMUX & 0xE0) | channelsForStates[0]; |
248 | ADMUX = (ADMUX & 0xE0) | channelsForStates[0]; |
248 | startADC(); |
249 | startADC(); |
249 | } |
250 | } |
250 | 251 | ||
251 | /***************************************************** |
252 | /***************************************************** |
252 | * Interrupt Service Routine for ADC |
253 | * Interrupt Service Routine for ADC |
253 | * Runs at 312.5 kHz or 3.2 µs. When all states are |
254 | * Runs at 312.5 kHz or 3.2 �s. When all states are |
254 | * processed further conversions are stopped. |
255 | * processed further conversions are stopped. |
255 | *****************************************************/ |
256 | *****************************************************/ |
256 | ISR(ADC_vect) { |
257 | ISR(ADC_vect) { |
257 | static uint8_t ad_channel = AD_GYRO_PITCH, state = 0; |
258 | static uint8_t ad_channel = AD_GYRO_PITCH, state = 0; |
258 | sensorInputs[ad_channel] += ADC; |
259 | sensorInputs[ad_channel] += ADC; |
259 | // set up for next state. |
260 | // set up for next state. |
260 | state++; |
261 | state++; |
261 | if (state < 18) { |
262 | if (state < 18) { |
262 | ad_channel = pgm_read_byte(&channelsForStates[state]); |
263 | ad_channel = pgm_read_byte(&channelsForStates[state]); |
263 | // set adc muxer to next ad_channel |
264 | // set adc muxer to next ad_channel |
264 | ADMUX = (ADMUX & 0xE0) | ad_channel; |
265 | ADMUX = (ADMUX & 0xE0) | ad_channel; |
265 | // after full cycle stop further interrupts |
266 | // after full cycle stop further interrupts |
266 | startADC(); |
267 | startADC(); |
267 | } else { |
268 | } else { |
268 | state = 0; |
269 | state = 0; |
269 | ADCycleCount++; |
270 | ADCycleCount++; |
270 | analogDataReady = 1; |
271 | analogDataReady = 1; |
271 | // do not restart ADC converter. |
272 | // do not restart ADC converter. |
272 | } |
273 | } |
273 | } |
274 | } |
274 | 275 | ||
275 | void analog_updateGyros(void) { |
276 | void analog_updateGyros(void) { |
276 | // for various filters... |
277 | // for various filters... |
277 | int16_t tempOffsetGyro, tempGyro; |
278 | int16_t tempOffsetGyro, tempGyro; |
278 | 279 | ||
279 | for (uint8_t axis=0; axis<2; axis++) { |
280 | for (uint8_t axis=0; axis<2; axis++) { |
280 | tempGyro = rawGyroSum[axis] = sensorInputs[AD_GYRO_PITCH-axis]; |
281 | tempGyro = rawGyroSum[axis] = sensorInputs[AD_GYRO_PITCH-axis]; |
281 | /* |
282 | /* |
282 | * Process the gyro data for the PID controller. |
283 | * Process the gyro data for the PID controller. |
283 | */ |
284 | */ |
284 | // 1) Extrapolate: Near the ends of the range, we boost the input significantly. This simulates a |
285 | // 1) Extrapolate: Near the ends of the range, we boost the input significantly. This simulates a |
285 | // gyro with a wider range, and helps counter saturation at full control. |
286 | // gyro with a wider range, and helps counter saturation at full control. |
286 | 287 | ||
287 | if (staticParams.bitConfig & CFG_GYRO_SATURATION_PREVENTION) { |
288 | if (staticParams.bitConfig & CFG_GYRO_SATURATION_PREVENTION) { |
288 | if (tempGyro < SENSOR_MIN_PITCHROLL) { |
289 | if (tempGyro < SENSOR_MIN_PITCHROLL) { |
289 | debugOut.digital[0] |= DEBUG_SENSORLIMIT; |
290 | debugOut.digital[0] |= DEBUG_SENSORLIMIT; |
290 | tempGyro = tempGyro * EXTRAPOLATION_SLOPE - EXTRAPOLATION_LIMIT; |
291 | tempGyro = tempGyro * EXTRAPOLATION_SLOPE - EXTRAPOLATION_LIMIT; |
291 | } else if (tempGyro > SENSOR_MAX_PITCHROLL) { |
292 | } else if (tempGyro > SENSOR_MAX_PITCHROLL) { |
292 | debugOut.digital[0] |= DEBUG_SENSORLIMIT; |
293 | debugOut.digital[0] |= DEBUG_SENSORLIMIT; |
293 | tempGyro = (tempGyro - SENSOR_MAX_PITCHROLL) * EXTRAPOLATION_SLOPE |
294 | tempGyro = (tempGyro - SENSOR_MAX_PITCHROLL) * EXTRAPOLATION_SLOPE |
294 | + SENSOR_MAX_PITCHROLL; |
295 | + SENSOR_MAX_PITCHROLL; |
295 | } else { |
296 | } else { |
296 | debugOut.digital[0] &= ~DEBUG_SENSORLIMIT; |
297 | debugOut.digital[0] &= ~DEBUG_SENSORLIMIT; |
297 | } |
298 | } |
298 | } |
299 | } |
299 | 300 | ||
300 | // 2) Apply sign and offset, scale before filtering. |
301 | // 2) Apply sign and offset, scale before filtering. |
301 | if (GYRO_REVERSED[axis]) { |
302 | if (GYRO_REVERSED[axis]) { |
302 | tempOffsetGyro = (gyroOffset.offsets[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL; |
303 | tempOffsetGyro = (gyroOffset.offsets[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL; |
303 | } else { |
304 | } else { |
304 | tempOffsetGyro = (tempGyro - gyroOffset.offsets[axis]) * GYRO_FACTOR_PITCHROLL; |
305 | tempOffsetGyro = (tempGyro - gyroOffset.offsets[axis]) * GYRO_FACTOR_PITCHROLL; |
305 | } |
306 | } |
306 | 307 | ||
307 | // 3) Scale and filter. |
308 | // 3) Scale and filter. |
308 | tempOffsetGyro = (gyro_PID[axis] * (staticParams.gyroPIDFilterConstant - 1) + tempOffsetGyro) / staticParams.gyroPIDFilterConstant; |
309 | tempOffsetGyro = (gyro_PID[axis] * (staticParams.gyroPIDFilterConstant - 1) + tempOffsetGyro) / staticParams.gyroPIDFilterConstant; |
309 | 310 | ||
310 | // 4) Measure noise. |
311 | // 4) Measure noise. |
311 | measureNoise(tempOffsetGyro, &gyroNoisePeak[axis], GYRO_NOISE_MEASUREMENT_DAMPING); |
312 | measureNoise(tempOffsetGyro, &gyroNoisePeak[axis], GYRO_NOISE_MEASUREMENT_DAMPING); |
312 | 313 | ||
313 | // 5) Differential measurement. |
314 | // 5) Differential measurement. |
314 | gyroD[axis] = (gyroD[axis] * (staticParams.gyroDFilterConstant - 1) + (tempOffsetGyro - gyro_PID[axis])) / staticParams.gyroDFilterConstant; |
315 | gyroD[axis] = (gyroD[axis] * (staticParams.gyroDFilterConstant - 1) + (tempOffsetGyro - gyro_PID[axis])) / staticParams.gyroDFilterConstant; |
315 | 316 | ||
316 | // 6) Done. |
317 | // 6) Done. |
317 | gyro_PID[axis] = tempOffsetGyro; |
318 | gyro_PID[axis] = tempOffsetGyro; |
318 | 319 | ||
319 | /* |
320 | /* |
320 | * Now process the data for attitude angles. |
321 | * Now process the data for attitude angles. |
321 | */ |
322 | */ |
322 | tempGyro = rawGyroSum[axis]; |
323 | tempGyro = rawGyroSum[axis]; |
323 | 324 | ||
324 | // 1) Apply sign and offset, scale before filtering. |
325 | // 1) Apply sign and offset, scale before filtering. |
325 | if (GYRO_REVERSED[axis]) { |
326 | if (GYRO_REVERSED[axis]) { |
326 | tempOffsetGyro = (gyroOffset.offsets[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL; |
327 | tempOffsetGyro = (gyroOffset.offsets[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL; |
327 | } else { |
328 | } else { |
328 | tempOffsetGyro = (tempGyro - gyroOffset.offsets[axis]) * GYRO_FACTOR_PITCHROLL; |
329 | tempOffsetGyro = (tempGyro - gyroOffset.offsets[axis]) * GYRO_FACTOR_PITCHROLL; |
329 | } |
330 | } |
330 | 331 | ||
331 | // 2) Filter. |
332 | // 2) Filter. |
332 | gyro_ATT[axis] = (gyro_ATT[axis] * (staticParams.gyroATTFilterConstant - 1) + tempOffsetGyro) / staticParams.gyroATTFilterConstant; |
333 | gyro_ATT[axis] = (gyro_ATT[axis] * (staticParams.gyroATTFilterConstant - 1) + tempOffsetGyro) / staticParams.gyroATTFilterConstant; |
333 | } |
334 | } |
334 | 335 | ||
335 | // Yaw gyro. |
336 | // Yaw gyro. |
336 | rawGyroSum[YAW] = sensorInputs[AD_GYRO_YAW]; |
337 | rawGyroSum[YAW] = sensorInputs[AD_GYRO_YAW]; |
337 | if (GYRO_REVERSED[YAW]) |
338 | if (GYRO_REVERSED[YAW]) |
338 | yawGyro = gyroOffset.offsets[YAW] - sensorInputs[AD_GYRO_YAW]; |
339 | yawGyro = gyroOffset.offsets[YAW] - sensorInputs[AD_GYRO_YAW]; |
339 | else |
340 | else |
340 | yawGyro = sensorInputs[AD_GYRO_YAW] - gyroOffset.offsets[YAW]; |
341 | yawGyro = sensorInputs[AD_GYRO_YAW] - gyroOffset.offsets[YAW]; |
341 | 342 | ||
342 | debugOut.analog[3] = gyro_ATT[PITCH]; |
343 | debugOut.analog[3] = gyro_ATT[PITCH]; |
343 | debugOut.analog[4] = gyro_ATT[ROLL]; |
344 | debugOut.analog[4] = gyro_ATT[ROLL]; |
344 | debugOut.analog[5] = yawGyro; |
345 | debugOut.analog[5] = yawGyro; |
345 | } |
346 | } |
346 | 347 | ||
347 | void analog_updateAccelerometers(void) { |
348 | void analog_updateAccelerometers(void) { |
348 | // Pitch and roll axis accelerations. |
349 | // Pitch and roll axis accelerations. |
349 | for (uint8_t axis=0; axis<2; axis++) { |
350 | for (uint8_t axis=0; axis<2; axis++) { |
350 | if (ACC_REVERSED[axis]) |
351 | if (ACC_REVERSED[axis]) |
351 | acc[axis] = accOffset.offsets[axis] - sensorInputs[AD_ACC_PITCH-axis]; |
352 | acc[axis] = accOffset.offsets[axis] - sensorInputs[AD_ACC_PITCH-axis]; |
352 | else |
353 | else |
353 | acc[axis] = sensorInputs[AD_ACC_PITCH-axis] - accOffset.offsets[axis]; |
354 | acc[axis] = sensorInputs[AD_ACC_PITCH-axis] - accOffset.offsets[axis]; |
354 | 355 | ||
355 | filteredAcc[axis] = (filteredAcc[axis] * (staticParams.accFilterConstant - 1) + acc[axis]) / staticParams.accFilterConstant; |
356 | filteredAcc[axis] = (filteredAcc[axis] * (staticParams.accFilterConstant - 1) + acc[axis]) / staticParams.accFilterConstant; |
356 | 357 | ||
357 | /* |
358 | /* |
358 | stronglyFilteredAcc[PITCH] = |
359 | stronglyFilteredAcc[PITCH] = |
359 | (stronglyFilteredAcc[PITCH] * 99 + acc[PITCH] * 10) / 100; |
360 | (stronglyFilteredAcc[PITCH] * 99 + acc[PITCH] * 10) / 100; |
360 | */ |
361 | */ |
361 | 362 | ||
362 | measureNoise(acc[axis], &accNoisePeak[axis], 1); |
363 | measureNoise(acc[axis], &accNoisePeak[axis], 1); |
363 | } |
364 | } |
364 | 365 | ||
365 | // Z acc. |
366 | // Z acc. |
366 | if (ACC_REVERSED[Z]) |
367 | if (ACC_REVERSED[Z]) |
367 | acc[Z] = accOffset.offsets[Z] - sensorInputs[AD_ACC_Z]; |
368 | acc[Z] = accOffset.offsets[Z] - sensorInputs[AD_ACC_Z]; |
368 | else |
369 | else |
369 | acc[Z] = sensorInputs[AD_ACC_Z] - accOffset.offsets[Z]; |
370 | acc[Z] = sensorInputs[AD_ACC_Z] - accOffset.offsets[Z]; |
370 | 371 | ||
371 | /* |
372 | /* |
372 | stronglyFilteredAcc[Z] = |
373 | stronglyFilteredAcc[Z] = |
373 | (stronglyFilteredAcc[Z] * 99 + acc[Z] * 10) / 100; |
374 | (stronglyFilteredAcc[Z] * 99 + acc[Z] * 10) / 100; |
374 | */ |
375 | */ |
375 | } |
376 | } |
376 | 377 | ||
377 | void analog_updateAirPressure(void) { |
378 | void analog_updateAirPressure(void) { |
378 | static uint16_t pressureAutorangingWait = 25; |
379 | static uint16_t pressureAutorangingWait = 25; |
379 | uint16_t rawAirPressure; |
380 | uint16_t rawAirPressure; |
380 | int16_t newrange; |
381 | int16_t newrange; |
381 | // air pressure |
382 | // air pressure |
382 | if (pressureAutorangingWait) { |
383 | if (pressureAutorangingWait) { |
383 | //A range switch was done recently. Wait for steadying. |
384 | //A range switch was done recently. Wait for steadying. |
384 | pressureAutorangingWait--; |
385 | pressureAutorangingWait--; |
385 | debugOut.analog[27] = (uint16_t) OCR0A; |
386 | debugOut.analog[27] = (uint16_t) OCR0A; |
386 | debugOut.analog[31] = simpleAirPressure; |
387 | debugOut.analog[31] = simpleAirPressure; |
387 | } else { |
388 | } else { |
388 | rawAirPressure = sensorInputs[AD_AIRPRESSURE]; |
389 | rawAirPressure = sensorInputs[AD_AIRPRESSURE]; |
389 | if (rawAirPressure < MIN_RAWPRESSURE) { |
390 | if (rawAirPressure < MIN_RAWPRESSURE) { |
390 | // value is too low, so decrease voltage on the op amp minus input, making the value higher. |
391 | // value is too low, so decrease voltage on the op amp minus input, making the value higher. |
391 | newrange = OCR0A - (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (MAX_RAWPRESSURE - rawAirPressure) / (rangewidth * 2) + 1; |
392 | newrange = OCR0A - (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (MAX_RAWPRESSURE - rawAirPressure) / (rangewidth * 2) + 1; |
392 | if (newrange > MIN_RANGES_EXTRAPOLATION) { |
393 | if (newrange > MIN_RANGES_EXTRAPOLATION) { |
393 | pressureAutorangingWait = (OCR0A - newrange) * AUTORANGE_WAIT_FACTOR; // = OCRA0 - OCRA0 + |
394 | pressureAutorangingWait = (OCR0A - newrange) * AUTORANGE_WAIT_FACTOR; // = OCRA0 - OCRA0 + |
394 | OCR0A = newrange; |
395 | OCR0A = newrange; |
395 | } else { |
396 | } else { |
396 | if (OCR0A) { |
397 | if (OCR0A) { |
397 | OCR0A--; |
398 | OCR0A--; |
398 | pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
399 | pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
399 | } |
400 | } |
400 | } |
401 | } |
401 | } else if (rawAirPressure > MAX_RAWPRESSURE) { |
402 | } else if (rawAirPressure > MAX_RAWPRESSURE) { |
402 | // value is too high, so increase voltage on the op amp minus input, making the value lower. |
403 | // value is too high, so increase voltage on the op amp minus input, making the value lower. |
403 | // If near the end, make a limited increase |
404 | // If near the end, make a limited increase |
404 | newrange = OCR0A + (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (rawAirPressure - MIN_RAWPRESSURE) / (rangewidth * 2) - 1; |
405 | newrange = OCR0A + (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (rawAirPressure - MIN_RAWPRESSURE) / (rangewidth * 2) - 1; |
405 | if (newrange < MAX_RANGES_EXTRAPOLATION) { |
406 | if (newrange < MAX_RANGES_EXTRAPOLATION) { |
406 | pressureAutorangingWait = (newrange - OCR0A) * AUTORANGE_WAIT_FACTOR; |
407 | pressureAutorangingWait = (newrange - OCR0A) * AUTORANGE_WAIT_FACTOR; |
407 | OCR0A = newrange; |
408 | OCR0A = newrange; |
408 | } else { |
409 | } else { |
409 | if (OCR0A < 254) { |
410 | if (OCR0A < 254) { |
410 | OCR0A++; |
411 | OCR0A++; |
411 | pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
412 | pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
412 | } |
413 | } |
413 | } |
414 | } |
414 | } |
415 | } |
415 | 416 | ||
416 | // Even if the sample is off-range, use it. |
417 | // Even if the sample is off-range, use it. |
417 | simpleAirPressure = getSimplePressure(rawAirPressure); |
418 | simpleAirPressure = getSimplePressure(rawAirPressure); |
418 | debugOut.analog[27] = (uint16_t) OCR0A; |
419 | debugOut.analog[27] = (uint16_t) OCR0A; |
419 | debugOut.analog[31] = simpleAirPressure; |
420 | debugOut.analog[31] = simpleAirPressure; |
420 | 421 | ||
421 | if (simpleAirPressure < MIN_RANGES_EXTRAPOLATION * rangewidth) { |
422 | if (simpleAirPressure < MIN_RANGES_EXTRAPOLATION * rangewidth) { |
422 | // Danger: pressure near lower end of range. If the measurement saturates, the |
423 | // Danger: pressure near lower end of range. If the measurement saturates, the |
423 | // copter may climb uncontrolledly... Simulate a drastic reduction in pressure. |
424 | // copter may climb uncontrolledly... Simulate a drastic reduction in pressure. |
424 | debugOut.digital[1] |= DEBUG_SENSORLIMIT; |
425 | debugOut.digital[1] |= DEBUG_SENSORLIMIT; |
425 | airPressureSum += (int16_t) MIN_RANGES_EXTRAPOLATION * rangewidth |
426 | airPressureSum += (int16_t) MIN_RANGES_EXTRAPOLATION * rangewidth |
426 | + (simpleAirPressure - (int16_t) MIN_RANGES_EXTRAPOLATION |
427 | + (simpleAirPressure - (int16_t) MIN_RANGES_EXTRAPOLATION |
427 | * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
428 | * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
428 | } else if (simpleAirPressure > MAX_RANGES_EXTRAPOLATION * rangewidth) { |
429 | } else if (simpleAirPressure > MAX_RANGES_EXTRAPOLATION * rangewidth) { |
429 | // Danger: pressure near upper end of range. If the measurement saturates, the |
430 | // Danger: pressure near upper end of range. If the measurement saturates, the |
430 | // copter may descend uncontrolledly... Simulate a drastic increase in pressure. |
431 | // copter may descend uncontrolledly... Simulate a drastic increase in pressure. |
431 | debugOut.digital[1] |= DEBUG_SENSORLIMIT; |
432 | debugOut.digital[1] |= DEBUG_SENSORLIMIT; |
432 | airPressureSum += (int16_t) MAX_RANGES_EXTRAPOLATION * rangewidth |
433 | airPressureSum += (int16_t) MAX_RANGES_EXTRAPOLATION * rangewidth |
433 | + (simpleAirPressure - (int16_t) MAX_RANGES_EXTRAPOLATION |
434 | + (simpleAirPressure - (int16_t) MAX_RANGES_EXTRAPOLATION |
434 | * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
435 | * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
435 | } else { |
436 | } else { |
436 | // normal case. |
437 | // normal case. |
437 | // If AIRPRESSURE_SUMMATION_FACTOR is an odd number we only want to add half the double sample. |
438 | // If AIRPRESSURE_SUMMATION_FACTOR is an odd number we only want to add half the double sample. |
438 | // The 2 cases above (end of range) are ignored for this. |
439 | // The 2 cases above (end of range) are ignored for this. |
439 | debugOut.digital[1] &= ~DEBUG_SENSORLIMIT; |
440 | debugOut.digital[1] &= ~DEBUG_SENSORLIMIT; |
440 | if (pressureMeasurementCount == AIRPRESSURE_SUMMATION_FACTOR - 1) |
441 | if (pressureMeasurementCount == AIRPRESSURE_SUMMATION_FACTOR - 1) |
441 | airPressureSum += simpleAirPressure / 2; |
442 | airPressureSum += simpleAirPressure / 2; |
442 | else |
443 | else |
443 | airPressureSum += simpleAirPressure; |
444 | airPressureSum += simpleAirPressure; |
444 | } |
445 | } |
445 | 446 | ||
446 | // 2 samples were added. |
447 | // 2 samples were added. |
447 | pressureMeasurementCount += 2; |
448 | pressureMeasurementCount += 2; |
448 | if (pressureMeasurementCount >= AIRPRESSURE_SUMMATION_FACTOR) { |
449 | if (pressureMeasurementCount >= AIRPRESSURE_SUMMATION_FACTOR) { |
449 | filteredAirPressure = (filteredAirPressure * (AIRPRESSURE_FILTER - 1) |
450 | filteredAirPressure = (filteredAirPressure * (AIRPRESSURE_FILTER - 1) |
450 | + airPressureSum + AIRPRESSURE_FILTER / 2) / AIRPRESSURE_FILTER; |
451 | + airPressureSum + AIRPRESSURE_FILTER / 2) / AIRPRESSURE_FILTER; |
451 | pressureMeasurementCount = airPressureSum = 0; |
452 | pressureMeasurementCount = airPressureSum = 0; |
452 | } |
453 | } |
453 | } |
454 | } |
454 | } |
455 | } |
455 | 456 | ||
456 | void analog_updateBatteryVoltage(void) { |
457 | void analog_updateBatteryVoltage(void) { |
457 | // Battery. The measured value is: (V * 1k/11k)/3v * 1024 = 31.03 counts per volt (max. measurable is 33v). |
458 | // Battery. The measured value is: (V * 1k/11k)/3v * 1024 = 31.03 counts per volt (max. measurable is 33v). |
458 | // This is divided by 3 --> 10.34 counts per volt. |
459 | // This is divided by 3 --> 10.34 counts per volt. |
459 | UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4; |
460 | UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4; |
460 | debugOut.analog[11] = UBat; |
461 | debugOut.analog[11] = UBat; |
461 | } |
462 | } |
462 | 463 | ||
463 | void analog_update(void) { |
464 | void analog_update(void) { |
464 | analog_updateGyros(); |
465 | analog_updateGyros(); |
465 | analog_updateAccelerometers(); |
466 | analog_updateAccelerometers(); |
466 | analog_updateAirPressure(); |
467 | analog_updateAirPressure(); |
467 | analog_updateBatteryVoltage(); |
468 | analog_updateBatteryVoltage(); |
468 | } |
469 | } |
469 | 470 | ||
470 | void analog_setNeutral() { |
471 | void analog_setNeutral() { |
471 | if (gyroOffset_readFromEEProm()) { |
472 | if (gyroOffset_readFromEEProm()) { |
472 | gyroOffset.offsets[PITCH] = gyroOffset.offsets[ROLL] = 512 * GYRO_SUMMATION_FACTOR_PITCHROLL; |
473 | gyroOffset.offsets[PITCH] = gyroOffset.offsets[ROLL] = 512 * GYRO_SUMMATION_FACTOR_PITCHROLL; |
473 | gyroOffset.offsets[YAW] = 512 * GYRO_SUMMATION_FACTOR_YAW; |
474 | gyroOffset.offsets[YAW] = 512 * GYRO_SUMMATION_FACTOR_YAW; |
474 | } |
475 | } |
475 | 476 | ||
476 | if (accOffset_readFromEEProm()) { |
477 | if (accOffset_readFromEEProm()) { |
477 | accOffset.offsets[PITCH] = accOffset.offsets[ROLL] = 512 * ACC_SUMMATION_FACTOR_PITCHROLL; |
478 | accOffset.offsets[PITCH] = accOffset.offsets[ROLL] = 512 * ACC_SUMMATION_FACTOR_PITCHROLL; |
478 | accOffset.offsets[Z] = 512 * ACC_SUMMATION_FACTOR_Z; |
479 | accOffset.offsets[Z] = 512 * ACC_SUMMATION_FACTOR_Z; |
479 | } |
480 | } |
480 | 481 | ||
481 | // Noise is relative to offset. So, reset noise measurements when changing offsets. |
482 | // Noise is relative to offset. So, reset noise measurements when changing offsets. |
482 | gyroNoisePeak[PITCH] = gyroNoisePeak[ROLL] = 0; |
483 | gyroNoisePeak[PITCH] = gyroNoisePeak[ROLL] = 0; |
483 | accNoisePeak[PITCH] = accNoisePeak[ROLL] = 0; |
484 | accNoisePeak[PITCH] = accNoisePeak[ROLL] = 0; |
484 | 485 | ||
485 | // Setting offset values has an influence in the analog.c ISR |
486 | // Setting offset values has an influence in the analog.c ISR |
486 | // Therefore run measurement for 100ms to achive stable readings |
487 | // Therefore run measurement for 100ms to achive stable readings |
487 | delay_ms_Mess(100); |
488 | delay_ms_Mess(100); |
488 | 489 | ||
489 | // Rough estimate. Hmm no nothing happens at calibration anyway. |
490 | // Rough estimate. Hmm no nothing happens at calibration anyway. |
490 | // airPressureSum = simpleAirPressure * (AIRPRESSURE_SUMMATION_FACTOR/2); |
491 | // airPressureSum = simpleAirPressure * (AIRPRESSURE_SUMMATION_FACTOR/2); |
491 | // pressureMeasurementCount = 0; |
492 | // pressureMeasurementCount = 0; |
492 | } |
493 | } |
493 | 494 | ||
494 | void analog_calibrateGyros(void) { |
495 | void analog_calibrateGyros(void) { |
495 | #define GYRO_OFFSET_CYCLES 32 |
496 | #define GYRO_OFFSET_CYCLES 32 |
496 | uint8_t i, axis; |
497 | uint8_t i, axis; |
497 | int32_t deltaOffsets[3] = { 0, 0, 0 }; |
498 | int32_t offsets[3] = { 0, 0, 0 }; |
498 | gyro_calibrate(); |
499 | gyro_calibrate(); |
499 | 500 | ||
500 | // determine gyro bias by averaging (requires that the copter does not rotate around any axis!) |
501 | // determine gyro bias by averaging (requires that the copter does not rotate around any axis!) |
501 | for (i = 0; i < GYRO_OFFSET_CYCLES; i++) { |
502 | for (i = 0; i < GYRO_OFFSET_CYCLES; i++) { |
502 | delay_ms_Mess(20); |
503 | delay_ms_Mess(20); |
503 | for (axis = PITCH; axis <= YAW; axis++) { |
504 | for (axis = PITCH; axis <= YAW; axis++) { |
504 | deltaOffsets[axis] += rawGyroSum[axis]; |
505 | offsets[axis] += rawGyroSum[axis]; |
505 | } |
506 | } |
506 | } |
507 | } |
507 | 508 | ||
508 | for (axis = PITCH; axis <= YAW; axis++) { |
509 | for (axis = PITCH; axis <= YAW; axis++) { |
509 | gyroOffset.offsets[axis] = (deltaOffsets[axis] + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
510 | gyroOffset.offsets[axis] = (offsets[axis] + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
510 | debugOut.analog[6+axis] = gyroOffset.offsets[axis]; |
- | |
511 | } |
511 | } |
512 | 512 | ||
513 | gyroOffset_writeToEEProm(); |
513 | gyroOffset_writeToEEProm(); |
514 | } |
514 | } |
515 | 515 | ||
516 | /* |
516 | /* |
517 | * Find acc. offsets for a neutral reading, and write them to EEPROM. |
517 | * Find acc. offsets for a neutral reading, and write them to EEPROM. |
518 | * Does not (!} update the local variables. This must be done with a |
518 | * Does not (!} update the local variables. This must be done with a |
519 | * call to analog_calibrate() - this always (?) is done by the caller |
519 | * call to analog_calibrate() - this always (?) is done by the caller |
520 | * anyway. There would be nothing wrong with updating the variables |
520 | * anyway. There would be nothing wrong with updating the variables |
521 | * directly from here, though. |
521 | * directly from here, though. |
522 | */ |
522 | */ |
523 | void analog_calibrateAcc(void) { |
523 | void analog_calibrateAcc(void) { |
524 | #define ACC_OFFSET_CYCLES 10 |
524 | #define ACC_OFFSET_CYCLES 10 |
525 | uint8_t i, axis; |
525 | uint8_t i, axis; |
526 | int32_t deltaOffset[3] = { 0, 0, 0 }; |
526 | int32_t deltaOffset[3] = { 0, 0, 0 }; |
527 | int16_t filteredDelta; |
527 | int16_t filteredDelta; |
528 | // int16_t pressureDiff, savedRawAirPressure; |
- | |
529 | 528 | ||
530 | for (i = 0; i < ACC_OFFSET_CYCLES; i++) { |
529 | for (i = 0; i < ACC_OFFSET_CYCLES; i++) { |
531 | delay_ms_Mess(10); |
530 | delay_ms_Mess(10); |
532 | for (axis = PITCH; axis <= YAW; axis++) { |
531 | for (axis = PITCH; axis <= YAW; axis++) { |
533 | deltaOffset[axis] += acc[axis]; |
532 | deltaOffset[axis] += acc[axis]; |
534 | } |
533 | } |
535 | } |
534 | } |
536 | 535 | ||
537 | for (axis = PITCH; axis <= YAW; axis++) { |
536 | for (axis = PITCH; axis <= YAW; axis++) { |
538 | filteredDelta = (deltaOffset[axis] + ACC_OFFSET_CYCLES / 2) |
537 | filteredDelta = (deltaOffset[axis] + ACC_OFFSET_CYCLES / 2) |
539 | / ACC_OFFSET_CYCLES; |
538 | / ACC_OFFSET_CYCLES; |
540 | accOffset.offsets[axis] += ACC_REVERSED[axis] ? -filteredDelta : filteredDelta; |
539 | accOffset.offsets[axis] += ACC_REVERSED[axis] ? -filteredDelta : filteredDelta; |
541 | } |
540 | } |
542 | 541 | ||
543 | accOffset_writeToEEProm(); |
542 | accOffset_writeToEEProm(); |
544 | } |
543 | } |
545 | 544 |