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

Add a zcam-rs and float32-data demos to work with rs and ROS2 #23

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ dashboard. Three subscriber frontends are provided: a Python script using
[Matplotlib](https://matplotlib.org/), a browser client and a
[Freeboard](https://freeboard.github.io) configuation.

**computer-vision/zcam/zcam-{python,rust,rest}**: consists of two Zenoh nodes: a
**computer-vision/zcam/zcam-{python,rust,rest,rs-python}**: consists of two Zenoh nodes: a
publisher capturing a camera video stream and a subscriber displaying said video
stream. Both of the Python and Rust implementations use
[OpenCV](https://opencv.org/) to encode and decode data.
Expand Down Expand Up @@ -97,6 +97,10 @@ obstacles and environment boundaries (e.g. walls of a room).
teleoperation messages by reading keyboard input (i.e. arrow keys). Both of the
Rust and Python implementations are terminal applications.

**ROS2/zenoh-python-float32-data**: Zenoh nodes that publish
[Float32](https://docs.ros.org/en/noetic/api/std_msgs/html/msg/Float32.html)
Float32 message might be used to publish real-time sensor data such as temperature readings, velocity, or any other numerical value.

**zenoh-home/{light,soil,temp-humi}-sensor**: Zenoh nodes running Zenoh
[Pico](https://github.com/eclipse-zenoh/zenoh-pico) on an
[ESP32](https://en.wikipedia.org/wiki/ESP32) board which publish sensor data
Expand Down
31 changes: 31 additions & 0 deletions ROS2/zenoh-python-float32-data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# A zenoh Python Float32 data for ROS2

## **Requirements**

* Python 3.6 minimum
* A [zenoh router](http://zenoh.io/docs/getting-started/quick-test/)
* The [zenoh/DDS bridge](https://github.com/eclipse-zenoh/zenoh-plugin-dds#trying-it-out)
* [zenoh-python](https://github.com/eclipse-zenoh/zenoh-python): install it with `pip install eclipse-zenoh`.
* [pycdr2](https://pypi.org/project/pycdr2/): install it with `pip install pycdr2`.

-----

## **Usage**

1. Start the zenoh/DDS bridge:

```bash
zenoh-bridge-dds
```

2. Start Ros2Float32

```bash
python ros2-float32-data.py
```

3. Start Rviz2 and Setting Plotter2D

```bash
python ros2-float32-data.py
```
103 changes: 103 additions & 0 deletions ROS2/zenoh-python-float32-data/ros2-float32-data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#
# Copyright (c) 2024 ZettaScale Technology Inc.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# The Zenoh Team, <[email protected]>
#

import argparse
import zenoh
import random
import time
import json

from dataclasses import dataclass
from pycdr2 import IdlStruct
from pycdr2.types import int32, uint32, float32


@dataclass
class Time(IdlStruct, typename="Time"):
sec: int32
nanosec: uint32

@dataclass
class Float32(IdlStruct, typename="std_msgs/msg/Float32"):
data: float32


def main():
# --- Command line argument parsing ---
parser = argparse.ArgumentParser(
prog='random_float32_publisher',
description='zenoh random Float32 publisher example')
parser.add_argument('--mode', '-m', dest='mode',
choices=['peer', 'client'],
type=str,
help='The zenoh session mode.')
parser.add_argument('--connect', '-e', dest='connect',
metavar='ENDPOINT',
action='append',
type=str,
help='zenoh endpoints to connect to.')
parser.add_argument('--listen', '-l', dest='listen',
metavar='ENDPOINT',
action='append',
type=str,
help='zenoh endpoints to listen on.')
parser.add_argument('--config', '-c', dest='config',
metavar='FILE',
type=str,
help='A configuration file.')
parser.add_argument('--topic', '-t', dest='topic',
default='random_float32',
type=str,
help='The topic to publish Float32 data.')
parser.add_argument('--float32_data', '-f', dest='float32_data',
default='10.0',
type=float,
help='The float32_data of publishing.')

args = parser.parse_args()
conf = zenoh.Config.from_file(args.config) if args.config is not None else zenoh.Config()
if args.mode is not None:
conf.insert_json5(zenoh.config.MODE_KEY, json.dumps(args.mode))
if args.connect is not None:
conf.insert_json5(zenoh.config.CONNECT_KEY, json.dumps(args.connect))
if args.listen is not None:
conf.insert_json5(zenoh.config.LISTEN_KEY, json.dumps(args.listen))
topic = args.topic
float32_data = args.float32_data

# zenoh-net session initialization
print("Opening session...")
session = zenoh.open(conf)

print(f"Publishing random Float32 data on topic '{topic}' at {float32_data}.")

def pub_float32(data):
print("Pub float32: {}".format(data))
t = Float32(data=float(data))
session.put(topic, t.serialize())

try:
interval = 1.0 / float32_data
while True:
random_value = random.uniform(-10, 10.0) # Generate random float between -100.0 and 100.0
pub_float32(random_value)
print(f"Published: {random_value} to topic '{topic}'")
time.sleep(interval)
except KeyboardInterrupt:
print("Shutting down...")
finally:
session.close()

if __name__ == "__main__":
main()
27 changes: 23 additions & 4 deletions ROS2/zenoh-rest-teleop/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,28 @@ via the zenoh REST API, bridged to ROS2.
* Keep pressing the arrow buttons (or arrows on your keyboard) to publish Twist messages (a STOP Twist with 0 speed is published when released).
* All the messages published on "rosout" via ROS2 will be displayed in the bottom box.

### ros2-tb3-teleop.html

⚠️ Note: this demo depends on the Ros2 Middleware implementation being set to CycloneDDS
Installation instructions for CycloneDDS below:
https://docs.ros.org/en/humble/Installation/DDS-Implementations/Working-with-Eclipse-CycloneDDS.html
Once installed the middleware env var must be set :
A simple teleop web page allowing to publish Twists and to subscribe to Logs
via the zenoh REST API, bridged to ROS2 for Turtlebot3 on Gazebo.

1. Start the Turtlebot3 launch file:
Please clone the turtlebot3_simulations package beforehand.

```bash
RMW_IMPLEMENTATION=rmw_cyclonedds_cpp ros2 launch turtlebot3_gazebo turtlebot3_wolrd.launch.py
```

2. Start the zenoh/DDS bridge, activating its REST API:

```bash
zenoh-bridge-ros2dds --rest-http-port 8000
```

3. Open the `ros2-tb3-teleop.html` file in a Web browser

⚠️ Note: this demo depends on the Ros2 Middleware implementation being set to CycloneDDS
Installation instructions for CycloneDDS below:
https://docs.ros.org/en/humble/Installation/DDS-Implementations/Working-with-Eclipse-CycloneDDS.html
Once installed the middleware env var must be set :
`RMW_IMPLEMENTATION=rmw_cyclonedds_cpp`
Loading