Add function to apply control to vehicles

This commit is contained in:
nsubiron 2018-07-26 22:04:44 +02:00
parent f89f728871
commit d543c3812e
6 changed files with 83 additions and 9 deletions

View File

@ -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;
}

View File

@ -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<Actor>(new Sensor{actor, GetWorld()});
}
return SharedPtr<Actor>(new Actor{actor, GetWorld()});
return SharedPtr<Actor>(new Vehicle{actor, GetWorld()});
}
void Client::ApplyControlToActor(

View File

@ -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 <https://opensource.org/licenses/MIT>.
#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 <typename ... Args>
Vehicle(Args && ... args) : Actor(std::forward<Args>(args)...) {}
};
} // namespace client
} // namespace carla

View File

@ -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,

View File

@ -7,6 +7,7 @@
#include <carla/client/Actor.h>
#include <carla/client/Image.h>
#include <carla/client/Sensor.h>
#include <carla/client/Vehicle.h>
#include <boost/python.hpp>
@ -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_<cc::Image, boost::noncopyable, boost::shared_ptr<cc::Image>>("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_<cc::Vehicle, bases<cc::Actor>, boost::noncopyable, boost::shared_ptr<cc::Vehicle>>("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))
;
}

View File

@ -16,6 +16,7 @@
#include <carla/rpc/ActorDescription.h>
#include <carla/rpc/Server.h>
#include <carla/rpc/Transform.h>
#include <carla/rpc/VehicleControl.h>
#include <carla/streaming/Server.h>
#include <compiler/enable-ue4-macros.h>
@ -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<ACarlaWheeledVehicle>(ActorView.GetActor());
if (Vehicle == nullptr) {
RespondErrorStr("unable to apply control: actor is not a vehicle");
}
Vehicle->ApplyVehicleControl(Control);
});
}
// =============================================================================