Added demo script for python wrapper

This commit is contained in:
Praveen Kumar 2019-10-14 10:19:15 +05:30 committed by bernat
parent eef5cd5276
commit 2798a2cdaf
3 changed files with 88 additions and 51 deletions

View File

@ -56,3 +56,8 @@ target_link_libraries(traffic_manager ${PYTHON_LIBRARIES}
# Setting target properties for python wrapper
set_target_properties(traffic_manager PROPERTIES SUFFIX .so)
set_target_properties(traffic_manager PROPERTIES PREFIX "")
file(COPY
${CARLA_LOCATION}/TrafficManager/source/pipeline/python/tm_demo.py
DESTINATION
${CARLA_LOCATION}/TrafficManager/build/)

View File

@ -1,65 +1,25 @@
#pragma once
#include <chrono>
#include <memory>
#include "carla/client/Client.h"
#include "boost/python.hpp"
#include "boost/python/suite/indexing/vector_indexing_suite.hpp"
#include "Pipeline.h"
namespace cc = carla::client;
using namespace boost::python;
namespace traffic_manager {
class TrafficManager {
private:
std::shared_ptr<Pipeline> pipeline;
cc::Client &client;
public:
TrafficManager (list &longitudinal_parameters,
list &longitudinal_highway_parameters,
list &lateral_parameters,
float urban_target_velocity,
float highway_target_velocity,
cc::Client &client):
client(client) {
std::vector<float> longitudinal_parameters_vector;
for (uint i = 0u; i < len(longitudinal_parameters); ++i) {
longitudinal_parameters_vector.push_back(
extract<double>(longitudinal_parameters[i]));
}
std::vector<float> longitudinal_highway_parameters_vector;
for (uint i = 0u; i < len(longitudinal_highway_parameters); ++i) {
longitudinal_highway_parameters_vector.push_back(
extract<double>(longitudinal_highway_parameters[i]));
}
std::vector<float> lateral_parameters_vector;
for (uint i = 0u; i < len(lateral_parameters); ++i) {
lateral_parameters_vector.push_back(
extract<double>(lateral_parameters[i]));
}
pipeline = std::make_shared<Pipeline>(longitudinal_parameters_vector,
longitudinal_highway_parameters_vector,
lateral_parameters_vector,
urban_target_velocity,
highway_target_velocity,
this->client);
};
};
}
using ActorList = std::vector<carla::SharedPtr<cc::Actor>>;
using Parameters = std::vector<float>;
BOOST_PYTHON_MODULE(traffic_manager) {
class_<traffic_manager::TrafficManager, boost::noncopyable>(
class_<ActorList>("actor_list").def(vector_indexing_suite<ActorList>());
class_<Parameters>("parameters").def(vector_indexing_suite<Parameters>());
class_<traffic_manager::Pipeline, boost::noncopyable>(
"traffic_manager",
init<list&, list&, list&, float, float, cc::Client&>())
init<Parameters, Parameters, Parameters, float, float, cc::Client&>())
.def("register_vehicles", &traffic_manager::Pipeline::RegisterVehicles)
.def("unregister_vehicles", &traffic_manager::Pipeline::UnregisterVehicles)
.def("start", &traffic_manager::Pipeline::Start)

View File

@ -0,0 +1,72 @@
import time
import carla
import random
import sys, os
from traffic_manager import traffic_manager, parameters, actor_list
ip = 'localhost'
port = 2000
number_of_vehicles = 20
client_connection = carla.Client(ip, port)
client_connection.set_timeout(2.0)
world = client_connection.get_world()
blueprints = world.get_blueprint_library().filter('vehicle.*')
blueprints = [x for x in blueprints if int(x.get_attribute('number_of_wheels')) == 4]
blueprints = [x for x in blueprints if not x.id.endswith('isetta')]
blueprints = [x for x in blueprints if not x.id.endswith('carlacola')]
spawn_points = world.get_map().get_spawn_points()
random.shuffle(spawn_points)
vehicle_list = []
batch = []
for n, transform in enumerate(spawn_points):
if n >= number_of_vehicles:
break
blueprint = random.choice(blueprints)
if blueprint.has_attribute('color'):
color = random.choice(blueprint.get_attribute('color').recommended_values)
blueprint.set_attribute('color', color)
if blueprint.has_attribute('driver_id'):
driver_id = random.choice(blueprint.get_attribute('driver_id').recommended_values)
blueprint.set_attribute('driver_id', driver_id)
blueprint.set_attribute('role_name', 'autopilot')
vehicle = world.spawn_actor(blueprint, transform)
vehicle_list.append(vehicle)
try:
long_pid = parameters()
long_high_pid = parameters()
lat_pid = parameters()
vehicle_vec = actor_list()
long_pid.extend([0.1, 0.15, 0.01])
long_high_pid.extend([5.0, 0.0, 0.1])
lat_pid.extend([10.0, 0.01, 0.1])
vehicle_vec.extend(vehicle_list)
tm = traffic_manager(long_pid, long_high_pid, lat_pid, 25.0/3.6, 50.0/3.6, client_connection)
tm.start()
time.sleep(1)
tm.register_vehicles(vehicle_vec)
while True:
time.sleep(1)
except Exception, e:
print e
print "Stopping TrafficManager!"
if tm:
tm.stop()
for vehicle in vehicle_list:
vehicle.destroy()