0,0 → 1,202 |
/*#######################################################################################*/ |
/* !!! 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 s16 Compass_Heading; // is written by mk3mag or ncmag implementation |
volatile u8 Compass_CalState; // is written by mk3mag or ncmag implementation |
s16 Hx = 0, Hy = 0; |
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) |
|
#define COMPASS_NONE 0 |
#define COMPASS_MK3MAG 1 |
#define COMPASS_NCMAG 2 |
u8 Compass_Device = COMPASS_NONE; |
|
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; |
|
/* if(Version_HW > 11) |
{ |
if( NCMAG_Init() ) Compass_Device = COMPASS_NCMAG; |
else if( MK3MAG_Init()) Compass_Device = COMPASS_MK3MAG; |
} |
else |
{ |
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: |
default: |
// nothing to do |
break; |
|
} |
fifo_init(&CompassCalcStateFiFo, CompassCalStateQueue, sizeof(CompassCalStateQueue), NO_ITLine, NO_ITLine); |
} |
|
|
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 |
// a float implementation takes too long |
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); |
// tbd. compensation signs and oriantation has to be fixed |
Hx = (s16)((MagVector.Y * cosnick + MagVector.Z * sinnick)/8192L); |
Hy = (s16)((MagVector.X * cosroll - MagVector.Z * sinroll)/8192L); |
// calculate heading |
tmp = (s16)(c_atan2_546(Hy, Hx)/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(); |
DebugOut.Analog[24] = MagVector.X; |
DebugOut.Analog[25] = MagVector.Y; |
DebugOut.Analog[26] = MagVector.Z; |
break; |
case COMPASS_NCMAG: |
NCMAG_Update(); |
DebugOut.Analog[24] = MagRawVector.X; |
DebugOut.Analog[25] = MagRawVector.Y; |
DebugOut.Analog[26] = MagRawVector.Z; |
default: |
break; |
} |
if(!((old.X == MagVector.X) || (old.Y == MagVector.Y) || (old.Z == MagVector.Z))) check_value_counter = 0; // Values are normally changing |
|
if(check_value_counter > 5000) |
{ |
Compass_Heading = -1; // values didn't change for 5 seconds -> 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); |
} |
|