|  | 
|  | 1 | +'use strict'; | 
|  | 2 | + | 
|  | 3 | +let rosnodejs = require('./index.js'); | 
|  | 4 | +const ActionClient = require('./lib/ActionClient.js'); | 
|  | 5 | + | 
|  | 6 | +rosnodejs.initNode('/my_node', { | 
|  | 7 | +  messages: [ | 
|  | 8 | +    'rosgraph_msgs/Log', // required for new logging approach | 
|  | 9 | +    'turtlesim/Pose', | 
|  | 10 | +    'turtle_actionlib/ShapeActionGoal', | 
|  | 11 | +    'turtle_actionlib/ShapeActionFeedback', | 
|  | 12 | +    'turtle_actionlib/ShapeActionResult', | 
|  | 13 | +    'geometry_msgs/Twist', | 
|  | 14 | +    'actionlib_msgs/GoalStatusArray', | 
|  | 15 | +    'actionlib_msgs/GoalID' | 
|  | 16 | +  ], | 
|  | 17 | +  services: ['std_srvs/SetBool', "turtlesim/TeleportRelative"] | 
|  | 18 | +}).then((rosNode) => { | 
|  | 19 | + | 
|  | 20 | +  // console.log(new (rosnodejs.require('rosgraph_msgs').msg.Log)()); | 
|  | 21 | +   | 
|  | 22 | + | 
|  | 23 | +  // --------------------------------------------------------- | 
|  | 24 | +  // Service Call | 
|  | 25 | + | 
|  | 26 | +  const TeleportRelative = rosnodejs.require('turtlesim').srv.TeleportRelative; | 
|  | 27 | +  const teleport_request = new TeleportRelative.Request({ | 
|  | 28 | +    linear: 0.1,  | 
|  | 29 | +    angular: 0.0 | 
|  | 30 | +  }); | 
|  | 31 | + | 
|  | 32 | +  let serviceClient2 = rosNode.serviceClient("/turtle1/teleport_relative",  | 
|  | 33 | +                                             "turtlesim/TeleportRelative"); | 
|  | 34 | +  rosNode.waitForService(serviceClient2.getService(), 2000) | 
|  | 35 | +    .then((available) => { | 
|  | 36 | +      if (available) { | 
|  | 37 | +        serviceClient2.call(teleport_request, (resp) => { | 
|  | 38 | +          console.log('Service response ' + JSON.stringify(resp)); | 
|  | 39 | +        }); | 
|  | 40 | +      } else { | 
|  | 41 | +        console.log('Service not available'); | 
|  | 42 | +      } | 
|  | 43 | +    }); | 
|  | 44 | + | 
|  | 45 | + | 
|  | 46 | +  // --------------------------------------------------------- | 
|  | 47 | +  // Subscribe | 
|  | 48 | +  rosNode.subscribe( | 
|  | 49 | +    '/turtle1/pose',  | 
|  | 50 | +    'turtlesim/Pose', | 
|  | 51 | +    (data) => { | 
|  | 52 | +      console.log('pose', data); | 
|  | 53 | +    }, | 
|  | 54 | +    {queueSize: 1, | 
|  | 55 | +     throttleMs: 1000}); | 
|  | 56 | + | 
|  | 57 | +  // --------------------------------------------------------- | 
|  | 58 | +  // Publish | 
|  | 59 | +  // equivalent to:  | 
|  | 60 | +  //   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 | 
|  | 62 | +  let cmd_vel = rosNode.advertise('/turtle1/cmd_vel','geometry_msgs/Twist', { | 
|  | 63 | +    queueSize: 1, | 
|  | 64 | +    latching: true, | 
|  | 65 | +    throttleMs: 9 | 
|  | 66 | +  }); | 
|  | 67 | + | 
|  | 68 | +  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); | 
|  | 79 | +  cmd_vel.publish(msgTwist); | 
|  | 80 | + | 
|  | 81 | +  // cmd_vel.on('connection', function(s) { | 
|  | 82 | +  //   console.log("connected", s); | 
|  | 83 | +  // }); | 
|  | 84 | + | 
|  | 85 | + | 
|  | 86 | +  // --------------------------------------------------------- | 
|  | 87 | +  // test actionlib | 
|  | 88 | +  // rosrun turtlesim turtlesim_node | 
|  | 89 | +  // rosrun turtle_actionlib shape_server | 
|  | 90 | + | 
|  | 91 | +  let pub_action =  | 
|  | 92 | +    rosNode.advertise('/turtle_shape/goal', 'turtle_actionlib/ShapeActionGoal', { | 
|  | 93 | +      queueSize: 1, | 
|  | 94 | +      latching: true, | 
|  | 95 | +      throttleMs: 9 | 
|  | 96 | +    }); | 
|  | 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 | + | 
|  | 140 | + | 
|  | 141 | +}); | 
0 commit comments