This repository was archived by the owner on Jun 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathROSBridge.java
More file actions
242 lines (203 loc) · 6.8 KB
/
ROSBridge.java
File metadata and controls
242 lines (203 loc) · 6.8 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package com.roboeaters.grantbot;
// Uses ROSBridge 2.0 protocol:
// https://github.com/RobotWebTools/rosbridge_suite/blob/groovy-devel/ROSBRIDGE_PROTOCOL.md
// See also: Rosbridge.org
// Autobahn Android webSocket library:
// http://autobahn.ws/android/getstarted
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import de.tavendo.autobahn.WebSocket;
import de.tavendo.autobahn.WebSocketConnection;
import de.tavendo.autobahn.WebSocketException;
public class ROSBridge {
String hostName;
String portNumber;
byte[] ipAddress;
private JSONObject advertisement;
private JSONObject unAdvertisement;
private JSONObject publication;
private JSONObject subscription;
private JSONObject unSubscription;
private JSONObject serviceCall;
private JSONObject serviceResponse;
private JSONObject outerPayload;
private JSONObject innerPayload;
private String command;
private static final String TAG = "ros_thread";
private final WebSocket mConnection = new WebSocketConnection();
boolean canMessage;
public boolean isActivated;
public ROSBridge(byte[] ip, String port){
advertisement = new JSONObject();
unAdvertisement = new JSONObject();
publication = new JSONObject();
subscription = new JSONObject();
unSubscription = new JSONObject();
serviceCall = new JSONObject();
serviceResponse = new JSONObject();
portNumber = port;
ipAddress = ip;
try {
InetAddress addr = InetAddress.getByAddress(ipAddress);
hostName = addr.getHostName();
} catch (UnknownHostException e) {
e.printStackTrace();
}
start();
}
// opens the websocket connection
public void start() {
final String wsuri = "ws://" + hostName + ":" + portNumber;
Log.d(TAG, "Connecting to: " + wsuri);
try {
mConnection.connect(wsuri, new RosbridgeHandler(this)
{
// WARNING
// currently set up for "std_msgs/String"
// expand if other types are to be taken in
@Override
public void onTextMessage(String payload) {
try {
outerPayload = new JSONObject(payload);
innerPayload = new JSONObject(outerPayload.getString("msg"));
command = innerPayload.getString("data");
Log.d(TAG, "Command: " + command);
} catch (JSONException e) {
e.printStackTrace();
} catch (NullPointerException n) {
Log.w(TAG, "eater_contol format error");
// instead of a million JSONObject "has" and "isNull" checks (for now)
}
if(command.equals("start"))
isActivated = true;
if(command.equals("stop"))
isActivated = false;
if(command.equals("yolo"))
Log.d("SWAG", "ROBOEATERS SO IMPOSSIBLY FRESH");
}
}
);
} catch (WebSocketException e) {
Log.d(TAG, e.toString());
}
}
// sends JSON as text over websocket
private boolean sendJSON(String topic, JSONObject transmission, String verb) {
try {
mConnection.sendTextMessage(transmission.toString());
} catch (NullPointerException n) {
Log.d(TAG, "Unable to " + verb + " topic: " + topic + ".");
return false;
}
//Log.d(TAG, "Successfully able to " + verb + " topic: " + topic + ".");
return true;
}
// informs the ROS master that you will publish messages to a topic (required)
public boolean advertiseToTopic(String topic, String type)
{
Log.d("AUTOBAHN", "Attempting to advertise to topic: " + topic + ".");
try {
advertisement.put("op", "advertise");
//advertisement.put("id", String); // optional
advertisement.put("topic", topic); // topic: "eater_input"
advertisement.put("type", type); // for stringified/toString(ed) JSONs
} catch (JSONException e) {
e.printStackTrace();
}
return sendJSON(topic, advertisement, "advertise to");
}
// stop publishing messages to topic
public boolean unadvertiseFromTopic(String topic)
{
try {
unAdvertisement.put("op", "unadvertise");
//unAdvertisement.put("id", String); // optional
unAdvertisement.put("topic", topic); // "eater_input"
} catch (JSONException e) {
e.printStackTrace();
}
return sendJSON(topic, unAdvertisement, "unadvertise from");
}
// sends a rosbridge JSON containing a JSON-format ROS message.
public boolean publishToTopic(String topic, JSONObject message)
{
try {
publication.put("op", "publish");
//publication.put("id", String); // optional
publication.put("topic", topic);
publication.put("msg", message);
} catch (JSONException e) {
e.printStackTrace();
}
return sendJSON(topic, publication, "publish to");
}
// informs the ROS master that you will accept messages that are published to
// the specified topic.
public boolean subscribeToTopic(String topic, String type)
{
try {
subscription.put("op", "subscribe");
subscription.put("topic", topic);
subscription.put("type", type); // topics have type specified by default
//subscription.put("throttle_rate, int) // min time (ms) between messages default: 0
//subscription.put("queue_length, int); // size of message buffer (due to throttle) default: 1
//subscription.put("fragment_size, int); // max message size before being fragmented
//subscription.put("compression, String); // "none" or "png"
} catch (JSONException e) {
e.printStackTrace();
}
return sendJSON(topic, subscription, "subscribe to");
}
// stop accepting messages from topic
public boolean unSubscribeFromTopic(String topic)
{
try {
unSubscription.put("op", "unsubscribe");
//unSubscription.put("id", String); // optional
unSubscription.put("topic", topic);
} catch (JSONException e) {
e.printStackTrace();
}
return sendJSON(topic, unSubscription, "unsubscribe from");
}
// calls ROS service
public void callService(String service, List<String> args)
{
try {
serviceCall.put("op", "call_service");
//serviceCall.put("id", String); // optional
serviceCall.put("service", service);
//serviceCall.put("args, list<json>); // if service has no args, none need be provided
//serviceCall.put("fragment_size, int); // max message size before fragmentation
//serviceCall.put("compression, String); // "none" or "png"
} catch (JSONException e) {
e.printStackTrace();
}
// send(serviceCall);
}
// responds to a service call send from a ROS node
public void respondToService (String service, List<String> args)
{
try {
serviceResponse.put("op", "service_response");
//serviceResponse.put("id", String); // optional
serviceResponse.put("service", service);
//serviceResponse.put("values, list<json>); // JSON return values
} catch (JSONException e) {
e.printStackTrace();
}
// do something
}
// disconnects, though seems to do so unsuccessfully
public void end() {
if (mConnection.isConnected())
mConnection.disconnect();
}
public boolean getIsActivated() {
return isActivated;
}
}