Subversion Repositories NaviCtrl

Rev

Rev 743 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
242 killagreg 1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + www.MikroKopter.com
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
360 holgerb 7
// + Software Nutzungsbedingungen (english version: see below)
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
489 killagreg 10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool
360 holgerb 11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
242 killagreg 13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
360 holgerb 14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
489 killagreg 21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand
360 holgerb 22
// + des Mitverschuldens offen.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
30
// +  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.
242 killagreg 31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
360 holgerb 32
// + Software LICENSING TERMS
242 killagreg 33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
360 holgerb 34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
489 killagreg 35
// + 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
360 holgerb 36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
37
// + The Software may only be used with the Licensor's products.
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
40
// + agreement shall be the property of the Licensor.
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
43
// + The customer shall be responsible for taking reasonable precautions
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
53
// + #### END OF LICENSING TERMS ####
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
242 killagreg 55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
#include "91x_lib.h"
57
#include "compass.h"
58
#include "mk3mag.h"
253 killagreg 59
#include "ncmag.h"
292 killagreg 60
#include "spi_slave.h"
61
#include "mymath.h"
242 killagreg 62
#include "uart1.h"
254 killagreg 63
#include "fifo.h"
264 killagreg 64
#include "led.h"
292 killagreg 65
#include "main.h"
242 killagreg 66
 
254 killagreg 67
u8 CompassCalStateQueue[10];
68
fifo_t CompassCalcStateFiFo;
69
 
70
volatile s16vec_t MagVector;   // is written by mk3mag or ncmag implementation
489 killagreg 71
volatile s16vec_t AccVector;   // current acceleration vector of compass, not supported by any HW version
493 killagreg 72
volatile s16vec_t MagVectorHorizontal; // vector componenents in horizontal projection
394 killagreg 73
volatile s16 Compass_Heading;  // is written by mk3mag or ncmag implementation
254 killagreg 74
volatile u8  Compass_CalState; // is written by mk3mag or ncmag implementation
493 killagreg 75
 
330 holgerb 76
s32 EarthMagneticField = 100;
77
s32 EarthMagneticFieldFiltered = 100;
78
s32 EarthMagneticInclination = 0;
340 holgerb 79
s32 EarthMagneticInclinationFiltered = 0;
338 holgerb 80
s32 EarthMagneticInclinationTheoretic = 0;
330 holgerb 81
u8  ErrorDisturbedEarthMagnetField = 0;
342 holgerb 82
s16 EarthMagneticStrengthTheoretic = 0; // in mT (50 in Germany - 22 in Brazil)
487 holgerb 83
u32 CompassValueErrorCount = 0;
254 killagreg 84
 
253 killagreg 85
#define COMPASS_NONE    0
86
#define COMPASS_MK3MAG  1
87
#define COMPASS_NCMAG   2
254 killagreg 88
u8 Compass_Device = COMPASS_NONE;
489 killagreg 89
I2C_TypeDef* Compass_I2CPort = I2C1;
242 killagreg 90
 
91
void Compass_Init(void)
92
{
268 killagreg 93
        switch(Compass_Device)
264 killagreg 94
        {
268 killagreg 95
                case COMPASS_NONE:
96
                        UART1_PutString("\r\n Looking for compass");
743 holgerb 97
                        if(UART_VersionInfo.HWMajor > 20)
685 holgerb 98
                         {
99
                          if( NCMAG_Init() )    Compass_Device = COMPASS_NCMAG;
100
                         }
101
                         else
102
                         {
103
                          if( MK3MAG_Init() ) Compass_Device = COMPASS_MK3MAG;
104
                          else if( NCMAG_Init() )       Compass_Device = COMPASS_NCMAG;
105
                         }
268 killagreg 106
                        break;
311 killagreg 107
 
268 killagreg 108
                case COMPASS_NCMAG:
109
                        if( NCMAG_Init() )      Compass_Device = COMPASS_NCMAG;
311 killagreg 110
                        else                            Compass_Device = COMPASS_NONE;
268 killagreg 111
                        break;
112
 
113
                case COMPASS_MK3MAG:
489 killagreg 114
                        if( MK3MAG_Init() ) Compass_Device = COMPASS_MK3MAG;
115
                        else                            Compass_Device = COMPASS_NONE;
116
                        break;
117
 
268 killagreg 118
                default:
119
                        // nothing to do
120
                        break;
121
 
264 killagreg 122
        }
254 killagreg 123
        fifo_init(&CompassCalcStateFiFo, CompassCalStateQueue, sizeof(CompassCalStateQueue), NO_ITLine, NO_ITLine);
454 holgerb 124
 
125
        if(Compass_Device == COMPASS_MK3MAG) UART_VersionInfo.Flags |= NC_VERSION_FLAG_MK3MAG_PRESENT; else UART_VersionInfo.Flags &= ~NC_VERSION_FLAG_MK3MAG_PRESENT;
126
 
489 killagreg 127
        MagVector.X = 0;
128
        MagVector.Y = 0;
129
        MagVector.Z = 0;
130
        Compass_Heading = -1;
242 killagreg 131
}
132
 
133
 
489 killagreg 134
void Compass_Check(void)
135
{
136
        switch(Compass_Device)
137
        {
138
                case COMPASS_NCMAG:
139
                        NCMAG_CheckOrientation();
140
                        break;
141
 
142
                case COMPASS_MK3MAG:
143
                case COMPASS_NONE:
144
                default:
145
                        // nothing to do
146
                        break;
147
        }
148
}
149
 
292 killagreg 150
void Compass_CalcHeading(void)
242 killagreg 151
{
292 killagreg 152
        if((UART_VersionInfo.HardwareError[0] & NC_ERROR0_SPI_RX) || Compass_CalState)
242 killagreg 153
        {
292 killagreg 154
                Compass_Heading = -1;
242 killagreg 155
        }
292 killagreg 156
        else // fc attitude is avialable and no compass calibration active
157
        {
158
                // calculate attitude correction
330 holgerb 159
                s16 tmp;
292 killagreg 160
                s32 sinnick, cosnick, sinroll, cosroll;
311 killagreg 161
                tmp = FromFlightCtrl.AngleNick/10; // in deg
292 killagreg 162
                sinnick = (s32)c_sin_8192(tmp);
163
                cosnick = (s32)c_cos_8192(tmp);
311 killagreg 164
                tmp = FromFlightCtrl.AngleRoll/10; // in deg
292 killagreg 165
                sinroll = (s32)c_sin_8192(tmp);
166
                cosroll = (s32)c_cos_8192(tmp);
493 killagreg 167
                MagVectorHorizontal.X = (s16)((MagVector.Y * cosnick + MagVector.Z * sinnick)/8192L);
168
                MagVectorHorizontal.Y = (s16)((MagVector.X * cosroll - MagVector.Z * sinroll)/8192L);
292 killagreg 169
                // calculate heading
493 killagreg 170
                tmp = (s16)(c_atan2_546(MagVectorHorizontal.Y, MagVectorHorizontal.X)/546L);
292 killagreg 171
                if (tmp > 0) tmp = 360 - tmp;
172
                else tmp = -tmp;
173
                Compass_Heading = tmp;
174
        }
242 killagreg 175
}
176
 
330 holgerb 177
 
292 killagreg 178
void Compass_Update(void)
242 killagreg 179
{
489 killagreg 180
static s16vec_t old;
321 holgerb 181
static u32 check_value_counter = 0;
292 killagreg 182
        // check for new cal state
183
        Compass_UpdateCalState();
351 holgerb 184
        if(Compass_CalState) FC_is_Calibrated = 0;
292 killagreg 185
        // initiate new compass communication
254 killagreg 186
        switch(Compass_Device)
242 killagreg 187
        {
188
                case COMPASS_MK3MAG:
292 killagreg 189
                        MK3MAG_Update();
242 killagreg 190
                        break;
253 killagreg 191
                case COMPASS_NCMAG:
489 killagreg 192
                        NCMAG_Update(0);
193
                        break;
242 killagreg 194
                default:
311 killagreg 195
                        break;
242 killagreg 196
        }
321 holgerb 197
 
489 killagreg 198
        DebugOut.Analog[24] = MagVector.X;
199
        DebugOut.Analog[25] = MagVector.Y;
200
        DebugOut.Analog[26] = MagVector.Z;
321 holgerb 201
 
489 killagreg 202
    if(!((old.X == MagVector.X) || (old.Y == MagVector.Y) || (old.Z == MagVector.Z))) check_value_counter = 0; // Values are normaly changing
203
 
492 killagreg 204
        if(check_value_counter == 2000) // 2 seconds no change of the compass values
489 killagreg 205
        {
206
                UART1_PutString("\n\r Init Mag.-Sensor");
492 killagreg 207
                Compass_Init();
489 killagreg 208
        }
492 killagreg 209
 
210
        if(check_value_counter > 5000) // 5 seconds no change of the compass values
489 killagreg 211
        {
492 killagreg 212
                Compass_Heading = -1; // -> probably a compass-fault
489 killagreg 213
        }
214
        else check_value_counter++;
215
 
321 holgerb 216
        old.X = MagVector.X;
217
        old.Y = MagVector.Y;
218
        old.Z = MagVector.Z;
242 killagreg 219
}
254 killagreg 220
 
221
// put cal state into fifo
222
void Compass_SetCalState(u8 CalState)
223
{
311 killagreg 224
        fifo_put(&CompassCalcStateFiFo, CalState);
254 killagreg 225
}
226
 
227
// get cal state from fifo
228
void Compass_UpdateCalState()
229
{
311 killagreg 230
        fifo_get(&CompassCalcStateFiFo, (u8*)&Compass_CalState);
254 killagreg 231
}
232