Rev 25 | Rev 27 | 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 |
19 | killagreg | 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 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
19 | killagreg | 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 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
19 | killagreg | 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 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | killagreg | 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 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
19 | killagreg | 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. |
||
19 | killagreg | 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) |
19 | killagreg | 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 |
||
19 | killagreg | 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 |
19 | killagreg | 55 | // + POSSIBILITY OF SUCH DAMAGE. |
5 | hbuss | 56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | killagreg | 57 | #include <avr/interrupt.h> |
58 | #include <math.h> |
||
59 | #include <stdlib.h> |
||
21 | killagreg | 60 | #include <stdio.h> |
5 | hbuss | 61 | |
1 | ingob | 62 | #include "main.h" |
19 | killagreg | 63 | #include "timer0.h" |
64 | #include "twislave.h" |
||
65 | #include "led.h" |
||
66 | #include "analog.h" |
||
67 | #include "uart.h" |
||
1 | ingob | 68 | |
69 | |||
19 | killagreg | 70 | int16_t RawMagnet1a, RawMagnet1b; // raw AD-Data |
71 | int16_t RawMagnet2a, RawMagnet2b; |
||
72 | int16_t RawMagnet3a, RawMagnet3b; |
||
7 | hbuss | 73 | |
21 | killagreg | 74 | uint16_t Led_Timer = 0; |
75 | |||
23 | killagreg | 76 | struct Scaling_t |
1 | ingob | 77 | { |
19 | killagreg | 78 | int16_t Range; |
79 | int16_t Offset; |
||
23 | killagreg | 80 | } ; |
1 | ingob | 81 | |
23 | killagreg | 82 | struct Calibration_t |
19 | killagreg | 83 | { |
23 | killagreg | 84 | struct Scaling_t X; |
85 | struct Scaling_t Y; |
||
86 | struct Scaling_t Z; |
||
87 | } ; |
||
19 | killagreg | 88 | |
23 | killagreg | 89 | struct Calibration_t eeCalibration EEMEM; // calibration data in EEProm |
90 | struct Calibration_t Calibration; // calibration data in RAM |
||
19 | killagreg | 91 | |
92 | |||
21 | killagreg | 93 | int16_t UncalMagnetX, UncalMagnetY, UncalMagnetZ; // sensor signal difference without Scaling |
22 | killagreg | 94 | int16_t MagnetX, MagnetY, MagnetZ; // rescaled magnetic field readings |
19 | killagreg | 95 | |
96 | uint8_t PC_Connected = 0; |
||
97 | |||
21 | killagreg | 98 | int16_t Heading = -1; |
19 | killagreg | 99 | |
100 | |||
1 | ingob | 101 | void CalcFields(void) |
7 | hbuss | 102 | { |
19 | killagreg | 103 | UncalMagnetX = (1 * UncalMagnetX + (RawMagnet1a - RawMagnet1b)) / 2; |
104 | UncalMagnetY = (1 * UncalMagnetY + (RawMagnet3a - RawMagnet3b)) / 2; |
||
105 | UncalMagnetZ = (1 * UncalMagnetZ + (RawMagnet2a - RawMagnet2b)) / 2; |
||
7 | hbuss | 106 | |
22 | killagreg | 107 | if(Calibration.X.Range != 0) MagnetX = (1024L * (int32_t)(UncalMagnetX - Calibration.X.Offset)) / (Calibration.X.Range); |
108 | else MagnetX = 0; |
||
109 | if(Calibration.Y.Range != 0) MagnetY = (1024L * (int32_t)(UncalMagnetY - Calibration.Y.Offset)) / (Calibration.Y.Range); |
||
110 | else MagnetY = 0; |
||
111 | if(Calibration.Y.Range != 0) MagnetZ = (1024L * (int32_t)(UncalMagnetZ - Calibration.Z.Offset)) / (Calibration.Z.Range); |
||
112 | else MagnetZ = 0; |
||
1 | ingob | 113 | } |
114 | |||
20 | killagreg | 115 | |
1 | ingob | 116 | void CalcHeading(void) |
117 | { |
||
19 | killagreg | 118 | double nick_rad, roll_rad, Hx, Hy, Cx, Cy, Cz; |
119 | int16_t heading = -1; |
||
1 | ingob | 120 | |
21 | killagreg | 121 | // blink code for normal operation |
122 | if(CheckDelay(Led_Timer)) |
||
123 | { |
||
124 | LED_GRN_TOGGLE; |
||
125 | Led_Timer = SetDelay(500); |
||
126 | } |
||
7 | hbuss | 127 | |
19 | killagreg | 128 | Cx = MagnetX; |
129 | Cy = MagnetY; |
||
130 | Cz = MagnetZ; |
||
7 | hbuss | 131 | |
19 | killagreg | 132 | if(ExternData.Orientation == 1) |
133 | { |
||
134 | Cx = MagnetX; |
||
135 | Cy = -MagnetY; |
||
136 | Cz = MagnetZ; |
||
137 | } |
||
1 | ingob | 138 | |
20 | killagreg | 139 | // calculate nick and roll angle in rad |
140 | nick_rad = ((double)ExternData.Attitude[NICK]) * M_PI / (double)(1800.0); |
||
141 | roll_rad = ((double)ExternData.Attitude[ROLL]) * M_PI / (double)(1800.0); |
||
142 | // calculate attitude correction |
||
143 | Hx = Cx * (double)cos(nick_rad) - Cz * (double)sin(nick_rad); |
||
144 | Hy = Cy * (double)cos(roll_rad) + Cz * (double)sin(roll_rad); |
||
19 | killagreg | 145 | |
20 | killagreg | 146 | // calculate Heading |
147 | heading = (int16_t)((180.0 * atan2(Hy, Hx)) / M_PI); |
||
148 | // atan2 returns angular range from -180 deg to 180 deg in counter clockwise notation |
||
149 | // but the compass course is defined in a range from 0 deg to 360 deg clockwise notation. |
||
150 | if (heading < 0) heading = -heading; |
||
151 | else heading = 360 - heading; |
||
19 | killagreg | 152 | |
153 | if(abs(heading) < 361) Heading = heading; |
||
20 | killagreg | 154 | else (Heading = -1); |
1 | ingob | 155 | } |
156 | |||
157 | |||
7 | hbuss | 158 | void Calibrate(void) |
159 | { |
||
19 | killagreg | 160 | uint8_t cal; |
21 | killagreg | 161 | static uint8_t calold = 0; |
19 | killagreg | 162 | static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0; |
21 | killagreg | 163 | static uint8_t blinkcount = 0; |
22 | killagreg | 164 | |
19 | killagreg | 165 | // check both sources of communication for calibration request |
166 | if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte; |
||
22 | killagreg | 167 | else cal = ExternData.CalState; |
19 | killagreg | 168 | |
23 | killagreg | 169 | |
170 | if(cal > 5) cal = 0; |
||
21 | killagreg | 171 | // blink code for current calibration state |
172 | if(cal) |
||
173 | { |
||
174 | if(CheckDelay(Led_Timer) || (cal != calold)) |
||
175 | { |
||
176 | if(blinkcount & 0x01) LED_GRN_OFF; |
||
177 | else LED_GRN_ON; |
||
178 | |||
179 | // end of blinkcount sequence |
||
180 | if( (blinkcount + 1 ) >= (2 * cal) ) |
||
181 | { |
||
182 | blinkcount = 0; |
||
183 | Led_Timer = SetDelay(1000); |
||
184 | } |
||
185 | else |
||
186 | { |
||
187 | blinkcount++; |
||
188 | Led_Timer = SetDelay(170); |
||
189 | } |
||
190 | } |
||
191 | } |
||
192 | else |
||
193 | { |
||
194 | LED_GRN_OFF; |
||
195 | } |
||
196 | |||
19 | killagreg | 197 | // calibration state machine |
198 | switch(cal) |
||
199 | { |
||
200 | case 1: // 1st step of calibration |
||
201 | // initialize ranges |
||
202 | // used to change the orientation of the MK3MAG in the horizontal plane |
||
203 | Xmin = 10000; |
||
204 | Xmax = -10000; |
||
205 | Ymin = 10000; |
||
206 | Ymax = -10000; |
||
207 | Zmin = 10000; |
||
208 | Zmax = -10000; |
||
209 | break; |
||
210 | |||
211 | case 2: // 2nd step of calibration |
||
212 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
||
213 | if(UncalMagnetX < Xmin) Xmin = UncalMagnetX; |
||
214 | if(UncalMagnetX > Xmax) Xmax = UncalMagnetX; |
||
215 | if(UncalMagnetY < Ymin) Ymin = UncalMagnetY; |
||
216 | if(UncalMagnetY > Ymax) Ymax = UncalMagnetY; |
||
217 | break; |
||
218 | |||
219 | case 3: // 3rd step of calibration |
||
26 | killagreg | 220 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
19 | killagreg | 221 | break; |
222 | |||
223 | case 4: |
||
224 | // find Min and Max of the Z-Sensor |
||
225 | if(UncalMagnetZ < Zmin) Zmin = UncalMagnetZ; |
||
226 | if(UncalMagnetZ > Zmax) Zmax = UncalMagnetZ; |
||
227 | break; |
||
228 | |||
229 | case 5: |
||
21 | killagreg | 230 | // Save values |
25 | killagreg | 231 | if(cal != calold) // avoid continously writing of eeprom! |
19 | killagreg | 232 | { |
21 | killagreg | 233 | Calibration.X.Range = Xmax - Xmin; |
234 | Calibration.X.Offset = (Xmin + Xmax) / 2; |
||
235 | Calibration.Y.Range = Ymax - Ymin; |
||
236 | Calibration.Y.Offset = (Ymin + Ymax) / 2; |
||
237 | Calibration.Z.Range = Zmax - Zmin; |
||
238 | Calibration.Z.Offset = (Zmin + Zmax) / 2; |
||
239 | if((Calibration.X.Range > 150) && (Calibration.Y.Range > 150) && (Calibration.Z.Range > 150)) |
||
240 | { |
||
241 | // indicate write process by setting the led |
||
242 | LED_GRN_ON; |
||
23 | killagreg | 243 | eeprom_write_block(&Calibration, &eeCalibration, sizeof(Calibration)); |
21 | killagreg | 244 | Delay_ms(2000); |
245 | // reset led state |
||
246 | LED_GRN_OFF; |
||
247 | // reset blinkcode |
||
248 | blinkcount = 0; |
||
249 | Led_Timer = SetDelay(1000); |
||
250 | } |
||
19 | killagreg | 251 | } |
252 | break; |
||
253 | |||
254 | default: |
||
255 | break; |
||
256 | } |
||
21 | killagreg | 257 | calold = cal; |
7 | hbuss | 258 | } |
1 | ingob | 259 | |
7 | hbuss | 260 | |
261 | void SetDebugValues(void) |
||
262 | { |
||
19 | killagreg | 263 | DebugOut.Analog[0] = MagnetX; |
264 | DebugOut.Analog[1] = MagnetY; |
||
265 | DebugOut.Analog[2] = MagnetZ; |
||
266 | DebugOut.Analog[3] = UncalMagnetX; |
||
267 | DebugOut.Analog[4] = UncalMagnetY; |
||
268 | DebugOut.Analog[5] = UncalMagnetZ; |
||
269 | DebugOut.Analog[6] = ExternData.Attitude[NICK]; |
||
270 | DebugOut.Analog[7] = ExternData.Attitude[ROLL]; |
||
271 | DebugOut.Analog[8] = Calibration.X.Offset; |
||
272 | DebugOut.Analog[9] = Calibration.X.Range; |
||
273 | DebugOut.Analog[10] = Calibration.Y.Offset; |
||
274 | DebugOut.Analog[11] = Calibration.Y.Range; |
||
275 | DebugOut.Analog[12] = Calibration.Z.Offset; |
||
276 | DebugOut.Analog[13] = Calibration.Z.Range; |
||
277 | DebugOut.Analog[14] = ExternData.CalState; |
||
278 | DebugOut.Analog[15] = Heading; |
||
279 | DebugOut.Analog[16] = ExternData.UserParam[0]; |
||
280 | DebugOut.Analog[17] = ExternData.UserParam[1]; |
||
7 | hbuss | 281 | } |
282 | |||
283 | |||
1 | ingob | 284 | int main (void) |
285 | { |
||
22 | killagreg | 286 | // reset input pullup |
287 | DDRC &=~((1<<DDC6)); |
||
288 | PORTC |= (1<<PORTC6); |
||
289 | |||
290 | LED_Init(); |
||
19 | killagreg | 291 | TIMER0_Init(); |
292 | USART0_Init(); |
||
1 | ingob | 293 | ADC_Init(); |
19 | killagreg | 294 | I2C_Init(); |
1 | ingob | 295 | |
22 | killagreg | 296 | sei(); // enable globale interrupts |
21 | killagreg | 297 | |
22 | killagreg | 298 | LED_GRN_ON; |
19 | killagreg | 299 | |
22 | killagreg | 300 | Debug_Timer = SetDelay(200); |
301 | Led_Timer = SetDelay(200); |
||
21 | killagreg | 302 | |
19 | killagreg | 303 | // read calibration info from eeprom |
23 | killagreg | 304 | eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration)); |
19 | killagreg | 305 | |
7 | hbuss | 306 | ExternData.Orientation = 0; |
307 | ExternData.CalState = 0; |
||
308 | I2C_WriteCal.CalByte = 0; |
||
19 | killagreg | 309 | |
22 | killagreg | 310 | |
19 | killagreg | 311 | // main loop |
1 | ingob | 312 | while (1) |
19 | killagreg | 313 | { |
314 | FLIP_LOW; |
||
315 | Delay_ms(2); |
||
316 | RawMagnet1a = ADC_GetValue(ADC0); |
||
317 | RawMagnet2a = -ADC_GetValue(ADC1); |
||
318 | RawMagnet3a = ADC_GetValue(ADC7); |
||
319 | Delay_ms(1); |
||
7 | hbuss | 320 | |
21 | killagreg | 321 | |
19 | killagreg | 322 | FLIP_HIGH; |
323 | Delay_ms(2); |
||
324 | RawMagnet1b = ADC_GetValue(ADC0); |
||
325 | RawMagnet2b = -ADC_GetValue(ADC1); |
||
326 | RawMagnet3b = ADC_GetValue(ADC7); |
||
327 | Delay_ms(1); |
||
1 | ingob | 328 | |
19 | killagreg | 329 | CalcFields(); |
1 | ingob | 330 | |
22 | killagreg | 331 | if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate(); |
19 | killagreg | 332 | else CalcHeading(); |
333 | |||
334 | // check data from USART |
||
335 | USART0_ProcessRxData(); |
||
23 | killagreg | 336 | USART0_TransmitTxData(); |
19 | killagreg | 337 | |
338 | if(PC_Connected) |
||
339 | { |
||
340 | USART0_EnableTXD(); |
||
341 | USART0_TransmitTxData(); |
||
342 | PC_Connected--; |
||
343 | } |
||
344 | else |
||
345 | { |
||
346 | USART0_DisableTXD(); |
||
347 | } |
||
348 | } // while(1) |
||
1 | ingob | 349 | } |
350 |