Subversion Repositories FlightCtrl

Rev

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

Rev 1775 Rev 1801
Line 50... Line 50...
50
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
50
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
51
#include <avr/io.h>
51
#include <avr/io.h>
52
#include <stdlib.h>
52
#include <stdlib.h>
53
#include <inttypes.h>
53
#include <inttypes.h>
54
#include "timer0.h"
54
#include "timer0.h"
55
// #include "rc.h"
-
 
56
#include "attitude.h"
55
#include "attitude.h"
57
#include "eeprom.h"
56
#include "eeprom.h"
58
#include "mk3mag.h"
57
#include "mk3mag.h"
Line -... Line 58...
-
 
58
 
-
 
59
// For the DebougOut alone.
-
 
60
#include "output.h"
59
 
61
 
60
uint8_t PWMTimeout = 12;
62
uint8_t PWMTimeout = 12;
Line 61... Line -...
61
ToMk3Mag_t ToMk3Mag;
-
 
62
 
63
ToMk3Mag_t ToMk3Mag;
63
 
64
 
64
/*********************************************/
65
/*********************************************/
65
/*  Initialize Interface to MK3MAG Compass   */
66
/*  Initialize Interface to MK3MAG Compass   */
66
/*********************************************/
-
 
67
void MK3MAG_Init(void)
67
/*********************************************/
68
{
68
void MK3MAG_Init(void) {
69
  // Port PC4 connected to PWM output from compass module
69
        // Port PC4 connected to PWM output from compass module
Line 70... Line 70...
70
  DDRC &= ~(1<<DDC4); // set as input
70
        DDRC &= ~(1 << DDC4); // set as input
Line 71... Line 71...
71
  PORTC |= (1<<PORTC4); // pull up  to increase PWM counter also if nothing is connected
71
        PORTC |= (1 << PORTC4); // pull up  to increase PWM counter also if nothing is connected
72
 
72
 
73
  PWMTimeout = 0;
73
        PWMTimeout = 0;
Line 74... Line -...
74
 
-
 
75
  ToMk3Mag.CalState = 0;
74
 
76
  ToMk3Mag.Orientation = 1;
75
        ToMk3Mag.CalState = 0;
77
}
76
        ToMk3Mag.Orientation = 1;
78
 
77
}
79
 
-
 
80
/*********************************************/
78
 
81
/*  Get PWM from MK3MAG                      */
79
/*********************************************/
-
 
80
/*  Get PWM from MK3MAG                      */
82
/*********************************************/
81
/*********************************************/
83
void MK3MAG_Update(void) // called every 102.4 us by timer 0 ISR
82
void MK3MAG_Update(void) {// called every 102.4 us by timer 0 ISR
84
{
83
        static uint16_t PWMCount = 0;
85
  static uint16_t PWMCount = 0;
84
        static uint16_t BeepDelay = 0;
86
  static uint16_t BeepDelay = 0;
85
        static uint16_t debugCounter = 0;
87
  // The pulse width varies from 1ms (0°) to 36.99ms (359.9°)
86
        // The pulse width varies from 1ms (0°) to 36.99ms (359.9°)
88
  // in other words 100us/° with a +1ms offset.
87
        // in other words 100us/° with a +1ms offset.
89
  // The signal goes low for 65ms between pulses,
88
        // The signal goes low for 65ms between pulses,
90
  // so the cycle time is 65mS + the pulse width.
89
        // so the cycle time is 65mS + the pulse width.
91
 
90
 
92
  // pwm is high
91
        // pwm is high
93
 
92
 
94
  if(PINC & (1<<PINC4))
93
        if (PINC & (1 << PINC4)) {
95
    {   // If PWM signal is high increment PWM high counter
94
                // If PWM signal is high increment PWM high counter
96
      // This counter is incremented by a periode of 102.4us,
95
                // This counter is incremented by a periode of 102.4us,
97
      // i.e. the resoluton of pwm coded heading is approx. 1 deg.
96
                // i.e. the resoluton of pwm coded heading is approx. 1 deg.
98
      PWMCount++;
97
                PWMCount++;
99
      // pwm overflow?
98
                // pwm overflow?
100
      if (PWMCount > 400)
99
                if (PWMCount > 400) {
101
        {
-
 
102
          if(PWMTimeout) PWMTimeout--; // decrement timeout
-
 
103
          compassHeading = -1; // unknown heading
100
                        if (PWMTimeout)
104
          PWMCount = 0; // reset PWM Counter
101
                                PWMTimeout--; // decrement timeout
105
        }
102
                        compassHeading = -1; // unknown heading
106
 
103
                        PWMCount = 0; // reset PWM Counter
107
    }
104
                }
-
 
105
        } else { // pwm is low
108
  else // pwm is low
106
         // ignore pwm values values of 0 and higher than 37 ms;
109
    {   // ignore pwm values values of 0 and higher than 37 ms;
107
                if ((PWMCount) && (PWMCount < 362)) { // 362 * 102.4us = 37.0688 ms
110
      if((PWMCount) && (PWMCount < 362)) // 362 * 102.4us = 37.0688 ms
108
                        if (PWMCount < 10)
111
        {
109
                                compassHeading = 0;
112
          if(PWMCount <10) compassHeading = 0;
110
                        else
113
          else compassHeading = ((uint32_t)(PWMCount - 10) * 1049L)/1024; // correct timebase and offset
111
                                compassHeading = ((uint32_t) (PWMCount - 10) * 1049L) / 1024; // correct timebase and offset
114
          compassOffCourse = ((540 + compassHeading - compassCourse) % 360) - 180;
112
                        compassOffCourse = ((540 + compassHeading - compassCourse) % 360) - 180;
-
 
113
                        PWMTimeout = 12; // if 12 periodes long no valid PWM was detected the data are invalid
115
          PWMTimeout = 12; // if 12 periodes long no valid PWM was detected the data are invalid
114
                        // 12 * 362 counts * 102.4 us
116
          // 12 * 362 counts * 102.4 us
-
 
117
        }
115
                }
118
      PWMCount = 0; // reset pwm counter
116
                PWMCount = 0; // reset pwm counter
119
    }
117
                DebugOut.Digital[0] ^= DEBUG_MK3MAG;
120
  if(!PWMTimeout)
118
                DebugOut.Digital[1] &= ~DEBUG_MK3MAG;
-
 
119
        } if (!PWMTimeout) {
-
 
120
                if (CheckDelay(BeepDelay)) {
121
    {
121
                        if (!BeepTime)
122
      if(CheckDelay(BeepDelay))
-
 
123
        {
122
                                BeepTime = 100; // make noise with 10Hz to signal the compass problem
Line 124... Line -...
124
          if(!BeepTime) BeepTime = 100; // make noise with 10Hz to signal the compass problem
-
 
125
          BeepDelay = SetDelay(100);
-