1+ /**
2+ An example of using rosnodejs with turtlesim, incl. services,
3+ pub/sub, and actionlib. This example uses the on-demand generated
4+ messages.
5+ */
6+
17'use strict' ;
28
39let rosnodejs = require ( './index.js' ) ;
410const ActionClient = require ( './lib/ActionClient.js' ) ;
511
612rosnodejs . initNode ( '/my_node' , {
713 messages : [
8- 'rosgraph_msgs/Log' , // required for new logging approach
914 'turtlesim/Pose' ,
1015 'turtle_actionlib/ShapeActionGoal' ,
1116 'turtle_actionlib/ShapeActionFeedback' ,
1217 'turtle_actionlib/ShapeActionResult' ,
1318 'geometry_msgs/Twist' ,
14- 'actionlib_msgs/GoalStatusArray' ,
15- 'actionlib_msgs/GoalID'
1619 ] ,
17- services : [ 'std_srvs/SetBool' , "turtlesim/TeleportRelative" ]
20+ services : [ "turtlesim/TeleportRelative" ]
1821} ) . then ( ( rosNode ) => {
1922
20- // console.log(new (rosnodejs.require('rosgraph_msgs').msg.Log)());
21-
22-
2323 // ---------------------------------------------------------
2424 // Service Call
2525
2626 const TeleportRelative = rosnodejs . require ( 'turtlesim' ) . srv . TeleportRelative ;
2727 const teleport_request = new TeleportRelative . Request ( {
28- linear : 0. 1,
28+ linear : - 1 ,
2929 angular : 0.0
3030 } ) ;
3131
32- let serviceClient2 = rosNode . serviceClient ( "/turtle1/teleport_relative" ,
32+ let serviceClient = rosNode . serviceClient ( "/turtle1/teleport_relative" ,
3333 "turtlesim/TeleportRelative" ) ;
34- rosNode . waitForService ( serviceClient2 . getService ( ) , 2000 )
34+
35+ rosNode . waitForService ( serviceClient . getService ( ) , 2000 )
3536 . then ( ( available ) => {
3637 if ( available ) {
37- serviceClient2 . call ( teleport_request , ( resp ) => {
38+ serviceClient . call ( teleport_request , ( resp ) => {
3839 console . log ( 'Service response ' + JSON . stringify ( resp ) ) ;
3940 } ) ;
4041 } else {
@@ -58,84 +59,38 @@ rosnodejs.initNode('/my_node', {
5859 // Publish
5960 // equivalent to:
6061 // rostopic pub /turtle1/cmd_vel geometry_msgs/Twist '[1, 0, 0]' '[0, 0, 0]'
61- // sudo tcpdump -ASs 0 -i lo | tee tmp/rostopic.dump
6262 let cmd_vel = rosNode . advertise ( '/turtle1/cmd_vel' , 'geometry_msgs/Twist' , {
6363 queueSize : 1 ,
6464 latching : true ,
6565 throttleMs : 9
6666 } ) ;
6767
6868 const Twist = rosnodejs . require ( 'geometry_msgs' ) . msg . Twist ;
69- const msgTwist = new Twist ( ) ;
70- msgTwist . linear = new ( rosnodejs . require ( 'geometry_msgs' ) . msg . Vector3 ) ( ) ;
71- msgTwist . linear . x = 1 ;
72- msgTwist . linear . y = 0 ;
73- msgTwist . linear . z = 0 ;
74- msgTwist . angular = new ( rosnodejs . require ( 'geometry_msgs' ) . msg . Vector3 ) ( ) ;
75- msgTwist . angular . x = 0 ;
76- msgTwist . angular . y = 0 ;
77- msgTwist . angular . z = 0 ;
78- // console.log("Twist", msgTwist);
69+ const msgTwist = new Twist ( {
70+ linear : { x : 1 , y : 0 , z : 0 } ,
71+ angular : { x : 0 , y : 0 , z : 1 }
72+ } ) ;
7973 cmd_vel . publish ( msgTwist ) ;
8074
81- // cmd_vel.on('connection', function(s) {
82- // console.log("connected", s);
83- // });
84-
8575
8676 // ---------------------------------------------------------
8777 // test actionlib
8878 // rosrun turtlesim turtlesim_node
8979 // rosrun turtle_actionlib shape_server
9080
91- let pub_action =
92- rosNode . advertise ( '/turtle_shape/goal' , 'turtle_actionlib/ShapeActionGoal' , {
93- queueSize : 1 ,
94- latching : true ,
95- throttleMs : 9
81+ // wait two seconds for previous example to complete
82+ setTimeout ( function ( ) {
83+ let shapeActionGoal = rosnodejs . require ( 'turtle_actionlib' ) . msg . ShapeActionGoal ;
84+ let ac = new ActionClient ( {
85+ type : "turtle_actionlib/ShapeAction" ,
86+ actionServer : "/turtle_shape"
9687 } ) ;
97-
98- let shapeActionGoal = rosnodejs . require ( 'turtle_actionlib' ) . msg . ShapeActionGoal ;
99- // console.log("shapeMsgGoal", shapeActionGoal);
100- var now = Date . now ( ) ;
101- var secs = parseInt ( now / 1000 ) ;
102- var nsecs = ( now % 1000 ) * 1000 ;
103- let shapeMsg = new shapeActionGoal ( {
104- header : {
105- seq : 0 ,
106- stamp : new Date ( ) ,
107- frame_id : ''
108- } ,
109- goal_id : {
110- stamp : new Date ( ) ,
111- id : "/my_node-1-" + secs + "." + nsecs + "000"
112- } ,
113- goal : {
114- edges : 5 ,
115- radius : 1
116- }
117- } ) ;
118-
119- // console.log("shapeMsg", shapeMsg);
120- pub_action . publish ( shapeMsg ) ;
121-
122-
123- // ---- Same with ActionClient:
124- // console.log("start");
125- // let ac = new ActionClient({
126- // type: "turtle_actionlib/ShapeAction",
127- // actionServer: "turtle_shape"
128- // });
129- // // console.log(ac);
130- // // ac.sendGoal(new shapeActionGoal({
131- // // goal: {
132- // // edges: 5,
133- // // radius: 1
134- // // }
135- // // }));
136- // ac.sendGoal(shapeMsg);
137-
138- console . log ( "\n** done\n" ) ;
139-
88+ ac . sendGoal ( new shapeActionGoal ( {
89+ goal : {
90+ edges : 3 ,
91+ radius : 1
92+ }
93+ } ) ) ;
94+ } , 2000 ) ;
14095
14196} ) ;
0 commit comments