Rev 2020 | Rev 2032 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2020 | Rev 2026 | ||
---|---|---|---|
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 | int16_t acc[3]; |
87 | int16_t acc[3]; |
88 | int16_t filteredAcc[3] = { 0,0,0 }; |
88 | int16_t filteredAcc[3] = { 0,0,0 }; |
89 | 89 | ||
90 | /* |
90 | /* |
91 | * 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 |
92 | * 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 |
93 | * integration to angles. |
93 | * integration to angles. |
94 | */ |
94 | */ |
95 | int16_t gyro_PID[2]; |
95 | int16_t gyro_PID[2]; |
96 | int16_t gyro_ATT[2]; |
96 | int16_t gyro_ATT[2]; |
97 | int16_t gyroD[2]; |
97 | int16_t gyroD[2]; |
98 | int16_t yawGyro; |
98 | int16_t yawGyro; |
99 | 99 | ||
100 | /* |
100 | /* |
101 | * 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 |
102 | * 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 |
103 | * to be centered on zero. |
103 | * to be centered on zero. |
104 | */ |
104 | */ |
105 | 105 | ||
106 | sensorOffset_t gyroOffset; |
106 | sensorOffset_t gyroOffset; |
107 | sensorOffset_t accOffset; |
107 | sensorOffset_t accOffset; |
108 | sensorOffset_t gyroAmplifierOffset; |
108 | sensorOffset_t gyroAmplifierOffset; |
109 | 109 | ||
110 | /* |
110 | /* |
111 | * In the MK coordinate system, nose-down is positive and left-roll is positive. |
111 | * In the MK coordinate system, nose-down is positive and left-roll is positive. |
112 | * If a sensor is used in an orientation where one but not both of the axes has |
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). |
113 | * an opposite sign, PR_ORIENTATION_REVERSED is set to 1 (true). |
114 | * Transform: |
114 | * Transform: |
115 | * pitch <- pp*pitch + pr*roll |
115 | * pitch <- pp*pitch + pr*roll |
116 | * roll <- rp*pitch + rr*roll |
116 | * roll <- rp*pitch + rr*roll |
117 | * Not reversed, GYRO_QUADRANT: |
117 | * Not reversed, GYRO_QUADRANT: |
118 | * 0: pp=1, pr=0, rp=0, rr=1 // 0 degrees |
118 | * 0: pp=1, pr=0, rp=0, rr=1 // 0 degrees |
119 | * 1: pp=1, pr=-1,rp=1, rr=1 // +45 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 |
120 | * 2: pp=0, pr=-1,rp=1, rr=0 // +90 degrees |
121 | * 3: pp=-1,pr=-1,rp=1, rr=1 // +135 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 |
122 | * 4: pp=-1,pr=0, rp=0, rr=-1 // +180 degrees |
123 | * 5: pp=-1,pr=1, rp=-1,rr=-1 // +225 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 |
124 | * 6: pp=0, pr=1, rp=-1,rr=0 // +270 degrees |
125 | * 7: pp=1, pr=1, rp=-1,rr=1 // +315 degrees |
125 | * 7: pp=1, pr=1, rp=-1,rr=1 // +315 degrees |
126 | * Reversed, GYRO_QUADRANT: |
126 | * Reversed, GYRO_QUADRANT: |
127 | * 0: pp=-1,pr=0, rp=0, rr=1 // 0 degrees with pitch reversed |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
134 | * 7: pp=-1,pr=1, rp=1, rr=1 // +315 degrees with pitch reversed |
135 | */ |
135 | */ |
136 | 136 | ||
137 | void rotate(int16_t* result, uint8_t quadrant, uint8_t reverse) { |
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}; |
138 | static const int8_t rotationTab[] = {1,1,0,-1,-1,-1,0,1}; |
139 | // Pitch to Pitch part |
139 | // Pitch to Pitch part |
140 | int8_t xx = reverse ? rotationTab[(quadrant+4)%8] : rotationTab[quadrant]; |
140 | int8_t xx = reverse ? rotationTab[(quadrant+4)%8] : rotationTab[quadrant]; |
141 | // Roll to Pitch part |
141 | // Roll to Pitch part |
142 | int8_t xy = rotationTab[(quadrant+2)%8]; |
142 | int8_t xy = rotationTab[(quadrant+2)%8]; |
143 | // Pitch to Roll part |
143 | // Pitch to Roll part |
144 | int8_t yx = reverse ? rotationTab[(quadrant+2)%8] : rotationTab[(quadrant+6)%8]; |
144 | int8_t yx = reverse ? rotationTab[(quadrant+2)%8] : rotationTab[(quadrant+6)%8]; |
145 | // Roll to Roll part |
145 | // Roll to Roll part |
146 | int8_t yy = rotationTab[quadrant]; |
146 | int8_t yy = rotationTab[quadrant]; |
147 | 147 | ||
148 | int16_t xIn = result[0]; |
148 | int16_t xIn = result[0]; |
149 | result[0] = xx*xIn + xy*result[1]; |
149 | result[0] = xx*xIn + xy*result[1]; |
150 | result[1] = yx*xIn + yy*result[1]; |
150 | result[1] = yx*xIn + yy*result[1]; |
151 | 151 | ||
152 | if (quadrant & 1) { |
152 | if (quadrant & 1) { |
153 | // A rotation was used above, where the factors were too large by sqrt(2). |
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). |
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 |
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... |
156 | // of 2 11 bit numbers, so 12 bits. We have 4 bits left... |
157 | result[0] = (result[0]*11) >> 4; |
157 | result[0] = (result[0]*11) >> 4; |
158 | result[1] = (result[1]*11) >> 4; |
158 | result[1] = (result[1]*11) >> 4; |
159 | } |
159 | } |
160 | } |
160 | } |
161 | 161 | ||
162 | /* |
162 | /* |
163 | * Air pressure |
163 | * Air pressure |
164 | */ |
164 | */ |
165 | volatile uint8_t rangewidth = 105; |
165 | volatile uint8_t rangewidth = 105; |
166 | 166 | ||
167 | // Direct from sensor, irrespective of range. |
167 | // Direct from sensor, irrespective of range. |
168 | // volatile uint16_t rawAirPressure; |
168 | // volatile uint16_t rawAirPressure; |
169 | 169 | ||
170 | // Value of 2 samples, with range. |
170 | // Value of 2 samples, with range. |
171 | uint16_t simpleAirPressure; |
171 | uint16_t simpleAirPressure; |
172 | 172 | ||
173 | // Value of AIRPRESSURE_OVERSAMPLING samples, with range, filtered. |
173 | // Value of AIRPRESSURE_OVERSAMPLING samples, with range, filtered. |
174 | int32_t filteredAirPressure; |
174 | int32_t filteredAirPressure; |
- | 175 | ||
- | 176 | #define MAX_AIRPRESSURE_WINDOW_LENGTH 64 |
|
- | 177 | int16_t airPressureWindow[MAX_AIRPRESSURE_WINDOW_LENGTH]; |
|
- | 178 | int32_t windowedAirPressure; |
|
- | 179 | uint8_t windowPtr; |
|
175 | 180 | ||
176 | // Partial sum of AIRPRESSURE_SUMMATION_FACTOR samples. |
181 | // Partial sum of AIRPRESSURE_SUMMATION_FACTOR samples. |
177 | int32_t airPressureSum; |
182 | int32_t airPressureSum; |
178 | 183 | ||
179 | // The number of samples summed into airPressureSum so far. |
184 | // The number of samples summed into airPressureSum so far. |
180 | uint8_t pressureMeasurementCount; |
185 | uint8_t pressureMeasurementCount; |
181 | 186 | ||
182 | /* |
187 | /* |
183 | * Battery voltage, in units of: 1k/11k / 3V * 1024 = 31.03 per volt. |
188 | * Battery voltage, in units of: 1k/11k / 3V * 1024 = 31.03 per volt. |
184 | * That is divided by 3 below, for a final 10.34 per volt. |
189 | * That is divided by 3 below, for a final 10.34 per volt. |
185 | * So the initial value of 100 is for 9.7 volts. |
190 | * So the initial value of 100 is for 9.7 volts. |
186 | */ |
191 | */ |
187 | int16_t UBat = 100; |
192 | int16_t UBat = 100; |
188 | 193 | ||
189 | /* |
194 | /* |
190 | * Control and status. |
195 | * Control and status. |
191 | */ |
196 | */ |
192 | volatile uint16_t ADCycleCount = 0; |
197 | volatile uint16_t ADCycleCount = 0; |
193 | volatile uint8_t analogDataReady = 1; |
198 | volatile uint8_t analogDataReady = 1; |
194 | 199 | ||
195 | /* |
200 | /* |
196 | * Experiment: Measuring vibration-induced sensor noise. |
201 | * Experiment: Measuring vibration-induced sensor noise. |
197 | */ |
202 | */ |
198 | uint16_t gyroNoisePeak[3]; |
203 | uint16_t gyroNoisePeak[3]; |
199 | uint16_t accNoisePeak[3]; |
204 | uint16_t accNoisePeak[3]; |
200 | 205 | ||
201 | volatile uint8_t adState; |
206 | volatile uint8_t adState; |
202 | volatile uint8_t adChannel; |
207 | volatile uint8_t adChannel; |
203 | 208 | ||
204 | // ADC channels |
209 | // ADC channels |
205 | #define AD_GYRO_YAW 0 |
210 | #define AD_GYRO_YAW 0 |
206 | #define AD_GYRO_ROLL 1 |
211 | #define AD_GYRO_ROLL 1 |
207 | #define AD_GYRO_PITCH 2 |
212 | #define AD_GYRO_PITCH 2 |
208 | #define AD_AIRPRESSURE 3 |
213 | #define AD_AIRPRESSURE 3 |
209 | #define AD_UBAT 4 |
214 | #define AD_UBAT 4 |
210 | #define AD_ACC_Z 5 |
215 | #define AD_ACC_Z 5 |
211 | #define AD_ACC_ROLL 6 |
216 | #define AD_ACC_ROLL 6 |
212 | #define AD_ACC_PITCH 7 |
217 | #define AD_ACC_PITCH 7 |
213 | 218 | ||
214 | /* |
219 | /* |
215 | * Table of AD converter inputs for each state. |
220 | * Table of AD converter inputs for each state. |
216 | * The number of samples summed for each channel is equal to |
221 | * The number of samples summed for each channel is equal to |
217 | * the number of times the channel appears in the array. |
222 | * the number of times the channel appears in the array. |
218 | * The max. number of samples that can be taken in 2 ms is: |
223 | * The max. number of samples that can be taken in 2 ms is: |
219 | * 20e6 / 128 / 13 / (1/2e-3) = 24. Since the main control |
224 | * 20e6 / 128 / 13 / (1/2e-3) = 24. Since the main control |
220 | * loop needs a little time between reading AD values and |
225 | * loop needs a little time between reading AD values and |
221 | * re-enabling ADC, the real limit is (how much?) lower. |
226 | * re-enabling ADC, the real limit is (how much?) lower. |
222 | * The acc. sensor is sampled even if not used - or installed |
227 | * The acc. sensor is sampled even if not used - or installed |
223 | * at all. The cost is not significant. |
228 | * at all. The cost is not significant. |
224 | */ |
229 | */ |
225 | 230 | ||
226 | const uint8_t channelsForStates[] PROGMEM = { |
231 | const uint8_t channelsForStates[] PROGMEM = { |
227 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_GYRO_YAW, |
232 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_GYRO_YAW, |
228 | AD_ACC_PITCH, AD_ACC_ROLL, AD_AIRPRESSURE, |
233 | AD_ACC_PITCH, AD_ACC_ROLL, AD_AIRPRESSURE, |
229 | 234 | ||
230 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_ACC_Z, // at 8, measure Z acc. |
235 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_ACC_Z, // at 8, measure Z acc. |
231 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_GYRO_YAW, // at 11, finish yaw gyro |
236 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_GYRO_YAW, // at 11, finish yaw gyro |
232 | 237 | ||
233 | AD_ACC_PITCH, // at 12, finish pitch axis acc. |
238 | AD_ACC_PITCH, // at 12, finish pitch axis acc. |
234 | AD_ACC_ROLL, // at 13, finish roll axis acc. |
239 | AD_ACC_ROLL, // at 13, finish roll axis acc. |
235 | AD_AIRPRESSURE, // at 14, finish air pressure. |
240 | AD_AIRPRESSURE, // at 14, finish air pressure. |
236 | 241 | ||
237 | AD_GYRO_PITCH, // at 15, finish pitch gyro |
242 | AD_GYRO_PITCH, // at 15, finish pitch gyro |
238 | AD_GYRO_ROLL, // at 16, finish roll gyro |
243 | AD_GYRO_ROLL, // at 16, finish roll gyro |
239 | AD_UBAT // at 17, measure battery. |
244 | AD_UBAT // at 17, measure battery. |
240 | }; |
245 | }; |
241 | 246 | ||
242 | // Feature removed. Could be reintroduced later - but should work for all gyro types then. |
247 | // Feature removed. Could be reintroduced later - but should work for all gyro types then. |
243 | // uint8_t GyroDefectPitch = 0, GyroDefectRoll = 0, GyroDefectYaw = 0; |
248 | // uint8_t GyroDefectPitch = 0, GyroDefectRoll = 0, GyroDefectYaw = 0; |
244 | 249 | ||
245 | void analog_init(void) { |
250 | void analog_init(void) { |
246 | uint8_t sreg = SREG; |
251 | uint8_t sreg = SREG; |
247 | // disable all interrupts before reconfiguration |
252 | // disable all interrupts before reconfiguration |
248 | cli(); |
253 | cli(); |
249 | 254 | ||
250 | //ADC0 ... ADC7 is connected to PortA pin 0 ... 7 |
255 | //ADC0 ... ADC7 is connected to PortA pin 0 ... 7 |
251 | DDRA = 0x00; |
256 | DDRA = 0x00; |
252 | PORTA = 0x00; |
257 | PORTA = 0x00; |
253 | // Digital Input Disable Register 0 |
258 | // Digital Input Disable Register 0 |
254 | // Disable digital input buffer for analog adc_channel pins |
259 | // Disable digital input buffer for analog adc_channel pins |
255 | DIDR0 = 0xFF; |
260 | DIDR0 = 0xFF; |
256 | // external reference, adjust data to the right |
261 | // external reference, adjust data to the right |
257 | ADMUX &= ~((1<<REFS1)|(1<<REFS0)|(1<<ADLAR)); |
262 | ADMUX &= ~((1<<REFS1)|(1<<REFS0)|(1<<ADLAR)); |
258 | // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice) |
263 | // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice) |
259 | ADMUX = (ADMUX & 0xE0); |
264 | ADMUX = (ADMUX & 0xE0); |
260 | //Set ADC Control and Status Register A |
265 | //Set ADC Control and Status Register A |
261 | //Auto Trigger Enable, Prescaler Select Bits to Division Factor 128, i.e. ADC clock = SYSCKL/128 = 156.25 kHz |
266 | //Auto Trigger Enable, Prescaler Select Bits to Division Factor 128, i.e. ADC clock = SYSCKL/128 = 156.25 kHz |
262 | ADCSRA = (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); |
267 | ADCSRA = (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); |
263 | //Set ADC Control and Status Register B |
268 | //Set ADC Control and Status Register B |
264 | //Trigger Source to Free Running Mode |
269 | //Trigger Source to Free Running Mode |
265 | ADCSRB &= ~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)); |
270 | ADCSRB &= ~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)); |
- | 271 | ||
- | 272 | for (uint8_t i=0; i<MAX_AIRPRESSURE_WINDOW_LENGTH; i++) { |
|
- | 273 | airPressureWindow[i] = 0; |
|
- | 274 | } |
|
- | 275 | ||
- | 276 | windowedAirPressure = 0; |
|
266 | 277 | ||
267 | startAnalogConversionCycle(); |
278 | startAnalogConversionCycle(); |
268 | 279 | ||
269 | // restore global interrupt flags |
280 | // restore global interrupt flags |
270 | SREG = sreg; |
281 | SREG = sreg; |
271 | } |
282 | } |
272 | 283 | ||
273 | uint16_t rawGyroValue(uint8_t axis) { |
284 | uint16_t rawGyroValue(uint8_t axis) { |
274 | return sensorInputs[AD_GYRO_PITCH-axis]; |
285 | return sensorInputs[AD_GYRO_PITCH-axis]; |
275 | } |
286 | } |
276 | 287 | ||
277 | uint16_t rawAccValue(uint8_t axis) { |
288 | uint16_t rawAccValue(uint8_t axis) { |
278 | return sensorInputs[AD_ACC_PITCH-axis]; |
289 | return sensorInputs[AD_ACC_PITCH-axis]; |
279 | } |
290 | } |
280 | 291 | ||
281 | void measureNoise(const int16_t sensor, |
292 | void measureNoise(const int16_t sensor, |
282 | volatile uint16_t* const noiseMeasurement, const uint8_t damping) { |
293 | volatile uint16_t* const noiseMeasurement, const uint8_t damping) { |
283 | if (sensor > (int16_t) (*noiseMeasurement)) { |
294 | if (sensor > (int16_t) (*noiseMeasurement)) { |
284 | *noiseMeasurement = sensor; |
295 | *noiseMeasurement = sensor; |
285 | } else if (-sensor > (int16_t) (*noiseMeasurement)) { |
296 | } else if (-sensor > (int16_t) (*noiseMeasurement)) { |
286 | *noiseMeasurement = -sensor; |
297 | *noiseMeasurement = -sensor; |
287 | } else if (*noiseMeasurement > damping) { |
298 | } else if (*noiseMeasurement > damping) { |
288 | *noiseMeasurement -= damping; |
299 | *noiseMeasurement -= damping; |
289 | } else { |
300 | } else { |
290 | *noiseMeasurement = 0; |
301 | *noiseMeasurement = 0; |
291 | } |
302 | } |
292 | } |
303 | } |
293 | 304 | ||
294 | /* |
305 | /* |
295 | * Min.: 0 |
306 | * Min.: 0 |
296 | * Max: About 106 * 240 + 2047 = 27487; it is OK with just a 16 bit type. |
307 | * Max: About 106 * 240 + 2047 = 27487; it is OK with just a 16 bit type. |
297 | */ |
308 | */ |
298 | uint16_t getSimplePressure(int advalue) { |
309 | uint16_t getSimplePressure(int advalue) { |
299 | return (uint16_t) OCR0A * (uint16_t) rangewidth + advalue; |
310 | uint16_t result = (uint16_t) OCR0A * (uint16_t) rangewidth + advalue; |
- | 311 | result += (acc[Z] * (staticParams.airpressureAccZCorrection-128)) >> 10; |
|
- | 312 | return result; |
|
300 | } |
313 | } |
301 | 314 | ||
302 | void startAnalogConversionCycle(void) { |
315 | void startAnalogConversionCycle(void) { |
303 | analogDataReady = 0; |
316 | analogDataReady = 0; |
304 | 317 | ||
305 | // Stop the sampling. Cycle is over. |
318 | // Stop the sampling. Cycle is over. |
306 | for (uint8_t i = 0; i < 8; i++) { |
319 | for (uint8_t i = 0; i < 8; i++) { |
307 | sensorInputs[i] = 0; |
320 | sensorInputs[i] = 0; |
308 | } |
321 | } |
309 | adState = 0; |
322 | adState = 0; |
310 | adChannel = AD_GYRO_PITCH; |
323 | adChannel = AD_GYRO_PITCH; |
311 | ADMUX = (ADMUX & 0xE0) | adChannel; |
324 | ADMUX = (ADMUX & 0xE0) | adChannel; |
312 | startADC(); |
325 | startADC(); |
313 | } |
326 | } |
314 | 327 | ||
315 | /***************************************************** |
328 | /***************************************************** |
316 | * Interrupt Service Routine for ADC |
329 | * Interrupt Service Routine for ADC |
317 | * Runs at 312.5 kHz or 3.2 �s. When all states are |
330 | * Runs at 312.5 kHz or 3.2 �s. When all states are |
318 | * processed further conversions are stopped. |
331 | * processed further conversions are stopped. |
319 | *****************************************************/ |
332 | *****************************************************/ |
320 | ISR(ADC_vect) { |
333 | ISR(ADC_vect) { |
321 | sensorInputs[adChannel] += ADC; |
334 | sensorInputs[adChannel] += ADC; |
322 | // set up for next state. |
335 | // set up for next state. |
323 | adState++; |
336 | adState++; |
324 | if (adState < sizeof(channelsForStates)) { |
337 | if (adState < sizeof(channelsForStates)) { |
325 | adChannel = pgm_read_byte(&channelsForStates[adState]); |
338 | adChannel = pgm_read_byte(&channelsForStates[adState]); |
326 | // set adc muxer to next adChannel |
339 | // set adc muxer to next adChannel |
327 | ADMUX = (ADMUX & 0xE0) | adChannel; |
340 | ADMUX = (ADMUX & 0xE0) | adChannel; |
328 | // after full cycle stop further interrupts |
341 | // after full cycle stop further interrupts |
329 | startADC(); |
342 | startADC(); |
330 | } else { |
343 | } else { |
331 | ADCycleCount++; |
344 | ADCycleCount++; |
332 | analogDataReady = 1; |
345 | analogDataReady = 1; |
333 | // do not restart ADC converter. |
346 | // do not restart ADC converter. |
334 | } |
347 | } |
335 | } |
348 | } |
336 | 349 | ||
337 | void analog_updateGyros(void) { |
350 | void analog_updateGyros(void) { |
338 | // for various filters... |
351 | // for various filters... |
339 | int16_t tempOffsetGyro[2], tempGyro; |
352 | int16_t tempOffsetGyro[2], tempGyro; |
340 | 353 | ||
341 | debugOut.digital[0] &= ~DEBUG_SENSORLIMIT; |
354 | debugOut.digital[0] &= ~DEBUG_SENSORLIMIT; |
342 | for (uint8_t axis=0; axis<2; axis++) { |
355 | for (uint8_t axis=0; axis<2; axis++) { |
343 | tempGyro = rawGyroValue(axis); |
356 | tempGyro = rawGyroValue(axis); |
344 | /* |
357 | /* |
345 | * Process the gyro data for the PID controller. |
358 | * Process the gyro data for the PID controller. |
346 | */ |
359 | */ |
347 | // 1) Extrapolate: Near the ends of the range, we boost the input significantly. This simulates a |
360 | // 1) Extrapolate: Near the ends of the range, we boost the input significantly. This simulates a |
348 | // gyro with a wider range, and helps counter saturation at full control. |
361 | // gyro with a wider range, and helps counter saturation at full control. |
349 | 362 | ||
350 | if (staticParams.bitConfig & CFG_GYRO_SATURATION_PREVENTION) { |
363 | if (staticParams.bitConfig & CFG_GYRO_SATURATION_PREVENTION) { |
351 | if (tempGyro < SENSOR_MIN_PITCHROLL) { |
364 | if (tempGyro < SENSOR_MIN_PITCHROLL) { |
352 | debugOut.digital[0] |= DEBUG_SENSORLIMIT; |
365 | debugOut.digital[0] |= DEBUG_SENSORLIMIT; |
353 | tempGyro = tempGyro * EXTRAPOLATION_SLOPE - EXTRAPOLATION_LIMIT; |
366 | tempGyro = tempGyro * EXTRAPOLATION_SLOPE - EXTRAPOLATION_LIMIT; |
354 | } else if (tempGyro > SENSOR_MAX_PITCHROLL) { |
367 | } else if (tempGyro > SENSOR_MAX_PITCHROLL) { |
355 | debugOut.digital[0] |= DEBUG_SENSORLIMIT; |
368 | debugOut.digital[0] |= DEBUG_SENSORLIMIT; |
356 | tempGyro = (tempGyro - SENSOR_MAX_PITCHROLL) * EXTRAPOLATION_SLOPE + SENSOR_MAX_PITCHROLL; |
369 | tempGyro = (tempGyro - SENSOR_MAX_PITCHROLL) * EXTRAPOLATION_SLOPE + SENSOR_MAX_PITCHROLL; |
357 | } |
370 | } |
358 | } |
371 | } |
359 | 372 | ||
360 | // 2) Apply sign and offset, scale before filtering. |
373 | // 2) Apply sign and offset, scale before filtering. |
361 | tempOffsetGyro[axis] = (tempGyro - gyroOffset.offsets[axis]) * GYRO_FACTOR_PITCHROLL; |
374 | tempOffsetGyro[axis] = (tempGyro - gyroOffset.offsets[axis]) * GYRO_FACTOR_PITCHROLL; |
362 | } |
375 | } |
363 | 376 | ||
364 | // 2.1: Transform axes. |
377 | // 2.1: Transform axes. |
365 | rotate(tempOffsetGyro, staticParams.gyroQuadrant, staticParams.imuReversedFlags & IMU_REVERSE_GYRO_PR); |
378 | rotate(tempOffsetGyro, staticParams.gyroQuadrant, staticParams.imuReversedFlags & IMU_REVERSE_GYRO_PR); |
366 | 379 | ||
367 | for (uint8_t axis=0; axis<2; axis++) { |
380 | for (uint8_t axis=0; axis<2; axis++) { |
368 | // 3) Filter. |
381 | // 3) Filter. |
369 | tempOffsetGyro[axis] = (gyro_PID[axis] * (staticParams.gyroPIDFilterConstant - 1) + tempOffsetGyro[axis]) / staticParams.gyroPIDFilterConstant; |
382 | tempOffsetGyro[axis] = (gyro_PID[axis] * (staticParams.gyroPIDFilterConstant - 1) + tempOffsetGyro[axis]) / staticParams.gyroPIDFilterConstant; |
370 | 383 | ||
371 | // 4) Measure noise. |
384 | // 4) Measure noise. |
372 | measureNoise(tempOffsetGyro[axis], &gyroNoisePeak[axis], GYRO_NOISE_MEASUREMENT_DAMPING); |
385 | measureNoise(tempOffsetGyro[axis], &gyroNoisePeak[axis], GYRO_NOISE_MEASUREMENT_DAMPING); |
373 | 386 | ||
374 | // 5) Differential measurement. |
387 | // 5) Differential measurement. |
375 | gyroD[axis] = (gyroD[axis] * (staticParams.gyroDFilterConstant - 1) + (tempOffsetGyro[axis] - gyro_PID[axis])) / staticParams.gyroDFilterConstant; |
388 | gyroD[axis] = (gyroD[axis] * (staticParams.gyroDFilterConstant - 1) + (tempOffsetGyro[axis] - gyro_PID[axis])) / staticParams.gyroDFilterConstant; |
376 | 389 | ||
377 | // 6) Done. |
390 | // 6) Done. |
378 | gyro_PID[axis] = tempOffsetGyro[axis]; |
391 | gyro_PID[axis] = tempOffsetGyro[axis]; |
379 | 392 | ||
380 | // Prepare tempOffsetGyro for next calculation below... |
393 | // Prepare tempOffsetGyro for next calculation below... |
381 | tempOffsetGyro[axis] = (rawGyroValue(axis) - gyroOffset.offsets[axis]) * GYRO_FACTOR_PITCHROLL; |
394 | tempOffsetGyro[axis] = (rawGyroValue(axis) - gyroOffset.offsets[axis]) * GYRO_FACTOR_PITCHROLL; |
382 | } |
395 | } |
383 | 396 | ||
384 | /* |
397 | /* |
385 | * Now process the data for attitude angles. |
398 | * Now process the data for attitude angles. |
386 | */ |
399 | */ |
387 | rotate(tempOffsetGyro, staticParams.gyroQuadrant, staticParams.imuReversedFlags & IMU_REVERSE_GYRO_PR); |
400 | rotate(tempOffsetGyro, staticParams.gyroQuadrant, staticParams.imuReversedFlags & IMU_REVERSE_GYRO_PR); |
388 | 401 | ||
389 | gyro_ATT[PITCH] = tempOffsetGyro[PITCH]; |
402 | gyro_ATT[PITCH] = tempOffsetGyro[PITCH]; |
390 | gyro_ATT[ROLL] = tempOffsetGyro[ROLL]; |
403 | gyro_ATT[ROLL] = tempOffsetGyro[ROLL]; |
391 | 404 | ||
392 | debugOut.analog[22 + 0] = gyro_PID[0]; |
405 | debugOut.analog[22 + 0] = gyro_PID[0]; |
393 | debugOut.analog[22 + 1] = gyro_PID[1]; |
406 | debugOut.analog[22 + 1] = gyro_PID[1]; |
394 | 407 | ||
395 | debugOut.analog[24 + 0] = gyro_ATT[0]; |
408 | debugOut.analog[24 + 0] = gyro_ATT[0]; |
396 | debugOut.analog[24 + 1] = gyro_ATT[1]; |
409 | debugOut.analog[24 + 1] = gyro_ATT[1]; |
397 | 410 | ||
398 | // 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. |
411 | // 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. |
399 | // gyro_ATT[PITCH] = (gyro_ATT[PITCH] * (staticParams.attitudeGyroFilter - 1) + tempOffsetGyro[PITCH]) / staticParams.attitudeGyroFilter; |
412 | // gyro_ATT[PITCH] = (gyro_ATT[PITCH] * (staticParams.attitudeGyroFilter - 1) + tempOffsetGyro[PITCH]) / staticParams.attitudeGyroFilter; |
400 | // gyro_ATT[ROLL] = (gyro_ATT[ROLL] * (staticParams.attitudeGyroFilter - 1) + tempOffsetGyro[ROLL]) / staticParams.attitudeGyroFilter; |
413 | // gyro_ATT[ROLL] = (gyro_ATT[ROLL] * (staticParams.attitudeGyroFilter - 1) + tempOffsetGyro[ROLL]) / staticParams.attitudeGyroFilter; |
401 | 414 | ||
402 | // Yaw gyro. |
415 | // Yaw gyro. |
403 | if (staticParams.imuReversedFlags & IMU_REVERSE_GYRO_YAW) |
416 | if (staticParams.imuReversedFlags & IMU_REVERSE_GYRO_YAW) |
404 | yawGyro = gyroOffset.offsets[YAW] - sensorInputs[AD_GYRO_YAW]; |
417 | yawGyro = gyroOffset.offsets[YAW] - sensorInputs[AD_GYRO_YAW]; |
405 | else |
418 | else |
406 | yawGyro = sensorInputs[AD_GYRO_YAW] - gyroOffset.offsets[YAW]; |
419 | yawGyro = sensorInputs[AD_GYRO_YAW] - gyroOffset.offsets[YAW]; |
407 | } |
420 | } |
408 | 421 | ||
409 | void analog_updateAccelerometers(void) { |
422 | void analog_updateAccelerometers(void) { |
410 | // Pitch and roll axis accelerations. |
423 | // Pitch and roll axis accelerations. |
411 | for (uint8_t axis=0; axis<2; axis++) { |
424 | for (uint8_t axis=0; axis<2; axis++) { |
412 | acc[axis] = rawAccValue(axis) - accOffset.offsets[axis]; |
425 | acc[axis] = rawAccValue(axis) - accOffset.offsets[axis]; |
413 | } |
426 | } |
414 | 427 | ||
415 | rotate(acc, staticParams.accQuadrant, staticParams.imuReversedFlags & IMU_REVERSE_ACC_XY); |
428 | rotate(acc, staticParams.accQuadrant, staticParams.imuReversedFlags & IMU_REVERSE_ACC_XY); |
416 | 429 | ||
417 | for(uint8_t axis=0; axis<3; axis++) { |
430 | for(uint8_t axis=0; axis<3; axis++) { |
418 | filteredAcc[axis] = (filteredAcc[axis] * (staticParams.accFilterConstant - 1) + acc[axis]) / staticParams.accFilterConstant; |
431 | filteredAcc[axis] = (filteredAcc[axis] * (staticParams.accFilterConstant - 1) + acc[axis]) / staticParams.accFilterConstant; |
419 | measureNoise(acc[axis], &accNoisePeak[axis], 1); |
432 | measureNoise(acc[axis], &accNoisePeak[axis], 1); |
420 | } |
433 | } |
421 | 434 | ||
422 | // Z acc. |
435 | // Z acc. |
423 | if (staticParams.imuReversedFlags & 8) |
436 | if (staticParams.imuReversedFlags & 8) |
424 | acc[Z] = accOffset.offsets[Z] - sensorInputs[AD_ACC_Z]; |
437 | acc[Z] = accOffset.offsets[Z] - sensorInputs[AD_ACC_Z]; |
425 | else |
438 | else |
426 | acc[Z] = sensorInputs[AD_ACC_Z] - accOffset.offsets[Z]; |
439 | acc[Z] = sensorInputs[AD_ACC_Z] - accOffset.offsets[Z]; |
427 | } |
440 | } |
428 | 441 | ||
429 | void analog_updateAirPressure(void) { |
442 | void analog_updateAirPressure(void) { |
430 | static uint16_t pressureAutorangingWait = 25; |
443 | static uint16_t pressureAutorangingWait = 25; |
431 | uint16_t rawAirPressure; |
444 | uint16_t rawAirPressure; |
432 | int16_t newrange; |
445 | int16_t newrange; |
433 | // air pressure |
446 | // air pressure |
434 | if (pressureAutorangingWait) { |
447 | if (pressureAutorangingWait) { |
435 | //A range switch was done recently. Wait for steadying. |
448 | //A range switch was done recently. Wait for steadying. |
436 | pressureAutorangingWait--; |
449 | pressureAutorangingWait--; |
437 | debugOut.analog[27] = (uint16_t) OCR0A; |
- | |
438 | debugOut.analog[31] = simpleAirPressure; |
- | |
439 | } else { |
450 | } else { |
440 | rawAirPressure = sensorInputs[AD_AIRPRESSURE]; |
451 | rawAirPressure = sensorInputs[AD_AIRPRESSURE]; |
441 | if (rawAirPressure < MIN_RAWPRESSURE) { |
452 | if (rawAirPressure < MIN_RAWPRESSURE) { |
442 | // value is too low, so decrease voltage on the op amp minus input, making the value higher. |
453 | // value is too low, so decrease voltage on the op amp minus input, making the value higher. |
443 | newrange = OCR0A - (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (MAX_RAWPRESSURE - rawAirPressure) / (rangewidth * 2) + 1; |
454 | newrange = OCR0A - (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (MAX_RAWPRESSURE - rawAirPressure) / (rangewidth * 2) + 1; |
444 | if (newrange > MIN_RANGES_EXTRAPOLATION) { |
455 | if (newrange > MIN_RANGES_EXTRAPOLATION) { |
445 | pressureAutorangingWait = (OCR0A - newrange) * AUTORANGE_WAIT_FACTOR; // = OCRA0 - OCRA0 + |
456 | pressureAutorangingWait = (OCR0A - newrange) * AUTORANGE_WAIT_FACTOR; // = OCRA0 - OCRA0 + |
446 | OCR0A = newrange; |
457 | OCR0A = newrange; |
447 | } else { |
458 | } else { |
448 | if (OCR0A) { |
459 | if (OCR0A) { |
449 | OCR0A--; |
460 | OCR0A--; |
450 | pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
461 | pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
451 | } |
462 | } |
452 | } |
463 | } |
453 | } else if (rawAirPressure > MAX_RAWPRESSURE) { |
464 | } else if (rawAirPressure > MAX_RAWPRESSURE) { |
454 | // value is too high, so increase voltage on the op amp minus input, making the value lower. |
465 | // value is too high, so increase voltage on the op amp minus input, making the value lower. |
455 | // If near the end, make a limited increase |
466 | // If near the end, make a limited increase |
456 | newrange = OCR0A + (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (rawAirPressure - MIN_RAWPRESSURE) / (rangewidth * 2) - 1; |
467 | newrange = OCR0A + (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (rawAirPressure - MIN_RAWPRESSURE) / (rangewidth * 2) - 1; |
457 | if (newrange < MAX_RANGES_EXTRAPOLATION) { |
468 | if (newrange < MAX_RANGES_EXTRAPOLATION) { |
458 | pressureAutorangingWait = (newrange - OCR0A) * AUTORANGE_WAIT_FACTOR; |
469 | pressureAutorangingWait = (newrange - OCR0A) * AUTORANGE_WAIT_FACTOR; |
459 | OCR0A = newrange; |
470 | OCR0A = newrange; |
460 | } else { |
471 | } else { |
461 | if (OCR0A < 254) { |
472 | if (OCR0A < 254) { |
462 | OCR0A++; |
473 | OCR0A++; |
463 | pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
474 | pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
464 | } |
475 | } |
465 | } |
476 | } |
466 | } |
477 | } |
467 | 478 | ||
468 | // Even if the sample is off-range, use it. |
479 | // Even if the sample is off-range, use it. |
469 | simpleAirPressure = getSimplePressure(rawAirPressure); |
480 | simpleAirPressure = getSimplePressure(rawAirPressure); |
470 | debugOut.analog[27] = (uint16_t) OCR0A; |
- | |
471 | debugOut.analog[31] = simpleAirPressure; |
- | |
472 | 481 | ||
473 | if (simpleAirPressure < MIN_RANGES_EXTRAPOLATION * rangewidth) { |
482 | if (simpleAirPressure < MIN_RANGES_EXTRAPOLATION * rangewidth) { |
474 | // Danger: pressure near lower end of range. If the measurement saturates, the |
483 | // Danger: pressure near lower end of range. If the measurement saturates, the |
475 | // copter may climb uncontrolledly... Simulate a drastic reduction in pressure. |
484 | // copter may climb uncontrolledly... Simulate a drastic reduction in pressure. |
476 | debugOut.digital[1] |= DEBUG_SENSORLIMIT; |
485 | debugOut.digital[1] |= DEBUG_SENSORLIMIT; |
477 | airPressureSum += (int16_t) MIN_RANGES_EXTRAPOLATION * rangewidth |
486 | airPressureSum += (int16_t) MIN_RANGES_EXTRAPOLATION * rangewidth |
478 | + (simpleAirPressure - (int16_t) MIN_RANGES_EXTRAPOLATION |
487 | + (simpleAirPressure - (int16_t) MIN_RANGES_EXTRAPOLATION |
479 | * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
488 | * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
480 | } else if (simpleAirPressure > MAX_RANGES_EXTRAPOLATION * rangewidth) { |
489 | } else if (simpleAirPressure > MAX_RANGES_EXTRAPOLATION * rangewidth) { |
481 | // Danger: pressure near upper end of range. If the measurement saturates, the |
490 | // Danger: pressure near upper end of range. If the measurement saturates, the |
482 | // copter may descend uncontrolledly... Simulate a drastic increase in pressure. |
491 | // copter may descend uncontrolledly... Simulate a drastic increase in pressure. |
483 | debugOut.digital[1] |= DEBUG_SENSORLIMIT; |
492 | debugOut.digital[1] |= DEBUG_SENSORLIMIT; |
484 | airPressureSum += (int16_t) MAX_RANGES_EXTRAPOLATION * rangewidth |
493 | airPressureSum += (int16_t) MAX_RANGES_EXTRAPOLATION * rangewidth |
485 | + (simpleAirPressure - (int16_t) MAX_RANGES_EXTRAPOLATION |
494 | + (simpleAirPressure - (int16_t) MAX_RANGES_EXTRAPOLATION |
486 | * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
495 | * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
487 | } else { |
496 | } else { |
488 | // normal case. |
497 | // normal case. |
489 | // If AIRPRESSURE_SUMMATION_FACTOR is an odd number we only want to add half the double sample. |
498 | // If AIRPRESSURE_OVERSAMPLING is an odd number we only want to add half the double sample. |
490 | // The 2 cases above (end of range) are ignored for this. |
499 | // The 2 cases above (end of range) are ignored for this. |
491 | debugOut.digital[1] &= ~DEBUG_SENSORLIMIT; |
500 | debugOut.digital[1] &= ~DEBUG_SENSORLIMIT; |
492 | if (pressureMeasurementCount == AIRPRESSURE_OVERSAMPLING - 1) |
501 | if (pressureMeasurementCount == AIRPRESSURE_OVERSAMPLING - 1) |
493 | airPressureSum += simpleAirPressure / 2; |
502 | airPressureSum += simpleAirPressure / 2; |
494 | else |
503 | else |
495 | airPressureSum += simpleAirPressure; |
504 | airPressureSum += simpleAirPressure; |
496 | } |
505 | } |
497 | 506 | ||
498 | // 2 samples were added. |
507 | // 2 samples were added. |
499 | pressureMeasurementCount += 2; |
508 | pressureMeasurementCount += 2; |
500 | if (pressureMeasurementCount >= AIRPRESSURE_OVERSAMPLING) { |
509 | if (pressureMeasurementCount >= AIRPRESSURE_OVERSAMPLING) { |
501 | filteredAirPressure = (filteredAirPressure * (AIRPRESSURE_FILTER - 1) |
510 | filteredAirPressure = (filteredAirPressure * (AIRPRESSURE_FILTER - 1) |
502 | + airPressureSum + AIRPRESSURE_FILTER / 2) / AIRPRESSURE_FILTER; |
511 | + airPressureSum + AIRPRESSURE_FILTER / 2) / AIRPRESSURE_FILTER; |
503 | pressureMeasurementCount = airPressureSum = 0; |
512 | pressureMeasurementCount = airPressureSum = 0; |
504 | } |
513 | } |
- | 514 | //int16_t airPressureWindow[MAX_AIRPRESSURE_WINDOW_LENGTH]; |
|
- | 515 | //int32_t windowedAirPressure = 0; |
|
- | 516 | //uint8_t windowPtr; |
|
- | 517 | windowedAirPressure += simpleAirPressure; |
|
- | 518 | windowedAirPressure -= airPressureWindow[windowPtr]; |
|
- | 519 | airPressureWindow[windowPtr] = simpleAirPressure; |
|
- | 520 | windowPtr = (windowPtr+1) % MAX_AIRPRESSURE_WINDOW_LENGTH; |
|
505 | } |
521 | } |
506 | } |
522 | } |
507 | 523 | ||
508 | void analog_updateBatteryVoltage(void) { |
524 | void analog_updateBatteryVoltage(void) { |
509 | // Battery. The measured value is: (V * 1k/11k)/3v * 1024 = 31.03 counts per volt (max. measurable is 33v). |
525 | // Battery. The measured value is: (V * 1k/11k)/3v * 1024 = 31.03 counts per volt (max. measurable is 33v). |
510 | // This is divided by 3 --> 10.34 counts per volt. |
526 | // This is divided by 3 --> 10.34 counts per volt. |
511 | UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4; |
527 | UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4; |
512 | debugOut.analog[11] = UBat; |
528 | debugOut.analog[11] = UBat; |
513 | } |
529 | } |
514 | 530 | ||
515 | void analog_update(void) { |
531 | void analog_update(void) { |
516 | analog_updateGyros(); |
532 | analog_updateGyros(); |
517 | analog_updateAccelerometers(); |
533 | analog_updateAccelerometers(); |
518 | analog_updateAirPressure(); |
534 | analog_updateAirPressure(); |
519 | analog_updateBatteryVoltage(); |
535 | analog_updateBatteryVoltage(); |
520 | } |
536 | } |
521 | 537 | ||
522 | void analog_setNeutral() { |
538 | void analog_setNeutral() { |
523 | gyro_init(); |
539 | gyro_init(); |
524 | 540 | ||
525 | if (gyroOffset_readFromEEProm()) { |
541 | if (gyroOffset_readFromEEProm()) { |
526 | printf("gyro offsets invalid%s",recal); |
542 | printf("gyro offsets invalid%s",recal); |
527 | gyroOffset.offsets[PITCH] = gyroOffset.offsets[ROLL] = 512 * GYRO_OVERSAMPLING_PITCHROLL; |
543 | gyroOffset.offsets[PITCH] = gyroOffset.offsets[ROLL] = 512 * GYRO_OVERSAMPLING_PITCHROLL; |
528 | gyroOffset.offsets[YAW] = 512 * GYRO_OVERSAMPLING_YAW; |
544 | gyroOffset.offsets[YAW] = 512 * GYRO_OVERSAMPLING_YAW; |
529 | } |
545 | } |
530 | 546 | ||
531 | if (accOffset_readFromEEProm()) { |
547 | if (accOffset_readFromEEProm()) { |
532 | printf("acc. meter offsets invalid%s",recal); |
548 | printf("acc. meter offsets invalid%s",recal); |
533 | accOffset.offsets[PITCH] = accOffset.offsets[ROLL] = 512 * ACC_OVERSAMPLING_XY; |
549 | accOffset.offsets[PITCH] = accOffset.offsets[ROLL] = 512 * ACC_OVERSAMPLING_XY; |
534 | accOffset.offsets[Z] = 717 * ACC_OVERSAMPLING_Z; |
550 | accOffset.offsets[Z] = 717 * ACC_OVERSAMPLING_Z; |
535 | } |
551 | } |
536 | 552 | ||
537 | // Noise is relative to offset. So, reset noise measurements when changing offsets. |
553 | // Noise is relative to offset. So, reset noise measurements when changing offsets. |
538 | gyroNoisePeak[PITCH] = gyroNoisePeak[ROLL] = 0; |
554 | gyroNoisePeak[PITCH] = gyroNoisePeak[ROLL] = 0; |
539 | accNoisePeak[PITCH] = accNoisePeak[ROLL] = 0; |
555 | accNoisePeak[PITCH] = accNoisePeak[ROLL] = 0; |
540 | 556 | ||
541 | // Setting offset values has an influence in the analog.c ISR |
557 | // Setting offset values has an influence in the analog.c ISR |
542 | // Therefore run measurement for 100ms to achive stable readings |
558 | // Therefore run measurement for 100ms to achive stable readings |
543 | delay_ms_with_adc_measurement(100, 0); |
559 | delay_ms_with_adc_measurement(100, 0); |
544 | 560 | ||
545 | // Rough estimate. Hmm no nothing happens at calibration anyway. |
561 | // Rough estimate. Hmm no nothing happens at calibration anyway. |
546 | // airPressureSum = simpleAirPressure * (AIRPRESSURE_OVERSAMPLING/2); |
562 | // airPressureSum = simpleAirPressure * (AIRPRESSURE_OVERSAMPLING/2); |
547 | // pressureMeasurementCount = 0; |
563 | // pressureMeasurementCount = 0; |
548 | } |
564 | } |
549 | 565 | ||
550 | void analog_calibrateGyros(void) { |
566 | void analog_calibrateGyros(void) { |
551 | #define GYRO_OFFSET_CYCLES 32 |
567 | #define GYRO_OFFSET_CYCLES 32 |
552 | uint8_t i, axis; |
568 | uint8_t i, axis; |
553 | int32_t offsets[3] = { 0, 0, 0 }; |
569 | int32_t offsets[3] = { 0, 0, 0 }; |
554 | gyro_calibrate(); |
570 | gyro_calibrate(); |
555 | 571 | ||
556 | // determine gyro bias by averaging (requires that the copter does not rotate around any axis!) |
572 | // determine gyro bias by averaging (requires that the copter does not rotate around any axis!) |
557 | for (i = 0; i < GYRO_OFFSET_CYCLES; i++) { |
573 | for (i = 0; i < GYRO_OFFSET_CYCLES; i++) { |
558 | delay_ms_with_adc_measurement(10, 1); |
574 | delay_ms_with_adc_measurement(10, 1); |
559 | for (axis = PITCH; axis <= YAW; axis++) { |
575 | for (axis = PITCH; axis <= YAW; axis++) { |
560 | offsets[axis] += rawGyroValue(axis); |
576 | offsets[axis] += rawGyroValue(axis); |
561 | } |
577 | } |
562 | } |
578 | } |
563 | 579 | ||
564 | for (axis = PITCH; axis <= YAW; axis++) { |
580 | for (axis = PITCH; axis <= YAW; axis++) { |
565 | gyroOffset.offsets[axis] = (offsets[axis] + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
581 | gyroOffset.offsets[axis] = (offsets[axis] + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
566 | 582 | ||
567 | int16_t min = (512-200) * (axis==YAW) ? GYRO_OVERSAMPLING_YAW : GYRO_OVERSAMPLING_PITCHROLL; |
583 | int16_t min = (512-200) * (axis==YAW) ? GYRO_OVERSAMPLING_YAW : GYRO_OVERSAMPLING_PITCHROLL; |
568 | int16_t max = (512+200) * (axis==YAW) ? GYRO_OVERSAMPLING_YAW : GYRO_OVERSAMPLING_PITCHROLL; |
584 | int16_t max = (512+200) * (axis==YAW) ? GYRO_OVERSAMPLING_YAW : GYRO_OVERSAMPLING_PITCHROLL; |
569 | if(gyroOffset.offsets[axis] < min || gyroOffset.offsets[axis] > max) |
585 | if(gyroOffset.offsets[axis] < min || gyroOffset.offsets[axis] > max) |
570 | versionInfo.hardwareErrors[0] |= FC_ERROR0_GYRO_PITCH << axis; |
586 | versionInfo.hardwareErrors[0] |= FC_ERROR0_GYRO_PITCH << axis; |
571 | } |
587 | } |
572 | 588 | ||
573 | gyroOffset_writeToEEProm(); |
589 | gyroOffset_writeToEEProm(); |
574 | startAnalogConversionCycle(); |
590 | startAnalogConversionCycle(); |
575 | } |
591 | } |
576 | 592 | ||
577 | /* |
593 | /* |
578 | * Find acc. offsets for a neutral reading, and write them to EEPROM. |
594 | * Find acc. offsets for a neutral reading, and write them to EEPROM. |
579 | * Does not (!} update the local variables. This must be done with a |
595 | * Does not (!} update the local variables. This must be done with a |
580 | * call to analog_calibrate() - this always (?) is done by the caller |
596 | * call to analog_calibrate() - this always (?) is done by the caller |
581 | * anyway. There would be nothing wrong with updating the variables |
597 | * anyway. There would be nothing wrong with updating the variables |
582 | * directly from here, though. |
598 | * directly from here, though. |
583 | */ |
599 | */ |
584 | void analog_calibrateAcc(void) { |
600 | void analog_calibrateAcc(void) { |
585 | #define ACC_OFFSET_CYCLES 32 |
601 | #define ACC_OFFSET_CYCLES 32 |
586 | uint8_t i, axis; |
602 | uint8_t i, axis; |
587 | int32_t offsets[3] = { 0, 0, 0 }; |
603 | int32_t offsets[3] = { 0, 0, 0 }; |
588 | 604 | ||
589 | for (i = 0; i < ACC_OFFSET_CYCLES; i++) { |
605 | for (i = 0; i < ACC_OFFSET_CYCLES; i++) { |
590 | delay_ms_with_adc_measurement(10, 1); |
606 | delay_ms_with_adc_measurement(10, 1); |
591 | for (axis = PITCH; axis <= YAW; axis++) { |
607 | for (axis = PITCH; axis <= YAW; axis++) { |
592 | offsets[axis] += rawAccValue(axis); |
608 | offsets[axis] += rawAccValue(axis); |
593 | } |
609 | } |
594 | } |
610 | } |
595 | 611 | ||
596 | for (axis = PITCH; axis <= YAW; axis++) { |
612 | for (axis = PITCH; axis <= YAW; axis++) { |
597 | accOffset.offsets[axis] = (offsets[axis] + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES; |
613 | accOffset.offsets[axis] = (offsets[axis] + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES; |
598 | int16_t min,max; |
614 | int16_t min,max; |
599 | if (axis==Z) { |
615 | if (axis==Z) { |
600 | if (staticParams.imuReversedFlags & IMU_REVERSE_ACC_Z) { |
616 | if (staticParams.imuReversedFlags & IMU_REVERSE_ACC_Z) { |
601 | // TODO: This assumes a sensitivity of +/- 2g. |
617 | // TODO: This assumes a sensitivity of +/- 2g. |
602 | min = (256-200) * ACC_OVERSAMPLING_Z; |
618 | min = (256-200) * ACC_OVERSAMPLING_Z; |
603 | max = (256+200) * ACC_OVERSAMPLING_Z; |
619 | max = (256+200) * ACC_OVERSAMPLING_Z; |
604 | } else { |
620 | } else { |
605 | // TODO: This assumes a sensitivity of +/- 2g. |
621 | // TODO: This assumes a sensitivity of +/- 2g. |
606 | min = (768-200) * ACC_OVERSAMPLING_Z; |
622 | min = (768-200) * ACC_OVERSAMPLING_Z; |
607 | max = (768+200) * ACC_OVERSAMPLING_Z; |
623 | max = (768+200) * ACC_OVERSAMPLING_Z; |
608 | } |
624 | } |
609 | } else { |
625 | } else { |
610 | min = (512-200) * ACC_OVERSAMPLING_XY; |
626 | min = (512-200) * ACC_OVERSAMPLING_XY; |
611 | max = (512+200) * ACC_OVERSAMPLING_XY; |
627 | max = (512+200) * ACC_OVERSAMPLING_XY; |
612 | } |
628 | } |
613 | if(gyroOffset.offsets[axis] < min || gyroOffset.offsets[axis] > max) { |
629 | if(gyroOffset.offsets[axis] < min || gyroOffset.offsets[axis] > max) { |
614 | versionInfo.hardwareErrors[0] |= FC_ERROR0_ACC_X << axis; |
630 | versionInfo.hardwareErrors[0] |= FC_ERROR0_ACC_X << axis; |
615 | } |
631 | } |
616 | } |
632 | } |
617 | 633 | ||
618 | accOffset_writeToEEProm(); |
634 | accOffset_writeToEEProm(); |
619 | startAnalogConversionCycle(); |
635 | startAnalogConversionCycle(); |
620 | } |
636 | } |
621 | 637 |