Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
1612 dongfang 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
3
// + Nur für den privaten Gebrauch
4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
7
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
17
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + eindeutig als Ursprung verlinkt werden
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
21
// + Benutzung auf eigene Gefahr
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
25
// + mit unserer Zustimmung zulässig
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
30
// + this list of conditions and the following disclaimer.
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
32
// +     from this software without specific prior written permission.
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
34
// +     for non-commercial use (directly or indirectly)
1868 - 35
// +     Commercial use (for example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
1612 dongfang 36
// +     with our written permission
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49
// +  POSSIBILITY OF SUCH DAMAGE.
50
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
51
#include <avr/io.h>
52
#include <stdlib.h>
53
#include <inttypes.h>
54
#include "timer0.h"
1775 - 55
#include "attitude.h"
1612 dongfang 56
#include "eeprom.h"
57
#include "mk3mag.h"
58
 
1801 - 59
// For the DebougOut alone.
60
#include "output.h"
61
 
1612 dongfang 62
uint8_t PWMTimeout = 12;
1969 - 63
ToMk3Mag_t toMk3Mag;
1612 dongfang 64
 
65
/*********************************************/
66
/*  Initialize Interface to MK3MAG Compass   */
67
/*********************************************/
1801 - 68
void MK3MAG_Init(void) {
69
        // Port PC4 connected to PWM output from compass module
70
        DDRC &= ~(1 << DDC4); // set as input
71
        PORTC |= (1 << PORTC4); // pull up  to increase PWM counter also if nothing is connected
1612 dongfang 72
 
1801 - 73
        PWMTimeout = 0;
1612 dongfang 74
 
1969 - 75
        toMk3Mag.CalState = 0;
76
        toMk3Mag.Orientation = 1;
1612 dongfang 77
}
78
 
79
/*********************************************/
80
/*  Get PWM from MK3MAG                      */
81
/*********************************************/
1801 - 82
void MK3MAG_Update(void) {// called every 102.4 us by timer 0 ISR
83
        static uint16_t PWMCount = 0;
1887 - 84
        static uint16_t beepDelay = 0;
1801 - 85
        static uint16_t debugCounter = 0;
86
        // The pulse width varies from 1ms (0°) to 36.99ms (359.9°)
87
        // in other words 100us/° with a +1ms offset.
88
        // The signal goes low for 65ms between pulses,
89
        // so the cycle time is 65mS + the pulse width.
90
        // pwm is high
1612 dongfang 91
 
1805 - 92
        if (debugCounter++ == 5000) {
1845 - 93
                // DebugOut.Digital[0] ^= DEBUG_MK3MAG;
1805 - 94
                debugCounter = 0;
95
        }
96
 
1801 - 97
        if (PINC & (1 << PINC4)) {
98
                // If PWM signal is high increment PWM high counter
99
                // This counter is incremented by a periode of 102.4us,
100
                // i.e. the resoluton of pwm coded heading is approx. 1 deg.
101
                PWMCount++;
102
                // pwm overflow?
103
                if (PWMCount > 400) {
104
                        if (PWMTimeout)
105
                                PWMTimeout--; // decrement timeout
106
                        compassHeading = -1; // unknown heading
107
                        PWMCount = 0; // reset PWM Counter
108
                }
109
        } else { // pwm is low
1805 - 110
                // ignore pwm values values of 0 and higher than 37 ms;
1801 - 111
                if ((PWMCount) && (PWMCount < 362)) { // 362 * 102.4us = 37.0688 ms
112
                        if (PWMCount < 10)
113
                                compassHeading = 0;
1805 - 114
                        else {
1801 - 115
                                compassHeading = ((uint32_t) (PWMCount - 10) * 1049L) / 1024; // correct timebase and offset
1845 - 116
                                //DebugOut.Digital[1] ^= DEBUG_MK3MAG; // correct signal recd.
1805 - 117
                        }
118
                        /*
119
                         compassHeading - compassCourse on a -180..179 range.
120
                         compassHeading 20 compassCourse 30 --> ((540 - 10)%360) - 180 = -10
121
                         compassHeading 30 compassCourse 20 --> ((540 + 10)%360) - 180 = 10
122
                         compassHeading 350 compassCourse 10 --> ((540 + 340)%360) - 180 = -20
123
                         compassHeading 10 compassCourse 350 --> ((540 - 340)%360) - 180 = 20
124
                         */
125
                        //compassOffCourse = ((540 + compassHeading - compassCourse) % 360) - 180;
1801 - 126
                        PWMTimeout = 12; // if 12 periodes long no valid PWM was detected the data are invalid
127
                        // 12 * 362 counts * 102.4 us
128
                }
129
                PWMCount = 0; // reset pwm counter
1805 - 130
        }
131
        if (!PWMTimeout) {
1887 - 132
                if (checkDelay(beepDelay)) {
133
                        if (!beepTime)
134
                                beepTime = 100; // make noise with 10Hz to signal the compass problem
135
                        beepDelay = setDelay(100);
1801 - 136
                }
1612 dongfang 137
        }
138
}
139