0
$\begingroup$

Where can I find a good example of a .NET application running in Windows communicating over ROS2 and how to make it happen? Some kind of unassuming tutorial?

I'm ultimately trying to get a Blazor app communicating over ROS2 with a console app, both of which are written in C# in .NET 8. I have installed Humble on my Windows 11 and after sourcing my code with:

call C:\dev\ros2_humble\local_setup.bat

I could run the following in admin cmd windows and watch them talk over the /chatter topic:

ros2 run demo_nodes_cpp talker
ros2 run demo_nodes_py listener

But that's the best I've been able to do so far. I could not get the "performance" talker/listener to run since it can't find the ros2cs_examples package--does my local_setup.bat need a tweak? I've hit up the Git repos for ros2_dotnet, ros2cs, and rclnet, but it's so unclear to me how to set it up to the point where I can bring in some ROS2 class into my C# code to send or receive messages. Many of the Visual Studio solution files that came with ros2cs have no actual, buildable code in them. Which DLLs should I import into my projects as references? I got the Rcl.NET NuGet package, and that let me use Rosidl.Runtime.Interop in my razor file, but it seems to only have "Sequence" structs. The documentation on rclnet is the most comprehensive but is still full of holes ("You can add the following line to somewhere in the source code of the project: [assembly: System.Runtime.CompilerServices.DisableRuntimeMarshalling]" Somewhere like WHERE?!).

I really am quite the ROS2 beginner. I did complete an online video course in ROS2, but am I missing some fundamentals about just how it works? If so, where's a good place to read up? After 3 unproductive days I sure could use a break, although the fact that I couldn't even make ".NET" or "C#" tags for this post leaves me with little hope. Thanks in advance from this dummy...

EDIT: I could also use a tip on which DDS implementation can be used in Windows besides RTI Connext, which is not free.

$\endgroup$

1 Answer 1

3
$\begingroup$

Apologies for the poor documentation from the author of rclnet.

Allow me to introduce some basic concepts about rclnet to you. First of all, rclnet, like rclpy for Python and rclcpp for C++, is a .NET wrapper of the low level ROS 2 client library known as rcl. Writing a node using rclnet is much like using rclpy or rclcpp, except that rclnet uses dotnet tooling, where as rclpy and rclcpp uses CMake and colcon. This means that developing with rclnet feels more dotnet-ish, rather than ROS-ish, which should be much easier for .NET developers.

No ROS 2 tooling implies that you can't have ROS 2 package dependencies when using rclnet. So how do you gain access to ROS 2 interfaces (messages, services and actions), given that interfaces are usually distributed as packages? This is where ros2cs (from rclnet) comes into play. ros2cs is a tool for generating P/Invoke codes which allows you to access ROS 2 interfaces as C# classes as structs.

ros2cs works on message files (not IDLs) to perform code generation. Message files are usually found in installed ROS 2 interface packages. ros2cs can locate interface packages by searching in the ament index inside ROS 2 distribution or custom overlay. This requires you to setup ROS 2 environment before running ros2cs:

call path_to_your_ros_distro\setup.bat
ros2cs path_to_your_ros2cs.spec

You can also instruct ros2cs to generate codes for interface packages from outside of ament index by specifying --from-directory option. This is especially useful when you want to generate messages and build nodes without installing ROS 2.

rclnet also provides project templates for nodes and message libraries which should provide a starting point for you. You can follow the instructions in this section.

Building rclnet nodes is as simple as just running dotnet build. Running ROS 2 nodes requires some environment setup. This can be done by calling setup scripts provided by the ROS 2 distribution (or your custom overlay). On Windows, it's setup.bat or local_setup.bat. Running rclnet nodes also have the same requirement, so to start a node you would usually do:

call path_to_your_ros_distro\setup.bat
dotnet run --project my_rclnet_node_project

It's a bit more trickier when it comes to debugging because your IDE is usually launched without setting up ROS 2 environment. In that case you'll end up with a DllNotFoundException and your node won't be able to launch. This can be worked around by manually setting up the following environment variables in launchSettings.json:

  • ROS_DISTRO
  • ROS_PYTHON_VERSION
  • ROS_VERSION
  • LD_LIBRARY_PATH (Linux) or PATH (Windows)
  • AMENT_PREFIX_PATH

You can inspect these environment variables by using the following command:

call path_to_your_ros_distro\setup.bat
set

and fill the values into launchSettings.json.

$\endgroup$
2
  • 1
    $\begingroup$ Nice first answer! $\endgroup$ Commented May 11 at 13:26
  • $\begingroup$ This level of detail is just what I needed. I set the variables in launchSettings.json to match my machine's environment variables after the Humble local_setup.bat ran, and I was finally able to launch the publisher node from Visual Studio. I built in release mode and executed that in a command window after running the .BAT, and got that node to subscribe and display the published messages. Thanks so much! Now I just need to figure out how to make my own message types... $\endgroup$ Commented May 15 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.