Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PyBind to bind CarmaClock to Python Module #24

Merged
merged 34 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
7731069
Add pybind to create python module binding for carma-clock
paulbourelly999 May 22, 2024
4479b13
Remove boost from install dependencies
paulbourelly999 May 22, 2024
bc54d8a
Update
paulbourelly999 May 22, 2024
f7efa99
Updates
paulbourelly999 May 22, 2024
f217ca1
Fix Python syntax
paulbourelly999 May 22, 2024
e3b4a36
Updates base on sonar lint
paulbourelly999 May 22, 2024
431fe85
Updates
paulbourelly999 May 22, 2024
3965fe4
Updates
paulbourelly999 May 22, 2024
e21cadd
Updates
paulbourelly999 May 22, 2024
6b656c4
Update
paulbourelly999 May 22, 2024
2699933
Updates
paulbourelly999 May 22, 2024
1c94ff5
Remove debug postfix from python imported library
paulbourelly999 May 22, 2024
d71abaf
Strip debug postfix
paulbourelly999 May 22, 2024
ad0eb38
Update
paulbourelly999 May 22, 2024
98a7202
Updates
paulbourelly999 May 22, 2024
d389e3e
Updates
paulbourelly999 May 22, 2024
d1f26b3
Updates
paulbourelly999 May 22, 2024
8abd078
Updates
paulbourelly999 May 22, 2024
d6a397b
Updates
paulbourelly999 May 22, 2024
4ee6cb8
Updates
paulbourelly999 May 22, 2024
7ed34b3
Fix sonar scan action
paulbourelly999 May 22, 2024
2bb1e24
Udpdates
paulbourelly999 May 22, 2024
fe2f7aa
Updates
paulbourelly999 May 22, 2024
b9adc87
Updates
paulbourelly999 May 22, 2024
c143580
Updates
paulbourelly999 May 22, 2024
5282d5f
Updates
paulbourelly999 May 22, 2024
030bba9
Updates
paulbourelly999 May 22, 2024
354ebd0
Updates
paulbourelly999 May 22, 2024
1924f68
Updates
paulbourelly999 May 22, 2024
863869d
Updates
paulbourelly999 May 24, 2024
1a54e7c
Updates
paulbourelly999 May 24, 2024
96ba379
Updates
paulbourelly999 May 24, 2024
7228b6f
Updated to make single library that condidtionally includes python
paulbourelly999 May 24, 2024
17a27ed
PR Updates
paulbourelly999 May 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
build: false
- architecture: armhf
ubuntu: jammy
build: true
build: false
- architecture: arm64
ubuntu: focal
build: false
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/sonar-scanner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ on:
name: Sonar Workflow
jobs:
sonarcloud:
uses: usdot-fhwa-stol/actions/.github/workflows/sonar-scanner-cpp.yml@main
uses: usdot-fhwa-stol/actions/.github/workflows/sonar-scanner-cpp.yml@fix-coverage
with:
sonar-project-key: usdot-fhwa-stol_carma-time-lib
build-image-tag: jammy
secrets:
github-token: ${{ secrets.GITHUB_TOKEN }}
sonarcloud-token: ${{ secrets.SONAR_TOKEN }}
35 changes: 31 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,51 @@ 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)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")

include(cmake/dependencies.cmake)
dan-du-car marked this conversation as resolved.
Show resolved Hide resolved
add_library(${PROJECT_NAME} SHARED )

target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
)

add_library(${PROJECT_NAME} SHARED)
set_target_properties(${PROJECT_NAME} PROPERTIES
SOVERSION 1
)

if (DEFINED ENV{BUILD_ARCHITECTURE})
dan-du-car marked this conversation as resolved.
Show resolved Hide resolved
message(STATUS "Building Python Module binding NOT supported for crosss compiling " $ENV{BUILD_ARCHITECTURE} " builds...")
else()
include(carma_clock_py/cmake/dependencies.cmake)
target_link_libraries( ${PROJECT_NAME} PUBLIC
Python3::Python
pybind11::module
)
message(STATUS "Building Python Module binding only supported for native x64 builds...")
# set up sources based on target name
target_sources (${PROJECT_NAME}
PRIVATE
carma_clock_py/src/carma_clock_py.cpp
)
FILE(COPY carma_clock_py/python_wrapper_test.py DESTINATION .)
add_test(NAME test_carma_clock_python_module_binding COMMAND ${PYTHON_EXECUTABLE} python_wrapper_test.py )
endif()

include(CommonSource)
include(Testing)
include(CodeCoverage)
include(InstallingGeneral)
include(InstallingConfigs)
set(CPACK_DEBIAN_PACKAGE_DEPENDS carma-clock-1 python3-dev)
set_target_properties(${PROJECT_NAME} PROPERTIES DEBUG_POSTFIX "")

include(Packing)


118 changes: 118 additions & 0 deletions carma_clock_py/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# CARMA Time Library

The CARMA Time Library is a focused library providing classes and utilities which enable a consuming project to synchronize mock time with the CARMA simulation framework.

# CI Status

These badges are for the default branch only.

[![Build Workflow](https://github.com/usdot-fhwa-stol/carma-time-lib/actions/workflows/build.yml/badge.svg)](https://github.com/usdot-fhwa-stol/carma-time-lib/actions/workflows/build.yml)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=usdot-fhwa-stol_carma-time-lib&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=usdot-fhwa-stol_carma-time-lib)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=usdot-fhwa-stol_carma-time-lib&metric=coverage)](https://sonarcloud.io/summary/new_code?id=usdot-fhwa-stol_carma-time-lib)
# Architecture

The CarmaClock class contains an internal representation for time which serves as a replacement for the epoch tracked by the system clock, or any other related time-keeping machinery.

This internal time value is stored as a TODO value representing TODO since TODO.

The framework provides the following capabilities:
1. Initialization and update functions which set the internal time representation mode and value.
2. Current time available for query.
3. Time representation available in real-time and simulation mode.
4. The framework falls back to system time if not running in simulation mode.
5. Utility functions.

# Building the Library
## Configuring Build Environment

The project build environment is supplied as a single-image [Docker Dev Environment](https://docs.docker.com/desktop/dev-environments/), with Visual Studio Code editor configurations to enable streamlined setup.

To load the project in VSCode:

1. Clone this repository.

```
git clone [email protected]:usdot-fhwa-stol/carma-time-lib.git
cd carma-time-lib/
```

2. Open the VSCode editor.
3. Open the cloned project folder.
4. Accept the **Reopen in Container** prompt for **"Folder contains a Dev Container configuration file. Reopen folder to develop in a container (learn more)."**

![Dev Container Rebuild Notification](doc/images/DevContainerRebuildNotification.png)

7. If that prompt did not appear, open the **View > Command Palette** and type to locate **"Dev Containers: Rebuild and Reopen in Container"**.
6. The editor should refresh and display the updated activity in the command line.

![Dev Container Rebuild Log](doc/images/DevContainerRebuildLog.png)

If the container startup was successful, the development environment is up and running and contains the library dependencies and compilation environment. It is possible to proceed with the build.

## Building
dan-du-car marked this conversation as resolved.
Show resolved Hide resolved

1. In VSCode, open **Build the Terminal > Run Build Task...**.

![Library Build Log](doc/images/LibraryBuildLog.png)

2. If the build is successful, tests are automatically executed and a Debian package is generated.
3. TODO more details (output product locations, reference information for generalized docker dev solution including container configured locations etc.).

## Additional IDE Configuration

Some additional configurations are provided out-of-the-box to streamline the developer's setup process. These are included for Visual Studio Code:

| Item | Description |
|---------------------------------------|----------------------------------------------------------------------------------------------------------------------|
| .devcontainer/devcontainer.json | Docker Dev Environment definition file. | | | |
| .editorconfig | Some standard, robust VSCode editor configurations. | | | |
| .github/workflows/build.yml | GitHub CI build definition. | | | |
| .github/workflows/sonar-scanner.yml | TODO | | | |
| .gitignore | Some helpful Git Ignore rules that should apply to Cmake-based C++ projects developed with most modern code editors. | | | |
| install_dependencies.sh | Docker Dev Environment internal script. TODO move this file? | | | |
| .vscode/tasks.json | Build task definition utilizing Docker Dev Environment. | | | |

## Deploying
TODO
# Using the Library
## Dependency Management
TODO
## Linking Against the Library
TODO
## Configuration

The following operating modes are available in the library:
* Real-time
* Simulated time

TODO How the library is configured

TODO Move this configuration logic into a time manager tool in a separate library.

# CARMA Projects

See all CARMA projects in the [USDOT FHWA STOL](https://github.com/usdot-fhwa-stol) organization on GitHub.

In particular, the CARMA Simulation framework is available [here](https://github.com/usdot-fhwa-stol/carma-simulation).

## Contribution

Welcome to the CARMA contributing guide. Please read this guide to learn about our development process, how to propose pull requests and improvements, and how to build and test your changes to this project. [CARMA Contributing Guide](https://github.com/usdot-fhwa-stol/carma-platform/blob/develop/Contributing.md)

## Code of Conduct

Please read our [CARMA Code of Conduct](https://github.com/usdot-fhwa-stol/carma-platform/blob/develop/Code_of_Conduct.md) which outlines our expectations for participants within the CARMA community, as well as steps to reporting unacceptable behavior. We are committed to providing a welcoming and inspiring community for all and expect our code of conduct to be honored. Anyone who violates this code of conduct may be banned from the community.

## Attribution

The development team would like to acknowledge the people who have made direct contributions to the design and code in this repository. [CARMA Attribution](https://github.com/usdot-fhwa-stol/carma-platform/blob/develop/ATTRIBUTION.md)

## License

By contributing to the Federal Highway Administration (FHWA) Connected Automated Research Mobility Applications (CARMA), you agree that your contributions will be licensed under its Apache License 2.0 license. [CARMA License](https://github.com/usdot-fhwa-stol/carma-platform/blob/develop/docs/License.md)

## Contact

Please click on the CARMA logo below to visit the Federal Highway Adminstration(FHWA) CARMA website.

[![CARMA Image](https://raw.githubusercontent.com/usdot-fhwa-stol/CARMAPlatform/develop/docs/image/CARMA_icon.png)](https://highways.dot.gov/research/research-programs/operations/CARMA)
15 changes: 15 additions & 0 deletions carma_clock_py/cmake/dependencies.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
dan-du-car marked this conversation as resolved.
Show resolved Hide resolved
include(GetCPM)
set(CPM_USE_LOCAL_PACKAGES ON)
set(PYBIND11_INSTALL ON)
set(PYBIND11_TEST OFF)
CPMAddPackage(
NAME pybind11
GITHUB_REPOSITORY pybind/pybind11
SYSTEM ON
GIT_TAG v2.12.0
OPTIONS
"PYBIND11_INSTALL ON"
"PYBIND11_TEST OFF"
)

29 changes: 29 additions & 0 deletions carma_clock_py/python_wrapper_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/python3
import unittest
import libcarma_clock
import time

"""Test Case for testing basic CarmaClock functionality in python
"""
class TestCarmaClock(unittest.TestCase):

def testSimClockInitializeException(self):
sim_clock = libcarma_clock.CarmaClock(True)
with self.assertRaises(ValueError) as e:
sim_clock.nowInMilliseconds()
dan-du-car marked this conversation as resolved.
Show resolved Hide resolved

self.assertIn('Clock is not initialized!',str(e.exception))

def testRealClockTime(self):
clock = libcarma_clock.CarmaClock()
self.assertAlmostEqual(time.time()*1000, clock.nowInMilliseconds(),delta=10)

def testSimClockUpdate(self):
sim_clock = libcarma_clock.CarmaClock(True)
sim_clock.update(100)
self.assertEqual(100, sim_clock.nowInMilliseconds())
sim_clock.update(1500)
self.assertEqual(1500, sim_clock.nowInMilliseconds())

if __name__ == '__main__':
unittest.main()
17 changes: 17 additions & 0 deletions carma_clock_py/src/carma_clock_py.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <pybind11/pybind11.h>

#include <carma_clock.h>
namespace py = pybind11;

PYBIND11_MODULE(libcarma_clock, m) {
py::class_<fwha_stol::lib::time::CarmaClock>(m, "CarmaClock")
.def(py::init<bool>(), py::arg("simulation_mode")=false)
.def("nowInSeconds", &fwha_stol::lib::time::CarmaClock::nowInSeconds)
.def("nowInMilliseconds", &fwha_stol::lib::time::CarmaClock::nowInMilliseconds)
.def("update", &fwha_stol::lib::time::CarmaClock::update, py::arg("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, py::arg("future_time"))
.def("sleep_for",&fwha_stol::lib::time::CarmaClock::sleep_for, py::arg("time_to_sleep"));

}
2 changes: 2 additions & 0 deletions cmake/dependencies.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
find_package(Threads)

10 changes: 8 additions & 2 deletions install_dependencies.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#!/bin/bash

set -ex

# TODO
# export BUILD_ARCHITECTURE=arm64
dan-du-car marked this conversation as resolved.
Show resolved Hide resolved
# Get ubuntu distribution code name. All STOL APT debian packages are pushed to S3 bucket based on distribution codename.
echo "BUILD_ARCHITECTURE=${BUILD_ARCHITECTURE}"
if [[ "${BUILD_ARCHITECTURE}" =~ arm* ]]; then
echo "WARNING: Python module support currently only exists for native x64 builds. Skipping Python module binding ... "
else
/opt/carma/scripts/install_dependencies_script.sh python3-dev
dan-du-car marked this conversation as resolved.
Show resolved Hide resolved
fi
Loading