From 802f941fae3347d46b3db504103588980ab6d14c Mon Sep 17 00:00:00 2001 From: Axel Date: Mon, 23 Nov 2020 16:04:17 +0100 Subject: [PATCH] Added function to set custom carsim simfile. Added option to use ue4 carsim road definition or unreal callback method. --- LibCarla/source/carla/client/Vehicle.cpp | 8 ++++-- LibCarla/source/carla/client/Vehicle.h | 5 +++- .../source/carla/client/detail/Client.cpp | 8 ++++-- LibCarla/source/carla/client/detail/Client.h | 5 ++++ .../source/carla/client/detail/Simulator.h | 8 ++++-- PythonAPI/carla/source/libcarla/Actor.cpp | 3 ++- .../Carla/Source/Carla/Server/CarlaServer.cpp | 26 +++++++++++++++++-- .../Carla/Vehicle/CarlaWheeledVehicle.cpp | 15 ++++++++++- .../Carla/Vehicle/CarlaWheeledVehicle.h | 5 +++- 9 files changed, 71 insertions(+), 12 deletions(-) diff --git a/LibCarla/source/carla/client/Vehicle.cpp b/LibCarla/source/carla/client/Vehicle.cpp index e9a444964..e15d36c5a 100644 --- a/LibCarla/source/carla/client/Vehicle.cpp +++ b/LibCarla/source/carla/client/Vehicle.cpp @@ -88,8 +88,12 @@ namespace client { return boost::static_pointer_cast(GetWorld().GetActor(id)); } - void Vehicle::SetCarSimEnabled(bool enabled) { - GetEpisode().Lock()->SetCarSimEnabled(*this, enabled); + void Vehicle::SetCarSimEnabled(bool enabled, std::string simfile_path) { + GetEpisode().Lock()->SetCarSimEnabled(*this, enabled, simfile_path); + } + + void Vehicle::UseCarSimRoad(bool enabled) { + GetEpisode().Lock()->UseCarSimRoad(*this, enabled); } } // namespace client diff --git a/LibCarla/source/carla/client/Vehicle.h b/LibCarla/source/carla/client/Vehicle.h index d839243bf..6c102e131 100644 --- a/LibCarla/source/carla/client/Vehicle.h +++ b/LibCarla/source/carla/client/Vehicle.h @@ -88,7 +88,10 @@ namespace client { SharedPtr GetTrafficLight() const; /// Enables CarSim simulation if it is availiable - void SetCarSimEnabled(bool enabled); + void SetCarSimEnabled(bool enabled, std::string simfile_path); + + /// Enables the use of CarSim internal road definition instead of unreal's + void UseCarSimRoad(bool enabled); private: diff --git a/LibCarla/source/carla/client/detail/Client.cpp b/LibCarla/source/carla/client/detail/Client.cpp index 6775cdb28..a69f1129f 100644 --- a/LibCarla/source/carla/client/detail/Client.cpp +++ b/LibCarla/source/carla/client/detail/Client.cpp @@ -330,8 +330,12 @@ namespace detail { _pimpl->AsyncCall("apply_control_to_vehicle", vehicle, control); } - void Client::SetCarSimEnabled(rpc::ActorId vehicle, bool enabled) { - _pimpl->AsyncCall("set_carsim_enabled", vehicle, enabled); + void Client::SetCarSimEnabled(rpc::ActorId vehicle, bool enabled, std::string simfile_path) { + _pimpl->AsyncCall("set_carsim_enabled", vehicle, enabled, simfile_path); + } + + void Client::UseCarSimRoad(rpc::ActorId vehicle, bool enabled) { + _pimpl->AsyncCall("use_carsim_road", vehicle, enabled); } void Client::ApplyControlToWalker(rpc::ActorId walker, const rpc::WalkerControl &control) { diff --git a/LibCarla/source/carla/client/detail/Client.h b/LibCarla/source/carla/client/detail/Client.h index 1befea8b3..c1b3f681e 100644 --- a/LibCarla/source/carla/client/detail/Client.h +++ b/LibCarla/source/carla/client/detail/Client.h @@ -212,6 +212,11 @@ namespace detail { const rpc::VehicleControl &control); void SetCarSimEnabled( + rpc::ActorId vehicle, + bool enabled, + std::string simfile_path); + + void UseCarSimRoad( rpc::ActorId vehicle, bool enabled); diff --git a/LibCarla/source/carla/client/detail/Simulator.h b/LibCarla/source/carla/client/detail/Simulator.h index bca66eea3..70315b309 100644 --- a/LibCarla/source/carla/client/detail/Simulator.h +++ b/LibCarla/source/carla/client/detail/Simulator.h @@ -445,8 +445,12 @@ namespace detail { _client.SetLightStateToVehicle(vehicle.GetId(), light_state); } - void SetCarSimEnabled(Vehicle &vehicle, bool enabled) { - _client.SetCarSimEnabled(vehicle.GetId(), enabled); + void SetCarSimEnabled(Vehicle &vehicle, bool enabled, std::string simfile_path) { + _client.SetCarSimEnabled(vehicle.GetId(), enabled, simfile_path); + } + + void UseCarSimRoad(Vehicle &vehicle, bool enabled) { + _client.UseCarSimRoad(vehicle.GetId(), enabled); } /// @} diff --git a/PythonAPI/carla/source/libcarla/Actor.cpp b/PythonAPI/carla/source/libcarla/Actor.cpp index 35521ecb0..7b0b98357 100644 --- a/PythonAPI/carla/source/libcarla/Actor.cpp +++ b/PythonAPI/carla/source/libcarla/Actor.cpp @@ -141,7 +141,8 @@ void export_actor() { .def("get_traffic_light_state", &cc::Vehicle::GetTrafficLightState) .def("is_at_traffic_light", &cc::Vehicle::IsAtTrafficLight) .def("get_traffic_light", &cc::Vehicle::GetTrafficLight) - .def("set_carsim_enabled", &cc::Vehicle::SetCarSimEnabled) + .def("set_carsim_enabled", &cc::Vehicle::SetCarSimEnabled, (arg("enabled"), arg("simfile_path") = "")) + .def("use_carsim_road", &cc::Vehicle::UseCarSimRoad, (arg("enabled"))) .def(self_ns::str(self_ns::self)) ; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp index a3154ecd2..56b4b3d14 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp @@ -1083,9 +1083,11 @@ void FCarlaServer::FPimpl::BindActions() return R::Success(); }; +//-----CARSIM-------------------------------- BIND_SYNC(set_carsim_enabled) << [this]( cr::ActorId ActorId, - bool bEnabled) -> R + bool bEnabled, + std::string SimfilePath) -> R { REQUIRE_CARLA_EPISODE(); auto ActorView = Episode->FindActor(ActorId); @@ -1098,9 +1100,29 @@ void FCarlaServer::FPimpl::BindActions() { RESPOND_ERROR("unable to set carsim: not actor is not a vehicle"); } - Vehicle->SetCarSimEnabled(bEnabled); + Vehicle->SetCarSimEnabled(bEnabled, carla::rpc::ToFString(SimfilePath)); return R::Success(); }; + + BIND_SYNC(use_carsim_road) << [this]( + cr::ActorId ActorId, + bool bEnabled) -> R + { + REQUIRE_CARLA_EPISODE(); + auto ActorView = Episode->FindActor(ActorId); + if (!ActorView.IsValid()) + { + RESPOND_ERROR("unable to set carsim road: actor not found"); + } + auto Vehicle = Cast(ActorView.GetActor()); + if (Vehicle == nullptr) + { + RESPOND_ERROR("unable to set carsim road: not actor is not a vehicle"); + } + Vehicle->UseCarSimRoad(bEnabled); + return R::Success(); + }; +//-----CARSIM-------------------------------- // ~~ Traffic lights ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BIND_SYNC(set_traffic_light_state) << [this]( diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/CarlaWheeledVehicle.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/CarlaWheeledVehicle.cpp index 7333be241..64b126959 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/CarlaWheeledVehicle.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/CarlaWheeledVehicle.cpp @@ -183,6 +183,7 @@ void ACarlaWheeledVehicle::FlushVehicleControl() { if (bCarSimEnabled) { + //-----CARSIM-------------------------------- CarSimMovementComponent->SetThrottleInput(InputControl.Control.Throttle); CarSimMovementComponent->SetSteeringInput(InputControl.Control.Steer); CarSimMovementComponent->SetBrakeInput(InputControl.Control.Brake); @@ -205,6 +206,7 @@ void ACarlaWheeledVehicle::FlushVehicleControl() // } // } InputControl.Control.Gear = CarSimMovementComponent->GetCurrentGear(); + //----------------------------------------- } else { @@ -487,14 +489,16 @@ void ACarlaWheeledVehicle::SetVehicleLightState(const FVehicleLightState &LightS } //-----CARSIM-------------------------------- -void ACarlaWheeledVehicle::SetCarSimEnabled(bool bEnabled) +void ACarlaWheeledVehicle::SetCarSimEnabled(bool bEnabled, FString SimfilePath) { carla::log_warning("Enabling CarSim", bEnabled); bCarSimEnabled = bEnabled; if (bCarSimEnabled) { + carla::log_warning("Loading simfile:", carla::rpc::FromFString(SimfilePath)); GetVehicleMovementComponent()->SetComponentTickEnabled(false); GetVehicleMovementComponent()->Deactivate(); + CarSimMovementComponent->VsConfigFile = SimfilePath; CarSimMovementComponent->Activate(); CarSimMovementComponent->ResetVsVehicle(false); CarSimMovementComponent->SyncVsVehicleLocOri(); @@ -507,7 +511,16 @@ void ACarlaWheeledVehicle::SetCarSimEnabled(bool bEnabled) GetVehicleMovementComponent()->Activate(); CarSimMovementComponent->SetComponentTickEnabled(false); CarSimMovementComponent->Deactivate(); + CarSimMovementComponent->VsConfigFile = ""; GetMesh()->SetSimulatePhysics(true); } } + +void ACarlaWheeledVehicle::UseCarSimRoad(bool bEnabled) +{ + carla::log_warning("Enabling CarSim Road", bEnabled); + CarSimMovementComponent->UseVehicleSimRoad = bEnabled; + CarSimMovementComponent->ResetVsVehicle(false); + CarSimMovementComponent->SyncVsVehicleLocOri(); +} //------------------------------------------- diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/CarlaWheeledVehicle.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/CarlaWheeledVehicle.h index 580b72364..6e904666b 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/CarlaWheeledVehicle.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/CarlaWheeledVehicle.h @@ -245,7 +245,10 @@ private: public: UFUNCTION(Category="CARLA Wheeled Vehicle", BlueprintCallable) - void SetCarSimEnabled(bool bEnabled); + void SetCarSimEnabled(bool bEnabled, FString SimfilePath = ""); + + UFUNCTION(Category="CARLA Wheeled Vehicle", BlueprintCallable) + void UseCarSimRoad(bool bEnabled); private: