Skip to content

MrShasha/ros-sharp

 
 

Repository files navigation

Author of this fork: Jiří Šašek, mrsasekjiri@gmail.com

Purpose of this fork

** Solved problem ** : While working on my project I needed to subscribe to such custom ROS messages, that did not have their C# class counterparts in the RosSharp library. One way would be to simply add these new C# classes to the RosSharp library manually, but this would require me to update this external library every time I need a support for new message type. Since this need is quite common in robotics projects, I decided to fork the RosSharp repository and add a feature that would allow to subscribe to a topic and receive messages of any type, without having to serialize them to a C# class for this type in the RosSharp library while only using json to transfer the information.

** How to use**: To subscribe to a topic with custom message type, use the new SubscribeWithMessageTypeJObject method of the RosSocket class. You can call it from your code for example like this:

  public Task<string> SubscribeDynamicAsync(string topic, string messageType, Action<object> callback)
{
    if (!IsConnected)
    {
        _logger.LogError("Not connected.");
        return Task.FromResult(string.Empty);
    }

    try
    {
        _logger.LogInformation("Creating dynamic subscription for topic '{Topic}' with type '{MessageType}'",
            topic, messageType);

        // Use new RosSharp method for raw JObject delivery
        string id = _rosSocket!.SubscribeWithMessageTypeJObject(
            topic,
            messageType,
            (Newtonsoft.Json.Linq.JObject jObj) =>
            {
                _uiDispatcher.InvokeAsync(() =>
                {
                    try
                    {
                        callback(jObj);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "Error processing dynamic JObject message callback");
                    }
                });
            }
        );

        if (!string.IsNullOrEmpty(id))
        {
            Interlocked.Increment(ref _activeSubscriptions);
            UpdateSubscriptionState();
            _logger.LogInformation("Dynamic subscription successful: Topic='{Topic}', Type='{MessageType}', ID='{SubId}'",
                topic, messageType, id);
        }
        else
        {
            _logger.LogError("Dynamic subscription failed - empty ID returned");
        }

        return Task.FromResult(id);
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, "Failed to create dynamic subscription for topic '{Topic}'", topic);
        return Task.FromResult(string.Empty);
    }
}

Original README:

ROS#

Overview

ROS# is a set of open-source software libraries and tools in C# for communicating with ROS from .NET applications, in particular Unity. With ROS#, developers can effortlessly create .NET applications that communicate with ROS nodes, subscribe to and publish topics, handle actions and services, and interact with ROS messages. This enables the development of robotics applications, simulations, and automation systems within the .NET ecosystem.

ROS# Architecture

Here are some showcases illustrating what can be done with ROS#. The community provided various other application examples for ROS# here.

ROS# Showcase

Installation

ROS# can be used with Unity Engine and/or with any compatible .NET project, see platform support and external dependencies.

For more installation options, detailed instractions, and getting started see wiki: Installing and Configuring ROS# for Unity.

  • For .NET Projects: NuGet Gallery

    1. Head to the NuGet page.
    2. Install the required packages individually from NuGet.

For more installation options, detailed instractions, and getting started see wiki: Installing and Configuring ROS# for .NET.

Platform Support

  • The ROS# dependencies require .NET 8 or .NET Standard 2.1 and Visual Studio 2022 or higher.
  • The Unity package has been developed with Unity 2022.3.17f1 (LTS) and should also be compatible with older versions. Unity 6 support has not yet been thoroughly tested. However, some community members were able to use ROS# with Unity 6.

For Unity versions below 2022.3: See wiki.

Repository Structure

Below is an overview of the main directories and their purposes:

  • com.siemens.ros-sharp/: Contains the custom Unity UPM package for integrating ROS# into Unity projects. The package includes the whole ROS# .NET solution, as well as Unity specific scripts, external dependencies, and samples. This folder follows a planned layout as Unity recommends.

    • Runtime/: Core ROS# .NET scripts and components for Unity integration.
    • Editor/: Unity specific editor scripts for mainly UI extensions.
    • Plugins/: External dependencies with specific versions.
    • Samples~/: Example samples that need to be imported through the package manager window. For more info about example scenes and reference code, see wiki.
  • Libraries/: .NET solution containing the core ROS# libraries and tools for communicating with ROS.

    • RosBridgeClient/: Core ROS# .NET library for communicating with ROS via websockets.
    • MessageGeneration/: Tools for generating C# classes from ROS message definitions, including messages, actions, and services with both ROS1/2 support.
    • Urdf/: Library for parsing URDF files and creating Unity GameObjects.
    • PostBuildEvents/: OS specific post build scripts. Please see post build events for more information.
  • ROS Packages/: ROS packages that are used by ROS# for tutorial, testing and demonstration purposes.

Further Info


© Siemens AG, 2017-2025

About

ROS# is a set of open source software libraries and tools in C# for communicating with ROS from .NET applications, in particular Unity3D

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • C# 97.2%
  • Python 1.5%
  • Other 1.3%