roslibpy

repository·main·Indexed 43 days ago

https://github.com/robotwebtools/roslibpy

A Python and IronPython library for interacting with ROS (Robot Operating System) via the rosbridge 2.0 WebSocket protocol. It enables ROS functionality—including topics, services, actionlib, and TF—on platforms without a local ROS installation, such as Windows or macOS. Supports ROS 1 and provides evolving support for ROS 2.

Tokens
13.6K
Snippets
46
Records
60
Agent score
55%

What's inside roslibpy

  1. Overview of roslibpy features

    main

    What is roslibpy?

    roslibpy is a Python ROS Bridge library that allows Python (CPython) and IronPython to interact with ROS via WebSockets. It connects to rosbridge 2.0 and provides essential ROS functionality without requiring a local ROS installation, making it suitable for non-Linux platforms.

    Key Capabilities

    • Topics: Publishing and subscribing to ROS topics.
    • Services: Service calls (client) and service advertisement (server).
    • Parameters: ROS parameter management (get, set, and delete).
    • Meta-information: Accessing ROS API services for system metadata.
    • Actionlib: Interfacing with preemptable tasks via actionlib.
    • TF: TF Client support via the tf2_web_republisher.

    Compatibility

    • ROS 1: Fully supported.
    • ROS 2: Support is currently in progress.
    • Runtimes: Supports Python 3.x and IronPython 2.7.
  2. Overview of roslibpy

    main

    roslibpy is a Python (and IronPython) library designed to interact with ROS (Robot Operating System) via WebSockets. It connects to rosbridge 2.0 to provide essential ROS functionalities including:

    • Publishing topics
    • Subscribing to topics
    • Service calls
    • actionlib
    • TF (Transform Library)

    Key Advantage: Unlike rospy, roslibpy does not require a local ROS installation, making it possible to interact with a robot from platforms other than Linux (e.g., Windows or macOS).

    Compatibility:

    • ROS 1: Fully supported.
    • ROS 2: Support is currently in progress.
    • API Design: The API is modeled to closely match roslibjs.
  3. Automatic reconnection in roslibpy

    main
    If rosbridge is unresponsive during startup or if an established connection drops unexpectedly, roslibpy will automatically attempt to reconnect. It will also automatically reconnect any active subscriber and publisher topics. Reconnection attempts use an exponential back-off strategy.
  4. Control the ROS event loop with run() and run_forever()

    main

    The roslibpy.Ros class provides two ways to manage the event loop:

    1. run(): Starts the event processing in a separate background thread. This is useful for non-blocking integration into existing applications.
    2. run_forever(): Blocks the calling thread to run the event loop in the foreground. This is useful when you want to manage the main loop explicitly. When using run_forever(), you must set up all connection handlers (like on_ready) before calling it.

    Use on_ready to register a callback that executes once the connection is established.

    import roslibpy
    
    ros = roslibpy.Ros(host='localhost', port=9090)
    
    def on_ready_callback():
        print("Connected!")
    
    ros.on_ready(on_ready_callback)
    ros.run_forever()
  5. Work with ROS Actions (Clients and Servers)

    main

    Actions are designed for long-running tasks (like navigation) because they are non-blocking and support cancellation (preemption). roslibpy supports both consuming actions and providing them.

    • Action Server: Uses roslibpy.actionlib.SimpleActionServer to provide an action service.
    • Action Client: Consumes an action. You can use roslibpy.actionlib.Goal.wait for simple synchronous-style waiting, or hook into the result event with a callback for robust asynchronous handling.
    • ROS 2 Support: roslibpy also provides clients specifically for dealing with ROS 2 action servers.
  6. Communicate using ROS Topics (Talker and Listener)

    main

    ROS Topics allow nodes to communicate via a publish/subscribe model.

    • Talker: A node that publishes messages to a specific topic in a loop.
    • Listener: A node that subscribes to a topic and reacts to incoming messages.

    These can run in separate processes or even on different computers, provided they connect to the same ROS instance.

  7. Establish a first connection to ROS

    main

    To connect to a ROS environment, import roslibpy and initialize a Ros instance with the host and port of your rosbridge instance. By default, host is 'localhost'. Use .run() to start the connection in a background thread.

    Note: If ROS is running on a different machine, replace 'localhost' with the IP address of the ROS instance.

    import roslibpy
    
    ros = roslibpy.Ros(host='localhost', port=9090)
    ros.run()
    
    print(ros.is_connected)  # Should return True
  8. Install roslibpy via pip

    main

    To install roslibpy for CPython, use the standard pip command. Note that you must have a working ROS setup with a rosbridge server and TF2 web republisher accessible on your network for the library to function.

    For IronPython users, the installation command requires specific flags to ensure correct environment handling.

    pip install roslibpy
  9. Use ROS Services to call and create services

    main

    Services provide a request/response communication pattern.

    Calling an existing service

    To call a service, you must know the service name and the request type definition. For example, calling get_loggers:

    # Example logic for calling a service
    service = roslibpy.Service(ros, '/rosout/get_loggers', 'roscpp/GetLoggers')
    # ... call service with request ...

    Creating a new service

    You can create new services if the service type definition is present in your ROS environment (e.g., std_srvs/SetBool).

  10. Select a WebSocket transport

    main

    By default, CPython uses the twisted transport and IronPython uses cli. You can switch to the asyncio transport, which runs the Autobahn WebSocket stack on an asyncio event loop without requiring extra dependencies.

    You can select the transport in three ways:

    1. Per client instance: Pass the transport argument to the Ros constructor.
    2. Process-wide default: Use roslibpy.set_default_transport().
    3. Environment variable: Set ROSLIBPY_TRANSPORT before running your script.
    # Per client
    roslibpy.Ros(host='localhost', port=9090, transport='asyncio')
    
    # Process-wide default
    roslibpy.set_default_transport('asyncio')