Subversion Repositories NaviCtrl

Rev

Rev 351 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 351 Rev 360
1
/*#######################################################################################*/
1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2010 Ingo Busker, Holger Buss
-
 
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
-
 
7
// + FOR NON COMMERCIAL USE ONLY
-
 
8
// + www.MikroKopter.com
5
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
7
// + Software Nutzungsbedingungen (english version: see below)
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
15
// + Verkauf von Luftbildaufnahmen, usw.
-
 
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
22
// + eindeutig als Ursprung verlinkt werden
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
25
// + Benutzung auf eigene Gefahr
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.
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + mit unserer Zustimmung zulässig
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.
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// + Software LICENSING TERMS
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
34
// + this list of conditions and the following disclaimer.
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 
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
36
// +     from this software without specific prior written permission.
37
// + The Software may only be used with the Licensor's products.
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
38
// +     for non-commercial use (directly or indirectly)
-
 
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
40
// +     with our written permission
40
// + agreement shall be the property of the Licensor.
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
42
// +     clearly linked as origin
-
 
43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
44
//
-
 
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43
// + The customer shall be responsible for taking reasonable precautions
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
55
// +  POSSIBILITY OF SUCH DAMAGE.
53
// + #### END OF LICENSING TERMS ####
-
 
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
#include "91x_lib.h"
56
#include "91x_lib.h"
58
#include "compass.h"
57
#include "compass.h"
59
#include "mk3mag.h"
58
#include "mk3mag.h"
60
#include "ncmag.h"
59
#include "ncmag.h"
61
#include "spi_slave.h"
60
#include "spi_slave.h"
62
#include "mymath.h"
61
#include "mymath.h"
63
#include "uart1.h"
62
#include "uart1.h"
64
#include "fifo.h"
63
#include "fifo.h"
65
#include "led.h"
64
#include "led.h"
66
#include "main.h"
65
#include "main.h"
67
 
66
 
68
u8 CompassCalStateQueue[10];
67
u8 CompassCalStateQueue[10];
69
fifo_t CompassCalcStateFiFo;
68
fifo_t CompassCalcStateFiFo;
70
 
69
 
71
volatile s16vec_t MagVector;   // is written by mk3mag or ncmag implementation
70
volatile s16vec_t MagVector;   // is written by mk3mag or ncmag implementation
72
volatile s16 Compass_Heading;   // is written by mk3mag or ncmag implementation
71
volatile s16 Compass_Heading;   // is written by mk3mag or ncmag implementation
73
volatile u8  Compass_CalState; // is written by mk3mag or ncmag implementation
72
volatile u8  Compass_CalState; // is written by mk3mag or ncmag implementation
74
s16 Hx = 0, Hy = 0;
73
s16 Hx = 0, Hy = 0;
75
s32 EarthMagneticField = 100;
74
s32 EarthMagneticField = 100;
76
s32 EarthMagneticFieldFiltered = 100;
75
s32 EarthMagneticFieldFiltered = 100;
77
s32 EarthMagneticInclination = 0;
76
s32 EarthMagneticInclination = 0;
78
s32 EarthMagneticInclinationFiltered = 0;
77
s32 EarthMagneticInclinationFiltered = 0;
79
s32 EarthMagneticInclinationTheoretic = 0;
78
s32 EarthMagneticInclinationTheoretic = 0;
80
u8  ErrorDisturbedEarthMagnetField = 0;
79
u8  ErrorDisturbedEarthMagnetField = 0;
81
s16 EarthMagneticStrengthTheoretic = 0; // in mT (50 in Germany - 22 in Brazil)
80
s16 EarthMagneticStrengthTheoretic = 0; // in mT (50 in Germany - 22 in Brazil)
82
 
81
 
83
#define COMPASS_NONE    0
82
#define COMPASS_NONE    0
84
#define COMPASS_MK3MAG  1
83
#define COMPASS_MK3MAG  1
85
#define COMPASS_NCMAG   2
84
#define COMPASS_NCMAG   2
86
u8 Compass_Device = COMPASS_NONE;
85
u8 Compass_Device = COMPASS_NONE;
87
 
86
 
88
void Compass_Init(void)
87
void Compass_Init(void)
89
{
88
{
90
        switch(Compass_Device)
89
        switch(Compass_Device)
91
        {
90
        {
92
                case COMPASS_NONE:
91
                case COMPASS_NONE:
93
                        UART1_PutString("\r\n Looking for compass");
92
                        UART1_PutString("\r\n Looking for compass");
94
                        if(     MK3MAG_Init() ) Compass_Device = COMPASS_MK3MAG;
93
                        if(     MK3MAG_Init() ) Compass_Device = COMPASS_MK3MAG;
95
                        else if( NCMAG_Init() ) Compass_Device = COMPASS_NCMAG;
94
                        else if( NCMAG_Init() ) Compass_Device = COMPASS_NCMAG;
96
 
95
 
97
/*                      if(Version_HW > 11)
96
/*                      if(Version_HW > 11)
98
                        {
97
                        {
99
                                if(      NCMAG_Init() ) Compass_Device = COMPASS_NCMAG;
98
                                if(      NCMAG_Init() ) Compass_Device = COMPASS_NCMAG;
100
                                else if( MK3MAG_Init()) Compass_Device = COMPASS_MK3MAG;
99
                                else if( MK3MAG_Init()) Compass_Device = COMPASS_MK3MAG;
101
                        }
100
                        }
102
                        else
101
                        else
103
                        {
102
                        {
104
                                if(     MK3MAG_Init() ) Compass_Device = COMPASS_MK3MAG;
103
                                if(     MK3MAG_Init() ) Compass_Device = COMPASS_MK3MAG;
105
                                else if( NCMAG_Init() ) Compass_Device = COMPASS_NCMAG;
104
                                else if( NCMAG_Init() ) Compass_Device = COMPASS_NCMAG;
106
                        }
105
                        }
107
*/
106
*/
108
                        break;
107
                        break;
109
 
108
 
110
                case COMPASS_NCMAG:
109
                case COMPASS_NCMAG:
111
                        if( NCMAG_Init() )      Compass_Device = COMPASS_NCMAG;
110
                        if( NCMAG_Init() )      Compass_Device = COMPASS_NCMAG;
112
                        else                            Compass_Device = COMPASS_NONE;
111
                        else                            Compass_Device = COMPASS_NONE;
113
                        break;
112
                        break;
114
 
113
 
115
                case COMPASS_MK3MAG:
114
                case COMPASS_MK3MAG:
116
                default:
115
                default:
117
                        // nothing to do
116
                        // nothing to do
118
                        break;
117
                        break;
119
 
118
 
120
        }
119
        }
121
        fifo_init(&CompassCalcStateFiFo, CompassCalStateQueue, sizeof(CompassCalStateQueue), NO_ITLine, NO_ITLine);
120
        fifo_init(&CompassCalcStateFiFo, CompassCalStateQueue, sizeof(CompassCalStateQueue), NO_ITLine, NO_ITLine);
122
}
121
}
123
 
122
 
124
 
123
 
125
void Compass_CalcHeading(void)
124
void Compass_CalcHeading(void)
126
{
125
{
127
        if((UART_VersionInfo.HardwareError[0] & NC_ERROR0_SPI_RX) || Compass_CalState)
126
        if((UART_VersionInfo.HardwareError[0] & NC_ERROR0_SPI_RX) || Compass_CalState)
128
        {
127
        {
129
                Compass_Heading = -1;
128
                Compass_Heading = -1;
130
        }
129
        }
131
        else // fc attitude is avialable and no compass calibration active
130
        else // fc attitude is avialable and no compass calibration active
132
        {
131
        {
133
                // calculate attitude correction
132
                // calculate attitude correction
134
                // a float implementation takes too long
133
                // a float implementation takes too long
135
                s16 tmp;
134
                s16 tmp;
136
                s32 sinnick, cosnick, sinroll, cosroll;
135
                s32 sinnick, cosnick, sinroll, cosroll;
137
                tmp = FromFlightCtrl.AngleNick/10; // in deg
136
                tmp = FromFlightCtrl.AngleNick/10; // in deg
138
                sinnick = (s32)c_sin_8192(tmp);
137
                sinnick = (s32)c_sin_8192(tmp);
139
                cosnick = (s32)c_cos_8192(tmp);
138
                cosnick = (s32)c_cos_8192(tmp);
140
                tmp = FromFlightCtrl.AngleRoll/10; // in deg
139
                tmp = FromFlightCtrl.AngleRoll/10; // in deg
141
                sinroll = (s32)c_sin_8192(tmp);
140
                sinroll = (s32)c_sin_8192(tmp);
142
                cosroll = (s32)c_cos_8192(tmp);
141
                cosroll = (s32)c_cos_8192(tmp);
143
                // tbd. compensation signs and oriantation has to be fixed
142
                // tbd. compensation signs and oriantation has to be fixed
144
                Hx = (s16)((MagVector.Y * cosnick + MagVector.Z * sinnick)/8192L);
143
                Hx = (s16)((MagVector.Y * cosnick + MagVector.Z * sinnick)/8192L);
145
                Hy = (s16)((MagVector.X * cosroll - MagVector.Z * sinroll)/8192L);
144
                Hy = (s16)((MagVector.X * cosroll - MagVector.Z * sinroll)/8192L);
146
                // calculate heading
145
                // calculate heading
147
                tmp = (s16)(c_atan2_546(Hy, Hx)/546L);
146
                tmp = (s16)(c_atan2_546(Hy, Hx)/546L);
148
                if (tmp > 0) tmp = 360 - tmp;
147
                if (tmp > 0) tmp = 360 - tmp;
149
                else tmp = -tmp;
148
                else tmp = -tmp;
150
                Compass_Heading = tmp;
149
                Compass_Heading = tmp;
151
        }
150
        }
152
}
151
}
153
 
152
 
154
 
153
 
155
void Compass_Update(void)
154
void Compass_Update(void)
156
{
155
{
157
static s16vec_t old;  
156
static s16vec_t old;  
158
static u32 check_value_counter = 0;
157
static u32 check_value_counter = 0;
159
        // check for new cal state
158
        // check for new cal state
160
        Compass_UpdateCalState();
159
        Compass_UpdateCalState();
161
        if(Compass_CalState) FC_is_Calibrated = 0;
160
        if(Compass_CalState) FC_is_Calibrated = 0;
162
        // initiate new compass communication
161
        // initiate new compass communication
163
        switch(Compass_Device)
162
        switch(Compass_Device)
164
        {
163
        {
165
                case COMPASS_MK3MAG:
164
                case COMPASS_MK3MAG:
166
                        MK3MAG_Update();
165
                        MK3MAG_Update();
167
                        DebugOut.Analog[24] = MagVector.X;
166
                        DebugOut.Analog[24] = MagVector.X;
168
                        DebugOut.Analog[25] = MagVector.Y;
167
                        DebugOut.Analog[25] = MagVector.Y;
169
                        DebugOut.Analog[26] = MagVector.Z;
168
                        DebugOut.Analog[26] = MagVector.Z;
170
                        break;
169
                        break;
171
                case COMPASS_NCMAG:
170
                case COMPASS_NCMAG:
172
                        NCMAG_Update();
171
                        NCMAG_Update();
173
                        DebugOut.Analog[24] = MagRawVector.X;
172
                        DebugOut.Analog[24] = MagRawVector.X;
174
                        DebugOut.Analog[25] = MagRawVector.Y;
173
                        DebugOut.Analog[25] = MagRawVector.Y;
175
                        DebugOut.Analog[26] = MagRawVector.Z;
174
                        DebugOut.Analog[26] = MagRawVector.Z;
176
                default:
175
                default:
177
                        break;
176
                        break;
178
        }
177
        }
179
    if(!((old.X == MagVector.X) || (old.Y == MagVector.Y) || (old.Z == MagVector.Z))) check_value_counter = 0; // Values are normally changing
178
    if(!((old.X == MagVector.X) || (old.Y == MagVector.Y) || (old.Z == MagVector.Z))) check_value_counter = 0; // Values are normally changing
180
 
179
 
181
        if(check_value_counter > 5000)
180
        if(check_value_counter > 5000)
182
         {
181
         {
183
          Compass_Heading = -1; // values didn't change for 5 seconds -> probably a compass-fault
182
          Compass_Heading = -1; // values didn't change for 5 seconds -> probably a compass-fault
184
         }
183
         }
185
         else check_value_counter++;
184
         else check_value_counter++;
186
 
185
 
187
        old.X = MagVector.X;
186
        old.X = MagVector.X;
188
        old.Y = MagVector.Y;
187
        old.Y = MagVector.Y;
189
        old.Z = MagVector.Z;
188
        old.Z = MagVector.Z;
190
}
189
}
191
 
190
 
192
// put cal state into fifo
191
// put cal state into fifo
193
void Compass_SetCalState(u8 CalState)
192
void Compass_SetCalState(u8 CalState)
194
{
193
{
195
        fifo_put(&CompassCalcStateFiFo, CalState);
194
        fifo_put(&CompassCalcStateFiFo, CalState);
196
}
195
}
197
 
196
 
198
// get cal state from fifo
197
// get cal state from fifo
199
void Compass_UpdateCalState()
198
void Compass_UpdateCalState()
200
{
199
{
201
        fifo_get(&CompassCalcStateFiFo, (u8*)&Compass_CalState);
200
        fifo_get(&CompassCalcStateFiFo, (u8*)&Compass_CalState);
202
}
201
}
203
 
202
 
204
 
203