Rev 72 | 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) |
62 | ingob | 8 | // + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY |
5 | hbuss | 9 | // + www.MikroKopter.com |
10 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
62 | ingob | 11 | // + Die Portierung oder Nutzung 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. |
62 | ingob | 34 | // + * porting the sources to other systems or using the software on other systems (except 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; |
||
71 | killagreg | 90 | uint8_t 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 |
||
37 | killagreg | 97 | int16_t RawMagnet1a, RawMagnet1b; // raw magnet sensor data |
32 | holgerb | 98 | int16_t RawMagnet2a, RawMagnet2b; |
99 | int16_t RawMagnet3a, RawMagnet3b; |
||
37 | killagreg | 100 | int16_t UncalMagX, UncalMagY, UncalMagZ; // sensor signal difference without Scaling |
32 | holgerb | 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 |
||
68 | holgerb | 109 | int16_t ZeroHeading = 180; |
71 | killagreg | 110 | uint8_t InternalCalstate = 0, ActualCalstate = 0; |
111 | int8_t PotiToFC[12] = {0,0,0,0,0,0,0,0,0,0,0,0}; |
||
32 | holgerb | 112 | |
1 | ingob | 113 | void CalcFields(void) |
7 | hbuss | 114 | { |
32 | holgerb | 115 | UncalMagX = (RawMagnet1a - RawMagnet1b); |
116 | UncalMagY = (RawMagnet3a - RawMagnet3b); |
||
117 | UncalMagZ = (RawMagnet2a - RawMagnet2b); |
||
118 | if(Calibration.MagX.Range != 0) MagX = (1024L * (int32_t)(UncalMagX - Calibration.MagX.Offset)) / (Calibration.MagX.Range); |
||
119 | else MagX = 0; |
||
120 | if(Calibration.MagY.Range != 0) MagY = (1024L * (int32_t)(UncalMagY - Calibration.MagY.Offset)) / (Calibration.MagY.Range); |
||
121 | else MagY = 0; |
||
122 | if(Calibration.MagY.Range != 0) MagZ = (1024L * (int32_t)(UncalMagZ - Calibration.MagZ.Offset)) / (Calibration.MagZ.Range); |
||
123 | else MagZ = 0; |
||
68 | holgerb | 124 | if(AttitudeSource == ATTITUDE_SOURCE_ACC) |
32 | holgerb | 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 | { |
||
55 | killagreg | 161 | double nick_rad, roll_rad, Cx = 0, Cy = 0, Cz = 0, Hx = 0, Hy = 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 | { |
||
37 | killagreg | 168 | if(Calibration.Version != CALIBRATION_VERSION) LED_GRN_TOGGLE; |
36 | hbuss | 169 | else LED_GRN_ON; |
170 | Led_Timer = SetDelay(150); |
||
32 | holgerb | 171 | } |
55 | killagreg | 172 | |
173 | if(Calibration.Version != CALIBRATION_VERSION) heading = -1; // Version of the calibration Data does not match |
||
174 | else |
||
32 | holgerb | 175 | { |
55 | killagreg | 176 | switch(Orientation) |
177 | { |
||
178 | case ORIENTATION_NC: |
||
179 | Cx = (double)MagX; |
||
180 | Cy = (double)MagY; |
||
181 | Cz = (double)MagZ; |
||
182 | break; |
||
7 | hbuss | 183 | |
55 | killagreg | 184 | case ORIENTATION_FC: |
185 | // rotation of 90 deg compared to NC setup |
||
186 | Cx = (double)MagY; |
||
187 | Cy = -(double)MagX; |
||
188 | Cz = (double)MagZ; |
||
189 | break; |
||
190 | } |
||
70 | holgerb | 191 | #ifdef HEADTRACKER |
192 | AttitudeSource = ATTITUDE_SOURCE_ACC; |
||
193 | #endif |
||
55 | killagreg | 194 | // calculate nick and roll angle in rad |
195 | switch(AttitudeSource) |
||
196 | { |
||
197 | case ATTITUDE_SOURCE_I2C: |
||
198 | cli(); // stop interrupts |
||
199 | nick = I2C_WriteAttitude.Nick; |
||
200 | roll = I2C_WriteAttitude.Roll; |
||
201 | sei(); // start interrupts |
||
202 | break; |
||
203 | case ATTITUDE_SOURCE_UART: |
||
204 | cli(); // stop interrupts |
||
205 | nick = ExternData.Attitude[NICK]; |
||
206 | roll = ExternData.Attitude[ROLL]; |
||
207 | sei(); // start interrupts |
||
208 | break; |
||
209 | case ATTITUDE_SOURCE_ACC: |
||
210 | nick = AccAttitudeNick; |
||
211 | roll = AccAttitudeRoll; |
||
212 | break; |
||
213 | default: |
||
214 | nick = 0; |
||
215 | roll = 0; |
||
32 | holgerb | 216 | break; |
55 | killagreg | 217 | } |
32 | holgerb | 218 | |
55 | killagreg | 219 | nick_rad = ((double)nick * M_PI) / 1800.0; |
220 | roll_rad = ((double)roll * M_PI) / 1800.0; |
||
32 | holgerb | 221 | |
55 | killagreg | 222 | // calculate attitude correction |
223 | Hx = Cx * cos(nick_rad) - Cz * sin(nick_rad); |
||
224 | Hy = Cy * cos(roll_rad) + Cz * sin(roll_rad); |
||
32 | holgerb | 225 | |
55 | killagreg | 226 | DebugOut.Analog[27] = (int16_t)Hx; |
227 | DebugOut.Analog[28] = (int16_t)Hy; |
||
32 | holgerb | 228 | |
55 | killagreg | 229 | // calculate Heading |
230 | heading = (int16_t)((180.0 * atan2(Hy, Hx)) / M_PI); |
||
231 | // atan2 returns angular range from -180 deg to 180 deg in counter clockwise notation |
||
232 | // but the compass course is defined in a range from 0 deg to 360 deg clockwise notation. |
||
233 | if (heading < 0) heading = -heading; |
||
234 | else heading = 360 - heading; |
||
235 | } |
||
236 | // stop interrrupts during heading update to avoid transmitting heading between the update of both bytes of the int16_t |
||
237 | cli(); // stop interrupts |
||
238 | if(abs(heading) < 361) Heading = heading; |
||
239 | else (Heading = -1); |
||
240 | sei(); // start interrupts |
||
1 | ingob | 241 | } |
242 | |||
7 | hbuss | 243 | void Calibrate(void) |
244 | { |
||
32 | holgerb | 245 | static uint8_t calold = 0; |
246 | static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0; |
||
247 | static uint8_t blinkcount = 0; |
||
248 | static uint8_t invert_blinking = 0; |
||
33 | killagreg | 249 | |
68 | holgerb | 250 | if(ActualCalstate > 5) ActualCalstate = 0; |
32 | holgerb | 251 | // blink code for current calibration state |
68 | holgerb | 252 | if(ActualCalstate) |
32 | holgerb | 253 | { |
68 | holgerb | 254 | if(CheckDelay(Led_Timer) || (ActualCalstate != calold)) |
32 | holgerb | 255 | { |
256 | if(blinkcount & 0x01) if(invert_blinking) LED_GRN_ON; else LED_GRN_OFF; |
||
257 | else if(invert_blinking) LED_GRN_OFF; else LED_GRN_ON; |
||
258 | |||
259 | // end of blinkcount sequence |
||
68 | holgerb | 260 | if((blinkcount + 1 ) >= (2 * ActualCalstate)) |
32 | holgerb | 261 | { |
262 | blinkcount = 0; |
||
35 | killagreg | 263 | Led_Timer = SetDelay(1500); |
32 | holgerb | 264 | } |
265 | else |
||
266 | { |
||
267 | blinkcount++; |
||
35 | killagreg | 268 | Led_Timer = SetDelay(100); |
32 | holgerb | 269 | } |
270 | } |
||
271 | } |
||
272 | else |
||
273 | { |
||
274 | if(invert_blinking) LED_GRN_ON; else LED_GRN_OFF; |
||
275 | } |
||
276 | // calibration state machine |
||
68 | holgerb | 277 | switch(ActualCalstate) |
32 | holgerb | 278 | { |
279 | case 1: // 1st step of calibration |
||
280 | // initialize ranges |
||
281 | // used to change the orientation of the MK3MAG in the horizontal plane |
||
282 | Xmin = 10000; |
||
283 | Xmax = -10000; |
||
284 | Ymin = 10000; |
||
285 | Ymax = -10000; |
||
286 | Zmin = 10000; |
||
287 | Zmax = -10000; |
||
288 | Calibration.AccX.Offset = RawAccX; |
||
289 | Calibration.AccY.Offset = RawAccY; |
||
37 | killagreg | 290 | Calibration.AccZ.Offset = RawAccZ; |
291 | invert_blinking = 0; |
||
32 | holgerb | 292 | break; |
293 | |||
294 | case 2: // 2nd step of calibration |
||
295 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
||
296 | if(UncalMagX < Xmin) Xmin = UncalMagX; |
||
297 | if(UncalMagX > Xmax) Xmax = UncalMagX; |
||
298 | if(UncalMagY < Ymin) Ymin = UncalMagY; |
||
299 | if(UncalMagY > Ymax) Ymax = UncalMagY; |
||
37 | killagreg | 300 | invert_blinking = 1; |
32 | holgerb | 301 | break; |
302 | |||
303 | case 3: // 3rd step of calibration |
||
304 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
||
37 | killagreg | 305 | invert_blinking = 0; |
32 | holgerb | 306 | break; |
307 | |||
308 | case 4: |
||
309 | // find Min and Max of the Z-Sensor |
||
310 | if(UncalMagZ < Zmin) Zmin = UncalMagZ; |
||
311 | if(UncalMagZ > Zmax) Zmax = UncalMagZ; |
||
37 | killagreg | 312 | invert_blinking = 1; |
32 | holgerb | 313 | break; |
314 | |||
315 | case 5: |
||
316 | // Save values |
||
68 | holgerb | 317 | if(ActualCalstate != calold) // avoid continously writing of eeprom! |
32 | holgerb | 318 | { |
319 | Calibration.MagX.Range = Xmax - Xmin; |
||
320 | Calibration.MagX.Offset = (Xmin + Xmax) / 2; |
||
321 | Calibration.MagY.Range = Ymax - Ymin; |
||
322 | Calibration.MagY.Offset = (Ymin + Ymax) / 2; |
||
323 | Calibration.MagZ.Range = Zmax - Zmin; |
||
324 | Calibration.MagZ.Offset = (Zmin + Zmax) / 2; |
||
325 | if((Calibration.MagX.Range > 150) && (Calibration.MagY.Range > 150) && (Calibration.MagZ.Range > 150)) |
||
326 | { |
||
36 | hbuss | 327 | Calibration.Version = CALIBRATION_VERSION; |
34 | killagreg | 328 | // indicate write process by setting the led off for 2 seconds |
329 | LED_GRN_OFF; |
||
32 | holgerb | 330 | eeprom_write_block(&Calibration, &eeCalibration, sizeof(Calibration)); |
331 | Led_Timer = SetDelay(2000); |
||
332 | // reset blinkcode |
||
333 | blinkcount = 0; |
||
334 | } |
||
55 | killagreg | 335 | else |
336 | { |
||
337 | // restore old calibration data from eeprom |
||
338 | eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration)); |
||
339 | } |
||
32 | holgerb | 340 | } |
37 | killagreg | 341 | invert_blinking = 0; |
32 | holgerb | 342 | break; |
343 | |||
344 | default: |
||
345 | break; |
||
346 | } |
||
68 | holgerb | 347 | calold = ActualCalstate; |
7 | hbuss | 348 | } |
1 | ingob | 349 | |
32 | holgerb | 350 | |
7 | hbuss | 351 | void SetDebugValues(void) |
352 | { |
||
32 | holgerb | 353 | DebugOut.Analog[0] = MagX; |
354 | DebugOut.Analog[1] = MagY; |
||
355 | DebugOut.Analog[2] = MagZ; |
||
356 | DebugOut.Analog[3] = UncalMagX; |
||
357 | DebugOut.Analog[4] = UncalMagY; |
||
358 | DebugOut.Analog[5] = UncalMagZ; |
||
359 | switch(AttitudeSource) |
||
360 | { |
||
361 | case ATTITUDE_SOURCE_ACC: |
||
362 | DebugOut.Analog[6] = AccAttitudeNick; |
||
363 | DebugOut.Analog[7] = AccAttitudeRoll; |
||
364 | break; |
||
365 | |||
366 | case ATTITUDE_SOURCE_UART: |
||
367 | DebugOut.Analog[6] = ExternData.Attitude[NICK]; |
||
368 | DebugOut.Analog[7] = ExternData.Attitude[ROLL]; |
||
369 | break; |
||
370 | case ATTITUDE_SOURCE_I2C: |
||
371 | DebugOut.Analog[6] = I2C_WriteAttitude.Nick; |
||
372 | DebugOut.Analog[7] = I2C_WriteAttitude.Roll; |
||
373 | break; |
||
69 | killagreg | 374 | default: |
68 | holgerb | 375 | DebugOut.Analog[6] = -1; |
376 | DebugOut.Analog[7] = -1; |
||
377 | break; |
||
32 | holgerb | 378 | } |
379 | DebugOut.Analog[8] = Calibration.MagX.Offset; |
||
380 | DebugOut.Analog[9] = Calibration.MagX.Range; |
||
381 | DebugOut.Analog[10] = Calibration.MagY.Offset; |
||
382 | DebugOut.Analog[11] = Calibration.MagY.Range; |
||
383 | DebugOut.Analog[12] = Calibration.MagZ.Offset; |
||
384 | DebugOut.Analog[13] = Calibration.MagZ.Range; |
||
68 | holgerb | 385 | DebugOut.Analog[14] = ActualCalstate; |
32 | holgerb | 386 | DebugOut.Analog[15] = Heading; |
68 | holgerb | 387 | DebugOut.Analog[16] = 0; |
388 | DebugOut.Analog[17] = 0; |
||
32 | holgerb | 389 | DebugOut.Analog[18] = AccX; |
390 | DebugOut.Analog[19] = AccY; |
||
391 | DebugOut.Analog[20] = AccZ; |
||
392 | DebugOut.Analog[21] = RawAccX; |
||
393 | DebugOut.Analog[22] = RawAccY; |
||
394 | DebugOut.Analog[23] = RawAccZ; |
||
68 | holgerb | 395 | DebugOut.Analog[24] = PotiToFC[0]; |
396 | DebugOut.Analog[25] = PotiToFC[1]; |
||
397 | DebugOut.Analog[26] = PotiToFC[2]; |
||
37 | killagreg | 398 | DebugOut.Analog[29] = AttitudeSource; |
7 | hbuss | 399 | } |
400 | |||
16 | holgerb | 401 | void AccMeasurement(void) |
402 | { |
||
32 | holgerb | 403 | if(AccPresent) |
404 | { |
||
405 | RawAccX = (RawAccX + (int16_t)ADC_GetValue(ACC_X))/2; |
||
406 | RawAccY = (RawAccY + (int16_t)ADC_GetValue(ACC_Y))/2; |
||
407 | RawAccZ = (RawAccZ + (int16_t)ADC_GetValue(ACC_Z))/2; |
||
408 | } |
||
409 | else |
||
410 | { |
||
411 | RawAccX = 0; |
||
412 | RawAccY = 0; |
||
413 | RawAccZ = 0; |
||
414 | } |
||
16 | holgerb | 415 | } |
7 | hbuss | 416 | |
68 | holgerb | 417 | #ifdef HEADTRACKER |
418 | void CalcFcPotis(void) |
||
419 | { |
||
69 | killagreg | 420 | int16_t tmp0, tmp1, tmp2; |
421 | if(AccPresent) |
||
422 | { |
||
71 | killagreg | 423 | tmp0 = AccAttitudeNick/4; |
424 | tmp1 = AccAttitudeRoll/4; |
||
425 | tmp2 = (360 + Heading - ZeroHeading) % 360; |
||
426 | tmp2 -= 180; |
||
427 | tmp2 *= 2; |
||
428 | if(tmp0 > 127) tmp0 = 127; else if(tmp0 < -127) tmp0 = -127; |
||
429 | if(tmp1 > 127) tmp1 = 127; else if(tmp1 < -127) tmp1 = -127; |
||
430 | if(tmp2 > 127) tmp2 = 127; else if(tmp2 < -127) tmp2 = -127; |
||
431 | PotiToFC[0] = (PotiToFC[0] + tmp0) / 2; |
||
432 | PotiToFC[1] = (PotiToFC[1] + tmp1) / 2; |
||
433 | PotiToFC[2] = (PotiToFC[2] + tmp2) / 2; |
||
434 | } |
||
68 | holgerb | 435 | } |
436 | #endif |
||
437 | |||
1 | ingob | 438 | int main (void) |
439 | { |
||
55 | killagreg | 440 | static uint8_t state = 0; |
441 | static uint16_t statetimer; |
||
32 | holgerb | 442 | // reset input pullup |
443 | DDRC &=~((1<<DDC6)); |
||
444 | PORTC |= (1<<PORTC6); |
||
17 | holgerb | 445 | |
37 | killagreg | 446 | LED_Init(); |
447 | TIMER0_Init(); |
||
448 | USART0_Init(); |
||
449 | ADC_Init(); |
||
32 | holgerb | 450 | I2C_Init(); |
17 | holgerb | 451 | |
37 | killagreg | 452 | sei(); // enable globale interrupts |
63 | holgerb | 453 | USART0_Print("\n\rthe use of this software is only permitted\n\ron original MikroKopter-Hardware"); |
68 | holgerb | 454 | USART0_Print("\n\rwww.MikroKopter.de (c) HiSystems GmbH\n\n"); |
455 | #ifdef HEADTRACKER |
||
72 | killagreg | 456 | USART0_Print("\n\rHead-Tracker"); |
68 | holgerb | 457 | #endif |
37 | killagreg | 458 | if(AccPresent) |
459 | { |
||
72 | killagreg | 460 | USART0_Print("\r\rACC present"); |
32 | holgerb | 461 | } |
68 | holgerb | 462 | #ifdef HEADTRACKER |
72 | killagreg | 463 | else USART0_Print("\n\rERROR: NO ACC"); |
68 | holgerb | 464 | #endif |
32 | holgerb | 465 | |
37 | killagreg | 466 | LED_GRN_ON; |
32 | holgerb | 467 | |
37 | killagreg | 468 | Led_Timer = SetDelay(200); |
32 | holgerb | 469 | |
470 | // read calibration info from eeprom |
||
471 | eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration)); |
||
472 | |||
68 | holgerb | 473 | InternalCalstate = 0; |
37 | killagreg | 474 | ExternData.CalState = 0; |
475 | I2C_WriteCal.CalByte = 0; |
||
69 | killagreg | 476 | ActualCalstate = 0; |
55 | killagreg | 477 | statetimer = SetDelay(0); |
32 | holgerb | 478 | // main loop |
37 | killagreg | 479 | while (1) |
480 | { |
||
7 | hbuss | 481 | |
55 | killagreg | 482 | switch(state) |
483 | { |
||
484 | case 0: |
||
485 | if(CheckDelay(statetimer)) |
||
486 | { |
||
487 | FLIP_LOW; |
||
488 | statetimer = SetDelay(2); |
||
489 | state = 1; |
||
490 | } |
||
491 | break; |
||
1 | ingob | 492 | |
55 | killagreg | 493 | case 1: |
494 | if(CheckDelay(statetimer)) |
||
495 | { |
||
496 | RawMagnet1a = ADC_GetValue(MAG_X); |
||
497 | RawMagnet2a = -ADC_GetValue(MAG_Y); |
||
498 | RawMagnet3a = ADC_GetValue(MAG_Z); |
||
499 | AccMeasurement(); |
||
500 | statetimer = SetDelay(1); |
||
501 | state = 2; |
||
502 | } |
||
503 | break; |
||
1 | ingob | 504 | |
55 | killagreg | 505 | case 2: |
506 | if(CheckDelay(statetimer)) |
||
507 | { |
||
508 | FLIP_HIGH; |
||
509 | statetimer = SetDelay(2); |
||
510 | state = 3; |
||
511 | } |
||
512 | break; |
||
16 | holgerb | 513 | |
55 | killagreg | 514 | case 3: |
515 | if(CheckDelay(statetimer)) |
||
516 | { |
||
517 | RawMagnet1b = ADC_GetValue(MAG_X); |
||
518 | RawMagnet2b = -ADC_GetValue(MAG_Y); |
||
519 | RawMagnet3b = ADC_GetValue(MAG_Z); |
||
520 | AccMeasurement(); |
||
521 | statetimer = SetDelay(1); |
||
522 | state = 0; |
||
523 | CalcFields(); |
||
68 | holgerb | 524 | // check both sources of communication for calibration request |
69 | killagreg | 525 | if(InternalCalstate) ActualCalstate = InternalCalstate; |
526 | else |
||
68 | holgerb | 527 | if(I2C_WriteCal.CalByte) ActualCalstate = I2C_WriteCal.CalByte; |
528 | else ActualCalstate = ExternData.CalState; |
||
529 | if(ActualCalstate) Calibrate(); |
||
55 | killagreg | 530 | else CalcHeading(); |
531 | if(NC_Connected) NC_Connected--; |
||
532 | if(FC_Connected) FC_Connected--; |
||
533 | if(PC_Connected) PC_Connected--; |
||
534 | // fall back to attitude estimation from onboard acc sensor if NC or FC does'nt send attitude data |
||
68 | holgerb | 535 | #ifdef HEADTRACKER |
69 | killagreg | 536 | CalcFcPotis(); |
68 | holgerb | 537 | if(AccPresent) AttitudeSource = ATTITUDE_SOURCE_ACC; |
538 | #else |
||
55 | killagreg | 539 | if(!FC_Connected && ! NC_Connected) |
540 | { |
||
68 | holgerb | 541 | if(AccPresent) AttitudeSource = ATTITUDE_SOURCE_ACC; |
542 | else AttitudeSource = ATTITUDE_SOURCE_NONE; |
||
55 | killagreg | 543 | Orientation = ORIENTATION_FC; |
544 | } |
||
68 | holgerb | 545 | #endif |
55 | killagreg | 546 | } |
547 | break; |
||
548 | |||
549 | default: // should never happen |
||
550 | statetimer = SetDelay(0); |
||
551 | state = 0; |
||
552 | break; |
||
553 | } // one full state cylce is 6 ms |
||
554 | |||
32 | holgerb | 555 | // check data from USART |
37 | killagreg | 556 | USART0_ProcessRxData(); |
68 | holgerb | 557 | #ifdef HEADTRACKER |
558 | USART0_TransmitTxData(); |
||
559 | #else |
||
37 | killagreg | 560 | if(PC_Connected) |
561 | { |
||
562 | USART0_EnableTXD(); |
||
563 | USART0_TransmitTxData(); |
||
32 | holgerb | 564 | } |
565 | else |
||
566 | { |
||
567 | USART0_DisableTXD(); |
||
568 | } |
||
68 | holgerb | 569 | #endif |
32 | holgerb | 570 | } // while(1) |
1 | ingob | 571 | } |
572 |