Skip to content

Commit

Permalink
added switch world API && world can be setuped in YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
hrnycze committed Aug 15, 2024
1 parent 3f70555 commit 25c32e1
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 3 deletions.
15 changes: 15 additions & 0 deletions config/unreal_simulator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ dynamic_rtf: true
# TODO describe
collisions: true

# 0 - LOW
# 1 - MEDIUM
# 2 - HIGH
# 4 - EPIC
# 5 - CINEMATIC
ueds_graphics_settings_enum: 0

# 0 - VALLEY,
# 1 - FOREST,
# 2 - INFINITE_FOREST,
# 3 - WAREHOUSE,
# 4 - CAVE,
# 5 - ERDING_AIRBASE
ueds_world_level_name_enum: 2

sensors:

lidar:
Expand Down
2 changes: 2 additions & 0 deletions include/ueds_connector/game_mode_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class GameModeController : public SocketClient {
std::pair<bool, double> GetTime();

bool SetGraphicsSettings(const Serializable::GameMode::GraphicsSettingsEnum& graphicsSettings);

bool SwitchWorldLevel(const Serializable::GameMode::WorldLevelEnum& worldLevelEnum);
};

} // namespace ueds_connector
33 changes: 32 additions & 1 deletion include/ueds_connector/serialization/serializable_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ namespace SetGraphicsSettings
template <class Archive>
void serialize(Archive& archive) {
archive(cereal::base_class<Common::NetworkRequest>(this), graphicsSettings);
}
}
};

struct Response : public Common::NetworkResponse
Expand All @@ -1034,6 +1034,37 @@ namespace SetGraphicsSettings

}// namespace SetGraphicsSettings

enum WorldLevelEnum : unsigned short
{
VALLEY,
FOREST,
INFINITE_FOREST,
WAREHOUSE,
CAVE,
ERDING_AIRBASE
};

namespace SwitchWorldLevel
{
struct Request : public Common::NetworkRequest
{
Request() : Common::NetworkRequest(MessageType::switch_world_level){};

WorldLevelEnum worldLevelEnum;

template <class Archive>
void serialize(Archive& archive) {
archive(cereal::base_class<Common::NetworkRequest>(this), worldLevelEnum);
}
};

struct Response : public Common::NetworkResponse
{
Response() : Common::NetworkResponse(static_cast<unsigned short>(MessageType::switch_world_level)){};
explicit Response(bool _status) : Common::NetworkResponse(MessageType::switch_world_level, _status){};
};
}// namespace SwitchWorldLevel

namespace GetFps
{
struct Request : public Common::NetworkRequest
Expand Down
14 changes: 14 additions & 0 deletions src/ueds_connector/game_mode_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ bool GameModeController::SetGraphicsSettings(const Serializable::GameMode::Graph
}
//}

/* SwitchWorldLevel() {*/
bool GameModeController::SwitchWorldLevel(const Serializable::GameMode::WorldLevelEnum& worldLevelEnum){

Serializable::GameMode::SwitchWorldLevel::Request request{};
request.worldLevelEnum = static_cast<Serializable::GameMode::WorldLevelEnum>(worldLevelEnum);

Serializable::GameMode::SwitchWorldLevel::Response response{};
const auto status = Request(request, response);
const auto success = status && response.status;

return success;
}
//}

/* getFps() //{ */

std::pair<bool, float> GameModeController::GetFps() {
Expand Down
42 changes: 41 additions & 1 deletion src/unreal_simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ class UnrealSimulator : public nodelet::Nodelet {
std::mutex mutex_wall_time_offset_;

double ueds_fps_ = 0;
int ueds_world_level_name_enum_ = 2U;
int ueds_graphics_settings_enum_ = 0U;

std::vector<double> last_rgb_ue_stamp_;
std::vector<double> last_rgb_seg_ue_stamp_;
Expand Down Expand Up @@ -288,6 +290,9 @@ void UnrealSimulator::onInit() {
param_loader.addYamlFileFromParam("config");
param_loader.addYamlFileFromParam("config_uavs");

param_loader.loadParam("ueds_graphics_settings_enum", ueds_graphics_settings_enum_);
param_loader.loadParam("ueds_world_level_name_enum", ueds_world_level_name_enum_);

param_loader.loadParam("simulation_rate", _simulation_rate_);
param_loader.loadParam("realtime_factor", drs_params_.realtime_factor);
param_loader.loadParam("dynamic_rtf", drs_params_.dynamic_rtf);
Expand Down Expand Up @@ -406,7 +411,41 @@ void UnrealSimulator::onInit() {
ros::shutdown();
}

Serializable::GameMode::GraphicsSettingsEnum graphicsSettings = Serializable::GameMode::GraphicsSettingsEnum::LOW;
// | --------------------- Set graphical settings and choose World Level --------------------- |

Serializable::GameMode::WorldLevelEnum worldLevelEnum = Serializable::GameMode::WorldLevelEnum(ueds_world_level_name_enum_);
res = ueds_game_controller_->SwitchWorldLevel(worldLevelEnum);
if(res){
ROS_INFO("[UnrealSimulator] World was switched succesfully.");
}else{
ROS_ERROR("[UnrealSimulator] World was not switched succesfully.");
}

res = ueds_game_controller_->Disconnect();
if(res){
ROS_INFO("[UnrealSimulator] ueds_game_controller_ was Disconnected succesfully.");
}else{
ROS_ERROR("[UnrealSimulator] ueds_game_controller_ was not Disconnected succesfully.");
}

ROS_WARN("SLEEP START");
//ros::Duration(1.0).sleep();
std::this_thread::sleep_for(std::chrono::seconds(1));
ROS_WARN("SLEEP STOP");

//ueds_game_controller_ = std::make_unique<ueds_connector::GameModeController>(LOCALHOST, 8000);
while (true) {
bool connect_result = ueds_game_controller_->Connect();
if (connect_result != 1) {
ROS_ERROR("[UnrealSimulator]: Error connecting to Unreal's game mode controller, connect_result was %d", connect_result);
} else {
break;
}
//ros::Duration(1.0).sleep();
std::this_thread::sleep_for(std::chrono::seconds(1));
}

Serializable::GameMode::GraphicsSettingsEnum graphicsSettings = Serializable::GameMode::GraphicsSettingsEnum(ueds_graphics_settings_enum_);
res = ueds_game_controller_->SetGraphicsSettings(graphicsSettings);

if(res){
Expand All @@ -421,6 +460,7 @@ void UnrealSimulator::onInit() {

const std::string uav_name = uav_names[i];

ROS_INFO("[UnrealSimulator]: %s spawning .......", uav_name.c_str());
const auto [resSpawn, port] = ueds_game_controller_->SpawnDrone();

if (!resSpawn) {
Expand Down
2 changes: 1 addition & 1 deletion tmux/one_drone/session.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ socket_name: mrs
attach: false
tmux_options: -f /etc/ctu-mrs/tmux.conf
# you can modify these
pre_window: export UAV_NAME=uav1; export RUN_TYPE=simulation; export UAV_TYPE=x500 #; source ~/workspace/devel/setup.bash
pre_window: export UAV_NAME=uav1; export RUN_TYPE=simulation; export UAV_TYPE=x500; source ~/workspace/devel/setup.bash
startup_window: goto
windows:
- roscore:
Expand Down

0 comments on commit 25c32e1

Please sign in to comment.