Subversion Repositories NaviCtrl

Rev

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

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