From eef5cd5276315dae2a30411d520f595480ec1ea4 Mon Sep 17 00:00:00 2001 From: Praveen Kumar Date: Fri, 11 Oct 2019 21:40:50 +0530 Subject: [PATCH] WIP : python wrapper for TrafficManager --- TrafficManager/CMakeLists.txt | 32 ++++++++-- .../source/pipeline/python/Wrapper.cpp | 61 ++++++++++++++++++- 2 files changed, 87 insertions(+), 6 deletions(-) diff --git a/TrafficManager/CMakeLists.txt b/TrafficManager/CMakeLists.txt index febadea6e..811195798 100644 --- a/TrafficManager/CMakeLists.txt +++ b/TrafficManager/CMakeLists.txt @@ -7,7 +7,11 @@ set(CMAKE_CXX_COMPILER /usr/bin/clang++-7) # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -fPIC -O3 -g -fsanitize=address" CACHE STRING "" FORCE) ## Release options -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -O3 -DNDEBUG" CACHE STRING "" FORCE) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -fPIC -O3 -DNDEBUG" CACHE STRING "" FORCE) + +# Scanning for python wrapper dependencies +find_package(PythonInterp REQUIRED) +find_package(PythonLibs REQUIRED) set(CARLA_LOCATION $ENV{CARLA_LOCATION}) @@ -16,6 +20,8 @@ include_directories(${CARLA_LOCATION}/LibCarla/source/) include_directories(${CARLA_LOCATION}/Build/boost-1.69.0-c7-install/include) include_directories(${CARLA_LOCATION}/Build/recast-cdce4e-c7-install/include) include_directories(${CARLA_LOCATION}/Build/rpclib-v2.2.1_c2-c7-libstdcxx-install/include) +# Including python wrapper dependencies +include_directories(${PYTHON_INCLUDE_DIRS}) link_directories(${CARLA_LOCATION}/Build/boost-1.69.0-c7-install/lib) link_directories(${CARLA_LOCATION}/Build/recast-cdce4e-c7-install/lib) @@ -23,12 +29,30 @@ link_directories(${CARLA_LOCATION}/Build/rpclib-v2.2.1_c2-c7-libstdcxx-install/l link_directories(${CARLA_LOCATION}/PythonAPI/carla/dependencies/lib) file(GLOB SOURCES ${CARLA_LOCATION}/TrafficManager/source/pipeline/*.cpp) + ## Release executable -file(GLOB SOURCES_EXE ${CARLA_LOCATION}/TrafficManager/source/pipeline/executable/PipelineExecutable.cpp) +# file(GLOB SOURCES_EXE ${CARLA_LOCATION}/TrafficManager/source/pipeline/executable/PipelineExecutable.cpp) + ## Debug executable # file(GLOB SOURCES_EXE ${CARLA_LOCATION}/TrafficManager/source/test/Test.cpp) add_library(pipeline ${SOURCES}) -add_executable(traffic_manager ${SOURCES_EXE}) -target_link_libraries(traffic_manager pipeline carla_client rpc pthread boost_system Detour DetourCrowd DetourTileCache Recast) +# Executable target +# add_executable(traffic_manager ${SOURCES_EXE}) + +# Python wrapper target +add_library(traffic_manager SHARED ${CARLA_LOCATION}/TrafficManager/source/pipeline/python/Wrapper.cpp) + +# Linking executable target +# target_link_libraries(traffic_manager pipeline carla_client rpc pthread boost_system +# Detour DetourCrowd DetourTileCache Recast) + +# Linking python wrapper +target_link_libraries(traffic_manager ${PYTHON_LIBRARIES} + pipeline carla_client rpc pthread boost_system boost_python27 + Detour DetourCrowd DetourTileCache Recast) + +# Setting target properties for python wrapper +set_target_properties(traffic_manager PROPERTIES SUFFIX .so) +set_target_properties(traffic_manager PROPERTIES PREFIX "") diff --git a/TrafficManager/source/pipeline/python/Wrapper.cpp b/TrafficManager/source/pipeline/python/Wrapper.cpp index 148fcea48..d2f8af097 100644 --- a/TrafficManager/source/pipeline/python/Wrapper.cpp +++ b/TrafficManager/source/pipeline/python/Wrapper.cpp @@ -1,10 +1,67 @@ +#pragma once +#include + +#include "carla/client/Client.h" #include "boost/python.hpp" #include "Pipeline.h" +namespace cc = carla::client; using namespace boost::python; +namespace traffic_manager { + + class TrafficManager { + + private: + std::shared_ptr 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 longitudinal_parameters_vector; + for (uint i = 0u; i < len(longitudinal_parameters); ++i) { + longitudinal_parameters_vector.push_back( + extract(longitudinal_parameters[i])); + } + + std::vector longitudinal_highway_parameters_vector; + for (uint i = 0u; i < len(longitudinal_highway_parameters); ++i) { + longitudinal_highway_parameters_vector.push_back( + extract(longitudinal_highway_parameters[i])); + } + + std::vector lateral_parameters_vector; + for (uint i = 0u; i < len(lateral_parameters); ++i) { + lateral_parameters_vector.push_back( + extract(lateral_parameters[i])); + } + + pipeline = std::make_shared(longitudinal_parameters_vector, + longitudinal_highway_parameters_vector, + lateral_parameters_vector, + urban_target_velocity, + highway_target_velocity, + this->client); + }; + }; + +} + BOOST_PYTHON_MODULE(traffic_manager) { - class_("traffic_manager", init<>()); -} \ No newline at end of file + class_( + "traffic_manager", + init()) + .def("register_vehicles", &traffic_manager::Pipeline::RegisterVehicles) + .def("unregister_vehicles", &traffic_manager::Pipeline::UnregisterVehicles) + .def("start", &traffic_manager::Pipeline::Start) + .def("stop", &traffic_manager::Pipeline::Stop); +}