Skip to content

Commit

Permalink
Store correct vehicle settings instead of default one
Browse files Browse the repository at this point in the history
  • Loading branch information
rajat2004 committed Mar 5, 2021
1 parent 1e3451b commit e574c72
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
15 changes: 11 additions & 4 deletions AirLib/include/common/AirSimSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,12 +436,19 @@ struct AirSimSettings {
}

// This is for the case when a new vehicle is made on the fly, at runtime
void addVehicleSetting(const VehicleSetting &vehicle_setting)
void addVehicleSetting(const std::string& vehicle_name, const std::string& vehicle_type, const Pose& pose, const std::string& pawn_path="")
{
std::unique_ptr<VehicleSetting> vehicle_setting_p = std::unique_ptr<VehicleSetting>(new VehicleSetting());
auto vehicle_setting = std::unique_ptr<VehicleSetting>(new VehicleSetting());

// Usually we have a pointer to an entry from the json, but here we have to make a new one
vehicles[vehicle_setting.vehicle_name] = std::move(vehicle_setting_p);
vehicle_setting->vehicle_name = vehicle_name;
vehicle_setting->vehicle_type = vehicle_type;
vehicle_setting->position = pose.position;
vehicle_setting->pawn_path = pawn_path;

VectorMath::toEulerianAngle(pose.orientation, vehicle_setting->rotation.pitch,
vehicle_setting->rotation.roll, vehicle_setting->rotation.yaw);

vehicles[vehicle_name] = std::move(vehicle_setting);
}

const VehicleSetting* getVehicleSetting(const std::string& vehicle_name) const
Expand Down
6 changes: 1 addition & 5 deletions Unreal/Plugins/AirSim/Source/SimMode/SimModeBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,17 +609,13 @@ std::unique_ptr<PawnSimApi> ASimModeBase::createVehicleApi(APawn* vehicle_pawn)
return vehicle_sim_api;
}

bool ASimModeBase::createVehicleAtRuntime(const AirSimSettings::VehicleSetting &vehicle_setting)
bool ASimModeBase::createVehicleAtRuntime(const AirSimSettings::VehicleSetting& vehicle_setting)
{
if (!isVehicleTypeSupported(vehicle_setting.vehicle_type)) {
Utils::log(Utils::stringf("Vehicle type %s is not supported in this game mode", vehicle_setting.vehicle_type.c_str()), Utils::kLogLevelWarn);
return false;
}

// Retroactively adjust AirSimSettings, so it's like we knew about this vehicle all along
// (Other places in the code use this for reference)
AirSimSettings::singleton().addVehicleSetting(vehicle_setting);

auto spawned_pawn = createVehiclePawn(vehicle_setting);

auto vehicle_sim_api = createVehicleApi(spawned_pawn);
Expand Down
13 changes: 5 additions & 8 deletions Unreal/Plugins/AirSim/Source/WorldSimApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,21 +270,18 @@ void WorldSimApi::setTimeOfDay(bool is_enabled, const std::string& start_datetim

bool WorldSimApi::createVehicleAtRuntime(const std::string& vehicle_name, const std::string& vehicle_type, const Pose& pose, const std::string& pawn_path)
{
// Create settings object
AirSimSettings::VehicleSetting vehicle_setting;
// TODO: Figure out a better way to add more fields
// Maybe allow passing a JSON string for the vehicle settings?

vehicle_setting.vehicle_name = vehicle_name;
vehicle_setting.vehicle_type = vehicle_type;
vehicle_setting.pawn_path = pawn_path;
vehicle_setting.position = pose.position;
VectorMath::toEulerianAngle(pose.orientation, vehicle_setting.rotation.pitch, vehicle_setting.rotation.roll, vehicle_setting.rotation.yaw);
// Retroactively adjust AirSimSettings, so it's like we knew about this vehicle all along
AirSimSettings::singleton().addVehicleSetting(vehicle_name, vehicle_type, pose, pawn_path);

bool result;

// We need to run this code on the main game thread, since it iterates over actors
FGraphEventRef task = FFunctionGraphTask::CreateAndDispatchWhenReady([&]()
{
result = simmode_->createVehicleAtRuntime(vehicle_setting);
result = simmode_->createVehicleAtRuntime(*(AirSimSettings::singleton().getVehicleSetting(vehicle_name)));
}, TStatId(), NULL, ENamedThreads::GameThread);

// Wait for the result
Expand Down

0 comments on commit e574c72

Please sign in to comment.