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