swift
pbh.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2019 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #ifndef SWIFT_CORE_FSD_PBH_H
7 #define SWIFT_CORE_FSD_PBH_H
8 
9 #include <QtGlobal>
10 #include <QtMath>
11 
12 namespace swift::core::fsd
13 {
15  union PBH
16  {
17  unsigned int pbh = 0;
18  struct
19  {
20  unsigned int unused : 1;
21  unsigned int onground : 1;
22  unsigned int hdg : 10;
23  int bank : 10;
24  int pitch : 10;
25  };
26  };
27 
29  constexpr double pitchMultiplier() { return 256.0 / 90.0; }
30 
32  constexpr double bankMultiplier() { return 512.0 / 180.0; }
33 
35  constexpr double headingMultiplier() { return 1024.0 / 360.0; }
36 
38  inline void packPBH(double pitch, double bank, double heading, bool onGround, quint32 &pbh)
39  {
40  PBH pbhstrct;
41  pbhstrct.pitch = qFloor(pitch * -pitchMultiplier()); // the "-" is the inverted pitch/bank
42  pbhstrct.bank = qFloor(bank * -bankMultiplier());
43  pbhstrct.hdg = static_cast<unsigned int>(heading * headingMultiplier());
44 
45  // FSD has inverted pitch and bank angles
46  // based on discussion https://discordapp.com/channels/539048679160676382/539925070550794240/687390301530095634
47  // we use *-1 inversion, not "~" (vPilot also uses *-1)
48  // with "~" 0->-1 (that is why we saw many -1 PB values on ground)
49  // pbhstrct.pitch = ~pbhstrct.pitch;
50  // pbhstrct.bank = ~pbhstrct.bank;
51 
52  pbhstrct.onground = onGround ? 1 : 0;
53  pbh = pbhstrct.pbh;
54  }
55 
57  inline void unpackPBH(quint32 pbh, double &pitch, double &bank, double &heading, bool &onGround)
58  {
59  PBH pbhstrct;
60  pbhstrct.pbh = pbh;
61  const int iPitch = qFloor(pbhstrct.pitch / -pitchMultiplier()); // the "-" is the inverted pitch/bank
62  const int iBank = qFloor(pbhstrct.bank / -bankMultiplier());
63 
64  // FSD has inverted pitch and bank angles
65  // based on discussion https://discordapp.com/channels/539048679160676382/539925070550794240/687390301530095634
66  // we use *-1 inversion, not "~" (vPilot also uses *-1)
67  // with "~" 0->-1 (that is why we saw many -1 PB values on ground)
68  // iPitch = ~iPitch;
69  // iBank = ~iBank;
70 
71  pitch = iPitch;
72  bank = iBank;
73  heading = pbhstrct.hdg / headingMultiplier();
74 
75  onGround = pbhstrct.onground == 1;
76  }
77 } // namespace swift::core::fsd
78 
79 #endif // SWIFT_CORE_FSD_PBH_H
void unpackPBH(quint32 pbh, double &pitch, double &bank, double &heading, bool &onGround)
Unpack pitch, bank, heading and onGround from 32 bit integer.
Definition: pbh.h:57
constexpr double bankMultiplier()
Bank multiplier.
Definition: pbh.h:32
constexpr double headingMultiplier()
Heading multiplier.
Definition: pbh.h:35
void packPBH(double pitch, double bank, double heading, bool onGround, quint32 &pbh)
Pack pitch, bank, heading and onGround into 32 bit integer.
Definition: pbh.h:38
constexpr double pitchMultiplier()
Pitch multiplier.
Definition: pbh.h:29
Pitch bank heading union.
Definition: pbh.h:16
int pitch
Pitch.
Definition: pbh.h:24
unsigned int unused
unused bit
Definition: pbh.h:20
unsigned int onground
Onground flag.
Definition: pbh.h:21
unsigned int hdg
Heading.
Definition: pbh.h:22
int bank
Bank.
Definition: pbh.h:23
unsigned int pbh
Pitch/Bank/Heading as integer value.
Definition: pbh.h:17