-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathTurtle.cs
100 lines (87 loc) · 3.6 KB
/
Turtle.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma warning disable SA1118 // Parameter must not span multiple lines
namespace TurtleROSSample
{
using System;
using System.Collections.Generic;
using Microsoft.Ros;
/// <summary>
/// ROS turtle bridge.
/// </summary>
public class Turtle
{
private const string NodeName = "/turtle_sample";
private const string CmdVelTopic = "/turtle1/cmd_vel";
private const string PoseTopic = "/turtle1/pose";
private readonly string rosNodeAddress;
private readonly string rosMasterAddress;
private RosNode.Node node;
private RosPublisher.IPublisher cmdVelPublisher;
private RosSubscriber.ISubscriber poseSubscriber;
private RosMessage.MessageDef poseMessageDef = RosMessage.CreateMessageDef(
"turtlesim/Pose",
"863b248d5016ca62ea2e895ae5265cf9",
new[]
{
Tuple.Create("x", RosMessage.RosFieldDef.Float32Def),
Tuple.Create("y", RosMessage.RosFieldDef.Float32Def),
Tuple.Create("theta", RosMessage.RosFieldDef.Float32Def),
Tuple.Create("linear_velocity", RosMessage.RosFieldDef.Float32Def),
Tuple.Create("angular_velocity", RosMessage.RosFieldDef.Float32Def),
});
/// <summary>
/// Initializes a new instance of the <see cref="Turtle"/> class.
/// </summary>
/// <param name="rosNodeAddress">ROS node address.</param>
/// <param name="rosMasterAddress">ROS master address.</param>
public Turtle(string rosNodeAddress, string rosMasterAddress)
{
this.rosNodeAddress = rosNodeAddress;
this.rosMasterAddress = rosMasterAddress;
}
/// <summary>
/// Pose changed event handler.
/// </summary>
public event EventHandler<(float, float, float)> PoseChanged;
/// <summary>
/// Connect to ROS.
/// </summary>
public void Connect()
{
this.node = new RosNode.Node(NodeName, this.rosNodeAddress, this.rosMasterAddress);
this.cmdVelPublisher = this.node.CreatePublisher(RosMessageTypes.Geometry.Twist.Def, CmdVelTopic, false);
this.poseSubscriber = this.node.Subscribe(this.poseMessageDef, PoseTopic, this.PoseUpdate);
}
/// <summary>
/// Disconnect from ROS.
/// </summary>
public void Disconnect()
{
this.node.UnregisterPublisher(CmdVelTopic);
this.node.UnregisterSubscriber(PoseTopic);
}
/// <summary>
/// Velocity action.
/// </summary>
/// <param name="linear">Linear speed.</param>
/// <param name="angular">Angular speed.</param>
public void Velocity(float linear, float angular)
{
this.cmdVelPublisher.Publish(
RosMessageTypes.Geometry.Twist.ToMessage(
new RosMessageTypes.Geometry.Twist.Kind(
new RosMessageTypes.Geometry.Vector3.Kind(linear, 0, 0),
new RosMessageTypes.Geometry.Vector3.Kind(0, 0, angular))));
}
private void PoseUpdate(IEnumerable<Tuple<string, RosMessage.RosFieldVal>> position)
{
if (this.PoseChanged != null)
{
dynamic pos = RosMessage.GetDynamicFieldVals(position);
this.PoseChanged(this, (pos.x, pos.y, pos.theta));
}
}
}
}
#pragma warning restore SA1118 // Parameter must not span multiple lines