Git Product home page Git Product logo

Comments (3)

InvincibleRMC avatar InvincibleRMC commented on June 29, 2024

I believe message_type = should be String not String()

from rclpy.

fujitatomoya avatar fujitatomoya commented on June 29, 2024

serialized message seems to be wrong, the following works w/o any problem.

def main():
    msg = String()
    msg.data = 'Hello, world!'
    msg_serialized = serialize_message(msg)
    print(msg_serialized)
    msg_deserialized = deserialize_message(msg_serialized, String)
    print(msg_deserialized)

the output is,

b'\x00\x01\x00\x00\x0e\x00\x00\x00Hello, world!\x00'
std_msgs.msg.String(data='Hello, world!')

from rclpy.

EnzoGhisoni avatar EnzoGhisoni commented on June 29, 2024

I have tried what you suggest @fujitatomoya and I'm able to successfully decode the string, the issue is that my data come from foxglove_bridge and the encoding seems to be a bit different.

To give a bit more context, I try to create a ws-client to subscribe to a topic with a foxglove_bridge (https://github.com/foxglove/ros-foxglove-bridge), so I have:
ROS2 string publisher -> foxglove_bridge-> ws-client
The data copy pasted in the question is the string received from the foxglove_bridge.

Here is the complete python script of the ws-client:

import asyncio
import websockets
import json

from std_msgs.msg import String
from rclpy.serialization import deserialize_message

from mcap.reader import make_reader
from mcap_ros2.reader import read_ros2_messages
from mcap_ros2.decoder import DecoderFactory

async def handle_event(websocket, message, subscribed):
    # Handle different types of events here
    print("Received event:", message)
    
    if subscribed == False:
        
        subscribed = True
        subsriber = {
            "op": "subscribe",
            "subscriptions": [
                {"id": 3, "channelId": 3} # replace by the selected channel id the subscribe
            ]
        }
        await websocket.send(json.dumps(subsriber))
    
    if type(message) == bytes:
        print("Start to deserialize")
        message_type = String()

        # Decode the message here

        #decoded_data = deserialize_message(serialized_message=message, message_type=message_type)
        
        # print(decoded_data)

async def connect_to_server():
    uri = "ws://0.0.0.0:9090"  # Change this to your server URI
    subscribed = False
    async with websockets.connect(uri, subprotocols=["foxglove.websocket.v1"]) as websocket:
        print("Connected to server.")
        try:
            while True:
                message = await websocket.recv()
                
                await handle_event(websocket, message, subscribed)
                subscribed = True
        except websockets.ConnectionClosed:
            print("Connection to server closed.")

async def main():
    await connect_to_server()

if __name__ == "__main__":
    asyncio.run(main())

from rclpy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.