Class ROS

Connects to a ROS bridge server (rosbridge_suite) and either publishes or subscribes to a ROS topic over WebSocket (port 9090).

JSON configuration

Use these block entries inside a pipeline configuration. Input names must match other blocks in that pipeline; device addresses and file paths must match your setup.

"ros_in": { "Type": "RosBlock", "Inputs": [],
  "Params": [ "192.168.1.100", "subscribe", "emg_data", "std_msgs/Float64MultiArray", "32" ] }
"ros_out": { "Type": "RosBlock", "Inputs": [ "processed" ],
  "Params": [ "192.168.1.100", "publish", "prediction", "std_msgs/Float64MultiArray", "5" ] }

How Type, Inputs and Params work · Block catalogue and parameter reference

Namespace: MOSAIC.Models.Streaming
Assembly: MOSAIC.dll
Syntax
public sealed class ROS : BaseBlock, INotifyPropertyChanged, INotifyPropertyChanging, ISubscriber, IPublisher, IBaseBlockProps, IJsonExportable, IDisposable, IAsyncDisposable
Remarks

Ensure the ROS bridge is running before starting MOSAIC:

ROS2: ros2 launch rosbridge_server rosbridge_websocket_launch.xml
ROS1: roslaunch rosbridge_server rosbridge_websocket.launch

Pipeline JSON example (subscribe):

"ros_in": { "Type": "RosBlock", "Inputs": [],
  "Params": [ "192.168.1.100", "subscribe", "emg_data", "std_msgs/Float64MultiArray", "32" ] }
Pipeline JSON example (publish):
"ros_out": { "Type": "RosBlock", "Inputs": [ "processed" ],
  "Params": [ "192.168.1.100", "publish", "prediction", "std_msgs/Float64MultiArray", "5" ] }

Thread safety: WebSocket receive loop runs on a background task. All UI-affecting property changes fire PropertyChanged which the ViewModel marshals to the UI thread.

Constructors

ROS(string, double, string, string, string, string, int)

Declaration
public ROS(string name, double desiredRate, string ipAddress, string action, string topic, string messageType, int numChannels = 1)
Parameters
Type Name Description
string name
double desiredRate
string ipAddress
string action
string topic
string messageType
int numChannels

Fields

_action

Declaration
[ObservableProperty]
private string _action
Field Value
Type Description
string

_connectionStatus

Declaration
[ObservableProperty]
private string _connectionStatus
Field Value
Type Description
string

_cts

Declaration
private CancellationTokenSource _cts
Field Value
Type Description
CancellationTokenSource

_ipAddress

Declaration
[ObservableProperty]
private string _ipAddress
Field Value
Type Description
string

_isConnected

Declaration
[ObservableProperty]
private bool _isConnected
Field Value
Type Description
bool

_messageCount

Declaration
[ObservableProperty]
private long _messageCount
Field Value
Type Description
long

_messageType

Declaration
[ObservableProperty]
private string _messageType
Field Value
Type Description
string

_messagesReceived

Declaration
private long _messagesReceived
Field Value
Type Description
long

_messagesSent

Declaration
private long _messagesSent
Field Value
Type Description
long

_numChannels

Declaration
[ObservableProperty]
private int _numChannels
Field Value
Type Description
int

_topic

Declaration
[ObservableProperty]
private string _topic
Field Value
Type Description
string

_ws

Declaration
private ClientWebSocket? _ws
Field Value
Type Description
ClientWebSocket

Properties

Action

Declaration
[ExcludeFromCodeCoverage]
public string Action { get; set; }
Property Value
Type Description
string

ConnectionStatus

Declaration
[ExcludeFromCodeCoverage]
public string ConnectionStatus { get; set; }
Property Value
Type Description
string

IpAddress

Declaration
[ExcludeFromCodeCoverage]
public string IpAddress { get; set; }
Property Value
Type Description
string

IsConnected

Declaration
[ExcludeFromCodeCoverage]
public bool IsConnected { get; set; }
Property Value
Type Description
bool

IsPublisher

Declaration
public bool IsPublisher { get; }
Property Value
Type Description
bool

IsSubscriber

Declaration
public bool IsSubscriber { get; }
Property Value
Type Description
bool

JsonTypeName

Block type identifier for JSON serialization. Override for a custom name.

Declaration
protected override string JsonTypeName { get; }
Property Value
Type Description
string
Overrides
BaseBlock.JsonTypeName

MaxInputs

Source block — produces a stream and takes no inputs.

Declaration
public override int MaxInputs { get; }
Property Value
Type Description
int
Overrides
BaseBlock.MaxInputs

MessageCount

Declaration
[ExcludeFromCodeCoverage]
public long MessageCount { get; set; }
Property Value
Type Description
long

MessageType

Declaration
[ExcludeFromCodeCoverage]
public string MessageType { get; set; }
Property Value
Type Description
string

MinInputs

Source block — produces a stream and takes no inputs.

Declaration
public override int MinInputs { get; }
Property Value
Type Description
int
Overrides
BaseBlock.MinInputs

NumChannels

Declaration
[ExcludeFromCodeCoverage]
public int NumChannels { get; set; }
Property Value
Type Description
int

Topic

Declaration
[ExcludeFromCodeCoverage]
public string Topic { get; set; }
Property Value
Type Description
string

Methods

ConfigureInput(IServiceProvider, JsonModel)

Declaration
public static ROS ConfigureInput(IServiceProvider sp, JsonModel m)
Parameters
Type Name Description
IServiceProvider sp
JsonModel m
Returns
Type Description
ROS

ConnectAsync()

Declaration
private Task ConnectAsync()
Returns
Type Description
Task

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

Declaration
public override void Dispose()
Overrides
BaseBlock.Dispose()

GetJsonParams()

Block-specific parameters for JSON. Override to provide custom parameters.

Declaration
protected override IReadOnlyList<object>? GetJsonParams()
Returns
Type Description
IReadOnlyList<object>

The values for Params, in the order this block's ConfigureInput reads them, or null when the block takes no parameters.

Overrides
BaseBlock.GetJsonParams()

OnActionChanged(string)

Executes the logic for when Action just changed.

Declaration
private void OnActionChanged(string value)
Parameters
Type Name Description
string value

The new property value that was set.

Remarks

This method is invoked right after the value of Action is changed.

OnActionChanged(string?, string)

Executes the logic for when Action just changed.

Declaration
private void OnActionChanged(string? oldValue, string newValue)
Parameters
Type Name Description
string oldValue

The previous property value that was replaced.

string newValue

The new property value that was set.

Remarks

This method is invoked right after the value of Action is changed.

OnActionChanging(string)

Executes the logic for when Action is changing.

Declaration
private void OnActionChanging(string value)
Parameters
Type Name Description
string value

The new property value being set.

Remarks

This method is invoked right before the value of Action is changed.

OnActionChanging(string?, string)

Executes the logic for when Action is changing.

Declaration
private void OnActionChanging(string? oldValue, string newValue)
Parameters
Type Name Description
string oldValue

The previous property value that is being replaced.

string newValue

The new property value being set.

Remarks

This method is invoked right before the value of Action is changed.

OnConnectionStatusChanged(string)

Executes the logic for when ConnectionStatus just changed.

Declaration
private void OnConnectionStatusChanged(string value)
Parameters
Type Name Description
string value

The new property value that was set.

Remarks

This method is invoked right after the value of ConnectionStatus is changed.

OnConnectionStatusChanged(string?, string)

Executes the logic for when ConnectionStatus just changed.

Declaration
private void OnConnectionStatusChanged(string? oldValue, string newValue)
Parameters
Type Name Description
string oldValue

The previous property value that was replaced.

string newValue

The new property value that was set.

Remarks

This method is invoked right after the value of ConnectionStatus is changed.

OnConnectionStatusChanging(string)

Executes the logic for when ConnectionStatus is changing.

Declaration
private void OnConnectionStatusChanging(string value)
Parameters
Type Name Description
string value

The new property value being set.

Remarks

This method is invoked right before the value of ConnectionStatus is changed.

OnConnectionStatusChanging(string?, string)

Executes the logic for when ConnectionStatus is changing.

Declaration
private void OnConnectionStatusChanging(string? oldValue, string newValue)
Parameters
Type Name Description
string oldValue

The previous property value that is being replaced.

string newValue

The new property value being set.

Remarks

This method is invoked right before the value of ConnectionStatus is changed.

OnIpAddressChanged(string)

Executes the logic for when IpAddress just changed.

Declaration
private void OnIpAddressChanged(string value)
Parameters
Type Name Description
string value

The new property value that was set.

Remarks

This method is invoked right after the value of IpAddress is changed.

OnIpAddressChanged(string?, string)

Executes the logic for when IpAddress just changed.

Declaration
private void OnIpAddressChanged(string? oldValue, string newValue)
Parameters
Type Name Description
string oldValue

The previous property value that was replaced.

string newValue

The new property value that was set.

Remarks

This method is invoked right after the value of IpAddress is changed.

OnIpAddressChanging(string)

Executes the logic for when IpAddress is changing.

Declaration
private void OnIpAddressChanging(string value)
Parameters
Type Name Description
string value

The new property value being set.

Remarks

This method is invoked right before the value of IpAddress is changed.

OnIpAddressChanging(string?, string)

Executes the logic for when IpAddress is changing.

Declaration
private void OnIpAddressChanging(string? oldValue, string newValue)
Parameters
Type Name Description
string oldValue

The previous property value that is being replaced.

string newValue

The new property value being set.

Remarks

This method is invoked right before the value of IpAddress is changed.

OnIsConnectedChanged(bool)

Executes the logic for when IsConnected just changed.

Declaration
private void OnIsConnectedChanged(bool value)
Parameters
Type Name Description
bool value

The new property value that was set.

Remarks

This method is invoked right after the value of IsConnected is changed.

OnIsConnectedChanged(bool, bool)

Executes the logic for when IsConnected just changed.

Declaration
private void OnIsConnectedChanged(bool oldValue, bool newValue)
Parameters
Type Name Description
bool oldValue

The previous property value that was replaced.

bool newValue

The new property value that was set.

Remarks

This method is invoked right after the value of IsConnected is changed.

OnIsConnectedChanging(bool)

Executes the logic for when IsConnected is changing.

Declaration
private void OnIsConnectedChanging(bool value)
Parameters
Type Name Description
bool value

The new property value being set.

Remarks

This method is invoked right before the value of IsConnected is changed.

OnIsConnectedChanging(bool, bool)

Executes the logic for when IsConnected is changing.

Declaration
private void OnIsConnectedChanging(bool oldValue, bool newValue)
Parameters
Type Name Description
bool oldValue

The previous property value that is being replaced.

bool newValue

The new property value being set.

Remarks

This method is invoked right before the value of IsConnected is changed.

OnMessageCountChanged(long)

Executes the logic for when MessageCount just changed.

Declaration
private void OnMessageCountChanged(long value)
Parameters
Type Name Description
long value

The new property value that was set.

Remarks

This method is invoked right after the value of MessageCount is changed.

OnMessageCountChanged(long, long)

Executes the logic for when MessageCount just changed.

Declaration
private void OnMessageCountChanged(long oldValue, long newValue)
Parameters
Type Name Description
long oldValue

The previous property value that was replaced.

long newValue

The new property value that was set.

Remarks

This method is invoked right after the value of MessageCount is changed.

OnMessageCountChanging(long)

Executes the logic for when MessageCount is changing.

Declaration
private void OnMessageCountChanging(long value)
Parameters
Type Name Description
long value

The new property value being set.

Remarks

This method is invoked right before the value of MessageCount is changed.

OnMessageCountChanging(long, long)

Executes the logic for when MessageCount is changing.

Declaration
private void OnMessageCountChanging(long oldValue, long newValue)
Parameters
Type Name Description
long oldValue

The previous property value that is being replaced.

long newValue

The new property value being set.

Remarks

This method is invoked right before the value of MessageCount is changed.

OnMessageTypeChanged(string)

Executes the logic for when MessageType just changed.

Declaration
private void OnMessageTypeChanged(string value)
Parameters
Type Name Description
string value

The new property value that was set.

Remarks

This method is invoked right after the value of MessageType is changed.

OnMessageTypeChanged(string?, string)

Executes the logic for when MessageType just changed.

Declaration
private void OnMessageTypeChanged(string? oldValue, string newValue)
Parameters
Type Name Description
string oldValue

The previous property value that was replaced.

string newValue

The new property value that was set.

Remarks

This method is invoked right after the value of MessageType is changed.

OnMessageTypeChanging(string)

Executes the logic for when MessageType is changing.

Declaration
private void OnMessageTypeChanging(string value)
Parameters
Type Name Description
string value

The new property value being set.

Remarks

This method is invoked right before the value of MessageType is changed.

OnMessageTypeChanging(string?, string)

Executes the logic for when MessageType is changing.

Declaration
private void OnMessageTypeChanging(string? oldValue, string newValue)
Parameters
Type Name Description
string oldValue

The previous property value that is being replaced.

string newValue

The new property value being set.

Remarks

This method is invoked right before the value of MessageType is changed.

OnNumChannelsChanged(int)

Executes the logic for when NumChannels just changed.

Declaration
private void OnNumChannelsChanged(int value)
Parameters
Type Name Description
int value

The new property value that was set.

Remarks

This method is invoked right after the value of NumChannels is changed.

OnNumChannelsChanged(int, int)

Executes the logic for when NumChannels just changed.

Declaration
private void OnNumChannelsChanged(int oldValue, int newValue)
Parameters
Type Name Description
int oldValue

The previous property value that was replaced.

int newValue

The new property value that was set.

Remarks

This method is invoked right after the value of NumChannels is changed.

OnNumChannelsChanging(int)

Executes the logic for when NumChannels is changing.

Declaration
private void OnNumChannelsChanging(int value)
Parameters
Type Name Description
int value

The new property value being set.

Remarks

This method is invoked right before the value of NumChannels is changed.

OnNumChannelsChanging(int, int)

Executes the logic for when NumChannels is changing.

Declaration
private void OnNumChannelsChanging(int oldValue, int newValue)
Parameters
Type Name Description
int oldValue

The previous property value that is being replaced.

int newValue

The new property value being set.

Remarks

This method is invoked right before the value of NumChannels is changed.

OnReceive(object, object)

Processes incoming data from an upstream block. Must be implemented by derived classes.

Declaration
protected override void OnReceive(object sender, object data)
Parameters
Type Name Description
object sender

The publishing block, or whatever else delivered the value.

object data
Overrides
BaseBlock.OnReceive(object, object)
Remarks

Called on the dispatching thread, not the UI thread.

OnTopicChanged(string)

Executes the logic for when Topic just changed.

Declaration
private void OnTopicChanged(string value)
Parameters
Type Name Description
string value

The new property value that was set.

Remarks

This method is invoked right after the value of Topic is changed.

OnTopicChanged(string?, string)

Executes the logic for when Topic just changed.

Declaration
private void OnTopicChanged(string? oldValue, string newValue)
Parameters
Type Name Description
string oldValue

The previous property value that was replaced.

string newValue

The new property value that was set.

Remarks

This method is invoked right after the value of Topic is changed.

OnTopicChanging(string)

Executes the logic for when Topic is changing.

Declaration
private void OnTopicChanging(string value)
Parameters
Type Name Description
string value

The new property value being set.

Remarks

This method is invoked right before the value of Topic is changed.

OnTopicChanging(string?, string)

Executes the logic for when Topic is changing.

Declaration
private void OnTopicChanging(string? oldValue, string newValue)
Parameters
Type Name Description
string oldValue

The previous property value that is being replaced.

string newValue

The new property value being set.

Remarks

This method is invoked right before the value of Topic is changed.

ProcessMessage(string)

Declaration
private void ProcessMessage(string json)
Parameters
Type Name Description
string json

ReceiveLoopAsync(CancellationToken)

Declaration
private Task ReceiveLoopAsync(CancellationToken ct)
Parameters
Type Name Description
CancellationToken ct
Returns
Type Description
Task

Reconnect()

Declaration
public void Reconnect()

ReshapeToVector(double[], int)

Declaration
private static Vector<double> ReshapeToVector(double[] data, int numChannels)
Parameters
Type Name Description
double[] data
int numChannels
Returns
Type Description
Vector<double>

SendJsonAsync(object)

Declaration
private Task SendJsonAsync(object payload)
Parameters
Type Name Description
object payload
Returns
Type Description
Task

Events

OnMessageReceived

Declaration
public event Action<Vector<double>>? OnMessageReceived
Event Type
Type Description
Action<Vector<double>>

Implements

INotifyPropertyChanged
INotifyPropertyChanging
ISubscriber
IPublisher
IBaseBlockProps
IJsonExportable
IDisposable
IAsyncDisposable

Search documentation

Start typing to search the documentation.