Subversion Repositories FlightCtrl

Rev

Rev 1868 | Rev 1969 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Copyright (c) 04.2007 Holger Buss
// + Nur für den privaten Gebrauch
// + www.MikroKopter.com
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
// + bzgl. der Nutzungsbedingungen aufzunehmen.
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
// + Verkauf von Luftbildaufnahmen, usw.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
// + eindeutig als Ursprung verlinkt werden
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
// + Benutzung auf eigene Gefahr
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
// + mit unserer Zustimmung zulässig
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
// + this list of conditions and the following disclaimer.
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
// +     from this software without specific prior written permission.
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
// +     for non-commercial use (directly or indirectly)
// +     Commercial use (for example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
// +     with our written permission
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
// +     clearly linked as origin
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// +  POSSIBILITY OF SUCH DAMAGE.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <avr/io.h>
#include <stdlib.h>
#include <inttypes.h>
#include "timer0.h"
#include "attitude.h"
#include "eeprom.h"
#include "mk3mag.h"

// For the DebougOut alone.
#include "output.h"

uint8_t PWMTimeout = 12;
ToMk3Mag_t ToMk3Mag;

/*********************************************/
/*  Initialize Interface to MK3MAG Compass   */
/*********************************************/
void MK3MAG_Init(void) {
        // Port PC4 connected to PWM output from compass module
        DDRC &= ~(1 << DDC4); // set as input
        PORTC |= (1 << PORTC4); // pull up  to increase PWM counter also if nothing is connected

        PWMTimeout = 0;

        ToMk3Mag.CalState = 0;
        ToMk3Mag.Orientation = 1;
}

/*********************************************/
/*  Get PWM from MK3MAG                      */
/*********************************************/
void MK3MAG_Update(void) {// called every 102.4 us by timer 0 ISR
        static uint16_t PWMCount = 0;
        static uint16_t beepDelay = 0;
        static uint16_t debugCounter = 0;
        // The pulse width varies from 1ms (0°) to 36.99ms (359.9°)
        // in other words 100us/° with a +1ms offset.
        // The signal goes low for 65ms between pulses,
        // so the cycle time is 65mS + the pulse width.
        // pwm is high

        if (debugCounter++ == 5000) {
                // DebugOut.Digital[0] ^= DEBUG_MK3MAG;
                debugCounter = 0;
        }

        if (PINC & (1 << PINC4)) {
                // If PWM signal is high increment PWM high counter
                // This counter is incremented by a periode of 102.4us,
                // i.e. the resoluton of pwm coded heading is approx. 1 deg.
                PWMCount++;
                // pwm overflow?
                if (PWMCount > 400) {
                        if (PWMTimeout)
                                PWMTimeout--; // decrement timeout
                        compassHeading = -1; // unknown heading
                        PWMCount = 0; // reset PWM Counter
                }
        } else { // pwm is low
                // ignore pwm values values of 0 and higher than 37 ms;
                if ((PWMCount) && (PWMCount < 362)) { // 362 * 102.4us = 37.0688 ms
                        if (PWMCount < 10)
                                compassHeading = 0;
                        else {
                                compassHeading = ((uint32_t) (PWMCount - 10) * 1049L) / 1024; // correct timebase and offset
                                //DebugOut.Digital[1] ^= DEBUG_MK3MAG; // correct signal recd.
                        }
                        /*
                         compassHeading - compassCourse on a -180..179 range.
                         compassHeading 20 compassCourse 30 --> ((540 - 10)%360) - 180 = -10
                         compassHeading 30 compassCourse 20 --> ((540 + 10)%360) - 180 = 10
                         compassHeading 350 compassCourse 10 --> ((540 + 340)%360) - 180 = -20
                         compassHeading 10 compassCourse 350 --> ((540 - 340)%360) - 180 = 20
                         */

                        //compassOffCourse = ((540 + compassHeading - compassCourse) % 360) - 180;
                        PWMTimeout = 12; // if 12 periodes long no valid PWM was detected the data are invalid
                        // 12 * 362 counts * 102.4 us
                }
                PWMCount = 0; // reset pwm counter
        }
        if (!PWMTimeout) {
                if (checkDelay(beepDelay)) {
                        if (!beepTime)
                                beepTime = 100; // make noise with 10Hz to signal the compass problem
                        beepDelay = setDelay(100);
                }
        }
}