Rev 1986 | Rev 1991 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1986 | Rev 1987 | ||
---|---|---|---|
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 | 158 | ||
158 | // ADC channels |
159 | // ADC channels |
159 | #define AD_GYRO_YAW 0 |
160 | #define AD_GYRO_YAW 0 |
160 | #define AD_GYRO_ROLL 1 |
161 | #define AD_GYRO_ROLL 1 |
161 | #define AD_GYRO_PITCH 2 |
162 | #define AD_GYRO_PITCH 2 |
162 | #define AD_AIRPRESSURE 3 |
163 | #define AD_AIRPRESSURE 3 |
163 | #define AD_UBAT 4 |
164 | #define AD_UBAT 4 |
164 | #define AD_ACC_Z 5 |
165 | #define AD_ACC_Z 5 |
165 | #define AD_ACC_ROLL 6 |
166 | #define AD_ACC_ROLL 6 |
166 | #define AD_ACC_PITCH 7 |
167 | #define AD_ACC_PITCH 7 |
167 | 168 | ||
168 | /* |
169 | /* |
169 | * Table of AD converter inputs for each state. |
170 | * Table of AD converter inputs for each state. |
170 | * The number of samples summed for each channel is equal to |
171 | * The number of samples summed for each channel is equal to |
171 | * the number of times the channel appears in the array. |
172 | * the number of times the channel appears in the array. |
172 | * 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: |
173 | * 20e6 / 128 / 13 / (1/2e-3) = 24. Since the main control |
174 | * 20e6 / 128 / 13 / (1/2e-3) = 24. Since the main control |
174 | * loop needs a little time between reading AD values and |
175 | * loop needs a little time between reading AD values and |
175 | * re-enabling ADC, the real limit is (how much?) lower. |
176 | * re-enabling ADC, the real limit is (how much?) lower. |
176 | * The acc. sensor is sampled even if not used - or installed |
177 | * The acc. sensor is sampled even if not used - or installed |
177 | * at all. The cost is not significant. |
178 | * at all. The cost is not significant. |
178 | */ |
179 | */ |
179 | 180 | ||
180 | const uint8_t channelsForStates[] PROGMEM = { |
181 | const uint8_t channelsForStates[] PROGMEM = { |
181 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_GYRO_YAW, |
182 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_GYRO_YAW, |
182 | AD_ACC_PITCH, AD_ACC_ROLL, AD_AIRPRESSURE, |
183 | AD_ACC_PITCH, AD_ACC_ROLL, AD_AIRPRESSURE, |
183 | 184 | ||
184 | 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. |
185 | 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 |
186 | 187 | ||
187 | AD_ACC_PITCH, // at 12, finish pitch axis acc. |
188 | AD_ACC_PITCH, // at 12, finish pitch axis acc. |
188 | AD_ACC_ROLL, // at 13, finish roll axis acc. |
189 | AD_ACC_ROLL, // at 13, finish roll axis acc. |
189 | AD_AIRPRESSURE, // at 14, finish air pressure. |
190 | AD_AIRPRESSURE, // at 14, finish air pressure. |
190 | 191 | ||
191 | AD_GYRO_PITCH, // at 15, finish pitch gyro |
192 | AD_GYRO_PITCH, // at 15, finish pitch gyro |
192 | AD_GYRO_ROLL, // at 16, finish roll gyro |
193 | AD_GYRO_ROLL, // at 16, finish roll gyro |
193 | AD_UBAT // at 17, measure battery. |
194 | AD_UBAT // at 17, measure battery. |
194 | }; |
195 | }; |
195 | 196 | ||
196 | // 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. |
197 | // uint8_t GyroDefectPitch = 0, GyroDefectRoll = 0, GyroDefectYaw = 0; |
198 | // uint8_t GyroDefectPitch = 0, GyroDefectRoll = 0, GyroDefectYaw = 0; |
198 | 199 | ||
199 | void analog_init(void) { |
200 | void analog_init(void) { |
200 | uint8_t sreg = SREG; |
201 | uint8_t sreg = SREG; |
201 | // disable all interrupts before reconfiguration |
202 | // disable all interrupts before reconfiguration |
202 | cli(); |
203 | cli(); |
203 | 204 | ||
204 | //ADC0 ... ADC7 is connected to PortA pin 0 ... 7 |
205 | //ADC0 ... ADC7 is connected to PortA pin 0 ... 7 |
205 | DDRA = 0x00; |
206 | DDRA = 0x00; |
206 | PORTA = 0x00; |
207 | PORTA = 0x00; |
207 | // Digital Input Disable Register 0 |
208 | // Digital Input Disable Register 0 |
208 | // Disable digital input buffer for analog adc_channel pins |
209 | // Disable digital input buffer for analog adc_channel pins |
209 | DIDR0 = 0xFF; |
210 | DIDR0 = 0xFF; |
210 | // external reference, adjust data to the right |
211 | // external reference, adjust data to the right |
211 | ADMUX &= ~((1<<REFS1)|(1<<REFS0)|(1<<ADLAR)); |
212 | ADMUX &= ~((1<<REFS1)|(1<<REFS0)|(1<<ADLAR)); |
212 | // 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) |
213 | ADMUX = (ADMUX & 0xE0) | AD_GYRO_PITCH; |
214 | ADMUX = (ADMUX & 0xE0); |
214 | //Set ADC Control and Status Register A |
215 | //Set ADC Control and Status Register A |
215 | //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 |
216 | ADCSRA = (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); |
217 | ADCSRA = (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); |
217 | //Set ADC Control and Status Register B |
218 | //Set ADC Control and Status Register B |
218 | //Trigger Source to Free Running Mode |
219 | //Trigger Source to Free Running Mode |
219 | ADCSRB &= ~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)); |
220 | ADCSRB &= ~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)); |
220 | 221 | ||
221 | startAnalogConversionCycle(); |
222 | startAnalogConversionCycle(); |
222 | 223 | ||
223 | // restore global interrupt flags |
224 | // restore global interrupt flags |
224 | SREG = sreg; |
225 | SREG = sreg; |
225 | } |
226 | } |
226 | 227 | ||
227 | void measureNoise(const int16_t sensor, |
228 | void measureNoise(const int16_t sensor, |
228 | volatile uint16_t* const noiseMeasurement, const uint8_t damping) { |
229 | volatile uint16_t* const noiseMeasurement, const uint8_t damping) { |
229 | if (sensor > (int16_t) (*noiseMeasurement)) { |
230 | if (sensor > (int16_t) (*noiseMeasurement)) { |
230 | *noiseMeasurement = sensor; |
231 | *noiseMeasurement = sensor; |
231 | } else if (-sensor > (int16_t) (*noiseMeasurement)) { |
232 | } else if (-sensor > (int16_t) (*noiseMeasurement)) { |
232 | *noiseMeasurement = -sensor; |
233 | *noiseMeasurement = -sensor; |
233 | } else if (*noiseMeasurement > damping) { |
234 | } else if (*noiseMeasurement > damping) { |
234 | *noiseMeasurement -= damping; |
235 | *noiseMeasurement -= damping; |
235 | } else { |
236 | } else { |
236 | *noiseMeasurement = 0; |
237 | *noiseMeasurement = 0; |
237 | } |
238 | } |
238 | } |
239 | } |
239 | 240 | ||
240 | /* |
241 | /* |
241 | * Min.: 0 |
242 | * Min.: 0 |
242 | * 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. |
243 | */ |
244 | */ |
244 | uint16_t getSimplePressure(int advalue) { |
245 | uint16_t getSimplePressure(int advalue) { |
245 | return (uint16_t) OCR0A * (uint16_t) rangewidth + advalue; |
246 | return (uint16_t) OCR0A * (uint16_t) rangewidth + advalue; |
246 | } |
247 | } |
247 | 248 | ||
248 | void startAnalogConversionCycle(void) { |
249 | void startAnalogConversionCycle(void) { |
249 | analogDataReady = 0; |
250 | analogDataReady = 0; |
250 | // Stop the sampling. Cycle is over. |
251 | // Stop the sampling. Cycle is over. |
251 | for (uint8_t i = 0; i < 8; i++) { |
252 | for (uint8_t i = 0; i < 8; i++) { |
252 | sensorInputs[i] = 0; |
253 | sensorInputs[i] = 0; |
253 | } |
254 | } |
254 | adState = 0; |
255 | adState = 0; |
- | 256 | adChannel = AD_GYRO_PITCH; |
|
255 | ADMUX = (ADMUX & 0xE0) | AD_GYRO_PITCH; |
257 | ADMUX = (ADMUX & 0xE0) | adChannel; |
256 | startADC(); |
258 | startADC(); |
257 | } |
259 | } |
258 | 260 | ||
259 | /***************************************************** |
261 | /***************************************************** |
260 | * Interrupt Service Routine for ADC |
262 | * Interrupt Service Routine for ADC |
261 | * 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 |
262 | * processed further conversions are stopped. |
264 | * processed further conversions are stopped. |
263 | *****************************************************/ |
265 | *****************************************************/ |
264 | ISR(ADC_vect) { |
266 | ISR(ADC_vect) { |
265 | static uint8_t adChannel = AD_GYRO_PITCH; |
- | |
266 | sensorInputs[adChannel] += ADC; |
267 | sensorInputs[adChannel] += ADC; |
267 | // set up for next state. |
268 | // set up for next state. |
268 | adState++; |
269 | adState++; |
269 | if (adState < sizeof(channelsForStates)) { |
270 | if (adState < sizeof(channelsForStates)) { |
270 | adChannel = pgm_read_byte(&channelsForStates[adState]); |
271 | adChannel = pgm_read_byte(&channelsForStates[adState]); |
271 | // set adc muxer to next adChannel |
272 | // set adc muxer to next adChannel |
272 | ADMUX = (ADMUX & 0xE0) | adChannel; |
273 | ADMUX = (ADMUX & 0xE0) | adChannel; |
273 | // after full cycle stop further interrupts |
274 | // after full cycle stop further interrupts |
274 | startADC(); |
275 | startADC(); |
275 | } else { |
276 | } else { |
276 | ADCycleCount++; |
277 | ADCycleCount++; |
277 | analogDataReady = 1; |
278 | analogDataReady = 1; |
278 | // do not restart ADC converter. |
279 | // do not restart ADC converter. |
279 | } |
280 | } |
280 | } |
281 | } |
281 | 282 | ||
282 | void analog_updateGyros(void) { |
283 | void analog_updateGyros(void) { |
283 | // for various filters... |
284 | // for various filters... |
284 | int16_t tempOffsetGyro, tempGyro; |
285 | int16_t tempOffsetGyro, tempGyro; |
285 | 286 | ||
286 | for (uint8_t axis=0; axis<2; axis++) { |
287 | for (uint8_t axis=0; axis<2; axis++) { |
287 | tempGyro = rawGyroSum[axis] = sensorInputs[AD_GYRO_PITCH-axis]; |
288 | tempGyro = rawGyroSum[axis] = sensorInputs[AD_GYRO_PITCH-axis]; |
288 | 289 | ||
289 | /* |
290 | /* |
290 | * Process the gyro data for the PID controller. |
291 | * Process the gyro data for the PID controller. |
291 | */ |
292 | */ |
292 | // 1) Extrapolate: Near the ends of the range, we boost the input significantly. This simulates a |
293 | // 1) Extrapolate: Near the ends of the range, we boost the input significantly. This simulates a |
293 | // gyro with a wider range, and helps counter saturation at full control. |
294 | // gyro with a wider range, and helps counter saturation at full control. |
294 | 295 | ||
295 | if (staticParams.bitConfig & CFG_GYRO_SATURATION_PREVENTION) { |
296 | if (staticParams.bitConfig & CFG_GYRO_SATURATION_PREVENTION) { |
296 | if (tempGyro < SENSOR_MIN_PITCHROLL) { |
297 | if (tempGyro < SENSOR_MIN_PITCHROLL) { |
297 | debugOut.digital[0] |= DEBUG_SENSORLIMIT; |
298 | debugOut.digital[0] |= DEBUG_SENSORLIMIT; |
298 | tempGyro = tempGyro * EXTRAPOLATION_SLOPE - EXTRAPOLATION_LIMIT; |
299 | tempGyro = tempGyro * EXTRAPOLATION_SLOPE - EXTRAPOLATION_LIMIT; |
299 | } else if (tempGyro > SENSOR_MAX_PITCHROLL) { |
300 | } else if (tempGyro > SENSOR_MAX_PITCHROLL) { |
300 | debugOut.digital[0] |= DEBUG_SENSORLIMIT; |
301 | debugOut.digital[0] |= DEBUG_SENSORLIMIT; |
301 | tempGyro = (tempGyro - SENSOR_MAX_PITCHROLL) * EXTRAPOLATION_SLOPE |
302 | tempGyro = (tempGyro - SENSOR_MAX_PITCHROLL) * EXTRAPOLATION_SLOPE |
302 | + SENSOR_MAX_PITCHROLL; |
303 | + SENSOR_MAX_PITCHROLL; |
303 | } else { |
304 | } else { |
304 | debugOut.digital[0] &= ~DEBUG_SENSORLIMIT; |
305 | debugOut.digital[0] &= ~DEBUG_SENSORLIMIT; |
305 | } |
306 | } |
306 | } |
307 | } |
307 | 308 | ||
308 | // 2) Apply sign and offset, scale before filtering. |
309 | // 2) Apply sign and offset, scale before filtering. |
309 | if (GYRO_REVERSED[axis]) { |
310 | if (GYRO_REVERSED[axis]) { |
310 | tempOffsetGyro = (gyroOffset.offsets[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL; |
311 | tempOffsetGyro = (gyroOffset.offsets[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL; |
311 | } else { |
312 | } else { |
312 | tempOffsetGyro = (tempGyro - gyroOffset.offsets[axis]) * GYRO_FACTOR_PITCHROLL; |
313 | tempOffsetGyro = (tempGyro - gyroOffset.offsets[axis]) * GYRO_FACTOR_PITCHROLL; |
313 | } |
314 | } |
314 | 315 | ||
315 | // 3) Scale and filter. |
316 | // 3) Scale and filter. |
316 | tempOffsetGyro = (gyro_PID[axis] * (staticParams.gyroPIDFilterConstant - 1) + tempOffsetGyro) / staticParams.gyroPIDFilterConstant; |
317 | tempOffsetGyro = (gyro_PID[axis] * (staticParams.gyroPIDFilterConstant - 1) + tempOffsetGyro) / staticParams.gyroPIDFilterConstant; |
317 | 318 | ||
318 | // 4) Measure noise. |
319 | // 4) Measure noise. |
319 | measureNoise(tempOffsetGyro, &gyroNoisePeak[axis], GYRO_NOISE_MEASUREMENT_DAMPING); |
320 | measureNoise(tempOffsetGyro, &gyroNoisePeak[axis], GYRO_NOISE_MEASUREMENT_DAMPING); |
320 | 321 | ||
321 | // 5) Differential measurement. |
322 | // 5) Differential measurement. |
322 | gyroD[axis] = (gyroD[axis] * (staticParams.gyroDFilterConstant - 1) + (tempOffsetGyro - gyro_PID[axis])) / staticParams.gyroDFilterConstant; |
323 | gyroD[axis] = (gyroD[axis] * (staticParams.gyroDFilterConstant - 1) + (tempOffsetGyro - gyro_PID[axis])) / staticParams.gyroDFilterConstant; |
323 | 324 | ||
324 | // 6) Done. |
325 | // 6) Done. |
325 | gyro_PID[axis] = tempOffsetGyro; |
326 | gyro_PID[axis] = tempOffsetGyro; |
326 | 327 | ||
327 | /* |
328 | /* |
328 | * Now process the data for attitude angles. |
329 | * Now process the data for attitude angles. |
329 | */ |
330 | */ |
330 | tempGyro = rawGyroSum[axis]; |
331 | tempGyro = rawGyroSum[axis]; |
331 | 332 | ||
332 | // 1) Apply sign and offset, scale before filtering. |
333 | // 1) Apply sign and offset, scale before filtering. |
333 | if (GYRO_REVERSED[axis]) { |
334 | if (GYRO_REVERSED[axis]) { |
334 | tempOffsetGyro = (gyroOffset.offsets[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL; |
335 | tempOffsetGyro = (gyroOffset.offsets[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL; |
335 | } else { |
336 | } else { |
336 | tempOffsetGyro = (tempGyro - gyroOffset.offsets[axis]) * GYRO_FACTOR_PITCHROLL; |
337 | tempOffsetGyro = (tempGyro - gyroOffset.offsets[axis]) * GYRO_FACTOR_PITCHROLL; |
337 | } |
338 | } |
338 | 339 | ||
339 | // 2) Filter. |
340 | // 2) Filter. |
340 | gyro_ATT[axis] = (gyro_ATT[axis] * (staticParams.gyroATTFilterConstant - 1) + tempOffsetGyro) / staticParams.gyroATTFilterConstant; |
341 | gyro_ATT[axis] = (gyro_ATT[axis] * (staticParams.gyroATTFilterConstant - 1) + tempOffsetGyro) / staticParams.gyroATTFilterConstant; |
341 | } |
342 | } |
342 | 343 | ||
343 | // Yaw gyro. |
344 | // Yaw gyro. |
344 | rawGyroSum[YAW] = sensorInputs[AD_GYRO_YAW]; |
345 | rawGyroSum[YAW] = sensorInputs[AD_GYRO_YAW]; |
345 | if (GYRO_REVERSED[YAW]) |
346 | if (GYRO_REVERSED[YAW]) |
346 | yawGyro = gyroOffset.offsets[YAW] - sensorInputs[AD_GYRO_YAW]; |
347 | yawGyro = gyroOffset.offsets[YAW] - sensorInputs[AD_GYRO_YAW]; |
347 | else |
348 | else |
348 | yawGyro = sensorInputs[AD_GYRO_YAW] - gyroOffset.offsets[YAW]; |
349 | yawGyro = sensorInputs[AD_GYRO_YAW] - gyroOffset.offsets[YAW]; |
349 | } |
350 | } |
350 | 351 | ||
351 | void analog_updateAccelerometers(void) { |
352 | void analog_updateAccelerometers(void) { |
352 | // Z acc. |
353 | // Z acc. |
353 | if (ACC_REVERSED[Z]) |
354 | if (ACC_REVERSED[Z]) |
354 | acc[Z] = accOffset.offsets[Z] - sensorInputs[AD_ACC_Z]; |
355 | acc[Z] = accOffset.offsets[Z] - sensorInputs[AD_ACC_Z]; |
355 | else |
356 | else |
356 | acc[Z] = sensorInputs[AD_ACC_Z] - accOffset.offsets[Z]; |
357 | acc[Z] = sensorInputs[AD_ACC_Z] - accOffset.offsets[Z]; |
357 | 358 | ||
358 | // Pitch and roll axis accelerations. |
359 | // Pitch and roll axis accelerations. |
359 | for (uint8_t axis=0; axis<2; axis++) { |
360 | for (uint8_t axis=0; axis<2; axis++) { |
360 | if (ACC_REVERSED[axis]) |
361 | if (ACC_REVERSED[axis]) |
361 | acc[axis] = accOffset.offsets[axis] - sensorInputs[AD_ACC_PITCH-axis]; |
362 | acc[axis] = accOffset.offsets[axis] - sensorInputs[AD_ACC_PITCH-axis]; |
362 | else |
363 | else |
363 | acc[axis] = sensorInputs[AD_ACC_PITCH-axis] - accOffset.offsets[axis]; |
364 | acc[axis] = sensorInputs[AD_ACC_PITCH-axis] - accOffset.offsets[axis]; |
364 | } |
365 | } |
365 | 366 | ||
366 | for (uint8_t axis=0; axis<3; axis++) { |
367 | for (uint8_t axis=0; axis<3; axis++) { |
367 | filteredAcc[axis] = (filteredAcc[axis] * (staticParams.accFilterConstant - 1) + acc[axis]) / staticParams.accFilterConstant; |
368 | filteredAcc[axis] = (filteredAcc[axis] * (staticParams.accFilterConstant - 1) + acc[axis]) / staticParams.accFilterConstant; |
368 | measureNoise(acc[axis], &accNoisePeak[axis], 1); |
369 | measureNoise(acc[axis], &accNoisePeak[axis], 1); |
369 | } |
370 | } |
370 | } |
371 | } |
371 | 372 | ||
372 | void analog_updateAirPressure(void) { |
373 | void analog_updateAirPressure(void) { |
373 | static uint16_t pressureAutorangingWait = 25; |
374 | static uint16_t pressureAutorangingWait = 25; |
374 | uint16_t rawAirPressure; |
375 | uint16_t rawAirPressure; |
375 | int16_t newrange; |
376 | int16_t newrange; |
376 | // air pressure |
377 | // air pressure |
377 | if (pressureAutorangingWait) { |
378 | if (pressureAutorangingWait) { |
378 | //A range switch was done recently. Wait for steadying. |
379 | //A range switch was done recently. Wait for steadying. |
379 | pressureAutorangingWait--; |
380 | pressureAutorangingWait--; |
380 | debugOut.analog[27] = (uint16_t) OCR0A; |
381 | debugOut.analog[27] = (uint16_t) OCR0A; |
381 | debugOut.analog[31] = simpleAirPressure; |
382 | debugOut.analog[31] = simpleAirPressure; |
382 | } else { |
383 | } else { |
383 | rawAirPressure = sensorInputs[AD_AIRPRESSURE]; |
384 | rawAirPressure = sensorInputs[AD_AIRPRESSURE]; |
384 | if (rawAirPressure < MIN_RAWPRESSURE) { |
385 | if (rawAirPressure < MIN_RAWPRESSURE) { |
385 | // value is too low, so decrease voltage on the op amp minus input, making the value higher. |
386 | // value is too low, so decrease voltage on the op amp minus input, making the value higher. |
386 | newrange = OCR0A - (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (MAX_RAWPRESSURE - rawAirPressure) / (rangewidth * 2) + 1; |
387 | newrange = OCR0A - (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (MAX_RAWPRESSURE - rawAirPressure) / (rangewidth * 2) + 1; |
387 | if (newrange > MIN_RANGES_EXTRAPOLATION) { |
388 | if (newrange > MIN_RANGES_EXTRAPOLATION) { |
388 | pressureAutorangingWait = (OCR0A - newrange) * AUTORANGE_WAIT_FACTOR; // = OCRA0 - OCRA0 + |
389 | pressureAutorangingWait = (OCR0A - newrange) * AUTORANGE_WAIT_FACTOR; // = OCRA0 - OCRA0 + |
389 | OCR0A = newrange; |
390 | OCR0A = newrange; |
390 | } else { |
391 | } else { |
391 | if (OCR0A) { |
392 | if (OCR0A) { |
392 | OCR0A--; |
393 | OCR0A--; |
393 | pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
394 | pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
394 | } |
395 | } |
395 | } |
396 | } |
396 | } else if (rawAirPressure > MAX_RAWPRESSURE) { |
397 | } else if (rawAirPressure > MAX_RAWPRESSURE) { |
397 | // value is too high, so increase voltage on the op amp minus input, making the value lower. |
398 | // value is too high, so increase voltage on the op amp minus input, making the value lower. |
398 | // If near the end, make a limited increase |
399 | // If near the end, make a limited increase |
399 | newrange = OCR0A + (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (rawAirPressure - MIN_RAWPRESSURE) / (rangewidth * 2) - 1; |
400 | newrange = OCR0A + (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (rawAirPressure - MIN_RAWPRESSURE) / (rangewidth * 2) - 1; |
400 | if (newrange < MAX_RANGES_EXTRAPOLATION) { |
401 | if (newrange < MAX_RANGES_EXTRAPOLATION) { |
401 | pressureAutorangingWait = (newrange - OCR0A) * AUTORANGE_WAIT_FACTOR; |
402 | pressureAutorangingWait = (newrange - OCR0A) * AUTORANGE_WAIT_FACTOR; |
402 | OCR0A = newrange; |
403 | OCR0A = newrange; |
403 | } else { |
404 | } else { |
404 | if (OCR0A < 254) { |
405 | if (OCR0A < 254) { |
405 | OCR0A++; |
406 | OCR0A++; |
406 | pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
407 | pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
407 | } |
408 | } |
408 | } |
409 | } |
409 | } |
410 | } |
410 | 411 | ||
411 | // Even if the sample is off-range, use it. |
412 | // Even if the sample is off-range, use it. |
412 | simpleAirPressure = getSimplePressure(rawAirPressure); |
413 | simpleAirPressure = getSimplePressure(rawAirPressure); |
413 | debugOut.analog[27] = (uint16_t) OCR0A; |
414 | debugOut.analog[27] = (uint16_t) OCR0A; |
414 | debugOut.analog[31] = simpleAirPressure; |
415 | debugOut.analog[31] = simpleAirPressure; |
415 | 416 | ||
416 | if (simpleAirPressure < MIN_RANGES_EXTRAPOLATION * rangewidth) { |
417 | if (simpleAirPressure < MIN_RANGES_EXTRAPOLATION * rangewidth) { |
417 | // Danger: pressure near lower end of range. If the measurement saturates, the |
418 | // Danger: pressure near lower end of range. If the measurement saturates, the |
418 | // copter may climb uncontrolledly... Simulate a drastic reduction in pressure. |
419 | // copter may climb uncontrolledly... Simulate a drastic reduction in pressure. |
419 | debugOut.digital[1] |= DEBUG_SENSORLIMIT; |
420 | debugOut.digital[1] |= DEBUG_SENSORLIMIT; |
420 | airPressureSum += (int16_t) MIN_RANGES_EXTRAPOLATION * rangewidth |
421 | airPressureSum += (int16_t) MIN_RANGES_EXTRAPOLATION * rangewidth |
421 | + (simpleAirPressure - (int16_t) MIN_RANGES_EXTRAPOLATION |
422 | + (simpleAirPressure - (int16_t) MIN_RANGES_EXTRAPOLATION |
422 | * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
423 | * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
423 | } else if (simpleAirPressure > MAX_RANGES_EXTRAPOLATION * rangewidth) { |
424 | } else if (simpleAirPressure > MAX_RANGES_EXTRAPOLATION * rangewidth) { |
424 | // Danger: pressure near upper end of range. If the measurement saturates, the |
425 | // Danger: pressure near upper end of range. If the measurement saturates, the |
425 | // copter may descend uncontrolledly... Simulate a drastic increase in pressure. |
426 | // copter may descend uncontrolledly... Simulate a drastic increase in pressure. |
426 | debugOut.digital[1] |= DEBUG_SENSORLIMIT; |
427 | debugOut.digital[1] |= DEBUG_SENSORLIMIT; |
427 | airPressureSum += (int16_t) MAX_RANGES_EXTRAPOLATION * rangewidth |
428 | airPressureSum += (int16_t) MAX_RANGES_EXTRAPOLATION * rangewidth |
428 | + (simpleAirPressure - (int16_t) MAX_RANGES_EXTRAPOLATION |
429 | + (simpleAirPressure - (int16_t) MAX_RANGES_EXTRAPOLATION |
429 | * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
430 | * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
430 | } else { |
431 | } else { |
431 | // normal case. |
432 | // normal case. |
432 | // If AIRPRESSURE_SUMMATION_FACTOR is an odd number we only want to add half the double sample. |
433 | // If AIRPRESSURE_SUMMATION_FACTOR is an odd number we only want to add half the double sample. |
433 | // The 2 cases above (end of range) are ignored for this. |
434 | // The 2 cases above (end of range) are ignored for this. |
434 | debugOut.digital[1] &= ~DEBUG_SENSORLIMIT; |
435 | debugOut.digital[1] &= ~DEBUG_SENSORLIMIT; |
435 | if (pressureMeasurementCount == AIRPRESSURE_SUMMATION_FACTOR - 1) |
436 | if (pressureMeasurementCount == AIRPRESSURE_SUMMATION_FACTOR - 1) |
436 | airPressureSum += simpleAirPressure / 2; |
437 | airPressureSum += simpleAirPressure / 2; |
437 | else |
438 | else |
438 | airPressureSum += simpleAirPressure; |
439 | airPressureSum += simpleAirPressure; |
439 | } |
440 | } |
440 | 441 | ||
441 | // 2 samples were added. |
442 | // 2 samples were added. |
442 | pressureMeasurementCount += 2; |
443 | pressureMeasurementCount += 2; |
443 | if (pressureMeasurementCount >= AIRPRESSURE_SUMMATION_FACTOR) { |
444 | if (pressureMeasurementCount >= AIRPRESSURE_SUMMATION_FACTOR) { |
444 | filteredAirPressure = (filteredAirPressure * (AIRPRESSURE_FILTER - 1) |
445 | filteredAirPressure = (filteredAirPressure * (AIRPRESSURE_FILTER - 1) |
445 | + airPressureSum + AIRPRESSURE_FILTER / 2) / AIRPRESSURE_FILTER; |
446 | + airPressureSum + AIRPRESSURE_FILTER / 2) / AIRPRESSURE_FILTER; |
446 | pressureMeasurementCount = airPressureSum = 0; |
447 | pressureMeasurementCount = airPressureSum = 0; |
447 | } |
448 | } |
448 | } |
449 | } |
449 | } |
450 | } |
450 | 451 | ||
451 | void analog_updateBatteryVoltage(void) { |
452 | void analog_updateBatteryVoltage(void) { |
452 | // Battery. The measured value is: (V * 1k/11k)/3v * 1024 = 31.03 counts per volt (max. measurable is 33v). |
453 | // Battery. The measured value is: (V * 1k/11k)/3v * 1024 = 31.03 counts per volt (max. measurable is 33v). |
453 | // This is divided by 3 --> 10.34 counts per volt. |
454 | // This is divided by 3 --> 10.34 counts per volt. |
454 | UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4; |
455 | UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4; |
455 | debugOut.analog[11] = UBat; |
456 | debugOut.analog[11] = UBat; |
456 | debugOut.analog[21] = sensorInputs[AD_UBAT]; |
457 | debugOut.analog[21] = sensorInputs[AD_UBAT]; |
457 | } |
458 | } |
458 | 459 | ||
459 | void analog_update(void) { |
460 | void analog_update(void) { |
460 | analog_updateGyros(); |
461 | analog_updateGyros(); |
461 | analog_updateAccelerometers(); |
462 | analog_updateAccelerometers(); |
462 | analog_updateAirPressure(); |
463 | analog_updateAirPressure(); |
463 | analog_updateBatteryVoltage(); |
464 | analog_updateBatteryVoltage(); |
464 | } |
465 | } |
465 | 466 | ||
466 | void analog_setNeutral() { |
467 | void analog_setNeutral() { |
467 | if (gyroAmplifierOffset_readFromEEProm()) { |
468 | if (gyroAmplifierOffset_readFromEEProm()) { |
468 | printf("gyro amp invalid%s",recal); |
469 | printf("gyro amp invalid%s",recal); |
469 | gyro_loadAmplifierOffsets(1); |
470 | gyro_loadAmplifierOffsets(1); |
470 | } else |
471 | } else |
471 | gyro_loadAmplifierOffsets(0); |
472 | gyro_loadAmplifierOffsets(0); |
472 | 473 | ||
473 | if (gyroOffset_readFromEEProm()) { |
474 | if (gyroOffset_readFromEEProm()) { |
474 | printf("gyro offsets invalid%s",recal); |
475 | printf("gyro offsets invalid%s",recal); |
475 | gyroOffset.offsets[PITCH] = gyroOffset.offsets[ROLL] = 512 * GYRO_SUMMATION_FACTOR_PITCHROLL; |
476 | gyroOffset.offsets[PITCH] = gyroOffset.offsets[ROLL] = 512 * GYRO_SUMMATION_FACTOR_PITCHROLL; |
476 | gyroOffset.offsets[YAW] = 512 * GYRO_SUMMATION_FACTOR_YAW; |
477 | gyroOffset.offsets[YAW] = 512 * GYRO_SUMMATION_FACTOR_YAW; |
477 | } |
478 | } |
478 | 479 | ||
479 | if (accOffset_readFromEEProm()) { |
480 | if (accOffset_readFromEEProm()) { |
480 | printf("acc. meter offsets invalid%s",recal); |
481 | printf("acc. meter offsets invalid%s",recal); |
481 | accOffset.offsets[PITCH] = accOffset.offsets[ROLL] = 512 * ACC_SUMMATION_FACTOR_PITCHROLL; |
482 | accOffset.offsets[PITCH] = accOffset.offsets[ROLL] = 512 * ACC_SUMMATION_FACTOR_PITCHROLL; |
482 | accOffset.offsets[Z] = 717 * ACC_SUMMATION_FACTOR_Z; |
483 | accOffset.offsets[Z] = 717 * ACC_SUMMATION_FACTOR_Z; |
483 | } |
484 | } |
484 | 485 | ||
485 | // Noise is relative to offset. So, reset noise measurements when changing offsets. |
486 | // Noise is relative to offset. So, reset noise measurements when changing offsets. |
486 | gyroNoisePeak[PITCH] = gyroNoisePeak[ROLL] = 0; |
487 | gyroNoisePeak[PITCH] = gyroNoisePeak[ROLL] = 0; |
487 | accNoisePeak[PITCH] = accNoisePeak[ROLL] = 0; |
488 | accNoisePeak[PITCH] = accNoisePeak[ROLL] = 0; |
488 | 489 | ||
489 | // Setting offset values has an influence in the analog.c ISR |
490 | // Setting offset values has an influence in the analog.c ISR |
490 | // Therefore run measurement for 100ms to achive stable readings |
491 | // Therefore run measurement for 100ms to achive stable readings |
491 | delay_ms_with_adc_measurement(100); |
492 | delay_ms_with_adc_measurement(100); |
492 | 493 | ||
493 | // Rough estimate. Hmm no nothing happens at calibration anyway. |
494 | // Rough estimate. Hmm no nothing happens at calibration anyway. |
494 | // airPressureSum = simpleAirPressure * (AIRPRESSURE_SUMMATION_FACTOR/2); |
495 | // airPressureSum = simpleAirPressure * (AIRPRESSURE_SUMMATION_FACTOR/2); |
495 | // pressureMeasurementCount = 0; |
496 | // pressureMeasurementCount = 0; |
496 | } |
497 | } |
497 | 498 | ||
498 | void analog_calibrateGyros(void) { |
499 | void analog_calibrateGyros(void) { |
499 | #define GYRO_OFFSET_CYCLES 32 |
500 | #define GYRO_OFFSET_CYCLES 32 |
500 | uint8_t i, axis; |
501 | uint8_t i, axis; |
501 | int32_t offsets[3] = { 0, 0, 0 }; |
502 | int32_t offsets[3] = { 0, 0, 0 }; |
502 | gyro_calibrate(); |
503 | gyro_calibrate(); |
503 | 504 | ||
504 | // determine gyro bias by averaging (requires that the copter does not rotate around any axis!) |
505 | // determine gyro bias by averaging (requires that the copter does not rotate around any axis!) |
505 | for (i = 0; i < GYRO_OFFSET_CYCLES; i++) { |
506 | for (i = 0; i < GYRO_OFFSET_CYCLES; i++) { |
506 | delay_ms_with_adc_measurement(20); |
507 | delay_ms_with_adc_measurement(20); |
507 | for (axis = PITCH; axis <= YAW; axis++) { |
508 | for (axis = PITCH; axis <= YAW; axis++) { |
508 | offsets[axis] += rawGyroSum[axis]; |
509 | offsets[axis] += rawGyroSum[axis]; |
509 | } |
510 | } |
510 | } |
511 | } |
511 | 512 | ||
512 | for (axis = PITCH; axis <= YAW; axis++) { |
513 | for (axis = PITCH; axis <= YAW; axis++) { |
513 | gyroOffset.offsets[axis] = (offsets[axis] + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
514 | gyroOffset.offsets[axis] = (offsets[axis] + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
514 | } |
515 | } |
515 | 516 | ||
516 | gyroOffset_writeToEEProm(); |
517 | gyroOffset_writeToEEProm(); |
517 | } |
518 | } |
518 | 519 | ||
519 | /* |
520 | /* |
520 | * Find acc. offsets for a neutral reading, and write them to EEPROM. |
521 | * Find acc. offsets for a neutral reading, and write them to EEPROM. |
521 | * Does not (!} update the local variables. This must be done with a |
522 | * Does not (!} update the local variables. This must be done with a |
522 | * call to analog_calibrate() - this always (?) is done by the caller |
523 | * call to analog_calibrate() - this always (?) is done by the caller |
523 | * anyway. There would be nothing wrong with updating the variables |
524 | * anyway. There would be nothing wrong with updating the variables |
524 | * directly from here, though. |
525 | * directly from here, though. |
525 | */ |
526 | */ |
526 | void analog_calibrateAcc(void) { |
527 | void analog_calibrateAcc(void) { |
527 | #define ACC_OFFSET_CYCLES 10 |
528 | #define ACC_OFFSET_CYCLES 10 |
528 | uint8_t i, axis; |
529 | uint8_t i, axis; |
529 | int32_t deltaOffset[3] = { 0, 0, 0 }; |
530 | int32_t deltaOffset[3] = { 0, 0, 0 }; |
530 | int16_t filteredDelta; |
531 | int16_t filteredDelta; |
531 | 532 | ||
532 | for (i = 0; i < ACC_OFFSET_CYCLES; i++) { |
533 | for (i = 0; i < ACC_OFFSET_CYCLES; i++) { |
533 | delay_ms_with_adc_measurement(10); |
534 | delay_ms_with_adc_measurement(10); |
534 | for (axis = PITCH; axis <= YAW; axis++) { |
535 | for (axis = PITCH; axis <= YAW; axis++) { |
535 | deltaOffset[axis] += acc[axis]; |
536 | deltaOffset[axis] += acc[axis]; |
536 | } |
537 | } |
537 | } |
538 | } |
538 | 539 | ||
539 | for (axis = PITCH; axis <= YAW; axis++) { |
540 | for (axis = PITCH; axis <= YAW; axis++) { |
540 | filteredDelta = (deltaOffset[axis] + ACC_OFFSET_CYCLES / 2) |
541 | filteredDelta = (deltaOffset[axis] + ACC_OFFSET_CYCLES / 2) |
541 | / ACC_OFFSET_CYCLES; |
542 | / ACC_OFFSET_CYCLES; |
542 | accOffset.offsets[axis] += ACC_REVERSED[axis] ? -filteredDelta : filteredDelta; |
543 | accOffset.offsets[axis] += ACC_REVERSED[axis] ? -filteredDelta : filteredDelta; |
543 | } |
544 | } |
544 | 545 | ||
545 | accOffset_writeToEEProm(); |
546 | accOffset_writeToEEProm(); |
546 | } |
547 | } |
547 | 548 |