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