Rev 1634 | Rev 1646 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1634 | Rev 1635 | ||
---|---|---|---|
1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + Copyright (c) 04.2007 Holger Buss |
2 | // + Copyright (c) 04.2007 Holger Buss |
3 | // + Nur für den privaten Gebrauch |
3 | // + Nur für den privaten Gebrauch |
4 | // + www.MikroKopter.com |
4 | // + www.MikroKopter.com |
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
6 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
6 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
7 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
7 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
8 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
8 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
9 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
9 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
10 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
10 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
11 | // + Verkauf von Luftbildaufnahmen, usw. |
11 | // + Verkauf von Luftbildaufnahmen, usw. |
12 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
12 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
13 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
13 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
14 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
14 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
15 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
15 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
16 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
16 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
17 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
17 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
18 | // + eindeutig als Ursprung verlinkt werden |
18 | // + eindeutig als Ursprung verlinkt werden |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
20 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
20 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
21 | // + Benutzung auf eigene Gefahr |
21 | // + Benutzung auf eigene Gefahr |
22 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
22 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
24 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
24 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
25 | // + mit unserer Zustimmung zulässig |
25 | // + mit unserer Zustimmung zulässig |
26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
27 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
27 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
29 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
29 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
30 | // + this list of conditions and the following disclaimer. |
30 | // + this list of conditions and the following disclaimer. |
31 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
31 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
32 | // + from this software without specific prior written permission. |
32 | // + from this software without specific prior written permission. |
33 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
33 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
34 | // + for non-commercial use (directly or indirectly) |
34 | // + for non-commercial use (directly or indirectly) |
35 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
35 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
36 | // + with our written permission |
36 | // + with our written permission |
37 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
37 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
38 | // + clearly linked as origin |
38 | // + clearly linked as origin |
39 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
39 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
40 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
40 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
41 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
41 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
42 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
42 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
43 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
43 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
44 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
44 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
45 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
45 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
46 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
46 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
47 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
47 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
48 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
48 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
49 | // + POSSIBILITY OF SUCH DAMAGE. |
49 | // + POSSIBILITY OF SUCH DAMAGE. |
50 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
50 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
51 | #include <avr/io.h> |
51 | #include <avr/io.h> |
52 | #include <avr/interrupt.h> |
52 | #include <avr/interrupt.h> |
53 | #include <avr/pgmspace.h> |
53 | #include <avr/pgmspace.h> |
54 | #include "analog.h" |
54 | #include "analog.h" |
55 | 55 | ||
56 | #include "sensors.h" |
56 | #include "sensors.h" |
57 | 57 | ||
58 | // for Delay functions |
58 | // for Delay functions |
59 | #include "timer0.h" |
59 | #include "timer0.h" |
60 | 60 | ||
61 | // For DebugOut |
61 | // For DebugOut |
62 | #include "uart0.h" |
62 | #include "uart0.h" |
63 | 63 | ||
64 | // For reading and writing acc. meter offsets. |
64 | // For reading and writing acc. meter offsets. |
65 | #include "eeprom.h" |
65 | #include "eeprom.h" |
66 | 66 | ||
67 | /* |
67 | /* |
68 | * Arrays could have been used arrays for the 2 * 3 axes, but despite some repetition, |
68 | * Arrays could have been used for the 2 * 3 axes, but despite some repetition, |
69 | * the code is easier to read without. |
69 | * the code is easier to read without. |
70 | * |
70 | * |
71 | * For each A/D conversion cycle, each channel (eg. the yaw gyro, or the Z axis |
71 | * For each A/D conversion cycle, each channel (eg. the yaw gyro, or the Z axis |
72 | * accelerometer) is sampled a number of times (see array channelsForStates), and |
72 | * accelerometer) is sampled a number of times (see array channelsForStates), and |
73 | * the results for each channel are summed. Here are those for the gyros and the |
73 | * the results for each channel are summed. Here are those for the gyros and the |
74 | * acc. meters. They are not zero-offset. |
74 | * acc. meters. They are not zero-offset. |
75 | * They are exported in the analog.h file - but please do not use them! The only |
75 | * They are exported in the analog.h file - but please do not use them! The only |
76 | * reason for the export is that the ENC-03_FC1.3 modules needs them for calibrating |
76 | * reason for the export is that the ENC-03_FC1.3 modules needs them for calibrating |
77 | * the offsets with the DAC. |
77 | * the offsets with the DAC. |
78 | */ |
78 | */ |
79 | volatile int16_t rawPitchGyroSum, rawRollGyroSum, rawYawGyroSum; |
79 | volatile int16_t rawPitchGyroSum, rawRollGyroSum, rawYawGyroSum; |
80 | volatile int16_t pitchAxisAcc = 0, rollAxisAcc = 0, ZAxisAcc = 0; |
80 | volatile int16_t pitchAxisAcc = 0, rollAxisAcc = 0, ZAxisAcc = 0; |
81 | volatile int16_t filteredPitchAxisAcc = 0, filteredRollAxisAcc = 0; |
81 | volatile int16_t filteredPitchAxisAcc = 0, filteredRollAxisAcc = 0; |
82 | 82 | ||
83 | // that float one - "Top" - is missing. |
83 | // that float one - "Top" - is missing. |
84 | 84 | ||
85 | /* |
85 | /* |
86 | * These 4 exported variables are zero-offset. The "filtered" ones are |
86 | * These 4 exported variables are zero-offset. The "filtered" ones are |
87 | * (if configured to with the GYROS_SECONDORDERFILTER define) low pass |
87 | * (if configured to with the GYROS_SECONDORDERFILTER define) low pass |
88 | * filtered versions of the other 2. |
88 | * filtered versions of the other 2. |
89 | * They are derived from the "raw" values above, by zero-offsetting. |
89 | * They are derived from the "raw" values above, by zero-offsetting. |
90 | */ |
90 | */ |
91 | volatile int16_t hiResPitchGyro = 0, hiResRollGyro = 0; |
91 | volatile int16_t hiResPitchGyro = 0, hiResRollGyro = 0; |
92 | volatile int16_t filteredHiResPitchGyro = 0, filteredHiResRollGyro = 0; |
92 | volatile int16_t filteredHiResPitchGyro = 0, filteredHiResRollGyro = 0; |
93 | volatile int16_t pitchGyroD = 0, rollGyroD = 0; |
93 | volatile int16_t pitchGyroD = 0, rollGyroD = 0; |
94 | volatile int16_t yawGyro = 0; |
94 | volatile int16_t yawGyro = 0; |
95 | 95 | ||
96 | /* |
96 | /* |
97 | * Offset values. These are the raw gyro and acc. meter sums when the copter is |
97 | * Offset values. These are the raw gyro and acc. meter sums when the copter is |
98 | * standing still. They are used for adjusting the gyro and acc. meter values |
98 | * standing still. They are used for adjusting the gyro and acc. meter values |
99 | * to be zero when the copter stands still. |
99 | * to be zero when the copter stands still. |
100 | */ |
100 | */ |
101 | volatile int16_t pitchOffset, rollOffset, yawOffset; |
101 | volatile int16_t pitchOffset, rollOffset, yawOffset; |
102 | volatile int16_t pitchAxisAccOffset, rollAxisAccOffset, ZAxisAccOffset; |
102 | volatile int16_t pitchAxisAccOffset, rollAxisAccOffset, ZAxisAccOffset; |
103 | 103 | ||
104 | /* |
104 | /* |
105 | * This allows some experimentation with the gyro filters. |
105 | * This allows some experimentation with the gyro filters. |
106 | * Should be replaced by #define's later on... |
106 | * Should be replaced by #define's later on... |
107 | */ |
107 | */ |
108 | volatile uint8_t GYROS_FIRSTORDERFILTER; |
108 | volatile uint8_t GYROS_FIRSTORDERFILTER; |
109 | volatile uint8_t GYROS_SECONDORDERFILTER; |
109 | volatile uint8_t GYROS_SECONDORDERFILTER; |
110 | volatile uint8_t GYROS_DFILTER; |
110 | volatile uint8_t GYROS_DFILTER; |
111 | volatile uint8_t ACC_FILTER; |
111 | volatile uint8_t ACC_FILTER; |
112 | 112 | ||
113 | // Air pressure (no support right now). |
113 | // Air pressure (no support right now). |
114 | // volatile int32_t AirPressure = 32000; |
114 | // volatile int32_t AirPressure = 32000; |
115 | // volatile uint8_t average_pressure = 0; |
115 | // volatile uint8_t average_pressure = 0; |
116 | // volatile int16_t StartAirPressure; |
116 | // volatile int16_t StartAirPressure; |
117 | // volatile uint16_t ReadingAirPressure = 1023; |
117 | // volatile uint16_t ReadingAirPressure = 1023; |
118 | // volatile int16_t HeightD = 0; |
118 | // volatile int16_t HeightD = 0; |
119 | 119 | ||
120 | /* |
120 | /* |
121 | * Battery voltage, in units of: 1k/11k / 3V * 1024 = 31.03 per volt. |
121 | * Battery voltage, in units of: 1k/11k / 3V * 1024 = 31.03 per volt. |
122 | * That is divided by 3 below, for a final 10.34 per volt. |
122 | * That is divided by 3 below, for a final 10.34 per volt. |
123 | * So the initial value of 100 is for 9.7 volts. |
123 | * So the initial value of 100 is for 9.7 volts. |
124 | */ |
124 | */ |
125 | volatile int16_t UBat = 100; |
125 | volatile int16_t UBat = 100; |
126 | 126 | ||
127 | volatile int16_t filteredAirPressure; |
127 | volatile int16_t filteredAirPressure; |
128 | 128 | ||
129 | /* |
129 | /* |
130 | * Control and status. |
130 | * Control and status. |
131 | */ |
131 | */ |
132 | volatile uint16_t ADCycleCount = 0; |
132 | volatile uint16_t ADCycleCount = 0; |
133 | volatile uint8_t analogDataReady = 1; |
133 | volatile uint8_t analogDataReady = 1; |
134 | 134 | ||
135 | /* |
135 | /* |
136 | * Experiment: Measuring vibration-induced sensor noise. |
136 | * Experiment: Measuring vibration-induced sensor noise. |
137 | */ |
137 | */ |
138 | volatile uint16_t pitchGyroNoisePeak, rollGyroNoisePeak; |
138 | volatile uint16_t pitchGyroNoisePeak, rollGyroNoisePeak; |
139 | volatile uint16_t pitchAccNoisePeak, rollAccNoisePeak; |
139 | volatile uint16_t pitchAccNoisePeak, rollAccNoisePeak; |
140 | 140 | ||
141 | // ADC channels |
141 | // ADC channels |
142 | #define AD_GYRO_YAW 0 |
142 | #define AD_GYRO_YAW 0 |
143 | #define AD_GYRO_ROLL 1 |
143 | #define AD_GYRO_ROLL 1 |
144 | #define AD_GYRO_PITCH 2 |
144 | #define AD_GYRO_PITCH 2 |
145 | #define AD_AIRPRESSURE 3 |
145 | #define AD_AIRPRESSURE 3 |
146 | #define AD_UBAT 4 |
146 | #define AD_UBAT 4 |
147 | #define AD_ACC_Z 5 |
147 | #define AD_ACC_Z 5 |
148 | #define AD_ACC_ROLL 6 |
148 | #define AD_ACC_ROLL 6 |
149 | #define AD_ACC_PITCH 7 |
149 | #define AD_ACC_PITCH 7 |
150 | 150 | ||
151 | /* |
151 | /* |
152 | * Table of AD converter inputs for each state. |
152 | * Table of AD converter inputs for each state. |
153 | * The number of samples summed for each channel is equal to |
153 | * The number of samples summed for each channel is equal to |
154 | * the number of times the channel appears in the array. |
154 | * the number of times the channel appears in the array. |
155 | * The max. number of samples that can be taken in 2 ms is: |
155 | * The max. number of samples that can be taken in 2 ms is: |
156 | * 20e6 / 128 / 13 / (1/2e-3) = 24. Since the main control |
156 | * 20e6 / 128 / 13 / (1/2e-3) = 24. Since the main control |
157 | * loop needs a little time between reading AD values and |
157 | * loop needs a little time between reading AD values and |
158 | * re-enabling ADC, the real limit is (how much?) lower. |
158 | * re-enabling ADC, the real limit is (how much?) lower. |
159 | * The acc. sensor is sampled even if not used - or installed |
159 | * The acc. sensor is sampled even if not used - or installed |
160 | * at all. The cost is not significant. |
160 | * at all. The cost is not significant. |
161 | */ |
161 | */ |
162 | 162 | ||
163 | const uint8_t channelsForStates[] PROGMEM = { |
163 | const uint8_t channelsForStates[] PROGMEM = { |
164 | AD_GYRO_PITCH, |
164 | AD_GYRO_PITCH, |
165 | AD_GYRO_ROLL, |
165 | AD_GYRO_ROLL, |
166 | AD_GYRO_YAW, |
166 | AD_GYRO_YAW, |
167 | 167 | ||
168 | AD_ACC_PITCH, |
168 | AD_ACC_PITCH, |
169 | AD_ACC_ROLL, |
169 | AD_ACC_ROLL, |
170 | // AD_AIRPRESSURE, |
170 | // AD_AIRPRESSURE, |
171 | 171 | ||
172 | AD_GYRO_PITCH, |
172 | AD_GYRO_PITCH, |
173 | AD_GYRO_ROLL, |
173 | AD_GYRO_ROLL, |
174 | AD_ACC_Z, // at 7, measure Z acc. |
174 | AD_ACC_Z, // at 7, measure Z acc. |
175 | 175 | ||
176 | AD_GYRO_PITCH, |
176 | AD_GYRO_PITCH, |
177 | AD_GYRO_ROLL, |
177 | AD_GYRO_ROLL, |
178 | AD_GYRO_YAW, // at 10, finish yaw gyro |
178 | AD_GYRO_YAW, // at 10, finish yaw gyro |
179 | 179 | ||
180 | AD_ACC_PITCH, // at 11, finish pitch axis acc. |
180 | AD_ACC_PITCH, // at 11, finish pitch axis acc. |
181 | AD_ACC_ROLL, // at 12, finish roll axis acc. |
181 | AD_ACC_ROLL, // at 12, finish roll axis acc. |
182 | AD_AIRPRESSURE, // at 13, finish air pressure. |
182 | AD_AIRPRESSURE, // at 13, finish air pressure. |
183 | 183 | ||
184 | AD_GYRO_PITCH, // at 14, finish pitch gyro |
184 | AD_GYRO_PITCH, // at 14, finish pitch gyro |
185 | AD_GYRO_ROLL, // at 15, finish roll gyro |
185 | AD_GYRO_ROLL, // at 15, finish roll gyro |
186 | AD_UBAT // at 16, measure battery. |
186 | AD_UBAT // at 16, measure battery. |
187 | }; |
187 | }; |
188 | 188 | ||
189 | // Feature removed. Could be reintroduced later - but should work for all gyro types then. |
189 | // Feature removed. Could be reintroduced later - but should work for all gyro types then. |
190 | // uint8_t GyroDefectPitch = 0, GyroDefectRoll = 0, GyroDefectYaw = 0; |
190 | // uint8_t GyroDefectPitch = 0, GyroDefectRoll = 0, GyroDefectYaw = 0; |
191 | 191 | ||
192 | void analog_init(void) { |
192 | void analog_init(void) { |
193 | uint8_t sreg = SREG; |
193 | uint8_t sreg = SREG; |
194 | // disable all interrupts before reconfiguration |
194 | // disable all interrupts before reconfiguration |
195 | cli(); |
195 | cli(); |
196 | 196 | ||
197 | //ADC0 ... ADC7 is connected to PortA pin 0 ... 7 |
197 | //ADC0 ... ADC7 is connected to PortA pin 0 ... 7 |
198 | DDRA = 0x00; |
198 | DDRA = 0x00; |
199 | PORTA = 0x00; |
199 | PORTA = 0x00; |
200 | // Digital Input Disable Register 0 |
200 | // Digital Input Disable Register 0 |
201 | // Disable digital input buffer for analog adc_channel pins |
201 | // Disable digital input buffer for analog adc_channel pins |
202 | DIDR0 = 0xFF; |
202 | DIDR0 = 0xFF; |
203 | // external reference, adjust data to the right |
203 | // external reference, adjust data to the right |
204 | ADMUX &= ~((1 << REFS1)|(1 << REFS0)|(1 << ADLAR)); |
204 | ADMUX &= ~((1 << REFS1)|(1 << REFS0)|(1 << ADLAR)); |
205 | // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice) |
205 | // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice) |
206 | ADMUX = (ADMUX & 0xE0) | AD_GYRO_PITCH; |
206 | ADMUX = (ADMUX & 0xE0) | AD_GYRO_PITCH; |
207 | //Set ADC Control and Status Register A |
207 | //Set ADC Control and Status Register A |
208 | //Auto Trigger Enable, Prescaler Select Bits to Division Factor 128, i.e. ADC clock = SYSCKL/128 = 156.25 kHz |
208 | //Auto Trigger Enable, Prescaler Select Bits to Division Factor 128, i.e. ADC clock = SYSCKL/128 = 156.25 kHz |
209 | ADCSRA = (0<<ADEN)|(0<<ADSC)|(0<<ADATE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)|(0<<ADIE); |
209 | ADCSRA = (0<<ADEN)|(0<<ADSC)|(0<<ADATE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)|(0<<ADIE); |
210 | //Set ADC Control and Status Register B |
210 | //Set ADC Control and Status Register B |
211 | //Trigger Source to Free Running Mode |
211 | //Trigger Source to Free Running Mode |
212 | ADCSRB &= ~((1 << ADTS2)|(1 << ADTS1)|(1 << ADTS0)); |
212 | ADCSRB &= ~((1 << ADTS2)|(1 << ADTS1)|(1 << ADTS0)); |
213 | // Start AD conversion |
213 | // Start AD conversion |
214 | analog_start(); |
214 | analog_start(); |
215 | // restore global interrupt flags |
215 | // restore global interrupt flags |
216 | SREG = sreg; |
216 | SREG = sreg; |
217 | } |
217 | } |
218 | 218 | ||
219 | void measureNoise(const int16_t sensor, volatile uint16_t* const noiseMeasurement, const uint8_t damping) { |
219 | void measureNoise(const int16_t sensor, volatile uint16_t* const noiseMeasurement, const uint8_t damping) { |
220 | if (sensor > (int16_t)(*noiseMeasurement)) { |
220 | if (sensor > (int16_t)(*noiseMeasurement)) { |
221 | *noiseMeasurement = sensor; |
221 | *noiseMeasurement = sensor; |
222 | } else if (-sensor > (int16_t)(*noiseMeasurement)) { |
222 | } else if (-sensor > (int16_t)(*noiseMeasurement)) { |
223 | *noiseMeasurement = -sensor; |
223 | *noiseMeasurement = -sensor; |
224 | } else if (*noiseMeasurement > damping) { |
224 | } else if (*noiseMeasurement > damping) { |
225 | *noiseMeasurement -= damping; |
225 | *noiseMeasurement -= damping; |
226 | } else { |
226 | } else { |
227 | *noiseMeasurement = 0; |
227 | *noiseMeasurement = 0; |
228 | } |
228 | } |
229 | } |
229 | } |
230 | 230 | ||
231 | 231 | ||
232 | #define ADCENTER (1023/2) |
232 | #define ADCENTER (1023/2) |
233 | #define HALFRANGE 400 |
233 | #define HALFRANGE 400 |
234 | uint8_t stepsize = 53; |
234 | uint8_t stepsize = 53; |
235 | 235 | ||
236 | uint16_t getAbsPressure(int advalue) { |
236 | uint16_t getAbsPressure(int advalue) { |
237 | return (uint16_t)OCR0A * (uint16_t)stepsize + advalue; |
237 | return (uint16_t)OCR0A * (uint16_t)stepsize + advalue; |
238 | } |
238 | } |
239 | 239 | ||
240 | uint16_t filterAirPressure(uint16_t rawpressure) { |
240 | uint16_t filterAirPressure(uint16_t rawpressure) { |
241 | return rawpressure; |
241 | return rawpressure; |
242 | } |
242 | } |
243 | 243 | ||
244 | /*****************************************************/ |
244 | /*****************************************************/ |
245 | /* Interrupt Service Routine for ADC */ |
245 | /* Interrupt Service Routine for ADC */ |
246 | /*****************************************************/ |
246 | /*****************************************************/ |
247 | // Runs at 312.5 kHz or 3.2 µs |
247 | // Runs at 312.5 kHz or 3.2 µs |
248 | // When all states are processed the interrupt is disabled |
248 | // When all states are processed the interrupt is disabled |
249 | // and the update of further AD conversions is stopped. |
249 | // and the update of further AD conversions is stopped. |
250 | 250 | ||
251 | ISR(ADC_vect) { |
251 | ISR(ADC_vect) { |
252 | static uint8_t ad_channel = AD_GYRO_PITCH, state = 0; |
252 | static uint8_t ad_channel = AD_GYRO_PITCH, state = 0; |
253 | static uint16_t sensorInputs[8] = {0,0,0,0,0,0,0,0}; |
253 | static uint16_t sensorInputs[8] = {0,0,0,0,0,0,0,0}; |
254 | 254 | ||
255 | uint8_t i; |
255 | uint8_t i; |
256 | int16_t step = OCR0A; |
256 | int16_t step = OCR0A; |
257 | 257 | ||
258 | // for various filters... |
258 | // for various filters... |
259 | static int16_t pitchGyroFilter, rollGyroFilter, tempOffsetGyro; |
259 | static int16_t pitchGyroFilter, rollGyroFilter, tempOffsetGyro; |
260 | 260 | ||
261 | sensorInputs[ad_channel] += ADC; |
261 | sensorInputs[ad_channel] += ADC; |
262 | 262 | ||
263 | /* |
263 | /* |
264 | * Actually we don't need this "switch". We could do all the sampling into the |
264 | * Actually we don't need this "switch". We could do all the sampling into the |
265 | * sensorInputs array first, and all the processing after the last sample. |
265 | * sensorInputs array first, and all the processing after the last sample. |
266 | */ |
266 | */ |
267 | switch(state++) { |
267 | switch(state++) { |
268 | case 7: // Z acc |
268 | case 7: // Z acc |
269 | #ifdef ACC_REVERSE_ZAXIS |
269 | #ifdef ACC_REVERSE_ZAXIS |
270 | ZAxisAcc = -ZAxisAccOffset - sensorInputs[AD_ACC_Z]; |
270 | ZAxisAcc = -ZAxisAccOffset - sensorInputs[AD_ACC_Z]; |
271 | #else |
271 | #else |
272 | ZAxisAcc = sensorInputs[AD_ACC_Z] - ZAxisAccOffset; |
272 | ZAxisAcc = sensorInputs[AD_ACC_Z] - ZAxisAccOffset; |
273 | #endif |
273 | #endif |
274 | break; |
274 | break; |
275 | 275 | ||
276 | case 10: // yaw gyro |
276 | case 10: // yaw gyro |
277 | rawYawGyroSum = sensorInputs[AD_GYRO_YAW]; |
277 | rawYawGyroSum = sensorInputs[AD_GYRO_YAW]; |
278 | #ifdef GYRO_REVERSE_YAW |
278 | #ifdef GYRO_REVERSE_YAW |
279 | yawGyro = rawYawGyroSum - yawOffset; |
279 | yawGyro = rawYawGyroSum - yawOffset; |
280 | #else |
280 | #else |
281 | yawGyro = yawOffset - rawYawGyroSum; // negative is "default" (FC 1.0-1.3). |
281 | yawGyro = yawOffset - rawYawGyroSum; // negative is "default" (FC 1.0-1.3). |
282 | #endif |
282 | #endif |
283 | break; |
283 | break; |
284 | 284 | ||
285 | case 11: // pitch axis acc. |
285 | case 11: // pitch axis acc. |
286 | #ifdef ACC_REVERSE_PITCHAXIS |
286 | #ifdef ACC_REVERSE_PITCHAXIS |
287 | pitchAxisAcc = -pitchAxisAccOffset - sensorInputs[AD_ACC_PITCH]; |
287 | pitchAxisAcc = -pitchAxisAccOffset - sensorInputs[AD_ACC_PITCH]; |
288 | #else |
288 | #else |
289 | pitchAxisAcc = sensorInputs[AD_ACC_PITCH] - pitchAxisAccOffset; |
289 | pitchAxisAcc = sensorInputs[AD_ACC_PITCH] - pitchAxisAccOffset; |
290 | #endif |
290 | #endif |
291 | filteredPitchAxisAcc = (filteredPitchAxisAcc * (ACC_FILTER-1) + pitchAxisAcc) / ACC_FILTER; |
291 | filteredPitchAxisAcc = (filteredPitchAxisAcc * (ACC_FILTER-1) + pitchAxisAcc) / ACC_FILTER; |
292 | 292 | ||
293 | measureNoise(pitchAxisAcc, &pitchAccNoisePeak, 1); |
293 | measureNoise(pitchAxisAcc, &pitchAccNoisePeak, 1); |
294 | break; |
294 | break; |
295 | 295 | ||
296 | case 12: // roll axis acc. |
296 | case 12: // roll axis acc. |
297 | #ifdef ACC_REVERSE_ROLLAXIS |
297 | #ifdef ACC_REVERSE_ROLLAXIS |
298 | rollAxisAcc = sensorInputs[AD_ACC_ROLL] - rollAxisAccOffset; |
298 | rollAxisAcc = sensorInputs[AD_ACC_ROLL] - rollAxisAccOffset; |
299 | #else |
299 | #else |
300 | rollAxisAcc = -rollAxisAccOffset - sensorInputs[AD_ACC_ROLL]; |
300 | rollAxisAcc = -rollAxisAccOffset - sensorInputs[AD_ACC_ROLL]; |
301 | #endif |
301 | #endif |
302 | filteredRollAxisAcc = (filteredRollAxisAcc * (ACC_FILTER-1) + rollAxisAcc) / ACC_FILTER; |
302 | filteredRollAxisAcc = (filteredRollAxisAcc * (ACC_FILTER-1) + rollAxisAcc) / ACC_FILTER; |
303 | measureNoise(rollAxisAcc, &rollAccNoisePeak, 1); |
303 | measureNoise(rollAxisAcc, &rollAccNoisePeak, 1); |
304 | break; |
304 | break; |
305 | 305 | ||
306 | case 13: // air pressure |
306 | case 13: // air pressure |
307 | if (sensorInputs[AD_AIRPRESSURE] < ADCENTER-HALFRANGE) { |
307 | if (sensorInputs[AD_AIRPRESSURE] < ADCENTER-HALFRANGE) { |
308 | // value is too low, so decrease voltage on the op amp minus input, making the value higher. |
308 | // value is too low, so decrease voltage on the op amp minus input, making the value higher. |
309 | step -= ((HALFRANGE-sensorInputs[AD_AIRPRESSURE]) / stepsize + 1); |
309 | step -= ((HALFRANGE-sensorInputs[AD_AIRPRESSURE]) / stepsize + 1); |
310 | if (step<0) step = 0; |
310 | if (step<0) step = 0; |
311 | OCR0A = step; |
311 | OCR0A = step; |
312 | // wait = ... (calculate something here .. calculate at what time the R/C filter is to within one sample off) |
312 | // wait = ... (calculate something here .. calculate at what time the R/C filter is to within one sample off) |
313 | } else if (sensorInputs[AD_AIRPRESSURE] > ADCENTER+HALFRANGE) { |
313 | } else if (sensorInputs[AD_AIRPRESSURE] > ADCENTER+HALFRANGE) { |
314 | // value is too high, so increase voltage on the op amp minus input, making the value lower. |
314 | // value is too high, so increase voltage on the op amp minus input, making the value lower. |
315 | step += ((sensorInputs[AD_AIRPRESSURE] - HALFRANGE)/stepsize + 1); |
315 | step += ((sensorInputs[AD_AIRPRESSURE] - HALFRANGE)/stepsize + 1); |
316 | if (step>254) step = 254; |
316 | if (step>254) step = 254; |
317 | OCR0A = step; |
317 | OCR0A = step; |
318 | // wait = ... (calculate something here .. calculate at what time the R/C filter is to within one sample off) |
318 | // wait = ... (calculate something here .. calculate at what time the R/C filter is to within one sample off) |
319 | } else { |
319 | } else { |
320 | filteredAirPressure = filterAirPressure(getAbsPressure(sensorInputs[AD_AIRPRESSURE])); |
320 | filteredAirPressure = filterAirPressure(getAbsPressure(sensorInputs[AD_AIRPRESSURE])); |
321 | } |
321 | } |
322 | break; |
322 | break; |
323 | 323 | ||
324 | case 14: // pitch gyro |
324 | case 14: // pitch gyro |
325 | rawPitchGyroSum = sensorInputs[AD_GYRO_PITCH]; |
325 | rawPitchGyroSum = sensorInputs[AD_GYRO_PITCH]; |
326 | // Filter already before offsetting. The offsetting resolution improvement obtained by divding by |
326 | // Filter already before offsetting. The offsetting resolution improvement obtained by divding by |
327 | // GYROS_FIRSTORDERFILTER _after_ offsetting is too small to be worth pursuing. |
327 | // GYROS_FIRSTORDERFILTER _after_ offsetting is too small to be worth pursuing. |
328 | pitchGyroFilter = (pitchGyroFilter * (GYROS_FIRSTORDERFILTER-1) + rawPitchGyroSum * GYRO_FACTOR_PITCHROLL) / GYROS_FIRSTORDERFILTER; |
328 | pitchGyroFilter = (pitchGyroFilter * (GYROS_FIRSTORDERFILTER-1) + rawPitchGyroSum * GYRO_FACTOR_PITCHROLL) / GYROS_FIRSTORDERFILTER; |
329 | // Offset to 0. |
329 | // Offset to 0. |
330 | #ifdef GYROS_REVERSE_PITCH |
330 | #ifdef GYROS_REVERSE_PITCH |
331 | tempOffsetGyro = pitchOffset - pitchGyroFilter; |
331 | tempOffsetGyro = pitchOffset - pitchGyroFilter; |
332 | #else |
332 | #else |
333 | tempOffsetGyro = pitchGyroFilter - pitchOffset; |
333 | tempOffsetGyro = pitchGyroFilter - pitchOffset; |
334 | #endif |
334 | #endif |
335 | // Calculate the delta from last shot and filter it. |
335 | // Calculate the delta from last shot and filter it. |
336 | pitchGyroD = (pitchGyroD * (GYROS_DFILTER-1) + (tempOffsetGyro - hiResPitchGyro)) / GYROS_DFILTER; |
336 | pitchGyroD = (pitchGyroD * (GYROS_DFILTER-1) + (tempOffsetGyro - hiResPitchGyro)) / GYROS_DFILTER; |
337 | // How we can overwrite the last value. This value is used for the D part of the PID controller. |
337 | // How we can overwrite the last value. This value is used for the D part of the PID controller. |
338 | hiResPitchGyro = tempOffsetGyro; |
338 | hiResPitchGyro = tempOffsetGyro; |
339 | // Filter a little more. This value is used in integration to angles. |
339 | // Filter a little more. This value is used in integration to angles. |
340 | filteredHiResPitchGyro = (filteredHiResPitchGyro * (GYROS_SECONDORDERFILTER-1) + hiResPitchGyro) / GYROS_SECONDORDERFILTER; |
340 | filteredHiResPitchGyro = (filteredHiResPitchGyro * (GYROS_SECONDORDERFILTER-1) + hiResPitchGyro) / GYROS_SECONDORDERFILTER; |
341 | measureNoise(hiResPitchGyro, &pitchGyroNoisePeak, GYRO_NOISE_MEASUREMENT_DAMPING); |
341 | measureNoise(hiResPitchGyro, &pitchGyroNoisePeak, GYRO_NOISE_MEASUREMENT_DAMPING); |
342 | break; |
342 | break; |
343 | 343 | ||
344 | case 15: // Roll gyro. Works the same as pitch. |
344 | case 15: // Roll gyro. Works the same as pitch. |
345 | rawRollGyroSum = sensorInputs[AD_GYRO_ROLL]; |
345 | rawRollGyroSum = sensorInputs[AD_GYRO_ROLL]; |
346 | rollGyroFilter = (rollGyroFilter * (GYROS_FIRSTORDERFILTER-1) + rawRollGyroSum * GYRO_FACTOR_PITCHROLL) / GYROS_FIRSTORDERFILTER; |
346 | rollGyroFilter = (rollGyroFilter * (GYROS_FIRSTORDERFILTER-1) + rawRollGyroSum * GYRO_FACTOR_PITCHROLL) / GYROS_FIRSTORDERFILTER; |
347 | #ifdef GYRO_REVERSE_ROLL |
347 | #ifdef GYRO_REVERSE_ROLL |
348 | tempOffsetGyro = rollOffset - rollGyroFilter; |
348 | tempOffsetGyro = rollOffset - rollGyroFilter; |
349 | #else |
349 | #else |
350 | tempOffsetGyro = rollGyroFilter - rollOffset; |
350 | tempOffsetGyro = rollGyroFilter - rollOffset; |
351 | #endif |
351 | #endif |
352 | rollGyroD = (rollGyroD * (GYROS_DFILTER-1) + (tempOffsetGyro - hiResRollGyro)) / GYROS_DFILTER; |
352 | rollGyroD = (rollGyroD * (GYROS_DFILTER-1) + (tempOffsetGyro - hiResRollGyro)) / GYROS_DFILTER; |
353 | hiResRollGyro = tempOffsetGyro; |
353 | hiResRollGyro = tempOffsetGyro; |
354 | filteredHiResRollGyro = (filteredHiResRollGyro * (GYROS_SECONDORDERFILTER-1) + hiResRollGyro) / GYROS_SECONDORDERFILTER; |
354 | filteredHiResRollGyro = (filteredHiResRollGyro * (GYROS_SECONDORDERFILTER-1) + hiResRollGyro) / GYROS_SECONDORDERFILTER; |
355 | measureNoise(hiResRollGyro, &rollGyroNoisePeak, GYRO_NOISE_MEASUREMENT_DAMPING); |
355 | measureNoise(hiResRollGyro, &rollGyroNoisePeak, GYRO_NOISE_MEASUREMENT_DAMPING); |
356 | break; |
356 | break; |
357 | 357 | ||
358 | case 16: |
358 | case 16: |
359 | // battery |
359 | // battery |
360 | UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4; |
360 | UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4; |
361 | analogDataReady = 1; // mark |
361 | analogDataReady = 1; // mark |
362 | ADCycleCount++; |
362 | ADCycleCount++; |
363 | // Stop the sampling. Cycle is over. |
363 | // Stop the sampling. Cycle is over. |
364 | state = 0; |
364 | state = 0; |
365 | for (i=0; i<8; i++) { |
365 | for (i=0; i<8; i++) { |
366 | sensorInputs[i] = 0; |
366 | sensorInputs[i] = 0; |
367 | } |
367 | } |
368 | break; |
368 | break; |
369 | default: {} // do nothing. |
369 | default: {} // do nothing. |
370 | } |
370 | } |
371 | 371 | ||
372 | // set up for next state. |
372 | // set up for next state. |
373 | ad_channel = pgm_read_byte(&channelsForStates[state]); |
373 | ad_channel = pgm_read_byte(&channelsForStates[state]); |
374 | // ad_channel = channelsForStates[state]; |
374 | // ad_channel = channelsForStates[state]; |
375 | 375 | ||
376 | // set adc muxer to next ad_channel |
376 | // set adc muxer to next ad_channel |
377 | ADMUX = (ADMUX & 0xE0) | ad_channel; |
377 | ADMUX = (ADMUX & 0xE0) | ad_channel; |
378 | // after full cycle stop further interrupts |
378 | // after full cycle stop further interrupts |
379 | if(state) analog_start(); |
379 | if(state) analog_start(); |
380 | } |
380 | } |
381 | 381 | ||
382 | void analog_calibrate(void) { |
382 | void analog_calibrate(void) { |
383 | #define GYRO_OFFSET_CYCLES 32 |
383 | #define GYRO_OFFSET_CYCLES 32 |
384 | uint8_t i; |
384 | uint8_t i; |
385 | int32_t _pitchOffset = 0, _rollOffset = 0, _yawOffset = 0; |
385 | int32_t _pitchOffset = 0, _rollOffset = 0, _yawOffset = 0; |
386 | 386 | ||
387 | // Set the filters... to be removed again, once some good settings are found. |
387 | // Set the filters... to be removed again, once some good settings are found. |
388 | GYROS_FIRSTORDERFILTER = (dynamicParams.UserParams[4] & 0b00000011) + 1; |
388 | GYROS_FIRSTORDERFILTER = (dynamicParams.UserParams[4] & 0b00000011) + 1; |
389 | GYROS_SECONDORDERFILTER = ((dynamicParams.UserParams[4] & 0b00001100) >> 2) + 1; |
389 | GYROS_SECONDORDERFILTER = ((dynamicParams.UserParams[4] & 0b00001100) >> 2) + 1; |
390 | GYROS_DFILTER = ((dynamicParams.UserParams[4] & 0b00110000) >> 4) + 1; |
390 | GYROS_DFILTER = ((dynamicParams.UserParams[4] & 0b00110000) >> 4) + 1; |
391 | ACC_FILTER = ((dynamicParams.UserParams[4] & 0b11000000) >> 6) + 1; |
391 | ACC_FILTER = ((dynamicParams.UserParams[4] & 0b11000000) >> 6) + 1; |
392 | 392 | ||
393 | pitchOffset = rollOffset = yawOffset = 0; |
393 | pitchOffset = rollOffset = yawOffset = 0; |
394 | 394 | ||
395 | gyro_calibrate(); |
395 | gyro_calibrate(); |
396 | 396 | ||
397 | // determine gyro bias by averaging (requires that the copter does not rotate around any axis!) |
397 | // determine gyro bias by averaging (requires that the copter does not rotate around any axis!) |
398 | for(i=0; i < GYRO_OFFSET_CYCLES; i++) { |
398 | for(i=0; i < GYRO_OFFSET_CYCLES; i++) { |
399 | Delay_ms_Mess(10); |
399 | Delay_ms_Mess(10); |
400 | _pitchOffset += rawPitchGyroSum * GYRO_FACTOR_PITCHROLL; |
400 | _pitchOffset += rawPitchGyroSum * GYRO_FACTOR_PITCHROLL; |
401 | _rollOffset += rawRollGyroSum * GYRO_FACTOR_PITCHROLL; |
401 | _rollOffset += rawRollGyroSum * GYRO_FACTOR_PITCHROLL; |
402 | _yawOffset += rawYawGyroSum; |
402 | _yawOffset += rawYawGyroSum; |
403 | } |
403 | } |
404 | 404 | ||
405 | pitchOffset = (_pitchOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
405 | pitchOffset = (_pitchOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
406 | rollOffset = (_rollOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
406 | rollOffset = (_rollOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
407 | yawOffset = (_yawOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
407 | yawOffset = (_yawOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
408 | 408 | ||
409 | filteredHiResPitchGyro = filteredHiResRollGyro = 0; |
409 | filteredHiResPitchGyro = filteredHiResRollGyro = 0; |
410 | 410 | ||
411 | pitchAxisAccOffset = (int16_t)GetParamWord(PID_ACC_NICK); |
411 | pitchAxisAccOffset = (int16_t)GetParamWord(PID_ACC_NICK); |
412 | rollAxisAccOffset = (int16_t)GetParamWord(PID_ACC_ROLL); |
412 | rollAxisAccOffset = (int16_t)GetParamWord(PID_ACC_ROLL); |
413 | ZAxisAccOffset = (int16_t)GetParamWord(PID_ACC_TOP); |
413 | ZAxisAccOffset = (int16_t)GetParamWord(PID_ACC_TOP); |
414 | 414 | ||
415 | // Noise is relative to offset. So, reset noise measurements when |
415 | // Noise is relative to offset. So, reset noise measurements when |
416 | // changing offsets. |
416 | // changing offsets. |
417 | pitchGyroNoisePeak = rollGyroNoisePeak = 0; |
417 | pitchGyroNoisePeak = rollGyroNoisePeak = 0; |
418 | 418 | ||
419 | // Setting offset values has an influence in the analog.c ISR |
419 | // Setting offset values has an influence in the analog.c ISR |
420 | // Therefore run measurement for 100ms to achive stable readings |
420 | // Therefore run measurement for 100ms to achive stable readings |
421 | Delay_ms_Mess(100); |
421 | Delay_ms_Mess(100); |
422 | } |
422 | } |
423 | 423 | ||
424 | /* |
424 | /* |
425 | * Find acc. offsets for a neutral reading, and write them to EEPROM. |
425 | * Find acc. offsets for a neutral reading, and write them to EEPROM. |
426 | * Does not (!} update the local variables. This must be done with a |
426 | * Does not (!} update the local variables. This must be done with a |
427 | * call to analog_calibrate() - this always (?) is done by the caller |
427 | * call to analog_calibrate() - this always (?) is done by the caller |
428 | * anyway. There would be nothing wrong with updating the variables |
428 | * anyway. There would be nothing wrong with updating the variables |
429 | * directly from here, though. |
429 | * directly from here, though. |
430 | */ |
430 | */ |
431 | void analog_calibrateAcc(void) { |
431 | void analog_calibrateAcc(void) { |
432 | #define ACC_OFFSET_CYCLES 10 |
432 | #define ACC_OFFSET_CYCLES 10 |
433 | uint8_t i; |
433 | uint8_t i; |
434 | int32_t _pitchAxisOffset = 0, _rollAxisOffset = 0, _ZAxisOffset = 0; |
434 | int32_t _pitchAxisOffset = 0, _rollAxisOffset = 0, _ZAxisOffset = 0; |
435 | 435 | ||
436 | pitchAxisAccOffset = rollAxisAccOffset = ZAxisAccOffset = 0; |
436 | pitchAxisAccOffset = rollAxisAccOffset = ZAxisAccOffset = 0; |
437 | 437 | ||
438 | for(i=0; i < ACC_OFFSET_CYCLES; i++) { |
438 | for(i=0; i < ACC_OFFSET_CYCLES; i++) { |
439 | Delay_ms_Mess(10); |
439 | Delay_ms_Mess(10); |
440 | _pitchAxisOffset += pitchAxisAcc; |
440 | _pitchAxisOffset += pitchAxisAcc; |
441 | _rollAxisOffset += rollAxisAcc; |
441 | _rollAxisOffset += rollAxisAcc; |
442 | _ZAxisOffset += ZAxisAcc; |
442 | _ZAxisOffset += ZAxisAcc; |
443 | } |
443 | } |
444 | 444 | ||
445 | // Save ACC neutral settings to eeprom |
445 | // Save ACC neutral settings to eeprom |
446 | SetParamWord(PID_ACC_NICK, (uint16_t)((_pitchAxisOffset + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES)); |
446 | SetParamWord(PID_ACC_NICK, (uint16_t)((_pitchAxisOffset + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES)); |
447 | SetParamWord(PID_ACC_ROLL, (uint16_t)((_rollAxisOffset + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES)); |
447 | SetParamWord(PID_ACC_ROLL, (uint16_t)((_rollAxisOffset + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES)); |
448 | SetParamWord(PID_ACC_TOP, (uint16_t)((_ZAxisOffset + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES)); |
448 | SetParamWord(PID_ACC_TOP, (uint16_t)((_ZAxisOffset + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES)); |
449 | 449 | ||
450 | // Noise is relative to offset. So, reset noise measurements when |
450 | // Noise is relative to offset. So, reset noise measurements when |
451 | // changing offsets. |
451 | // changing offsets. |
452 | pitchAccNoisePeak = rollAccNoisePeak = 0; |
452 | pitchAccNoisePeak = rollAccNoisePeak = 0; |
453 | } |
453 | } |
454 | 454 |