-
Notifications
You must be signed in to change notification settings - Fork 335
/
vision_env.cpp
713 lines (592 loc) · 21.9 KB
/
vision_env.cpp
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
#include "flightlib/envs/vision_env/vision_env.hpp"
namespace flightlib {
VisionEnv::VisionEnv()
: VisionEnv(getenv("FLIGHTMARE_PATH") +
std::string("/flightpy/configs/vision/config.yaml"),
0) {}
VisionEnv::VisionEnv(const std::string &cfg_path, const int env_id)
: EnvBase() {
// check if configuration file exist
if (!(file_exists(cfg_path))) {
logger_.error("Configuration file %s does not exists.", cfg_path);
}
// load configuration file
cfg_ = YAML::LoadFile(cfg_path);
//
init();
env_id_ = env_id;
}
VisionEnv::VisionEnv(const YAML::Node &cfg_node, const int env_id) : EnvBase() {
cfg_ = cfg_node;
//
init();
env_id_ = env_id;
}
void VisionEnv::init() {
//
is_collision_ = false;
unity_render_offset_ << 0.0, 0.0, 0.0;
goal_linear_vel_ << 0.0, 0.0, 0.0;
cmd_.setZeros();
// create quadrotors
quad_ptr_ = std::make_shared<Quadrotor>();
// update dynamics
QuadrotorDynamics dynamics;
dynamics.updateParams(cfg_);
quad_ptr_->updateDynamics(dynamics);
// define input and output dimension for the environment
obs_dim_ = visionenv::kNObs;
act_dim_ = visionenv::kNAct;
rew_dim_ = 0;
num_detected_obstacles_ = visionenv::kNObstacles;
// load parameters
loadParam(cfg_);
// add camera
if (!configCamera(cfg_)) {
logger_.error(
"Cannot config RGB Camera. Something wrong with the config file");
}
obstacle_cfg_path_ = getenv("FLIGHTMARE_PATH") +
std::string("/flightpy/configs/vision/") +
difficulty_level_ + std::string("/") + env_folder_;
// add dynamic objects
std::string dynamic_object_yaml =
obstacle_cfg_path_ + std::string("/dynamic_obstacles.yaml");
if (!configDynamicObjects(dynamic_object_yaml)) {
logger_.error(
"Cannot config Dynamic Object Yaml. Something wrong with the config "
"file");
}
// add static objects
static_object_csv_ =
obstacle_cfg_path_ + std::string("/static_obstacles.csv");
if (!configStaticObjects(static_object_csv_)) {
logger_.error(
"Cannot config Static Object. Something wrong with the config file");
}
// use single rotor control or bodyrate control
Scalar max_force = quad_ptr_->getDynamics().getForceMax();
Vector<3> max_omega = quad_ptr_->getDynamics().getOmegaMax();
//
act_mean_ << (max_force / quad_ptr_->getMass()) / 2, 0.0, 0.0, 0.0;
act_std_ << (max_force / quad_ptr_->getMass()) / 2, max_omega.x(),
max_omega.y(), max_omega.z();
}
VisionEnv::~VisionEnv() {}
bool VisionEnv::reset(Ref<Vector<>> obs) {
quad_state_.setZero();
pi_act_.setZero();
old_pi_act_.setZero();
is_collision_ = false;
// randomly reset the quadrotor state
// reset position
quad_state_.x(QS::POSX) = uniform_dist_(random_gen_);
quad_state_.x(QS::POSY) = uniform_dist_(random_gen_) * 9.0;
quad_state_.x(QS::POSZ) = uniform_dist_(random_gen_) * 4 + 5.0;
// reset quadrotor with random states
quad_ptr_->reset(quad_state_);
// reset control command
cmd_.t = 0.0;
// use collective thrust and bodyrate control mode
cmd_.setCmdMode(quadcmd::THRUSTRATE);
cmd_.collective_thrust = 0;
cmd_.omega.setZero();
// obtain observations
getObs(obs);
return true;
}
bool VisionEnv::reset(Ref<Vector<>> obs, bool random) { return reset(obs); }
bool VisionEnv::getObs(Ref<Vector<>> obs) {
if (obs.size() != obs_dim_) {
logger_.error("Observation dimension mismatch. %d != %d", obs.size(),
obs_dim_);
return false;
}
// compute rotation matrix
Vector<9> ori = Map<Vector<>>(quad_state_.R().data(), quad_state_.R().size());
// get N most closest obstacles as the observation
Vector<visionenv::kNObstacles * visionenv::kNObstaclesState> obstacle_obs;
getObstacleState(obstacle_obs);
// Observations
obs << goal_linear_vel_, ori, quad_state_.v, obstacle_obs;
return true;
}
bool VisionEnv::getObstacleState(Ref<Vector<>> obs_state) {
if (dynamic_objects_.size() <= 0 || static_objects_.size() <= 0) {
logger_.error("No dynamic or static obstacles.");
return false;
}
// make sure to reset the collision penalty
relative_pos_norm_.clear();
obstacle_radius_.clear();
//
quad_ptr_->getState(&quad_state_);
// compute relative distance to dynamic obstacles
std::vector<Vector<3>, Eigen::aligned_allocator<Vector<3>>> relative_pos;
for (int i = 0; i < (int)dynamic_objects_.size(); i++) {
// compute relative position vector
Vector<3> delta_pos = dynamic_objects_[i]->getPos() - quad_state_.p;
relative_pos.push_back(delta_pos);
// compute relative distance
Scalar obstacle_dist = delta_pos.norm();
// limit observation range
if (obstacle_dist > max_detection_range_) {
obstacle_dist = max_detection_range_;
}
relative_pos_norm_.push_back(obstacle_dist);
// store the obstacle radius
Scalar obs_radius = dynamic_objects_[i]->getScale()[0] / 2;
obstacle_radius_.push_back(obs_radius);
//
if (obstacle_dist < obs_radius) {
is_collision_ = true;
}
}
// compute relatiev distance to static obstacles
for (int i = 0; i < (int)static_objects_.size(); i++) {
// compute relative position vector
Vector<3> delta_pos = static_objects_[i]->getPos() - quad_state_.p;
relative_pos.push_back(delta_pos);
// compute relative distance
Scalar obstacle_dist = delta_pos.norm();
if (obstacle_dist > max_detection_range_) {
obstacle_dist = max_detection_range_;
}
relative_pos_norm_.push_back(obstacle_dist);
// store the obstacle radius
Scalar obs_radius = static_objects_[i]->getScale()[0] / 2;
obstacle_radius_.push_back(obs_radius);
if (obstacle_dist < obs_radius) {
is_collision_ = true;
}
}
// std::cout << relative_pos_norm_ << std::endl;
size_t idx = 0;
for (size_t sort_idx : sort_indexes(relative_pos_norm_)) {
if (idx >= visionenv::kNObstacles) break;
if (idx < relative_pos.size()) {
// if enough obstacles in the environment
if (relative_pos_norm_[sort_idx] <= max_detection_range_) {
// if obstacles are within detection range
obs_state.segment<visionenv::kNObstaclesState>(
idx * visionenv::kNObstaclesState)
<< relative_pos[sort_idx],
obstacle_radius_[sort_idx];
} else {
// if obstacles are beyong detection range
obs_state.segment<visionenv::kNObstaclesState>(
idx * visionenv::kNObstaclesState) =
Vector<4>(max_detection_range_, max_detection_range_,
max_detection_range_, obstacle_radius_[sort_idx]);
}
} else {
// if not enough obstacles in the environment
obs_state.segment<visionenv::kNObstaclesState>(
idx * visionenv::kNObstaclesState) =
Vector<visionenv::kNObstaclesState>(max_detection_range_,
max_detection_range_,
max_detection_range_, 0.0);
}
idx += 1;
}
return true;
}
bool VisionEnv::step(const Ref<Vector<>> act, Ref<Vector<>> obs,
Ref<Vector<>> reward) {
if (!act.allFinite() || act.rows() != act_dim_ || rew_dim_ != reward.rows()) {
return false;
logger_.error(
"Cannot run environment simulation. dimension mismatch or invalid "
"actions.");
}
//
old_pi_act_ = pi_act_;
// compute actual control actions
// act has range between [-1, 1] due to Tanh layer of the NN policy
pi_act_ = act.cwiseProduct(act_std_) + act_mean_;
cmd_.t += sim_dt_;
quad_state_.t += sim_dt_;
// apply old actions to simulate delay
cmd_.collective_thrust = old_pi_act_(0);
cmd_.omega = old_pi_act_.segment<3>(1);
// simulate quadrotor
quad_ptr_->run(cmd_, sim_dt_);
// update quadrotor state and old quad_state
quad_old_state_ = quad_state_;
quad_ptr_->getState(&quad_state_);
// simulate dynamic obstacles
simDynamicObstacles(sim_dt_);
// update observations
getObs(obs);
return computeReward(reward);
}
bool VisionEnv::simDynamicObstacles(const Scalar dt) {
if (dynamic_objects_.size() <= 0) {
logger_.warn(
"No Dynamic Obstacles defined. Skipping dynamic obstacles simulation.");
return false;
}
for (int i = 0; i < int(dynamic_objects_.size()); i++) {
dynamic_objects_[i]->run(sim_dt_);
}
return true;
}
bool VisionEnv::computeReward(Ref<Vector<>> reward) {
// ---------------------- reward function design
// - compute collision penalty
Scalar collision_penalty = 0.0;
size_t idx = 0;
for (size_t sort_idx : sort_indexes(relative_pos_norm_)) {
if (idx >= visionenv::kNObstacles) break;
Scalar relative_dist =
relative_pos_norm_[sort_idx]
? (relative_pos_norm_[sort_idx] > 0) &&
(relative_pos_norm_[sort_idx] < max_detection_range_)
: max_detection_range_;
const Scalar dist_margin = 0.5;
if (relative_pos_norm_[sort_idx] <=
obstacle_radius_[sort_idx] + dist_margin) {
// compute distance penalty
collision_penalty += collision_coeff_ * std::exp(-1.0 * relative_dist);
}
idx += 1;
}
// - tracking a constant linear velocity
Scalar lin_vel_reward =
vel_coeff_ * (quad_state_.v - goal_linear_vel_).norm();
// - angular velocity penalty, to avoid oscillations
const Scalar ang_vel_penalty = angular_vel_coeff_ * quad_state_.w.norm();
// change progress reward as survive reward
const Scalar total_reward =
lin_vel_reward + collision_penalty + ang_vel_penalty + survive_rew_;
// return all reward components for debug purposes
// only the total reward is used by the RL algorithm
reward << lin_vel_reward, collision_penalty, ang_vel_penalty, survive_rew_,
total_reward;
return true;
}
bool VisionEnv::isTerminalState(Scalar &reward) {
// if (is_collision_) {
// reward = -1.0;
// return true;
// }
// simulation time out
if (cmd_.t >= max_t_ - sim_dt_) {
reward = 0.0;
return true;
}
// world boundling box check
// - x, y, and z
const Scalar safty_threshold = 0.1;
bool x_valid = quad_state_.p(QS::POSX) >= world_box_[0] + safty_threshold &&
quad_state_.p(QS::POSX) <= world_box_[1] - safty_threshold;
bool y_valid = quad_state_.p(QS::POSY) >= world_box_[2] + safty_threshold &&
quad_state_.p(QS::POSY) <= world_box_[3] - safty_threshold;
bool z_valid = quad_state_.x(QS::POSZ) >= world_box_[4] + safty_threshold &&
quad_state_.x(QS::POSZ) <= world_box_[5] - safty_threshold;
if (!x_valid || !y_valid || !z_valid) {
reward = -1.0;
return true;
}
return false;
}
bool VisionEnv::getQuadAct(Ref<Vector<>> act) const {
if (cmd_.t >= 0.0 && pi_act_.allFinite() && (act.size() == pi_act_.size())) {
act = pi_act_;
return true;
}
return false;
}
bool VisionEnv::getQuadState(Ref<Vector<>> obs) const {
if (quad_state_.t >= 0.0 && (obs.rows() == visionenv::kNQuadState)) {
obs << quad_state_.t, quad_state_.p, quad_state_.qx, quad_state_.v,
quad_state_.w, quad_state_.a, quad_ptr_->getMotorOmega(),
quad_ptr_->getMotorThrusts();
return true;
}
logger_.error("Get Quadrotor state failed.");
return false;
}
bool VisionEnv::getDepthImage(Ref<DepthImgVector<>> depth_img) {
if (!rgb_camera_ || !rgb_camera_->getEnabledLayers()[0]) {
logger_.error(
"No RGB Camera or depth map is not enabled. Cannot retrieve depth "
"images.");
return false;
}
rgb_camera_->getDepthMap(depth_img_);
depth_img = Map<DepthImgVector<>>((float_t *)depth_img_.data,
depth_img_.rows * depth_img_.cols);
return true;
}
bool VisionEnv::getImage(Ref<ImgVector<>> img, const bool rgb) {
if (!rgb_camera_) {
logger_.error("No Camera! Cannot retrieve Images.");
return false;
}
rgb_camera_->getRGBImage(rgb_img_);
if (rgb_img_.rows != img_height_ || rgb_img_.cols != img_width_) {
logger_.error(
"Image resolution mismatch. Aborting.. Image rows %d != %d, Image cols "
"%d != %d",
rgb_img_.rows, img_height_, rgb_img_.cols, img_width_);
return false;
}
if (!rgb) {
// converting rgb image to gray image
cvtColor(rgb_img_, gray_img_, CV_RGB2GRAY);
// map cv::Mat data to Eiegn::Vector
img = Map<ImgVector<>>(gray_img_.data, gray_img_.rows * gray_img_.cols);
} else {
img = Map<ImgVector<>>(rgb_img_.data, rgb_img_.rows * rgb_img_.cols *
rgb_camera_->getChannels());
}
return true;
}
bool VisionEnv::loadParam(const YAML::Node &cfg) {
if (cfg["environment"]) {
difficulty_level_ = cfg["environment"]["level"].as<std::string>();
env_folder_ = cfg["environment"]["env_folder"].as<std::string>();
world_box_ = cfg["environment"]["world_box"].as<std::vector<Scalar>>();
std::vector<Scalar> goal_vel_vec =
cfg["environment"]["goal_vel"].as<std::vector<Scalar>>();
goal_linear_vel_ = Vector<3>(goal_vel_vec.data());
max_detection_range_ =
cfg["environment"]["max_detection_range"].as<Scalar>();
}
if (cfg["simulation"]) {
sim_dt_ = cfg["simulation"]["sim_dt"].as<Scalar>();
max_t_ = cfg["simulation"]["max_t"].as<Scalar>();
} else {
logger_.error("Cannot load [quadrotor_env] parameters");
return false;
}
if (cfg["rewards"]) {
// load reward coefficients for reinforcement learning
vel_coeff_ = cfg["rewards"]["vel_coeff"].as<Scalar>();
collision_coeff_ = cfg["rewards"]["collision_coeff"].as<Scalar>();
angular_vel_coeff_ = cfg["rewards"]["angular_vel_coeff"].as<Scalar>();
survive_rew_ = cfg["rewards"]["survive_rew"].as<Scalar>();
// load reward settings
reward_names_ = cfg["rewards"]["names"].as<std::vector<std::string>>();
rew_dim_ = reward_names_.size();
} else {
logger_.error("Cannot load [rewards] parameters");
return false;
}
// environment
if (cfg["unity"]) {
unity_render_ = cfg["unity"]["render"].as<bool>();
scene_id_ = cfg["unity"]["scene_id"].as<SceneID>();
}
//
std::string scene_file =
getenv("FLIGHTMARE_PATH") + std::string("/flightpy/configs/scene.yaml");
// check if configuration file exist
if (!(file_exists(scene_file))) {
logger_.error("Unity scene configuration file %s does not exists.",
scene_file);
}
// load configuration file
YAML::Node scene_cfg_node = YAML::LoadFile(scene_file);
std::string scene_idx = "scene_" + std::to_string(scene_id_);
std::vector<Scalar> render_offset =
scene_cfg_node[scene_idx]["render_offset"].as<std::vector<Scalar>>();
unity_render_offset_ = Vector<3>(render_offset.data());
return true;
}
bool VisionEnv::configDynamicObjects(const std::string &yaml_file) {
//
if (!(file_exists(yaml_file))) {
logger_.error("Configuration file %s does not exists.", yaml_file);
return false;
}
YAML::Node cfg_node = YAML::LoadFile(yaml_file);
// logger_.info("Configuring dynamic objects");
int num_objects = cfg_node["N"].as<int>();
// create static objects
for (int i = 0; i < num_objects; i++) {
std::string object_id = "Object" + std::to_string(i + 1);
std::string prefab_id = cfg_node[object_id]["prefab"].as<std::string>();
std::shared_ptr<UnityObject> obj =
std::make_shared<UnityObject>(object_id, prefab_id);
// load location, rotation and size
std::vector<Scalar> posvec =
(cfg_node[object_id]["position"]).as<std::vector<Scalar>>();
std::vector<Scalar> rotvec =
(cfg_node[object_id]["rotation"]).as<std::vector<Scalar>>();
std::vector<Scalar> scalevec =
(cfg_node[object_id]["scale"]).as<std::vector<Scalar>>();
obj->setPosition(Vector<3>(posvec.data()));
obj->setRotation(Quaternion(rotvec.data()));
// actual size in meters
obj->setSize(Vector<3>(1.0, 1.0, 1.0));
// scale of the original size
obj->setScale(Vector<3>(scalevec.data()));
std::string csv_name = cfg_node[object_id]["csvtraj"].as<std::string>();
std::string csv_file = obstacle_cfg_path_ + std::string("/csvtrajs/") +
csv_name + std::string(".csv");
if (!(file_exists(csv_file))) {
logger_.error("Configuration file %s does not exists.", csv_file);
return false;
}
obj->loadTrajectory(csv_file);
dynamic_objects_.push_back(obj);
}
num_dynamic_objects_ = dynamic_objects_.size();
return true;
}
bool VisionEnv::configStaticObjects(const std::string &csv_file) {
//
if (!(file_exists(csv_file))) {
logger_.error("Configuration file %s does not exists.", csv_file);
return false;
}
std::ifstream infile(csv_file);
int i = 0;
for (auto &row : CSVRange(infile)) {
// Read column 0 for time
std::string object_id = "StaticObject" + std::to_string(i + 1);
std::string prefab_id = (std::string)row[0];
//
std::shared_ptr<UnityObject> obj =
std::make_shared<UnityObject>(object_id, prefab_id);
//
Vector<3> pos;
pos << std::stod((std::string)row[1]), std::stod((std::string)row[2]),
std::stod((std::string)row[3]);
Quaternion quat;
quat.w() = std::stod((std::string)row[4]);
quat.x() = std::stod((std::string)row[5]);
quat.y() = std::stod((std::string)row[6]);
quat.z() = std::stod((std::string)row[7]);
Vector<3> scale;
scale << std::stod((std::string)row[8]), std::stod((std::string)row[9]),
std::stod((std::string)row[10]);
//
obj->setPosition(pos);
obj->setRotation(quat);
// actual size in meters
obj->setSize(Vector<3>(1.0, 1.0, 1.0));
// scale of the original size
obj->setScale(scale);
static_objects_.push_back(obj);
}
num_static_objects_ = static_objects_.size();
return true;
}
bool VisionEnv::configCamera(const YAML::Node &cfg) {
if (!cfg["rgb_camera"]) {
logger_.error("Cannot config RGB Camera");
return false;
}
if (!cfg["rgb_camera"]["on"].as<bool>()) {
logger_.warn("Camera is off. Please turn it on.");
return false;
}
if (quad_ptr_->getNumCamera() >= 1) {
logger_.warn("Camera has been added. Skipping the camera configuration.");
return false;
}
// create camera
rgb_camera_ = std::make_shared<RGBCamera>();
// load camera settings
std::vector<Scalar> t_BC_vec =
cfg["rgb_camera"]["t_BC"].as<std::vector<Scalar>>();
std::vector<Scalar> r_BC_vec =
cfg["rgb_camera"]["r_BC"].as<std::vector<Scalar>>();
//
Vector<3> t_BC(t_BC_vec.data());
Matrix<3, 3> r_BC =
(AngleAxis(r_BC_vec[2] * M_PI / 180.0, Vector<3>::UnitZ()) *
AngleAxis(r_BC_vec[1] * M_PI / 180.0, Vector<3>::UnitY()) *
AngleAxis(r_BC_vec[0] * M_PI / 180.0, Vector<3>::UnitX()))
.toRotationMatrix();
std::vector<bool> post_processing = {false, false, false};
post_processing[0] = cfg["rgb_camera"]["enable_depth"].as<bool>();
post_processing[1] = cfg["rgb_camera"]["enable_segmentation"].as<bool>();
post_processing[2] = cfg["rgb_camera"]["enable_opticalflow"].as<bool>();
//
rgb_camera_->setFOV(cfg["rgb_camera"]["fov"].as<Scalar>());
rgb_camera_->setWidth(cfg["rgb_camera"]["width"].as<int>());
rgb_camera_->setChannels(cfg["rgb_camera"]["channels"].as<int>());
rgb_camera_->setHeight(cfg["rgb_camera"]["height"].as<int>());
rgb_camera_->setRelPose(t_BC, r_BC);
rgb_camera_->setPostProcessing(post_processing);
// add camera to the quadrotor
quad_ptr_->addRGBCamera(rgb_camera_);
// adapt parameters
img_width_ = rgb_camera_->getWidth();
img_height_ = rgb_camera_->getHeight();
rgb_img_ = cv::Mat::zeros(img_height_, img_width_,
CV_MAKETYPE(CV_8U, rgb_camera_->getChannels()));
depth_img_ = cv::Mat::zeros(img_height_, img_width_, CV_32FC1);
return true;
}
bool VisionEnv::addQuadrotorToUnity(const std::shared_ptr<UnityBridge> bridge) {
if (!quad_ptr_) return false;
bridge->addQuadrotor(quad_ptr_);
for (int i = 0; i < (int)dynamic_objects_.size(); i++) {
bridge->addDynamicObject(dynamic_objects_[i]);
}
//
bridge->setRenderOffset(unity_render_offset_);
bridge->setObjectCSV(static_object_csv_);
return true;
}
bool VisionEnv::setUnity(bool render) {
unity_render_ = render;
if (!unity_render_ || unity_bridge_ptr_ != nullptr) {
logger_.warn(
"Unity render is False or Flightmare Bridge has been already created. "
"Cannot set Unity.");
return false;
}
// create unity bridge
unity_bridge_ptr_ = UnityBridge::getInstance();
// add objects to Unity
addQuadrotorToUnity(unity_bridge_ptr_);
logger_.info("Flightmare Bridge created.");
return true;
}
bool VisionEnv::connectUnity(void) {
if (unity_bridge_ptr_ == nullptr) return false;
unity_ready_ = unity_bridge_ptr_->connectUnity(scene_id_);
return unity_ready_;
}
FrameID VisionEnv::updateUnity(const FrameID frame_id) {
if (unity_render_ && unity_ready_) {
unity_bridge_ptr_->getRender(frame_id);
return unity_bridge_ptr_->handleOutput(frame_id);
} else {
return 0;
}
}
void VisionEnv::disconnectUnity(void) {
if (unity_bridge_ptr_ != nullptr) {
unity_bridge_ptr_->disconnectUnity();
unity_ready_ = false;
} else {
logger_.warn("Flightmare Unity Bridge is not initialized.");
}
}
int VisionEnv::getNumDetectedObstacles(void) { return num_detected_obstacles_; }
std::ostream &operator<<(std::ostream &os, const VisionEnv &vision_env) {
os.precision(3);
os << "Vision Environment:\n"
<< "obs dim = [" << vision_env.obs_dim_ << "]\n"
<< "act dim = [" << vision_env.act_dim_ << "]\n"
<< "#dynamic objects= [" << vision_env.num_dynamic_objects_ << "]\n"
<< "#static objects= [" << vision_env.num_static_objects_ << "]\n"
<< "obstacle dim = [" << vision_env.num_detected_obstacles_ << "]\n"
<< "sim dt = [" << vision_env.sim_dt_ << "]\n"
<< "max_t = [" << vision_env.max_t_ << "]\n"
<< "act_mean = [" << vision_env.act_mean_.transpose() << "]\n"
<< "act_std = [" << vision_env.act_std_.transpose() << "]\n"
<< "obs_mean = [" << vision_env.obs_mean_.transpose() << "]\n"
<< "obs_std = [" << vision_env.obs_std_.transpose() << "]"
<< std::endl;
os.precision();
return os;
}
} // namespace flightlib