This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ros_connect.js
405 lines (324 loc) · 11.8 KB
/
ros_connect.js
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
'use strict';
var messages_received_body = [];
var commands_sent_body = [];
var messages_received_wrist = [];
var commands_sent_wrist = [];
var rosImageReceived = false
var img = document.createElement("IMG")
img.style.visibility = 'hidden'
var rosJointStateReceived = false
var jointState = null
var session_body = {ws:null, ready:false, port_details:{}, port_name:"", version:"", commands:[], hostname:"", serial_ports:[]};
var session_wrist = {ws:null, ready:false, port_details:{}, port_name:"", version:"", commands:[], hostname:"", serial_ports:[]};
// connect to rosbridge websocket
var ros = new ROSLIB.Ros({
url : 'ws://localhost:9090'
});
ros.on('connection', function() {
console.log('Connected to websocket.');
});
ros.on('error', function(error) {
console.log('Error connecting to websocket: ', error);
});
ros.on('close', function() {
console.log('Connection to websocket has been closed.');
});
var imageTopic = new ROSLIB.Topic({
ros : ros,
name : '/camera/color/image_raw/compressed',
messageType : 'sensor_msgs/CompressedImage'
});
imageTopic.subscribe(function(message) {
//console.log('Received compressed image on ' + imageTopic.name);
//console.log('message.header =', message.header)
//console.log('message.format =', message.format)
img.src = 'data:image/jpg;base64,' + message.data
if (rosImageReceived === false) {
console.log('Received first compressed image from ROS topic ' + imageTopic.name);
rosImageReceived = true
}
//console.log('img.width =', img.width)
//console.log('img.height =', img.height)
//console.log('img.naturalWidth =', img.naturalWidth)
//console.log('img.naturalHeight =', img.naturalHeight)
//console.log('attempted to draw image to the canvas')
//imageTopic.unsubscribe()
});
function getJointEffort(jointStateMessage, jointName) {
var jointIndex = jointStateMessage.name.indexOf(jointName)
return jointStateMessage.effort[jointIndex]
}
function getJointValue(jointStateMessage, jointName) {
var jointIndex = jointStateMessage.name.indexOf(jointName)
return jointStateMessage.position[jointIndex]
}
var jointStateTopic = new ROSLIB.Topic({
ros : ros,
name : '/stretch/joint_states/',
messageType : 'sensor_msgs/JointState'
});
jointStateTopic.subscribe(function(message) {
jointState = message
if (rosJointStateReceived === false) {
console.log('Received first joint state from ROS topic ' + jointStateTopic.name);
rosJointStateReceived = true
}
// send wrist joint effort
var JointEffort = getJointEffort(jointState, 'joint_wrist_yaw')
var message = {'type': 'sensor', 'subtype':'wrist', 'name':'yaw_torque', 'value': JointEffort}
sendData(message)
// send gripper effort
JointEffort = getJointEffort(jointState, 'joint_gripper_finger_left')
var message = {'type': 'sensor', 'subtype':'gripper', 'name':'gripper_torque', 'value': JointEffort}
sendData(message)
// send lift effort
JointEffort = getJointEffort(jointState, 'joint_lift')
var message = {'type': 'sensor', 'subtype':'lift', 'name':'lift_effort', 'value': JointEffort}
sendData(message)
// send telescoping arm effort
JointEffort = getJointEffort(jointState, 'joint_arm_l0')
var message = {'type': 'sensor', 'subtype':'arm', 'name':'arm_effort', 'value': JointEffort}
sendData(message)
// Header header
// string[] name
// float64[] position
// float64[] velocity
// float64[] effort
//imageTopic.unsubscribe()
});
var trajectoryClient = new ROSLIB.ActionClient({
ros : ros,
serverName : '/stretch_controller/follow_joint_trajectory',
actionName : 'control_msgs/FollowJointTrajectoryAction'
});
function generatePoseGoal(pose){
var outStr = '{'
for (var key in pose) {
outStr = outStr + String(key) + ':' + String(pose[key]) + ', '
}
outStr = outStr + '}'
console.log('generatePoseGoal( ' + outStr + ' )')
var jointNames = []
var jointPositions = []
for (var key in pose) {
jointNames.push(key)
jointPositions.push(pose[key])
}
var newGoal = new ROSLIB.Goal({
actionClient : trajectoryClient,
goalMessage : {
trajectory : {
joint_names : jointNames,
points : [
{
positions : jointPositions
}
]
}
}
})
console.log('newGoal created =' + newGoal)
// newGoal.on('feedback', function(feedback) {
// console.log('Feedback: ' + feedback.sequence);
// });
// newGoal.on('result', function(result) {
// console.log('Final Result: ' + result.sequence);
// });
return newGoal
}
////////////////////////////////////////////////////////////////////////////////////
function loggedWebSocketSendWrist(cmd) {
session_wrist.ws.send(cmd);
commands_sent_wrist.push(cmd);
}
function sendCommandWrist(cmd) {
if(session_wrist.ready) {
command = JSON.stringify(cmd);
loggedWebSocketSendWrist(command);
}
}
function loggedWebSocketSendBody(cmd) {
session_body.ws.send(cmd);
commands_sent_body.push(cmd);
}
function sendCommandBody(cmd) {
if(session_body.ready) {
command = JSON.stringify(cmd);
loggedWebSocketSendBody(command);
}
}
////////////////////////////////////////////////////////////////////////////////////
//Called from mode switch
function robotModeOn(modeKey) {
console.log('robotModeOn called with modeKey = ' + modeKey)
if (modeKey === 'nav') {
var headNavPoseGoal = generatePoseGoal({'joint_head_pan': 0.0, 'joint_head_tilt': -1.0})
headNavPoseGoal.send()
console.log('sending navigation pose to head')
}
if (modeKey === 'low_arm') {
var headManPoseGoal = generatePoseGoal({'joint_head_pan': -1.57, 'joint_head_tilt': -0.9})
headManPoseGoal.send()
console.log('sending manipulation pose to head')
}
if (modeKey === 'high_arm') {
var headManPoseGoal = generatePoseGoal({'joint_head_pan': -1.57, 'joint_head_tilt': -0.45})
headManPoseGoal.send()
console.log('sending manipulation pose to head')
}
}
////////////////////////////////////////////////////////////////////////////////////
//Called from button click
function baseTranslate(dist, vel) {
// distance in centimeters
// velocity in centimeters / second
console.log('sending baseTranslate command')
if (dist > 0.0){
var baseForwardPoseGoal = generatePoseGoal({'translate_mobile_base': -0.02})
baseForwardPoseGoal.send()
} else if (dist < 0.0) {
var baseBackwardPoseGoal = generatePoseGoal({'translate_mobile_base': 0.02})
baseBackwardPoseGoal.send()
}
//sendCommandBody({type: "base",action:"translate", dist:dist, vel:vel});
}
function baseTurn(ang_deg, vel) {
// angle in degrees
// velocity in centimeter / second (linear wheel velocity - same as BaseTranslate)
console.log('sending baseTurn command')
if (ang_deg > 0.0){
var baseTurnLeftPoseGoal = generatePoseGoal({'rotate_mobile_base': -0.1})
baseTurnLeftPoseGoal.send()
} else if (ang_deg < 0.0) {
var baseTurnRightPoseGoal = generatePoseGoal({'rotate_mobile_base': 0.1})
baseTurnRightPoseGoal.send()
}
//sendCommandBody({type: "base",action:"turn", ang:ang_deg, vel:vel});
}
function sendIncrementalMove(jointName, jointValueInc) {
console.log('sendIncrementalMove start: jointName =' + jointName)
if (jointState !== null) {
var newJointValue = getJointValue(jointState, jointName)
newJointValue = newJointValue + jointValueInc
console.log('poseGoal call: jointName =' + jointName)
var pose = {[jointName]: newJointValue}
var poseGoal = generatePoseGoal(pose)
poseGoal.send()
return true
}
return false
}
function armMove(dist, timeout) {
console.log('attempting to sendarmMove command')
var jointValueInc = 0.0
if (dist > 0.0) {
jointValueInc = 0.02
} else if (dist < 0.0) {
jointValueInc = -0.02
}
sendIncrementalMove('wrist_extension', jointValueInc)
//sendCommandBody({type: "arm", action:"move", dist:dist, timeout:timeout});
}
function liftMove(dist, timeout) {
console.log('attempting to sendliftMove command')
var jointValueInc = 0.0
if (dist > 0.0) {
jointValueInc = 0.02
} else if (dist < 0.0) {
jointValueInc = -0.02
}
sendIncrementalMove('joint_lift', jointValueInc)
//sendCommandBody({type: "lift", action:"move", dist:dist, timeout:timeout});
}
function gripperDeltaAperture(deltaWidthCm) {
// attempt to change the gripper aperture
console.log('attempting to sendgripper delta command');
var jointValueInc = 0.0
if (deltaWidthCm > 0.0) {
jointValueInc = 0.05
} else if (deltaWidthCm < 0.0) {
jointValueInc = -0.05
}
sendIncrementalMove('joint_gripper_finger_left', jointValueInc)
//sendCommandWrist({type:'gripper', action:'delta', delta_aperture_cm:deltaWidthCm});
}
function wristMove(angRad) {
console.log('attempting to send wristMove command')
var jointValueInc = 0.0
if (angRad > 0.0) {
jointValueInc = 0.1
} else if (angRad < 0.0) {
jointValueInc = -0.1
}
sendIncrementalMove('joint_wrist_yaw', jointValueInc)
}
function headTilt(angRad) {
console.log('attempting to send headTilt command')
sendIncrementalMove('joint_head_tilt', angRad)
}
function headPan(angRad) {
console.log('attempting to send headPan command')
sendIncrementalMove('joint_head_pan', angRad)
}
////////////////////////////////////////////////////////////////////////////////////
function armHome() {
console.log('sending armHome command')
sendCommandBody({type: "arm", action:"home"});
}
function liftHome() {
console.log('sending liftHome command')
sendCommandBody({type: "lift", action:"home"});
}
function wristStopMotion() {
console.log('sending wrist stop motion command');
sendCommandWrist({type:'wrist', action:'stop_motion'});
}
function wristBendVelocity(deg_per_sec) {
console.log('sending wrist bend velocity of ' + deg_per_sec + ' command');
sendCommandWrist({type:'wrist', action:'bend_velocity', angle:deg_per_sec});
}
function wristAutoBend(angleDeg) {
// attempt to bend the wrist by deltaAngle degrees
//console.log('*** no wrist bend control exists yet ***');
console.log('sending auto wrist bend to ' + angleDeg + ' command');
sendCommandWrist({type:'wrist', action:'auto_bend', angle:angleDeg});
}
function initFixedWrist() {
// try to emulate a fixed wrist with gripper flat and bent down 45 degrees from horizontal
console.log('sending init_fixed_wrist command');
sendCommandWrist({type:'wrist', action:'init_fixed_wrist'});
}
function wristBend(deltaAngle) {
// attempt to bend the wrist by deltaAngle degrees
//console.log('*** no wrist bend control exists yet ***');
console.log('sending wrist bend command');
sendCommandWrist({type:'wrist', action:'bend', angle:deltaAngle});
}
function wristRoll(deltaAngle) {
// attempt to roll the wrist by deltaAngle degrees
//console.log('*** no wrist roll control exists yet ***');
console.log('sending wrist roll command');
sendCommandWrist({type:'wrist', action:'roll', angle:deltaAngle});
}
function gripperGoalAperture(goalWidthCm) {
// attempt to change the gripper aperture
console.log('sending gripper command');
sendCommandWrist({type:'gripper', action:'width', goal_aperture_cm:goalWidthCm});
}
function gripperGoalAperture(goalWidthCm) {
// attempt to change the gripper aperture
console.log('sending gripper command');
sendCommandWrist({type:'gripper', action:'width', goal_aperture_cm:goalWidthCm});
}
function gripperFullyClose() {
console.log('sending fully close gripper command');
sendCommandWrist({type:'gripper', action:'fully_close'});
}
function gripperHalfOpen() {
console.log('sending half open gripper command');
sendCommandWrist({type:'gripper', action:'half_open'});
}
function gripperFullyOpen() {
console.log('sending fully open gripper command');
sendCommandWrist({type:'gripper', action:'fully_open'});
}