From d543c3812ee159ff5c66526391278515e127d0c0 Mon Sep 17 00:00:00 2001 From: nsubiron Date: Thu, 26 Jul 2018 22:04:44 +0200 Subject: [PATCH] Add function to apply control to vehicles --- LibCarla/source/carla/client/Actor.h | 5 --- LibCarla/source/carla/client/Client.cpp | 3 +- LibCarla/source/carla/client/Vehicle.h | 31 +++++++++++++++++++ LibCarla/source/carla/rpc/VehicleControl.h | 21 +++++++++++++ PythonAPI/source/libcarla/Actor.cpp | 14 ++++++++- .../Source/Carla/Server/TheNewCarlaServer.cpp | 18 +++++++++-- 6 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 LibCarla/source/carla/client/Vehicle.h diff --git a/LibCarla/source/carla/client/Actor.h b/LibCarla/source/carla/client/Actor.h index 879151ed7..c7c641b1e 100644 --- a/LibCarla/source/carla/client/Actor.h +++ b/LibCarla/source/carla/client/Actor.h @@ -8,7 +8,6 @@ #include "carla/Debug.h" #include "carla/NonCopyable.h" -#include "carla/client/Control.h" #include "carla/client/Memory.h" #include "carla/client/World.h" #include "carla/rpc/Actor.h" @@ -40,10 +39,6 @@ namespace client { return _world; } - void ApplyControl(const VehicleControl &control) { - _world->GetClient().ApplyControlToActor(*this, control); - } - const auto &Serialize() const { return _actor; } diff --git a/LibCarla/source/carla/client/Client.cpp b/LibCarla/source/carla/client/Client.cpp index 70ab33ad0..7bc92fe33 100644 --- a/LibCarla/source/carla/client/Client.cpp +++ b/LibCarla/source/carla/client/Client.cpp @@ -9,6 +9,7 @@ #include "carla/client/Actor.h" #include "carla/client/Control.h" #include "carla/client/Sensor.h" +#include "carla/client/Vehicle.h" #include "carla/client/World.h" namespace carla { @@ -31,7 +32,7 @@ namespace client { if (actor.IsASensor()) { return SharedPtr(new Sensor{actor, GetWorld()}); } - return SharedPtr(new Actor{actor, GetWorld()}); + return SharedPtr(new Vehicle{actor, GetWorld()}); } void Client::ApplyControlToActor( diff --git a/LibCarla/source/carla/client/Vehicle.h b/LibCarla/source/carla/client/Vehicle.h new file mode 100644 index 000000000..e0dfb328d --- /dev/null +++ b/LibCarla/source/carla/client/Vehicle.h @@ -0,0 +1,31 @@ +// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#pragma once + +#include "carla/client/Actor.h" +#include "carla/client/Control.h" + +namespace carla { +namespace client { + + class Vehicle : public Actor { + public: + + void ApplyControl(const VehicleControl &control) { + GetWorld()->GetClient().ApplyControlToActor(*this, control); + } + + private: + + friend class Client; + + template + Vehicle(Args && ... args) : Actor(std::forward(args)...) {} + }; + +} // namespace client +} // namespace carla diff --git a/LibCarla/source/carla/rpc/VehicleControl.h b/LibCarla/source/carla/rpc/VehicleControl.h index 018977f85..bc7447cf5 100644 --- a/LibCarla/source/carla/rpc/VehicleControl.h +++ b/LibCarla/source/carla/rpc/VehicleControl.h @@ -34,6 +34,27 @@ namespace rpc { bool hand_brake = false; bool reverse = false; +#ifdef LIBCARLA_INCLUDED_FROM_UE4 + + VehicleControl(const FVehicleControl &Control) + : throttle(Control.Throttle), + steer(Control.Steer), + brake(Control.Brake), + hand_brake(Control.bHandBrake), + reverse(Control.bReverse) {} + + operator FVehicleControl() const { + FVehicleControl Control; + Control.Throttle = throttle; + Control.Steer = steer; + Control.Brake = brake; + Control.bHandBrake = hand_brake; + Control.bReverse = reverse; + return Control; + } + +#endif // LIBCARLA_INCLUDED_FROM_UE4 + MSGPACK_DEFINE_ARRAY( throttle, steer, diff --git a/PythonAPI/source/libcarla/Actor.cpp b/PythonAPI/source/libcarla/Actor.cpp index 3040727a8..1a59a889d 100644 --- a/PythonAPI/source/libcarla/Actor.cpp +++ b/PythonAPI/source/libcarla/Actor.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include @@ -40,6 +41,11 @@ namespace client { return out; } + std::ostream &operator<<(std::ostream &out, const Vehicle &vehicle) { + out << "Vehicle(id=" << vehicle.GetId() << ", type=" << vehicle.GetTypeId() << ')'; + return out; + } + std::ostream &operator<<(std::ostream &out, const Sensor &sensor) { out << "Sensor(id=" << sensor.GetId() << ", type=" << sensor.GetTypeId() << ')'; return out; @@ -51,6 +57,7 @@ namespace client { void export_actor() { using namespace boost::python; namespace cc = carla::client; + namespace cr = carla::rpc; class_>("Image", no_init) .add_property("frame_number", &cc::Image::GetFrameNumber) @@ -72,7 +79,11 @@ void export_actor() { return self.GetTypeId(); }) .def("get_world", &cc::Actor::GetWorld) - .def("apply_control", &cc::Actor::ApplyControl) + .def(self_ns::str(self_ns::self)) + ; + + class_, boost::noncopyable, boost::shared_ptr>("Vehicle", no_init) + .def("apply_control", &cc::Vehicle::ApplyControl) .def(self_ns::str(self_ns::self)) ; @@ -103,5 +114,6 @@ void export_actor() { } }); }) + .def(self_ns::str(self_ns::self)) ; } diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/TheNewCarlaServer.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/TheNewCarlaServer.cpp index 46a2b522d..29ea5652a 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/TheNewCarlaServer.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/TheNewCarlaServer.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -123,11 +124,11 @@ private: { if (!Child.IsValid()) { - RespondError("unable to attach actor: child actor not found"); + RespondErrorStr("unable to attach actor: child actor not found"); } if (!Parent.IsValid()) { - RespondError("unable to attach actor: parent actor not found"); + RespondErrorStr("unable to attach actor: parent actor not found"); } ::AttachActors(Child.GetActor(), Parent.GetActor()); } @@ -189,6 +190,19 @@ void FTheNewCarlaServer::FPimpl::BindActions() auto &Registry = Episode->GetActorRegistry(); AttachActors(Registry.Find(Child.id), Registry.Find(Parent.id)); }); + + Server.BindSync("apply_control_to_actor", [this](cr::Actor Actor, cr::VehicleControl Control) { + RequireEpisode(); + auto ActorView = Episode->GetActorRegistry().Find(Actor.id); + if (!ActorView.IsValid()) { + RespondErrorStr("unable to apply control: actor not found"); + } + auto Vehicle = Cast(ActorView.GetActor()); + if (Vehicle == nullptr) { + RespondErrorStr("unable to apply control: actor is not a vehicle"); + } + Vehicle->ApplyVehicleControl(Control); + }); } // =============================================================================