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