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