-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathRpcLibClientBase.cpp
More file actions
413 lines (344 loc) · 15.6 KB
/
RpcLibClientBase.cpp
File metadata and controls
413 lines (344 loc) · 15.6 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//in header only mode, control library is not available
#ifndef AIRLIB_HEADER_ONLY
//RPC code requires C++14. If build system like Unreal doesn't support it then use compiled binaries
#ifndef AIRLIB_NO_RPC
//if using Unreal Build system then include precompiled header file first
#include "api/RpcLibClientBase.hpp"
#include "common/Common.hpp"
#include "common/ClockFactory.hpp"
#include <functional>
#include <vector>
#include <thread>
STRICT_MODE_OFF
#ifndef RPCLIB_MSGPACK
#define RPCLIB_MSGPACK clmdep_msgpack
#endif // !RPCLIB_MSGPACK
#ifdef nil
#undef nil
#endif // nil
#include "common/common_utils/WindowsApisCommonPre.hpp"
#undef FLOAT
#undef check
#include "rpc/client.h"
//TODO: HACK: UE4 defines macro with stupid names like "check" that conflicts with msgpack library
#ifndef check
#define check(expr) (static_cast<void>((expr)))
#endif
#include "common/common_utils/WindowsApisCommonPost.hpp"
#include "api/RpcLibAdapatorsBase.hpp"
STRICT_MODE_ON
#ifdef _MSC_VER
__pragma(warning( disable : 4239))
#endif
namespace msr { namespace airlib {
struct RpcLibClientBase::impl {
impl(const string& ip_address, uint16_t port, float timeout_sec)
: client(ip_address, port)
{
// some long flight path commands can take a while, so we give it up to 1 hour max.
client.set_timeout(static_cast<int64_t>(timeout_sec * 1.0E3));
}
rpc::client client;
};
typedef msr::airlib_rpclib::RpcLibAdapatorsBase RpcLibAdapatorsBase;
RpcLibClientBase::RpcLibClientBase(const string& ip_address, uint16_t port, float timeout_sec) : ip_address_(ip_address), port_(port), timeout_sec_(timeout_sec)
{
pimpl_.reset(new impl(ip_address_, port_, timeout_sec_));
}
RpcLibClientBase::~RpcLibClientBase()
{}
bool RpcLibClientBase::ping()
{
return pimpl_->client.call("ping").as<bool>();
}
RpcLibClientBase::ConnectionState RpcLibClientBase::getConnectionState()
{
switch (pimpl_->client.get_connection_state()) {
case rpc::client::connection_state::connected: return ConnectionState::Connected;
case rpc::client::connection_state::disconnected: return ConnectionState::Disconnected;
case rpc::client::connection_state::initial: return ConnectionState::Initial;
case rpc::client::connection_state::reset: return ConnectionState::Reset;
default:
return ConnectionState::Unknown;
}
}
void RpcLibClientBase::enableApiControl(bool is_enabled, const std::string& vehicle_name)
{
pimpl_->client.call("enableApiControl", is_enabled, vehicle_name);
}
bool RpcLibClientBase::isApiControlEnabled(const std::string& vehicle_name) const
{
return pimpl_->client.call("isApiControlEnabled", vehicle_name).as<bool>();
}
int RpcLibClientBase::getClientVersion() const
{
return 1; //sync with Python client
}
int RpcLibClientBase::getMinRequiredServerVersion() const
{
return 1; //sync with Python client
}
int RpcLibClientBase::getMinRequiredClientVersion() const
{
return pimpl_->client.call("getMinRequiredClientVersion").as<int>();
}
int RpcLibClientBase::getServerVersion() const
{
return pimpl_->client.call("getServerVersion").as<int>();
}
void RpcLibClientBase::reset()
{
pimpl_->client.call("reset");
}
void RpcLibClientBase::restart()
{
pimpl_->client.call("restart");
}
void RpcLibClientBase::confirmConnection(double timeout)
{
ClockBase* clock = ClockFactory::get();
double delay = 0.2;
for (double elapsed = 0.0; elapsed <= timeout; elapsed += delay) {
pimpl_.reset(new impl(ip_address_, port_, timeout_sec_));
if (getConnectionState() == RpcLibClientBase::ConnectionState::Connected)
{
break;
}
clock->sleep_for(delay);
}
if (getConnectionState() != RpcLibClientBase::ConnectionState::Connected)
{
throw std::runtime_error("Failed connecting to RPC server (airsim). Is the simulator running?");
}
auto server_ver = getServerVersion();
auto client_ver = getClientVersion();
auto server_min_ver = getMinRequiredServerVersion();
auto client_min_ver = getMinRequiredClientVersion();
std::string ver_info = Utils::stringf("Client Ver:%i (Min Req:%i), Server Ver:%i (Min Req:%i)",
client_ver, client_min_ver, server_ver, server_min_ver);
if (server_ver < server_min_ver) {
throw std::runtime_error("AirSim server is of older version and not supported by this client. Please upgrade! (" + ver_info + ")");
}
else if (client_ver < client_min_ver) {
throw std::runtime_error("AirSim client is of older version and not supported by this server. Please upgrade! (" + ver_info + ")");
}
}
msr::airlib::GeoPoint RpcLibClientBase::getHomeGeoPoint(const std::string& vehicle_name) const
{
return pimpl_->client.call("getHomeGeoPoint", vehicle_name).as<RpcLibAdapatorsBase::GeoPoint>().to();
}
msr::airlib::LidarData RpcLibClientBase::getLidarData(const std::string& lidar_name, const std::string& vehicle_name) const
{
return pimpl_->client.call("getLidarData", lidar_name, vehicle_name).as<RpcLibAdapatorsBase::LidarData>().to();
}
msr::airlib::ImuBase::Output RpcLibClientBase::getImuData(const std::string& imu_name, const std::string& vehicle_name) const
{
return pimpl_->client.call("getImuData", imu_name, vehicle_name).as<RpcLibAdapatorsBase::ImuData>().to();
}
msr::airlib::GpsBase::Output RpcLibClientBase::getGpsData(const std::string& gps_name, const std::string& vehicle_name) const
{
return pimpl_->client.call("getGpsData", gps_name, vehicle_name).as<RpcLibAdapatorsBase::GpsData>().to();
}
msr::airlib::DistanceBase::Output RpcLibClientBase::getDistanceSensorData(const std::string& distance_sensor_name, const std::string& vehicle_name) const
{
return pimpl_->client.call("getDistanceSensorData", distance_sensor_name, vehicle_name).as<RpcLibAdapatorsBase::DistanceSensorData>().to();
}
msr::airlib::GSSSimple::Output RpcLibClientBase::getGroundSpeedSensorData(const std::string& vehicle_name) const
{
return pimpl_->client.call("getGroundSpeedSensorData", vehicle_name).as<RpcLibAdapatorsBase::GSSData>().to();
}
vector<int> RpcLibClientBase::simGetLidarSegmentation(const std::string& lidar_name, const std::string& vehicle_name) const
{
return pimpl_->client.call("simGetLidarSegmentation", lidar_name, vehicle_name).as<vector<int>>();
}
bool RpcLibClientBase::simSetSegmentationObjectID(const std::string& mesh_name, int object_id, bool is_name_regex)
{
return pimpl_->client.call("simSetSegmentationObjectID", mesh_name, object_id, is_name_regex).as<bool>();
}
int RpcLibClientBase::simGetSegmentationObjectID(const std::string& mesh_name) const
{
return pimpl_->client.call("simGetSegmentationObjectID", mesh_name).as<int>();
}
CollisionInfo RpcLibClientBase::simGetCollisionInfo(const std::string& vehicle_name) const
{
return pimpl_->client.call("simGetCollisionInfo", vehicle_name).as<RpcLibAdapatorsBase::CollisionInfo>().to();
}
//sim only
Pose RpcLibClientBase::simGetVehiclePose(const std::string& vehicle_name) const
{
return pimpl_->client.call("simGetVehiclePose", vehicle_name).as<RpcLibAdapatorsBase::Pose>().to();
}
void RpcLibClientBase::simSetVehiclePose(const Pose& pose, bool ignore_collision, const std::string& vehicle_name)
{
pimpl_->client.call("simSetVehiclePose", RpcLibAdapatorsBase::Pose(pose), ignore_collision, vehicle_name);
}
void RpcLibClientBase::simSetTraceLine(const std::vector<float>& color_rgba, float thickness, const std::string & vehicle_name)
{
pimpl_->client.call("simSetTraceLine", color_rgba, thickness, vehicle_name);
}
vector<ImageCaptureBase::ImageResponse> RpcLibClientBase::simGetImages(vector<ImageCaptureBase::ImageRequest> request, const std::string& vehicle_name)
{
const auto& response_adaptor = pimpl_->client.call("simGetImages",
RpcLibAdapatorsBase::ImageRequest::from(request), vehicle_name)
.as<vector<RpcLibAdapatorsBase::ImageResponse>>();
return RpcLibAdapatorsBase::ImageResponse::to(response_adaptor);
}
vector<uint8_t> RpcLibClientBase::simGetImage(const std::string& camera_name, ImageCaptureBase::ImageType type, const std::string& vehicle_name)
{
vector<uint8_t> result = pimpl_->client.call("simGetImage", camera_name, type, vehicle_name).as<vector<uint8_t>>();
if (result.size() == 1) {
// rpclib has a bug with serializing empty vectors, so we return a 1 byte vector instead.
result.clear();
}
return result;
}
vector<MeshPositionVertexBuffersResponse> RpcLibClientBase::simGetMeshPositionVertexBuffers()
{
const auto& response_adaptor = pimpl_->client.call("simGetMeshPositionVertexBuffers").as<vector<RpcLibAdapatorsBase::MeshPositionVertexBuffersResponse>>();
return RpcLibAdapatorsBase::MeshPositionVertexBuffersResponse::to(response_adaptor);
}
void RpcLibClientBase::simPrintLogMessage(const std::string& message, std::string message_param, unsigned char severity)
{
pimpl_->client.call("simPrintLogMessage", message, message_param, severity);
}
void RpcLibClientBase::simFlushPersistentMarkers()
{
pimpl_->client.call("simFlushPersistentMarkers");
}
void RpcLibClientBase::simPlotPoints(const vector<Vector3r>& points, const vector<float>& color_rgba, float size, float duration, bool is_persistent)
{
vector<RpcLibAdapatorsBase::Vector3r> conv_points;
RpcLibAdapatorsBase::from(points, conv_points);
pimpl_->client.call("simPlotPoints", conv_points, color_rgba, size, duration, is_persistent);
}
void RpcLibClientBase::simPlotLineStrip(const vector<Vector3r>& points, const vector<float>& color_rgba, float thickness, float duration, bool is_persistent)
{
vector<RpcLibAdapatorsBase::Vector3r> conv_points;
RpcLibAdapatorsBase::from(points, conv_points);
pimpl_->client.call("simPlotLineStrip", conv_points, color_rgba, thickness, duration, is_persistent);
}
void RpcLibClientBase::simPlotLineList(const vector<Vector3r>& points, const vector<float>& color_rgba, float thickness, float duration, bool is_persistent)
{
vector<RpcLibAdapatorsBase::Vector3r> conv_points;
RpcLibAdapatorsBase::from(points, conv_points);
pimpl_->client.call("simPlotLineList", conv_points, color_rgba, thickness, duration, is_persistent);
}
void RpcLibClientBase::simPlotArrows(const vector<Vector3r>& points_start, const vector<Vector3r>& points_end, const vector<float>& color_rgba, float thickness, float arrow_size, float duration, bool is_persistent)
{
vector<RpcLibAdapatorsBase::Vector3r> conv_points_start;
RpcLibAdapatorsBase::from(points_start, conv_points_start);
vector<RpcLibAdapatorsBase::Vector3r> conv_points_end;
RpcLibAdapatorsBase::from(points_end, conv_points_end);
pimpl_->client.call("simPlotArrows", conv_points_start, conv_points_end, color_rgba, thickness, arrow_size, duration, is_persistent);
}
void RpcLibClientBase::simPlotStrings(const vector<std::string>& strings, const vector<Vector3r>& positions, float scale, const vector<float>& color_rgba, float duration)
{
vector<RpcLibAdapatorsBase::Vector3r> conv_positions;
RpcLibAdapatorsBase::from(positions, conv_positions);
pimpl_->client.call("simPlotStrings", strings, conv_positions, scale, color_rgba, duration);
}
void RpcLibClientBase::simPlotTransforms(const vector<Pose>& poses, float scale, float thickness, float duration, bool is_persistent)
{
vector<RpcLibAdapatorsBase::Pose> conv_poses;
RpcLibAdapatorsBase::from(poses, conv_poses);
pimpl_->client.call("simPlotTransforms", conv_poses, scale, thickness, duration, is_persistent);
}
void RpcLibClientBase::simPlotTransformsWithNames(const vector<Pose>& poses, const vector<std::string>& names, float tf_scale, float tf_thickness, float text_scale, const vector<float>& text_color_rgba, float duration)
{
vector<RpcLibAdapatorsBase::Pose> conv_poses;
RpcLibAdapatorsBase::from(poses, conv_poses);
pimpl_->client.call("simPlotTransformsWithNames", conv_poses, names, tf_scale, tf_thickness, text_scale, text_color_rgba, duration);
}
bool RpcLibClientBase::simIsPaused() const
{
return pimpl_->client.call("simIsPaused").as<bool>();
}
void RpcLibClientBase::simPause(bool is_paused)
{
pimpl_->client.call("simPause", is_paused);
}
void RpcLibClientBase::simContinueForTime(double seconds)
{
pimpl_->client.call("simContinueForTime", seconds);
}
void RpcLibClientBase::simEnableWeather(bool enable)
{
pimpl_->client.call("simEnableWeather", enable);
}
void RpcLibClientBase::simSetWeatherParameter(WorldSimApiBase::WeatherParameter param, float val)
{
pimpl_->client.call("simSetWeatherParameter", param, val);
}
void RpcLibClientBase::simSetTimeOfDay(bool is_enabled, const string& start_datetime, bool is_start_datetime_dst,
float celestial_clock_speed, float update_interval_secs, bool move_sun)
{
pimpl_->client.call("simSetTimeOfDay", is_enabled, start_datetime, is_start_datetime_dst,
celestial_clock_speed, update_interval_secs, move_sun);
}
vector<string> RpcLibClientBase::simListSceneObjects(const string& name_regex) const
{
return pimpl_->client.call("simListSceneObjects", name_regex).as<vector<string>>();
}
std::vector<std::string> RpcLibClientBase::simSwapTextures(const std::string& tags, int tex_id, int component_id, int material_id)
{
return pimpl_->client.call("simSwapTextures", tags, tex_id, component_id, material_id).as<vector<string>>();
}
msr::airlib::Pose RpcLibClientBase::simGetObjectPose(const std::string& object_name) const
{
return pimpl_->client.call("simGetObjectPose", object_name).as<RpcLibAdapatorsBase::Pose>().to();
}
bool RpcLibClientBase::simSetObjectPose(const std::string& object_name, const msr::airlib::Pose& pose, bool teleport)
{
return pimpl_->client.call("simSetObjectPose", object_name, RpcLibAdapatorsBase::Pose(pose), teleport).as<bool>();
}
CameraInfo RpcLibClientBase::simGetCameraInfo(const std::string& camera_name, const std::string& vehicle_name) const
{
return pimpl_->client.call("simGetCameraInfo", camera_name, vehicle_name).as<RpcLibAdapatorsBase::CameraInfo>().to();
}
void RpcLibClientBase::simSetCameraOrientation(const std::string& camera_name, const Quaternionr& orientation, const std::string& vehicle_name)
{
pimpl_->client.call("simSetCameraOrientation", camera_name, RpcLibAdapatorsBase::Quaternionr(orientation), vehicle_name);
}
void RpcLibClientBase::simSetCameraFov(const std::string& camera_name, float fov_degrees, const std::string& vehicle_name)
{
pimpl_->client.call("simSetCameraFov", camera_name, fov_degrees, vehicle_name);
}
msr::airlib::Kinematics::State RpcLibClientBase::simGetGroundTruthKinematics(const std::string& vehicle_name) const
{
return pimpl_->client.call("simGetGroundTruthKinematics", vehicle_name).as<RpcLibAdapatorsBase::KinematicsState>().to();
}
msr::airlib::WheelStates RpcLibClientBase::simGetWheelStates(const std::string& vehicle_name) const
{
return pimpl_->client.call("simGetWheelStates", vehicle_name).as<RpcLibAdapatorsBase::WheelStates>().to();
}
void RpcLibClientBase::cancelLastTask(const std::string& vehicle_name)
{
pimpl_->client.call("cancelLastTask", vehicle_name);
}
std::string RpcLibClientBase::getSettingsString() const
{
return pimpl_->client.call("getSettingsString").as<std::string>();
}
//return value of last task. It should be true if task completed without
//cancellation or timeout
RpcLibClientBase* RpcLibClientBase::waitOnLastTask(bool* task_result, float timeout_sec)
{
//should be implemented by derived class if it supports async task,
//for example using futures
unused(timeout_sec);
unused(task_result);
return this;
}
void* RpcLibClientBase::getClient()
{
return &pimpl_->client;
}
const void* RpcLibClientBase::getClient() const
{
return &pimpl_->client;
}
}} //namespace
#endif
#endif