Subversion Repositories NaviCtrl

Rev

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

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