Prettyfying the files in response to review

This commit is contained in:
Aidan Clear 2019-01-30 11:20:23 +01:00
parent f033823752
commit faac288002
15 changed files with 252 additions and 201 deletions

View File

@ -55,6 +55,5 @@ namespace client {
return GetEpisode().Lock()->GetActorDynamicState(*this).state.traffic_light_data.time_is_frozen;
}
} // namespace client
} // namespace carla
} // namespace carla

View File

@ -22,17 +22,28 @@ namespace client {
/// @note These functions do not call the simulator, they return the
/// data received in the last tick.
void SetState(rpc::TrafficLightState state);
rpc::TrafficLightState GetState() const;
void SetGreenTime(float green_time);
float GetGreenTime() const;
void SetYellowTime(float yellow_time);
float GetYellowTime() const;
void SetRedTime(float red_time);
float GetRedTime() const;
float GetElapsedTime() const;
void Freeze(bool freeze);
bool IsFrozen() const;
};
} // namespace client
} // namespace carla
} // namespace carla

View File

@ -29,27 +29,23 @@ namespace client {
return GetEpisode().Lock()->GetActorDynamicState(*this).state.vehicle_data.control;
}
float Vehicle::GetSpeedLimit() const
{
float Vehicle::GetSpeedLimit() const {
return GetEpisode().Lock()->GetActorDynamicState(*this).state.vehicle_data.speed_limit;
}
rpc::TrafficLightState Vehicle::GetTrafficLightState() const
{
rpc::TrafficLightState Vehicle::GetTrafficLightState() const {
return GetEpisode().Lock()->GetActorDynamicState(*this).state.vehicle_data.traffic_light_state;
}
bool Vehicle::IsAtTrafficLight()
{
bool Vehicle::IsAtTrafficLight() {
return GetEpisode().Lock()->GetActorDynamicState(*this).state.vehicle_data.has_traffic_light;
}
SharedPtr<TrafficLight> Vehicle::GetTrafficLight() const
{
SharedPtr<TrafficLight> Vehicle::GetTrafficLight() const {
auto id = GetEpisode().Lock()->GetActorDynamicState(*this).state.vehicle_data.traffic_light_id;
SharedPtr<Actor> actor = GetWorld().GetActors()->Find(id);
return boost::static_pointer_cast<TrafficLight>(actor);
}
} // namespace client
} // namespace carla
} // namespace carla

View File

@ -29,7 +29,8 @@ namespace client {
/// Return the control last applied to this vehicle.
///
/// @note The following functions do not call the simulator, they return the data
/// @note The following functions do not call the simulator, they return the
/// data
/// received in the last tick.
//////////////////////////////////////////////////////////////////////////////////
Control GetControl() const;
@ -43,8 +44,9 @@ namespace client {
SharedPtr<TrafficLight> GetTrafficLight() const;
private:
Control _control;
};
} // namespace client
} // namespace carla
} // namespace carla

View File

@ -15,7 +15,6 @@
#include "carla/rpc/WalkerControl.h"
#include "carla/streaming/Client.h"
#include <thread>
namespace carla {
@ -45,9 +44,9 @@ namespace detail {
worker_threads > 0u ? worker_threads : std::thread::hardware_concurrency());
}
template <typename T, typename... Args>
auto CallAndWait(const std::string &function, Args &&... args) {
auto object = rpc_client.call(function, std::forward<Args>(args)...);
template <typename T, typename ... Args>
auto CallAndWait(const std::string &function, Args && ... args) {
auto object = rpc_client.call(function, std::forward<Args>(args) ...);
using R = typename carla::rpc::Response<T>;
auto response = object.template as<R>();
if (response.HasError()) {
@ -56,10 +55,10 @@ namespace detail {
return Get(response);
}
template <typename... Args>
void AsyncCall(const std::string &function, Args &&... args) {
template <typename ... Args>
void AsyncCall(const std::string &function, Args && ... args) {
// Discard returned future.
rpc_client.async_call(function, std::forward<Args>(args)...);
rpc_client.async_call(function, std::forward<Args>(args) ...);
}
rpc::Client rpc_client;
@ -171,7 +170,9 @@ namespace detail {
_pimpl->AsyncCall("apply_control_to_walker", walker, control);
}
void Client::SetTrafficLightState(const rpc::Actor &trafficLight, const rpc::TrafficLightState trafficLightState) {
void Client::SetTrafficLightState(
const rpc::Actor &trafficLight,
const rpc::TrafficLightState trafficLightState) {
_pimpl->AsyncCall("set_traffic_light_state", trafficLight, trafficLightState);
}
@ -219,4 +220,4 @@ namespace detail {
} // namespace detail
} // namespace client
} // namespace carla
} // namespace carla

View File

@ -17,7 +17,6 @@
#include "carla/rpc/WeatherParameters.h"
#include "carla/rpc/TrafficLightState.h"
#include <functional>
#include <memory>
#include <string>
@ -32,8 +31,10 @@ namespace rpc {
class VehicleControl;
class WalkerControl;
}
namespace sensor { class SensorData; }
namespace streaming { class Token; }
namespace sensor { class SensorData;
}
namespace streaming { class Token;
}
}
namespace carla {
@ -157,4 +158,4 @@ namespace detail {
} // namespace detail
} // namespace client
} // namespace carla
} // namespace carla

View File

@ -285,4 +285,4 @@ namespace detail {
} // namespace detail
} // namespace client
} // namespace carla
} // namespace carla

View File

@ -25,4 +25,4 @@ namespace rpc {
} // namespace rpc
} // namespace carla
MSGPACK_ADD_ENUM(carla::rpc::TrafficLightState);
MSGPACK_ADD_ENUM(carla::rpc::TrafficLightState);

View File

@ -50,6 +50,7 @@ namespace detail {
bool manual_gear_shift;
int32_t gear;
};
#pragma pack(pop)
#pragma pack(push, 1)
@ -85,6 +86,7 @@ namespace detail {
float speed;
bool jump;
};
#pragma pack(pop)
#pragma pack(push, 1)
@ -129,4 +131,4 @@ namespace detail {
} // namespace data
} // namespace sensor
} // namespace carla
} // namespace carla

View File

@ -37,80 +37,83 @@ void export_actor() {
namespace cr = carla::rpc;
class_<std::vector<int>>("vector_of_ints")
.def(vector_indexing_suite<std::vector<int>>())
.def(self_ns::str(self_ns::self))
.def(vector_indexing_suite<std::vector<int>>())
.def(self_ns::str(self_ns::self))
;
class_<cc::Actor, boost::noncopyable, boost::shared_ptr<cc::Actor>>("Actor", no_init)
// work-around, force return copy to resolve Actor instead of ActorState.
.add_property("id", CALL_RETURNING_COPY(cc::Actor, GetId))
.add_property("type_id", CALL_RETURNING_COPY(cc::Actor, GetTypeId))
.add_property("parent", CALL_RETURNING_COPY(cc::Actor, GetParent))
.add_property("semantic_tags", &GetSemanticTags)
.add_property("is_alive", CALL_RETURNING_COPY(cc::Actor, IsAlive))
.add_property("attributes", +[](const cc::Actor &self) {
boost::python::dict atttribute_dict;
for (auto &&attribute_value : self.GetAttributes()) {
atttribute_dict[attribute_value.GetId()] = attribute_value.GetValue();
}
return atttribute_dict;
})
.def("get_world", CALL_RETURNING_COPY(cc::Actor, GetWorld))
.def("get_location", &cc::Actor::GetLocation)
.def("get_transform", &cc::Actor::GetTransform)
.def("get_velocity", &cc::Actor::GetVelocity)
.def("get_angular_velocity", &cc::Actor::GetAngularVelocity)
.def("get_acceleration", &cc::Actor::GetAcceleration)
.def("set_location", &cc::Actor::SetLocation, (arg("location")))
.def("set_transform", &cc::Actor::SetTransform, (arg("transform")))
.def("set_velocity", &cc::Actor::SetVelocity, (arg("vector")))
.def("set_angular_velocity", &cc::Actor::SetAngularVelocity, (arg("vector")))
.def("add_impulse", &cc::Actor::AddImpulse, (arg("vector")))
.def("set_simulate_physics", &cc::Actor::SetSimulatePhysics, (arg("enabled")=true))
.def("destroy", CALL_WITHOUT_GIL(cc::Actor, Destroy))
.def(self_ns::str(self_ns::self))
// work-around, force return copy to resolve Actor instead of ActorState.
.add_property("id", CALL_RETURNING_COPY(cc::Actor, GetId))
.add_property("type_id", CALL_RETURNING_COPY(cc::Actor, GetTypeId))
.add_property("parent", CALL_RETURNING_COPY(cc::Actor, GetParent))
.add_property("semantic_tags", &GetSemanticTags)
.add_property("is_alive", CALL_RETURNING_COPY(cc::Actor, IsAlive))
.add_property("attributes", +[] (const cc::Actor &self) {
boost::python::dict atttribute_dict;
for (auto &&attribute_value : self.GetAttributes()) {
atttribute_dict[attribute_value.GetId()] = attribute_value.GetValue();
}
return atttribute_dict;
})
.def("get_world", CALL_RETURNING_COPY(cc::Actor, GetWorld))
.def("get_location", &cc::Actor::GetLocation)
.def("get_transform", &cc::Actor::GetTransform)
.def("get_velocity", &cc::Actor::GetVelocity)
.def("get_angular_velocity", &cc::Actor::GetAngularVelocity)
.def("get_acceleration", &cc::Actor::GetAcceleration)
.def("set_location", &cc::Actor::SetLocation, (arg("location")))
.def("set_transform", &cc::Actor::SetTransform, (arg("transform")))
.def("set_velocity", &cc::Actor::SetVelocity, (arg("vector")))
.def("set_angular_velocity", &cc::Actor::SetAngularVelocity, (arg("vector")))
.def("add_impulse", &cc::Actor::AddImpulse, (arg("vector")))
.def("set_simulate_physics", &cc::Actor::SetSimulatePhysics, (arg("enabled") = true))
.def("destroy", CALL_WITHOUT_GIL(cc::Actor, Destroy))
.def(self_ns::str(self_ns::self))
;
class_<cc::Vehicle, bases<cc::Actor>, boost::noncopyable, boost::shared_ptr<cc::Vehicle>>("Vehicle", no_init)
.add_property("bounding_box", CALL_RETURNING_COPY(cc::Vehicle, GetBoundingBox))
.def("apply_control", &cc::Vehicle::ApplyControl, (arg("control")))
.def("get_control", &cc::Vehicle::GetControl)
.def("set_autopilot", &cc::Vehicle::SetAutopilot, (arg("enabled")=true))
.def("get_speed_limit", &cc::Vehicle::GetSpeedLimit)
.def("get_traffic_light_state", &cc::Vehicle::GetTrafficLightState)
.def("is_at_traffic_light", &cc::Vehicle::IsAtTrafficLight)
.def("get_traffic_light", &cc::Vehicle::GetTrafficLight)
.def(self_ns::str(self_ns::self))
class_<cc::Vehicle, bases<cc::Actor>, boost::noncopyable, boost::shared_ptr<cc::Vehicle>>("Vehicle",
no_init)
.add_property("bounding_box", CALL_RETURNING_COPY(cc::Vehicle, GetBoundingBox))
.def("apply_control", &cc::Vehicle::ApplyControl, (arg("control")))
.def("get_control", &cc::Vehicle::GetControl)
.def("set_autopilot", &cc::Vehicle::SetAutopilot, (arg("enabled") = true))
.def("get_speed_limit", &cc::Vehicle::GetSpeedLimit)
.def("get_traffic_light_state", &cc::Vehicle::GetTrafficLightState)
.def("is_at_traffic_light", &cc::Vehicle::IsAtTrafficLight)
.def("get_traffic_light", &cc::Vehicle::GetTrafficLight)
.def(self_ns::str(self_ns::self))
;
class_<cc::Walker, bases<cc::Actor>, boost::noncopyable, boost::shared_ptr<cc::Walker>>("Walker", no_init)
.add_property("bounding_box", CALL_RETURNING_COPY(cc::Walker, GetBoundingBox))
.def("apply_control", &cc::Walker::ApplyControl, (arg("control")))
.def("get_control", &cc::Walker::GetWalkerControl)
.def(self_ns::str(self_ns::self))
.add_property("bounding_box", CALL_RETURNING_COPY(cc::Walker, GetBoundingBox))
.def("apply_control", &cc::Walker::ApplyControl, (arg("control")))
.def("get_control", &cc::Walker::GetWalkerControl)
.def(self_ns::str(self_ns::self))
;
enum_<cr::TrafficLightState>("TrafficLightState")
.value("Red", cr::TrafficLightState::Red)
.value("Yellow", cr::TrafficLightState::Yellow)
.value("Green", cr::TrafficLightState::Green)
.value("Off", cr::TrafficLightState::Off)
.value("Unknown", cr::TrafficLightState::Unknown)
.value("Red", cr::TrafficLightState::Red)
.value("Yellow", cr::TrafficLightState::Yellow)
.value("Green", cr::TrafficLightState::Green)
.value("Off", cr::TrafficLightState::Off)
.value("Unknown", cr::TrafficLightState::Unknown)
;
class_<cc::TrafficLight, bases<cc::Actor>, boost::noncopyable, boost::shared_ptr<cc::TrafficLight>>("TrafficLight", no_init)
.add_property("state", &cc::TrafficLight::GetState)
.def("set_state", &cc::TrafficLight::SetState, (arg("state")))
.def("get_state", &cc::TrafficLight::GetState)
.def("set_green_time", &cc::TrafficLight::SetGreenTime, (arg("green_time")))
.def("get_green_time", &cc::TrafficLight::GetGreenTime)
.def("set_yellow_time", &cc::TrafficLight::SetYellowTime, (arg("yellow_time")))
.def("get_yellow_time", &cc::TrafficLight::GetYellowTime)
.def("set_red_time", &cc::TrafficLight::SetRedTime, (arg("red_time")))
.def("get_red_time", &cc::TrafficLight::GetRedTime)
.def("get_elapsed_time", &cc::TrafficLight::GetElapsedTime)
.def("freeze", &cc::TrafficLight::Freeze, (arg("freeze")))
.def("is_frozen", &cc::TrafficLight::IsFrozen)
.def(self_ns::str(self_ns::self))
class_<cc::TrafficLight, bases<cc::Actor>, boost::noncopyable, boost::shared_ptr<cc::TrafficLight>>(
"TrafficLight",
no_init)
.add_property("state", &cc::TrafficLight::GetState)
.def("set_state", &cc::TrafficLight::SetState, (arg("state")))
.def("get_state", &cc::TrafficLight::GetState)
.def("set_green_time", &cc::TrafficLight::SetGreenTime, (arg("green_time")))
.def("get_green_time", &cc::TrafficLight::GetGreenTime)
.def("set_yellow_time", &cc::TrafficLight::SetYellowTime, (arg("yellow_time")))
.def("get_yellow_time", &cc::TrafficLight::GetYellowTime)
.def("set_red_time", &cc::TrafficLight::SetRedTime, (arg("red_time")))
.def("get_red_time", &cc::TrafficLight::GetRedTime)
.def("get_elapsed_time", &cc::TrafficLight::GetElapsedTime)
.def("freeze", &cc::TrafficLight::Freeze, (arg("freeze")))
.def("is_frozen", &cc::TrafficLight::IsFrozen)
.def(self_ns::str(self_ns::self))
;
}
}

View File

@ -99,7 +99,8 @@ static carla::Buffer AWorldObserver_Serialize(
write_data(header);
// Write every actor.
for (auto &&View : Registry) {
for (auto &&View : Registry)
{
check(View.IsValid());
constexpr float TO_METERS = 1e-2;
const auto velocity = TO_METERS * View.GetActor()->GetVelocity();
@ -107,7 +108,9 @@ static carla::Buffer AWorldObserver_Serialize(
const auto RootComponent = Cast<UPrimitiveComponent>(View.GetActor()->GetRootComponent());
FVector angularVelocity { 0.0f, 0.0f, 0.0f };
if (RootComponent != nullptr)
angularVelocity = RootComponent->GetPhysicsAngularVelocityInDegrees();
{
angularVelocity = RootComponent->GetPhysicsAngularVelocityInDegrees();
}
ActorDynamicState info = {
View.GetActorId(),
View.GetActor()->GetActorTransform(),
@ -122,7 +125,7 @@ static carla::Buffer AWorldObserver_Serialize(
return buffer;
}
AWorldObserver::AWorldObserver(const FObjectInitializer& ObjectInitializer)
AWorldObserver::AWorldObserver(const FObjectInitializer &ObjectInitializer)
: Super(ObjectInitializer)
{
PrimaryActorTick.bCanEverTick = true;

View File

@ -55,7 +55,8 @@ public:
FPimpl(uint16_t port)
: Server(port),
StreamingServer(port + 1u) {
StreamingServer(port + 1u)
{
BindActions();
}
@ -68,6 +69,7 @@ public:
private:
void BindActions();
};
// =============================================================================
@ -80,17 +82,17 @@ private:
# define CARLA_ENSURE_GAME_THREAD()
#endif // WITH_EDITOR
#define RESPOND_ERROR(str) { \
#define RESPOND_ERROR(str) { \
UE_LOG(LogCarlaServer, Log, TEXT("Responding error: %s"), TEXT(str)); \
return carla::rpc::ResponseError(str); }
#define RESPOND_ERROR_FSTRING(fstr) { \
#define RESPOND_ERROR_FSTRING(fstr) { \
UE_LOG(LogCarlaServer, Log, TEXT("Responding error: %s"), *fstr); \
return carla::rpc::ResponseError(carla::rpc::FromFString(fstr)); }
#define REQUIRE_CARLA_EPISODE() \
CARLA_ENSURE_GAME_THREAD(); \
if (Episode == nullptr) { RESPOND_ERROR("episode not ready"); }
CARLA_ENSURE_GAME_THREAD(); \
if (Episode == nullptr) { RESPOND_ERROR("episode not ready"); }
// =============================================================================
// -- Bind Actions -------------------------------------------------------------
@ -110,13 +112,14 @@ void FTheNewCarlaServer::FPimpl::BindActions()
{
REQUIRE_CARLA_EPISODE();
auto WorldObserver = Episode->GetWorldObserver();
if (WorldObserver == nullptr) {
if (WorldObserver == nullptr)
{
RESPOND_ERROR("internal error: missing world observer");
}
return cr::EpisodeInfo{
Episode->GetId(),
cr::FromFString(Episode->GetMapName()),
WorldObserver->GetToken()};
Episode->GetId(),
cr::FromFString(Episode->GetMapName()),
WorldObserver->GetToken()};
});
Server.BindSync("get_map_info", [this]() -> R<cr::MapInfo>
@ -125,9 +128,9 @@ void FTheNewCarlaServer::FPimpl::BindActions()
auto FileContents = FOpenDrive::Load(Episode->GetMapName());
const auto &SpawnPoints = Episode->GetRecommendedSpawnPoints();
return cr::MapInfo{
cr::FromFString(Episode->GetMapName()),
cr::FromFString(FileContents),
MakeVectorFromTArray<cg::Transform>(SpawnPoints)};
cr::FromFString(Episode->GetMapName()),
cr::FromFString(FileContents),
MakeVectorFromTArray<cg::Transform>(SpawnPoints)};
});
Server.BindSync("get_actor_definitions", [this]() -> R<std::vector<cr::ActorDefinition>>
@ -158,7 +161,7 @@ void FTheNewCarlaServer::FPimpl::BindActions()
});
Server.BindSync("set_weather_parameters", [this](
const cr::WeatherParameters &weather) -> R<void>
const cr::WeatherParameters &weather) -> R<void>
{
REQUIRE_CARLA_EPISODE();
auto *Weather = Episode->GetWeather();
@ -171,7 +174,7 @@ void FTheNewCarlaServer::FPimpl::BindActions()
});
Server.BindSync("get_actors_by_id", [this](
const std::vector<FActorView::IdType> &ids) -> R<std::vector<cr::Actor>>
const std::vector<FActorView::IdType> &ids) -> R<std::vector<cr::Actor>>
{
REQUIRE_CARLA_EPISODE();
std::vector<cr::Actor> Result;
@ -188,8 +191,8 @@ void FTheNewCarlaServer::FPimpl::BindActions()
});
Server.BindSync("spawn_actor", [this](
cr::ActorDescription Description,
const cr::Transform &Transform) -> R<cr::Actor>
cr::ActorDescription Description,
const cr::Transform &Transform) -> R<cr::Actor>
{
REQUIRE_CARLA_EPISODE();
auto Result = Episode->SpawnActorWithInfo(Transform, std::move(Description));
@ -205,9 +208,9 @@ void FTheNewCarlaServer::FPimpl::BindActions()
});
Server.BindSync("spawn_actor_with_parent", [this](
cr::ActorDescription Description,
const cr::Transform &Transform,
cr::Actor Parent) -> R<cr::Actor>
cr::ActorDescription Description,
const cr::Transform &Transform,
cr::Actor Parent) -> R<cr::Actor>
{
REQUIRE_CARLA_EPISODE();
auto Result = Episode->SpawnActorWithInfo(Transform, std::move(Description));
@ -244,8 +247,8 @@ void FTheNewCarlaServer::FPimpl::BindActions()
});
Server.BindSync("attach_actors", [this](
cr::Actor Child,
cr::Actor Parent) -> R<void>
cr::Actor Child,
cr::Actor Parent) -> R<void>
{
REQUIRE_CARLA_EPISODE();
auto ChildView = Episode->FindActor(Child.id);
@ -263,8 +266,8 @@ void FTheNewCarlaServer::FPimpl::BindActions()
});
Server.BindSync("set_actor_location", [this](
cr::Actor Actor,
cr::Location Location) -> R<void>
cr::Actor Actor,
cr::Location Location) -> R<void>
{
REQUIRE_CARLA_EPISODE();
auto ActorView = Episode->FindActor(Actor.id);
@ -273,16 +276,16 @@ void FTheNewCarlaServer::FPimpl::BindActions()
RESPOND_ERROR("unable to set actor location: actor not found");
}
ActorView.GetActor()->SetActorRelativeLocation(
Location,
false,
nullptr,
ETeleportType::TeleportPhysics);
Location,
false,
nullptr,
ETeleportType::TeleportPhysics);
return R<void>::Success();
});
Server.BindSync("set_actor_transform", [this](
cr::Actor Actor,
cr::Transform Transform) -> R<void>
cr::Actor Actor,
cr::Transform Transform) -> R<void>
{
REQUIRE_CARLA_EPISODE();
auto ActorView = Episode->FindActor(Actor.id);
@ -291,16 +294,16 @@ void FTheNewCarlaServer::FPimpl::BindActions()
RESPOND_ERROR("unable to set actor transform: actor not found");
}
ActorView.GetActor()->SetActorRelativeTransform(
Transform,
false,
nullptr,
ETeleportType::TeleportPhysics);
Transform,
false,
nullptr,
ETeleportType::TeleportPhysics);
return R<void>::Success();
});
Server.BindSync("set_actor_velocity", [this](
cr::Actor Actor,
cr::Vector3D vector) -> R<void>
cr::Actor Actor,
cr::Vector3D vector) -> R<void>
{
REQUIRE_CARLA_EPISODE();
auto ActorView = Episode->FindActor(Actor.id);
@ -314,15 +317,15 @@ void FTheNewCarlaServer::FPimpl::BindActions()
RESPOND_ERROR("unable to set actor velocity: not supported by actor");
}
RootComponent->SetPhysicsLinearVelocity(
vector.ToCentimeters(),
false,
"None");
vector.ToCentimeters(),
false,
"None");
return R<void>::Success();
});
Server.BindSync("set_actor_angular_velocity", [this](
cr::Actor Actor,
cr::Vector3D vector) -> R<void>
cr::Actor Actor,
cr::Vector3D vector) -> R<void>
{
REQUIRE_CARLA_EPISODE();
auto ActorView = Episode->FindActor(Actor.id);
@ -336,15 +339,15 @@ void FTheNewCarlaServer::FPimpl::BindActions()
RESPOND_ERROR("unable to set actor angular velocity: not supported by actor");
}
RootComponent->SetPhysicsAngularVelocityInDegrees(
vector,
false,
"None");
vector,
false,
"None");
return R<void>::Success();
});
Server.BindSync("add_actor_impulse", [this](
cr::Actor Actor,
cr::Vector3D vector) -> R<void>
cr::Actor Actor,
cr::Vector3D vector) -> R<void>
{
REQUIRE_CARLA_EPISODE();
auto ActorView = Episode->FindActor(Actor.id);
@ -358,15 +361,15 @@ void FTheNewCarlaServer::FPimpl::BindActions()
RESPOND_ERROR("unable to add actor impulse: not supported by actor");
}
RootComponent->AddImpulse(
vector.ToCentimeters(),
"None",
false);
vector.ToCentimeters(),
"None",
false);
return R<void>::Success();
});
Server.BindSync("set_actor_simulate_physics", [this](
cr::Actor Actor,
bool bEnabled) -> R<void>
cr::Actor Actor,
bool bEnabled) -> R<void>
{
REQUIRE_CARLA_EPISODE();
auto ActorView = Episode->FindActor(Actor.id);
@ -384,8 +387,8 @@ void FTheNewCarlaServer::FPimpl::BindActions()
});
Server.BindSync("apply_control_to_vehicle", [this](
cr::Actor Actor,
cr::VehicleControl Control) -> R<void>
cr::Actor Actor,
cr::VehicleControl Control) -> R<void>
{
REQUIRE_CARLA_EPISODE();
auto ActorView = Episode->FindActor(Actor.id);
@ -403,8 +406,8 @@ void FTheNewCarlaServer::FPimpl::BindActions()
});
Server.BindSync("apply_control_to_walker", [this](
cr::Actor Actor,
cr::WalkerControl Control) -> R<void>
cr::Actor Actor,
cr::WalkerControl Control) -> R<void>
{
REQUIRE_CARLA_EPISODE();
auto ActorView = Episode->FindActor(Actor.id);
@ -427,8 +430,8 @@ void FTheNewCarlaServer::FPimpl::BindActions()
});
Server.BindSync("set_actor_autopilot", [this](
cr::Actor Actor,
bool bEnabled) -> R<void>
cr::Actor Actor,
bool bEnabled) -> R<void>
{
REQUIRE_CARLA_EPISODE();
auto ActorView = Episode->FindActor(Actor.id);
@ -451,16 +454,18 @@ void FTheNewCarlaServer::FPimpl::BindActions()
});
Server.BindSync("set_traffic_light_state", [this](
cr::Actor Actor,
cr::TrafficLightState trafficLightState) -> R<void>
cr::Actor Actor,
cr::TrafficLightState trafficLightState) -> R<void>
{
REQUIRE_CARLA_EPISODE();
auto ActorView = Episode->GetActorRegistry().Find(Actor.id);
if (!ActorView.IsValid() || ActorView.GetActor()->IsPendingKill()) {
if (!ActorView.IsValid() || ActorView.GetActor()->IsPendingKill())
{
RESPOND_ERROR("unable to set state: actor not found");
}
auto TrafficLight = Cast<ATrafficLightBase>(ActorView.GetActor());
if (TrafficLight == nullptr) {
if (TrafficLight == nullptr)
{
RESPOND_ERROR("unable to set state: actor is not a traffic light");
}
TrafficLight->SetTrafficLightState(static_cast<ETrafficLightState>(trafficLightState));
@ -468,16 +473,18 @@ void FTheNewCarlaServer::FPimpl::BindActions()
});
Server.BindSync("set_traffic_light_green_time", [this](
cr::Actor Actor,
float GreenTime) -> R<void>
cr::Actor Actor,
float GreenTime) -> R<void>
{
REQUIRE_CARLA_EPISODE();
auto ActorView = Episode->GetActorRegistry().Find(Actor.id);
if (!ActorView.IsValid() || ActorView.GetActor()->IsPendingKill()) {
if (!ActorView.IsValid() || ActorView.GetActor()->IsPendingKill())
{
RESPOND_ERROR("unable to set green time: actor not found");
}
auto TrafficLight = Cast<ATrafficLightBase>(ActorView.GetActor());
if (TrafficLight == nullptr) {
if (TrafficLight == nullptr)
{
RESPOND_ERROR("unable to set green time: actor is not a traffic light");
}
TrafficLight->SetGreenTime(GreenTime);
@ -485,16 +492,18 @@ void FTheNewCarlaServer::FPimpl::BindActions()
});
Server.BindSync("set_traffic_light_yellow_time", [this](
cr::Actor Actor,
float YellowTime) -> R<void>
cr::Actor Actor,
float YellowTime) -> R<void>
{
REQUIRE_CARLA_EPISODE();
auto ActorView = Episode->GetActorRegistry().Find(Actor.id);
if (!ActorView.IsValid() || ActorView.GetActor()->IsPendingKill()) {
if (!ActorView.IsValid() || ActorView.GetActor()->IsPendingKill())
{
RESPOND_ERROR("unable to set yellow time: actor not found");
}
auto TrafficLight = Cast<ATrafficLightBase>(ActorView.GetActor());
if (TrafficLight == nullptr) {
if (TrafficLight == nullptr)
{
RESPOND_ERROR("unable to set yellow time: actor is not a traffic light");
}
TrafficLight->SetYellowTime(YellowTime);
@ -502,16 +511,18 @@ void FTheNewCarlaServer::FPimpl::BindActions()
});
Server.BindSync("set_traffic_light_red_time", [this](
cr::Actor Actor,
float RedTime) -> R<void>
cr::Actor Actor,
float RedTime) -> R<void>
{
REQUIRE_CARLA_EPISODE();
auto ActorView = Episode->GetActorRegistry().Find(Actor.id);
if (!ActorView.IsValid() || ActorView.GetActor()->IsPendingKill()) {
if (!ActorView.IsValid() || ActorView.GetActor()->IsPendingKill())
{
RESPOND_ERROR("unable to set red time: actor not found");
}
auto TrafficLight = Cast<ATrafficLightBase>(ActorView.GetActor());
if (TrafficLight == nullptr) {
if (TrafficLight == nullptr)
{
RESPOND_ERROR("unable to set red time: actor is not a traffic light");
}
TrafficLight->SetRedTime(RedTime);
@ -519,16 +530,18 @@ void FTheNewCarlaServer::FPimpl::BindActions()
});
Server.BindSync("freeze_traffic_light", [this](
cr::Actor Actor,
bool Freeze) -> R<void>
cr::Actor Actor,
bool Freeze) -> R<void>
{
REQUIRE_CARLA_EPISODE();
auto ActorView = Episode->GetActorRegistry().Find(Actor.id);
if (!ActorView.IsValid() || ActorView.GetActor()->IsPendingKill()) {
if (!ActorView.IsValid() || ActorView.GetActor()->IsPendingKill())
{
RESPOND_ERROR("unable to alter frozen state: actor not found");
}
auto TrafficLight = Cast<ATrafficLightBase>(ActorView.GetActor());
if (TrafficLight == nullptr) {
if (TrafficLight == nullptr)
{
RESPOND_ERROR("unable to alter frozen state: actor is not a traffic light");
}
TrafficLight->SetTimeIsFrozen(Freeze);

View File

@ -19,8 +19,10 @@ static bool IsValid(const ACarlaWheeledVehicle *Vehicle)
return ((Vehicle != nullptr) && !Vehicle->IsPendingKill());
}
static ETrafficSignState ToTrafficSignState(ETrafficLightState State) {
switch (State) {
static ETrafficSignState ToTrafficSignState(ETrafficLightState State)
{
switch (State)
{
case ETrafficLightState::Green:
return ETrafficSignState::TrafficLightGreen;
case ETrafficLightState::Yellow:
@ -38,7 +40,7 @@ static ETrafficSignState ToTrafficSignState(ETrafficLightState State) {
ATrafficLightBase::ATrafficLightBase(const FObjectInitializer &ObjectInitializer)
: Super(ObjectInitializer)
{
PrimaryActorTick.bCanEverTick = true;
PrimaryActorTick.bCanEverTick = true;
}
void ATrafficLightBase::OnConstruction(const FTransform &Transform)
@ -58,7 +60,8 @@ void ATrafficLightBase::Tick(float DeltaSeconds)
float ChangeTime;
switch (State) {
switch (State)
{
case ETrafficLightState::Red:
ChangeTime = RedTime;
break;
@ -66,7 +69,7 @@ void ATrafficLightBase::Tick(float DeltaSeconds)
ChangeTime = YellowTime;
break;
case ETrafficLightState::Green:
ChangeTime = GreenTime;
ChangeTime = GreenTime;
break;
default:
UE_LOG(LogCarla, Error, TEXT("Invalid traffic light state!"));
@ -74,7 +77,8 @@ void ATrafficLightBase::Tick(float DeltaSeconds)
return;
}
if (ElapsedTime > ChangeTime) {
if (ElapsedTime > ChangeTime)
{
SwitchTrafficLightState();
}
}
@ -85,7 +89,8 @@ void ATrafficLightBase::PostEditChangeProperty(FPropertyChangedEvent &Event)
Super::PostEditChangeProperty(Event);
const FName PropertyName = (Event.Property != nullptr ? Event.Property->GetFName() : NAME_None);
if (PropertyName == GET_MEMBER_NAME_CHECKED(ATrafficLightBase, State)) {
if (PropertyName == GET_MEMBER_NAME_CHECKED(ATrafficLightBase, State))
{
SetTrafficLightState(State);
}
}
@ -97,15 +102,19 @@ void ATrafficLightBase::SetTrafficLightState(const ETrafficLightState InState)
ElapsedTime = 0.0f;
State = InState;
SetTrafficSignState(ToTrafficSignState(State));
for (auto Controller : Vehicles) {
if (Controller != nullptr) {
for (auto Controller : Vehicles)
{
if (Controller != nullptr)
{
Controller->SetTrafficLightState(State);
if (State == ETrafficLightState::Green) {
if (State == ETrafficLightState::Green)
{
Controller->SetTrafficLight(nullptr);
}
}
}
if (State == ETrafficLightState::Green) {
if (State == ETrafficLightState::Green)
{
Vehicles.Empty();
}
OnTrafficLightStateChanged(State);
@ -113,7 +122,8 @@ void ATrafficLightBase::SetTrafficLightState(const ETrafficLightState InState)
void ATrafficLightBase::SwitchTrafficLightState()
{
switch (State) {
switch (State)
{
case ETrafficLightState::Red:
SetTrafficLightState(ETrafficLightState::Green);
break;
@ -132,11 +142,14 @@ void ATrafficLightBase::SwitchTrafficLightState()
void ATrafficLightBase::NotifyWheeledVehicle(ACarlaWheeledVehicle *Vehicle)
{
if (IsValid(Vehicle)) {
if (IsValid(Vehicle))
{
auto Controller = Cast<AWheeledVehicleAIController>(Vehicle->GetController());
if (Controller != nullptr) {
if (Controller != nullptr)
{
Controller->SetTrafficLightState(State);
if (State != ETrafficLightState::Green) {
if (State != ETrafficLightState::Green)
{
Vehicles.Add(Controller);
Controller->SetTrafficLight(this);
}
@ -197,4 +210,4 @@ bool ATrafficLightBase::GetTimeIsFrozen() const
int ATrafficLightBase::GetNumChanges() const
{
return NumChanges;
}
}

View File

@ -12,12 +12,12 @@
#include "TrafficLightBase.generated.h"
class ACarlaWheeledVehicle;
class AWheeledVehicleAIController;
UCLASS()
class CARLA_API ATrafficLightBase : public ATrafficSignBase {
class CARLA_API ATrafficLightBase : public ATrafficSignBase
{
GENERATED_BODY()
@ -31,10 +31,9 @@ protected:
virtual void OnConstruction(const FTransform &Transform) override;
#if WITH_EDITOR
virtual void PostEditChangeProperty(FPropertyChangedEvent &PropertyChangedEvent) override;
#endif // WITH_EDITOR
public:
@ -115,4 +114,4 @@ private:
UPROPERTY(Category = "Traffic Light", VisibleAnywhere)
int NumChanges = 0;
};
};

View File

@ -29,9 +29,10 @@ class CARLA_API AWheeledVehicleAIController : public APlayerController
/// @name Constructor and destructor
// ===========================================================================
/// @{
public:
AWheeledVehicleAIController(const FObjectInitializer& ObjectInitializer);
AWheeledVehicleAIController(const FObjectInitializer &ObjectInitializer);
~AWheeledVehicleAIController();
@ -40,6 +41,7 @@ public:
/// @name APlayerController overrides
// ===========================================================================
/// @{
public:
void Possess(APawn *aPawn) override;
@ -53,6 +55,7 @@ public:
/// @name Possessed vehicle
// ===========================================================================
/// @{
public:
UFUNCTION(Category = "Wheeled Vehicle Controller", BlueprintCallable)
@ -83,6 +86,7 @@ public:
/// @name Road map
// ===========================================================================
/// @{
public:
void SetRoadMap(URoadMap *InRoadMap)
@ -101,6 +105,7 @@ public:
/// @name Random engine
// ===========================================================================
/// @{
public:
UFUNCTION(Category = "Random Engine", BlueprintCallable)
@ -115,6 +120,7 @@ public:
/// @name Autopilot
// ===========================================================================
/// @{
public:
UFUNCTION(Category = "Wheeled Vehicle Controller", BlueprintCallable)
@ -126,7 +132,8 @@ public:
UFUNCTION(Category = "Wheeled Vehicle Controller", BlueprintCallable)
void SetAutopilot(bool Enable)
{
if (IsAutopilotEnabled() != Enable) {
if (IsAutopilotEnabled() != Enable)
{
ConfigureAutopilot(Enable);
}
}
@ -146,6 +153,7 @@ private:
/// @name Traffic
// ===========================================================================
/// @{
public:
/// Get current speed limit in km/h.
@ -178,21 +186,21 @@ public:
/// Get traffic light currently affecting this vehicle.
UFUNCTION(Category = "Wheeled Vehicle Controller", BlueprintCallable)
ATrafficLightBase* GetTrafficLight() const
ATrafficLightBase *GetTrafficLight() const
{
return TrafficLight;
}
/// Set traffic light currently affecting this vehicle.
UFUNCTION(Category = "Wheeled Vehicle Controller", BlueprintCallable)
void SetTrafficLight(ATrafficLightBase* InTrafficLight)
void SetTrafficLight(ATrafficLightBase *InTrafficLight)
{
TrafficLight = InTrafficLight;
}
/// Set a fixed route to follow if autopilot is enabled.
UFUNCTION(Category = "Wheeled Vehicle Controller", BlueprintCallable)
void SetFixedRoute(const TArray<FVector> &Locations, bool bOverwriteCurrent=true);
void SetFixedRoute(const TArray<FVector> &Locations, bool bOverwriteCurrent = true);
/// @}
// ===========================================================================
@ -206,7 +214,6 @@ public:
return AutopilotControl;
}
private:
void TickAutopilotController();
@ -227,6 +234,7 @@ private:
// ===========================================================================
// -- Member variables -------------------------------------------------------
// ===========================================================================
private:
UPROPERTY()
@ -251,9 +259,9 @@ private:
float MaximumSteerAngle = -1.0f;
UPROPERTY()
ATrafficLightBase* TrafficLight;
ATrafficLightBase *TrafficLight;
FVehicleControl AutopilotControl;
std::queue<FVector> TargetLocations;
};
};