-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoosanROSJointService.cs
179 lines (152 loc) · 5.68 KB
/
DoosanROSJointService.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// System
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
// Unity
using Newtonsoft.Json;
using UnityEngine;
using RosSharp.RosBridgeClient;
using RosSharp.RosBridgeClient.Protocols;
using RosSharp.RosBridgeClient.Services;
using RosSharp.RosBridgeClient.MessageTypes.Sensor;
using RosSharp.RosBridgeClient.MessageTypes.Std;
// NERVV
using NERVV;
/// <summary>
/// Implements Doosan's MoveJoint service.
/// Details can be found at http://wiki.ros.org/doosan-robotics?action=AttachFile&do=get&target=Doosan_Robotics_ROS_Manual_ver0.92_190508A%28EN.%29.pdf
///</summary>
public class DoosanROSJointService : OutputSource {
#region Static
public enum ProtocolSelection { WebSocketSharp, WebSocketNET };
#endregion
#region ROS Settings
/// <summary>Name of service to call</summary>
[Tooltip("Name of service to call"), Header("ROS Settings")]
public string ServiceName = "/dsrm0609/motion/move_joint";
/// <summary>URL of RosBridgeClient websocket to subscribe from</summary>
[Tooltip("URL of RosBridgeClient websocket to subscribe from")]
public string URL = "";
/// <summary>Protocol to use to connect to RosBridgeClient</summary>
[Tooltip("Protocol to use to connect to RosBridgeClient")]
public ProtocolSelection Protocol = ProtocolSelection.WebSocketNET;
/// <summary>Serialization mode of RosBridgeClient</summary>
[Tooltip("Serialization mode of RosBridgeClient")]
public RosSocket.SerializerEnum SerializationMode = RosSocket.SerializerEnum.JSON;
#endregion
#region NERVV Settings
/// <summary>Machine to set angles from topic</summary>
[Tooltip("Machine to set angles from topic"), Header("NERVV Settings")]
public Machine machineToPublish;
/// <summary>Interval in seconds to poll</summary>
[Tooltip("Interval in seconds to poll")]
public float pollInterval = 0.25f;
#endregion
#region Vars
RosSocket rosSocket = null;
string serviceID = null;
float timeToTrigger = 0.0f;
#endregion
#region Unity methods
/// <summary>Safety checks and initialization</summary>
protected override void Start() {
base.Start();
// Safety checks
if (machineToPublish == null) {
LogError("Machine null, disabling self...");
OutputEnabled = false;
}
}
/// <summary>Initializes socket connection when object is enabled</summary>
void OnEnable() {
if (rosSocket != null)
LogWarning("Socket not null! Overwriting...");
rosSocket = null;
// Get protocol object
IProtocol p = null;
switch (Protocol) {
case ProtocolSelection.WebSocketSharp:
p = new WebSocketSharpProtocol(URL);
break;
case ProtocolSelection.WebSocketNET:
p = new WebSocketNetProtocol(URL);
break;
default:
Debug.LogError("Could not get find matching protocol for RosSocket!");
return;
}
Debug.Assert(p != null);
// OnConnected and OnClosed event handlers
p.OnConnected += OnConnected;
p.OnClosed += OnDisconnected;
// Start coroutine
rosSocket = new RosSocket(p, RosSocket.SerializerEnum.JSON);
}
/// <summary>Destroys socket connection if object is disabled</summary>
protected override void OnDisable() {
// Stop socket and close
if (rosSocket != null) {
serviceID = null;
rosSocket.Close();
rosSocket = null;
}
base.OnDisable();
}
/// <summary>Check if need to publish message</summary>
void Update() {
if (OutputEnabled && UnityEngine.Time.time > timeToTrigger) {
// Set new time to trigger
timeToTrigger = UnityEngine.Time.time + pollInterval;
SendJointsMessage();
}
}
#endregion
#region Methods
/// <summary>Sends message to service to move joints to specified location</summary>
void SendJointsMessage() {
// Safety checks
if (!OutputEnabled) return;
if (!rosSocket.protocol.IsAlive()) {
LogError("RosSocket is not active!");
return;
}
if (!string.IsNullOrEmpty(serviceID)) {
Log("ServiceID not null, not calling again");
return;
}
// Create new JointState message
MoveJointRequest message = new MoveJointRequest {
pos = new float[machineToPublish.Axes.Count],
vel = 225,
acc = 225,
time = 1,
radius = 20,
mode = 0,
blendType = 1,
syncType = 0
};
// Set joint angles
for (int i = 0; i < machineToPublish.Axes.Count; i++)
message.pos[i] = machineToPublish.Axes[i].ExternalValue;
// Call move joint service
serviceID = rosSocket.CallService<MoveJointRequest, MoveJointResponse>(
ServiceName,
VerifySuccess,
message
);
}
/// <summary>Callback with response from Doosan MoveJoint Service</summary>
void VerifySuccess(MoveJointResponse r) {
if (!r.success)
LogWarning("Could not successfully move angles to new joint!");
serviceID = null;
}
/// <summary>Callback when socket is connected</summary>
void OnConnected(object sender, EventArgs e) =>
Log($"Doosan ROS Joint Service connected to RosBridge: {URL}");
/// <summary>Callback when socket is disconnected</summary>
void OnDisconnected(object sender, EventArgs e) =>
Log($"Doosan ROS Joint Service disconnected from RosBridge: {URL}");
#endregion
}