Subversion Repositories FlightCtrl

Rev

Rev 773 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
700 killagreg 1
/*
2
 
741 killagreg 3
Copyright 2008, by Killagreg
700 killagreg 4
 
741 killagreg 5
This program (files mm3.c and mm3.h) is free software; you can redistribute it and/or modify
700 killagreg 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
 
741 killagreg 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
700 killagreg 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
 
28
#define MAX_AXIS_VALUE          500
29
 
30
 
31
typedef struct
32
{
33
        uint8_t STATE;
34
        uint16_t DRDY;
35
        uint8_t AXIS;
36
        int16_t x_axis;
37
        int16_t y_axis;
38
        int16_t z_axis;
39
} MM3_working_t;
40
 
41
 
42
// MM3 State Machine
43
#define MM3_STATE_RESET                         0
44
#define MM3_STATE_START_TRANSFER        1
45
#define MM3_STATE_WAIT_DRDY                     2
46
#define MM3_STATE_DRDY                          3
47
#define MM3_STATE_BYTE2                         4
48
 
49
#define MM3_X_AXIS              0x01
50
#define MM3_Y_AXIS              0x02
51
#define MM3_Z_AXIS              0x03
52
 
53
 
54
#define MM3_PERIOD_32   0x00
55
#define MM3_PERIOD_64   0x10
56
#define MM3_PERIOD_128  0x20
57
#define MM3_PERIOD_256  0x30
58
#define MM3_PERIOD_512  0x40
59
#define MM3_PERIOD_1024 0x50
60
#define MM3_PERIOD_2048 0x60
61
#define MM3_PERIOD_4096 0x70
62
 
63
MM3_calib_t MM3_calib;
64
volatile MM3_working_t MM3;
754 killagreg 65
volatile uint8_t MM3_Timeout = 0;
700 killagreg 66
 
67
 
68
 
69
/*********************************************/
70
/*  Initialize Interface to MM3 Compass      */
71
/*********************************************/
726 killagreg 72
void MM3_Init(void)
700 killagreg 73
{
74
        uint8_t sreg = SREG;
75
 
76
        cli();
77
 
78
        // Configure Pins for SPI
79
        // set SCK (PB7), MOSI (PB5) as output
80
        DDRB |= (1<<DDB7)|(1<<DDB5);
81
        // set MISO (PB6) as input
82
        DDRB &= ~(1<<DDB6);
83
 
781 killagreg 84
#ifdef USE_WALTER_EXT // walthers board
85
        // Output Pins (J9)PC6->MM3_SS ,(J8)PB2->MM3_RESET
86
        DDRB |= (1<<DDB2);
87
        DDRC |= (1<<DDC6);
88
        // set pins permanent to low
89
        PORTB &= ~((1<<PORTB2));
90
        PORTC &= ~((1<<PORTC6));
91
#else // killagregs board
700 killagreg 92
        // Output Pins PC4->MM3_SS ,PC5->MM3_RESET
93
        DDRC |= (1<<DDC4)|(1<<DDC5);
94
        // set pins permanent to low
95
        PORTC &= ~((1<<PORTC4)|(1<<PORTC5));
781 killagreg 96
#endif
700 killagreg 97
 
98
        // Initialize SPI-Interface
99
        // Enable interrupt (SPIE=1)
100
        // Enable SPI bus (SPE=1)
101
        // MSB transmitted first (DORD = 0)
102
        // Master SPI Mode (MSTR=1)
781 killagreg 103
        // Clock polarity low when idle (CPOL=0)
104
        // Clock phase sample at leading edge (CPHA=0)
105
        // Clock rate = SYSCLK/128 (SPI2X=0, SPR1=1, SPR0=1) 20MHz/128 = 156.25kHz
700 killagreg 106
        SPCR = (1<<SPIE)|(1<<SPE)|(0<<DORD)|(1<<MSTR)|(0<<CPOL)|(0<<CPHA)|(1<<SPR1)|(1<<SPR0);
107
        SPSR &= ~(1<<SPI2X);
108
 
109
    // Init Statemachine
110
        MM3.AXIS = MM3_X_AXIS;
111
        MM3.STATE = MM3_STATE_RESET;
112
 
113
        // Read calibration from EEprom
114
        MM3_calib.X_off = (int8_t)GetParamByte(PID_MM3_X_OFF);
115
        MM3_calib.Y_off = (int8_t)GetParamByte(PID_MM3_Y_OFF);
116
        MM3_calib.Z_off = (int8_t)GetParamByte(PID_MM3_Z_OFF);
117
        MM3_calib.X_range = (int16_t)GetParamWord(PID_MM3_X_RANGE);
118
        MM3_calib.Y_range = (int16_t)GetParamWord(PID_MM3_Y_RANGE);
119
        MM3_calib.Z_range = (int16_t)GetParamWord(PID_MM3_Z_RANGE);
120
 
741 killagreg 121
        MM3_Timeout = 0;
122
 
700 killagreg 123
        SREG = sreg;
124
}
125
 
126
 
127
/*********************************************/
128
/*  Get Data from MM3                        */
129
/*********************************************/
754 killagreg 130
void MM3_Update(void) // called every 102.4 µs by timer 0 ISR
700 killagreg 131
{
132
        switch (MM3.STATE)
133
        {
134
        case MM3_STATE_RESET:
781 killagreg 135
                #ifdef USE_WALTER_EXT // walthers board
136
                PORTC &= ~(1<<PORTC6);  // select slave
137
                PORTB |= (1<<PORTB2);   // PB2 to High, MM3 Reset
138
                #else
741 killagreg 139
                PORTC &= ~(1<<PORTC4);  // select slave
700 killagreg 140
                PORTC |= (1<<PORTC5);   // PC5 to High, MM3 Reset
781 killagreg 141
                #endif
700 killagreg 142
                MM3.STATE = MM3_STATE_START_TRANSFER;
143
                return;
144
 
145
        case MM3_STATE_START_TRANSFER:
781 killagreg 146
                #ifdef USE_WALTER_EXT // walthers board
147
                PORTB &= ~(1<<PORTB2);  // PB2 auf Low (was 102.4 µs at high level)
148
                #else
700 killagreg 149
                PORTC &= ~(1<<PORTC5);  // PC4 auf Low (was 102.4 µs at high level)
781 killagreg 150
                #endif
700 killagreg 151
                // write to SPDR triggers automatically the transfer MOSI MISO
152
                // MM3 Period, + AXIS code
741 killagreg 153
                switch(MM3.AXIS)
154
                {
155
                case MM3_X_AXIS:
156
                        SPDR = MM3_PERIOD_256 + MM3_X_AXIS;
157
                        break;
158
                case MM3_Y_AXIS:
159
                        SPDR = MM3_PERIOD_256 + MM3_Y_AXIS;
160
                        break;
161
                case MM3_Z_AXIS:
162
                        SPDR = MM3_PERIOD_256 + MM3_Z_AXIS;
163
                        break;
164
                default:
165
                        MM3.AXIS = MM3_X_AXIS;
166
                        MM3.STATE = MM3_STATE_RESET;
167
                        return;
168
                }
700 killagreg 169
 
170
                // DRDY line is not connected, therefore
171
                // wait before reading data back
172
                MM3.DRDY = SetDelay(8); // wait 8ms for data ready
173
                MM3.STATE = MM3_STATE_WAIT_DRDY;
174
                return;
175
 
176
        case MM3_STATE_WAIT_DRDY:
177
                if (CheckDelay(MM3.DRDY))
178
                {
179
                        // write something into SPDR to trigger data reading
180
                        SPDR = 0x00;
181
                        MM3.STATE = MM3_STATE_DRDY;
182
                }
183
                return;
184
        }
185
}
186
 
187
 
188
/*********************************************/
189
/*  Interrupt SPI transfer complete          */
190
/*********************************************/
191
ISR(SPI_STC_vect)
192
{
193
        static int8_t tmp;
194
        int16_t value;
195
 
196
        switch (MM3.STATE)
197
        {
198
        // 1st byte received
199
        case MM3_STATE_DRDY:
200
                tmp = SPDR;     // store 1st byte
201
                SPDR = 0x00;    // trigger transfer of 2nd byte
202
                MM3.STATE = MM3_STATE_BYTE2;
203
                return;
204
 
205
        case MM3_STATE_BYTE2:           // 2nd byte received
206
                value = (int16_t)tmp;   // combine the 1st and 2nd byte to a word
207
                value <<= 8;            // shift 1st byte to MSB-Position
208
                value |= (int16_t)SPDR; // add 2nd byte
209
 
210
                if(abs(value) < MAX_AXIS_VALUE)         // ignore spikes
211
                {
212
                        switch (MM3.AXIS)
213
                        {
214
                        case MM3_X_AXIS:
215
                                MM3.x_axis = value;
216
                                MM3.AXIS = MM3_Y_AXIS;
217
                                break;
218
                        case MM3_Y_AXIS:
219
                                MM3.y_axis = value;
220
                                MM3.AXIS = MM3_Z_AXIS;
221
                                break;
222
                        case MM3_Z_AXIS:
223
                                MM3.z_axis = value;
224
                                MM3.AXIS = MM3_X_AXIS;
225
                                break;
226
                        default:
227
                                MM3.AXIS = MM3_X_AXIS;
228
                                break;
229
                        }
230
                }
781 killagreg 231
                #ifdef USE_WALTER_EXT // walthers board
232
                PORTC |= (1<<PORTC6); // deselect slave
233
                #else
727 killagreg 234
                PORTC |= (1<<PORTC4); // deselect slave
781 killagreg 235
                #endif
700 killagreg 236
                MM3.STATE = MM3_STATE_RESET;
741 killagreg 237
                // Update timeout is called every 102.4 µs.
238
                // It takes 2 cycles to write a measurement data request for one axis and
239
                // at at least 8 ms / 102.4 µs = 79 cycles to read the requested data back.
240
                // I.e. 81 cycles * 102.4 µs = 8.3ms per axis.
241
                // The two function accessing the MM3 Data - MM3_Calibrate() and MM3_Heading() -
242
                // decremtent the MM3_Timeout every 100 ms.
243
                // incrementing the counter by 1 every 8.3 ms is sufficient to avoid a timeout.
244
                if ((MM3.x_axis != MM3.y_axis) || (MM3.x_axis != MM3.z_axis) || (MM3.y_axis != MM3.z_axis))
245
                {       // if all axis measurements give diffrent readings the data should be valid
246
                        if(MM3_Timeout < 20) MM3_Timeout++;
247
                }
248
                else // something is very strange here
249
                {
250
                        if(MM3_Timeout ) MM3_Timeout--;
251
                }
252
                return;
253
 
254
        default:
255
                return;
700 killagreg 256
        }
257
}
258
 
259
 
260
 
261
/*********************************************/
262
/*  Calibrate Compass                        */
263
/*********************************************/
726 killagreg 264
void MM3_Calibrate(void)
700 killagreg 265
{
266
        int16_t x_min = 0, x_max = 0, y_min = 0, y_max = 0, z_min = 0, z_max = 0;
267
        uint8_t measurement = 50, beeper = 0;
268
        uint16_t timer;
269
 
270
        GRN_ON;
271
        ROT_OFF;
272
 
273
        // get maximum and minimum reading of all axis
773 killagreg 274
        while (measurement)
700 killagreg 275
        {
276
                if (MM3.x_axis > x_max) x_max = MM3.x_axis;
277
                else if (MM3.x_axis < x_min) x_min = MM3.x_axis;
278
 
279
                if (MM3.y_axis > y_max) y_max = MM3.y_axis;
280
                else if (MM3.y_axis < y_min) y_min = MM3.y_axis;
281
 
282
                if (MM3.z_axis > z_max) z_max = MM3.z_axis;
283
                else if (MM3.z_axis < z_min) z_min = MM3.z_axis;
284
 
285
                if (!beeper)
286
                {
287
                        ROT_FLASH;
288
                        GRN_FLASH;
289
                        BeepTime = 50;
290
                        beeper = 50;
291
                }
292
                beeper--;
293
                // loop with period of 10 ms / 100 Hz
294
                timer = SetDelay(10);
295
                while(!CheckDelay(timer));
296
 
707 killagreg 297
                // If thrust is less than 100, stop calibration with a delay of 0.5 seconds
298
                if (PPM_in[ParamSet.ChannelAssignment[CH_THRUST]] < 100) measurement--;
700 killagreg 299
        }
773 killagreg 300
        // Rage of all axis
301
        MM3_calib.X_range = (x_max - x_min);
302
        MM3_calib.Y_range = (y_max - y_min);
303
        MM3_calib.Z_range = (z_max - z_min);
700 killagreg 304
 
773 killagreg 305
        // Offset of all axis
306
        MM3_calib.X_off = (x_max + x_min) / 2;
307
        MM3_calib.Y_off = (y_max + y_min) / 2;
308
        MM3_calib.Z_off = (z_max + z_min) / 2;
700 killagreg 309
 
773 killagreg 310
        // save to EEProm
311
        SetParamByte(PID_MM3_X_OFF,   (uint8_t)MM3_calib.X_off);
312
        SetParamByte(PID_MM3_Y_OFF,   (uint8_t)MM3_calib.Y_off);
313
        SetParamByte(PID_MM3_Z_OFF,   (uint8_t)MM3_calib.Z_off);
314
        SetParamWord(PID_MM3_X_RANGE, (uint16_t)MM3_calib.X_range);
315
        SetParamWord(PID_MM3_Y_RANGE, (uint16_t)MM3_calib.Y_range);
316
        SetParamWord(PID_MM3_Z_RANGE, (uint16_t)MM3_calib.Z_range);
317
 
700 killagreg 318
}
319
 
320
 
321
/*********************************************/
322
/*  Calculate north direction (heading)      */
323
/*********************************************/
726 killagreg 324
int16_t MM3_Heading(void)
700 killagreg 325
{
701 killagreg 326
        int32_t sin_pitch, cos_pitch, sin_roll, cos_roll, sin_yaw, cos_yaw;
700 killagreg 327
        int32_t  Hx, Hy, Hz, Hx_corr, Hy_corr;
328
        int16_t angle;
329
        uint16_t div_factor;
330
        int16_t heading;
331
 
741 killagreg 332
        if (MM3_Timeout)
333
        {
334
                // Offset correction and normalization (values of H are +/- 512)
335
                Hx = (((int32_t)(MM3.x_axis - MM3_calib.X_off)) * 1024) / (int32_t)MM3_calib.X_range;
336
                Hy = (((int32_t)(MM3.y_axis - MM3_calib.Y_off)) * 1024) / (int32_t)MM3_calib.Y_range;
337
                Hz = (((int32_t)(MM3.z_axis - MM3_calib.Z_off)) * 1024) / (int32_t)MM3_calib.Z_range;
700 killagreg 338
 
741 killagreg 339
                // Compensate the angle of the MM3-arrow to the head of the MK by a yaw rotation transformation
340
                // assuming the MM3 board is mounted parallel to the frame.
341
                // User Param 4 is used to define the positive angle from the MM3-arrow to the MK heading
342
                // in a top view counter clockwise direction.
343
                // North is in opposite direction of the small arrow on the MM3 board.
344
                // Therefore 180 deg must be added to that angle.
345
                angle = ((int16_t)ParamSet.UserParam4 + 180);
346
                // wrap angle to interval of 0°- 359°
347
                angle += 360;
348
                angle %= 360;
349
                sin_yaw = (int32_t)(c_sin_8192(angle));
350
                cos_yaw = (int32_t)(c_cos_8192(angle));
700 killagreg 351
 
741 killagreg 352
                Hx_corr = Hx;
353
                Hy_corr = Hy;
700 killagreg 354
 
741 killagreg 355
                // rotate
356
                Hx = (Hx_corr * cos_yaw - Hy_corr  * sin_yaw) / 8192;
357
                Hy = (Hx_corr * sin_yaw + Hy_corr  * cos_yaw) / 8192;
700 killagreg 358
 
359
 
741 killagreg 360
                // tilt compensation
700 killagreg 361
 
741 killagreg 362
                // calibration factor for transforming Gyro Integrals to angular degrees
363
                div_factor = (uint16_t)ParamSet.UserParam3 * 8;
700 killagreg 364
 
741 killagreg 365
                // calculate sinus cosinus of pitch and tilt angle
366
                angle = (IntegralPitch/div_factor);
367
                sin_pitch = (int32_t)(c_sin_8192(angle));
368
                cos_pitch = (int32_t)(c_cos_8192(angle));
700 killagreg 369
 
741 killagreg 370
                angle = (IntegralRoll/div_factor);
371
                sin_roll = (int32_t)(c_sin_8192(angle));
372
                cos_roll = (int32_t)(c_cos_8192(angle));
700 killagreg 373
 
741 killagreg 374
                Hx_corr = Hx * cos_pitch;
375
                Hx_corr -= Hz * sin_pitch;
376
                Hx_corr /= 8192;
700 killagreg 377
 
741 killagreg 378
                Hy_corr = Hy * cos_roll;
379
                Hy_corr += Hz * sin_roll;
380
                Hy_corr /= 8192;
700 killagreg 381
 
741 killagreg 382
                // calculate Heading
383
                heading = c_atan2(Hy_corr, Hx_corr);
700 killagreg 384
 
741 killagreg 385
                // atan returns angular range from -180 deg to 180 deg in counter clockwise notation
386
                // but the compass course is defined in a range from 0 deg to 360 deg clockwise notation.
387
                if (heading < 0) heading = -heading;
388
                else heading = 360 - heading;
389
        }
390
        else // MM3_Timeout = 0 i.e now new data from external board
391
        {
765 killagreg 392
                if(!BeepTime) BeepTime = 100; // make noise to signal the compass problem
741 killagreg 393
                heading = -1;
394
        }
700 killagreg 395
return heading;
396
}