Skip to content

User_App_DotNet_Intro_RosBridgeClient

Mehmet Emre Çakal edited this page Oct 10, 2024 · 2 revisions

4.1 Introduction to RosBridgeClient

The RosBridgeClient is a powerful .NET library that enables seamless communication between your .NET application and the ROS ecosystem. With ROSBridgeClient, you can easily publish and subscribe to ROS topics, handle service calls, and respond to service requests.

This guide will walk you through the essential steps to get started with the ROSBridgeClient library. You will learn how to set up the ROSBridge server, establish a ROS socket connection, publish and subscribe to topics, handle service calls, and respond to service requests.

Table of Contents

4.1.1 Setting Up the ROSBridge Server 4.1.2 Creating a ROS Socket 4.1.3 Publishing a Topic 4.1.4 Subscribing to a Topic 4.1.5 Handling Service Calls 4.1.6 Responding to Service Requests 4.1.7 Cleaning Up

4.1.1 Setting Up the RosBridge Server

Ensure the RosBridge server is running on the ROS machine. This allows your .NET application to interact with ROS topics and services. Refer to the 1. Installation and Configuration section for specific instructions.

4.1.2 Creating a ROS Socket

The first step in your application is to establish a connection with the RosBridge server using the RosSocket class. This socket will serve as the communication bridge between your .NET application and the ROS ecosystem.

Code Example:

using System;
using RosSharp.RosBridgeClient;

namespace RosSharpExample
{
    public class Program
    {
        private static readonly string uri = "ws://localhost:9090";

        public static void Main(string[] args)
        {
            // Establish a connection to the RosBridge server
            RosSocket rosSocket = new RosSocket(new RosBridgeClient.Protocols.WebSocketNetProtocol(uri));

            // Add your publishing/subscribing logic here

            // Close the connection when done
            rosSocket.Close();
        }
    }
}
  • URI: This string represents the WebSocket address of the RosBridge server. Adjust the localhost to the appropriate IP if connecting to a remote ROS system.

4.1.3 Publishing a Topic

To publish a message to a ROS topic, you first need to advertise the topic, then create a message of the appropriate type, and finally publish that message.

Code Example:

using std_msgs = RosSharp.RosBridgeClient.MessageTypes.Std;

public static void PublishExample(RosSocket rosSocket)
{
    // Create a message object
    std_msgs.String message = new std_msgs.String
    {
        data = "Hello ROS from .NET!"
    };

    // Advertise the topic
    string publication_id = rosSocket.Advertise<std_msgs.String>("<topic_name>");

    // Publish the message
    rosSocket.Publish(publication_id, message);

    Console.WriteLine("Published message: " + message.data);
}
  • std_msgs.String: This is a predefined message type in ROS# corresponding to ROS's std_msgs/String.
  • Advertise: Registers the topic with ROS, specifying the type of messages it will carry.

4.1.4 Subscribing to a Topic

Subscribing to a ROS topic allows your application to receive messages whenever they are published on that topic. This is particularly useful for listening to sensor data or receiving updates from other nodes.

Steps:

  1. Subscribe to the Topic: Register a callback method that will handle incoming messages.
  2. Define a Handler: Create a method that processes the received messages.

Code Example:

public static void SubscribeExample(RosSocket rosSocket)
{
    // Subscribe to a topic
    string subscription_id = rosSocket.Subscribe<std_msgs.String>("/<incoming_topic_name>", SubscriptionHandler);
}

// Handler method for incoming messages
private static void SubscriptionHandler(std_msgs.String message)
{
    Console.WriteLine("Received message: " + message.data);
}
  • SubscriptionHandler: This callback method processes each incoming message.

4.1.5 Handling Service Calls

ROS services allow you to perform request/response communication between nodes. In ROS#, you can call a service and process the response using the CallService method.

Steps:

  1. Call the Service: Specify the service name, the request type, and a callback to handle the response.
  2. Handle the Response: Define the callback method to process the response.

Code Example:

using rosapi = RosSharp.RosBridgeClient.MessageTypes.Rosapi;

public static void CallServiceExample(RosSocket rosSocket)
{
    // Call a service and handle the response
    rosSocket.CallService<rosapi.GetParamRequest, rosapi.GetParamResponse>(
        "/rosapi/get_param", 
        ServiceCallHandler, 
        new rosapi.GetParamRequest("/rosdistro", "default_value") // Just "default" for ROS1
    ); 
}

// Callback for handling service response
private static void ServiceCallHandler(rosapi.GetParamResponse message)
{
    Console.WriteLine("Received ROS distro: " + message.value);
}
  • rosapi.GetParamRequest: Represents the request message for the get_param service in ROS.
  • ServiceCallHandler: A method that processes the service response.

4.1.6 Responding to Service Requests

Sometimes, your .NET application may need to act as a service provider. This involves responding to service requests from other ROS nodes.

Steps:

  1. Advertise the Service: Notify ROS that your application can handle a specific service request.
  2. Define the Service Handler: Implement the logic that processes incoming service requests and sends back a response.

Code Example:

public static void AdvertiseServiceExample(RosSocket rosSocket)
{
    // Advertise a service
    string service_id = rosSocket.AdvertiseService<rosapi.GetParamRequest, rosapi.GetParamResponse>(
        "/my_service", 
        ServiceHandler
    );
}

// Handler method for service requests
private static bool ServiceHandler(rosapi.GetParamRequest request, out rosapi.GetParamResponse response)
{
    Console.WriteLine("Received request: " + request.name);
    
    // Process request and create response
    response = new rosapi.GetParamResponse
    {
        value = "response_value"
    };

    return true; // Indicates successful handling
}
  • AdvertiseService: Registers your service with ROS, allowing it to receive and respond to requests.
  • ServiceHandler: Processes incoming requests and generates appropriate responses.

4.1.7 Cleaning Up: Closing Connections and Resource Management

  1. Unadvertise Topics: Unregister any topics your application was publishing to.
  2. Unsubscribe from Topics: Unregister any subscriptions.
  3. Unadvertise Services: Unregister any services.
  4. Close the ROS Socket: Gracefully close the WebSocket connection to the ROSBridge server.

Code Example:

public static void CleanUp(RosSocket rosSocket, string publicationId, string subscriptionId)
{
    // Unadvertise the topic
    rosSocket.Unadvertise(publicationId);

    // Unsubscribe from the topic
    rosSocket.Unsubscribe(subscriptionId);

    // Unadvertise the service    
    rosSocket.UnadvertiseService(serviceId);

    // Close the ROS socket connection
    rosSocket.Close();

    Console.WriteLine("Cleaned up ROS connections");
}

4.1.8 Example Files

In the ROS# .NET solution you will see the RosBridgeClientTest project. For executable implementation examples, see the console examples in this project.

Next tutorial: Image Publication


© Siemens AG, 2017-2024

Clone this wiki locally