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