Added batch command SetVehicleLightState

This commit is contained in:
Marc Garcia Puig 2020-05-06 18:16:21 +02:00 committed by Jacopo Bartiromo
parent 1e98335808
commit 923960751f
6 changed files with 46 additions and 8 deletions

View File

@ -219,7 +219,7 @@ namespace detail {
void Client::SetLightStateToVehicle( void Client::SetLightStateToVehicle(
rpc::ActorId vehicle, rpc::ActorId vehicle,
const rpc::VehicleLightState &light_state) { const rpc::VehicleLightState &light_state) {
return _pimpl->AsyncCall("apply_vehicle_light_state", vehicle, light_state); return _pimpl->AsyncCall("set_vehicle_light_state", vehicle, light_state);
} }
rpc::Actor Client::SpawnActor( rpc::Actor Client::SpawnActor(

View File

@ -12,6 +12,7 @@
#include "carla/rpc/ActorDescription.h" #include "carla/rpc/ActorDescription.h"
#include "carla/rpc/ActorId.h" #include "carla/rpc/ActorId.h"
#include "carla/rpc/VehicleControl.h" #include "carla/rpc/VehicleControl.h"
#include "carla/rpc/VehicleLightState.h"
#include "carla/rpc/WalkerControl.h" #include "carla/rpc/WalkerControl.h"
#include <boost/variant.hpp> #include <boost/variant.hpp>
@ -152,8 +153,6 @@ namespace rpc {
}; };
struct SetAutopilot : CommandBase<SetAutopilot> { struct SetAutopilot : CommandBase<SetAutopilot> {
using TM = traffic_manager::TrafficManager;
SetAutopilot() = default; SetAutopilot() = default;
SetAutopilot( SetAutopilot(
ActorId id, ActorId id,
@ -168,6 +167,18 @@ namespace rpc {
MSGPACK_DEFINE_ARRAY(actor, enabled); MSGPACK_DEFINE_ARRAY(actor, enabled);
}; };
struct SetVehicleLightState : CommandBase<SetVehicleLightState> {
SetVehicleLightState() = default;
SetVehicleLightState(
ActorId id,
VehicleLightState::flag_type value)
: actor(id),
light_state(value) {}
ActorId actor;
VehicleLightState::flag_type light_state;
MSGPACK_DEFINE_ARRAY(actor, light_state);
};
using CommandType = boost::variant< using CommandType = boost::variant<
SpawnActor, SpawnActor,
DestroyActor, DestroyActor,
@ -180,7 +191,8 @@ namespace rpc {
ApplyImpulse, ApplyImpulse,
ApplyAngularImpulse, ApplyAngularImpulse,
SetSimulatePhysics, SetSimulatePhysics,
SetAutopilot>; SetAutopilot,
SetVehicleLightState>;
CommandType command; CommandType command;

View File

@ -166,6 +166,13 @@ void export_commands() {
.def_readwrite("enabled", &cr::Command::SetAutopilot::enabled) .def_readwrite("enabled", &cr::Command::SetAutopilot::enabled)
; ;
class_<cr::Command::SetVehicleLightState>("SetVehicleLightState")
.def("__init__", &command_impl::CustomInit<ActorPtr, bool>, (arg("actor"), arg("light_state")))
.def(init<cr::ActorId, cr::VehicleLightState::flag_type>((arg("actor_id"), arg("light_state"))))
.def_readwrite("actor_id", &cr::Command::SetVehicleLightState::actor)
.def_readwrite("light_state", &cr::Command::SetVehicleLightState::light_state)
;
implicitly_convertible<cr::Command::SpawnActor, cr::Command>(); implicitly_convertible<cr::Command::SpawnActor, cr::Command>();
implicitly_convertible<cr::Command::DestroyActor, cr::Command>(); implicitly_convertible<cr::Command::DestroyActor, cr::Command>();
implicitly_convertible<cr::Command::ApplyVehicleControl, cr::Command>(); implicitly_convertible<cr::Command::ApplyVehicleControl, cr::Command>();
@ -178,4 +185,5 @@ void export_commands() {
implicitly_convertible<cr::Command::ApplyAngularImpulse, cr::Command>(); implicitly_convertible<cr::Command::ApplyAngularImpulse, cr::Command>();
implicitly_convertible<cr::Command::SetSimulatePhysics, cr::Command>(); implicitly_convertible<cr::Command::SetSimulatePhysics, cr::Command>();
implicitly_convertible<cr::Command::SetAutopilot, cr::Command>(); implicitly_convertible<cr::Command::SetAutopilot, cr::Command>();
implicitly_convertible<cr::Command::SetVehicleLightState, cr::Command>();
} }

View File

@ -420,11 +420,11 @@ class KeyboardControl(object):
if self._control.brake: if self._control.brake:
current_lights |= carla.VehicleLightState.Brake current_lights |= carla.VehicleLightState.Brake
else: # Remove the Brake flag else: # Remove the Brake flag
current_lights &= carla.VehicleLightState.All ^ carla.VehicleLightState.Brake current_lights &= ~carla.VehicleLightState.Brake
if self._control.reverse: if self._control.reverse:
current_lights |= carla.VehicleLightState.Reverse current_lights |= carla.VehicleLightState.Reverse
else: # Remove the Reverse flag else: # Remove the Reverse flag
current_lights &= carla.VehicleLightState.All ^ carla.VehicleLightState.Reverse current_lights &= ~carla.VehicleLightState.Reverse
if current_lights != self._lights: # Change the light state only if necessary if current_lights != self._lights: # Change the light state only if necessary
self._lights = current_lights self._lights = current_lights
world.player.set_light_state(carla.VehicleLightState(self._lights)) world.player.set_light_state(carla.VehicleLightState(self._lights))

View File

@ -23,6 +23,8 @@ except IndexError:
import carla import carla
from carla import VehicleLightState as vls
import argparse import argparse
import logging import logging
import random import random
@ -82,6 +84,11 @@ def main():
'--hybrid', '--hybrid',
action='store_true', action='store_true',
help='Enanble') help='Enanble')
argparser.add_argument(
'--car-lights-on',
action='store_true',
default=False,
help='Enanble car lights')
args = argparser.parse_args() args = argparser.parse_args()
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO) logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
@ -135,6 +142,7 @@ def main():
# @todo cannot import these directly. # @todo cannot import these directly.
SpawnActor = carla.command.SpawnActor SpawnActor = carla.command.SpawnActor
SetAutopilot = carla.command.SetAutopilot SetAutopilot = carla.command.SetAutopilot
SetVehicleLightState = carla.command.SetVehicleLightState
FutureActor = carla.command.FutureActor FutureActor = carla.command.FutureActor
# -------------- # --------------
@ -152,7 +160,16 @@ def main():
driver_id = random.choice(blueprint.get_attribute('driver_id').recommended_values) driver_id = random.choice(blueprint.get_attribute('driver_id').recommended_values)
blueprint.set_attribute('driver_id', driver_id) blueprint.set_attribute('driver_id', driver_id)
blueprint.set_attribute('role_name', 'autopilot') blueprint.set_attribute('role_name', 'autopilot')
batch.append(SpawnActor(blueprint, transform).then(SetAutopilot(FutureActor, True, traffic_manager.get_port())))
# prepare the light state of the cars to spawn
light_state = vls.NONE
if args.car_lights_on:
light_state = vls.Position | vls.LowBeam | vls.LowBeam
# spawn the cars and set their autopilot and light state all together
batch.append(SpawnActor(blueprint, transform)
.then(SetAutopilot(FutureActor, True, traffic_manager.get_port()))
.then(SetVehicleLightState(FutureActor, light_state)))
for response in client.apply_batch_sync(batch, synchronous_master): for response in client.apply_batch_sync(batch, synchronous_master):
if response.error: if response.error:

View File

@ -665,7 +665,7 @@ void FCarlaServer::FPimpl::BindActions()
return R<void>::Success(); return R<void>::Success();
}; };
BIND_SYNC(apply_vehicle_light_state) << [this]( BIND_SYNC(set_vehicle_light_state) << [this](
cr::ActorId ActorId, cr::ActorId ActorId,
cr::VehicleLightState LightState) -> R<void> cr::VehicleLightState LightState) -> R<void>
{ {
@ -1057,6 +1057,7 @@ void FCarlaServer::FPimpl::BindActions()
[=](auto, const C::SetSimulatePhysics &c) { MAKE_RESULT(set_actor_simulate_physics(c.actor, c.enabled)); }, [=](auto, const C::SetSimulatePhysics &c) { MAKE_RESULT(set_actor_simulate_physics(c.actor, c.enabled)); },
// TODO: SetAutopilot should be removed. This is the old way to control the vehicles // TODO: SetAutopilot should be removed. This is the old way to control the vehicles
[=](auto, const C::SetAutopilot &c) { MAKE_RESULT(set_actor_autopilot(c.actor, c.enabled)); }, [=](auto, const C::SetAutopilot &c) { MAKE_RESULT(set_actor_autopilot(c.actor, c.enabled)); },
[=](auto, const C::SetVehicleLightState &c) { MAKE_RESULT(set_vehicle_light_state(c.actor, c.light_state)); },
[=](auto, const C::ApplyWalkerState &c) { MAKE_RESULT(set_walker_state(c.actor, c.transform, c.speed)); }); [=](auto, const C::ApplyWalkerState &c) { MAKE_RESULT(set_walker_state(c.actor, c.transform, c.speed)); });
#undef MAKE_RESULT #undef MAKE_RESULT