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