Subversion Repositories FlightCtrl

Rev

Rev 2039 | Rev 2044 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2039 Rev 2041
Line 78... Line 78...
78
/*  Get PWM from MK3MAG                      */
78
/*  Get PWM from MK3MAG                      */
79
/*********************************************/
79
/*********************************************/
80
void MK3MAG_periodicTask(void) {// called every 102.4 us by timer 0 ISR
80
void MK3MAG_periodicTask(void) {// called every 102.4 us by timer 0 ISR
81
        static uint16_t PWMCount = 0;
81
        static uint16_t PWMCount = 0;
82
        static uint16_t beepDelay = 0;
82
        static uint16_t beepDelay = 0;
83
        static uint16_t debugCounter = 0;
83
        // static uint16_t debugCounter = 0;
84
        // The pulse width varies from 1ms (0°) to 36.99ms (359.9°)
84
        // The pulse width varies from 1ms (0°) to 36.99ms (359.9°)
85
        // in other words 100us/° with a +1ms offset.
85
        // in other words 100us/° with a +1ms offset.
86
        // The signal goes low for 65ms between pulses,
86
        // The signal goes low for 65ms between pulses,
87
        // so the cycle time is 65mS + the pulse width.
87
        // so the cycle time is 65mS + the pulse width.
88
        // pwm is high
88
        // pwm is high
Line 89... Line 89...
89
 
89
 
90
        if (debugCounter++ == 5000) {
90
        // if (debugCounter++ == 5000) {
91
                // DebugOut.Digital[0] ^= DEBUG_MK3MAG;
91
    // DebugOut.Digital[0] ^= DEBUG_MK3MAG;
92
                debugCounter = 0;
92
        // debugCounter = 0;
Line 93... Line 93...
93
        }
93
        // }
94
 
94
 
95
        if (PINC & (1 << PINC4)) {
95
        if (PINC & (1 << PINC4)) {
96
                // If PWM signal is high increment PWM high counter
96
                // If PWM signal is high increment PWM high counter
97
                // This counter is incremented by a periode of 102.4us,
97
                // This counter is incremented by a periode of 102.4us,
98
                // i.e. the resoluton of pwm coded heading is approx. 1 deg.
98
                // i.e. the resoluton of pwm coded heading is approx. 1 deg.
99
                PWMCount++;
99
                PWMCount++;
100
                // pwm overflow?
100
                // pwm overflow?
101
                if (PWMCount > 400) {
101
                if (PWMCount > 400) {
102
                        if (PWMTimeout)
102
                        if (PWMTimeout)
103
                                PWMTimeout--; // decrement timeout
103
                                PWMTimeout--; // decrement timeout
104
                        compassHeading = -1; // unknown heading
104
                        magneticHeading = -1; // unknown heading
105
                        PWMCount = 0; // reset PWM Counter
105
                        PWMCount = 0; // reset PWM Counter
106
                }
106
                }
107
        } else { // pwm is low
107
        } else { // pwm is low
108
                // ignore pwm values values of 0 and higher than 37 ms;
108
                // ignore pwm values values of 0 and higher than 37 ms;
109
                if ((PWMCount) && (PWMCount < 362)) { // 362 * 102.4us = 37.0688 ms
109
                if ((PWMCount) && (PWMCount < 362)) { // 362 * 102.4us = 37.0688 ms
110
                        if (PWMCount < 10)
110
                        if (PWMCount < 10)
111
                                compassHeading = 0;
111
                                magneticHeading = 0;
112
                        else {
112
                        else {
113
                                compassHeading = ((uint32_t) (PWMCount - 10) * 1049L) / 1024; // correct timebase and offset
113
                          magneticHeading = ((uint32_t) (PWMCount - 10) * 1049L) / 1024; // correct timebase and offset
114
                                //DebugOut.Digital[1] ^= DEBUG_MK3MAG; // correct signal recd.
114
                                //DebugOut.Digital[1] ^= DEBUG_MK3MAG; // correct signal recd.
115
                        }
115
                        }
116
                        /*
116
                        /*
Line 124... Line 124...
124
                        PWMTimeout = 12; // if 12 periodes long no valid PWM was detected the data are invalid
124
                        PWMTimeout = 12; // if 12 periodes long no valid PWM was detected the data are invalid
125
                        // 12 * 362 counts * 102.4 us
125
                        // 12 * 362 counts * 102.4 us
126
                }
126
                }
127
                PWMCount = 0; // reset pwm counter
127
                PWMCount = 0; // reset pwm counter
128
        }
128
        }
-
 
129
 
129
        if (!PWMTimeout) {
130
        if (!PWMTimeout) {
130
                if (checkDelay(beepDelay)) {
131
                if (checkDelay(beepDelay)) {
131
                        if (!beepTime)
132
                        if (!beepTime)
132
                                beepTime = 100; // make noise with 10Hz to signal the compass problem
133
                                beep(100); // make noise with 10Hz to signal the compass problem
133
                        beepDelay = setDelay(100);
134
                        beepDelay = setDelay(100);
134
                }
135
                }
135
        }
136
        }
136
}
137
}