forked from hcrlab/stretch_web_interface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot.ts
852 lines (756 loc) · 29.8 KB
/
robot.ts
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
import * as ROSLIB from "roslib";
import { NavGoalCommand, PoseGoalCommand } from "shared/commands";
import { Pose, ValidJoints, ROSCompressedImage, ROSJointState, VelocityGoalArray, GoalMessage, PoseGoalMessage, NavGoalMessage, quaternionToEuler } from "shared/util";
export const ALL_JOINTS: ValidJoints[] = ['joint_head_tilt', 'joint_head_pan', 'joint_gripper_finger_left', 'wrist_extension', 'joint_lift', 'joint_wrist_yaw', "translate_mobile_base", "rotate_mobile_base", 'gripper_aperture'];
export let JOINT_LIMITS: { [key in ValidJoints]?: [number, number] } = {
"wrist_extension": [0, .518],
"joint_wrist_yaw": [-1.38, 4.58],
"joint_lift": [0.15, 1.1],
"translate_mobile_base": [-30.0, 30.0],
"rotate_mobile_base": [-3.14, 3.14],
"joint_gripper_finger_left": [-0.375, 0.166]
}
export class Robot {
ros!: ROSLIB.Ros
private inSim!: boolean
robotFrameTfClient?: ROSLIB.TFClient
globalFrameTfClient?: ROSLIB.TFClient
trajectoryClient?: ROSLIB.ActionClient
moveBaseClient?: ROSLIB.ActionClient
jointStateTopic?: ROSLIB.Topic<ROSJointState>
cmdVel?: ROSLIB.Topic
velocityGoal?: ROSLIB.Goal
poseGoal?: ROSLIB.Goal
tfCallback: (frame: string, tranform: ROSLIB.Transform) => void
jointStateCallback: (jointState: ROSJointState) => void
goalCallback: (goal: GoalMessage) => void
setNavMode?: ROSLIB.Service
setPositionMode?: ROSLIB.Service
// TODO (kavidey): check whether this variable is necessary, we never read from it
// private currentJointTrajectoryGoal
private currentTrajectoryKillInterval?: number // same as lookAtGripperInterval, should be `typeof setTimeout`
private panOffset = 0;
private tiltOffset = 0;
private linkGripperFingerLeftTF?: ROSLIB.Transform
private linkHeadTiltTF?: ROSLIB.Transform
private cameraColorFrameTF?: ROSLIB.Transform
baseTF?: ROSLIB.Transform
jointState?: ROSJointState
private videoTopics: [ROSLIB.Topic<ROSCompressedImage>?] = []
private isWristFollowingActive = false
// TODO (kavidey): this should be `typeof setTimeout`, but TS wants it to be number
private lookAtGripperInterval?: number
private commands: { [key: string]: { [key: string]: (...args: any[]) => void } } = {
"drive": {
"forward": (size: number) => {
this.executeIncrementalMove("translate_mobile_base", size)
},
"backward": (size: number) => {
this.executeIncrementalMove("translate_mobile_base", -size)
},
"turn_right": (size: number) => {
this.executeIncrementalMove("rotate_mobile_base", -size)
},
"turn_left": (size: number) => {
this.executeIncrementalMove("rotate_mobile_base", size)
},
"turn_ccw": (_: any) => {
this.baseTurn(Math.PI / 2);
},
"turn_cw": (_: any) => {
this.baseTurn(-Math.PI / 2);
},
"velocities": ({ linVel, angVel }) => {
this.executeBaseVelocity(linVel, angVel)
},
"configure_mode": (mode: "position" | "navigation") => {
this.setBaseMode(mode)
}
},
"lift": {
"up": (size: number) => {
this.executeIncrementalMove("joint_lift", size)
},
"down": (size: number) => {
this.executeIncrementalMove("joint_lift", -size)
}
},
"arm": {
"extend": (size: number) => {
this.executeIncrementalMove("wrist_extension", size)
},
"retract": (size: number) => {
this.executeIncrementalMove("wrist_extension", -size)
}
},
"wrist": {
"in": (size: number) => {
this.executeIncrementalMove("joint_wrist_yaw", size)
},
"out": (size: number) => {
this.executeIncrementalMove("joint_wrist_yaw", -size)
},
"stop_all_motion": () => {
this.wristStopMotion();
},
"bend_velocity": (deg_per_sec: number) => {
this.wristBendVelocity(deg_per_sec);
},
"auto_bend": (ang_deg: number) => {
this.wristAutoBend(ang_deg);
},
"init_fixed_wrist": (vsize: number, vscalesize: number) => {
this.initFixedWrist();
},
"bend_up": (vsize: number, vscalesize: number) => {
this.wristBend(5.0); // attempt to bed the wrist upward by 5 degrees
},
"bend_down": (vsize: number, vscalesize: number) => {
this.wristBend(-5.0); // attempt to bed the wrist downward by 5 degrees
},
"roll_left": (vsize: number, vscalesize: number) => {
this.wristRoll(-5.0); // attempt to roll the wrist to the left (clockwise) by 5 degrees
},
"roll_right": (vsize: number, vscalesize: number) => {
this.wristRoll(5.0); // attempt to roll the wrist to the right (counterclockwise) by 5 degrees
}
},
"gripper": {
"set_goal": (goalWidthCm: number) => {
this.gripperGoalAperture(goalWidthCm);
},
"open": (vsize: number, vscalesize: number) => {
this.gripperDeltaAperture(0.1);
},
"close": (vsize: number, vscalesize: number) => {
this.gripperDeltaAperture(-0.1);
},
"configure_camera": (configuration: any) => {
// TODO (kavidey): Implement or remove this
}
},
"head": {
"up": (size: number) => {
this.executeIncrementalMove("joint_head_tilt", size)
},
"down": (size: number) => {
this.executeIncrementalMove("joint_head_tilt", -size)
},
"left": (size: number) => {
this.executeIncrementalMove("joint_head_pan", size)
},
"right": (size: number) => {
this.executeIncrementalMove("joint_head_pan", -size)
},
"gripper_follow": (value: boolean) => {
this.setPanTiltFollowGripper(value);
},
"set_pan_tilt": (values: Map) => {
this.setPanTilt(values["pan"], values["tilt"])
},
"look_at_base": () => {
this.lookAtBase();
},
"look_at_arm": () => {
this.lookAtArm();
},
"configure_overhead_camera": (configuration: any) => {
// TODO (kavidey): Implement or remove this
}
}
}
constructor(jointStateCallback: (jointState: ROSJointState) => void, tfCallback: (frame: string, tranform: ROSLIB.Transform) => void, goalCallback: (goal: GoalMessage) => void) {
this.jointStateCallback = jointStateCallback
this.tfCallback = tfCallback
this.goalCallback = goalCallback
}
async connect(): Promise<void> {
// connect to rosbridge websocket
let ros = new ROSLIB.Ros({
url: 'wss://localhost:9090'
});
return new Promise<void>((res, rej) => {
ros.on('connection', async () => {
let simTime = new ROSLIB.Param({
ros: ros,
name: '/use_sim_time'
});
simTime.get(value => {
this.inSim = value
if (this.inSim) {
// Joints have different limits in simulator.
JOINT_LIMITS["joint_gripper_finger_left"]![1] = .6
JOINT_LIMITS["wrist_extension"]![1] = .51
JOINT_LIMITS["joint_wrist_yaw"]![0] = -1.74
JOINT_LIMITS["joint_wrist_yaw"]![1] = 3.99
}
});
await this.onConnect(ros);
res()
});
ros.on('error', (error) => {
rej(error)
});
ros.on('close', () => {
rej('Connection to websocket has been closed.')
});
})
}
async onConnect(ros: ROSLIB.Ros) {
this.ros = ros;
this.jointStateTopic = new ROSLIB.Topic({
ros: this.ros,
name: '/stretch/joint_states/',
messageType: 'sensor_msgs/JointState'
});
this.jointStateTopic.subscribe(message => {
if (this.jointState === null) {
console.log('Received first joint state from ROS topic ' + this.jointStateTopic?.name);
}
this.jointState = message;
if (this.jointStateCallback) this.jointStateCallback(message)
});
this.setNavMode = new ROSLIB.Service({
ros: ros,
name: '/switch_to_navigation_mode',
serviceType: '/switch_to_navigation_mode'
});
this.setPositionMode = new ROSLIB.Service({
ros: ros,
name: '/switch_to_position_mode',
serviceType: '/switch_to_position_mode'
});
this.cmdVel = new ROSLIB.Topic({
ros: ros,
name: '/stretch/cmd_vel',
messageType: 'geometry_msgs/Twist'
});
this.robotFrameTfClient = new ROSLIB.TFClient({
ros: this.ros,
fixedFrame: 'base_link',
angularThres: 0.01,
transThres: 0.01,
rate: 10
});
this.globalFrameTfClient = new ROSLIB.TFClient({
ros: this.ros,
fixedFrame: 'map',
angularThres: 0.01,
transThres: 0.01,
rate: 10
});
this.robotFrameTfClient.subscribe('link_gripper_finger_left', transform => {
this.linkGripperFingerLeftTF = transform;
if (this.tfCallback) {
this.tfCallback('link_gripper_finger_left', transform)
}
});
this.robotFrameTfClient.subscribe('link_head_tilt', transform => {
this.linkHeadTiltTF = transform;
if (this.tfCallback) {
this.tfCallback('link_head_tilt', transform)
}
});
this.robotFrameTfClient.subscribe('camera_color_frame', transform => {
this.cameraColorFrameTF = transform;
if (this.tfCallback) {
this.tfCallback('camera_color_frame', transform)
}
});
this.globalFrameTfClient.subscribe('base_link', transform => {
this.baseTF = transform;
if (this.tfCallback) {
this.tfCallback('base_frame', transform)
}
});
this.trajectoryClient = new ROSLIB.ActionClient({
ros: this.ros,
serverName: '/stretch_controller/follow_joint_trajectory',
actionName: 'control_msgs/FollowJointTrajectoryAction',
timeout: 100 // TODO (kavidey): Figure out what unit this is and update
});
this.moveBaseClient = new ROSLIB.ActionClient({
ros: this.ros,
serverName: '/move_base',
actionName: 'move_base_msgs/MoveBaseAction',
timeout: 3000
})
return Promise.resolve()
}
subscribeToVideo(topicName: string, callback: (message: ROSCompressedImage) => void) {
let topic: ROSLIB.Topic<ROSCompressedImage> = new ROSLIB.Topic({
ros: this.ros,
name: topicName,
messageType: 'sensor_msgs/CompressedImage'
});
this.videoTopics.push(topic)
topic.subscribe(callback)
}
////////////////////////////////////////////////////////////////////////////////////
baseTranslate(dist: number) {
if (!this.trajectoryClient) throw 'trajectoryClient is undefined';
makePoseGoal({ 'translate_mobile_base': dist }, this.trajectoryClient).send();
}
baseTurn(ang_deg: number) {
// angle in degrees
// velocity in centimeter / second (linear wheel velocity - same as BaseTranslate)
if (!this.trajectoryClient) throw 'trajectoryClient is undefined';
makePoseGoal({ 'rotate_mobile_base': ang_deg }, this.trajectoryClient).send();
}
makeIncrementalMoveGoal(jointName: ValidJoints, jointValueInc: number): ROSLIB.Goal {
if (!this.jointState) throw 'jointState is undefined';
let newJointValue = getJointValue(this.jointState, jointName)
// Paper over Hello's fake joints
if (jointName === "translate_mobile_base" || jointName === "rotate_mobile_base") {
// These imaginary joints are floating, always have 0 as their reference
newJointValue = 0
}
newJointValue = newJointValue + jointValueInc
if (jointName in JOINT_LIMITS) {
// Make sure new joint value is within limits
let minJointVal = JOINT_LIMITS[jointName]![0]
let maxJointVal = JOINT_LIMITS[jointName]![1]
if (newJointValue > maxJointVal) {
newJointValue = maxJointVal;
} else if (newJointValue < minJointVal) {
newJointValue = minJointVal;
}
}
let pose = { [jointName]: newJointValue }
if (!this.trajectoryClient) throw 'trajectoryClient is undefined';
return makePoseGoal(pose, this.trajectoryClient)
// let velocities = [{}]
// velocities[0][jointName] = jointValueInc
// let positions = [{}]
// positions[0][jointName] = newJointValue
// return makeVelocityGoal(positions, velocities, this.trajectoryClient)
}
setPanTiltFollowGripper(value: boolean) {
// Idempotent: setting same value has no effect
if (this.isWristFollowingActive === value) return;
this.isWristFollowingActive = value;
if (value) {
this.panOffset = 0;
this.tiltOffset = 0;
let lookIfReadyAndRepeat = () => {
if (this.linkGripperFingerLeftTF && this.linkHeadTiltTF) {
this.lookAtGripper(this.panOffset, this.tiltOffset);
}
this.lookAtGripperInterval = window.setTimeout(lookIfReadyAndRepeat, 500)
}
lookIfReadyAndRepeat()
} else {
clearTimeout(this.lookAtGripperInterval)
// this.lookAtGripperInterval = null
}
return true;
}
lookAtGripper(panOffset: number, tiltOffset: number) {
if (!this.linkGripperFingerLeftTF) throw 'linkGripperFingerLeftTF is undefined';
if (!this.linkHeadTiltTF) throw 'linkHeadTiltTF is undefined';
let gripperTF = this.linkGripperFingerLeftTF!
let tiltTF = this.linkHeadTiltTF!
let posDifference = {
x: gripperTF.translation.x - tiltTF.translation.x,
y: gripperTF.translation.y - tiltTF.translation.y,
z: gripperTF.translation.z - tiltTF.translation.z
};
// Normalize posDifference
const scalar = Math.sqrt(posDifference.x ** 2 + posDifference.y ** 2 + posDifference.z ** 2);
posDifference.x /= scalar;
posDifference.y /= scalar;
posDifference.z /= scalar;
const pan = Math.atan2(posDifference.y, posDifference.x) + this.panOffset;
const tilt = Math.atan2(posDifference.z, -posDifference.y) + this.tiltOffset;
if (!this.jointState) throw 'jointState is undefined';
let panDiff = Math.abs(getJointValue(this.jointState, "joint_head_pan") - pan);
let tiltDiff = Math.abs(getJointValue(this.jointState, "joint_head_tilt") - tilt);
// FIXME(nickswalker,2-1-22): Goals really close to current state cause some whiplash
// these joints in simulation. Ignoring small goals hacks around this for now
// console.log(panDiff, tiltDiff)
if (panDiff < 0.02 && tiltDiff < 0.02) {
return
}
if (!this.trajectoryClient) throw 'trajectoryClient is undefined';
let headFollowPoseGoal = makePoseGoal({
'joint_head_pan': pan + panOffset,
'joint_head_tilt': tilt + tiltOffset
}, this.trajectoryClient)
headFollowPoseGoal.send()
}
setPanTilt(pan: number, tilt: number) {
makePoseGoal({
'joint_head_pan': pan,
'joint_head_tilt': tilt
}, this.trajectoryClient!).send()
}
lookAtBase() {
this.setPanTilt(0.05, -1.4);
}
lookAtArm() {
this.setPanTilt(-1.5, -1.4);
}
gripperDeltaAperture(deltaWidthCm: number) {
// attempt to change the gripper aperture
try {
this.makeIncrementalMoveGoal('joint_gripper_finger_left', deltaWidthCm).send()
} catch (e) {
console.warn(e);
}
}
headTilt(angRad: number) {
if (this.isWristFollowingActive) {
this.tiltOffset += angRad;
} else {
try {
this.makeIncrementalMoveGoal('joint_head_tilt', angRad).send()
} catch (e) {
console.warn(e);
}
}
}
headPan(angRad: number) {
if (this.isWristFollowingActive) {
this.panOffset += angRad;
} else {
try {
this.makeIncrementalMoveGoal('joint_head_pan', angRad).send()
} catch (e) {
console.warn(e);
}
}
}
executeCommand(type: string, name: string, modifier: any[]) {
console.info(type, name, modifier)
this.commands[type][name](modifier)
}
executeIncrementalMove(jointName: ValidJoints, increment: number) {
this.stopExecution();
// this.moveBaseClient?.cancel();
// this.trajectoryClient?.cancel();
this.poseGoal = this.makeIncrementalMoveGoal(jointName, increment)
this.poseGoal.send()
// this.affirmExecution()
}
executeVelocityMove(jointName: ValidJoints, velocity: number) {
this.stopExecution();
let velocities: VelocityGoalArray = [{}, {}];
velocities[0][jointName] = velocity;
velocities[1][jointName] = velocity;
let positions: VelocityGoalArray = [{}, {}];
positions[0][jointName] = getJointValue(this.jointState!, jointName)
const jointLimit = JOINT_LIMITS[jointName];
if (!jointLimit) throw `Joint ${jointName} does not have limits`
positions[1][jointName] = jointLimit[Math.sign(velocity) === -1 ? 0 : 1]
this.velocityGoal = makeVelocityGoal(positions, velocities, this.trajectoryClient!)
this.velocityGoal.send()
this.affirmExecution()
}
setBaseMode(mode: "position" | "navigation") {
let request = new ROSLIB.ServiceRequest({});
if (mode === "position") {
this.setPositionMode?.callService(request, () => {
console.log("Set stretch to position mode");
})
} else if (mode === "navigation") {
this.setNavMode?.callService(request, () => {
console.log("Set stretch to navigation mode");
})
} else {
throw new Error("Invalid mode", mode)
}
}
executeBaseVelocity(linVel: number, angVel: number) {
let twist = new ROSLIB.Message({
linear: {
x: linVel,
y: 0,
z: 0
},
angular: {
x: 0,
y: 0,
z: angVel
}
});
this.cmdVel!.publish(twist);
}
affirmExecution() {
if (this.currentTrajectoryKillInterval) {
clearTimeout(this.currentTrajectoryKillInterval)
}
this.currentTrajectoryKillInterval = window.setTimeout(() => {
this.stopExecution()
}, 200)
}
stopExecution() {
this.trajectoryClient!.cancel()
// this.currentJointTrajectoryGoal = null
if (this.currentTrajectoryKillInterval) {
clearTimeout(this.currentTrajectoryKillInterval)
// this.currentTrajectoryKillInterval = null
}
this.moveBaseClient?.cancel()
if (this.poseGoal) {
this.poseGoal.cancel()
this.poseGoal = undefined
}
if (this.velocityGoal) {
this.velocityGoal.cancel()
this.velocityGoal = undefined
}
}
executeNavGoal(goal: NavGoalCommand) {
this.moveBaseClient?.cancel();
this.trajectoryClient?.cancel();
makeNavGoal(goal, this.moveBaseClient!, this, this.goalCallback).send()
}
executePoseGoal(pose: PoseGoalCommand) {
this.moveBaseClient?.cancel();
this.trajectoryClient?.cancel();
makeNamedPoseGoal(pose, this.trajectoryClient!, this, this.goalCallback).send();
}
}
function makeNamedPoseGoal(pose: PoseGoalCommand, trajectoryClient: ROSLIB.ActionClient, robot: Robot, goalCallback?: (goal: GoalMessage) => void) {
const goal = makePoseGoal(pose.goal.jointState, trajectoryClient);
goal.on('result', (result) => {
console.log(result);
if (goalCallback) {
goalCallback({
type: "goal",
name: "pose",
status: "success",
value: pose
})
}
});
let cancelCallbackFired = false;
goal.on("status", (status) => {
// Status Message Docs: http://docs.ros.org/en/fuerte/api/actionlib_msgs/html/msg/GoalStatus.html
// If status.status > 3, it was cancelled or failed
if (status.status > 3 && !cancelCallbackFired) {
cancelCallbackFired = true;
if (goalCallback) {
let callbackGoal: PoseGoalMessage = {
type: "goal",
name: "pose",
status: "failure",
value: { ...pose }
}
// Get the current positions of each of the joints that were part of this goal
const newJointStates: Pose = {};
Object.keys(pose.goal.jointState).forEach((jointName) => {
newJointStates[jointName as ValidJoints] = getJointValue(robot.jointState!, jointName as ValidJoints)
})
// Insert the current joint states into the goal while keeping everything else the same (id, name, etc.)
callbackGoal.value.goal.jointState = newJointStates;
goalCallback(callbackGoal)
}
}
})
return goal
}
function makePoseGoal(pose: Pose, trajectoryClient: ROSLIB.ActionClient, goalCallback?: (goal: GoalMessage) => void) {
let jointNames: ValidJoints[] = []
let jointPositions: number[] = []
for (let key in pose) {
jointNames.push(key as ValidJoints)
jointPositions.push(pose[key as ValidJoints]!)
}
let newGoal = new ROSLIB.Goal({
actionClient: trajectoryClient,
goalMessage: {
trajectory: {
header: {
stamp: {
secs: 0,
nsecs: 0
}
},
joint_names: jointNames,
points: [
{
positions: jointPositions,
// The following might causing the jumpiness in continuous motions
time_from_start: {
secs: 0,
nsecs: 1
}
}
]
}
}
});
newGoal.on('feedback', function (feedback) {
//console.log('Feedback: ' + feedback);
});
newGoal.on('result', function (result) {
console.log('Final Result: ' + result);
// if (goalCallback) {
// goalCallback({
// type: "pose",
// goal: pose
// })
// }
});
return newGoal
}
function makeVelocityGoal(positions: VelocityGoalArray, velocities: VelocityGoalArray, trajectoryClient: ROSLIB.ActionClient, goalCallback?: (goal: GoalMessage) => void) {
let points = [];
let jointNames;
for (let i = 0; i < positions.length; i++) {
let positionsT = positions[i];
let velocitiesT = velocities[i];
let positionsOut = [];
let velocitiesOut = [];
let names: [ValidJoints?] = [];
for (let key in positionsT) {
// Make sure that typescript knows that key will be a valid key
const typedKey = key as ValidJoints;
names.push(typedKey);
positionsOut.push(positionsT[typedKey]);
velocitiesOut.push(velocitiesT[typedKey]);
}
points.push({
positions: positionsOut, velocities: velocitiesOut, time_from_start: {
secs: i * 60,
nsecs: 1
}
});
jointNames = names;
}
let newGoal = new ROSLIB.Goal({
actionClient: trajectoryClient,
goalMessage: {
trajectory: {
header: {
stamp: {
secs: 0,
nsecs: 0
}
},
joint_names: jointNames,
points: points
}
}
});
newGoal.on('feedback', function (feedback) {
//console.log('Feedback: ', feedback);
});
newGoal.on('result', function (result) {
console.log('Final Result: ', result);
// if (goalCallback) {
// goalCallback({
// type: "velocity",
// goal: {
// positions: positions,
// velocities: velocities
// }
// })
// }
});
//this.velocityGoal = newGoal;
return newGoal;
}
function makeNavGoal(goal: NavGoalCommand, moveBaseClient: ROSLIB.ActionClient, robot: Robot, goalCallback?: (goal: GoalMessage) => void) {
let newGoal = new ROSLIB.Goal({
actionClient: moveBaseClient,
goalMessage: {
target_pose: {
header: {
stamp: {
secs: 0,
nsecs: 0
},
frame_id: 'map'
},
pose: {
position: {
x: goal.goal.x,
y: goal.goal.y,
z: 0
},
orientation: eulerToQuaternion(goal.goal.theta ? goal.goal.theta : 0, 0, 0)
}
}
}
});
newGoal.on('feedback', function (feedback) {
// console.log('Feedback:');
// console.log(feedback)
});
newGoal.on('result', function (result) {
console.log('Final Result:', result);
if (goalCallback) {
goalCallback({
type: "goal",
name: "nav",
status: "success",
value: goal
})
}
});
let cancelCallbackFired = false;
newGoal.on("status", (status) => {
// Status Message Docs: http://docs.ros.org/en/fuerte/api/actionlib_msgs/html/msg/GoalStatus.html
// If status.status > 3, it was cancelled or failed
if (status.status > 3 && !cancelCallbackFired) {
console.log(goal.id, status.status);
cancelCallbackFired = true;
if (goalCallback) {
let callbackGoal: NavGoalMessage = {
type: "goal",
name: "nav",
status: "failure",
value: { ...goal }
}
callbackGoal.value.goal = {
x: robot.baseTF?.translation.x!,
y: robot.baseTF?.translation.y!,
theta: quaternionToEuler(robot.baseTF?.rotation!, "YZX").z
}
goalCallback(callbackGoal)
}
}
})
return newGoal
}
export function getJointEffort(jointStateMessage: ROSJointState, jointName: ValidJoints) {
let jointIndex = jointStateMessage.name.indexOf(jointName)
return jointStateMessage.effort[jointIndex]
}
export function getJointValue(jointStateMessage: ROSJointState, jointName: ValidJoints): number {
// Paper over Hello's fake joint implementation
if (jointName === "wrist_extension") {
return getJointValue(jointStateMessage, "joint_arm_l0") +
getJointValue(jointStateMessage, "joint_arm_l1") +
getJointValue(jointStateMessage, "joint_arm_l2") +
getJointValue(jointStateMessage, "joint_arm_l3")
} else if (jointName === "translate_mobile_base" || jointName === "rotate_mobile_base") {
return 0
}
let jointIndex = jointStateMessage.name.indexOf(jointName)
return jointStateMessage.position[jointIndex]
}
export function inJointLimits(jointStateMessage: ROSJointState, jointName: ValidJoints) : [boolean, boolean] {
let jointValue = getJointValue(jointStateMessage, jointName)
let jointLimits = JOINT_LIMITS[jointName]!
let eps = 0.05
let inLimits: [boolean, boolean] = [false, false]
inLimits[0] = jointValue - eps >= jointLimits[0]
inLimits[1] = jointValue + eps <= jointLimits[1]
return inLimits
}
// Modified from: https://math.stackexchange.com/a/2975462
function eulerToQuaternion(yaw: number, pitch: number, roll: number) {
const qx = Math.sin(roll / 2) * Math.cos(pitch / 2) * Math.cos(yaw / 2) - Math.cos(roll / 2) * Math.sin(pitch / 2) * Math.sin(yaw / 2)
const qy = Math.cos(roll / 2) * Math.sin(pitch / 2) * Math.cos(yaw / 2) + Math.sin(roll / 2) * Math.cos(pitch / 2) * Math.sin(yaw / 2)
const qz = Math.cos(roll / 2) * Math.cos(pitch / 2) * Math.sin(yaw / 2) - Math.sin(roll / 2) * Math.sin(pitch / 2) * Math.cos(yaw / 2)
const qw = Math.cos(roll / 2) * Math.cos(pitch / 2) * Math.cos(yaw / 2) + Math.sin(roll / 2) * Math.sin(pitch / 2) * Math.sin(yaw / 2)
return {
x: qx,
y: qy,
z: qz,
w: qw
}
}