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

feat: migrate communication interfaces #190

Merged
merged 5 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Release Versions

- [8.1.0](#810)
- [8.0.0](#800)
- [7.4.0](#740)
- [7.3.0](#730)
Expand All @@ -13,6 +14,16 @@ Release Versions
- [6.3.0](#630)
- [6.2.0](#620)

## 8.1.0

Version 8.1.0 adds a new module called `communication_interfaces` to the control libraries. This is a library for
simple socket communication and was previously developed in a different place. It currently implements sockets for UPD,
TCP, and ZMQ communication.

### Full changelog

- feat: migrate communication interfaces (#190)

## 8.0.0

Version 8.0.0 is a major update that adds new state types and collision detection features to control-libraries.
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ COPY --from=apt-dependencies /tmp/apt /
COPY --from=base-dependencies /tmp/deps /usr
COPY dependencies/dependencies.cmake CMakeLists.txt
RUN --mount=type=cache,target=/build,id=cmake-deps-${TARGETPLATFORM}-${CACHEID},uid=1000 \
cmake -B build -Dprotobuf_BUILD_TESTS=OFF -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} && cmake --build build && cmake --install build --prefix /tmp/deps
cmake -B build -Dprotobuf_BUILD_TESTS=OFF -DCPPZMQ_BUILD_TESTS=OFF -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} \
&& cmake --build build && cmake --install build --prefix /tmp/deps
COPY --from=base-dependencies /tmp/deps /tmp/deps
COPY --from=pinocchio-dependencies /tmp/deps /tmp/deps

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8.0.0
8.1.0
1 change: 1 addition & 0 deletions apt-packages.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ libboost-all-dev
libeigen3-dev
liburdfdom-dev
libassimp-dev
libzmq3-dev
2 changes: 1 addition & 1 deletion demos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

find_package(control_libraries 8.0.0 CONFIG REQUIRED)
find_package(control_libraries 8.1.0 CONFIG REQUIRED)

set(DEMOS_SCRIPTS
task_space_control_loop
Expand Down
10 changes: 9 additions & 1 deletion dependencies/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ FetchContent_Declare(
SOURCE_SUBDIR cmake
)

FetchContent_MakeAvailable(OsqpEigen protobuf)
FetchContent_Declare(
cppzmq
GIT_REPOSITORY https://github.com/zeromq/cppzmq/
GIT_TAG v4.7.1
)
FetchContent_MakeAvailable(cppzmq)


FetchContent_MakeAvailable(OsqpEigen protobuf cppzmq)
2 changes: 1 addition & 1 deletion doxygen/doxygen.conf
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ PROJECT_NAME = "Control Libraries"
# could be handy for archiving the generated documentation or if some version
# control system is used.

PROJECT_NUMBER = 8.0.0
PROJECT_NUMBER = 8.1.0

# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
Expand Down
2 changes: 1 addition & 1 deletion protocol/clproto_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.15)

project(clproto VERSION 8.0.0)
project(clproto VERSION 8.1.0)

# Default to C99
if(NOT CMAKE_C_STANDARD)
Expand Down
31 changes: 31 additions & 0 deletions python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,34 @@ encoded_msg = clproto.encode(B, clproto.MessageType.JOINT_STATE_MESSAGE)

decoded_object = clproto.decode(encoded_msg)
```

### Note on the communication interfaces

The Python bindings require an additional step of sanitizing the data when sending and receiving bytes. To illustrate
this, an example is provided here.

```python
# First a server and a client is connected
context = ZMQContext()
server = ZMQPublisher(ZMQSocketConfiguration(context, "127.0.0.1", "5001", True))
client = ZMQSubscriber(ZMQSocketConfiguration(context, "127.0.0.1", "5001", False))
server.open()
client.open()
```

```python
# Then a string is sent through
str_msg = "Hello!"
server.send_bytes(str_msg)
received_str_msg = client.receive_bytes()
if received_str_msg is not None:
print(received_str_msg)
```

Here we expect the printed value to be `Hello!`, however due to the way strings and bytes are processed, the string
message is left as a byte literal and `b'Hello!'` is printed instead. We can correct this as follows:

```python
# Instead, decode the value
print(received_str_msg.decode("utf-8")) # will print Hello! as expected
```
8 changes: 8 additions & 0 deletions python/include/communication_interfaces_bindings.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <pybind11/pybind11.h>

namespace py = pybind11;
using namespace pybind11::literals;

void bind_tcp(py::module_& m);
void bind_udp(py::module_& m);
void bind_zmq(py::module_& m);
16 changes: 14 additions & 2 deletions python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
# names of the environment variables that define osqp and openrobots include directories
osqp_path_var = 'OSQP_INCLUDE_DIR'

__version__ = "8.0.0"
__libraries__ = ['state_representation', 'clproto', 'controllers', 'dynamical_systems', 'robot_model']
__version__ = "8.1.0"
__libraries__ = ['state_representation', 'clproto', 'controllers', 'dynamical_systems', 'robot_model', 'communication_interfaces']
__include_dirs__ = ['include']

__install_clproto_module__ = True
__install_controllers_module__ = True
__install_dynamical_systems_module__ = True
__install_robot_model_module__ = True
__install_communication_interfaces_module__ = True

# check that necessary libraries can be found
try:
Expand Down Expand Up @@ -122,6 +123,17 @@
)
)

if __install_communication_interfaces_module__:
ext_modules.append(
Pybind11Extension('communication_interfaces',
sorted(glob('source/communication_interfaces/*.cpp')),
cxx_std=17,
include_dirs=__include_dirs__,
libraries=['communication_interfaces'],
define_macros=[('MODULE_VERSION_INFO', __version__)],
)
)

setup(
name='control-libraries',
version=__version__,
Expand Down
27 changes: 27 additions & 0 deletions python/source/communication_interfaces/bind_tcp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "communication_interfaces_bindings.hpp"

#include <communication_interfaces/sockets/TCPClient.hpp>
#include <communication_interfaces/sockets/TCPServer.hpp>

using namespace communication_interfaces::sockets;

void bind_tcp(py::module_& m) {
py::class_<TCPClientConfiguration>(m, "TCPClientConfiguration")
.def(
py::init<std::string, int, int>(), "TCPClientConfiguration struct", "ip_address"_a, "port"_a, "buffer_size"_a)
.def_readwrite("ip_address", &TCPClientConfiguration::ip_address)
.def_readwrite("port", &TCPClientConfiguration::port)
.def_readwrite("buffer_size", &TCPClientConfiguration::buffer_size);

py::class_<TCPClient, std::shared_ptr<TCPClient>, ISocket>(m, "TCPClient")
.def(py::init<TCPClientConfiguration>(), "Constructor taking the configuration struct", "configuration"_a);

py::class_<TCPServerConfiguration>(m, "TCPServerConfiguration")
.def(py::init<int, int, bool>(), "TCPServerConfiguration struct", "port"_a, "buffer_size"_a, "enable_reuse"_a)
.def_readwrite("port", &TCPServerConfiguration::port)
.def_readwrite("buffer_size", &TCPServerConfiguration::buffer_size)
.def_readwrite("enable_reuse", &TCPServerConfiguration::enable_reuse);

py::class_<TCPServer, std::shared_ptr<TCPServer>, ISocket>(m, "TCPServer")
.def(py::init<TCPServerConfiguration>(), "Constructor taking the configuration struct", "configuration"_a);
}
24 changes: 24 additions & 0 deletions python/source/communication_interfaces/bind_udp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "communication_interfaces_bindings.hpp"

#include <communication_interfaces/sockets/UDPClient.hpp>
#include <communication_interfaces/sockets/UDPServer.hpp>

using namespace communication_interfaces::sockets;

void bind_udp(py::module_& m) {
py::class_<UDPSocketConfiguration>(m, "UDPSocketConfiguration")
.def(
py::init<std::string, int, int, bool, double>(), "UDPSocketConfiguration struct", "ip_address"_a, "port"_a,
"buffer_size"_a, "enable_reuse"_a = false, "timeout_duration_sec"_a = 0.0)
.def_readwrite("ip_address", &UDPSocketConfiguration::ip_address)
.def_readwrite("port", &UDPSocketConfiguration::port)
.def_readwrite("buffer_size", &UDPSocketConfiguration::buffer_size)
.def_readwrite("enable_reuse", &UDPSocketConfiguration::enable_reuse)
.def_readwrite("timeout_duration_sec", &UDPSocketConfiguration::timeout_duration_sec);

py::class_<UDPClient, std::shared_ptr<UDPClient>, ISocket>(m, "UDPClient")
.def(py::init<UDPSocketConfiguration>(), "Constructor taking the configuration struct", "configuration"_a);

py::class_<UDPServer, std::shared_ptr<UDPServer>, ISocket>(m, "UDPServer")
.def(py::init<UDPSocketConfiguration>(), "Constructor taking the configuration struct", "configuration"_a);
}
87 changes: 87 additions & 0 deletions python/source/communication_interfaces/bind_zmq.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include "communication_interfaces_bindings.hpp"

#include <communication_interfaces/sockets/ZMQPublisherSubscriber.hpp>

using namespace communication_interfaces::sockets;

class PyZMQContext {
public:
PyZMQContext(int io_threads = 1) { context = std::make_shared<zmq::context_t>(io_threads); }

std::shared_ptr<zmq::context_t> context;
};

struct PyZMQSocketConfiguration {
PyZMQContext context;
std::string ip_address;
std::string port;
bool bind = true;
bool wait = false;
};

struct PyZMQCombinedSocketsConfiguration {
PyZMQContext context;
std::string ip_address;
std::string publisher_port;
std::string subscriber_port;
bool bind_publisher = true;
bool bind_subscriber = true;
bool wait = false;
};

class PyZMQPublisher : public ZMQPublisher, public std::enable_shared_from_this<PyZMQPublisher> {
public:
PyZMQPublisher(PyZMQSocketConfiguration config)
: ZMQPublisher({config.context.context, config.ip_address, config.port, config.bind, config.wait}) {}
};

class PyZMQSubscriber : public ZMQSubscriber, public std::enable_shared_from_this<PyZMQSubscriber> {
public:
PyZMQSubscriber(PyZMQSocketConfiguration config)
: ZMQSubscriber({config.context.context, config.ip_address, config.port, config.bind, config.wait}) {}
};

class PyZMQPublisherSubscriber : public ZMQPublisherSubscriber,
public std::enable_shared_from_this<PyZMQPublisherSubscriber> {
public:
PyZMQPublisherSubscriber(PyZMQCombinedSocketsConfiguration config)
: ZMQPublisherSubscriber(
{config.context.context, config.ip_address, config.publisher_port, config.subscriber_port,
config.bind_publisher, config.bind_subscriber, config.wait}) {}
};

void bind_zmq(py::module_& m) {
py::class_<PyZMQContext>(m, "ZMQContext").def(py::init<int>(), "Create a ZMQ context", "io_threads"_a = 1);

py::class_<PyZMQSocketConfiguration>(m, "ZMQSocketConfiguration")
.def(
py::init<PyZMQContext, std::string, std::string, bool, bool>(), "ZMQSocketConfiguration struct", "context"_a,
"ip_address"_a, "port"_a, "bind"_a = true, "wait"_a = false)
.def_readwrite("ip_address", &PyZMQSocketConfiguration::ip_address)
.def_readwrite("port", &PyZMQSocketConfiguration::port)
.def_readwrite("bind", &PyZMQSocketConfiguration::bind)
.def_readwrite("wait", &PyZMQSocketConfiguration::wait);

py::class_<PyZMQCombinedSocketsConfiguration>(m, "ZMQCombinedSocketsConfiguration")
.def(
py::init<PyZMQContext, std::string, std::string, std::string, bool, bool, bool>(),
"ZMQCombinedSocketsConfiguration struct", "context"_a, "ip_address"_a, "publisher_port"_a,
"subscriber_port"_a, "bind_publisher"_a = true, "bind_subscriber"_a = true, "wait"_a = false)
.def_readwrite("ip_address", &PyZMQCombinedSocketsConfiguration::ip_address)
.def_readwrite("publisher_port", &PyZMQCombinedSocketsConfiguration::publisher_port)
.def_readwrite("subscriber_port", &PyZMQCombinedSocketsConfiguration::subscriber_port)
.def_readwrite("bind_publisher", &PyZMQCombinedSocketsConfiguration::bind_publisher)
.def_readwrite("bind_subscriber", &PyZMQCombinedSocketsConfiguration::bind_subscriber)
.def_readwrite("wait", &PyZMQCombinedSocketsConfiguration::wait);

py::class_<PyZMQPublisher, std::shared_ptr<PyZMQPublisher>, ISocket>(m, "ZMQPublisher")
.def(py::init<PyZMQSocketConfiguration>(), "Constructor taking the configuration struct", "configuration"_a);

py::class_<PyZMQSubscriber, std::shared_ptr<PyZMQSubscriber>, ISocket>(m, "ZMQSubscriber")
.def(py::init<PyZMQSocketConfiguration>(), "Constructor taking the configuration struct", "configuration"_a);

py::class_<PyZMQPublisherSubscriber, std::shared_ptr<PyZMQPublisherSubscriber>, ISocket>(m, "ZMQPublisherSubscriber")
.def(
py::init<PyZMQCombinedSocketsConfiguration>(), "Constructor taking the configuration struct",
"configuration"_a);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "communication_interfaces_bindings.hpp"

#include <communication_interfaces/exceptions/SocketConfigurationException.hpp>
#include <communication_interfaces/sockets/ISocket.hpp>

#define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x)

using namespace communication_interfaces;

PYBIND11_MODULE(communication_interfaces, m) {
m.doc() = "Python bindings for communication interfaces";

#ifdef MODULE_VERSION_INFO
m.attr("__version__") = MACRO_STRINGIFY(MODULE_VERSION_INFO);
#else
m.attr("__version__") = "dev";
#endif

auto m_sub_ex = m.def_submodule("exceptions", "Submodule for custom communication interfaces exceptions");
py::register_exception<exceptions::SocketConfigurationException>(
m_sub_ex, "SocketConfigurationError", PyExc_RuntimeError);

auto m_sub_sock = m.def_submodule("sockets", "Submodule for communication interfaces sockets");

py::class_<sockets::ISocket, std::shared_ptr<sockets::ISocket>>(m_sub_sock, "ISocket")
.def("open", &sockets::ISocket::open, "Perform configuration steps to open the socket for communication")
.def(
"receive_bytes",
[](sockets::ISocket& socket) -> py::object {
std::string buffer;
auto res = socket.receive_bytes(buffer);
if (res) {
return py::bytes(buffer);
} else {
return py::none();
}
},
"Receive bytes from the socket")
.def("send_bytes", &sockets::ISocket::send_bytes, "Receive bytes from the socket", "buffer"_a)
.def("close", &sockets::ISocket::close, "Perform steps to disconnect and close the socket communication");

bind_tcp(m_sub_sock);
bind_udp(m_sub_sock);
bind_zmq(m_sub_sock);
}
Loading
Loading