Skip to content

Commit

Permalink
Update carma-clock to compile into python module using boost python
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbourelly999 committed May 20, 2024
1 parent b15b5a8 commit 2c9b1d5
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-dockerfile
{
"name": "CARMA build image",
"image" : "ghcr.io/usdot-fhwa-stol/carma-builds-x64:latest",
"image" : "ghcr.io/usdot-fhwa-stol/carma-builds-x64:jammy",
// "image" : "carma_cmake_builder_x64",

// Features to add to the dev container. More info: https://containers.dev/features.
Expand Down
16 changes: 14 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,35 @@ set(CMAKE_MODULE_PATH "$ENV{CARMA_OPT_DIR}/cmake")
set(CMAKE_CXX_STANDARD 17)


project(carma-clock
project(carma_clock
DESCRIPTION "CARMA time library"
HOMEPAGE_URL https://github.com/usdot-fhwa-stol/carma-time-lib
VERSION 0.0.1
LANGUAGES CXX
)
find_package(Threads)
include(cmake/dependencies.cmake)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")


add_library(${PROJECT_NAME} SHARED)
target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
)
target_link_libraries( ${PROJECT_NAME} PUBLIC
Boost::python
Python3::Python
)

set_target_properties(${PROJECT_NAME} PROPERTIES
SOVERSION 1
)

include(CommonSource)
include(Testing)
# Test for Python inclusion
FILE(COPY python_wrapper_test.py DESTINATION .)
ADD_TEST(NAME python_wrapper_test COMMAND ${PYTHON_EXECUTABLE} python_wrapper_test.py)
include(CodeCoverage)
include(InstallingGeneral)
include(InstallingConfigs)
Expand Down
4 changes: 4 additions & 0 deletions Config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")

find_dependency(Boost COMPONENTS python REQUIRED )
find_dependency(Threads)
find_dependency(Python3 COMPONENTS Interpreter Development)

check_required_components(@PROJECT_NAME@)
6 changes: 6 additions & 0 deletions include/carma_clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <mutex>
#include <vector>


namespace fwha_stol::lib::time {

/** Alias for a timestamp in seconds. */
Expand Down Expand Up @@ -48,6 +49,11 @@ class CarmaClock {
* @brief Destructor
*/
~CarmaClock() = default;
// Public move constructor and move assignment operator
CarmaClock(const CarmaClock&) = delete; // Deleted copy constructor
CarmaClock& operator=(const CarmaClock&) = delete; // Deleted copy assignment operator
CarmaClock(CarmaClock&&) = delete; // Deleted move constructor
CarmaClock& operator=(CarmaClock&&) = delete; // Deleted move assignment operator

public:
/**
Expand Down
1 change: 1 addition & 0 deletions install_dependencies.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash

set -ex
apt install libboost-python1.74-dev

# TODO
6 changes: 6 additions & 0 deletions python_wrapper_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/python3

import libcarma_clockd
print("Creating Clock Object")

# clock = libcarma_clockd.CarmaClock()
16 changes: 16 additions & 0 deletions src/python_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <boost/python.hpp>
#include <boost/python/def.hpp>
#include "carma_clock.h"

BOOST_PYTHON_MODULE(libcarma_clockd) {
using namespace boost::python;
class_<fwha_stol::lib::time::CarmaClock, boost::noncopyable>("CarmaClock", init<bool>())
.def("nowInSeconds", &fwha_stol::lib::time::CarmaClock::nowInSeconds)
.def("nowInMilliseconds", &fwha_stol::lib::time::CarmaClock::nowInMilliseconds)
.def("update", &fwha_stol::lib::time::CarmaClock::update,args("current_time"))
.def("is_simulation_mode", &fwha_stol::lib::time::CarmaClock::is_simulation_mode)
.def("wait_for_initialization",&fwha_stol::lib::time::CarmaClock::wait_for_initialization)
.def("sleep_until",&fwha_stol::lib::time::CarmaClock::sleep_until,args("future_time"))
.def("sleep_for",&fwha_stol::lib::time::CarmaClock::sleep_for,args("time_to_sleep"));
init_module_libcarma_clockd();
}

0 comments on commit 2c9b1d5

Please sign in to comment.