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