-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathDrone.cpp
More file actions
892 lines (733 loc) · 30.8 KB
/
Drone.cpp
File metadata and controls
892 lines (733 loc) · 30.8 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
// Copyright (C) Microsoft Corporation.
// Copyright (C) 2025 IAMAI CONSULTING CORP
//
// MIT License. All rights reserved.
#include "Drone.h"
#include <exception>
#include <limits>
#include <regex>
#include <unordered_set>
#include "AirSimClient.h"
#include "AirSimMessage/response_message.hpp"
#include "AsyncResultInternal.h"
#include "Common.h"
#include "JSONUtils.h"
#include "pch.h"
namespace microsoft {
namespace projectairsim {
namespace client {
class Drone::Impl {
public:
Impl(void) noexcept;
~Impl();
Status Arm(bool* pf_is_armed_out);
Status CanArm(bool* pf_can_be_armed_out) const;
Status CancelLastTask(bool* pf_task_is_canceled_out);
Status DisableAPIControl(bool* pf_is_disabled_out);
Status Disarm(bool* pf_is_disarmed_out);
Status EnableAPIControl(bool* pf_is_enabled_out);
Status GetCameraRay(const std::string& str_camera_name, ImageType image_type,
int x, int y, Pose* ppose_out);
Status GetGroundTruthPose(Transform* ptransform_out);
size_t GetRobotInfoCount(void) const;
const char* GetRobotInfoName(int iname) const;
const char* GetRobotTopic(const std::string& str_info_name) const;
size_t GetSensorCount(void) const;
size_t GetSensorInfoCount(const std::string& str_sensor_name) const;
const char* GetSensorInfoName(const std::string& str_sensor_name,
int iinfo) const;
const char* GetSensorName(int iname) const;
const char* GetSensorTopic(const std::string& str_sensor_name,
const std::string& str_info_name) const;
AsyncResult GoHomeAsync(float sec_timeout, float velocity,
FnResponseCallback fnresponse_callback);
AsyncResult HoverAsync(FnResponseCallback fnresponse_callback);
Status Initialize(std::shared_ptr<Client>& pclient,
std::shared_ptr<World>& pworld,
const std::string& str_drone_name);
Status IsAPIControlEnabled(bool* pf_is_enabled_out) const;
AsyncResult LandAsync(float sec_timeout,
FnResponseCallback fnresponse_callback);
AsyncResult MoveByVelocityAsync(float v_north, float v_east, float v_down,
float sec_duration,
YawControlMode yaw_control_mode,
bool yaw_is_rate, float yaw,
FnResponseCallback fnresponse_callback);
ASC_DECL AsyncResult MoveByVelocityBodyFrameAsync(
float v_forward, float v_right, float v_down, float sec_duration,
YawControlMode yaw_control_mode, bool yaw_is_rate, float yaw,
FnResponseCallback fnresponse_callback);
ASC_DECL AsyncResult MoveOnPathAsync(const VecVector3& path, float velocity,
float timeout_sec,
YawControlMode yaw_control_mode,
bool yaw_is_rate, float yaw,
float lookahead,
float adaptive_lookahead,
FnResponseCallback fnresponse_callback);
ASC_DECL AsyncResult MoveToPositionAsync(
float north, float east, float down, float velocity, float timeout_sec,
YawControlMode yaw_control_mode, bool yaw_is_rate, float yaw,
float lookahead, float adaptive_lookahead,
FnResponseCallback fnresponse_callback);
Status SetPose(const Transform& transform, bool reset_kinematics);
AsyncResult TakeoffAsync(float sec_timeout,
FnResponseCallback fnresponse_callback);
protected:
// Mapping from a string to another
typedef std::unordered_map<std::string, std::string> UMStrStr;
// Mapping from a string to a string-to-string map
typedef std::unordered_map<std::string, UMStrStr> UMStrUMStr;
// Mapping from a string to vector of strings
typedef std::unordered_map<std::string, std::vector<std::string>> UMStrVecStr;
protected:
void LogTopics(void) const;
void SetRobotInfoTopics(void);
void SetSensorTopics(World* pworld);
void SetTopics(void);
Status RequestDrone(const std::string str_method,
const json& json_params = json()) const;
Status RequestDrone(const std::string str_method, bool* pf_out) const;
Status RequestDrone(const std::string str_method, json* pjson_out) const;
Status RequestDrone(const std::string str_method, const json& json_params,
bool* pf_out) const;
Status RequestDrone(const std::string str_method, const json& json_params,
json* pjson_out) const;
AsyncResult RequestDroneAsync(const std::string str_method,
const json& json_params,
FnResponseCallback fnresponse_callback) const;
protected:
std::shared_ptr<Client> pclient_; // Pointer to client connection object
std::shared_ptr<World> pworld_; // Pointer to scene object
std::string str_drone_name_; // Name of the drone
std::string str_parent_topic_; // Topic root of the drone
std::string str_sensor_topic_; // Topic root of the sensors
std::string str_world_parent_topic_; // Topic root of the scene
UMStrStr umrobot_name_topic_; // Mapping from robot info name to topic name
UMStrUMStr umsensor_name_uminfo_topic_; // Mapping from sensor name to
// mapping from info to topic name
UMStrVecStr umsensor_name_vecinfo_; // Mapping from sensor name to list of
// sensor info names
VecStr vecstr_robot_info_name_; // List of robot info names
VecStr vecstr_sensor_name_; // List of sensor names
}; // class Drone::Impl
ASC_DECL const float Drone::kNoTimeout = std::numeric_limits<float>::max();
ASC_DECL Drone::Drone(void) noexcept : pimpl_(new Impl()) {}
ASC_DECL Drone::~Drone() {}
Status Drone::Initialize(std::shared_ptr<Client>& pclient,
std::shared_ptr<World>& pworld,
const std::string& str_drone_name) {
RETURN_CATCH_STATUS(pimpl_->Initialize(pclient, pworld, str_drone_name));
}
ASC_DECL Status Drone::Arm(bool* pf_is_armed_out) {
RETURN_CATCH_STATUS(pimpl_->Arm(pf_is_armed_out));
}
ASC_DECL Status Drone::Disarm(bool* pf_is_disarmed_out) {
RETURN_CATCH_STATUS(pimpl_->Disarm(pf_is_disarmed_out));
}
ASC_DECL Status Drone::CanArm(bool* pf_can_be_armed_out) const {
RETURN_CATCH_STATUS(pimpl_->CanArm(pf_can_be_armed_out));
}
ASC_DECL Status Drone::CancelLastTask(bool* pf_task_is_canceled_out) {
RETURN_CATCH_STATUS(pimpl_->CancelLastTask(pf_task_is_canceled_out));
}
ASC_DECL Status Drone::DisableAPIControl(bool* pf_is_disabled_out) {
RETURN_CATCH_STATUS(pimpl_->DisableAPIControl(pf_is_disabled_out));
}
ASC_DECL Status Drone::EnableAPIControl(bool* pf_is_enabled_out) {
RETURN_CATCH_STATUS(pimpl_->EnableAPIControl(pf_is_enabled_out));
}
ASC_DECL Status Drone::GetCameraRay(const std::string& str_camera_name,
ImageType image_type, int x, int y,
Pose* ppose_out) {
RETURN_CATCH_STATUS(
pimpl_->GetCameraRay(str_camera_name, image_type, x, y, ppose_out));
}
ASC_DECL Status Drone::GetGroundTruthPose(Transform* ptransform_out) {
RETURN_CATCH_STATUS(pimpl_->GetGroundTruthPose(ptransform_out));
}
ASC_DECL size_t Drone::GetRobotInfoCount(void) const {
return (pimpl_->GetRobotInfoCount());
}
ASC_DECL const char* Drone::GetRobotInfoName(int iname) const {
return (pimpl_->GetRobotInfoName(iname));
}
ASC_DECL const char* Drone::GetRobotTopic(
const std::string& str_info_name) const {
return (pimpl_->GetRobotTopic(str_info_name));
}
ASC_DECL size_t Drone::GetSensorCount(void) const {
return (pimpl_->GetSensorCount());
}
ASC_DECL size_t
Drone::GetSensorInfoCount(const std::string& str_sensor_name) const {
return (pimpl_->GetSensorInfoCount(str_sensor_name));
}
ASC_DECL const char* Drone::GetSensorInfoName(
const std::string& str_sensor_name, int iinfo) const {
return (pimpl_->GetSensorInfoName(str_sensor_name, iinfo));
}
ASC_DECL const char* Drone::GetSensorName(int iname) const {
return (pimpl_->GetSensorName(iname));
}
ASC_DECL const char* Drone::GetSensorTopic(
const std::string& str_sensor_name,
const std::string& str_info_name) const {
return (pimpl_->GetSensorTopic(str_sensor_name, str_info_name));
}
ASC_DECL AsyncResult Drone::GoHomeAsync(
float sec_timeout, float velocity, FnResponseCallback fnresponse_callback) {
return (pimpl_->GoHomeAsync(sec_timeout, velocity, fnresponse_callback));
}
ASC_DECL AsyncResult Drone::HoverAsync(FnResponseCallback fnresponse_callback) {
return (pimpl_->HoverAsync(fnresponse_callback));
}
ASC_DECL Status Drone::IsAPIControlEnabled(bool* pf_is_enabled_out) const {
RETURN_CATCH_STATUS(pimpl_->IsAPIControlEnabled(pf_is_enabled_out));
}
ASC_DECL AsyncResult Drone::LandAsync(float sec_timeout,
FnResponseCallback fnresponse_callback) {
return (pimpl_->LandAsync(sec_timeout, fnresponse_callback));
}
ASC_DECL AsyncResult Drone::MoveByVelocityAsync(
float v_north, float v_east, float v_down, float sec_duration,
YawControlMode yaw_control_mode, bool yaw_is_rate, float yaw,
FnResponseCallback fnresponse_callback) {
return (pimpl_->MoveByVelocityAsync(v_north, v_east, v_down, sec_duration,
yaw_control_mode, yaw_is_rate, yaw,
fnresponse_callback));
}
ASC_DECL AsyncResult Drone::MoveByVelocityBodyFrameAsync(
float v_forward, float v_right, float v_down, float sec_duration,
YawControlMode yaw_control_mode, bool yaw_is_rate, float yaw,
FnResponseCallback fnresponse_callback) {
return (pimpl_->MoveByVelocityBodyFrameAsync(
v_forward, v_right, v_down, sec_duration, yaw_control_mode, yaw_is_rate,
yaw, fnresponse_callback));
}
ASC_DECL AsyncResult Drone::MoveOnPathAsync(
const VecVector3& path, float velocity, float timeout_sec,
YawControlMode yaw_control_mode, bool yaw_is_rate, float yaw,
float lookahead, float adaptive_lookahead,
FnResponseCallback fnresponse_callback) {
return (pimpl_->MoveOnPathAsync(path, velocity, timeout_sec, yaw_control_mode,
yaw_is_rate, yaw, lookahead,
adaptive_lookahead, fnresponse_callback));
}
ASC_DECL AsyncResult Drone::MoveToPositionAsync(
float north, float east, float down, float velocity, float timeout_sec,
YawControlMode yaw_control_mode, bool yaw_is_rate, float yaw,
float lookahead, float adaptive_lookahead,
FnResponseCallback fnresponse_callback) {
return (pimpl_->MoveToPositionAsync(
north, east, down, velocity, timeout_sec, yaw_control_mode, yaw_is_rate,
yaw, lookahead, adaptive_lookahead, fnresponse_callback));
}
ASC_DECL Status Drone::SetPose(const Transform& transform,
bool reset_kinematics) {
RETURN_CATCH_STATUS(pimpl_->SetPose(transform, reset_kinematics));
}
ASC_DECL AsyncResult
Drone::TakeoffAsync(float sec_timeout, FnResponseCallback fnresponse_callback) {
return (pimpl_->TakeoffAsync(sec_timeout, fnresponse_callback));
}
Drone::Impl::Impl() noexcept
: pclient_(),
pworld_(),
str_drone_name_(),
str_parent_topic_(),
str_sensor_topic_(),
str_world_parent_topic_(),
umrobot_name_topic_(),
umsensor_name_uminfo_topic_(),
umsensor_name_vecinfo_(),
vecstr_robot_info_name_(),
vecstr_sensor_name_() {}
Drone::Impl::~Impl() {}
Status Drone::Impl::Arm(bool* pf_is_armed_out) {
return (RequestDrone("Arm", pf_is_armed_out));
}
Status Drone::Impl::CanArm(bool* pf_can_be_armed_out) const {
return (RequestDrone("CanArm", pf_can_be_armed_out));
}
Status Drone::Impl::CancelLastTask(bool* pf_task_is_canceled_out) {
TAsyncResult<Message> ar;
Status status;
ar = pclient_->RequestPriorityAsync(str_parent_topic_ + "/CancelLastTask", json(), nullptr);
if ((status = ar.Wait()) == Status::OK) {
const Message& message = ar.GetResult();
ResponseMessage response_message;
response_message.Deserialize(message);
if (response_message.GetErrorCode() == 0)
*pf_task_is_canceled_out = response_message.GetResult().is_boolean() && static_cast<bool>(response_message.GetResult());
else
{
status = Status::RejectedByServer;
log.ErrorF(
"Server method \"CancelLastTask\" failed: error %d: %s",
response_message.GetErrorCode(),
static_cast<std::string>(response_message.GetResult()["message"])
.c_str());
}
}
return (status);
}
Status Drone::Impl::DisableAPIControl(bool* pf_is_disabled_out) {
return (RequestDrone("DisableApiControl", pf_is_disabled_out));
}
Status Drone::Impl::Disarm(bool* pf_is_disarmed_out) {
return (RequestDrone("Disarm", pf_is_disarmed_out));
}
Status Drone::Impl::EnableAPIControl(bool* pf_is_enabled_out) {
return (RequestDrone("EnableApiControl", pf_is_enabled_out));
}
Status Drone::Impl::GetCameraRay(const std::string& str_camera_name,
ImageType image_type, int x, int y,
Pose* ppose_out) {
Status status;
json json_params = {{"camera_id", str_camera_name},
{"image_type", (int)image_type},
{"x", x},
{"y", y}};
json json_response;
if ((status = RequestDrone("GetCameraRay", json_params, &json_response)) ==
Status::OK)
*ppose_out = json_response;
return (status);
}
Status Drone::Impl::GetGroundTruthPose(Transform* ptransform_out) {
Status status;
json json_response;
if ((status = RequestDrone("GetGroundTruthPose", &json_response)) ==
Status::OK)
*ptransform_out = json_response;
return (status);
}
size_t Drone::Impl::GetRobotInfoCount(void) const {
return (vecstr_robot_info_name_.size());
}
const char* Drone::Impl::GetRobotInfoName(int iname) const {
const char* sz_ret = nullptr;
if ((iname >= 0) && (iname < vecstr_robot_info_name_.size()))
sz_ret = vecstr_robot_info_name_[iname].c_str();
return (sz_ret);
}
const char* Drone::Impl::GetRobotTopic(const std::string& str_info_name) const {
const char* sz_ret = nullptr;
auto it_robot = umrobot_name_topic_.find(str_info_name);
if (it_robot != umrobot_name_topic_.end()) sz_ret = it_robot->second.c_str();
return (sz_ret);
}
size_t Drone::Impl::GetSensorCount(void) const {
return (vecstr_sensor_name_.size());
}
size_t Drone::Impl::GetSensorInfoCount(
const std::string& str_sensor_name) const {
size_t cinfo = 0;
auto it = umsensor_name_vecinfo_.find(str_sensor_name);
if (it != umsensor_name_vecinfo_.end()) cinfo = it->second.size();
return (cinfo);
}
const char* Drone::Impl::GetSensorInfoName(const std::string& str_sensor_name,
int iinfo) const {
const char* sz_ret = nullptr;
auto it = umsensor_name_vecinfo_.find(str_sensor_name);
if ((it != umsensor_name_vecinfo_.end()) && (iinfo >= 0) &&
(iinfo < it->second.size())) {
sz_ret = it->second[iinfo].c_str();
}
return (sz_ret);
}
const char* Drone::Impl::GetSensorName(int iname) const {
const char* sz_ret = nullptr;
if ((iname >= 0) && (iname < vecstr_sensor_name_.size()))
sz_ret = vecstr_sensor_name_[iname].c_str();
return (sz_ret);
}
const char* Drone::Impl::GetSensorTopic(
const std::string& str_sensor_name,
const std::string& str_info_name) const {
const char* sz_ret = nullptr;
auto it_sensor = umsensor_name_uminfo_topic_.find(str_sensor_name);
if (it_sensor != umsensor_name_uminfo_topic_.end()) {
auto& uminfo_topic = it_sensor->second;
auto it_info = uminfo_topic.find(str_info_name);
if (it_info != uminfo_topic.end()) sz_ret = it_info->second.c_str();
}
return (sz_ret);
}
AsyncResult Drone::Impl::GoHomeAsync(float sec_timeout, float velocity,
FnResponseCallback fnresponse_callback) {
json json_params = {
{"timeout_sec", sec_timeout},
{"velocity", velocity},
};
return (RequestDroneAsync("GoHome", json_params, fnresponse_callback));
}
AsyncResult Drone::Impl::HoverAsync(FnResponseCallback fnresponse_callback) {
return (RequestDroneAsync("Hover", json(), fnresponse_callback));
}
Status Drone::Impl::Initialize(std::shared_ptr<Client>& pclient,
std::shared_ptr<World>& pworld,
const std::string& str_drone_name) {
Status status = Status::OK;
log.InfoF("Initalizing Drone '%s'...", str_drone_name.c_str());
pclient_ = pclient;
pworld_ = pworld;
str_drone_name_ = str_drone_name;
str_world_parent_topic_ = pworld->GetParentTopic();
try {
SetTopics();
LogTopics();
log.InfoF("Drone '%s' initialized for World scene '%s'",
str_drone_name_.c_str(), str_world_parent_topic_.c_str());
} catch (Status status_caught) {
status = status_caught;
}
if (status != Status::OK) {
char sz_error[512];
GetStatusString(status, sz_error);
log.ErrorF("Failed to initialize drone '%s': %s", str_drone_name.c_str(),
sz_error);
}
return (status);
}
Status Drone::Impl::IsAPIControlEnabled(bool* pf_is_enabled_out) const {
return (RequestDrone("IsApiControlEnabled", pf_is_enabled_out));
}
AsyncResult Drone::Impl::LandAsync(float sec_timeout,
FnResponseCallback fnresponse_callback) {
json json_params = {{"timeout_sec", sec_timeout}};
return (RequestDroneAsync("Land", json_params, fnresponse_callback));
}
AsyncResult Drone::Impl::MoveByVelocityAsync(
float v_north, float v_east, float v_down, float sec_duration,
YawControlMode yaw_control_mode, bool yaw_is_rate, float yaw,
FnResponseCallback fnresponse_callback) {
json json_params = {
{"vx", v_north},
{"vy", v_east},
{"vz", v_down},
{"duration", sec_duration},
{"drivetrain", yaw_control_mode},
{"yaw_is_rate", yaw_is_rate},
{"yaw", yaw},
};
return (
RequestDroneAsync("MoveByVelocity", json_params, fnresponse_callback));
}
AsyncResult Drone::Impl::MoveByVelocityBodyFrameAsync(
float v_forward, float v_right, float v_down, float sec_duration,
YawControlMode yaw_control_mode, bool yaw_is_rate, float yaw,
FnResponseCallback fnresponse_callback) {
json json_params = {
{"vx", v_forward},
{"vy", v_right},
{"vz", v_down},
{"duration", sec_duration},
{"drivetrain", yaw_control_mode},
{"yaw_is_rate", yaw_is_rate},
{"yaw", yaw},
};
return (RequestDroneAsync("MoveByVelocityBodyFrame", json_params,
fnresponse_callback));
}
AsyncResult Drone::Impl::MoveOnPathAsync(
const VecVector3& path, float velocity, float timeout_sec,
YawControlMode yaw_control_mode, bool yaw_is_rate, float yaw,
float lookahead, float adaptive_lookahead,
FnResponseCallback fnresponse_callback) {
json json_params = {
{"path", path},
{"velocity", velocity},
{"timeout_sec", timeout_sec},
{"drivetrain", yaw_control_mode},
{"yaw_is_rate", yaw_is_rate},
{"yaw", yaw},
{"lookahead", lookahead},
{"adaptive_lookahead", adaptive_lookahead},
};
return (RequestDroneAsync("MoveOnPath", json_params, fnresponse_callback));
}
AsyncResult Drone::Impl::MoveToPositionAsync(
float north, float east, float down, float velocity, float timeout_sec,
YawControlMode yaw_control_mode, bool yaw_is_rate, float yaw,
float lookahead, float adaptive_lookahead,
FnResponseCallback fnresponse_callback) {
json json_params = {
{"x", north},
{"y", east},
{"z", down},
{"velocity", velocity},
{"timeout_sec", timeout_sec},
{"drivetrain", yaw_control_mode},
{"yaw_is_rate", yaw_is_rate},
{"yaw", yaw},
{"lookahead", lookahead},
{"adaptive_lookahead", adaptive_lookahead},
};
return (
RequestDroneAsync("MoveToPosition", json_params, fnresponse_callback));
}
AsyncResult Drone::Impl::TakeoffAsync(float sec_timeout,
FnResponseCallback fnresponse_callback) {
json json_params = {{"timeout_sec", sec_timeout}};
return (RequestDroneAsync("Takeoff", json_params, fnresponse_callback));
}
void Drone::Impl::LogTopics(void) const {
log.Info("-------------------------------------------------");
log.InfoF("The following topics can be subscribed to for robot '%s':",
str_drone_name_.c_str());
for (auto& pair_sensor : umsensor_name_uminfo_topic_) {
for (auto& pair_info : pair_sensor.second)
log.InfoF(" sensors[\"%s\"][\"%s\"]", pair_sensor.first.c_str(),
pair_info.first.c_str());
}
for (auto& pair_robot : umrobot_name_topic_)
log.InfoF(" robot_info[\"%s\"]", pair_robot.first.c_str());
log.Info("-------------------------------------------------");
}
Status Drone::Impl::RequestDrone(const std::string str_method,
const json& json_params) const {
Status status;
Message message_response;
status = pclient_->Request(str_parent_topic_ + "/" + str_method, json_params,
&message_response);
if (status == Status::OK) {
ResponseMessage response_message;
response_message.Deserialize(message_response);
if (response_message.GetErrorCode() != 0) {
status = Status::RejectedByServer;
log.ErrorF(
"Server method \"%s\" failed: error %d: %s", str_method.c_str(),
response_message.GetErrorCode(),
static_cast<std::string>(response_message.GetResult()["message"])
.c_str());
}
}
return (status);
}
Status Drone::Impl::RequestDrone(const std::string str_method,
bool* pf_out) const {
return (RequestDrone(str_method, json(), pf_out));
}
Status Drone::Impl::RequestDrone(const std::string str_method,
json* pjson_out) const {
return (RequestDrone(str_method, json(), pjson_out));
}
Status Drone::Impl::RequestDrone(const std::string str_method,
const json& json_params, bool* pf_out) const {
json json_response;
Status status;
status = RequestDrone(str_method, json_params, &json_response);
if (status == Status::OK) *pf_out = json_response;
return (status);
}
Status Drone::Impl::RequestDrone(const std::string str_method,
const json& json_params,
json* pjson_out) const {
Message message_response;
Status status;
status = pclient_->Request(str_parent_topic_ + "/" + str_method, json_params,
&message_response);
if (status == Status::OK) {
ResponseMessage response_message;
response_message.Deserialize(message_response);
if (response_message.GetErrorCode() == 0)
*pjson_out = response_message.GetResult();
else {
status = Status::RejectedByServer;
log.ErrorF(
"Server method \"%s\" failed: error %d: %s", str_method.c_str(),
response_message.GetErrorCode(),
static_cast<std::string>(response_message.GetResult()["message"])
.c_str());
}
}
return (status);
}
AsyncResult Drone::Impl::RequestDroneAsync(
const std::string str_method, const json& json_params,
FnResponseCallback fnresponse_callback) const {
return (internal::AsyncResultMessageConverter(pclient_->RequestAsync(
str_parent_topic_ + "/" + str_method, json_params, fnresponse_callback)));
}
Status Drone::Impl::SetPose(const Transform& transform, bool reset_kinematics) {
json json_params = {{"pose", transform},
{"reset_kinematics", reset_kinematics}};
return (RequestDrone("SetPose", json_params));
}
void Drone::Impl::SetRobotInfoTopics(void) {
// TODO: Change this to construct this info from the published topics rather
// than
// hard coding it.
const char* c_sz_robot_info_name[3] = {
"actual_pose",
"collision_info",
"rotor_info",
};
umrobot_name_topic_.clear();
vecstr_robot_info_name_.clear();
for (auto& sz : c_sz_robot_info_name) {
umrobot_name_topic_.insert(
std::make_pair(sz, str_parent_topic_ + "/" + sz));
vecstr_robot_info_name_.push_back(sz);
}
}
#ifdef ORIG_IMPL // Implementation as done in Drone.py
void Drone::Impl::SetSensorTopics(World* pworld) {
// TODO: Change this to construct this info from the published topics rather
// than
// extracting it from the configuration.
static const std::unordered_map<int, std::string> c_umcapture_name_setting = {
{0, "scene_camera"}, // TODO: rename scene_camera topic to rgb_camera
{1, "depth_camera"},
{2, "depth_planar_camera"},
{3, "segmentation_camera"},
{4, "depth_vis_camera"},
{5, "disparity_normalized_camera"},
{6, "surface_normals_camera"},
};
auto& json_config = pworld->GetConfiguration();
json json_actor_config;
auto fnAddSensorInfo = [this](std::string sensor_name, std::string info_name,
std::string topic_root_name) {
auto it = umsensor_name_uminfo_topic_.find(sensor_name);
if (it == umsensor_name_uminfo_topic_.end()) {
auto pair = umsensor_name_uminfo_topic_.emplace(sensor_name, UMStrStr());
it = pair.first;
vecstr_sensor_name_.push_back(sensor_name);
umsensor_name_vecinfo_.emplace(sensor_name, VecStr());
}
it->second.insert(
std::make_pair(info_name, topic_root_name + "/" + info_name));
umsensor_name_vecinfo_[sensor_name].push_back(info_name);
};
umsensor_name_uminfo_topic_.clear();
umsensor_name_vecinfo_.clear();
vecstr_sensor_name_.clear();
// Get the config for this drone
if (!json_config.is_null() && json_config["actors"].is_array()) {
for (auto& json_actor : json_config["actors"]) {
if (json_actor["name"] == str_drone_name_) {
json_actor_config = json_actor["robot-config"];
break;
}
}
}
if (json_actor_config.is_null()) {
log.ErrorF("Actor \"%s\" not found in the config", str_drone_name_.c_str());
throw Status::NotFound;
}
// Loop through the robot's sensors
if (!json_actor_config["sensors"].is_array()) return;
for (auto& json_sensor : json_actor_config["sensors"]) {
// Add topic entries for each recognized sensor type
std::string str_name = json_sensor["id"];
std::string str_sensor_type = json_sensor["type"];
std::string str_sensor_topic_root = str_sensor_topic_ + "/" + str_name;
if (str_sensor_type == "airspeed")
fnAddSensorInfo(str_name, "airspeed", str_sensor_topic_root);
else if (str_sensor_type == "barometer")
fnAddSensorInfo(str_name, "barometer", str_sensor_topic_root);
else if (str_sensor_type == "battery")
fnAddSensorInfo(str_name, "battery", str_sensor_topic_root);
else if (str_sensor_type == "camera") {
auto json_subcameras = json_sensor["capture-settings"];
if (json_subcameras.is_array()) {
for (auto& json_subcamera : json_subcameras) {
auto i_image_type = json_subcamera["image-type"];
auto it = c_umcapture_name_setting.find(i_image_type);
if (it != c_umcapture_name_setting.end()) {
auto str_image_type = it->second;
fnAddSensorInfo(str_name, str_image_type, str_sensor_topic_root);
fnAddSensorInfo(str_name, str_image_type + "_info",
str_sensor_topic_root);
}
}
}
} else if (str_sensor_type == "distance-sensor")
fnAddSensorInfo(str_name, "distance-sensor", str_sensor_topic_root);
else if (str_sensor_type == "gps")
fnAddSensorInfo(str_name, "gps", str_sensor_topic_root);
else if (str_sensor_type == "imu")
fnAddSensorInfo(str_name, "imu_kinematics", str_sensor_topic_root);
else if (str_sensor_type == "lidar")
fnAddSensorInfo(str_name, "lidar", str_sensor_topic_root);
else if (str_sensor_type == "magnetometer")
fnAddSensorInfo(str_name, "magnetometer", str_sensor_topic_root);
else if (str_sensor_type == "radar") {
fnAddSensorInfo(str_name, "radar_detections", str_sensor_topic_root);
fnAddSensorInfo(str_name, "radar_tracks", str_sensor_topic_root);
} else {
log.ErrorF("Unknown sensor type \"%s\" found in config for sensor \"%s\"",
str_sensor_type.c_str(), str_name.c_str());
throw Status::NotFound;
}
}
}
#endif // ORIG_IMPL
void Drone::Impl::SetSensorTopics(World* /*pworld*/) {
bool fdrone_found = false;
std::regex re_drone("^/Sim/.+/robots/([^/]+)/.+$");
std::regex re_sensor("^/Sim/.+/sensors/([^/]+)/([^/]+)$");
auto vec_str_topic = pclient_->GetTopicInfo();
std::unordered_set<std::string> us_sensor;
// Clear existing sensor lists
umsensor_name_uminfo_topic_.clear();
umsensor_name_vecinfo_.clear();
vecstr_sensor_name_.clear();
// Loop through the robot's sensor topics
for (auto& str_topic : vec_str_topic) {
std::smatch smatch_drone;
// Ignore topics except those for this drone
if (std::regex_match(str_topic, smatch_drone, re_drone)) {
std::ssub_match ssub_match_drone = smatch_drone[1];
if (ssub_match_drone.str() == str_drone_name_) {
std::smatch smatch_sensor;
fdrone_found = true;
// Ignore topics except those for the sensors
if (std::regex_match(str_topic, smatch_sensor, re_sensor)) {
// Add topic entries for each recognized sensor type
std::string str_sensor_name = smatch_sensor[1].str();
std::string str_sensor_info_name = smatch_sensor[2].str();
// Add sensor name to list
us_sensor.insert(str_sensor_name);
// Add sensor info name to sensor's list
{
auto it_info = umsensor_name_vecinfo_.find(str_sensor_name);
if (it_info == umsensor_name_vecinfo_.end()) {
auto pair =
umsensor_name_vecinfo_.emplace(str_sensor_name, VecStr());
it_info = pair.first;
}
it_info->second.push_back(str_sensor_info_name);
}
// Add sensor info topic to sensor's list
{
auto it_um_info = umsensor_name_uminfo_topic_.find(str_sensor_name);
if (it_um_info == umsensor_name_uminfo_topic_.end()) {
auto pair = umsensor_name_uminfo_topic_.insert(
std::make_pair(str_sensor_name, UMStrStr()));
it_um_info = pair.first;
}
it_um_info->second.insert(
std::make_pair(str_sensor_info_name, str_topic));
}
}
}
}
}
if (!fdrone_found) {
log.ErrorF("Actor \"%s\" not found in the config", str_drone_name_.c_str());
throw Status::NotFound;
}
}
void Drone::Impl::SetTopics(void) {
str_parent_topic_ = str_world_parent_topic_ + "/robots/" + str_drone_name_;
str_sensor_topic_ = str_parent_topic_ + "/sensors";
SetSensorTopics(pworld_.get());
SetRobotInfoTopics();
}
} // namespace client
} // namespace projectairsim
} // namespace microsoft