Subversion Repositories NaviCtrl

Rev

Rev 511 | Rev 514 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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