Subversion Repositories FlightCtrl

Rev

Rev 2051 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1612 dongfang 1
#include <avr/io.h>
2
#include <stdlib.h>
3
#include <inttypes.h>
4
#include "timer0.h"
2052 - 5
#include "output.h"
1612 dongfang 6
#include "eeprom.h"
7
#include "mk3mag.h"
8
 
9
uint8_t PWMTimeout = 12;
2051 - 10
// This will updated in interrupt handler. Should not be processed by main, other than just atomic copy.
11
volatile uint16_t volatileMagneticHeading;
1969 - 12
ToMk3Mag_t toMk3Mag;
1612 dongfang 13
 
14
/*********************************************/
15
/*  Initialize Interface to MK3MAG Compass   */
16
/*********************************************/
2039 - 17
void MK3MAG_init(void) {
1801 - 18
        // Port PC4 connected to PWM output from compass module
19
        DDRC &= ~(1 << DDC4); // set as input
20
        PORTC |= (1 << PORTC4); // pull up  to increase PWM counter also if nothing is connected
21
        PWMTimeout = 0;
2039 - 22
        toMk3Mag.calState = 0;
23
        toMk3Mag.orientation = 1;
1612 dongfang 24
}
25
 
26
/*********************************************/
27
/*  Get PWM from MK3MAG                      */
28
/*********************************************/
2039 - 29
void MK3MAG_periodicTask(void) {// called every 102.4 us by timer 0 ISR
1801 - 30
        static uint16_t PWMCount = 0;
1887 - 31
        static uint16_t beepDelay = 0;
2041 - 32
        // static uint16_t debugCounter = 0;
1801 - 33
        // The pulse width varies from 1ms (0°) to 36.99ms (359.9°)
34
        // in other words 100us/° with a +1ms offset.
35
        // The signal goes low for 65ms between pulses,
36
        // so the cycle time is 65mS + the pulse width.
37
        // pwm is high
1612 dongfang 38
 
2041 - 39
        // if (debugCounter++ == 5000) {
40
    // DebugOut.Digital[0] ^= DEBUG_MK3MAG;
41
        // debugCounter = 0;
42
        // }
1805 - 43
 
1801 - 44
        if (PINC & (1 << PINC4)) {
45
                // If PWM signal is high increment PWM high counter
46
                // This counter is incremented by a periode of 102.4us,
47
                // i.e. the resoluton of pwm coded heading is approx. 1 deg.
48
                PWMCount++;
49
                // pwm overflow?
50
                if (PWMCount > 400) {
51
                        if (PWMTimeout)
52
                                PWMTimeout--; // decrement timeout
2051 - 53
                        volatileMagneticHeading = -1; // unknown heading
1801 - 54
                        PWMCount = 0; // reset PWM Counter
55
                }
56
        } else { // pwm is low
1805 - 57
                // ignore pwm values values of 0 and higher than 37 ms;
1801 - 58
                if ((PWMCount) && (PWMCount < 362)) { // 362 * 102.4us = 37.0688 ms
59
                        if (PWMCount < 10)
2051 - 60
                          volatileMagneticHeading = 0;
1805 - 61
                        else {
2051 - 62
                          volatileMagneticHeading = ((uint32_t) (PWMCount - 10) * 1049L) / 1024; // correct timebase and offset
1845 - 63
                                //DebugOut.Digital[1] ^= DEBUG_MK3MAG; // correct signal recd.
1805 - 64
                        }
65
                        /*
66
                         compassHeading - compassCourse on a -180..179 range.
67
                         compassHeading 20 compassCourse 30 --> ((540 - 10)%360) - 180 = -10
68
                         compassHeading 30 compassCourse 20 --> ((540 + 10)%360) - 180 = 10
69
                         compassHeading 350 compassCourse 10 --> ((540 + 340)%360) - 180 = -20
70
                         compassHeading 10 compassCourse 350 --> ((540 - 340)%360) - 180 = 20
71
                         */
72
                        //compassOffCourse = ((540 + compassHeading - compassCourse) % 360) - 180;
1801 - 73
                        PWMTimeout = 12; // if 12 periodes long no valid PWM was detected the data are invalid
74
                        // 12 * 362 counts * 102.4 us
75
                }
76
                PWMCount = 0; // reset pwm counter
1805 - 77
        }
2041 - 78
 
1805 - 79
        if (!PWMTimeout) {
1887 - 80
                if (checkDelay(beepDelay)) {
81
                        if (!beepTime)
2044 - 82
                                beep(50); // make noise with 10Hz to signal the compass problem
1887 - 83
                        beepDelay = setDelay(100);
1801 - 84
                }
1612 dongfang 85
        }
86
}
87