Skip to content

Commit

Permalink
Merge pull request #53 from westonrobot/feature-python_bindings
Browse files Browse the repository at this point in the history
Feature python bindings
  • Loading branch information
hanskw-weston authored May 30, 2024
2 parents 012a9aa + e4e7836 commit 700040b
Show file tree
Hide file tree
Showing 25 changed files with 1,291 additions and 7 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/pybind-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Pybind

on:
push:
pull_request:
branches:
- main

jobs:
build:
strategy:
matrix:
os: [ ubuntu-22.04, ubuntu-20.04 ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- name: Install dependencies
run: sudo apt-get install -y build-essential cmake libasio-dev python3-dev
- name: Build python package
run: python setup.py build_ext --inplace
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ cmake-build-release/

.editorconfig

# Python
dist
*.egg-info

# Temp files
*~
*/~
Expand Down Expand Up @@ -49,7 +53,7 @@ src/lcmtypes/python
*.pch

# Compiled Dynamic libraries
#*.so
*.so
#*.dylib
#*.dll

Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "test/googletest"]
path = test/googletest
url = https://github.com/google/googletest.git
[submodule "python/pybind11"]
path = python/pybind11
url = https://github.com/pybind/pybind11.git
10 changes: 8 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ endif()
## Project Options
option(BUILD_TESTING "Build tests" OFF)
option(STATIC_CHECK "Run static check" OFF)
# set(CMAKE_BUILD_TYPE Release)
# set(CMAKE_BUILD_TYPE Debug)
option(PYTHON_BINDING "Build python binding" OFF)

## Check if pkg is built with ROS catkin
if(CATKIN_DEVEL_PREFIX)
Expand Down Expand Up @@ -111,6 +110,7 @@ add_library(${PROJECT_NAME}
)
target_link_libraries(${PROJECT_NAME} PUBLIC Threads::Threads)
target_compile_definitions(${PROJECT_NAME} PUBLIC ASIO_ENABLE_OLD_SERVICES)
target_compile_options(${PROJECT_NAME} PRIVATE -fPIC)
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
Expand All @@ -121,6 +121,12 @@ if(BUILD_WITHOUT_ROS)
add_subdirectory(sample)
endif()

# Build python binding
if(PYTHON_BINDING)
message(STATUS "Build python binding")
add_subdirectory(python)
endif()

# Build tests
if(BUILD_TESTS)
add_subdirectory(test)
Expand Down
9 changes: 9 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
include CMakeLists.txt
include LICENSE
include README.md
include package.xml
recursive-include sample *
recursive-include cmake *
recursive-include include *
recursive-include src *
recursive-include python *
52 changes: 50 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
This software package provides a C++ interface to communicate with the mobile platforms, for sending commands to the
robot and receiving the latest robot state. The repository is a joint effort by the development teams at Weston Robot (Singapore) and AgileX Robotics (China).

- Copyright (c) 2020-2023 [Weston Robot](https://www.westonrobot.com/)
- Copyright (c) 2020-2023 [AgileX Robotics](http://www.agilex.ai/?lang=zh-cn)
- Copyright (c) 2020-2024 [Weston Robot](https://www.westonrobot.com/)
- Copyright (c) 2020-2024 [AgileX Robotics](http://www.agilex.ai/?lang=zh-cn)

Please create an issue on Github at https://github.com/westonrobot/ugv_sdk/issues if you encounter any problems when
using the packages.
Expand Down Expand Up @@ -62,6 +62,54 @@ $ cd ..
$ catkin_make
```

### Build the package as a CMake project

```
$ git clone https://github.com/westonrobot/ugv_sdk.git
$ cd ugv_sdk
$ mkdir build && cd build
$ cmake .. && make
```

If you need Python binding, you can build the package with the following command

```bash
$ sudo apt-get install python3-dev
```

```
$ git clone --recursive https://github.com/westonrobot/ugv_sdk.git
$ cd ugv_sdk
# if you've checked out ugv_sdk without --recursive, you can run the following command to fetch the submodule
$ git submodule update --init --recursive
$ mkdir build && cd build
$ cmake -DPYTHON_BINDING=ON .. && make
```

### Build the package as a Python package

For development and testing, you can use inplace build

```bash
$ python setup.py build_ext --inplace
```

It will generate library files in build/temp.linux-x86_64-3.10/lib (the name could be different based on your system) and in the directory where you run the command.

You can then use the library in a python script from the same directory directly. If you want to use the library in other directories, you can add the path to the PYTHONPATH environment variable.

Please note that setting the PYTHONPATH directly in the shell will only affect the current session.

```bash
$ export PYTHONPATH=$PYTHONPATH:<path-to-the-repo>/build/lib.linux-x86_64-3.10
```

You can also install the package to your Python environment using

```bash
$ python3 -m pip install .
```

## Setup CAN-To-USB Adapter

1. Enable gs_usb kernel module
Expand Down
2 changes: 1 addition & 1 deletion include/ugv_sdk/mobile_robot/ranger_robot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class RangerRobot : public RobotCommonInterface, public RangerInterface {
bool Connect(std::string can_name) override;

void EnableCommandedMode() override;
std::string RequestVersion(int timeout_sec) override;
std::string RequestVersion(int timeout_sec = 3) override;

// functions to be implemented by each robot class
void ResetRobotState() override;
Expand Down
2 changes: 1 addition & 1 deletion package.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<package>
<name>ugv_sdk</name>
<version>0.1.6</version>
<version>0.8.0</version>
<description>Weston Robot Platform SDK</description>

<author email="[email protected]">Ruixiang Du</author>
Expand Down
2 changes: 2 additions & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_subdirectory(pybind11)
add_subdirectory(ugv_sdk_py)
1 change: 1 addition & 0 deletions python/pybind11
Submodule pybind11 added at 3e9dfa
17 changes: 17 additions & 0 deletions python/ugv_sdk_py/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pybind11_add_module(ugv_sdk_py
src/ugv_sdk_py.cpp
src/agilex_message.cpp
src/scout_robot.cpp
src/hunter_robot.cpp
src/ranger_robot.cpp
)
target_link_libraries(ugv_sdk_py PRIVATE ugv_sdk)
target_include_directories(ugv_sdk_py PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE src)
set(PYBIND_OUTPUT_LIBDIR ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR} CACHE PATH "Output directory for pybind libraries")
message(STATUS " - To install pybind components to ${PYBIND_OUTPUT_LIBDIR}")
set_target_properties(ugv_sdk_py PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${PYBIND_OUTPUT_LIBDIR}
)
28 changes: 28 additions & 0 deletions python/ugv_sdk_py/include/ugv_sdk_py/agilex_message.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* agilex_message.hpp
*
* Created on 5/8/24 7:36 PM
* Description:
*
* Copyright (c) 2024 Weston Robot Pte. Ltd.
*/

#ifndef UGV_SDK_AGILEX_MESSAGE_HPP
#define UGV_SDK_AGILEX_MESSAGE_HPP

#include <pybind11/pybind11.h>

namespace westonrobot {
void BindProtocolVersion(pybind11::module &m);
void BindSystemStateMessage(pybind11::module &m);
void BindMotionStateMessage(pybind11::module &m);
void BindLightStateMessage(pybind11::module &m);
void BindRcStateMessage(pybind11::module &m);
void BindActuatorHSStateMessage(pybind11::module &m);
void BindActuatorLSStateMessage(pybind11::module &m);
void BindActuatorStateMessageV1(pybind11::module &m);
void BindMotionModeStateMessage(pybind11::module &m);
void BindBmsBasicMessage(pybind11::module &m);
}

#endif //UGV_SDK_AGILEX_MESSAGE_HPP
19 changes: 19 additions & 0 deletions python/ugv_sdk_py/include/ugv_sdk_py/hunter_robot.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @file hunter_robot.hpp
* @brief
* @date 28-05-2024
*
* hunter_robot submodule for ugv_sdk_py
*
* @copyright Copyright (c) 2024 Weston Robot Pte. Ltd.
*/
#ifndef UGV_SDK_PY_HUNTER_ROBOT_HPP
#define UGV_SDK_PY_HUNTER_ROBOT_HPP

#include <pybind11/pybind11.h>

namespace westonrobot {
void BindHunterRobot(pybind11::module &m);
}

#endif /* UGV_SDK_PY_HUNTER_ROBOT_HPP */
19 changes: 19 additions & 0 deletions python/ugv_sdk_py/include/ugv_sdk_py/ranger_robot.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @file ranger_robot.hpp
* @brief
* @date 28-05-2024
*
* ranger_robot submodule for ugv_sdk_py
*
* @copyright Copyright (c) 2024 Weston Robot Pte. Ltd.
*/
#ifndef UGV_SDK_PY_RANGER_ROBOT_HPP
#define UGV_SDK_PY_RANGER_ROBOT_HPP
#include <pybind11/pybind11.h>

namespace westonrobot {
void BindRangerRobot(pybind11::module &m);
}


#endif /* UGV_SDK_PY_RANGER_ROBOT_HPP */
19 changes: 19 additions & 0 deletions python/ugv_sdk_py/include/ugv_sdk_py/scout_robot.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @file scout_robot.hpp
* @brief
* @date 28-05-2024
*
* scout_robot submodule for ugv_sdk_py
*
* @copyright Copyright (c) 2024 Weston Robot Pte. Ltd.
*/
#ifndef UGV_SDK_PY_SCOUT_ROBOT_HPP
#define UGV_SDK_PY_SCOUT_ROBOT_HPP

#include <pybind11/pybind11.h>

namespace westonrobot {
void BindScoutRobot(pybind11::module &m);
}

#endif /* UGV_SDK_PY_SCOUT_ROBOT_HPP */
Loading

0 comments on commit 700040b

Please sign in to comment.