-
Notifications
You must be signed in to change notification settings - Fork 371
Dev_PreProcDir
Preprocessor directives are used mainly for ROS version dependent behavior, i.e. when it is not possible or practical to dynamically change class behavior at runtime, the solution is built specifically for each ROS version. For more information about C# Preprocessor Directives, see the official .NET page.
In the .NET solution, you can see preprocessor directives in each class under the RosSharp.RosBridgeClient.MessageTypes
namespace, as they are all automatically generated via MessageGeneration
. For example, see the Header.cs
message type in the RosBridgeClient project:
Runtime\Libraries\RosBridgeClient\MessageTypes\ROS1\Std\msg\Header.cs
:
#if !ROS2
namespace RosSharp.RosBridgeClient.MessageTypes.Std
{
...
public Header()
{
this.seq = 0;
this.stamp = new Time();
this.frame_id = "";
}
...
}
#endif
And compare it with:
Runtime\Libraries\RosBridgeClient\MessageTypes\ROS2\Std\msg\Header.cs
:
#if ROS2
using RosSharp.RosBridgeClient.MessageTypes.BuiltinInterfaces;
namespace RosSharp.RosBridgeClient.MessageTypes.Std
{
...
public Header()
{
this.stamp = new Time();
this.frame_id = "";
}
...
}
#endif
Both versions differ in their constructor, and additionally the ROS2 version depends on a different namespace which does not exist in ROS1. And considering the nature of ROS, we want both to be Std\msg\Header.cs
at the same time, while accepting their differences, so they are compiled separately. In Visual Studio it is possible to configure a batch build and compile both versions at the same time (see the image below), but that's not the case for Unity.
In Unity, this compilation is performed only once, for the version selected from the drop-down menu located in the RosConnector component. When a version change is triggered (manually or via OnInspectionGUI), the ROS2 define symbol (#define ROS2)
is added to the EditorUserBuildSettings.selectedBuildTargetGroup
via PlayerSettings.SetScriptingDefineSymbolsForGroup
. Finally, the assembly builder is triggered with AssetDatabase.Refresh()
. Of course, all necessary scripts will be recompiled and this process takes quite a long time.
com.siemens.ros-sharp\Editor\RosBridgeClient\RosConnectorEditor.cs
:
// Toggle ROS Version
public static void ToggleROSVersion(RosVersion selectedROSVersion)
{
string defineSymbolROS2 = "ROS2";
BuildTargetGroup targetGroup = EditorUserBuildSettings.selectedBuildTargetGroup;
string defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup);
// Remove ROS2 define symbol
defines = defines.Replace($"{defineSymbolROS2};", "").Replace(defineSymbolROS2, "");
// Add the define symbol for the selected ROS version (ROS2 is default if not present)
string defineSymbol = selectedROSVersion == RosVersion.ROS1 ? "" : defineSymbolROS2;
if (!string.IsNullOrEmpty(defineSymbol) && !defines.Contains(defineSymbol))
{
defines += $";{defineSymbol}";
}
// Set scripting define symbols
PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, defines);
// Execute Assembly Builder
AssetDatabase.Refresh();
}
Why not build both in the first place and dynamically link like in .NET? Well, because Unity does not provide the latest stable versions of our dependencies, and we have to follow what they provide. This means that our DLLs would ask for different versions (e.g. System.Text.Json.dll) and throw errors unless we downgrade our dependencies. Yes, in theory it is possible to modify the Unity installation and use our preferred DLLs. In practice, however, this approach is neither very user-friendly nor very secure. So Unity compiles our scripts the way it wants, using its own built-in DLLs, and we do not deliberately try to downgrade ROS# just to follow Unity's old packages.
© Siemens AG, 2017-2024
-
- 1.3.1 R2D2 Setup
- 1.3.2 Gazebo Setup on VM
- 1.3.3 TurtleBot Setup (Optional for ROS2)
-
- 2.1 Quick Start
- 2.2 Transfer a URDF from ROS to Unity
- 2.3 Transfer a URDF from Unity to ROS
- 2.4 Unity Simulation Scene Example
- 2.5 Gazebo Simulation Scene Example
- 2.6 Fibonacci Action Client
- 2.7 Fibonacci Action Server
- 3.1 Import a URDF on Windows
- 3.2 Create, Modify and Export a URDF Model
- 3.3 Animate a Robot Model in Unity
- Message Handling: Readers & Writers
- Thread Safety for Message Reception
- File Server Package
- ROS-Unity Coordinate System Conversions
- Post Build Events
- Preprocessor Directives in ROS#
- Adding New Message Types
- RosBridgeClient Protocols
- RosBridgeClient Serializers
- Action Server State Machine Model
© Siemens AG, 2017-2024