Rev 838 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
838 | MikeW | 1 | /* |
2 | |||
3 | Copyright 2008, by Killagreg |
||
4 | |||
5 | This program (files mm3.c and mm3.h) is free software; you can redistribute it and/or modify |
||
6 | it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; |
||
7 | either version 3 of the License, or (at your option) any later version. |
||
8 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
||
9 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
10 | GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License |
||
11 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
12 | |||
13 | Please note: The original implementation was done by Niklas Nold. |
||
14 | All the other files for the project "Mikrokopter" by H. Buss are under the license (license_buss.txt) published by www.mikrokopter.de |
||
15 | */ |
||
16 | #include <stdlib.h> |
||
17 | #include <avr/io.h> |
||
18 | #include <avr/interrupt.h> |
||
19 | |||
20 | #include "mm3.h" |
||
21 | #include "main.h" |
||
22 | #include "mymath.h" |
||
23 | #include "fc.h" |
||
24 | #include "timer0.h" |
||
25 | #include "rc.h" |
||
26 | //#include "eeprom.h" |
||
27 | #include "printf_P.h" |
||
28 | #include "kafi.h" |
||
29 | |||
30 | |||
31 | #define MAX_AXIS_VALUE 500 |
||
32 | |||
33 | #ifdef USE_Extended_MM3_Measurement_Model |
||
34 | f32_t MM3_Hx, MM3_Hy, MM3_Hz; |
||
35 | #endif |
||
36 | |||
37 | extern int Theta45; |
||
38 | extern int Phi45; |
||
39 | |||
40 | typedef struct |
||
41 | { |
||
966 | MikeW | 42 | uint8_t STATE; |
43 | uint16_t DRDY; |
||
44 | uint8_t AXIS; |
||
45 | int16_t x_axis; |
||
46 | int16_t y_axis; |
||
47 | int16_t z_axis; |
||
838 | MikeW | 48 | } MM3_working_t; |
49 | |||
50 | |||
51 | // MM3 State Machine |
||
966 | MikeW | 52 | #define MM3_STATE_RESET 0 |
53 | #define MM3_STATE_START_TRANSFER 1 |
||
54 | #define MM3_STATE_WAIT_DRDY 2 |
||
55 | #define MM3_STATE_DRDY 3 |
||
56 | #define MM3_STATE_BYTE2 4 |
||
838 | MikeW | 57 | |
966 | MikeW | 58 | #define MM3_X_AXIS 0x01 |
59 | #define MM3_Y_AXIS 0x02 |
||
60 | #define MM3_Z_AXIS 0x03 |
||
838 | MikeW | 61 | |
62 | |||
966 | MikeW | 63 | #define MM3_PERIOD_32 0x00 |
64 | #define MM3_PERIOD_64 0x10 |
||
65 | #define MM3_PERIOD_128 0x20 |
||
66 | #define MM3_PERIOD_256 0x30 |
||
67 | #define MM3_PERIOD_512 0x40 |
||
838 | MikeW | 68 | #define MM3_PERIOD_1024 0x50 |
69 | #define MM3_PERIOD_2048 0x60 |
||
70 | #define MM3_PERIOD_4096 0x70 |
||
71 | |||
72 | MM3_calib_t MM3_calib; |
||
73 | volatile MM3_working_t MM3; |
||
74 | volatile uint8_t MM3_Timeout = 0; |
||
75 | |||
76 | /***************************************************/ |
||
77 | /* Read Parameter from EEPROM as byte */ |
||
78 | /***************************************************/ |
||
79 | uint8_t GetParamByte(uint8_t param_id) |
||
80 | { |
||
966 | MikeW | 81 | return eeprom_read_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id]); |
838 | MikeW | 82 | } |
83 | |||
84 | /***************************************************/ |
||
85 | /* Write Parameter to EEPROM as byte */ |
||
86 | /***************************************************/ |
||
87 | void SetParamByte(uint8_t param_id, uint8_t value) |
||
88 | { |
||
966 | MikeW | 89 | eeprom_write_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value); |
838 | MikeW | 90 | } |
91 | |||
92 | /***************************************************/ |
||
93 | /* Read Parameter from EEPROM as word */ |
||
94 | /***************************************************/ |
||
95 | uint16_t GetParamWord(uint8_t param_id) |
||
96 | { |
||
966 | MikeW | 97 | return eeprom_read_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id]); |
838 | MikeW | 98 | } |
99 | |||
100 | /***************************************************/ |
||
101 | /* Write Parameter to EEPROM as word */ |
||
102 | /***************************************************/ |
||
103 | void SetParamWord(uint8_t param_id, uint16_t value) |
||
104 | { |
||
966 | MikeW | 105 | eeprom_write_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value); |
838 | MikeW | 106 | } |
107 | |||
108 | /*********************************************/ |
||
109 | /* Initialize Interface to MM3 Compass */ |
||
110 | /*********************************************/ |
||
111 | void MM3_Init(void) |
||
112 | { |
||
966 | MikeW | 113 | uint8_t sreg = SREG; |
114 | |||
115 | cli(); |
||
116 | |||
117 | // Configure Pins for SPI |
||
118 | // set SCK (PB7), MOSI (PB5) as output |
||
119 | DDRB |= (1<<DDB7)|(1<<DDB5); |
||
120 | // set MISO (PB6) as input |
||
121 | DDRB &= ~(1<<DDB6); |
||
122 | |||
123 | // Output Pins PC4->MM3_SS ,PC5->MM3_RESET |
||
124 | DDRC |= (1<<DDC4)|(1<<DDC5); |
||
125 | // set pins permanent to low |
||
126 | PORTC &= ~((1<<PORTC4)|(1<<PORTC5)); |
||
127 | |||
128 | // Initialize SPI-Interface |
||
129 | // Enable interrupt (SPIE=1) |
||
130 | // Enable SPI bus (SPE=1) |
||
131 | // MSB transmitted first (DORD = 0) |
||
132 | // Master SPI Mode (MSTR=1) |
||
133 | // Clock polarity low when idle (CPOL=0) |
||
134 | // Clock phase sample at leading edge (CPHA=0) |
||
135 | // Clock rate = SYSCLK/128 (SPI2X=0, SPR1=1, SPR0=1) 20MHz/128 = 156.25kHz |
||
136 | SPCR = (1<<SPIE)|(1<<SPE)|(0<<DORD)|(1<<MSTR)|(0<<CPOL)|(0<<CPHA)|(1<<SPR1)|(1<<SPR0); |
||
137 | SPSR &= ~(1<<SPI2X); |
||
138 | |||
139 | // Init Statemachine |
||
140 | MM3.AXIS = MM3_X_AXIS; |
||
141 | MM3.STATE = MM3_STATE_RESET; |
||
142 | |||
143 | // Read calibration from EEprom |
||
144 | MM3_calib.X_off = (int16_t)GetParamWord(PID_MM3_X_OFF); |
||
145 | MM3_calib.Y_off = (int16_t)GetParamWord(PID_MM3_Y_OFF); |
||
146 | MM3_calib.Z_off = (int16_t)GetParamWord(PID_MM3_Z_OFF); |
||
147 | MM3_calib.X_range = (int16_t)GetParamWord(PID_MM3_X_RANGE); |
||
148 | MM3_calib.Y_range = (int16_t)GetParamWord(PID_MM3_Y_RANGE); |
||
149 | MM3_calib.Z_range = (int16_t)GetParamWord(PID_MM3_Z_RANGE); |
||
150 | |||
151 | MM3_Timeout = 0; |
||
152 | |||
153 | SREG = sreg; |
||
838 | MikeW | 154 | } |
155 | |||
156 | /*********************************************/ |
||
157 | /* Get Data from MM3 */ |
||
158 | /*********************************************/ |
||
159 | void MM3_Update(void) // called every 102.4 µs by timer 0 ISR |
||
160 | { |
||
966 | MikeW | 161 | switch (MM3.STATE) |
162 | { |
||
163 | case MM3_STATE_RESET: |
||
164 | PORTC &= ~(1<<PORTC4); // select slave |
||
165 | PORTC |= (1<<PORTC5); // PC5 to High, MM3 Reset |
||
166 | MM3.STATE = MM3_STATE_START_TRANSFER; |
||
167 | return; |
||
168 | |||
169 | case MM3_STATE_START_TRANSFER: |
||
170 | PORTC &= ~(1<<PORTC5); // PC4 auf Low (was 102.4 µs at high level) |
||
171 | // write to SPDR triggers automatically the transfer MOSI MISO |
||
172 | // MM3 Period, + AXIS code |
||
173 | switch(MM3.AXIS) |
||
174 | { |
||
175 | case MM3_X_AXIS: |
||
176 | SPDR = MM3_PERIOD_256 + MM3_X_AXIS; |
||
177 | break; |
||
178 | case MM3_Y_AXIS: |
||
179 | SPDR = MM3_PERIOD_256 + MM3_Y_AXIS; |
||
180 | break; |
||
181 | case MM3_Z_AXIS: |
||
182 | SPDR = MM3_PERIOD_256 + MM3_Z_AXIS; |
||
183 | break; |
||
184 | default: |
||
185 | MM3.AXIS = MM3_X_AXIS; |
||
186 | MM3.STATE = MM3_STATE_RESET; |
||
187 | return; |
||
188 | } |
||
189 | |||
190 | // DRDY line is not connected, therefore |
||
191 | // wait before reading data back |
||
192 | MM3.DRDY = SetDelay(8); // wait 8ms for data ready |
||
193 | MM3.STATE = MM3_STATE_WAIT_DRDY; |
||
194 | return; |
||
195 | |||
196 | case MM3_STATE_WAIT_DRDY: |
||
197 | if (CheckDelay(MM3.DRDY)) |
||
198 | { |
||
199 | // write something into SPDR to trigger data reading |
||
200 | SPDR = 0x00; |
||
201 | MM3.STATE = MM3_STATE_DRDY; |
||
202 | } |
||
203 | return; |
||
204 | } |
||
838 | MikeW | 205 | } |
206 | |||
207 | /*********************************************/ |
||
208 | /* Interrupt SPI transfer complete */ |
||
209 | /*********************************************/ |
||
210 | ISR(SPI_STC_vect) |
||
211 | { |
||
966 | MikeW | 212 | static int8_t tmp; |
213 | int16_t value; |
||
214 | |||
215 | switch (MM3.STATE) |
||
216 | { |
||
217 | // 1st byte received |
||
218 | case MM3_STATE_DRDY: |
||
219 | tmp = SPDR; // store 1st byte |
||
220 | SPDR = 0x00; // trigger transfer of 2nd byte |
||
221 | MM3.STATE = MM3_STATE_BYTE2; |
||
222 | return; |
||
223 | |||
224 | case MM3_STATE_BYTE2: // 2nd byte received |
||
225 | value = (int16_t)tmp; // combine the 1st and 2nd byte to a word |
||
226 | value <<= 8; // shift 1st byte to MSB-Position |
||
227 | value |= (int16_t)SPDR; // add 2nd byte |
||
228 | |||
229 | if(abs(value) < MAX_AXIS_VALUE) // ignore spikes |
||
230 | { |
||
231 | switch (MM3.AXIS) |
||
232 | { |
||
233 | case MM3_X_AXIS: |
||
234 | MM3.x_axis = value; |
||
235 | MM3.AXIS = MM3_Y_AXIS; |
||
236 | break; |
||
237 | case MM3_Y_AXIS: |
||
238 | MM3.y_axis = value; |
||
239 | MM3.AXIS = MM3_Z_AXIS; |
||
240 | break; |
||
241 | case MM3_Z_AXIS: |
||
242 | MM3.z_axis = value; |
||
243 | MM3.AXIS = MM3_X_AXIS; |
||
244 | break; |
||
245 | default: |
||
246 | MM3.AXIS = MM3_X_AXIS; |
||
247 | break; |
||
248 | } |
||
249 | } |
||
250 | |||
251 | PORTC |= (1<<PORTC4); // deselect slave |
||
252 | MM3.STATE = MM3_STATE_RESET; |
||
253 | // Update timeout is called every 102.4 µs. |
||
254 | // It takes 2 cycles to write a measurement data request for one axis and |
||
255 | // at at least 8 ms / 102.4 µs = 79 cycles to read the requested data back. |
||
256 | // I.e. 81 cycles * 102.4 µs = 8.3ms per axis. |
||
257 | // The two function accessing the MM3 Data - MM3_Calibrate() and MM3_Heading() - |
||
258 | // decremtent the MM3_Timeout every 100 ms. |
||
259 | // incrementing the counter by 1 every 8.3 ms is sufficient to avoid a timeout. |
||
260 | if ((MM3.x_axis != MM3.y_axis) || (MM3.x_axis != MM3.z_axis) || (MM3.y_axis != MM3.z_axis)) |
||
261 | { // if all axis measurements give diffrent readings the data should be valid |
||
262 | if(MM3_Timeout < 20) MM3_Timeout++; |
||
263 | } |
||
264 | else // something is very strange here |
||
265 | { |
||
266 | if(MM3_Timeout ) MM3_Timeout--; |
||
267 | } |
||
268 | return; |
||
269 | |||
270 | default: |
||
271 | return; |
||
272 | } |
||
838 | MikeW | 273 | } |
274 | |||
275 | |||
276 | |||
277 | /*********************************************/ |
||
278 | /* Calibrate Compass */ |
||
279 | /*********************************************/ |
||
280 | void MM3_Calibrate(void) |
||
281 | { |
||
966 | MikeW | 282 | static uint8_t debugcounter = 0; |
283 | int16_t x_min = 0, x_max = 0, y_min = 0, y_max = 0, z_min = 0, z_max = 0; |
||
284 | uint8_t measurement = 50, beeper = 0; |
||
285 | uint16_t timer; |
||
286 | |||
287 | GRN_ON; |
||
288 | ROT_OFF; |
||
289 | |||
290 | x_max = -16000; |
||
291 | x_min = 16000; |
||
292 | y_max = -16000; |
||
293 | y_min = 16000; |
||
294 | z_max = -16000; |
||
295 | z_min = 16000; |
||
296 | |||
297 | // get maximum and minimum reading of all axis |
||
298 | while (measurement) |
||
299 | { |
||
300 | if (MM3.x_axis > x_max) x_max = MM3.x_axis; |
||
301 | else if (MM3.x_axis < x_min) x_min = MM3.x_axis; |
||
302 | |||
303 | if (MM3.y_axis > y_max) y_max = MM3.y_axis; |
||
304 | else if (MM3.y_axis < y_min) y_min = MM3.y_axis; |
||
305 | |||
306 | if (MM3.z_axis > z_max) z_max = MM3.z_axis; |
||
307 | else if (MM3.z_axis < z_min) z_min = MM3.z_axis; |
||
308 | |||
309 | if (!beeper) |
||
310 | { |
||
311 | ROT_FLASH; |
||
312 | GRN_FLASH; |
||
313 | beeper = 50; |
||
314 | } |
||
315 | beeper--; |
||
316 | |||
317 | // loop with period of 10 ms / 100 Hz |
||
318 | timer = SetDelay(10); |
||
319 | while(!CheckDelay(timer)); |
||
320 | |||
321 | if(debugcounter++ > 30) |
||
322 | { |
||
323 | printf("\n\rXMin:%4d, XMax:%4d, YMin:%4d, YMax:%4d, ZMin:%4d, ZMax:%4d",x_min,x_max,y_min,y_max,z_min,z_max); |
||
324 | debugcounter = 0; |
||
325 | } |
||
326 | |||
327 | // If thrust is less than 100, stop calibration with a delay of 0.5 seconds |
||
328 | if (PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] < 100) measurement--; |
||
329 | } |
||
330 | |||
331 | // Rage of all axis |
||
332 | MM3_calib.X_range = (x_max - x_min); |
||
333 | MM3_calib.Y_range = (y_max - y_min); |
||
334 | MM3_calib.Z_range = (z_max - z_min); |
||
335 | |||
336 | // Offset of all axis |
||
337 | MM3_calib.X_off = (x_max + x_min) / 2; |
||
338 | MM3_calib.Y_off = (y_max + y_min) / 2; |
||
339 | MM3_calib.Z_off = (z_max + z_min) / 2; |
||
340 | |||
341 | // save to EEProm |
||
342 | SetParamWord(PID_MM3_X_OFF, (uint16_t)MM3_calib.X_off); |
||
343 | SetParamWord(PID_MM3_Y_OFF, (uint16_t)MM3_calib.Y_off); |
||
344 | SetParamWord(PID_MM3_Z_OFF, (uint16_t)MM3_calib.Z_off); |
||
345 | SetParamWord(PID_MM3_X_RANGE, (uint16_t)MM3_calib.X_range); |
||
346 | SetParamWord(PID_MM3_Y_RANGE, (uint16_t)MM3_calib.Y_range); |
||
347 | SetParamWord(PID_MM3_Z_RANGE, (uint16_t)MM3_calib.Z_range); |
||
838 | MikeW | 348 | } |
349 | |||
350 | |||
351 | /*********************************************/ |
||
352 | /* Calculate north direction (heading) */ |
||
353 | /*********************************************/ |
||
354 | int16_t MM3_Heading(void) |
||
355 | { |
||
966 | MikeW | 356 | int32_t sin_pitch, cos_pitch, sin_roll, cos_roll, sin_yaw, cos_yaw; |
357 | int32_t Hx, Hy, Hz, Hx_corr, Hy_corr; |
||
358 | int16_t angle; |
||
359 | int16_t heading; |
||
360 | |||
361 | if (MM3_Timeout) |
||
362 | { |
||
363 | // Offset correction and normalization (values of H are +/- 512) |
||
364 | Hx = (((int32_t)(MM3.x_axis - MM3_calib.X_off)) * 1024) / (int32_t)MM3_calib.X_range; |
||
365 | Hy = (((int32_t)(MM3.y_axis - MM3_calib.Y_off)) * 1024) / (int32_t)MM3_calib.Y_range; |
||
366 | Hz = (((int32_t)(MM3.z_axis - MM3_calib.Z_off)) * 1024) / (int32_t)MM3_calib.Z_range; |
||
367 | |||
368 | |||
369 | // Compensate the angle of the MM3-arrow to the head of the MK by a yaw rotation transformation |
||
838 | MikeW | 370 | // assuming the MM3 board is mounted parallel to the frame. |
371 | // User Param 4 is used to define the positive angle from the MM3-arrow to the MK heading |
||
372 | // in a top view counter clockwise direction. |
||
373 | // North is in opposite direction of the small arrow on the MM3 board. |
||
374 | // Therefore 180 deg must be added to that angle. |
||
966 | MikeW | 375 | angle = ((int16_t)180); |
376 | // wrap angle to interval of 0°- 359° |
||
377 | angle += 360; |
||
378 | angle %= 360; |
||
379 | sin_yaw = (int32_t)(c_sin_8192(angle)); |
||
380 | cos_yaw = (int32_t)(c_cos_8192(angle)); |
||
381 | |||
382 | Hx_corr = Hx; |
||
383 | Hy_corr = Hy; |
||
384 | |||
385 | // rotate |
||
386 | Hx = (Hx_corr * cos_yaw - Hy_corr * sin_yaw) / 8192; |
||
387 | Hy = (Hx_corr * sin_yaw + Hy_corr * cos_yaw) / 8192; |
||
388 | |||
838 | MikeW | 389 | #ifdef USE_Extended_MM3_Measurement_Model |
966 | MikeW | 390 | /* Normalize the values to be in the same range as the accelerations */ |
391 | MM3_Hx = Hx / 51.2F; |
||
392 | MM3_Hy = Hy / 51.2F; |
||
393 | MM3_Hz = Hz / 51.2F; |
||
838 | MikeW | 394 | #endif |
966 | MikeW | 395 | |
838 | MikeW | 396 | // tilt compensation |
966 | MikeW | 397 | |
398 | // calibration factor for transforming Gyro Integrals to angular degrees |
||
399 | |||
400 | // calculate sinus cosinus of pitch and tilt angle |
||
401 | //angle = (status. IntegralPitch/div_factor); |
||
402 | angle = (status.iTheta10 / 10); |
||
403 | angle = 0; |
||
404 | sin_pitch = (int32_t)(c_sin_8192(angle)); |
||
405 | cos_pitch = (int32_t)(c_cos_8192(angle)); |
||
406 | |||
407 | //angle = (IntegralRoll/div_factor); |
||
408 | angle = (status.iPhi10/10); |
||
409 | angle = 0; |
||
410 | sin_roll = (int32_t)(c_sin_8192(angle)); |
||
411 | cos_roll = (int32_t)(c_cos_8192(angle)); |
||
412 | |||
413 | Hx_corr = (Hx * cos_pitch - Hz * sin_pitch) / 8192; |
||
414 | Hy_corr = (Hy * cos_roll + Hz * sin_roll) / 8192; |
||
415 | |||
416 | // calculate Heading |
||
417 | heading = c_atan2(Hy_corr, Hx_corr); |
||
418 | |||
419 | #if 0 |
||
420 | DebugOut.Analog[3] = Hx; |
||
421 | DebugOut.Analog[4] = Hy; |
||
422 | DebugOut.Analog[5] = Hz; |
||
423 | #endif |
||
424 | |||
838 | MikeW | 425 | // atan returns angular range from -180 deg to 180 deg in counter clockwise notation |
426 | // but the compass course is defined in a range from 0 deg to 360 deg clockwise notation. |
||
966 | MikeW | 427 | if (heading < 0) heading = -heading; |
428 | else heading = 360 - heading; |
||
429 | } |
||
430 | else // MM3_Timeout = 0 i.e now new data from external board |
||
431 | { |
||
432 | // if(!BeepTime) BeepTime = 100; // make noise to signal the compass problem |
||
433 | heading = -1; |
||
434 | } |
||
435 | return heading; |
||
838 | MikeW | 436 | } |
437 | |||
438 | |||
439 | |||
440 |