Subversion Repositories NaviCtrl

Rev

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

Rev 693 Rev 703
1
/*#######################################################################################*/
1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + www.MikroKopter.com
5
// + www.MikroKopter.com
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
// + Software Nutzungsbedingungen (english version: see below)
7
// + Software Nutzungsbedingungen (english version: see below)
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
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
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
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.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
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.
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
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.
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
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.
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
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand
22
// + des Mitverschuldens offen.
22
// + des Mitverschuldens offen.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
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.
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.
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
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.
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.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
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
// +  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.
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Software LICENSING TERMS
32
// + Software LICENSING TERMS
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
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
// + 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
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
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.
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
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
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.
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
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.
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
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
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
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
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.
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
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.
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
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.
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.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
53
// + #### END OF LICENSING TERMS ####
53
// + #### END OF LICENSING TERMS ####
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
 
56
 
57
#include <string.h>
57
#include <string.h>
58
#include <math.h>
58
#include <math.h>
59
#include <stdlib.h>
59
#include <stdlib.h>
60
#include "91x_lib.h"
60
#include "91x_lib.h"
61
#include "led.h"
61
#include "led.h"
62
#include "gps.h"
62
#include "gps.h"
63
#include "uart1.h"
63
#include "uart1.h"
64
#include "spi_slave.h"
64
#include "spi_slave.h"
65
#include "compass.h"
65
#include "compass.h"
66
#include "timer1.h"
66
#include "timer1.h"
67
#include "timer2.h"
67
#include "timer2.h"
68
#include "config.h"
68
#include "config.h"
69
#include "main.h"
69
#include "main.h"
70
#include "params.h"
70
#include "params.h"
71
#include "settings.h"
71
#include "settings.h"
-
 
72
#include "triggerlog.h"
72
 
73
 
73
#define SPI_FCSYNCBYTE1         0xAA
74
#define SPI_FCSYNCBYTE1         0xAA
74
#define SPI_FCSYNCBYTE2         0x85
75
#define SPI_FCSYNCBYTE2         0x85
75
#define SPI_FCSYNCBYTE_HB1      0xA2  // for the huge Block
76
#define SPI_FCSYNCBYTE_HB1      0xA2  // for the huge Block
76
#define SPI_FCSYNCBYTE_HB2      0x48  // for the huge Block
77
#define SPI_FCSYNCBYTE_HB2      0x48  // for the huge Block
77
#define SPI_NCSYNCBYTE1         0x82
78
#define SPI_NCSYNCBYTE1         0x82
78
#define SPI_NCSYNCBYTE2         0x55
79
#define SPI_NCSYNCBYTE2         0x55
79
#define SPI_NCSYNCBYTE_HB2      0xA5  // for the huge Block
80
#define SPI_NCSYNCBYTE_HB2      0xA5  // for the huge Block
80
 
81
 
81
 
82
 
82
//communication packets
83
//communication packets
83
FromFlightCtrl_t   FromFlightCtrl;
84
FromFlightCtrl_t   FromFlightCtrl;
84
ToFlightCtrl_t     ToFlightCtrl;
85
ToFlightCtrl_t     ToFlightCtrl;
85
paramset_t EE_Parameter;
86
paramset_t EE_Parameter;
86
#define SPI0_TIMEOUT     2500  //ms
87
#define SPI0_TIMEOUT     2500  //ms
87
volatile u32 SPI0_Timeout = 0;
88
volatile u32 SPI0_Timeout = 0;
88
u8 Logging_FCStatusFlags1 = 0,Logging_FCStatusFlags2 = 0;
89
u8 Logging_FCStatusFlags1 = 0,Logging_FCStatusFlags2 = 0;
89
u8 SpeakHoTT = 0;
90
u8 SpeakHoTT = 0;
90
u32 LoggingGasFilter = 0, LoggingGasCnt = 0;
91
u32 LoggingGasFilter = 0, LoggingGasCnt = 0;
91
u8 Out1TriggerUpdateBlocked = 0;
92
u8 Out1TriggerUpdateBlocked = 0;
92
u8 Out1TriggerUpdateNewData = 0;
93
u8 Out1TriggerUpdateNewData = 0;
93
 
94
 
94
// tx packet buffer
95
// tx packet buffer
95
volatile u8 SPI_TxBuffer[sizeof(HugeBlockFromFC) + 10];
96
volatile u8 SPI_TxBuffer[sizeof(HugeBlockFromFC) + 10];
96
volatile u8 SPI_TxBufferIndex = 0;
97
volatile u8 SPI_TxBufferIndex = 0;
97
u8 *Ptr_TxChksum = NULL ;  // pointer to checksum in TxBuffer
98
u8 *Ptr_TxChksum = NULL ;  // pointer to checksum in TxBuffer
98
u16 SPI_Tx_Datasize = sizeof(ToFlightCtrl) + 2;
99
u16 SPI_Tx_Datasize = sizeof(ToFlightCtrl) + 2;
99
// rx packet buffer
100
// rx packet buffer
100
volatile u8 SPI_RxBuffer[sizeof(HugeBlockFromFC)+10];
101
volatile u8 SPI_RxBuffer[sizeof(HugeBlockFromFC)+10];
101
volatile u8 SPI_RxBufferIndex = 0;
102
volatile u8 SPI_RxBufferIndex = 0;
102
volatile u8 SPI_RxBuffer_Request = 0;
103
volatile u8 SPI_RxBuffer_Request = 0;
103
#define SPI_COMMAND_INDEX 0
104
#define SPI_COMMAND_INDEX 0
104
 
105
 
105
s32 Kalman_K = 32;
106
s32 Kalman_K = 32;
106
s32 Kalman_MaxDrift = 5 * 16;
107
s32 Kalman_MaxDrift = 5 * 16;
107
s32 Kalman_MaxFusion = 64;
108
s32 Kalman_MaxFusion = 64;
108
s32 Kalman_Kompass = 32;
109
s32 Kalman_Kompass = 32;
109
s32 ToFcGpsZ = 0;
110
s32 ToFcGpsZ = 0;
110
u8 CompassCalState = 0;
111
u8 CompassCalState = 0;
111
u8 RequestConfigFromFC = 1;
112
u8 RequestConfigFromFC = 1;
112
 
113
 
113
u8 SPI_CommandSequence[] = {    SPI_NCCMD_KALMAN, SPI_NCCMD_GPSINFO, SPI_SERIAL_CH,  // Achtung: SPI_SERIAL_CH darf nicht am Ende des Arrays stehen     (wird gescipped)
114
u8 SPI_CommandSequence[] = {    SPI_NCCMD_KALMAN, SPI_NCCMD_GPSINFO, SPI_SERIAL_CH,  // Achtung: SPI_SERIAL_CH darf nicht am Ende des Arrays stehen     (wird gescipped)
114
                                                                SPI_NCCMD_KALMAN, SPI_NCCMD_HOTT_INFO,
115
                                                                SPI_NCCMD_KALMAN, SPI_NCCMD_HOTT_INFO,
115
                                                                SPI_NCCMD_KALMAN, SPI_MISC,
116
                                                                SPI_NCCMD_KALMAN, SPI_MISC,
116
                                                                SPI_NCCMD_KALMAN, SPI_NCCMD_VERSION };
117
                                                                SPI_NCCMD_KALMAN, SPI_NCCMD_VERSION };
117
u8 SPI_CommandCounter = 0;
118
u8 SPI_CommandCounter = 0;
118
s32 ToFC_Rotate_C = 64, ToFC_Rotate_S = 0;
119
s32 ToFC_Rotate_C = 64, ToFC_Rotate_S = 0;
119
s32 HeadFreeStartAngle = 0;       // in 0,1°
120
s32 HeadFreeStartAngle = 0;       // in 0,1°
120
s32 CompassDirectionAtMotorStart = 0; // in 0,1°
121
s32 CompassDirectionAtMotorStart = 0; // in 0,1°
121
s16 FC_WP_EventChannel = 0, LogFC_WP_EventChannel = 0, FC_WP_EventChannel_Processed = 0; // gibt einen Schaltkanal an die FC weiter, wenn der Wegpunkt erreicht wurde
122
s16 FC_WP_EventChannel = 0, LogFC_WP_EventChannel = 0, FC_WP_EventChannel_Processed = 0; // gibt einen Schaltkanal an die FC weiter, wenn der Wegpunkt erreicht wurde
122
u32 ToFC_AltitudeRate = 0;
123
u32 ToFC_AltitudeRate = 0;
123
s32 ToFC_AltitudeSetpoint = 0;
124
s32 ToFC_AltitudeSetpoint = 0;
124
u8  FromFC_VarioCharacter = ' ';
125
u8  FromFC_VarioCharacter = ' ';
125
s16 GPS_Aid_StickMultiplikator = 0;
126
s16 GPS_Aid_StickMultiplikator = 0;
126
u8 NC_GPS_ModeCharacter = ' ';
127
u8 NC_GPS_ModeCharacter = ' ';
127
u8 FCCalibActive = 0;
128
u8 FCCalibActive = 0;
128
u8 FC_is_Calibrated = 0;
129
u8 FC_is_Calibrated = 0;
129
Motor_t Motor[MAX_MOTORS];
130
Motor_t Motor[MAX_MOTORS];
130
u8 Motor_Version[MAX_MOTORS]; // das kann nicht in die struct, weil der PC die Struktur bekommt
131
u8 Motor_Version[MAX_MOTORS]; // das kann nicht in die struct, weil der PC die Struktur bekommt
131
u8 NC_To_FC_Flags = 0;
132
u8 NC_To_FC_Flags = 0;
132
u8 BL_MinOfMaxPWM = 255;  // indication if all BL-controllers run on full power
133
u8 BL_MinOfMaxPWM = 255;  // indication if all BL-controllers run on full power
133
u8 Logging_BL_MinOfMaxPWM = 255;
134
u8 Logging_BL_MinOfMaxPWM = 255;
134
u8 ErrorCheck_BL_MinOfMaxPWM = 255;
135
u8 ErrorCheck_BL_MinOfMaxPWM = 255;
135
u32 FC_I2C_ErrorConter;
136
u32 FC_I2C_ErrorConter;
136
SPI_Version_t FC_Version;
137
SPI_Version_t FC_Version;
137
s16 POI_KameraNick = 0;
138
s16 POI_KameraNick = 0;
138
u8 NC_Wait_for_LED = 0;
139
u8 NC_Wait_for_LED = 0;
139
s16 GyroCompassCorrected = 0; // corrected with the magnetic declination
140
s16 GyroCompassCorrected = 0; // corrected with the magnetic declination
140
s16 CompassSetpointCorrected = 0; // The compass setpoint that the FC tries to keep - corrected with the magnetic declination
141
s16 CompassSetpointCorrected = 0; // The compass setpoint that the FC tries to keep - corrected with the magnetic declination
141
s16 CompassSetpoint = 0;          // in 0,1°
142
s16 CompassSetpoint = 0;          // in 0,1°
142
s16 SimulatedDirection = 0;     // only for flight simulation
143
s16 SimulatedDirection = 0;     // only for flight simulation
143
u8 AmountOfMotors = 0;
144
u8 AmountOfMotors = 0;
144
u16 FlugMinutenGesamt;
145
u16 FlugMinutenGesamt;
145
u8 HoverGas = 0;
146
u8 HoverGas = 0;
146
u8 LowVoltageLandingActive = 0;
147
u8 LowVoltageLandingActive = 0;
147
s8 PPM_In[MAX_RC_IN] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
148
s8 PPM_In[MAX_RC_IN] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
148
s16 FromFlightCtrl_AccNick = 0,FromFlightCtrl_AccRoll = 0,FromFlightCtrl_GyroNick = 0,FromFlightCtrl_GyroRoll = 0;
149
s16 FromFlightCtrl_AccNick = 0,FromFlightCtrl_AccRoll = 0,FromFlightCtrl_GyroNick = 0,FromFlightCtrl_GyroRoll = 0;
149
str_HugeBlockFromFC HugeBlockFromFC;
150
str_HugeBlockFromFC HugeBlockFromFC;
150
str_HugeBlockToFC HugeBlockToFC;
151
str_HugeBlockToFC HugeBlockToFC;
151
//--------------------------------------------------------------
152
//--------------------------------------------------------------
152
void SSP0_IRQHandler(void)
153
void SSP0_IRQHandler(void)
153
{
154
{
154
        static u8 rxchksum = 0;
155
        static u8 rxchksum = 0;
155
        u8 rxdata;
156
        u8 rxdata;
156
 
157
 
157
        #define SPI_SYNC1       0
158
        #define SPI_SYNC1       0
158
        #define SPI_SYNC2       1
159
        #define SPI_SYNC2       1
159
        #define SPI_DATA        2
160
        #define SPI_DATA        2
160
        #define SPI_SYNC_HB     3
161
        #define SPI_SYNC_HB     3
161
        #define SPI_DATA_HB     4
162
        #define SPI_DATA_HB     4
162
        static u8 SPI_State = SPI_SYNC1;
163
        static u8 SPI_State = SPI_SYNC1;
163
        //IENABLE;
164
        //IENABLE;
164
 
165
 
165
        // clear pending bits
166
        // clear pending bits
166
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxTimeOut);
167
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxTimeOut);
167
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxFifo);
168
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxFifo);
168
 
169
 
169
        // while RxFIFO not empty
170
        // while RxFIFO not empty
170
        while(SSP_GetFlagStatus(SSP0, SSP_FLAG_RxFifoNotEmpty) == SET)
171
        while(SSP_GetFlagStatus(SSP0, SSP_FLAG_RxFifoNotEmpty) == SET)
171
        {
172
        {
172
                rxdata =  SSP0->DR; // catch the received byte
173
                rxdata =  SSP0->DR; // catch the received byte
173
                switch (SPI_State)
174
                switch (SPI_State)
174
                {
175
                {
175
                        case SPI_SYNC1:
176
                        case SPI_SYNC1:
176
                                SPI_RxBufferIndex = 0; // reset buffer index
177
                                SPI_RxBufferIndex = 0; // reset buffer index
177
                                SPI_TxBufferIndex = 0; // reset buffer index
178
                                SPI_TxBufferIndex = 0; // reset buffer index
178
                                rxchksum = rxdata;     // init checksum
179
                                rxchksum = rxdata;     // init checksum
179
                                if (rxdata == SPI_FCSYNCBYTE1)
180
                                if (rxdata == SPI_FCSYNCBYTE1)
180
                                {   // 1st syncbyte ok
181
                                {   // 1st syncbyte ok
181
                                        SPI_State = SPI_SYNC2;  // step to sync2
182
                                        SPI_State = SPI_SYNC2;  // step to sync2
182
                                        ToFlightCtrl.Chksum = 0;
183
                                        ToFlightCtrl.Chksum = 0;
183
                                        memcpy((u8 *) &(SPI_TxBuffer[2]), (u8 *) &ToFlightCtrl, sizeof(ToFlightCtrl));
184
                                        memcpy((u8 *) &(SPI_TxBuffer[2]), (u8 *) &ToFlightCtrl, sizeof(ToFlightCtrl));
184
                                        SPI_TxBuffer[0] = SPI_NCSYNCBYTE1;
185
                                        SPI_TxBuffer[0] = SPI_NCSYNCBYTE1;
185
                                        SPI_TxBuffer[1] = SPI_NCSYNCBYTE2;
186
                                        SPI_TxBuffer[1] = SPI_NCSYNCBYTE2;
186
                                        SPI_Tx_Datasize = sizeof(ToFlightCtrl) + 2;     // +2 weil die Syncbytes nicht in dem Block stehen
187
                                        SPI_Tx_Datasize = sizeof(ToFlightCtrl) + 2;     // +2 weil die Syncbytes nicht in dem Block stehen
187
                                        Ptr_TxChksum = (u8 *) &(((ToFlightCtrl_t *) &(SPI_TxBuffer[2]))->Chksum);
188
                                        Ptr_TxChksum = (u8 *) &(((ToFlightCtrl_t *) &(SPI_TxBuffer[2]))->Chksum);
188
                                }
189
                                }
189
                                else
190
                                else
190
                                if (rxdata == SPI_FCSYNCBYTE_HB1)
191
                                if (rxdata == SPI_FCSYNCBYTE_HB1)
191
                                {   // 1st syncbyte ok
192
                                {   // 1st syncbyte ok
192
                                        SPI_State = SPI_SYNC_HB;  // step to syncHB
193
                                        SPI_State = SPI_SYNC_HB;  // step to syncHB
193
                                        SPI_RxBuffer[SPI_RxBufferIndex++]= rxdata; // sync1
194
                                        SPI_RxBuffer[SPI_RxBufferIndex++]= rxdata; // sync1
194
                                        HugeBlockToFC.Sync1 = SPI_NCSYNCBYTE1;
195
                                        HugeBlockToFC.Sync1 = SPI_NCSYNCBYTE1;
195
                                        HugeBlockToFC.Sync2 = SPI_NCSYNCBYTE_HB2;
196
                                        HugeBlockToFC.Sync2 = SPI_NCSYNCBYTE_HB2;
196
                                        HugeBlockToFC.Chksum = 0;
197
                                        HugeBlockToFC.Chksum = 0;
197
                                        memcpy((u8 *) &(SPI_TxBuffer[0]), (u8 *) &HugeBlockToFC, sizeof(HugeBlockToFC));
198
                                        memcpy((u8 *) &(SPI_TxBuffer[0]), (u8 *) &HugeBlockToFC, sizeof(HugeBlockToFC));
198
                                        SPI_Tx_Datasize = sizeof(HugeBlockToFC);
199
                                        SPI_Tx_Datasize = sizeof(HugeBlockToFC);
199
                                        Ptr_TxChksum = (u8 *) &(((str_HugeBlockToFC *) &(SPI_TxBuffer[0]))->Chksum);
200
                                        Ptr_TxChksum = (u8 *) &(((str_HugeBlockToFC *) &(SPI_TxBuffer[0]))->Chksum);
200
                                        *Ptr_TxChksum = 0;
201
                                        *Ptr_TxChksum = 0;
201
                                }
202
                                }
202
                                break;
203
                                break;
203
                        case SPI_SYNC2:
204
                        case SPI_SYNC2:
204
                                if (rxdata == SPI_FCSYNCBYTE2)
205
                                if (rxdata == SPI_FCSYNCBYTE2)
205
                                {  // 2nd Syncbyte ok
206
                                {  // 2nd Syncbyte ok
206
                                        rxchksum += rxdata;
207
                                        rxchksum += rxdata;
207
                                        SPI_State = SPI_DATA;
208
                                        SPI_State = SPI_DATA;
208
                                }  // 2nd Syncbyte does not match
209
                                }  // 2nd Syncbyte does not match
209
                                else
210
                                else
210
                                {
211
                                {
211
                                        SPI_State  = SPI_SYNC1; //jump back to sync1
212
                                        SPI_State  = SPI_SYNC1; //jump back to sync1
212
                                }
213
                                }
213
                                break;
214
                                break;
214
                        case SPI_SYNC_HB:
215
                        case SPI_SYNC_HB:
215
                                if (rxdata == SPI_FCSYNCBYTE_HB2)
216
                                if (rxdata == SPI_FCSYNCBYTE_HB2)
216
                                {  // 2nd Syncbyte ok
217
                                {  // 2nd Syncbyte ok
217
                                        rxchksum += rxdata;
218
                                        rxchksum += rxdata;
218
                                        SPI_State = SPI_DATA_HB;
219
                                        SPI_State = SPI_DATA_HB;
219
                                        SPI_RxBuffer[SPI_RxBufferIndex++]= rxdata; // sync2
220
                                        SPI_RxBuffer[SPI_RxBufferIndex++]= rxdata; // sync2
220
                                }  // 2nd Syncbyte does not match
221
                                }  // 2nd Syncbyte does not match
221
                                else
222
                                else
222
                                {
223
                                {
223
                                        SPI_State  = SPI_SYNC1; //jump back to sync1
224
                                        SPI_State  = SPI_SYNC1; //jump back to sync1
224
                                }
225
                                }
225
                                break;
226
                                break;
226
                        case SPI_DATA:
227
                        case SPI_DATA:
227
                                SPI_RxBuffer[SPI_RxBufferIndex++]= rxdata; // copy databyte to rx buffer
228
                                SPI_RxBuffer[SPI_RxBufferIndex++]= rxdata; // copy databyte to rx buffer
228
                                if(SPI_RxBufferIndex >= sizeof(ToFlightCtrl))  // end of packet is reached
229
                                if(SPI_RxBufferIndex >= sizeof(ToFlightCtrl))  // end of packet is reached
229
                                {
230
                                {
230
                                        if (rxdata == rxchksum) // verify checksum byte
231
                                        if (rxdata == rxchksum) // verify checksum byte
231
                                        {
232
                                        {
232
                                                // copy SPI_RxBuffer -> FromFlightCtrl
233
                                                // copy SPI_RxBuffer -> FromFlightCtrl
233
                                                if(!SPI_RxBuffer_Request) // block writing to FromFlightCtrl on reading access
234
                                                if(!SPI_RxBuffer_Request) // block writing to FromFlightCtrl on reading access
234
                                                {
235
                                                {
235
                                                        memcpy((u8 *) &FromFlightCtrl, (u8 *) SPI_RxBuffer, sizeof(FromFlightCtrl));
236
                                                        memcpy((u8 *) &FromFlightCtrl, (u8 *) SPI_RxBuffer, sizeof(FromFlightCtrl));
236
                                                        SPI_RxBuffer_Request = 1;
237
                                                        SPI_RxBuffer_Request = 1;
237
                                                }
238
                                                }
238
                                                // reset timeout counter on good packet
239
                                                // reset timeout counter on good packet
239
                                                SPI0_Timeout = SetDelay(SPI0_TIMEOUT);
240
                                                SPI0_Timeout = SetDelay(SPI0_TIMEOUT);
240
//                                              DebugOut.Analog[13]++;
241
//                                              DebugOut.Analog[13]++;
241
                                        }
242
                                        }
242
                                        else // bad checksum byte
243
                                        else // bad checksum byte
243
                                        {
244
                                        {
244
                                                DebugOut.Analog[12]++; // increase SPI chksum error counter
245
                                                DebugOut.Analog[12]++; // increase SPI chksum error counter
245
                                        }
246
                                        }
246
                                        SPI_State  = SPI_SYNC1; // reset state
247
                                        SPI_State  = SPI_SYNC1; // reset state
247
                                }
248
                                }
248
                                else // end of packet not reached
249
                                else // end of packet not reached
249
                                {
250
                                {
250
                                        rxchksum += rxdata;      // update checksum
251
                                        rxchksum += rxdata;      // update checksum
251
                                }
252
                                }
252
                                break;
253
                                break;
253
                        case SPI_DATA_HB:
254
                        case SPI_DATA_HB:
254
                                SPI_RxBuffer[SPI_RxBufferIndex++]= rxdata; // copy databyte to rx buffer
255
                                SPI_RxBuffer[SPI_RxBufferIndex++]= rxdata; // copy databyte to rx buffer
255
                                if(SPI_RxBufferIndex >= sizeof(HugeBlockFromFC))  // end of packet is reached
256
                                if(SPI_RxBufferIndex >= sizeof(HugeBlockFromFC))  // end of packet is reached
256
                                {
257
                                {
257
                                        if (rxdata == rxchksum) // verify checksum byte
258
                                        if (rxdata == rxchksum) // verify checksum byte
258
                                        {
259
                                        {
259
                                                // copy SPI_RxBuffer -> FromFlightCtrl
260
                                                // copy SPI_RxBuffer -> FromFlightCtrl
260
                                                memcpy((u8 *) &HugeBlockFromFC, (u8 *) SPI_RxBuffer, sizeof(HugeBlockFromFC));
261
                                                memcpy((u8 *) &HugeBlockFromFC, (u8 *) SPI_RxBuffer, sizeof(HugeBlockFromFC));
261
                                                // reset timeout counter on good packet
262
                                                // reset timeout counter on good packet
262
                                                SPI0_Timeout = SetDelay(SPI0_TIMEOUT);
263
                                                SPI0_Timeout = SetDelay(SPI0_TIMEOUT);
263
                                        }
264
                                        }
264
                                        else // bad checksum byte
265
                                        else // bad checksum byte
265
                                        {
266
                                        {
266
                                                DebugOut.Analog[12]++; // increase SPI chksum error counter
267
                                                DebugOut.Analog[12]++; // increase SPI chksum error counter
267
                                        }
268
                                        }
268
                                        SPI_State  = SPI_SYNC1; // reset state
269
                                        SPI_State  = SPI_SYNC1; // reset state
269
                                }
270
                                }
270
                                else // end of packet not reached
271
                                else // end of packet not reached
271
                                {
272
                                {
272
                                        rxchksum += rxdata;      // update checksum
273
                                        rxchksum += rxdata;      // update checksum
273
                                }
274
                                }
274
                                break;
275
                                break;
275
                        default:
276
                        default:
276
                                SPI_State  = SPI_SYNC1;
277
                                SPI_State  = SPI_SYNC1;
277
                                break;
278
                                break;
278
                }
279
                }
279
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
280
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
280
// + Ouput Data
281
// + Ouput Data
281
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
282
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
282
                // Fill TxFIFO while its not full or end of packet is reached
283
                // Fill TxFIFO while its not full or end of packet is reached
283
                while((SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoNotFull) == SET) && (SPI_TxBufferIndex  < SPI_Tx_Datasize))
284
                while((SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoNotFull) == SET) && (SPI_TxBufferIndex  < SPI_Tx_Datasize))
284
                {
285
                {
285
                        SSP0->DR = SPI_TxBuffer[SPI_TxBufferIndex];       // send a byte
286
                        SSP0->DR = SPI_TxBuffer[SPI_TxBufferIndex];       // send a byte
286
                        *Ptr_TxChksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum
287
                        *Ptr_TxChksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum
287
                        if(SPIWatchDog == 0) *Ptr_TxChksum += 1; // disturbe this packet to stop the communication!
288
                        if(SPIWatchDog == 0) *Ptr_TxChksum += 1; // disturbe this packet to stop the communication!
288
                        SPI_TxBufferIndex++; // pointer to next byte
289
                        SPI_TxBufferIndex++; // pointer to next byte
289
                }
290
                }
290
        }
291
        }
291
        VIC1->VAR = 0xFF; // write any value to VIC0 Vector address register
292
        VIC1->VAR = 0xFF; // write any value to VIC0 Vector address register
292
}
293
}
293
 
294
 
294
//--------------------------------------------------------------
295
//--------------------------------------------------------------
295
void SPI0_Init(void)
296
void SPI0_Init(void)
296
{
297
{
297
        GPIO_InitTypeDef GPIO_InitStructure;
298
        GPIO_InitTypeDef GPIO_InitStructure;
298
        SSP_InitTypeDef   SSP_InitStructure;
299
        SSP_InitTypeDef   SSP_InitStructure;
299
 
300
 
300
        UART1_PutString("\r\n SPI init...");
301
        UART1_PutString("\r\n SPI init...");
301
 
302
 
302
        SCU_APBPeriphClockConfig(__GPIO2 ,ENABLE);
303
        SCU_APBPeriphClockConfig(__GPIO2 ,ENABLE);
303
        SCU_APBPeriphClockConfig(__SSP0 ,ENABLE);
304
        SCU_APBPeriphClockConfig(__SSP0 ,ENABLE);
304
 
305
 
305
        GPIO_DeInit(GPIO2);
306
        GPIO_DeInit(GPIO2);
306
        //SSP0_CLK, SSP0_MOSI, SSP0_NSS pins
307
        //SSP0_CLK, SSP0_MOSI, SSP0_NSS pins
307
        GPIO_StructInit(&GPIO_InitStructure);
308
        GPIO_StructInit(&GPIO_InitStructure);
308
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
309
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
309
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7;
310
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7;
310
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
311
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
311
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
312
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
312
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; //SSP0_SCLK, SSP0_MOSI, SSP0_NSS
313
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; //SSP0_SCLK, SSP0_MOSI, SSP0_NSS
313
        GPIO_Init (GPIO2, &GPIO_InitStructure);
314
        GPIO_Init (GPIO2, &GPIO_InitStructure);
314
 
315
 
315
        // SSP0_MISO pin GPIO2.6
316
        // SSP0_MISO pin GPIO2.6
316
        GPIO_StructInit(&GPIO_InitStructure);
317
        GPIO_StructInit(&GPIO_InitStructure);
317
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
318
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
318
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
319
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
319
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
320
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
320
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
321
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
321
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //SSP0_MISO
322
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //SSP0_MISO
322
        GPIO_Init (GPIO2, &GPIO_InitStructure);
323
        GPIO_Init (GPIO2, &GPIO_InitStructure);
323
        GPIO_WriteBit(GPIO2, GPIO_Pin_6, Bit_RESET); // switch MISO to low 
324
        GPIO_WriteBit(GPIO2, GPIO_Pin_6, Bit_RESET); // switch MISO to low 
324
 
325
 
325
        SSP_DeInit(SSP0);
326
        SSP_DeInit(SSP0);
326
        SSP_StructInit(&SSP_InitStructure);
327
        SSP_StructInit(&SSP_InitStructure);
327
        SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola;
328
        SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola;
328
        SSP_InitStructure.SSP_Mode = SSP_Mode_Slave;
329
        SSP_InitStructure.SSP_Mode = SSP_Mode_Slave;
329
        SSP_InitStructure.SSP_SlaveOutput = SSP_SlaveOutput_Enable;
330
        SSP_InitStructure.SSP_SlaveOutput = SSP_SlaveOutput_Enable;
330
        SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge;
331
        SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge;
331
        SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low;
332
        SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low;
332
        SSP_InitStructure.SSP_ClockRate = 0;
333
        SSP_InitStructure.SSP_ClockRate = 0;
333
 
334
 
334
        SSP_Init(SSP0, &SSP_InitStructure);
335
        SSP_Init(SSP0, &SSP_InitStructure);
335
        SSP_ITConfig(SSP0, SSP_IT_RxFifo | SSP_IT_RxTimeOut, ENABLE);
336
        SSP_ITConfig(SSP0, SSP_IT_RxFifo | SSP_IT_RxTimeOut, ENABLE);
336
        SSP_Cmd(SSP0, ENABLE);
337
        SSP_Cmd(SSP0, ENABLE);
337
        // initialize the syncbytes in the tx buffer
338
        // initialize the syncbytes in the tx buffer
338
        SPI_TxBuffer[0] = SPI_NCSYNCBYTE1;
339
        SPI_TxBuffer[0] = SPI_NCSYNCBYTE1;
339
        SPI_TxBuffer[1] = SPI_NCSYNCBYTE2;
340
        SPI_TxBuffer[1] = SPI_NCSYNCBYTE2;
340
        // set the pointer to the checksum byte in the tx buffer
341
        // set the pointer to the checksum byte in the tx buffer
341
        Ptr_TxChksum = (u8 *) &(((ToFlightCtrl_t *) &(SPI_TxBuffer[2]))->Chksum);
342
        Ptr_TxChksum = (u8 *) &(((ToFlightCtrl_t *) &(SPI_TxBuffer[2]))->Chksum);
342
 
343
 
343
        ToFlightCtrl.GPSStick.Nick = 0;
344
        ToFlightCtrl.GPSStick.Nick = 0;
344
        ToFlightCtrl.GPSStick.Roll = 0;
345
        ToFlightCtrl.GPSStick.Roll = 0;
345
//      ToFlightCtrl.GPSStick.Yaw = 0;
346
//      ToFlightCtrl.GPSStick.Yaw = 0;
346
 
347
 
347
        VIC_Config(SSP0_ITLine, VIC_IRQ, PRIORITY_SPI0);
348
        VIC_Config(SSP0_ITLine, VIC_IRQ, PRIORITY_SPI0);
348
        VIC_ITCmd(SSP0_ITLine, ENABLE);
349
        VIC_ITCmd(SSP0_ITLine, ENABLE);
349
 
350
 
350
        SPI0_Timeout = SetDelay(4*SPI0_TIMEOUT);
351
        SPI0_Timeout = SetDelay(4*SPI0_TIMEOUT);
351
        EE_Parameter.Revision = 0;
352
        EE_Parameter.Revision = 0;
352
        sprintf(EE_Parameter.Name,"???\0");
353
        sprintf(EE_Parameter.Name,"???\0");
353
        UART1_PutString("ok");
354
        UART1_PutString("ok");
354
}
355
}
355
 
356
 
356
 
357
 
357
//------------------------------------------------------
358
//------------------------------------------------------
358
void SPI0_UpdateBuffer(void)
359
void SPI0_UpdateBuffer(void)
359
{
360
{
360
        static u32 timeout = 0;
361
        static u32 timeout = 0;
361
        static u8 counter = 50,hott_index = 0, last_error_code = 0, enable_injecting = 0;
362
        static u8 counter = 50,hott_index = 0, last_error_code = 0, enable_injecting = 0;
362
        static s16 last_wp_event = 0;
363
        static s16 last_wp_event = 0;
363
        u8 index;
364
        u8 index;
364
        s16 tmp;
365
        s16 tmp;
365
        s32 i1,i2;
366
        s32 i1,i2;
366
/*
367
/*
367
        union
368
        union
368
        {
369
        {
369
                unsigned char Byte[4];
370
                unsigned char Byte[4];
370
                unsigned int Int[2];
371
                unsigned int Int[2];
371
                unsigned long Long;
372
                unsigned long Long;
372
        } Temp;
373
        } Temp;
373
*/
374
*/
374
        SPIWatchDog = 3500;     // stop communication to FC after this timeout
375
        SPIWatchDog = 3500;     // stop communication to FC after this timeout
375
        if(SPI_RxBuffer_Request)
376
        if(SPI_RxBuffer_Request)
376
        {
377
        {
377
                // avoid sending data via SPI during the update of the  ToFlightCtrl structure
378
                // avoid sending data via SPI during the update of the  ToFlightCtrl structure
378
                VIC_ITCmd(SSP0_ITLine, DISABLE); // disable SPI interrupt
379
                VIC_ITCmd(SSP0_ITLine, DISABLE); // disable SPI interrupt
379
                ToFlightCtrl.CompassHeading = Compass_Heading;
380
                ToFlightCtrl.CompassHeading = Compass_Heading;
380
                DebugOut.Analog[10] = ToFlightCtrl.CompassHeading;
381
                DebugOut.Analog[10] = ToFlightCtrl.CompassHeading;
381
                GyroCompassCorrected = (3600 + FromFlightCtrl.GyroHeading + FC.FromFC_CompassOffset + GeoMagDec) % 3600;
382
                GyroCompassCorrected = (3600 + FromFlightCtrl.GyroHeading + FC.FromFC_CompassOffset + GeoMagDec) % 3600;
382
                if(ToFlightCtrl.CompassHeading >= 0) ToFlightCtrl.CompassHeading = (360 + ToFlightCtrl.CompassHeading + FromFlightCtrl.GyroYaw / 12) % 360;
383
                if(ToFlightCtrl.CompassHeading >= 0) ToFlightCtrl.CompassHeading = (360 + ToFlightCtrl.CompassHeading + FromFlightCtrl.GyroYaw / 12) % 360;
383
//              ToFlightCtrl.MagVecX = MagVector.X;
384
//              ToFlightCtrl.MagVecX = MagVector.X;
384
//              ToFlightCtrl.MagVecY = MagVector.Y;
385
//              ToFlightCtrl.MagVecY = MagVector.Y;
385
//              ToFlightCtrl.MagVecZ = MagVector.Z;
386
//              ToFlightCtrl.MagVecZ = MagVector.Z;
386
//              ToFlightCtrl.NCStatus = 0;
387
//              ToFlightCtrl.NCStatus = 0;
387
// ++++++++++++++++++++++++++++++++++++++++++++++++
388
// ++++++++++++++++++++++++++++++++++++++++++++++++
388
 
389
 
389
// ++++++++++++++++++++++++++++++++++++++++++++++++
390
// ++++++++++++++++++++++++++++++++++++++++++++++++
390
                // cycle spi commands
391
                // cycle spi commands
391
                if(ErrorCode != last_error_code && enable_injecting)
392
                if(ErrorCode != last_error_code && enable_injecting)
392
                {
393
                {
393
                        ToFlightCtrl.Command = SPI_NCCMD_VERSION;
394
                        ToFlightCtrl.Command = SPI_NCCMD_VERSION;
394
                        last_error_code = ErrorCode;
395
                        last_error_code = ErrorCode;
395
                        enable_injecting = 0;
396
                        enable_injecting = 0;
396
                }
397
                }
397
                else
398
                else
398
                if(FC_WP_EventChannel != last_wp_event && enable_injecting)
399
                if(FC_WP_EventChannel != last_wp_event && enable_injecting)
399
                {
400
                {
400
                        ToFlightCtrl.Command = SPI_NCCMD_GPSINFO;
401
                        ToFlightCtrl.Command = SPI_NCCMD_GPSINFO;
401
                        last_wp_event = FC_WP_EventChannel;
402
                        last_wp_event = FC_WP_EventChannel;
402
                        enable_injecting = 0;
403
                        enable_injecting = 0;
403
                }
404
                }
404
                else
405
                else
405
                {
406
                {
406
                        ToFlightCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++];
407
                        ToFlightCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++];
407
                        if((ToFlightCtrl.Command == SPI_SERIAL_CH) && !NewSerialChannelFrame) ToFlightCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++];
408
                        if((ToFlightCtrl.Command == SPI_SERIAL_CH) && !NewSerialChannelFrame) ToFlightCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++];
408
                        // restart command cycle at the end
409
                        // restart command cycle at the end
409
                        if(SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0;
410
                        if(SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0;
410
                        if(ToFlightCtrl.Command == SPI_NCCMD_KALMAN) enable_injecting = 1;
411
                        if(ToFlightCtrl.Command == SPI_NCCMD_KALMAN) enable_injecting = 1;
411
                }
412
                }
412
// ++++++++++++++++++++++++++++++++++++++++++++++++
413
// ++++++++++++++++++++++++++++++++++++++++++++++++
413
 
414
 
414
#define FLAG_GPS_AID 0x01
415
#define FLAG_GPS_AID 0x01
415
                switch (ToFlightCtrl.Command)
416
                switch (ToFlightCtrl.Command)
416
                {
417
                {
417
                        case  SPI_NCCMD_KALMAN:  // wird am häufigsten betätigt
418
                        case  SPI_NCCMD_KALMAN:  // wird am häufigsten betätigt
418
                                ToFlightCtrl.Param.sByte[0] = (s8) Kalman_K;
419
                                ToFlightCtrl.Param.sByte[0] = (s8) Kalman_K;
419
                                ToFlightCtrl.Param.sByte[1] = (s8) Kalman_MaxFusion;
420
                                ToFlightCtrl.Param.sByte[1] = (s8) Kalman_MaxFusion;
420
                                ToFlightCtrl.Param.sByte[2] = (s8) Kalman_MaxDrift;
421
                                ToFlightCtrl.Param.sByte[2] = (s8) Kalman_MaxDrift;
421
                                ToFlightCtrl.Param.Byte[3]      = (u8) Kalman_Kompass;
422
                                ToFlightCtrl.Param.Byte[3]      = (u8) Kalman_Kompass;
422
                                ToFlightCtrl.Param.Byte[4] = 0; // siehe bitcodiert unten
423
                                ToFlightCtrl.Param.Byte[4] = 0; // siehe bitcodiert unten
423
                                if(DebugUART == UART2) ToFlightCtrl.Param.Byte[4] = 0x02; // informs the FC to listen to the UART
424
                                if(DebugUART == UART2) ToFlightCtrl.Param.Byte[4] = 0x02; // informs the FC to listen to the UART
424
                                ToFlightCtrl.Param.Byte[5] = (s8) ToFC_Rotate_C;
425
                                ToFlightCtrl.Param.Byte[5] = (s8) ToFC_Rotate_C;
425
                                ToFlightCtrl.Param.Byte[6] = (s8) ToFC_Rotate_S;
426
                                ToFlightCtrl.Param.Byte[6] = (s8) ToFC_Rotate_S;
426
                                ToFlightCtrl.Param.Byte[7] = GPS_Aid_StickMultiplikator;
427
                                ToFlightCtrl.Param.Byte[7] = GPS_Aid_StickMultiplikator;
427
                                if(CAM_Orientation.UpdateMask & CAM_UPDATE_AZIMUTH)
428
                                if(CAM_Orientation.UpdateMask & CAM_UPDATE_AZIMUTH)
428
                                {
429
                                {
429
//                                      if(CAM_Orientation.Azimuth != -1) ToFlightCtrl.Param.sInt[4] = (CAM_Orientation.Azimuth + (3*360) - (FC.FromFC_CompassOffset / 10 + GeoMagDec/10 + Parameter.OrientationAngle * 15)) % 360;  // the FC uses the uncorrected comnpass value
430
//                                      if(CAM_Orientation.Azimuth != -1) ToFlightCtrl.Param.sInt[4] = (CAM_Orientation.Azimuth + (3*360) - (FC.FromFC_CompassOffset / 10 + GeoMagDec/10 + Parameter.OrientationAngle * 15)) % 360;  // the FC uses the uncorrected comnpass value
430
                                        if(CAM_Orientation.Azimuth != -1) ToFlightCtrl.Param.sInt[4] = (CAM_Orientation.Azimuth + (3*360) - (FC.FromFC_CompassOffset / 10 + GeoMagDec/10 + Parameter.CamOrientation * 15)) % 360;  // the FC uses the uncorrected comnpass value
431
                                        if(CAM_Orientation.Azimuth != -1) ToFlightCtrl.Param.sInt[4] = (CAM_Orientation.Azimuth + (3*360) - (FC.FromFC_CompassOffset / 10 + GeoMagDec/10 + Parameter.CamOrientation * 15)) % 360;  // the FC uses the uncorrected comnpass value
431
                                        else CAM_Orientation.Azimuth = -1;
432
                                        else CAM_Orientation.Azimuth = -1;
432
                                        if(CAM_Orientation.UpdateMask & FORCE_AZIMUTH_ROTATION) ToFlightCtrl.Param.Byte[4] |= 0x01;     // allows Yawing without CareFree (Yawing at Coming Home)
433
                                        if(CAM_Orientation.UpdateMask & FORCE_AZIMUTH_ROTATION) ToFlightCtrl.Param.Byte[4] |= 0x01;     // allows Yawing without CareFree (Yawing at Coming Home)
433
                                        CAM_Orientation.UpdateMask &= ~(CAM_UPDATE_AZIMUTH | FORCE_AZIMUTH_ROTATION);
434
                                        CAM_Orientation.UpdateMask &= ~(CAM_UPDATE_AZIMUTH | FORCE_AZIMUTH_ROTATION);
434
                                }
435
                                }
435
                                else
436
                                else
436
                                {
437
                                {
437
                                        ToFlightCtrl.Param.sInt[4] = -1;
438
                                        ToFlightCtrl.Param.sInt[4] = -1;
438
                                }
439
                                }
439
 
440
 
440
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_NEW_CAMERA_ELEVATION, &tmp))  // Elevation set via 'j' command
441
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_NEW_CAMERA_ELEVATION, &tmp))  // Elevation set via 'j' command
441
                                {
442
                                {
442
                                        POI_KameraNick = tmp;
443
                                        POI_KameraNick = tmp;
443
                                }
444
                                }
444
                                else
445
                                else
445
                                {
446
                                {
446
                                        //if(FC.StatusFlags2 & FC_STATUS2_CAREFREE) // only, if carefree is active
447
                                        //if(FC.StatusFlags2 & FC_STATUS2_CAREFREE) // only, if carefree is active
447
                                        POI_KameraNick = CAM_Orientation.Elevation;
448
                                        POI_KameraNick = CAM_Orientation.Elevation;
448
                                        //else ToFlightCtrl.Param.sInt[5] = 0;
449
                                        //else ToFlightCtrl.Param.sInt[5] = 0;
449
                                }
450
                                }
450
                                ToFlightCtrl.Param.sInt[5] = POI_KameraNick;
451
                                ToFlightCtrl.Param.sInt[5] = POI_KameraNick;
451
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
452
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
452
                                if(BeepTime > 255 * 16) BeepTime = 255 * 16;
453
                                if(BeepTime > 255 * 16) BeepTime = 255 * 16;
453
                                ToFlightCtrl.Param.Byte[12] = BeepTime/16;  // set beeptime
454
                                ToFlightCtrl.Param.Byte[12] = BeepTime/16;  // set beeptime
454
BeepTime = 0; // reset local beeptime
455
BeepTime = 0; // reset local beeptime
455
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
456
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
456
if(NewExternalControlFrame)
457
if(NewExternalControlFrame)
457
 {
458
 {
458
  NewExternalControlFrame = 0;
459
  NewExternalControlFrame = 0;
459
  ToFlightCtrl.Param.Byte[4] |= 0x10; // Bit: there is External Control in the Data
460
  ToFlightCtrl.Param.Byte[4] |= 0x10; // Bit: there is External Control in the Data
460
  memcpy((u8 *) &(ToFlightCtrl.Param.Byte[13]), (u8 *) &ExternControl, 7); // 6 Bytes External Control
461
  memcpy((u8 *) &(ToFlightCtrl.Param.Byte[13]), (u8 *) &ExternControl, 7); // 6 Bytes External Control
461
 }
462
 }
462
                                break;
463
                                break;
463
 
464
 
464
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
465
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
465
                        case SPI_SERIAL_CH:
466
                        case SPI_SERIAL_CH:
466
                                memcpy((u8 *) &(ToFlightCtrl.Param.Byte[0]), (u8 *) &SerialChannel, 12); // 12 Bytes Serial Channels
467
                                memcpy((u8 *) &(ToFlightCtrl.Param.Byte[0]), (u8 *) &SerialChannel, 12); // 12 Bytes Serial Channels
467
                                NewSerialChannelFrame = 0;
468
                                NewSerialChannelFrame = 0;
468
                                break;
469
                                break;
469
                        case SPI_NCCMD_VERSION:
470
                        case SPI_NCCMD_VERSION:
470
                                //+++++++++++++++++++++++++++++++++++++++++++++++++++
471
                                //+++++++++++++++++++++++++++++++++++++++++++++++++++
471
                                //+ higher than the maximum allowed altitude
472
                                //+ higher than the maximum allowed altitude
472
                                //+++++++++++++++++++++++++++++++++++++++++++++++++++
473
                                //+++++++++++++++++++++++++++++++++++++++++++++++++++
473
                                ToFlightCtrl.Param.Byte[0] = VERSION_MAJOR;
474
                                ToFlightCtrl.Param.Byte[0] = VERSION_MAJOR;
474
                                ToFlightCtrl.Param.Byte[1] = VERSION_MINOR;
475
                                ToFlightCtrl.Param.Byte[1] = VERSION_MINOR;
475
                                ToFlightCtrl.Param.Byte[2] = VERSION_PATCH;
476
                                ToFlightCtrl.Param.Byte[2] = VERSION_PATCH;
476
                                ToFlightCtrl.Param.Byte[3] = FC_SPI_COMPATIBLE;
477
                                ToFlightCtrl.Param.Byte[3] = FC_SPI_COMPATIBLE;
477
                                ToFlightCtrl.Param.Byte[4] = Version_HW;
478
                                ToFlightCtrl.Param.Byte[4] = Version_HW;
478
                                ToFlightCtrl.Param.Byte[5] = DebugOut.StatusGreen;
479
                                ToFlightCtrl.Param.Byte[5] = DebugOut.StatusGreen;
479
                                ToFlightCtrl.Param.Byte[6] = DebugOut.StatusRed;
480
                                ToFlightCtrl.Param.Byte[6] = DebugOut.StatusRed;
480
                                ToFlightCtrl.Param.Byte[7] = ErrorCode;                          // muss in SPI_NCCMD_VERSION bleiben! (siehe oben)
481
                                ToFlightCtrl.Param.Byte[7] = ErrorCode;                          // muss in SPI_NCCMD_VERSION bleiben! (siehe oben)
481
                                ToFlightCtrl.Param.Byte[8] = NC_GPS_ModeCharacter;
482
                                ToFlightCtrl.Param.Byte[8] = NC_GPS_ModeCharacter;
482
                                ToFlightCtrl.Param.Byte[9] = SerialLinkOkay;
483
                                ToFlightCtrl.Param.Byte[9] = SerialLinkOkay;
483
                                ToFlightCtrl.Param.Byte[10] = NC_To_FC_Flags;
484
                                ToFlightCtrl.Param.Byte[10] = NC_To_FC_Flags;
484
                                ToFlightCtrl.Param.Byte[11] = SpeakHoTT;
485
                                ToFlightCtrl.Param.Byte[11] = SpeakHoTT;
485
                                ToFlightCtrl.Param.Byte[12] = RequestConfigFromFC & 0x01;
486
                                ToFlightCtrl.Param.Byte[12] = RequestConfigFromFC & 0x01;
486
                                SpeakHoTT = 0;
487
                                SpeakHoTT = 0;
487
//                              if(AbsoluteFlyingAltitude > 255) ToFlightCtrl.Param.Byte[11] = 0; // then the limitation of the FC doesn't work
488
//                              if(AbsoluteFlyingAltitude > 255) ToFlightCtrl.Param.Byte[11] = 0; // then the limitation of the FC doesn't work
488
//                              else ToFlightCtrl.Param.Byte[11] = AbsoluteFlyingAltitude;
489
//                              else ToFlightCtrl.Param.Byte[11] = AbsoluteFlyingAltitude;
489
                                break;
490
                                break;
490
                        case SPI_MISC:
491
                        case SPI_MISC:
491
                                ToFlightCtrl.Param.Byte[0] = EarthMagneticFieldFiltered/5;
492
                                ToFlightCtrl.Param.Byte[0] = EarthMagneticFieldFiltered/5;
492
                                ToFlightCtrl.Param.Byte[1] = EarthMagneticInclination;
493
                                ToFlightCtrl.Param.Byte[1] = EarthMagneticInclination;
493
                                ToFlightCtrl.Param.Byte[2] = EarthMagneticInclinationTheoretic;
494
                                ToFlightCtrl.Param.Byte[2] = EarthMagneticInclinationTheoretic;
494
                                ToFlightCtrl.Param.Byte[3] = 0;
495
                                ToFlightCtrl.Param.Byte[3] = 0;
495
                                ToFlightCtrl.Param.Byte[4] = NaviData.WaypointIndex; // index of current waypoints running from 0 to WaypointNumber-1
496
                                ToFlightCtrl.Param.Byte[4] = NaviData.WaypointIndex; // index of current waypoints running from 0 to WaypointNumber-1
496
                                ToFlightCtrl.Param.Byte[5] = NaviData.WaypointNumber; // number of stored waypoints
497
                                ToFlightCtrl.Param.Byte[5] = NaviData.WaypointNumber; // number of stored waypoints
497
                                ToFlightCtrl.Param.Int[3] =  NaviData.TargetPositionDeviation.Distance / 10;
498
                                ToFlightCtrl.Param.Int[3] =  NaviData.TargetPositionDeviation.Distance / 10;
498
                                ToFlightCtrl.Param.Byte[8] = NaviData.TargetHoldTime; // time in s to stay at the given target, counts down to 0 if target has been reached
499
                                ToFlightCtrl.Param.Byte[8] = NaviData.TargetHoldTime; // time in s to stay at the given target, counts down to 0 if target has been reached
499
                                ToFlightCtrl.Param.Byte[9] = ToFC_MaxWpListIndex;
500
                                ToFlightCtrl.Param.Byte[9] = ToFC_MaxWpListIndex;
500
                                ToFlightCtrl.Param.sInt[5] = GyroCompassCorrected / 10; // Bytes 10 & 11
501
                                ToFlightCtrl.Param.sInt[5] = GyroCompassCorrected / 10; // Bytes 10 & 11
501
                                break;
502
                                break;
502
 
503
 
503
                        case SPI_NCCMD_GPSINFO:
504
                        case SPI_NCCMD_GPSINFO:
504
                                ToFlightCtrl.Param.Byte[0] = GPSData.Flags;
505
                                ToFlightCtrl.Param.Byte[0] = GPSData.Flags;
505
                                ToFlightCtrl.Param.Byte[1] = GPSData.NumOfSats;
506
                                ToFlightCtrl.Param.Byte[1] = GPSData.NumOfSats;
506
                                ToFlightCtrl.Param.Byte[2] = GPSData.SatFix;
507
                                ToFlightCtrl.Param.Byte[2] = GPSData.SatFix;
507
                                ToFlightCtrl.Param.Byte[3] = GPSData.Speed_Ground / 100; // m/s
508
                                ToFlightCtrl.Param.Byte[3] = GPSData.Speed_Ground / 100; // m/s
508
                                ToFlightCtrl.Param.Int[2]  = NaviData.HomePositionDeviation.Distance; // dm   //4&5
509
                                ToFlightCtrl.Param.Int[2]  = NaviData.HomePositionDeviation.Distance; // dm   //4&5
509
                                ToFlightCtrl.Param.sInt[3] = NaviData.HomePositionDeviation.Bearing;  // deg  //6&7
510
                                ToFlightCtrl.Param.sInt[3] = NaviData.HomePositionDeviation.Bearing;  // deg  //6&7
510
                                if(FC_WP_EventChannel > 254) FC_WP_EventChannel = 254;    // Muss in SPI_NCCMD_GPSINFO bleiben! (siehe oben)
511
                                if(FC_WP_EventChannel > 254) FC_WP_EventChannel = 254;    // Muss in SPI_NCCMD_GPSINFO bleiben! (siehe oben)
511
                                if(FC_WP_EventChannel)
512
                                if(FC_WP_EventChannel)
512
                                 {
513
                                 {
513
                                  LogFC_WP_EventChannel = FC_WP_EventChannel; // to make sure that it will be logged
514
                                  LogFC_WP_EventChannel = FC_WP_EventChannel; // to make sure that it will be logged
514
                                  NaviData_WP.WP_Eventchannel = FC_WP_EventChannel; // to make sure that it will be logged
515
                                  NaviData_WP.WP_Eventchannel = FC_WP_EventChannel; // to make sure that it will be logged
515
  Out1TriggerUpdateBlocked = 3; // makes sure that the right trigger-pos is sent in command 18
516
  Out1TriggerUpdateBlocked = 3; // makes sure that the right trigger-pos is sent in command 18
516
  NaviData_Out1Trigger.Longitude = NaviData.CurrentPosition.Longitude;
517
  NaviData_Out1Trigger.Longitude = NaviData.CurrentPosition.Longitude;
517
  NaviData_Out1Trigger.Latitude = NaviData.CurrentPosition.Latitude;
518
  NaviData_Out1Trigger.Latitude = NaviData.CurrentPosition.Latitude;
518
                                 }
519
                                 }
519
                                FC_WP_EventChannel_Processed = 1;
520
                                FC_WP_EventChannel_Processed = 1;
520
//DebugOut.Analog[] = FC_WP_EventChannel;
521
//DebugOut.Analog[] = FC_WP_EventChannel;
521
  // ++++++++++++++++++++++++++++++++++
522
  // ++++++++++++++++++++++++++++++++++
522
  // Waypoint event +++++++++++++++++++
523
  // Waypoint event +++++++++++++++++++
523
  // ++++++++++++++++++++++++++++++++++
524
  // ++++++++++++++++++++++++++++++++++
524
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_WP_EVENT_ONCE, &tmp))
525
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_WP_EVENT_ONCE, &tmp))
525
                                {
526
                                {
526
                                        ToFlightCtrl.Param.Byte[8] = (s8)(tmp - 127);
527
                                        ToFlightCtrl.Param.Byte[8] = (s8)(tmp - 127);
527
                                        NCParams_ClearValue(NCPARAMS_WP_EVENT_ONCE);
528
                                        NCParams_ClearValue(NCPARAMS_WP_EVENT_ONCE);
528
                                        NCParams_ClearValue(NCPARAMS_WP_EVENT_FOREVER);
529
                                        NCParams_ClearValue(NCPARAMS_WP_EVENT_FOREVER);
529
                                }
530
                                }
530
                                else
531
                                else
531
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_WP_EVENT_FOREVER, &tmp))
532
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_WP_EVENT_FOREVER, &tmp))
532
                                {
533
                                {
533
                                        ToFlightCtrl.Param.Byte[8] = (s8)(tmp - 127);
534
                                        ToFlightCtrl.Param.Byte[8] = (s8)(tmp - 127);
534
                                        if(tmp == 0) NCParams_ClearValue(NCPARAMS_WP_EVENT_FOREVER);
535
                                        if(tmp == 0) NCParams_ClearValue(NCPARAMS_WP_EVENT_FOREVER);
535
                                }
536
                                }
536
                                else ToFlightCtrl.Param.Byte[8] = (s8)(FC_WP_EventChannel - 127);
537
                                else ToFlightCtrl.Param.Byte[8] = (s8)(FC_WP_EventChannel - 127);
537
//                              FC_WP_EventChannel = 0; // the GPS-Routine will set it again
538
//                              FC_WP_EventChannel = 0; // the GPS-Routine will set it again
538
  // ++++++++++++++++++++++++++++++++++
539
  // ++++++++++++++++++++++++++++++++++
539
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_ALTITUDE_RATE, &tmp))
540
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_ALTITUDE_RATE, &tmp))
540
                                {
541
                                {
541
                                        ToFlightCtrl.Param.Byte[9] = (u8)tmp;
542
                                        ToFlightCtrl.Param.Byte[9] = (u8)tmp;
542
                                }
543
                                }
543
                                else
544
                                else
544
                                {
545
                                {
545
                                        ToFlightCtrl.Param.Byte[9] = (u8)ToFC_AltitudeRate;
546
                                        ToFlightCtrl.Param.Byte[9] = (u8)ToFC_AltitudeRate;
546
                                }
547
                                }
547
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_ALTITUDE_SETPOINT, &tmp))
548
                                if(NCRARAM_STATE_VALID == NCParams_GetValue(NCPARAMS_ALTITUDE_SETPOINT, &tmp))
548
                                {
549
                                {
549
                                        ToFlightCtrl.Param.sInt[5] = tmp;
550
                                        ToFlightCtrl.Param.sInt[5] = tmp;
550
                                }
551
                                }
551
                                else
552
                                else
552
                                {
553
                                {
553
                                        ToFlightCtrl.Param.sInt[5] = (s16)ToFC_AltitudeSetpoint;
554
                                        ToFlightCtrl.Param.sInt[5] = (s16)ToFC_AltitudeSetpoint;
554
                                }
555
                                }
555
//DebugOut.Analog[] = ToFlightCtrl.Param.Byte[8];
556
//DebugOut.Analog[] = ToFlightCtrl.Param.Byte[8];
556
                                break;
557
                                break;
557
                        case SPI_NCCMD_HOTT_INFO:
558
                        case SPI_NCCMD_HOTT_INFO:
558
                                if(NewWPL_Name) hott_index = 100;
559
                                if(NewWPL_Name) hott_index = 100;
559
                                switch(hott_index++)
560
                                switch(hott_index++)
560
                                {
561
                                {
561
                                case 0:
562
                                case 0:
562
                                        //Dezimalgrad            --> Grad mit Dezimalminuten     --> Grad, Minuten, Sekunden
563
                                        //Dezimalgrad            --> Grad mit Dezimalminuten     --> Grad, Minuten, Sekunden
563
                                        //53.28 5788 7.4847269   --> N53° 17.14728 E7° 29.08362  --> N53° 17' 8.837" E7° 29' 5.017"
564
                                        //53.28 5788 7.4847269   --> N53° 17.14728 E7° 29.08362  --> N53° 17' 8.837" E7° 29' 5.017"
564
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GPS_PACKET_ID;
565
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GPS_PACKET_ID;
565
                                        ToFlightCtrl.Param.Byte[0] = 3+3;       // index  +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
566
                                        ToFlightCtrl.Param.Byte[0] = 3+3;       // index  +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
566
                                        ToFlightCtrl.Param.Byte[1] = 9-1;       // how many
567
                                        ToFlightCtrl.Param.Byte[1] = 9-1;       // how many
567
                                        //-----------------------------
568
                                        //-----------------------------
568
                                        ToFlightCtrl.Param.Byte[2] = GyroCompassCorrected / 20;//NaviData.HomePositionDeviation.Bearing / 2;
569
                                        ToFlightCtrl.Param.Byte[2] = GyroCompassCorrected / 20;//NaviData.HomePositionDeviation.Bearing / 2;
569
                                        i1 = GPSData.Speed_Ground; // in cm/sec
570
                                        i1 = GPSData.Speed_Ground; // in cm/sec
570
                                        i1 *= 36;
571
                                        i1 *= 36;
571
                                        i1 /= 1000;
572
                                        i1 /= 1000;
572
                                        ToFlightCtrl.Param.Byte[3] = i1 % 256;
573
                                        ToFlightCtrl.Param.Byte[3] = i1 % 256;
573
                                        ToFlightCtrl.Param.Byte[4] = i1 / 256;
574
                                        ToFlightCtrl.Param.Byte[4] = i1 / 256;
574
                                        //-----------------------------
575
                                        //-----------------------------
575
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5]  = 1; // 1 = S
576
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5]  = 1; // 1 = S
576
                                        else ToFlightCtrl.Param.Byte[5]  = 0; // 1 = S
577
                                        else ToFlightCtrl.Param.Byte[5]  = 0; // 1 = S
577
                                        i1 = abs(GPSData.Position.Latitude)/10000000L;
578
                                        i1 = abs(GPSData.Position.Latitude)/10000000L;
578
                                        i2 = abs(GPSData.Position.Latitude)%10000000L;
579
                                        i2 = abs(GPSData.Position.Latitude)%10000000L;
579
                                        if(!(NCFlags & NC_FLAG_GPS_OK)) {i1 = 0; i2 = 0;}
580
                                        if(!(NCFlags & NC_FLAG_GPS_OK)) {i1 = 0; i2 = 0;}
580
                                        i1 *= 100;
581
                                        i1 *= 100;
581
                                // Minuten
582
                                // Minuten
582
                                i2 *= 6;
583
                                i2 *= 6;
583
                                i2 /= 10;
584
                                i2 /= 10;
584
                                        i1 += i2 / 100000;
585
                                        i1 += i2 / 100000;
585
                                        i2  = i2 % 100000;
586
                                        i2  = i2 % 100000;
586
                                        i2 /= 10;
587
                                        i2 /= 10;
587
                                        ToFlightCtrl.Param.Byte[6]  = i1 % 256;
588
                                        ToFlightCtrl.Param.Byte[6]  = i1 % 256;
588
                                        ToFlightCtrl.Param.Byte[7]  = i1 / 256;
589
                                        ToFlightCtrl.Param.Byte[7]  = i1 / 256;
589
                                        ToFlightCtrl.Param.Byte[8]  = i2 % 256;
590
                                        ToFlightCtrl.Param.Byte[8]  = i2 % 256;
590
                                        ToFlightCtrl.Param.Byte[9]  = i2 / 256;
591
                                        ToFlightCtrl.Param.Byte[9]  = i2 / 256;
591
                                        break;
592
                                        break;
592
                                case 1:
593
                                case 1:
593
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GPS_PACKET_ID;
594
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GPS_PACKET_ID;
594
                                        ToFlightCtrl.Param.Byte[0] = 11+3;      // index          +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
595
                                        ToFlightCtrl.Param.Byte[0] = 11+3;      // index          +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
595
                                        ToFlightCtrl.Param.Byte[1] = 8-1;       // how many
596
                                        ToFlightCtrl.Param.Byte[1] = 8-1;       // how many
596
                                        //-----------------------------
597
                                        //-----------------------------
597
                                        if(GPSData.Position.Longitude < 0) ToFlightCtrl.Param.Byte[2]  = 1; // 1 = E
598
                                        if(GPSData.Position.Longitude < 0) ToFlightCtrl.Param.Byte[2]  = 1; // 1 = E
598
                                        else ToFlightCtrl.Param.Byte[2]  = 0; // 1 = S
599
                                        else ToFlightCtrl.Param.Byte[2]  = 0; // 1 = S
599
                                        i1 = abs(GPSData.Position.Longitude)/10000000L;
600
                                        i1 = abs(GPSData.Position.Longitude)/10000000L;
600
                                        i2 = abs(GPSData.Position.Longitude)%10000000L;
601
                                        i2 = abs(GPSData.Position.Longitude)%10000000L;
601
                                        if(!(NCFlags & NC_FLAG_GPS_OK)) {i1 = 0; i2 = 0;}
602
                                        if(!(NCFlags & NC_FLAG_GPS_OK)) {i1 = 0; i2 = 0;}
602
                                        i1 *= 100;
603
                                        i1 *= 100;
603
                                // Minuten
604
                                // Minuten
604
                                i2 *= 6;
605
                                i2 *= 6;
605
                                i2 /= 10;
606
                                i2 /= 10;
606
                                        i1 += i2 / 100000;
607
                                        i1 += i2 / 100000;
607
                                        i2  = i2 % 100000;
608
                                        i2  = i2 % 100000;
608
                                        i2 /= 10;
609
                                        i2 /= 10;
609
                                        ToFlightCtrl.Param.Byte[3]  = i1 % 256;
610
                                        ToFlightCtrl.Param.Byte[3]  = i1 % 256;
610
                                        ToFlightCtrl.Param.Byte[4]  = i1 / 256;
611
                                        ToFlightCtrl.Param.Byte[4]  = i1 / 256;
611
                                        ToFlightCtrl.Param.Byte[5]  = i2 % 256;
612
                                        ToFlightCtrl.Param.Byte[5]  = i2 % 256;
612
                                        ToFlightCtrl.Param.Byte[6]  = i2 / 256;
613
                                        ToFlightCtrl.Param.Byte[6]  = i2 / 256;
613
                                        //-----------------------------
614
                                        //-----------------------------
614
                                        i1 = NaviData.HomePositionDeviation.Distance / 10; // dann in m
615
                                        i1 = NaviData.HomePositionDeviation.Distance / 10; // dann in m
615
                                        ToFlightCtrl.Param.Byte[7]  = i1 % 256;
616
                                        ToFlightCtrl.Param.Byte[7]  = i1 % 256;
616
                                        ToFlightCtrl.Param.Byte[8]  = i1 / 256;
617
                                        ToFlightCtrl.Param.Byte[8]  = i1 / 256;
617
                                        break;
618
                                        break;
618
                                 case 2:
619
                                 case 2:
619
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GENERAL_PACKET_ID;
620
                                        ToFlightCtrl.Param.Byte[11] = HOTT_GENERAL_PACKET_ID;
620
                                        ToFlightCtrl.Param.Byte[0] = 5+3;       // index   // +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
621
                                        ToFlightCtrl.Param.Byte[0] = 5+3;       // index   // +3, weil bei HoTT V4 3 Bytes eingeschoben wurden
621
                                        ToFlightCtrl.Param.Byte[1] = 2; // how many
622
                                        ToFlightCtrl.Param.Byte[1] = 2; // how many
622
                                        ToFlightCtrl.Param.Byte[2] = EarthMagneticField / (5 * 2);
623
                                        ToFlightCtrl.Param.Byte[2] = EarthMagneticField / (5 * 2);
623
                                        ToFlightCtrl.Param.Byte[3] = EarthMagneticInclination / 2;
624
                                        ToFlightCtrl.Param.Byte[3] = EarthMagneticInclination / 2;
624
                                        break;
625
                                        break;
625
                                 case 3:
626
                                 case 3:
626
                                        ToFlightCtrl.Param.Byte[11] = JETI_GPS_PACKET_ID1;
627
                                        ToFlightCtrl.Param.Byte[11] = JETI_GPS_PACKET_ID1;
627
                                        ToFlightCtrl.Param.Byte[0] = 0; // index
628
                                        ToFlightCtrl.Param.Byte[0] = 0; // index
628
                                        ToFlightCtrl.Param.Byte[1] = 4; // how many
629
                                        ToFlightCtrl.Param.Byte[1] = 4; // how many
629
                                        //JetiExData[14].Value =  53 * 0x10000 + 23467;
630
                                        //JetiExData[14].Value =  53 * 0x10000 + 23467;
630
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5] = 0x40;
631
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5] = 0x40;
631
                                        else ToFlightCtrl.Param.Byte[5] = 0x00;
632
                                        else ToFlightCtrl.Param.Byte[5] = 0x00;
632
                                        ToFlightCtrl.Param.Byte[4] = abs(GPSData.Position.Latitude)/10000000L;
633
                                        ToFlightCtrl.Param.Byte[4] = abs(GPSData.Position.Latitude)/10000000L;
633
                                        i2 = abs(GPSData.Position.Latitude)%10000000L;
634
                                        i2 = abs(GPSData.Position.Latitude)%10000000L;
634
                                        i2 *= 6;
635
                                        i2 *= 6;
635
                                        i2 /= 1000;
636
                                        i2 /= 1000;
636
                                        ToFlightCtrl.Param.Byte[3] = i2 / 256;
637
                                        ToFlightCtrl.Param.Byte[3] = i2 / 256;
637
                                        ToFlightCtrl.Param.Byte[2] = i2 % 256;
638
                                        ToFlightCtrl.Param.Byte[2] = i2 % 256;
638
                                        break;
639
                                        break;
639
                                 case 4:
640
                                 case 4:
640
                                        ToFlightCtrl.Param.Byte[11] = JETI_GPS_PACKET_ID2;
641
                                        ToFlightCtrl.Param.Byte[11] = JETI_GPS_PACKET_ID2;
641
                                        ToFlightCtrl.Param.Byte[0] = 0; // index
642
                                        ToFlightCtrl.Param.Byte[0] = 0; // index
642
                                        ToFlightCtrl.Param.Byte[1] = 4; // how many
643
                                        ToFlightCtrl.Param.Byte[1] = 4; // how many
643
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5] = 0x60;
644
                                        if(GPSData.Position.Latitude < 0) ToFlightCtrl.Param.Byte[5] = 0x60;
644
                                        else ToFlightCtrl.Param.Byte[5] = 0x20;
645
                                        else ToFlightCtrl.Param.Byte[5] = 0x20;
645
                                        ToFlightCtrl.Param.Byte[4] = abs(GPSData.Position.Longitude)/10000000L;
646
                                        ToFlightCtrl.Param.Byte[4] = abs(GPSData.Position.Longitude)/10000000L;
646
                                        i2 = abs(GPSData.Position.Longitude)%10000000L;
647
                                        i2 = abs(GPSData.Position.Longitude)%10000000L;
647
                                        i2 *= 6;
648
                                        i2 *= 6;
648
                                        i2 /= 1000;
649
                                        i2 /= 1000;
649
                                        ToFlightCtrl.Param.Byte[3] = i2 / 256;
650
                                        ToFlightCtrl.Param.Byte[3] = i2 / 256;
650
                                        ToFlightCtrl.Param.Byte[2] = i2 % 256;
651
                                        ToFlightCtrl.Param.Byte[2] = i2 % 256;
651
                        hott_index = 0;
652
                        hott_index = 0;
652
                                        break;
653
                                        break;
653
 
654
 
654
                                        case 100:
655
                                        case 100:
655
                                        ToFlightCtrl.Param.Byte[11] = HOTT_WPL_NAME;
656
                                        ToFlightCtrl.Param.Byte[11] = HOTT_WPL_NAME;
656
                                        ToFlightCtrl.Param.Byte[0] = 0; // index
657
                                        ToFlightCtrl.Param.Byte[0] = 0; // index
657
                                        ToFlightCtrl.Param.Byte[2] = WPL_Store.Name[0];
658
                                        ToFlightCtrl.Param.Byte[2] = WPL_Store.Name[0];
658
                                        ToFlightCtrl.Param.Byte[3] = WPL_Store.Name[1];
659
                                        ToFlightCtrl.Param.Byte[3] = WPL_Store.Name[1];
659
                                        ToFlightCtrl.Param.Byte[4] = WPL_Store.Name[2];
660
                                        ToFlightCtrl.Param.Byte[4] = WPL_Store.Name[2];
660
                                        ToFlightCtrl.Param.Byte[5] = WPL_Store.Name[3];
661
                                        ToFlightCtrl.Param.Byte[5] = WPL_Store.Name[3];
661
                                        ToFlightCtrl.Param.Byte[6] = WPL_Store.Name[4];
662
                                        ToFlightCtrl.Param.Byte[6] = WPL_Store.Name[4];
662
                                        ToFlightCtrl.Param.Byte[7] = WPL_Store.Name[5];
663
                                        ToFlightCtrl.Param.Byte[7] = WPL_Store.Name[5];
663
                                        ToFlightCtrl.Param.Byte[8] = WPL_Store.Name[6];
664
                                        ToFlightCtrl.Param.Byte[8] = WPL_Store.Name[6];
664
                                        ToFlightCtrl.Param.Byte[9] = WPL_Store.Name[7];
665
                                        ToFlightCtrl.Param.Byte[9] = WPL_Store.Name[7];
665
                                        ToFlightCtrl.Param.Byte[10]= WPL_Store.Name[8];
666
                                        ToFlightCtrl.Param.Byte[10]= WPL_Store.Name[8];
666
                                        ToFlightCtrl.Param.Byte[1] = 9; // how many
667
                                        ToFlightCtrl.Param.Byte[1] = 9; // how many
667
                                        NewWPL_Name = 0;
668
                                        NewWPL_Name = 0;
668
                                        hott_index = 0;
669
                                        hott_index = 0;
669
                                        break;
670
                                        break;
670
                                 default:
671
                                 default:
671
                                        ToFlightCtrl.Param.Byte[0] = 255;
672
                                        ToFlightCtrl.Param.Byte[0] = 255;
672
                                        hott_index = 0;
673
                                        hott_index = 0;
673
                                        break;
674
                                        break;
674
                                }
675
                                }
675
                                break;
676
                                break;
676
                        default:
677
                        default:
677
                                break;
678
                                break;
678
// 0 = 0,1
679
// 0 = 0,1
679
// 1 = 2,3
680
// 1 = 2,3
680
// 2 = 4,5
681
// 2 = 4,5
681
// 3 = 6,7
682
// 3 = 6,7
682
// 4 = 8,9
683
// 4 = 8,9
683
// 5 = 10,11
684
// 5 = 10,11
684
                }
685
                }
685
                VIC_ITCmd(SSP0_ITLine, ENABLE);         // enable SPI interrupt
686
                VIC_ITCmd(SSP0_ITLine, ENABLE);         // enable SPI interrupt
686
                switch(FromFlightCtrl.Command)
687
                switch(FromFlightCtrl.Command)
687
                {
688
                {
688
                        case SPI_FCCMD_USER:
689
                        case SPI_FCCMD_USER:
689
                                Parameter.User1 = FromFlightCtrl.Param.Byte[0];
690
                                Parameter.User1 = FromFlightCtrl.Param.Byte[0];
690
                                Parameter.User2 = FromFlightCtrl.Param.Byte[1];
691
                                Parameter.User2 = FromFlightCtrl.Param.Byte[1];
691
                                Parameter.User3 = FromFlightCtrl.Param.Byte[2];
692
                                Parameter.User3 = FromFlightCtrl.Param.Byte[2];
692
                                Parameter.User4 = FromFlightCtrl.Param.Byte[3];
693
                                Parameter.User4 = FromFlightCtrl.Param.Byte[3];
693
                                Parameter.User5 = FromFlightCtrl.Param.Byte[4];
694
                                Parameter.User5 = FromFlightCtrl.Param.Byte[4];
694
                                Parameter.User6 = FromFlightCtrl.Param.Byte[5];
695
                                Parameter.User6 = FromFlightCtrl.Param.Byte[5];
695
                                Parameter.User7 = FromFlightCtrl.Param.Byte[6];
696
                                Parameter.User7 = FromFlightCtrl.Param.Byte[6];
696
                                Parameter.User8 = FromFlightCtrl.Param.Byte[7];
697
                                Parameter.User8 = FromFlightCtrl.Param.Byte[7];
697
                                FC.RealStatusFlags = FromFlightCtrl.Param.Byte[8];
698
                                FC.RealStatusFlags = FromFlightCtrl.Param.Byte[8];
698
                                if(FC.RealStatusFlags & FC_STATUS_MOTOR_RUN) SimulationFlags = 0; // stop the simulation if the motors would really start
699
                                if(FC.RealStatusFlags & FC_STATUS_MOTOR_RUN) SimulationFlags = 0; // stop the simulation if the motors would really start
699
 
700
 
700
                                if(!(SimulationFlags & SIMULATION_ACTIVE))
701
                                if(!(SimulationFlags & SIMULATION_ACTIVE))
701
                                {
702
                                {
702
                                        if(ClearFCStatusFlags)
703
                                        if(ClearFCStatusFlags)
703
                                        {
704
                                        {
704
                                                FC.StatusFlags = 0;
705
                                                FC.StatusFlags = 0;
705
                                                ClearFCStatusFlags = 0;
706
                                                ClearFCStatusFlags = 0;
706
                                        }
707
                                        }
707
                                }
708
                                }
708
                                FC.StatusFlags |= FC.RealStatusFlags;
709
                                FC.StatusFlags |= FC.RealStatusFlags;
709
                                if(FC.StatusFlags & FC_STATUS_CALIBRATE && !FCCalibActive)
710
                                if(FC.StatusFlags & FC_STATUS_CALIBRATE && !FCCalibActive)
710
                                {
711
                                {
711
                                        HeadFreeStartAngle = (3600 + Compass_Heading * 10 + GeoMagDec) % 3600;
712
                                        HeadFreeStartAngle = (3600 + Compass_Heading * 10 + GeoMagDec) % 3600;
712
                                        CompassDirectionAtMotorStart = HeadFreeStartAngle;
713
                                        CompassDirectionAtMotorStart = HeadFreeStartAngle;
713
                                        Compass_Init();
714
                                        Compass_Init();
714
                                        FCCalibActive = 15;
715
                                        FCCalibActive = 15;
715
                                        FC_is_Calibrated = 0;
716
                                        FC_is_Calibrated = 0;
716
                                        FreqNewGpsData = 50;
717
                                        FreqNewGpsData = 50;
717
                                }
718
                                }
718
                                else
719
                                else
719
                                {
720
                                {
720
                                        if(FCCalibActive)
721
                                        if(FCCalibActive)
721
                                        {
722
                                        {
722
                                                if(--FCCalibActive == 0)
723
                                                if(--FCCalibActive == 0)
723
                                                {
724
                                                {
724
                                                        FC_is_Calibrated = 1;
725
                                                        FC_is_Calibrated = 1;
725
                                                        Compass_Check();
726
                                                        Compass_Check();
726
                                                }
727
                                                }
727
                                        }
728
                                        }
728
                                }
729
                                }
729
                                if(FC.StatusFlags & FC_STATUS_START)
730
                                if(FC.StatusFlags & FC_STATUS_START)
730
                                {
731
                                {
731
                                        if(Compass_Heading != -1) HeadFreeStartAngle = (3600 + Compass_Heading * 10 + GeoMagDec) % 3600;
732
                                        if(Compass_Heading != -1) HeadFreeStartAngle = (3600 + Compass_Heading * 10 + GeoMagDec) % 3600;
732
                                        else HeadFreeStartAngle = GyroCompassCorrected;
733
                                        else HeadFreeStartAngle = GyroCompassCorrected;
733
                                        CompassDirectionAtMotorStart = HeadFreeStartAngle;
734
                                        CompassDirectionAtMotorStart = HeadFreeStartAngle;
734
                                }
735
                                }
735
 
736
 
736
                                if((Parameter.ExtraConfig & CFG_TEACHABLE_CAREFREE))
737
                                if((Parameter.ExtraConfig & CFG_TEACHABLE_CAREFREE))
737
                                {
738
                                {
738
                                        if(!(FC.StatusFlags2 & FC_STATUS2_CAREFREE)) // CF ist jetzt ausgeschaltet -> neue Richtung lernen
739
                                        if(!(FC.StatusFlags2 & FC_STATUS2_CAREFREE)) // CF ist jetzt ausgeschaltet -> neue Richtung lernen
739
                                        {
740
                                        {
740
                                                if((NaviData.HomePositionDeviation.Distance > 200) && (NCFlags & NC_FLAG_GPS_OK))        // nur bei ausreichender Distance -> 20m
741
                                                if((NaviData.HomePositionDeviation.Distance > 200) && (NCFlags & NC_FLAG_GPS_OK))        // nur bei ausreichender Distance -> 20m
741
                                                {
742
                                                {
742
                                                        HeadFreeStartAngle = (10 * NaviData.HomePositionDeviation.Bearing + 1800 + 3600 -  Parameter.OrientationAngle * 150) % 3600; // in 0.1°
743
                                                        HeadFreeStartAngle = (10 * NaviData.HomePositionDeviation.Bearing + 1800 + 3600 -  Parameter.OrientationAngle * 150) % 3600; // in 0.1°
743
                                                }
744
                                                }
744
                                                else                                                                                             // Ansonsten die aktuelle Richtung übernehmen
745
                                                else                                                                                             // Ansonsten die aktuelle Richtung übernehmen
745
                                                HeadFreeStartAngle = GyroCompassCorrected; // in 0.1°
746
                                                HeadFreeStartAngle = GyroCompassCorrected; // in 0.1°
746
                                        }
747
                                        }
747
                                }
748
                                }
748
                                NaviData.FCStatusFlags = FC.StatusFlags;
749
                                NaviData.FCStatusFlags = FC.StatusFlags;
749
                                if(FC.StatusFlags2 & FC_STATUS2_WAIT_FOR_TAKEOFF) NaviData.FCStatusFlags &= ~FC_STATUS_FLY;
750
                                if(FC.StatusFlags2 & FC_STATUS2_WAIT_FOR_TAKEOFF) NaviData.FCStatusFlags &= ~FC_STATUS_FLY;
750
                                FC.StatusFlags2 = FromFlightCtrl.Param.Byte[9];
751
                                FC.StatusFlags2 = FromFlightCtrl.Param.Byte[9];
751
                                NaviData.FCStatusFlags2 = (NaviData.FCStatusFlags2 & (FC_STATUS2_OUT1_ACTIVE | FC_STATUS2_OUT2_ACTIVE)) | (FC.StatusFlags2 & (0xff - (FC_STATUS2_OUT1_ACTIVE | FC_STATUS2_OUT2_ACTIVE)));
752
                                NaviData.FCStatusFlags2 = (NaviData.FCStatusFlags2 & (FC_STATUS2_OUT1_ACTIVE | FC_STATUS2_OUT2_ACTIVE)) | (FC.StatusFlags2 & (0xff - (FC_STATUS2_OUT1_ACTIVE | FC_STATUS2_OUT2_ACTIVE)));
752
                                if((!(LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) && (FC.StatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) NaviData.FCStatusFlags2 |= FC_STATUS2_OUT1_ACTIVE;
753
                                if((!(LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) && (FC.StatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) NaviData.FCStatusFlags2 |= FC_STATUS2_OUT1_ACTIVE;
753
                                else
754
                                else
754
                                if(((LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) && !(FC.StatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) NaviData.FCStatusFlags2 &= ~FC_STATUS2_OUT1_ACTIVE;
755
                                if(((LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) && !(FC.StatusFlags2 & FC_STATUS2_OUT1_ACTIVE)) NaviData.FCStatusFlags2 &= ~FC_STATUS2_OUT1_ACTIVE;
755
 
756
 
756
                                if((!(LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) && (FC.StatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) NaviData.FCStatusFlags2 |= FC_STATUS2_OUT2_ACTIVE;
757
                                if((!(LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) && (FC.StatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) NaviData.FCStatusFlags2 |= FC_STATUS2_OUT2_ACTIVE;
757
                                else
758
                                else
758
                                if(((LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) && !(FC.StatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) NaviData.FCStatusFlags2 &= ~FC_STATUS2_OUT2_ACTIVE;
759
                                if(((LastTransmittedFCStatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) && !(FC.StatusFlags2 & FC_STATUS2_OUT2_ACTIVE)) NaviData.FCStatusFlags2 &= ~FC_STATUS2_OUT2_ACTIVE;
759
//DebugOut.Analog[] = (NaviData.FCStatusFlags2 & FC_STATUS2_OUT1_ACTIVE);
760
//DebugOut.Analog[] = (NaviData.FCStatusFlags2 & FC_STATUS2_OUT1_ACTIVE);
760
                                Logging_FCStatusFlags1 |= FC.StatusFlags;
761
                                Logging_FCStatusFlags1 |= FC.StatusFlags;
761
                                Logging_FCStatusFlags2 |= FC.StatusFlags2;
762
                                Logging_FCStatusFlags2 |= FC.StatusFlags2;
762
                                FC.BAT_Voltage = FromFlightCtrl.Param.Int[5]; // 10 & 11
763
                                FC.BAT_Voltage = FromFlightCtrl.Param.Int[5]; // 10 & 11
763
                                DebugOut.Analog[7] = FC.BAT_Voltage;
764
                                DebugOut.Analog[7] = FC.BAT_Voltage;
764
                                DebugOut.Analog[5] = FC.StatusFlags;
765
                                DebugOut.Analog[5] = FC.StatusFlags;
765
                                NaviData.UBat = (u8) FC.BAT_Voltage;     // Achtung: die (u8) NaviData.UBat kann überlaufen -> das KopterTool müsste dann 25,5V drauf rechnen
766
                                NaviData.UBat = (u8) FC.BAT_Voltage;     // Achtung: die (u8) NaviData.UBat kann überlaufen -> das KopterTool müsste dann 25,5V drauf rechnen
766
                                break;
767
                                break;
767
                        case SPI_FCCMD_BL_ACCU:
768
                        case SPI_FCCMD_BL_ACCU:
768
                                FC.BAT_Current = FromFlightCtrl.Param.Int[0];
769
                                FC.BAT_Current = FromFlightCtrl.Param.Int[0];
769
                                DebugOut.Analog[8] = FC.BAT_Current;
770
                                DebugOut.Analog[8] = FC.BAT_Current;
770
                                BL_MinOfMaxPWM = FromFlightCtrl.Param.Byte[3];  
771
                                BL_MinOfMaxPWM = FromFlightCtrl.Param.Byte[3];  
771
                                if(BL_MinOfMaxPWM < Logging_BL_MinOfMaxPWM)     Logging_BL_MinOfMaxPWM = BL_MinOfMaxPWM;        // hold the value until logged
772
                                if(BL_MinOfMaxPWM < Logging_BL_MinOfMaxPWM)     Logging_BL_MinOfMaxPWM = BL_MinOfMaxPWM;        // hold the value until logged
772
                                if(BL_MinOfMaxPWM < ErrorCheck_BL_MinOfMaxPWM)  ErrorCheck_BL_MinOfMaxPWM = BL_MinOfMaxPWM;             // hold the value until Error processed
773
                                if(BL_MinOfMaxPWM < ErrorCheck_BL_MinOfMaxPWM)  ErrorCheck_BL_MinOfMaxPWM = BL_MinOfMaxPWM;             // hold the value until Error processed
773
                                Parameter.NaviGpsModeControl = FromFlightCtrl.Param.Byte[4];
774
                                Parameter.NaviGpsModeControl = FromFlightCtrl.Param.Byte[4];
774
                                FromFC_VarioCharacter = FromFlightCtrl.Param.Byte[5];
775
                                FromFC_VarioCharacter = FromFlightCtrl.Param.Byte[5];
775
//0x40
776
//0x40
776
//0x20
777
//0x20
777
//0x10
778
//0x10
778
if(FromFlightCtrl.Param.Byte[2] & 0x80) // this Flag marks a changed Out1
779
if(FromFlightCtrl.Param.Byte[2] & 0x80) // this Flag marks a changed Out1
779
 {
780
 {
780
  Out1TriggerUpdateNewData = 1;
781
  Out1TriggerUpdateNewData = 1;
781
  if(!Out1TriggerUpdateBlocked)
782
  if(!Out1TriggerUpdateBlocked)
782
   {
783
   {
783
                NaviData_Out1Trigger.Longitude = NaviData.CurrentPosition.Longitude;
784
                NaviData_Out1Trigger.Longitude = NaviData.CurrentPosition.Longitude;
784
                NaviData_Out1Trigger.Latitude = NaviData.CurrentPosition.Latitude;
785
                NaviData_Out1Trigger.Latitude = NaviData.CurrentPosition.Latitude;
785
   }
786
   }
786
  Out1TriggerUpdateBlocked = 0;
787
  Out1TriggerUpdateBlocked = 0;
787
 }
788
 }
788
if(Out1TriggerUpdateBlocked) Out1TriggerUpdateBlocked--;
789
if(Out1TriggerUpdateBlocked) Out1TriggerUpdateBlocked--;
789
                                index = FromFlightCtrl.Param.Byte[2] & 0x0f; //MAX_MOTORS
790
                                index = FromFlightCtrl.Param.Byte[2] & 0x0f; //MAX_MOTORS
790
                                if(AmountOfMotors < index+1) AmountOfMotors = index+1;
791
                                if(AmountOfMotors < index+1) AmountOfMotors = index+1;
791
                                Motor[index].NotReadyCnt = FromFlightCtrl.Param.Byte[6];
792
                                Motor[index].NotReadyCnt = FromFlightCtrl.Param.Byte[6];
792
                                Motor_Version[index] = FromFlightCtrl.Param.Byte[7];
793
                                Motor_Version[index] = FromFlightCtrl.Param.Byte[7];
793
                                Motor[index].MaxPWM = FromFlightCtrl.Param.Byte[8];
794
                                Motor[index].MaxPWM = FromFlightCtrl.Param.Byte[8];
794
                                Motor[index].State = FromFlightCtrl.Param.Byte[9];
795
                                Motor[index].State = FromFlightCtrl.Param.Byte[9];
795
                                Motor[index].Temperature = FromFlightCtrl.Param.Byte[10];
796
                                Motor[index].Temperature = FromFlightCtrl.Param.Byte[10];
796
                                Motor[index].Current = FromFlightCtrl.Param.Byte[11];
797
                                Motor[index].Current = FromFlightCtrl.Param.Byte[11];
797
                                if(FromFC_VarioCharacter == '+' || FromFC_VarioCharacter == '-') // manual setpoint clears the NC-Parameter command
798
                                if(FromFC_VarioCharacter == '+' || FromFC_VarioCharacter == '-') // manual setpoint clears the NC-Parameter command
798
                                {
799
                                {
799
                                        NCParams_ClearValue(NCPARAMS_ALTITUDE_RATE);
800
                                        NCParams_ClearValue(NCPARAMS_ALTITUDE_RATE);
800
                                }
801
                                }
801
                                if(!(SimulationFlags & SIMULATION_ACTIVE)) NaviData.Current = FC.BAT_Current;
802
                                if(!(SimulationFlags & SIMULATION_ACTIVE)) NaviData.Current = FC.BAT_Current;
802
                                NaviData.UsedCapacity = FC.BAT_UsedCapacity;
803
                                NaviData.UsedCapacity = FC.BAT_UsedCapacity;
803
                                FromFlightCtrl_AccNick = FromFlightCtrl.Param.sInt[6];
804
                                FromFlightCtrl_AccNick = FromFlightCtrl.Param.sInt[6];
804
                                FromFlightCtrl_AccRoll = FromFlightCtrl.Param.sInt[7];
805
                                FromFlightCtrl_AccRoll = FromFlightCtrl.Param.sInt[7];
805
                                DebugOut.Analog[2] = FromFlightCtrl_AccNick;
806
                                DebugOut.Analog[2] = FromFlightCtrl_AccNick;
806
                                DebugOut.Analog[3] = FromFlightCtrl_AccRoll;
807
                                DebugOut.Analog[3] = FromFlightCtrl_AccRoll;
807
                                break;
808
                                break;
808
                        case SPI_FCCMD_PARAMETER1:
809
                        case SPI_FCCMD_PARAMETER1:
809
                                Parameter.LowVoltageWarning = FromFlightCtrl.Param.Byte[0];
810
                                Parameter.LowVoltageWarning = FromFlightCtrl.Param.Byte[0];
810
                                CHK_POTI_MM(Parameter.NaviGpsGain,FromFlightCtrl.Param.Byte[1],0,255);
811
                                CHK_POTI_MM(Parameter.NaviGpsGain,FromFlightCtrl.Param.Byte[1],0,255);
811
                                CHK_POTI_MM(Parameter.NaviGpsP,FromFlightCtrl.Param.Byte[2],0,255);
812
                                CHK_POTI_MM(Parameter.NaviGpsP,FromFlightCtrl.Param.Byte[2],0,255);
812
                                CHK_POTI_MM(Parameter.NaviGpsI,FromFlightCtrl.Param.Byte[3],0,255);
813
                                CHK_POTI_MM(Parameter.NaviGpsI,FromFlightCtrl.Param.Byte[3],0,255);
813
                                CHK_POTI_MM(Parameter.NaviGpsD,FromFlightCtrl.Param.Byte[4],0,255);
814
                                CHK_POTI_MM(Parameter.NaviGpsD,FromFlightCtrl.Param.Byte[4],0,255);
814
                                CHK_POTI_MM(Parameter.NaviGpsACC,FromFlightCtrl.Param.Byte[5],0,255);
815
                                CHK_POTI_MM(Parameter.NaviGpsACC,FromFlightCtrl.Param.Byte[5],0,255);
815
                                Parameter.NaviGpsMinSat = FromFlightCtrl.Param.Byte[6];
816
                                Parameter.NaviGpsMinSat = FromFlightCtrl.Param.Byte[6];
816
                                Parameter.NaviStickThreshold = FromFlightCtrl.Param.Byte[7];
817
                                Parameter.NaviStickThreshold = FromFlightCtrl.Param.Byte[7];
817
//                              CHK_POTI_MM(Parameter.NaviOperatingRadius,FromFlightCtrl.Param.Byte[8],0,255);
818
//                              CHK_POTI_MM(Parameter.NaviOperatingRadius,FromFlightCtrl.Param.Byte[8],0,255);
818
                                Parameter.NaviMaxFlyingRange = FromFlightCtrl.Param.Byte[8];
819
                                Parameter.NaviMaxFlyingRange = FromFlightCtrl.Param.Byte[8];
819
                                CHK_POTI_MM(Parameter.NaviWindCorrection,FromFlightCtrl.Param.Byte[9],0,255);
820
                                CHK_POTI_MM(Parameter.NaviWindCorrection,FromFlightCtrl.Param.Byte[9],0,255);
820
                                CHK_POTI_MM(Parameter.NaviAccCompensation,FromFlightCtrl.Param.Byte[10],0,255);
821
                                CHK_POTI_MM(Parameter.NaviAccCompensation,FromFlightCtrl.Param.Byte[10],0,255);
821
                                CHK_POTI_MM(Parameter.NaviAngleLimitation,FromFlightCtrl.Param.Byte[11],0,255);
822
                                CHK_POTI_MM(Parameter.NaviAngleLimitation,FromFlightCtrl.Param.Byte[11],0,255);
822
                                break;
823
                                break;
823
                        case SPI_FCCMD_PARAMETER2:
824
                        case SPI_FCCMD_PARAMETER2:
824
                                CHK_POTI_MM(FC.AutoPhotoDistance,FromFlightCtrl.Param.Byte[0],0,255);
825
                                CHK_POTI_MM(FC.AutoPhotoDistance,FromFlightCtrl.Param.Byte[0],0,255);
825
                                if(FromFlightCtrl.Param.Byte[1])
826
                                if(FromFlightCtrl.Param.Byte[1])
826
                                 {
827
                                 {
827
                                  FC.FromFC_SpeakHoTT = FromFlightCtrl.Param.Byte[1];                   // will be cleared in the SD-Logging
828
                                  FC.FromFC_SpeakHoTT = FromFlightCtrl.Param.Byte[1];                   // will be cleared in the SD-Logging
828
                                 }
829
                                 }
829
                                if(NaviData_Flags_SpeakHoTT_Processed)
830
                                if(NaviData_Flags_SpeakHoTT_Processed)
830
                                 {
831
                                 {
831
                                  NaviData_Flags.SpeakHoTT = FromFlightCtrl.Param.Byte[1];              // will be cleared after the Uart-Trasmitting
832
                                  NaviData_Flags.SpeakHoTT = FromFlightCtrl.Param.Byte[1];              // will be cleared after the Uart-Trasmitting
832
                                  NaviData_Flags_SpeakHoTT_Processed = 0;
833
                                  NaviData_Flags_SpeakHoTT_Processed = 0;
833
                                 }
834
                                 }
834
                                FC.BAT_UsedCapacity = FromFlightCtrl.Param.Int[1]; // 2 & 3
835
                                FC.BAT_UsedCapacity = FromFlightCtrl.Param.Int[1]; // 2 & 3
835
                                Parameter.FromFC_LowVoltageHomeActive = FromFlightCtrl.Param.Byte[4];
836
                                Parameter.FromFC_LowVoltageHomeActive = FromFlightCtrl.Param.Byte[4];
836
                                if(FromFlightCtrl.Param.Byte[5]) FromFC_LoadWP_List = FromFlightCtrl.Param.Byte[5];
837
                                if(FromFlightCtrl.Param.Byte[5]) FromFC_LoadWP_List = FromFlightCtrl.Param.Byte[5];
837
                                if(FromFlightCtrl.Param.Byte[6]) FromFC_Load_SinglePoint = FromFlightCtrl.Param.Byte[6];
838
                                if(FromFlightCtrl.Param.Byte[6]) FromFC_Load_SinglePoint = FromFlightCtrl.Param.Byte[6];
838
                                if(FromFlightCtrl.Param.Byte[7]) FromFC_Save_SinglePoint = FromFlightCtrl.Param.Byte[7];
839
                                if(FromFlightCtrl.Param.Byte[7]) FromFC_Save_SinglePoint = FromFlightCtrl.Param.Byte[7];
839
                                CompassSetpoint = FromFlightCtrl.Param.sInt[4] * 10; // 8 & 9
840
                                CompassSetpoint = FromFlightCtrl.Param.sInt[4] * 10; // 8 & 9
840
                                CompassSetpointCorrected = (3600 + CompassSetpoint + FC.FromFC_CompassOffset + GeoMagDec) % 3600;
841
                                CompassSetpointCorrected = (3600 + CompassSetpoint + FC.FromFC_CompassOffset + GeoMagDec) % 3600;
841
                                FC.StatusFlags3 = FromFlightCtrl.Param.Byte[10];
842
                                FC.StatusFlags3 = FromFlightCtrl.Param.Byte[10];
842
                                Parameter.SingleWpSpeed = FromFlightCtrl.Param.Byte[11];
843
                                Parameter.SingleWpSpeed = FromFlightCtrl.Param.Byte[11];
843
                                break;
844
                                break;
844
                        case SPI_FCCMD_STICK:
845
                        case SPI_FCCMD_STICK:
845
                                FC.StickGas     = FromFlightCtrl.Param.sByte[0];
846
                                FC.StickGas     = FromFlightCtrl.Param.sByte[0];
846
                                FC.StickYaw     = FromFlightCtrl.Param.sByte[1];
847
                                FC.StickYaw     = FromFlightCtrl.Param.sByte[1];
847
                                FC.StickRoll    = FromFlightCtrl.Param.sByte[2];
848
                                FC.StickRoll    = FromFlightCtrl.Param.sByte[2];
848
                                FC.StickNick    = FromFlightCtrl.Param.sByte[3];
849
                                FC.StickNick    = FromFlightCtrl.Param.sByte[3];
849
                                FC.Poti[0]              = FromFlightCtrl.Param.Byte[4];
850
                                FC.Poti[0]              = FromFlightCtrl.Param.Byte[4];
850
                                FC.Poti[1]              = FromFlightCtrl.Param.Byte[5];
851
                                FC.Poti[1]              = FromFlightCtrl.Param.Byte[5];
851
                                FC.Poti[2]              = FromFlightCtrl.Param.Byte[6];
852
                                FC.Poti[2]              = FromFlightCtrl.Param.Byte[6];
852
                                FC.Poti[3]              = FromFlightCtrl.Param.Byte[7];
853
                                FC.Poti[3]              = FromFlightCtrl.Param.Byte[7];
853
                                FC.Poti[4]              = FromFlightCtrl.Param.Byte[8];
854
                                FC.Poti[4]              = FromFlightCtrl.Param.Byte[8];
854
                                FC.Poti[5]              = FromFlightCtrl.Param.Byte[9];
855
                                FC.Poti[5]              = FromFlightCtrl.Param.Byte[9];
855
                                FC.Poti[6]              = FromFlightCtrl.Param.Byte[10];
856
                                FC.Poti[6]              = FromFlightCtrl.Param.Byte[10];
856
                                FC.Poti[7]              = FromFlightCtrl.Param.Byte[11];
857
                                FC.Poti[7]              = FromFlightCtrl.Param.Byte[11];
857
                                CHK_POTI_MM(WaypointAcceleration,WaypointAccelerationSetting,0,255); // that could be a Poti-Value
858
                                CHK_POTI_MM(WaypointAcceleration,WaypointAccelerationSetting,0,255); // that could be a Poti-Value
858
                                break;
859
                                break;
859
                        case SPI_FCCMD_STICK2:
860
                        case SPI_FCCMD_STICK2:
860
                                FC.StickGas     = FromFlightCtrl.Param.sByte[0];
861
                                FC.StickGas     = FromFlightCtrl.Param.sByte[0];
861
                                FC.StickYaw     = FromFlightCtrl.Param.sByte[1];
862
                                FC.StickYaw     = FromFlightCtrl.Param.sByte[1];
862
                                FC.StickRoll    = FromFlightCtrl.Param.sByte[2];
863
                                FC.StickRoll    = FromFlightCtrl.Param.sByte[2];
863
                                FC.StickNick    = FromFlightCtrl.Param.sByte[3];
864
                                FC.StickNick    = FromFlightCtrl.Param.sByte[3];
864
                                memcpy((u8 *) &(PPM_In[1]), (u8 *) &FromFlightCtrl.Param.sByte[4], 16); // 16 Bytes PPM-Data
865
                                memcpy((u8 *) &(PPM_In[1]), (u8 *) &FromFlightCtrl.Param.sByte[4], 16); // 16 Bytes PPM-Data
865
                                PPM_In[0] = 0;
866
                                PPM_In[0] = 0;
866
                                break;
867
                                break;
867
                        case SPI_FCCMD_MISC:
868
                        case SPI_FCCMD_MISC:
868
                                if(CompassCalState != FromFlightCtrl.Param.Byte[0])
869
                                if(CompassCalState != FromFlightCtrl.Param.Byte[0])
869
                                {       // put only new CompassCalState into queue to send via I2C
870
                                {       // put only new CompassCalState into queue to send via I2C
870
//                                      if(FromFlightCtrl.Param.Byte[0] == CompassCalState+1 || FromFlightCtrl.Param.Byte[0] == 0)
871
//                                      if(FromFlightCtrl.Param.Byte[0] == CompassCalState+1 || FromFlightCtrl.Param.Byte[0] == 0)
871
                                         {
872
                                         {
872
                                           CompassCalState = FromFlightCtrl.Param.Byte[0];
873
                                           CompassCalState = FromFlightCtrl.Param.Byte[0];
873
                                           Compass_SetCalState(CompassCalState);
874
                                           Compass_SetCalState(CompassCalState);
874
                                         }
875
                                         }
875
//                                       else CompassCalState = 0;
876
//                                       else CompassCalState = 0;
876
                                }
877
                                }
877
                                HoverGas = FromFlightCtrl.Param.Byte[1];
878
                                HoverGas = FromFlightCtrl.Param.Byte[1];
878
                                NaviData.Variometer = (NaviData.Variometer + 2 * (FromFlightCtrl.Param.sInt[1] - NaviData.Altimeter)) / 2; // provisorisch
879
                                NaviData.Variometer = (NaviData.Variometer + 2 * (FromFlightCtrl.Param.sInt[1] - NaviData.Altimeter)) / 2; // provisorisch
879
                                FC.Altimeter = FromFlightCtrl.Param.sInt[1]; // in 5cm
880
                                FC.Altimeter = FromFlightCtrl.Param.sInt[1]; // in 5cm
880
                                if(!(SimulationFlags & SIMULATION_ACTIVE)) NaviData.SetpointAltitude = FromFlightCtrl.Param.sInt[2]; // in 5cm
881
                                if(!(SimulationFlags & SIMULATION_ACTIVE)) NaviData.SetpointAltitude = FromFlightCtrl.Param.sInt[2]; // in 5cm
881
                                FC.Error[0]                     |= FromFlightCtrl.Param.Byte[6];
882
                                FC.Error[0]                     |= FromFlightCtrl.Param.Byte[6];
882
                                FC.Error[1]                     |= FromFlightCtrl.Param.Byte[7];
883
                                FC.Error[1]                     |= FromFlightCtrl.Param.Byte[7];
883
                                DebugOut.StatusGreen |= AMPEL_FC; // status of FC Present
884
                                DebugOut.StatusGreen |= AMPEL_FC; // status of FC Present
884
                                DebugOut.StatusGreen |= AMPEL_BL; // status of BL Present
885
                                DebugOut.StatusGreen |= AMPEL_BL; // status of BL Present
885
                                if(FC.Error[0] || FC.Error[1] /* || FC.Error[2] || FC.Error[3] || FC.Error[4]*/) DebugOut.StatusRed |= AMPEL_FC;
886
                                if(FC.Error[0] || FC.Error[1] /* || FC.Error[2] || FC.Error[3] || FC.Error[4]*/) DebugOut.StatusRed |= AMPEL_FC;
886
                                else DebugOut.StatusRed &= ~AMPEL_FC;
887
                                else DebugOut.StatusRed &= ~AMPEL_FC;
887
                                FC_I2C_ErrorConter = FromFlightCtrl.Param.Byte[8];
888
                                FC_I2C_ErrorConter = FromFlightCtrl.Param.Byte[8];
888
                                FC.RC_Quality   = FromFlightCtrl.Param.Byte[9];
889
                                FC.RC_Quality   = FromFlightCtrl.Param.Byte[9];
889
                                if(FC.RC_Quality > 160) NaviData.RC_Quality = 200; else NaviData.RC_Quality = FC.RC_Quality;
890
                                if(FC.RC_Quality > 160) NaviData.RC_Quality = 200; else NaviData.RC_Quality = FC.RC_Quality;
890
                                NC_Wait_for_LED = FromFlightCtrl.Param.Byte[10];
891
                                NC_Wait_for_LED = FromFlightCtrl.Param.Byte[10];
891
//DebugOut.Analog[] = NC_Wait_for_LED;
892
//DebugOut.Analog[] = NC_Wait_for_LED;
892
                                NaviData.Gas    = (FC.BAT_Voltage * (u32) FromFlightCtrl.Param.Byte[11]) / (u32) Parameter.LowVoltageWarning;
893
                                NaviData.Gas    = (FC.BAT_Voltage * (u32) FromFlightCtrl.Param.Byte[11]) / (u32) Parameter.LowVoltageWarning;
893
                                if(LoggingGasCnt == 0) LoggingGasFilter = 0;
894
                                if(LoggingGasCnt == 0) LoggingGasFilter = 0;
894
                                LoggingGasFilter += (u32) FromFlightCtrl.Param.Byte[11];
895
                                LoggingGasFilter += (u32) FromFlightCtrl.Param.Byte[11];
895
                                LoggingGasCnt++;
896
                                LoggingGasCnt++;
896
                                break;
897
                                break;
897
 
898
 
898
                        case SPI_FCCMD_VERSION:  // slow!
899
                        case SPI_FCCMD_VERSION:  // slow!
899
                                FC_Version.Major                = FromFlightCtrl.Param.Byte[0];
900
                                FC_Version.Major                = FromFlightCtrl.Param.Byte[0];
900
                                FC_Version.Minor                = FromFlightCtrl.Param.Byte[1];
901
                                FC_Version.Minor                = FromFlightCtrl.Param.Byte[1];
901
                                FC_Version.Patch                = FromFlightCtrl.Param.Byte[2];
902
                                FC_Version.Patch                = FromFlightCtrl.Param.Byte[2];
902
                                FC_Version.Compatible   = FromFlightCtrl.Param.Byte[3];
903
                                FC_Version.Compatible   = FromFlightCtrl.Param.Byte[3];
903
                                FC_Version.Hardware             = FromFlightCtrl.Param.Byte[4];
904
                                FC_Version.Hardware             = FromFlightCtrl.Param.Byte[4];
904
                                Parameter.FromFC_LandingSpeed = FromFlightCtrl.Param.Byte[5];
905
                                Parameter.FromFC_LandingSpeed = FromFlightCtrl.Param.Byte[5];
905
                                Parameter.ComingHomeAltitude = FromFlightCtrl.Param.Byte[6];
906
                                Parameter.ComingHomeAltitude = FromFlightCtrl.Param.Byte[6];
906
                                CHK_POTI_MM(Parameter.AutoPhotoAltitudes,FromFlightCtrl.Param.Byte[7],0,255);
907
                                CHK_POTI_MM(Parameter.AutoPhotoAltitudes,FromFlightCtrl.Param.Byte[7],0,255);
907
                                UART_VersionInfo.BL_Firmware = FromFlightCtrl.Param.Byte[8];
908
                                UART_VersionInfo.BL_Firmware = FromFlightCtrl.Param.Byte[8];
908
                                Parameter.ActiveSetting = FromFlightCtrl.Param.Byte[9];
909
                                Parameter.ActiveSetting = FromFlightCtrl.Param.Byte[9];
909
                                FlugMinutenGesamt = FromFlightCtrl.Param.Int[5]; // 10 & 11
910
                                FlugMinutenGesamt = FromFlightCtrl.Param.Int[5]; // 10 & 11
-
 
911
                                if(FromFlightCtrl.Param.Byte[12] == 1) IamMaster = MASTER;
-
 
912
                                else
-
 
913
                                if(FromFlightCtrl.Param.Byte[12] == 2) IamMaster = SLAVE;
910
                                break;
914
                                break;
911
                        case SPI_FCCMD_NEUTRAL:  // slow!
915
                        case SPI_FCCMD_NEUTRAL:  // slow!
912
                                FC.AdNeutralNick = FromFlightCtrl.Param.Int[0];
916
                                FC.AdNeutralNick = FromFlightCtrl.Param.Int[0];
913
                                FC.AdNeutralRoll = FromFlightCtrl.Param.Int[1];
917
                                FC.AdNeutralRoll = FromFlightCtrl.Param.Int[1];
914
                                FC.AdNeutralYaw = FromFlightCtrl.Param.Int[2];
918
                                FC.AdNeutralYaw = FromFlightCtrl.Param.Int[2];
915
                                Parameter.Driftkomp = FromFlightCtrl.Param.Byte[6];    
919
                                Parameter.Driftkomp = FromFlightCtrl.Param.Byte[6];    
916
                                Parameter.NaviPH_LoginTime = FromFlightCtrl.Param.Byte[7];
920
                                Parameter.NaviPH_LoginTime = FromFlightCtrl.Param.Byte[7];
917
                                Parameter.ReceiverType = FromFlightCtrl.Param.Byte[8];
921
                                Parameter.ReceiverType = FromFlightCtrl.Param.Byte[8];
918
                                CHK_POTI_MM(Parameter.NaviGpsPLimit,FromFlightCtrl.Param.Byte[9],0,255);
922
                                CHK_POTI_MM(Parameter.NaviGpsPLimit,FromFlightCtrl.Param.Byte[9],0,255);
919
                                CHK_POTI_MM(Parameter.NaviGpsILimit,FromFlightCtrl.Param.Byte[10],0,255);
923
                                CHK_POTI_MM(Parameter.NaviGpsILimit,FromFlightCtrl.Param.Byte[10],0,255);
920
                                CHK_POTI_MM(Parameter.NaviGpsDLimit,FromFlightCtrl.Param.Byte[11],0,255);
924
                                CHK_POTI_MM(Parameter.NaviGpsDLimit,FromFlightCtrl.Param.Byte[11],0,255);
921
                                break;
925
                                break;
922
                        case SPI_FCCMD_SLOW2:   // slow!
926
                        case SPI_FCCMD_SLOW2:   // slow!
923
                                FC.BoatNeutralNick = FromFlightCtrl.Param.Int[0];  // 0 & 1
927
                                FC.BoatNeutralNick = FromFlightCtrl.Param.Int[0];  // 0 & 1
924
                                FC.BoatNeutralRoll = FromFlightCtrl.Param.Int[1];  // 2 & 3
928
                                FC.BoatNeutralRoll = FromFlightCtrl.Param.Int[1];  // 2 & 3
925
                                FC.BoatNeutralYaw = FromFlightCtrl.Param.Int[2];   // 4 & 5
929
                                FC.BoatNeutralYaw = FromFlightCtrl.Param.Int[2];   // 4 & 5
926
                                Parameter.CamOrientation = FromFlightCtrl.Param.Byte[6];
930
                                Parameter.CamOrientation = FromFlightCtrl.Param.Byte[6];
927
                                if(FromFlightCtrl.Param.Byte[7] >= 68 && FromFlightCtrl.Param.Byte[7] <= 188)
931
                                if(FromFlightCtrl.Param.Byte[7] >= 68 && FromFlightCtrl.Param.Byte[7] <= 188)
928
                                {
932
                                {
929
                                        FC.FromFC_DisableDeclination = 1;
933
                                        FC.FromFC_DisableDeclination = 1;
930
                                        FC.FromFC_CompassOffset = 10 * (s8) (FromFlightCtrl.Param.Byte[7] - 128);
934
                                        FC.FromFC_CompassOffset = 10 * (s8) (FromFlightCtrl.Param.Byte[7] - 128);
931
                                        GeoMagDec = 0;
935
                                        GeoMagDec = 0;
932
                                }
936
                                }
933
                                else
937
                                else
934
                                {
938
                                {
935
                                        FC.FromFC_DisableDeclination = 0;
939
                                        FC.FromFC_DisableDeclination = 0;
936
                                        FC.FromFC_CompassOffset = 10 * FromFlightCtrl.Param.sByte[7];
940
                                        FC.FromFC_CompassOffset = 10 * FromFlightCtrl.Param.sByte[7];
937
                                }
941
                                }
938
                                Parameter.GlobalConfig = FromFlightCtrl.Param.Byte[8];
942
                                Parameter.GlobalConfig = FromFlightCtrl.Param.Byte[8];
939
                                Parameter.ExtraConfig = FromFlightCtrl.Param.Byte[9];
943
                                Parameter.ExtraConfig = FromFlightCtrl.Param.Byte[9];
940
                                Parameter.OrientationAngle = FromFlightCtrl.Param.Byte[10];
944
                                Parameter.OrientationAngle = FromFlightCtrl.Param.Byte[10];
941
                                Parameter.GlobalConfig3 = FromFlightCtrl.Param.Byte[11];
945
                                Parameter.GlobalConfig3 = FromFlightCtrl.Param.Byte[11];
942
                                break;
946
                                break;
943
                        case SPI_FCCMD_SLOW3:  // slow!
947
                        case SPI_FCCMD_SLOW3:  // slow!
944
                                ServoParams.NickControl = FromFlightCtrl.Param.Byte[0];
948
                                ServoParams.NickControl = FromFlightCtrl.Param.Byte[0];
945
                                ServoParams.RollControl = FromFlightCtrl.Param.Byte[1];
949
                                ServoParams.RollControl = FromFlightCtrl.Param.Byte[1];
946
                                Parameter.DescendRange = FromFlightCtrl.Param.Byte[2];
950
                                Parameter.DescendRange = FromFlightCtrl.Param.Byte[2];
947
                                Parameter.MaximumAltitude = FromFlightCtrl.Param.Byte[3];
951
                                Parameter.MaximumAltitude = FromFlightCtrl.Param.Byte[3];
948
                                ServoParams.CompInvert = FromFlightCtrl.Param.Byte[4];
952
                                ServoParams.CompInvert = FromFlightCtrl.Param.Byte[4];
949
                                Parameter.HomeYawMode = ((ServoParams.CompInvert & 0x18) >> 3);
953
                                Parameter.HomeYawMode = ((ServoParams.CompInvert & 0x18) >> 3);
950
                                NaviData_Home.LipoCellCount = FromFlightCtrl.Param.Byte[5];
954
                                NaviData_Home.LipoCellCount = FromFlightCtrl.Param.Byte[5];
-
 
955
                                if(UART_VersionInfo.HWMajor >= 30) NaviData_Volatile.ShutterCounter = TrigLogging.Count;
951
                                NaviData_Volatile.ShutterCounter = FromFlightCtrl.Param.Int[3];   // 6 & 7
956
                                else                    NaviData_Volatile.ShutterCounter = FromFlightCtrl.Param.Int[3];   // 6 & 7
952
                                LowVoltageLandingActive = FromFlightCtrl.Param.Byte[8];
957
                                LowVoltageLandingActive = FromFlightCtrl.Param.Byte[8];
953
                                Parameter.FailSafeTime = FromFlightCtrl.Param.Byte[9];
958
                                Parameter.FailSafeTime = FromFlightCtrl.Param.Byte[9];
954
// DebugOut.Analog[] = NaviData_Volatile.ShutterCounter;
959
// DebugOut.Analog[] = NaviData_Volatile.ShutterCounter;
955
// 8
960
// 8
956
// 9
961
// 9
957
// 10
962
// 10
958
// 11
963
// 11
959
                                break;
964
                                break;
960
 
965
 
961
                        default:
966
                        default:
962
                                break;
967
                                break;
963
                }
968
                }
964
                DebugOut.Analog[0] = FromFlightCtrl.AngleNick;
969
                DebugOut.Analog[0] = FromFlightCtrl.AngleNick;
965
                DebugOut.Analog[1] = FromFlightCtrl.AngleRoll;
970
                DebugOut.Analog[1] = FromFlightCtrl.AngleRoll;
966
                DebugOut.Analog[11] = FromFlightCtrl.GyroHeading/10;// in deg
971
                DebugOut.Analog[11] = FromFlightCtrl.GyroHeading/10;// in deg
967
                // every time we got new data from the FC via SPI call the navigation routine
972
                // every time we got new data from the FC via SPI call the navigation routine
968
                // and update GPSStick that are returned to FC
973
                // and update GPSStick that are returned to FC
969
                SPI_RxBuffer_Request = 0;
974
                SPI_RxBuffer_Request = 0;
970
                GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick));
975
                GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick));
971
                ClearFCStatusFlags = 1;
976
                ClearFCStatusFlags = 1;
972
                if(counter)
977
                if(counter)
973
                {
978
                {
974
                        counter--;                                       // count down to enable servo
979
                        counter--;                                       // count down to enable servo
975
                        if(!counter) TIMER2_Init();  // enable Servo Output
980
                        if(!counter) TIMER2_Init();  // enable Servo Output
976
                }
981
                }
977
                timeout = SetDelay(80); // 80 ms, new data are send every 20 ms
982
                timeout = SetDelay(80); // 80 ms, new data are send every 20 ms
978
 
983
 
979
        }       // EOF if(SPI_RxBuffer_Request)
984
        }       // EOF if(SPI_RxBuffer_Request)
980
        else // no new SPI data
985
        else // no new SPI data
981
        {
986
        {
982
                if(CheckDelay(timeout) && (counter == 0))
987
                if(CheckDelay(timeout) && (counter == 0))
983
                {
988
                {
984
                        TIMER2_Deinit();  // disable Servo Output
989
                        TIMER2_Deinit();  // disable Servo Output
985
                        counter = 50;     // reset counter for enabling Servo Output
990
                        counter = 50;     // reset counter for enabling Servo Output
986
                }
991
                }
987
        }
992
        }
988
}
993
}
989
 
994
 
990
//------------------------------------------------------
995
//------------------------------------------------------
991
void SPI0_GetFlightCtrlVersion(void)
996
void SPI0_GetFlightCtrlVersion(void)
992
{
997
{
993
        u32 timeout;
998
        u32 timeout;
994
        u8 repeat;
999
        u8 repeat;
995
        u8 msg[64];
1000
        u8 msg[64];
996
 
1001
 
997
        UART1_PutString("\r\n Looking for FlightControl");
1002
        UART1_PutString("\r\n Looking for FlightControl");
998
        FC_Version.Major = 0xFF;
1003
        FC_Version.Major = 0xFF;
999
        FC_Version.Minor = 0xFF;
1004
        FC_Version.Minor = 0xFF;
1000
        FC_Version.Patch = 0xFF;
1005
        FC_Version.Patch = 0xFF;
1001
        FC_Version.Compatible = 0xFF;
1006
        FC_Version.Compatible = 0xFF;
1002
 
1007
 
1003
        // polling FC version info
1008
        // polling FC version info
1004
        repeat = 0;
1009
        repeat = 0;
1005
        do
1010
        do
1006
        {
1011
        {
1007
                timeout = SetDelay(250);
1012
                timeout = SetDelay(250);
1008
                do
1013
                do
1009
                {
1014
                {
1010
                        SPI0_UpdateBuffer();
1015
                        SPI0_UpdateBuffer();
1011
                        if (FC_Version.Major != 0xFF)  break;
1016
                        if (FC_Version.Major != 0xFF)  break;
1012
                }while (!CheckDelay(timeout));
1017
                }while (!CheckDelay(timeout));
1013
                UART1_PutString(".");
1018
                UART1_PutString(".");
1014
                repeat++;
1019
                repeat++;
1015
                FCCalibActive = 0;
1020
                FCCalibActive = 0;
1016
        }while((FC_Version.Major == 0xFF) && (repeat < 40)); // 40*250ms = 10s
1021
        }while((FC_Version.Major == 0xFF) && (repeat < 40)); // 40*250ms = 10s
1017
        // if we got it
1022
        // if we got it
1018
        if (FC_Version.Major != 0xFF)
1023
        if (FC_Version.Major != 0xFF)
1019
        {
1024
        {
1020
                sprintf(msg, " FC V%d.%02d%c HW:%d.%d", FC_Version.Major, FC_Version.Minor, 'a'+FC_Version.Patch, FC_Version.Hardware/10,FC_Version.Hardware%10);
1025
                sprintf(msg, " FC V%d.%02d%c HW:%d.%d", FC_Version.Major, FC_Version.Minor, 'a'+FC_Version.Patch, FC_Version.Hardware/10,FC_Version.Hardware%10);
1021
                UART1_PutString(msg);
1026
                UART1_PutString(msg);
1022
        }
1027
        }
1023
        else UART1_PutString("\n\r not found!");
1028
        else UART1_PutString("\n\r not found!");
1024
 
1029
 
1025
//sprintf(msg, "\n\r size: HugeBlockFromFC %i",sizeof(HugeBlockFromFC));
1030
//sprintf(msg, "\n\r size: HugeBlockFromFC %i",sizeof(HugeBlockFromFC));
1026
//UART1_PutString(msg);
1031
//UART1_PutString(msg);
1027
}
1032
}
1028
 
1033
 
1029
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1034
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1030
// + extended Current measurement -> 200 = 20A    201 = 21A    255 = 75A (20+55)
1035
// + extended Current measurement -> 200 = 20A    201 = 21A    255 = 75A (20+55)
1031
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1036
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1032
u16 BL3_Current(u8 who) // in 0,1A
1037
u16 BL3_Current(u8 who) // in 0,1A
1033
{
1038
{
1034
        if(Motor[who].Current <= 200) return((u16) Motor[who].Current);
1039
        if(Motor[who].Current <= 200) return((u16) Motor[who].Current);
1035
        else
1040
        else
1036
        {
1041
        {
1037
                if(Motor_Version[who] & MOTOR_STATE_BL30) return(200 + 10 * (u16) (Motor[who].Current - 200));
1042
                if(Motor_Version[who] & MOTOR_STATE_BL30) return(200 + 10 * (u16) (Motor[who].Current - 200));
1038
                else return((u16) Motor[who].Current);
1043
                else return((u16) Motor[who].Current);
1039
        }
1044
        }
1040
}
1045
}
1041
 
1046
 
1042
 
1047