Subversion Repositories NaviCtrl

Compare Revisions

Ignore whitespace Rev 505 → Rev 506

/tags/V2.04a/compass.c
0,0 → 1,225
/*#######################################################################################*/
/* !!! THIS IS NOT FREE SOFTWARE !!! */
/*#######################################################################################*/
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + www.MikroKopter.com
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Software Nutzungsbedingungen (english version: see below)
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand
// + des Mitverschuldens offen.
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
// + Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Software LICENSING TERMS
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
// + The Software may only be used with the Licensor's products.
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
// + agreement shall be the property of the Licensor.
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
// + features that can be used to identify the program may not be altered or defaced by the customer.
// + The customer shall be responsible for taking reasonable precautions
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
// + #### END OF LICENSING TERMS ####
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include "91x_lib.h"
#include "compass.h"
#include "mk3mag.h"
#include "ncmag.h"
#include "spi_slave.h"
#include "mymath.h"
#include "uart1.h"
#include "fifo.h"
#include "led.h"
#include "main.h"
 
u8 CompassCalStateQueue[10];
fifo_t CompassCalcStateFiFo;
 
volatile s16vec_t MagVector; // is written by mk3mag or ncmag implementation
volatile s16vec_t AccVector; // current acceleration vector of compass, not supported by any HW version
volatile s16vec_t MagVectorHorizontal; // vector componenents in horizontal projection
volatile s16 Compass_Heading; // is written by mk3mag or ncmag implementation
volatile u8 Compass_CalState; // is written by mk3mag or ncmag implementation
 
s32 EarthMagneticField = 100;
s32 EarthMagneticFieldFiltered = 100;
s32 EarthMagneticInclination = 0;
s32 EarthMagneticInclinationFiltered = 0;
s32 EarthMagneticInclinationTheoretic = 0;
u8 ErrorDisturbedEarthMagnetField = 0;
s16 EarthMagneticStrengthTheoretic = 0; // in mT (50 in Germany - 22 in Brazil)
u32 CompassValueErrorCount = 0;
 
#define COMPASS_NONE 0
#define COMPASS_MK3MAG 1
#define COMPASS_NCMAG 2
u8 Compass_Device = COMPASS_NONE;
I2C_TypeDef* Compass_I2CPort = I2C1;
 
void Compass_Init(void)
{
switch(Compass_Device)
{
case COMPASS_NONE:
UART1_PutString("\r\n Looking for compass");
if( MK3MAG_Init() ) Compass_Device = COMPASS_MK3MAG;
else if( NCMAG_Init() ) Compass_Device = COMPASS_NCMAG;
break;
 
case COMPASS_NCMAG:
if( NCMAG_Init() ) Compass_Device = COMPASS_NCMAG;
else Compass_Device = COMPASS_NONE;
break;
 
case COMPASS_MK3MAG:
if( MK3MAG_Init() ) Compass_Device = COMPASS_MK3MAG;
else Compass_Device = COMPASS_NONE;
break;
 
default:
// nothing to do
break;
 
}
fifo_init(&CompassCalcStateFiFo, CompassCalStateQueue, sizeof(CompassCalStateQueue), NO_ITLine, NO_ITLine);
 
if(Compass_Device == COMPASS_MK3MAG) UART_VersionInfo.Flags |= NC_VERSION_FLAG_MK3MAG_PRESENT; else UART_VersionInfo.Flags &= ~NC_VERSION_FLAG_MK3MAG_PRESENT;
 
MagVector.X = 0;
MagVector.Y = 0;
MagVector.Z = 0;
Compass_Heading = -1;
}
 
 
void Compass_Check(void)
{
switch(Compass_Device)
{
case COMPASS_NCMAG:
NCMAG_CheckOrientation();
break;
 
case COMPASS_MK3MAG:
case COMPASS_NONE:
default:
// nothing to do
break;
}
}
 
void Compass_CalcHeading(void)
{
if((UART_VersionInfo.HardwareError[0] & NC_ERROR0_SPI_RX) || Compass_CalState)
{
Compass_Heading = -1;
}
else // fc attitude is avialable and no compass calibration active
{
// calculate attitude correction
s16 tmp;
s32 sinnick, cosnick, sinroll, cosroll;
tmp = FromFlightCtrl.AngleNick/10; // in deg
sinnick = (s32)c_sin_8192(tmp);
cosnick = (s32)c_cos_8192(tmp);
tmp = FromFlightCtrl.AngleRoll/10; // in deg
sinroll = (s32)c_sin_8192(tmp);
cosroll = (s32)c_cos_8192(tmp);
MagVectorHorizontal.X = (s16)((MagVector.Y * cosnick + MagVector.Z * sinnick)/8192L);
MagVectorHorizontal.Y = (s16)((MagVector.X * cosroll - MagVector.Z * sinroll)/8192L);
// calculate heading
tmp = (s16)(c_atan2_546(MagVectorHorizontal.Y, MagVectorHorizontal.X)/546L);
if (tmp > 0) tmp = 360 - tmp;
else tmp = -tmp;
Compass_Heading = tmp;
}
}
 
 
void Compass_Update(void)
{
static s16vec_t old;
static u32 check_value_counter = 0;
// check for new cal state
Compass_UpdateCalState();
if(Compass_CalState) FC_is_Calibrated = 0;
// initiate new compass communication
switch(Compass_Device)
{
case COMPASS_MK3MAG:
MK3MAG_Update();
break;
case COMPASS_NCMAG:
NCMAG_Update(0);
break;
default:
break;
}
 
DebugOut.Analog[24] = MagVector.X;
DebugOut.Analog[25] = MagVector.Y;
DebugOut.Analog[26] = MagVector.Z;
 
if(!((old.X == MagVector.X) || (old.Y == MagVector.Y) || (old.Z == MagVector.Z))) check_value_counter = 0; // Values are normaly changing
 
if(check_value_counter == 2000) // 2 seconds no change of the compass values
{
UART1_PutString("\n\r Init Mag.-Sensor");
Compass_Init();
}
if(check_value_counter > 5000) // 5 seconds no change of the compass values
{
Compass_Heading = -1; // -> probably a compass-fault
}
else check_value_counter++;
 
old.X = MagVector.X;
old.Y = MagVector.Y;
old.Z = MagVector.Z;
}
 
// put cal state into fifo
void Compass_SetCalState(u8 CalState)
{
fifo_put(&CompassCalcStateFiFo, CalState);
}
 
// get cal state from fifo
void Compass_UpdateCalState()
{
fifo_get(&CompassCalcStateFiFo, (u8*)&Compass_CalState);
}