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