Subversion Repositories NaviCtrl

Rev

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

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