-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmpadapter_xpilot.cpp
More file actions
219 lines (180 loc) · 7.81 KB
/
mpadapter_xpilot.cpp
File metadata and controls
219 lines (180 loc) · 7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//
// openSAM: open source SAM emulator for X Plane
//
// Copyright (C) 2025, 2026 Holger Teutsch
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
// USA
//
#include <cstdlib>
#include <cstring>
#include <stdexcept>
#include <cassert>
#include "openSAM.h"
#include "plane.h"
#include "mpadapter_xpilot.h"
static XPLMDataRef modeS_id_dr, icao_type_dr, flight_id_dr, // identity
x_dr, y_dr, z_dr, psi_dr, // position
on_ground_dr, lights_dr, throttle_dr; // state
class MpPlane_xPilot : public Plane {
const int slot_;
std::string flight_id_;
// parking brake emulation
float last_move_ts_{0};
float x_last_move_{0}, z_last_move_{0};
float y_adjust_; // plane y + y_adjust_ == y of ground level
float scan_mp_planes();
public:
MpPlane_xPilot(int slot, const std::string& flight_id, const std::string& icao);
~MpPlane_xPilot() override {
}
void update(float x, float y, float z, float psi, float throttle, int lights);
bool auto_mode() const override {
return true;
}
bool dock_requested() override {
return true;
}
};
MpPlane_xPilot::MpPlane_xPilot(int slot, const std::string& flight_id, const std::string& icao) : slot_(slot) {
flight_id_ = flight_id;
icao_ = icao;
on_ground_ = true; // otherwise we were not here
LogMsg("pid=%02d, constructing MpPlane %s/%s", id_, flight_id_.c_str(), icao_.c_str());
n_door_ = 0;
// Use iterator to check and assign door_info_[0]
auto it = csl_door_info_map.find(icao_ + '1');
if (it != csl_door_info_map.end()) {
door_info_[0] = it->second;
n_door_++;
LogMsg("pid=%02d, found door 1 in door_info_map: x: %0.2f, y: %0.2f, z: %0.2f", id_, door_info_[0].x,
door_info_[0].y, door_info_[0].z);
} else {
LogMsg("pid=%02d, %s: door 1 is not defined in door_info_map, deactivating slot", id_, icao_.c_str());
state_ = DISABLED;
return;
}
state_ = IDLE;
}
void MpPlane_xPilot::update(float x, float y, float z, float psi, float throttle, int lights) {
if (state_ == DISABLED)
return;
x_ = x;
y_ = y;
z_ = z;
psi_ = psi;
engines_on_ = (throttle > 0.1f);
// 'parkbrake' detection
if (fabsf(x_ - x_last_move_) > 0.5f || fabsf(z_ - z_last_move_) > 0.5f) {
x_last_move_ = x_;
z_last_move_ = z_;
last_move_ts_ = now;
}
beacon_on_ = ((lights & 1) == 1);
parkbrake_set_ = ((now - last_move_ts_) > 10.0f);
LogMsg("MP update: pid=%02d, slot: %02d, icao: %s, id: %s, beacon: %d, parkbrake_set: %d, engine_on: %d, state: %s",
id_, slot_, icao_.c_str(), flight_id_.c_str(), beacon_on_, parkbrake_set_, engines_on_, state_str_[state_]);
}
//============== MpAdapter_xPilot ========================================
bool MpAdapter_xPilot::probe() {
static XPLMDataRef xpilot_status_dr;
if (xpilot_status_dr == nullptr)
xpilot_status_dr = XPLMFindDataRef("xpilot/login/status");
return (xpilot_status_dr && (XPLMGetDatai(xpilot_status_dr) > 0));
}
MpAdapter_xPilot::MpAdapter_xPilot() {
LogMsg("MpAdapter_xPilot constructor");
static bool init_done{false};
if (!init_done) {
modeS_id_dr = XPLMFindDataRef("sim/cockpit2/tcas/targets/modeS_id");
icao_type_dr = XPLMFindDataRef("sim/cockpit2/tcas/targets/icao_type");
flight_id_dr = XPLMFindDataRef("sim/cockpit2/tcas/targets/flight_id");
x_dr = XPLMFindDataRef("sim/cockpit2/tcas/targets/position/x");
y_dr = XPLMFindDataRef("sim/cockpit2/tcas/targets/position/y");
z_dr = XPLMFindDataRef("sim/cockpit2/tcas/targets/position/z");
psi_dr = XPLMFindDataRef("sim/cockpit2/tcas/targets/position/psi");
on_ground_dr = XPLMFindDataRef("sim/cockpit2/tcas/targets/position/weight_on_wheels");
lights_dr = XPLMFindDataRef("sim/cockpit2/tcas/targets/position/lights");
throttle_dr = XPLMFindDataRef("sim/cockpit2/tcas/targets/position/throttle");
init_done = true;
}
n_planes_ = XPLMGetDatavi(modeS_id_dr, NULL, 0, 0);
LogMsg("MpPlane_xPilot drefs #: %d", n_planes_);
modeS_id_val_ = std::make_unique_for_overwrite<int[]>(n_planes_);
icao_type_val_ = std::make_unique_for_overwrite<char[]>(n_planes_ * 8);
flight_id_val_ = std::make_unique_for_overwrite<char[]>(n_planes_ * 8);
on_ground_val_ = std::make_unique_for_overwrite<int[]>(n_planes_);
lights_val_ = std::make_unique_for_overwrite<int[]>(n_planes_);
x_val_ = std::make_unique_for_overwrite<float[]>(n_planes_);
y_val_ = std::make_unique_for_overwrite<float[]>(n_planes_);
z_val_ = std::make_unique_for_overwrite<float[]>(n_planes_);
psi_val_ = std::make_unique_for_overwrite<float[]>(n_planes_);
throttle_val_ = std::make_unique_for_overwrite<float[]>(n_planes_);
}
MpAdapter_xPilot::~MpAdapter_xPilot() {
LogMsg("MpAdapter_xPilot destructor");
}
#define LOAD_DR(type, name) XPLMGetDatav##type(name##_dr, name##_val_.get(), 0, n_planes_)
// static
float MpAdapter_xPilot::update() {
LOAD_DR(i, modeS_id);
LOAD_DR(i, on_ground);
LOAD_DR(i, lights);
LOAD_DR(f, x);
LOAD_DR(f, y);
LOAD_DR(f, z);
LOAD_DR(f, psi);
LOAD_DR(f, throttle);
XPLMGetDatab(icao_type_dr, icao_type_val_.get(), 0, n_planes_ * 8);
XPLMGetDatab(flight_id_dr, flight_id_val_.get(), 0, n_planes_ * 8);
std::unordered_map<std::string, bool> dref_planes;
dref_planes.reserve(n_planes_);
for (int i = 1; i < n_planes_; i++) {
// slot empty or not on ground or far away -> ignore
if (0 == modeS_id_val_[i] || !on_ground_val_[i] ||
len2f(x_val_[i] - my_plane->x(), z_val_[i] - my_plane->z()) > kMpMaxDist)
continue;
// flight_id
char* cptr = flight_id_val_.get() + i * 8;
cptr[7] = '\0'; // play it safe
std::string flight_id = cptr;
// icao type
cptr = icao_type_val_.get() + i * 8;
cptr[7] = '\0'; // play it safe
std::string icao = cptr;
std::string key = flight_id + '/' + icao;
dref_planes[key] = true;
auto it = mp_planes_.find(key);
if (it != mp_planes_.end()) {
MpPlane_xPilot* mp_plane = static_cast<MpPlane_xPilot*>(it->second.get());
mp_plane->update(x_val_[i], y_val_[i], z_val_[i], psi_val_[i], throttle_val_[i], lights_val_[i]);
} else {
MpPlane_xPilot* mp_plane = new MpPlane_xPilot(i, flight_id, icao);
mp_plane->update(x_val_[i], y_val_[i], z_val_[i], psi_val_[i], throttle_val_[i], lights_val_[i]);
mp_planes_.emplace(key, mp_plane);
}
}
// loop over mp_planes_ and delete the ones that are no longer in drefs
for (auto& mp : mp_planes_) {
const std::string& key = mp.first;
Plane& plane = *(mp.second);
if (dref_planes.find(key) == dref_planes.end()) {
LogMsg("pid=%02d key: %s not longer exists, deleted", plane.id_, key.c_str());
mp_planes_.erase(key);
}
}
LogMsg("------------------ MP active planes found: %d -----------------", (int)mp_planes_.size());
return 2.0f;
}