Rev 35 | Rev 37 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
5 | hbuss | 1 | /*####################################################################################### |
7 | hbuss | 2 | MK3Mag 3D-Magnet sensor |
32 | holgerb | 3 | !!! THIS IS NOT FREE SOFTWARE !!! |
5 | hbuss | 4 | #######################################################################################*/ |
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
7 | hbuss | 6 | // + Copyright (c) 05.2008 Holger Buss |
8 | hbuss | 7 | // + Thanks to Ilja Fähnrich (P_Latzhalter) |
5 | hbuss | 8 | // + Nur für den privaten Gebrauch |
9 | // + www.MikroKopter.com |
||
10 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
32 | holgerb | 11 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
12 | hbuss | 12 | // + mit unserer Zustimmung zulässig |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
32 | holgerb | 14 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
15 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
||
12 | hbuss | 16 | // + AUSNAHME: Ein bei www.mikrokopter.de erworbener vorbestückter MK3Mag darf als Baugruppe auch in kommerziellen Systemen verbaut werden |
17 | // + Im Zweifelsfall bitte anfragen bei: info@mikrokopter.de |
||
5 | hbuss | 18 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | holgerb | 19 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
5 | hbuss | 20 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
21 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
22 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
||
23 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
||
24 | // + eindeutig als Ursprung verlinkt werden |
||
25 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
26 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
||
27 | // + Benutzung auf eigene Gefahr |
||
28 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
||
29 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
30 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
||
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
32 | holgerb | 32 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
5 | hbuss | 33 | // + this list of conditions and the following disclaimer. |
12 | hbuss | 34 | // + * PORTING this software (or parts of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed |
5 | hbuss | 35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
36 | // + from this software without specific prior written permission. |
||
32 | holgerb | 37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
5 | hbuss | 38 | // + for non-commercial use (directly or indirectly) |
32 | holgerb | 39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
5 | hbuss | 40 | // + with our written permission |
12 | hbuss | 41 | // + Exception: A preassembled MK3Mag, purchased from www.mikrokopter.de may be used as a part of commercial systems |
42 | // + In case of doubt please contact: info@MikroKopter.de |
||
32 | holgerb | 43 | // + * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be |
44 | // + clearly linked as origin |
||
5 | hbuss | 45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
12 | hbuss | 52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||
5 | hbuss | 54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
32 | holgerb | 55 | // + POSSIBILITY OF SUCH DAMAGE. |
5 | hbuss | 56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | holgerb | 57 | #include <avr/interrupt.h> |
58 | #include <math.h> |
||
59 | #include <stdlib.h> |
||
60 | #include <stdio.h> |
||
17 | holgerb | 61 | |
1 | ingob | 62 | #include "main.h" |
32 | holgerb | 63 | #include "timer0.h" |
64 | #include "twislave.h" |
||
65 | #include "led.h" |
||
66 | #include "analog.h" |
||
67 | #include "uart.h" |
||
1 | ingob | 68 | |
36 | hbuss | 69 | #define CALIBRATION_VERSION 1 |
32 | holgerb | 70 | |
71 | AttitudeSource_t AttitudeSource = ATTITUDE_SOURCE_ACC; |
||
72 | Orientation_t Orientation = ORIENTATION_FC; |
||
73 | |||
74 | uint16_t Led_Timer = 0; |
||
75 | |||
76 | typedef struct |
||
1 | ingob | 77 | { |
32 | holgerb | 78 | int16_t Range; |
79 | int16_t Offset; |
||
80 | } Scaling_t; |
||
1 | ingob | 81 | |
32 | holgerb | 82 | typedef struct |
83 | { |
||
84 | Scaling_t MagX; |
||
85 | Scaling_t MagY; |
||
86 | Scaling_t MagZ; |
||
87 | Scaling_t AccX; |
||
88 | Scaling_t AccY; |
||
89 | Scaling_t AccZ; |
||
36 | hbuss | 90 | unsigned char Version; |
32 | holgerb | 91 | } Calibration_t; |
92 | |||
93 | Calibration_t eeCalibration EEMEM; // calibration data in EEProm |
||
94 | Calibration_t Calibration; // calibration data in RAM |
||
95 | |||
96 | // magnet sensor variable |
||
97 | int16_t RawMagnet1a, RawMagnet1b; // raw magnet sensor data |
||
98 | int16_t RawMagnet2a, RawMagnet2b; |
||
99 | int16_t RawMagnet3a, RawMagnet3b; |
||
100 | int16_t UncalMagX, UncalMagY, UncalMagZ; // sensor signal difference without Scaling |
||
101 | int16_t MagX = 0, MagY = 0, MagZ = 0; // rescaled magnetic field readings |
||
102 | |||
103 | // acceleration sensor variables |
||
104 | int16_t RawAccX = 0, RawAccY = 0, RawAccZ = 0; // raw acceleration readings |
||
105 | int16_t AccX = 0, AccY = 0, AccZ = 0; // rescaled acceleration readings |
||
106 | int16_t AccAttitudeNick = 0, AccAttitudeRoll = 0; // nick and roll angle from acc |
||
107 | |||
108 | int16_t Heading = -1; // the current compass heading in deg |
||
109 | |||
110 | |||
1 | ingob | 111 | void CalcFields(void) |
7 | hbuss | 112 | { |
32 | holgerb | 113 | UncalMagX = (RawMagnet1a - RawMagnet1b); |
114 | UncalMagY = (RawMagnet3a - RawMagnet3b); |
||
115 | UncalMagZ = (RawMagnet2a - RawMagnet2b); |
||
7 | hbuss | 116 | |
32 | holgerb | 117 | if(Calibration.MagX.Range != 0) MagX = (1024L * (int32_t)(UncalMagX - Calibration.MagX.Offset)) / (Calibration.MagX.Range); |
118 | else MagX = 0; |
||
119 | if(Calibration.MagY.Range != 0) MagY = (1024L * (int32_t)(UncalMagY - Calibration.MagY.Offset)) / (Calibration.MagY.Range); |
||
120 | else MagY = 0; |
||
121 | if(Calibration.MagY.Range != 0) MagZ = (1024L * (int32_t)(UncalMagZ - Calibration.MagZ.Offset)) / (Calibration.MagZ.Range); |
||
122 | else MagZ = 0; |
||
7 | hbuss | 123 | |
32 | holgerb | 124 | if(AccPresent) |
125 | { |
||
126 | AccX = (RawAccX - Calibration.AccX.Offset); |
||
127 | AccY = (RawAccY - Calibration.AccY.Offset); |
||
128 | AccZ = (Calibration.AccZ.Offset - RawAccZ); |
||
129 | #if (BOARD == 10) // the hardware 1.0 has the LIS3L02AL |
||
130 | // acc mode assumes orientation like FC |
||
131 | if(AccX > 136) AccAttitudeNick = -800; |
||
132 | else |
||
133 | if(AccX < -136) AccAttitudeNick = 800; |
||
134 | else AccAttitudeNick = (int16_t)(-1800.0 * asin((double) AccX / 138.0) / M_PI); |
||
135 | |||
136 | |||
137 | if(AccY > 136) AccAttitudeRoll = 800; |
||
138 | else |
||
139 | if(AccY < -136) AccAttitudeRoll = -800; |
||
140 | else AccAttitudeRoll = (int16_t)( 1800.0 * asin((double) AccY / 138.0) / M_PI); |
||
141 | |||
142 | #else // the hardware 1.1 has the LIS344ALH with a different axis definition (X -> -Y, Y -> X, Z -> Z) |
||
143 | // acc mode assumes orientation like FC |
||
144 | if(AccY > 136) AccAttitudeNick = 800; |
||
145 | else |
||
146 | if(AccY < -136) AccAttitudeNick = -800; |
||
147 | else AccAttitudeNick = (int16_t)( 1800.0 * asin((double) AccY / 138.0) / M_PI); |
||
148 | |||
149 | |||
150 | if(AccX > 136) AccAttitudeRoll = 800; |
||
151 | else |
||
152 | if(AccX < -136) AccAttitudeRoll = -800; |
||
153 | else AccAttitudeRoll = (int16_t)( 1800.0 * asin((double) AccX / 138.0) / M_PI); |
||
154 | #endif |
||
155 | } |
||
1 | ingob | 156 | } |
157 | |||
32 | holgerb | 158 | |
1 | ingob | 159 | void CalcHeading(void) |
160 | { |
||
32 | holgerb | 161 | double nick_rad, roll_rad, Hx, Hy, Cx = 0.0, Cy = 0.0, Cz = 0.0; |
33 | killagreg | 162 | int16_t nick, roll; |
32 | holgerb | 163 | int16_t heading = -1; |
1 | ingob | 164 | |
32 | holgerb | 165 | // blink code for normal operation |
166 | if(CheckDelay(Led_Timer)) |
||
167 | { |
||
36 | hbuss | 168 | if(Calibration.Version != CALIBRATION_VERSION) LED_GRN_TOGGLE; |
169 | else LED_GRN_ON; |
||
170 | Led_Timer = SetDelay(150); |
||
32 | holgerb | 171 | } |
172 | switch(Orientation) |
||
173 | { |
||
174 | case ORIENTATION_NC: |
||
175 | Cx = MagX; |
||
176 | Cy = MagY; |
||
177 | Cz = MagZ; |
||
178 | break; |
||
7 | hbuss | 179 | |
32 | holgerb | 180 | case ORIENTATION_FC: |
181 | // rotation of 90 deg compared to NC setup |
||
182 | Cx = MagY; |
||
183 | Cy = -MagX; |
||
184 | Cz = MagZ; |
||
185 | break; |
||
186 | } |
||
187 | |||
188 | // calculate nick and roll angle in rad |
||
189 | switch(AttitudeSource) |
||
190 | { |
||
191 | case ATTITUDE_SOURCE_I2C: |
||
192 | cli(); // stop interrupts |
||
193 | nick = I2C_WriteAttitude.Nick; |
||
194 | roll = I2C_WriteAttitude.Roll; |
||
195 | sei(); // start interrupts |
||
196 | break; |
||
197 | case ATTITUDE_SOURCE_UART: |
||
198 | cli(); // stop interrupts |
||
199 | nick = ExternData.Attitude[NICK]; |
||
200 | roll = ExternData.Attitude[ROLL]; |
||
201 | sei(); // start interrupts |
||
202 | break; |
||
203 | case ATTITUDE_SOURCE_ACC: |
||
204 | nick = AccAttitudeNick; |
||
205 | roll = AccAttitudeRoll; |
||
206 | break; |
||
207 | default: |
||
35 | killagreg | 208 | nick = 0; |
209 | roll = 0; |
||
32 | holgerb | 210 | break; |
211 | } |
||
212 | |||
213 | nick_rad = ((double)nick) * M_PI / (double)(1800.0); |
||
214 | roll_rad = ((double)roll) * M_PI / (double)(1800.0); |
||
215 | |||
216 | // calculate attitude correction |
||
217 | Hx = Cx * cos(nick_rad) - Cz * sin(nick_rad); |
||
218 | Hy = Cy * cos(roll_rad) + Cz * sin(roll_rad); |
||
219 | |||
220 | DebugOut.Analog[27] = (int16_t)Hx; |
||
221 | DebugOut.Analog[28] = (int16_t)Hy; |
||
222 | |||
223 | // calculate Heading |
||
224 | heading = (int16_t)((180.0 * atan2(Hy, Hx)) / M_PI); |
||
225 | // atan2 returns angular range from -180 deg to 180 deg in counter clockwise notation |
||
226 | // but the compass course is defined in a range from 0 deg to 360 deg clockwise notation. |
||
227 | if (heading < 0) heading = -heading; |
||
228 | else heading = 360 - heading; |
||
33 | killagreg | 229 | |
32 | holgerb | 230 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
36 | hbuss | 231 | if(Calibration.Version != CALIBRATION_VERSION) heading = -1; // Version of the calibration Data does not match |
32 | holgerb | 232 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
33 | killagreg | 233 | cli(); // stop interrupts |
32 | holgerb | 234 | if(abs(heading) < 361) Heading = heading; |
235 | else (Heading = -1); |
||
33 | killagreg | 236 | sei(); // start interrupts |
237 | |||
1 | ingob | 238 | } |
239 | |||
7 | hbuss | 240 | void Calibrate(void) |
241 | { |
||
32 | holgerb | 242 | uint8_t cal; |
243 | static uint8_t calold = 0; |
||
244 | static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0; |
||
245 | static uint8_t blinkcount = 0; |
||
246 | static uint8_t invert_blinking = 0; |
||
33 | killagreg | 247 | |
32 | holgerb | 248 | // check both sources of communication for calibration request |
249 | if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte; |
||
250 | else cal = ExternData.CalState; |
||
16 | holgerb | 251 | |
32 | holgerb | 252 | |
253 | if(cal > 5) cal = 0; |
||
254 | // blink code for current calibration state |
||
255 | if(cal) |
||
256 | { |
||
257 | if(CheckDelay(Led_Timer) || (cal != calold)) |
||
258 | { |
||
259 | if(blinkcount & 0x01) if(invert_blinking) LED_GRN_ON; else LED_GRN_OFF; |
||
260 | else if(invert_blinking) LED_GRN_OFF; else LED_GRN_ON; |
||
261 | |||
262 | // end of blinkcount sequence |
||
263 | if((blinkcount + 1 ) >= (2 * cal)) |
||
264 | { |
||
265 | blinkcount = 0; |
||
35 | killagreg | 266 | Led_Timer = SetDelay(1500); |
32 | holgerb | 267 | } |
268 | else |
||
269 | { |
||
270 | blinkcount++; |
||
35 | killagreg | 271 | Led_Timer = SetDelay(100); |
32 | holgerb | 272 | } |
273 | } |
||
274 | } |
||
275 | else |
||
276 | { |
||
277 | if(invert_blinking) LED_GRN_ON; else LED_GRN_OFF; |
||
278 | } |
||
279 | // calibration state machine |
||
280 | switch(cal) |
||
281 | { |
||
282 | case 1: // 1st step of calibration |
||
283 | // initialize ranges |
||
284 | // used to change the orientation of the MK3MAG in the horizontal plane |
||
285 | Xmin = 10000; |
||
286 | Xmax = -10000; |
||
287 | Ymin = 10000; |
||
288 | Ymax = -10000; |
||
289 | Zmin = 10000; |
||
290 | Zmax = -10000; |
||
291 | Calibration.AccX.Offset = RawAccX; |
||
292 | Calibration.AccY.Offset = RawAccY; |
||
293 | Calibration.AccZ.Offset = RawAccZ; |
||
294 | invert_blinking = 0; |
||
295 | break; |
||
296 | |||
297 | case 2: // 2nd step of calibration |
||
298 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
||
299 | if(UncalMagX < Xmin) Xmin = UncalMagX; |
||
300 | if(UncalMagX > Xmax) Xmax = UncalMagX; |
||
301 | if(UncalMagY < Ymin) Ymin = UncalMagY; |
||
302 | if(UncalMagY > Ymax) Ymax = UncalMagY; |
||
303 | invert_blinking = 1; |
||
304 | break; |
||
305 | |||
306 | case 3: // 3rd step of calibration |
||
307 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
||
308 | invert_blinking = 0; |
||
309 | break; |
||
310 | |||
311 | case 4: |
||
312 | // find Min and Max of the Z-Sensor |
||
313 | if(UncalMagZ < Zmin) Zmin = UncalMagZ; |
||
314 | if(UncalMagZ > Zmax) Zmax = UncalMagZ; |
||
315 | invert_blinking = 1; |
||
316 | break; |
||
317 | |||
318 | case 5: |
||
319 | // Save values |
||
320 | if(cal != calold) // avoid continously writing of eeprom! |
||
321 | { |
||
322 | Calibration.MagX.Range = Xmax - Xmin; |
||
323 | Calibration.MagX.Offset = (Xmin + Xmax) / 2; |
||
324 | Calibration.MagY.Range = Ymax - Ymin; |
||
325 | Calibration.MagY.Offset = (Ymin + Ymax) / 2; |
||
326 | Calibration.MagZ.Range = Zmax - Zmin; |
||
327 | Calibration.MagZ.Offset = (Zmin + Zmax) / 2; |
||
328 | if((Calibration.MagX.Range > 150) && (Calibration.MagY.Range > 150) && (Calibration.MagZ.Range > 150)) |
||
329 | { |
||
36 | hbuss | 330 | Calibration.Version = CALIBRATION_VERSION; |
34 | killagreg | 331 | // indicate write process by setting the led off for 2 seconds |
332 | LED_GRN_OFF; |
||
32 | holgerb | 333 | eeprom_write_block(&Calibration, &eeCalibration, sizeof(Calibration)); |
334 | Led_Timer = SetDelay(2000); |
||
335 | // reset blinkcode |
||
336 | blinkcount = 0; |
||
337 | } |
||
338 | } |
||
339 | invert_blinking = 0; |
||
340 | break; |
||
341 | |||
342 | default: |
||
343 | break; |
||
344 | } |
||
345 | calold = cal; |
||
7 | hbuss | 346 | } |
1 | ingob | 347 | |
32 | holgerb | 348 | |
7 | hbuss | 349 | void SetDebugValues(void) |
350 | { |
||
32 | holgerb | 351 | DebugOut.Analog[0] = MagX; |
352 | DebugOut.Analog[1] = MagY; |
||
353 | DebugOut.Analog[2] = MagZ; |
||
354 | DebugOut.Analog[3] = UncalMagX; |
||
355 | DebugOut.Analog[4] = UncalMagY; |
||
356 | DebugOut.Analog[5] = UncalMagZ; |
||
357 | switch(AttitudeSource) |
||
358 | { |
||
359 | case ATTITUDE_SOURCE_ACC: |
||
360 | DebugOut.Analog[6] = AccAttitudeNick; |
||
361 | DebugOut.Analog[7] = AccAttitudeRoll; |
||
362 | break; |
||
363 | |||
364 | case ATTITUDE_SOURCE_UART: |
||
365 | DebugOut.Analog[6] = ExternData.Attitude[NICK]; |
||
366 | DebugOut.Analog[7] = ExternData.Attitude[ROLL]; |
||
367 | break; |
||
368 | |||
369 | |||
370 | case ATTITUDE_SOURCE_I2C: |
||
371 | DebugOut.Analog[6] = I2C_WriteAttitude.Nick; |
||
372 | DebugOut.Analog[7] = I2C_WriteAttitude.Roll; |
||
373 | break; |
||
374 | } |
||
375 | DebugOut.Analog[8] = Calibration.MagX.Offset; |
||
376 | DebugOut.Analog[9] = Calibration.MagX.Range; |
||
377 | DebugOut.Analog[10] = Calibration.MagY.Offset; |
||
378 | DebugOut.Analog[11] = Calibration.MagY.Range; |
||
379 | DebugOut.Analog[12] = Calibration.MagZ.Offset; |
||
380 | DebugOut.Analog[13] = Calibration.MagZ.Range; |
||
35 | killagreg | 381 | if(I2C_WriteCal.CalByte) DebugOut.Analog[14] = I2C_WriteCal.CalByte; |
382 | else DebugOut.Analog[14] = ExternData.CalState; |
||
32 | holgerb | 383 | DebugOut.Analog[15] = Heading; |
384 | DebugOut.Analog[16] = ExternData.UserParam[0]; |
||
385 | DebugOut.Analog[17] = ExternData.UserParam[1]; |
||
386 | DebugOut.Analog[18] = AccX; |
||
387 | DebugOut.Analog[19] = AccY; |
||
388 | DebugOut.Analog[20] = AccZ; |
||
389 | DebugOut.Analog[21] = RawAccX; |
||
390 | DebugOut.Analog[22] = RawAccY; |
||
391 | DebugOut.Analog[23] = RawAccZ; |
||
392 | DebugOut.Analog[24] = Calibration.AccX.Offset; |
||
393 | DebugOut.Analog[25] = Calibration.AccY.Offset; |
||
394 | DebugOut.Analog[26] = Calibration.AccZ.Offset; |
||
395 | DebugOut.Analog[29] = AttitudeSource; |
||
7 | hbuss | 396 | } |
397 | |||
16 | holgerb | 398 | void AccMeasurement(void) |
399 | { |
||
32 | holgerb | 400 | if(AccPresent) |
401 | { |
||
402 | RawAccX = (RawAccX + (int16_t)ADC_GetValue(ACC_X))/2; |
||
403 | RawAccY = (RawAccY + (int16_t)ADC_GetValue(ACC_Y))/2; |
||
404 | RawAccZ = (RawAccZ + (int16_t)ADC_GetValue(ACC_Z))/2; |
||
405 | } |
||
406 | else |
||
407 | { |
||
408 | RawAccX = 0; |
||
409 | RawAccY = 0; |
||
410 | RawAccZ = 0; |
||
411 | } |
||
16 | holgerb | 412 | } |
7 | hbuss | 413 | |
1 | ingob | 414 | int main (void) |
415 | { |
||
32 | holgerb | 416 | // reset input pullup |
417 | DDRC &=~((1<<DDC6)); |
||
418 | PORTC |= (1<<PORTC6); |
||
17 | holgerb | 419 | |
32 | holgerb | 420 | LED_Init(); |
421 | TIMER0_Init(); |
||
422 | USART0_Init(); |
||
1 | ingob | 423 | ADC_Init(); |
32 | holgerb | 424 | I2C_Init(); |
17 | holgerb | 425 | |
32 | holgerb | 426 | sei(); // enable globale interrupts |
427 | |||
428 | if(AccPresent) |
||
429 | { |
||
430 | USART0_Print("ACC present\n"); |
||
431 | } |
||
432 | |||
433 | LED_GRN_ON; |
||
434 | |||
435 | Debug_Timer = SetDelay(200); |
||
436 | Led_Timer = SetDelay(200); |
||
437 | |||
438 | // read calibration info from eeprom |
||
439 | eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration)); |
||
440 | |||
7 | hbuss | 441 | ExternData.CalState = 0; |
442 | I2C_WriteCal.CalByte = 0; |
||
32 | holgerb | 443 | |
444 | |||
445 | // main loop |
||
1 | ingob | 446 | while (1) |
32 | holgerb | 447 | { |
448 | FLIP_LOW; |
||
449 | Delay_ms(2); |
||
450 | RawMagnet1a = ADC_GetValue(MAG_X); |
||
451 | RawMagnet2a = -ADC_GetValue(MAG_Y); |
||
452 | RawMagnet3a = ADC_GetValue(MAG_Z); |
||
453 | AccMeasurement(); |
||
454 | Delay_ms(1); |
||
7 | hbuss | 455 | |
32 | holgerb | 456 | FLIP_HIGH; |
457 | Delay_ms(2); |
||
458 | RawMagnet1b = ADC_GetValue(MAG_X); |
||
459 | RawMagnet2b = -ADC_GetValue(MAG_Y); |
||
460 | RawMagnet3b = ADC_GetValue(MAG_Z); |
||
461 | AccMeasurement(); |
||
462 | Delay_ms(1); |
||
1 | ingob | 463 | |
32 | holgerb | 464 | CalcFields(); |
1 | ingob | 465 | |
32 | holgerb | 466 | if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate(); |
467 | else CalcHeading(); |
||
16 | holgerb | 468 | |
32 | holgerb | 469 | // check data from USART |
470 | USART0_ProcessRxData(); |
||
471 | |||
472 | if(NC_Connected) NC_Connected--; |
||
473 | if(FC_Connected) FC_Connected--; |
||
474 | // fall back to attitude estimation from acc sensor if NC or FC does'nt send attittude data |
||
475 | if(!FC_Connected && ! NC_Connected) |
||
476 | { |
||
477 | AttitudeSource = ATTITUDE_SOURCE_ACC; |
||
478 | Orientation = ORIENTATION_FC; |
||
479 | } |
||
480 | |||
481 | if(PC_Connected) |
||
482 | { |
||
483 | USART0_EnableTXD(); |
||
484 | USART0_TransmitTxData(); |
||
485 | PC_Connected--; |
||
486 | } |
||
487 | else |
||
488 | { |
||
489 | USART0_DisableTXD(); |
||
490 | } |
||
491 | } // while(1) |
||
1 | ingob | 492 | } |
493 |