-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathTurtleComponent.cs
74 lines (65 loc) · 2.31 KB
/
TurtleComponent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
namespace TurtleROSSample
{
using System;
using Microsoft.Psi;
using Microsoft.Psi.Components;
/// <summary>
/// Turtle ROS bridge component.
/// </summary>
public class TurtleComponent : ISourceComponent
{
private readonly Pipeline pipeline;
private readonly Turtle turtle;
private bool stopped;
/// <summary>
/// Initializes a new instance of the <see cref="TurtleComponent"/> class.
/// </summary>
/// <param name="pipeline">Pipeline to which component belongs.</param>
/// <param name="turtle">Turtle ROS bridge instance.</param>
public TurtleComponent(Pipeline pipeline, Turtle turtle)
{
this.pipeline = pipeline;
this.turtle = turtle;
this.Velocity = pipeline.CreateReceiver<(float, float)>(this, (c, _) => this.turtle.Velocity(c.Item1, c.Item2), nameof(this.Velocity));
this.PoseChanged = pipeline.CreateEmitter<(float, float, float)>(this, nameof(this.PoseChanged));
}
/// <summary>
/// Gets velocity receiver.
/// </summary>
public Receiver<(float, float)> Velocity { get; private set; }
/// <summary>
/// Gets pose changed emitter.
/// </summary>
public Emitter<(float, float, float)> PoseChanged { get; private set; }
/// <inheritdoc/>
public void Start(Action<DateTime> notifyCompletionTime)
{
notifyCompletionTime(DateTime.MaxValue);
this.turtle.Connect();
this.turtle.PoseChanged += this.OnPoseChanged;
}
/// <inheritdoc/>
public void Stop(DateTime finalOriginatingTime, Action notifyCompleted)
{
try
{
this.stopped = true;
this.turtle.Disconnect();
this.turtle.PoseChanged -= this.OnPoseChanged;
}
finally
{
notifyCompleted();
}
}
private void OnPoseChanged(object sender, (float, float, float) pose)
{
if (!this.stopped)
{
this.PoseChanged.Post(pose, this.pipeline.GetCurrentTime());
}
}
}
}