Class Myo
High-performance Myo armband EMG source block (8 channels, BLE).
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.
JSON configuration:
{
"MyMyo": {
"Type": "Myo",
"DesiredRate": 200,
"Inputs": [ "Clock" ],
"Params": [ "MyArmband" ],
"Path": "C:/Data/session1"
}
}
Params:
| Index | Description |
|---|---|
| 0 |
MyoName (string, optional). Substring filter applied during BLE scan.
Only devices whose name contains this value (case-insensitive) are shown
in FoundDevices. If omitted, all discovered devices are listed.
|
How Type, Inputs and Params work · Block catalogue and parameter reference
Implements
Inherited Members
Namespace: MOSAIC.Models.Devices
Assembly: MOSAIC.dll
Syntax
public sealed class Myo : BaseBlock, INotifyPropertyChanged, INotifyPropertyChanging, ISubscriber, IPublisher, IBaseBlockProps, IJsonExportable, IDisposable, IAsyncDisposable
Remarks
Overview: Streams 8-channel surface EMG from a Thalmic Myo armband over Bluetooth Low Energy. Each tick dequeues one BLE sample, decodes it into a MathNet.Numerics.LinearAlgebra.Vector<T> of double in the range [−1.0, +0.992], and publishes it downstream. The block is tick-driven — an upstream clock block provides the cadence; the Myo's own sample rate is not used directly.
Sample ownership: Acquisition reuses a private buffer. Each publication copies it, so queued consumers and recordings retain the sample even after later BLE packets arrive.
Lookup table decode: A 256-entry LUT (_byteToDouble) maps each raw byte
[0, 255] to a double in [−1.0, +0.992] via signed reinterpretation
((sbyte)b / 128.0), eliminating per-sample division on the hot path.
BLE buffer management: ShortenBuffer(int) is called each tick to keep BLE latency bounded to at most MaxDelay packets, discarding older samples when the consumer falls behind.
BLE lifecycle:
- ScanAsync(CancellationToken) — scans for 5 seconds, populates FoundDevices (filtered by optional MyoName substring).
- ConnectAsync(string, CancellationToken) — connects, sends vibration feedback, runs the BLE connection routine, and configures the Myo for filtered EMG streaming (no IMU, no classifier).
- DisconnectAsync() — sends short vibration feedback, disconnects, and zeroes the write buffer for clean UI decay.
CSV logging: When a Path is specified in the JSON configuration, the base class
Dumper automatically logs every published vector as
timestamp, ch₀, ch₁, …, ch₇ via Publish(object).
Constructors
Myo(BleManager, string, double)
Declaration
internal Myo(BleManager bleManager, string name = "Myo", double desiredRate = 200)
Parameters
| Type | Name | Description |
|---|---|---|
| BleManager | bleManager | |
| string | name | |
| double | desiredRate |
Myo(string, double)
Initializes a new Myo block with a private acquisition buffer ready for streaming.
Declaration
public Myo(string name = "Myo", double desiredRate = 200)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Display name of the block. |
| double | desiredRate | Desired processing rate in Hz. Defaults to 200 (Myo's native EMG sample rate). |
Fields
Channels
Number of EMG channels on the Myo armband.
Declaration
private const int Channels = 8
Field Value
| Type | Description |
|---|---|
| int |
MYOHW_EMG_SCALE
Scale factor for Myo EMG data. Raw signed bytes are divided by this value to produce the [−1.0, +0.992] range. Used to build _byteToDouble.
Declaration
private const double MYOHW_EMG_SCALE = 128
Field Value
| Type | Description |
|---|---|
| double |
MaxDelay
Maximum number of BLE packets allowed to accumulate before older ones are discarded. Keeps streaming latency bounded.
Declaration
private const int MaxDelay = 15
Field Value
| Type | Description |
|---|---|
| int |
_bleManager
Singleton BLE manager shared across all BLE device blocks.
Declaration
private readonly BleManager _bleManager
Field Value
| Type | Description |
|---|---|
| BleManager |
_byteToDouble
Precomputed lookup table mapping each byte [0, 255] to a double in [−1.0, +0.992]. Eliminates per-sample signed reinterpretation and division on the hot path.
Declaration
private static readonly double[] _byteToDouble
Field Value
| Type | Description |
|---|---|
| double[] |
_cmd
Myo-specific BLE command definitions (vibration, EMG mode, sleep mode, etc.).
Declaration
private readonly MyoCommandDefinitions _cmd
Field Value
| Type | Description |
|---|---|
| MyoCommandDefinitions |
_dev
The connected BLE device handle, or null if not connected.
Declaration
private BleDevice? _dev
Field Value
| Type | Description |
|---|---|
| BleDevice |
_streamReady
Gets a value indicating whether the Myo is currently connected and streaming.
Declaration
private volatile bool _streamReady
Field Value
| Type | Description |
|---|---|
| bool |
_writeArr
Declaration
private readonly double[] _writeArr
Field Value
| Type | Description |
|---|---|
| double[] |
_writeVec
Declaration
private readonly DenseVector _writeVec
Field Value
| Type | Description |
|---|---|
| DenseVector |
Properties
DumpFilePrefix
Recordings from this block are named <block>_emg.csv.
Declaration
protected override string DumpFilePrefix { get; }
Property Value
| Type | Description |
|---|---|
| string |
Overrides
FoundDevices
Observable collection of discovered BLE device names, populated by ScanAsync(CancellationToken). Bound to the UI device picker.
Declaration
public ObservableCollection<string> FoundDevices { get; }
Property Value
| Type | Description |
|---|---|
| ObservableCollection<string> |
IsConnected
Declaration
public bool IsConnected { 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
LastConnectionError
The last connection/setup failure, displayed on the Myo card.
Declaration
public string? LastConnectionError { get; private set; }
Property Value
| Type | Description |
|---|---|
| string |
MyoName
Optional substring filter applied during ScanAsync(CancellationToken) to restrict FoundDevices to matching Myo armbands.
Declaration
public string? MyoName { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
Visualization
This block's visualization, or null when it has none.
Declaration
protected override BlockVisualization? Visualization { get; }
Property Value
| Type | Description |
|---|---|
| BlockVisualization |
Overrides
Remarks
Blocks own their scope as a public Viz property rather than inheriting one, so the
base class has no way to reach it. Overriding this is what lets the rate below be wired up
automatically. With correct publication metadata, the bundle derives the time axis for
one vector per publication or fixed-size contiguous sample packets from the feed shape.
When a plot needs a rate that publication frequency and feed shape cannot express (for
example, variable-sized packets or overlapping windows), declare it with
Viz.UpdateSignalRate. An explicit declaration wins over the derived rate. A block
with several bundles must configure each one; this hook exposes only one bundle.
Viz
Visualization helper for binding the EMG signal to the UI scope.
Declaration
public BlockVisualization? Viz { get; }
Property Value
| Type | Description |
|---|---|
| BlockVisualization |
Methods
BuildLut()
Builds the 256-entry byte-to-double lookup table. Each byte is reinterpreted as a signed value (sbyte) and divided by MYOHW_EMG_SCALE (128.0), producing a range of [−1.0, +0.992].
Declaration
private static double[] BuildLut()
Returns
| Type | Description |
|---|---|
| double[] | A 256-element double array indexed by raw byte value. |
ConfigureInput(IServiceProvider, JsonModel)
Creates and configures a Myo block from a JSON model definition.
Declaration
public static Myo ConfigureInput(IServiceProvider sp, JsonModel m)
Parameters
| Type | Name | Description |
|---|---|---|
| IServiceProvider | sp | Service provider for dependency injection. |
| JsonModel | m | The JSON model. See the class-level example for the expected |
Returns
| Type | Description |
|---|---|
| Myo | A fully configured Myo instance. |
See Also
ConnectAsync(string, CancellationToken)
Connects to the specified Myo device and configures it for filtered EMG streaming.
Declaration
public ValueTask<bool> ConnectAsync(string selectedDeviceName, CancellationToken ct = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | selectedDeviceName | Display name of the device to connect to (must match an entry in FoundDevices). |
| CancellationToken | ct | Optional cancellation token. |
Returns
| Type | Description |
|---|---|
| ValueTask<bool> | true if the connection succeeded and EMG streaming was configured; false otherwise. |
Remarks
Connection sequence:
- Vibrate long ×2 (tactile feedback that pairing started).
- Run the BLE connection routine (service/characteristic discovery).
- Set EMG mode: filtered EMG, no IMU, no classifier.
- Disable auto-sleep to keep the armband streaming.
DisconnectAsync()
Disconnects from the Myo armband and zeroes the write buffer for clean UI decay.
Declaration
public ValueTask DisconnectAsync()
Returns
| Type | Description |
|---|---|
| ValueTask |
Remarks
Disconnect sequence:
- Vibrate short ×2 (tactile feedback that disconnect started).
- Disconnect the BLE device.
- Zero the current write buffer so the scope decays to zero smoothly.
Dispose()
Releases all resources: visualization, BLE connection, and base class resources (including the CSV dumper if configured).
Declaration
public override void Dispose()
Overrides
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 |
Overrides
OnReceive(object, object)
Processes one tick: dequeues a BLE sample (if available), decodes it into the write buffer, and publishes the current vector.
Declaration
protected override void OnReceive(object sender, object tNow)
Parameters
| Type | Name | Description |
|---|---|---|
| object | sender | The upstream clock block. |
| object | tNow | Tick payload (ignored — timing comes from the clock block's cadence). |
Overrides
Remarks
BLE decode: Raw bytes are converted to doubles via the precomputed _byteToDouble LUT — no division, no branching per sample. If the BLE packet is shorter than Channels, trailing channels retain their previous values (graceful degradation).
Buffer management: ShortenBuffer(int) is called each tick to cap BLE queue depth at MaxDelay packets.
PublishCurrent()
Publishes a stable copy of the latest acquired sample.
Declaration
private void PublishCurrent()
ScanAsync(CancellationToken)
Scans for BLE devices for 5 seconds and populates FoundDevices.
Declaration
public ValueTask ScanAsync(CancellationToken ct = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | ct | Optional cancellation token. |
Returns
| Type | Description |
|---|---|
| ValueTask |
Remarks
If MyoName is set, only devices whose name contains the substring (case-insensitive) are added to FoundDevices.
All collection mutations are marshalled to the UI thread since FoundDevices is an ObservableCollection<T>.