ROS的roslibjs基本功能使用测试

Posted scruffybear

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ROS的roslibjs基本功能使用测试相关的知识,希望对你有一定的参考价值。

文章目录

小结

成功测试了roslibjs的基本功能。

进行roslibjs的安装

安装nodejs

john@ubuntu:~$ sudo apt-get install nodejs

john@ubuntu:~$ sudo apt-get install npm

安装roslibjs

john@ubuntu:~/roslibjs$ pwd
/home/john/roslibjs
john@ubuntu:~/roslibjs$ 
john@ubuntu:~$ git clone https://github.com/RobotWebTools/roslibjs.git
john@ubuntu:~/roslibjs$ npm install
#提示需要执行npm audit fix
john@ubuntu:~/roslibjs$ npm audit fix
#重新安装
john@ubuntu:~/roslibjs$ npm install

这里的主要测试html的代码是simple.html, 参考https://github.com/RobotWebTools/roslibjs/blob/develop/examples/simple.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="https://static.robotwebtools.org/EventEmitter2/current/eventemitter2.min.js"></script>
<script src="../build/roslib.js"></script>

<script>
  // Connecting to ROS
  // -----------------
  var ros = new ROSLIB.Ros();

  // If there is an error on the backend, an 'error' emit will be emitted.
  ros.on('error', function(error) 
    document.getElementById('connecting').style.display = 'none';
    document.getElementById('connected').style.display = 'none';
    document.getElementById('closed').style.display = 'none';
    document.getElementById('error').style.display = 'inline';
    console.log(error);
  );

  // Find out exactly when we made a connection.
  ros.on('connection', function() 
    console.log('Connection made!');
    document.getElementById('connecting').style.display = 'none';
    document.getElementById('error').style.display = 'none';
    document.getElementById('closed').style.display = 'none';
    document.getElementById('connected').style.display = 'inline';
  );

  ros.on('close', function() 
    console.log('Connection closed.');
    document.getElementById('connecting').style.display = 'none';
    document.getElementById('connected').style.display = 'none';
    document.getElementById('closed').style.display = 'inline';
  );

  // Create a connection to the rosbridge WebSocket server.
  ros.connect('ws://localhost:9090');

  // Publishing a Topic
  // ------------------

  // First, we create a Topic object with details of the topic's name and message type.
  var cmdVel = new ROSLIB.Topic(
    ros : ros,
    name : '/cmd_vel',
    messageType : 'geometry_msgs/Twist'
  );

  // Then we create the payload to be published. The object we pass in to ros.Message matches the
  // fields defined in the geometry_msgs/Twist.msg definition.
  var twist = new ROSLIB.Message(
    linear : 
      x : 0.1,
      y : 0.2,
      z : 0.3
    ,
    angular : 
      x : -0.1,
      y : -0.2,
      z : -0.3
    
  );

  // And finally, publish.
  cmdVel.publish(twist);

  //Subscribing to a Topic
  //----------------------

  // Like when publishing a topic, we first create a Topic object with details of the topic's name
  // and message type. Note that we can call publish or subscribe on the same topic object.
  var listener = new ROSLIB.Topic(
    ros : ros,
    name : '/listener',
    messageType : 'std_msgs/String'
  );

  // Then we add a callback to be called every time a message is published on this topic.
  listener.subscribe(function(message) 
    console.log('Received message on ' + listener.name + ': ' + message.data);

    // If desired, we can unsubscribe from the topic as well.
    listener.unsubscribe();
  );

  // Calling a service
  // -----------------

  // First, we create a Service client with details of the service's name and service type.
  var addTwoIntsClient = new ROSLIB.Service(
    ros : ros,
    name : '/add_two_ints',
    serviceType : 'rospy_tutorials/AddTwoInts'
  );

  // Then we create a Service Request. The object we pass in to ROSLIB.ServiceRequest matches the
  // fields defined in the rospy_tutorials AddTwoInts.srv file.
  var request = new ROSLIB.ServiceRequest(
    a : 1,
    b : 2
  );

  // Finally, we call the /add_two_ints service and get back the results in the callback. The result
  // is a ROSLIB.ServiceResponse object.
  addTwoIntsClient.callService(request, function(result) 
    console.log('Result for service call on ' + addTwoIntsClient.name + ': ' + result.sum);
  );

  // Advertising a Service
  // ---------------------

  // The Service object does double duty for both calling and advertising services
  var setBoolServer = new ROSLIB.Service(
    ros : ros,
    name : '/set_bool',
    serviceType : 'std_srvs/SetBool'
  );

  // Use the advertise() method to indicate that we want to provide this service
  setBoolServer.advertise(function(request, response) 
    console.log('Received service request on ' + setBoolServer.name + ': ' + request.data);
    response['success'] = true;
    response['message'] = 'Set successfully';
    return true;
  );

  // Setting a param value
  // ---------------------

  ros.getParams(function(params) 
    console.log(params);
  );

  // First, we create a Param object with the name of the param.
  var maxVelX = new ROSLIB.Param(
    ros : ros,
    name : 'max_vel_y'
  );

  //Then we set the value of the param, which is sent to the ROS Parameter Server.
  maxVelX.set(0.8);
  maxVelX.get(function(value) 
    console.log('MAX VAL: ' + value);
  );

  // Getting a param value
  // ---------------------

  var favoriteColor = new ROSLIB.Param(
    ros : ros,
    name : 'favorite_color'
  );

  favoriteColor.set('red');
  favoriteColor.get(function(value) 
    console.log('My robot\\'s favorite color is ' + value);
  );
</script>
</head>

<body>
  <h1>Simple roslib Example</h1>
  <p>Run the following commands in the terminal then refresh this page. Check the javascript
    console for the output.</p>
  <ol>
    <li><tt>roscore</tt></li>
    <li><tt>rostopic pub /listener std_msgs/String "Hello, World"</tt></li>
    <li><tt>rostopic echo /cmd_vel</tt></li>
    <li><tt>rosrun rospy_tutorials add_two_ints_server</tt></li>
    <li><tt>roslaunch rosbridge_server rosbridge_websocket.launch</tt></li>
  </ol>
  <div id="statusIndicator">
    <p id="connecting">
      Connecting to rosbridge...
    </p>
    <p id="connected" style="color:#00D600; display:none">
      Connected
    </p>
    <p id="error" style="color:#FF0000; display:none">
      Error in the backend!
    </p>
    <p id="closed" style="display:none">
      Connection closed.
    </p>
  </div>
</body>
</html>

进行测试

将文件file:///home/john/roslibjs/examples/simple.html拖入到Firefox中去,打开后Firefox窗口中有如下显示:

Simple roslib Example

  1. roscore
  2. rostopic pub /listener std_msgs/String “Hello, World”
  3. rostopic echo /cmd_vel
  4. rosrun rospy_tutorials add_two_ints_server
  5. roslaunch rosbridge_server rosbridge_websocket.launch

Connected

经过测试后,命令行的输出如下(开不同的窗口):
启动roscore:

john@ubuntu:~$ roscore
... logging to /home/john/.ros/log/9ce7d29e-119b-11ed-ad09-9d6885123387/roslaunch-ubuntu-14886.log
Checking log directory for disk usage. This may take a while.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

started roslaunch server http://ubuntu:45603/
ros_comm version 1.15.14


SUMMARY
========

PARAMETERS
 * /rosdistro: noetic
 * /rosversion: 1.15.14

NODES

auto-starting new master
process[master]: started with pid [14895]
ROS_MASTER_URI=http://ubuntu:11311/

setting /run_id to 9ce7d29e-119b-11ed-ad09-9d6885123387
process[rosout-1]: started with pid [14906]
started core service [/rosout]

rostopic pub /listener std_msgs/String "Hello, World"输出:

john@ubuntu:~$ rostopic pub /listener std_msgs/String "Hello, World"
publishing and latching message. Press ctrl-C to terminate

监测/cmd_vel的输出,接收simple.html的输入:

john@ubuntu:~$ rostopic echo /cmd_vel
WARNING: topic [/cmd_vel] does not appear to be published yet
linear: 
  x: 0.1
  y: 0.2
  z: 0.3
angular: 
  x: -0.1
  y: -0.2
  z: -0.3
---

`roslaunch rosbridge_server rosbridge_websocket.launch`的输出:
```shell
john@ubuntu:~$ roslaunch rosbridge_server rosbridge_websocket.launch
... logging to /home/john/.ros/log/9ce7d29e-119b-11ed-ad09-9d6885123387/roslaunch-ubuntu-15411.log
Checking log directory for disk usage. This may take a while.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

started roslaunch server http://ubuntu:43691/

SUMMARY
========

PARAMETERS
 * /rosapi/params_glob: [*]
 * /rosapi/services_glob: [*]
 * /rosapi/topics_glob: [*]
 * /rosbridge_websocket/address: 0.0.0.0
 * /rosbridge_websocket/authenticate: False
 * /rosbridge_websocket/bson_only_mode: False
 * /rosbridge_websocket/delay_between_messages: 0
 * /rosbridge_websocket/fragment_timeout: 600
 * /rosbridge_websocket/max_message_size: None
 * /rosbridge_websocket/params_glob: [*]
 * /rosbridge_websocket/port: 9090
 * /rosbridge_websocket/retry_startup_delay: 5
 * /rosbridge_websocket/services_glob: [*]
 * /rosbridge_websocket/topics_glob: [*]
 * /rosbridge_websocket/unregister_timeout: 10
 * /rosbridge_websocket/use_compression: False
 * /rosbridge_websocket/websocket_external_port: None
 * /rosbridge_websocket/websocket_ping_interval: 0
 * /rosbridge_websocket/websocket_ping_timeout: 30
 * /rosdistro: noetic
 * /rosversion: 1.15.14

NODES
  /
    rosapi (rosapi/rosapi_node)
    rosbridge_websocket (rosbridge_server/rosbridge_websocket)

ROS_MASTER_URI=http://localhost:11311

process[rosbridge_websocket-1]: started with pid [15432]
process[rosapi-2]: started with pid [15434]
2022-08-01 21:13:32+0800 [-] Log opened.
[INFO] [1659359612.771473]: Rosapi started
2022-08-01 21:13:32+0800 [-] registered capabilities (classes):
2022-08-01 21:13:32+0800 [-]  - <class 'rosbridge_library.capabilities.call_service.CallService'>
2022-08-01 21:13:32+0800 [-]  - <class 'rosbridge_library.capabilities.advertise.Advertise'>
2022-08-01 21:13:32+0800 [-]  - <class 'rosbridge_library.capabilities.publish.Publish'>
2022-08-01 21:13:32+0800 [-]  - <class 'rosbridge_library.capabilities.subscribe.Subscribe'>
2022-08-01 21:13:32+0800 [-]  - <class 'rosbridge_library.capabilities.defragmentation.Defragment'>
2022-08-01 21:13:32+0800 [-]  - <class 'rosbridge_library.capabilities.advertise_service.AdvertiseService'>
2022-08-01 21:13:32+0800 [-]  - <class 'rosbridge_library.capabilities.service_response.ServiceResponse'>
2022-08-01 21:13:32+0800 [-]  - <class 'rosbridge_library.capabilities.unadvertise_service.UnadvertiseService'>
2022-08-01 21:13:33+0800 [-] WebSocketServerFactory starting on 9090
2022-08-01 21:13:33+0800 [-] Starting factory <autobahn.twisted.websocket.WebSocketServerFactory object at 0x7fe5550d17f0>
2022-08-01 21:13:33+0800 [-] [INFO] [1659359613.034557]: Rosbridge WebSocket server started at ws://0.0.0.0:9090
2022-08-01 21:43:13+0800 [-] [INFO] [1659361393.971597]: Client connected.  1 clients total.
2022-08-01 21:43:14+0800 [-] [ERROR] [1659361394.318899]: [Client 0] [id: advertise:/tf2_web_republisher/goal:1] advertise: Unable to load the manifest for package tf2_web_republisher. Caused by: tf2_web_republisher
2022-08-01 21:43:14+0800 [-] ROS path [0]=/opt/ros/noetic/share/ros
2022-08-01 21:43:14+0800 [-] ROS path [1]=/home/john/jetmax/src
2022-08-01 21:43:14+0800 [-] ROS path [2]=/home/john/catkin_ws/src
2022-08-01 21:43:14+0800 [-] ROS path [3]=/opt/ros/noetic/share
2022-08-01 21:43:15+0800 [-] [ERROR] [1659361395.049878]: [Client 0] [id: subscribe:/tf2_web_republisher/feedback:3] subscribe: Unable to load the manifest for package tf2_web_republisher. Caused by: tf2_web_republisher
2022-08-01 21:43:15+0800 [-] ROS path [0]=/opt/ros/noetic/share/ros
2022-08-01 21:43:15+0800 [-] ROS path [1]=/home/john/jetmax/src
2022-08-01 21:43:15+0800 [-] ROS path [2]=/home/john/catkin_ws/src
2022-08-01 21:43:15+0800 [-] ROS path [3]=/opt/ros/noetic/share
2022-08-01 21:43:15+0800 [-] [ERROR] [1659361395.054902]: [Client 0] [id: publish:/tf2_web_republisher/goal:4] publish: Cannot infer topic type for topic /tf2_web_republisher/goal as it is not yet advertised
2022-08-01 21:43:20+0800 [-] [INFO] [1659361400.824869]: Client disconnected. 0 clients total.
2022-08-01 21:43:21+0800 [-] [INFO] [1659361401.006295]: Client connected.  1 clients total.
2022-08-01 21:43:21+0800 [-] [ERROR] [1659361401.142243]: [Client 1] [id: advertise:/tf2_web_republisher/goal:1] advertise: Unable to load the manifest for package tf2_web_republisher. Caused by: tf2_web_republisher
2022-08-01 21:43:21+0800 [-] ROS path [0]=/opt/ros/noetic/share/ros
2022-08-01 21:43:21+0800 [-] ROS path [1]=/home/john/jetmax/src
2022-08-01 21:43:21+0800 [-] ROS path [2]=/home/john/catkin_ws/src
2022-08-01 21:43:21+0800 [-] ROS path [3]=/opt/ros/noetic/share
2022-08-01 21:43:21+0800 [-] [ERROR] [1659361401.146786]: [Client 1] [id: subscribe:/tf2_web_republisher/feedback:3] subscribe: Unable to load the manifest for package tf2_web_republisher. Caused by: tf2_web_republisher
2022-08-01 21:43:21+0800 [-] ROS path [0]=/opt/ros/noetic/share/ros
2022-08-01 21:43:21+0800 [-] ROS path [1]=/home/john/jetmax/src
2022-08-01 21:43:21+0800 [-] ROS path [2]=/home/john/catkin_ws/src
2022-08-01 21:43:21+0800 [-] ROS path [3]=/opt/ros/noetic/share
2022-08-01 21:43:21+0800 [-] [ERROR] [1659361401.154688]: [Client 1] [id: publish:/tf2_web_republisher/goal:4] publish: Cannot infer topic type for topic /tf2_web_republisher/goal as it is not yet advertised
2022-08-01 21:45:54+0800 [-] [INFO] [1659361554.937012]: Client disconnected. 0 clients total.
2022-08-01 21:45:55+0800 [-] [INFO] [1659361555.125830]: Client connected.  1 clients total.
2022-08-01 21:45:55+0800 [-] [ERROR] [1659361555.218223]: [Client 2] [id: advertise:/tf2_web_republisher/goal:1] advertise: Unable to load the manifest for package tf2_web_republisher. Caused by: tf2_web_republisher
2022-08-01 21:45:55+0800 [-] ROS path [0]=/opt/ros/noetic/share/ros
2022-08-01 21:45:55+0800 [-] ROS path [1]=/home/john/jetmax/src
2022-08-01 21:45:55+0800 [-] ROS path [2]=/home/john/catkin_ws/src
2022-08-01 21:45:55+0800 [-] ROS path [3]=/opt/ros/noetic/share
2022-08-01 21:45:55+0800 [-] [ERROR] [1659361555.223203]: [Client 2] [id: subscribe:/tf2_web_republisher/feedback:3] subscribe: Unable to load the manifest for package tf2_web_republisher. Caused by: tf2_web_republisher
2022-08-01 21:45:55+0800 [-] ROS path [0]=/opt/ros/noetic/share/ros
2022-08-01 21:45:55+0800 [-] ROS path [1]=/home/john/jetmax/src
2022-08-01 21:45:55+0800 [-] ROS path [2]=/home/john/catkin_ws/src
2022-08-01 21:45:55+0800 [-] ROS path [3]=/opt/ros/noetic/share
2022-08-01 21:45:55+0800 [-] [ERROR] [1659361555.227773]: [Client 2] [id: publish:/tf2_web_republisher/goal:4] publish: Cannot infer topic type for topic /tf2_web_republisher/goal as it is not yet advertised
2022-08-01 21:46:22+0800 [-] [INFO] [1659361582.056263]: Client disconnected. 0 clients total.
2022-08-01 21:46:26+0800 [-] [INFO] [1659361586.740728]: Client connected.  1 clients total.
2022-08-01 21:46:26+0800 [-] [INFO] [1659361586.873740]: [Client 3] Subscribed to /listener
2022-08-01 21:46:26+0800 [-] [INFO] [1659361586.910506]: [Client 3] Advertised service /set_bool.
2022-08-01 21:46:26+0800 [-] [INFO] [1659361586.959539]: [Client 3] Unsubscribed from /listener
2022-08-01 21:50:56+0800 [-] [INFO] [1659361856.983826]: Client disconnected. 0 clients total.
2022-08-01 21:50:57+0800 [-] [INFO] [1659361857.427746]: Client connected.  1 clients total.
2022-08-01 21:50:57+0800 [-] [INFO] [1659361857.462512]: [Client 4] Subscribed to /listener
2022-08-01 21:50:57+0800 [-] [WARN] [1659361857.465272]: [Client 4] Duplicate service advertised. Overwriting /set_bool.
2022-08-01 21:50:57+0800 [-] [INFO] [1659361857.481433]: [Client 4] Advertised service /set_bool.
2022-08-01 21:50:57+0800 [-] [INFO] [1659361857.527972]: [Client 4] Unsubscribed from /listener

rosrun rospy_tutorials add_two_ints_server的输出,进行了1+2=3的服务调用:

john@ubuntu:~$ rosrun rospy_tutorials add_two_ints_server
Returning [1 + 2 = 3]

Ctrl + Shift + K 打开Firefox的Console窗口,在Firefox的console窗口里有以下输出,与simple.html的源代码匹配,测试成功。

Connection closed. simple.html:32:13
Connection made! simple.html:24:13
Result for service call on /add_two_ints: 3 simple.html:108:13
Received message on /listener: Hello, World simple.html:82:13
Array(27) [ "/run_id", "/roslaunch/uris/host_ubuntu__45603", "/roslaunch/uris/host_ubuntu__43691", "/rosversion", "/rosdistro", "/rosbridge_websocket/authenticate", "/rosbridge_websocket/port", "/rosbridge_websocket/address", "/rosbridge_websocket/retry_startup_delay", "/rosbridge_websocket/fragment_timeout", … ]0: "/run_id"1: "/roslaunch/uris/host_ubuntu__45603"2: "/roslaunch/uris/host_ubuntu__43691"3: "/rosversion"4: "/rosdistro"5: "/rosbridge_websocket/authenticate"6: "/rosbridge_websocket/port"7: "/rosbridge_websocket/address"8: "/rosbridge_websocket/retry_startup_delay"9: "/rosbridge_websocket/fragment_timeout"10: "/rosbridge_websocket/delay_between_messages"11: "/rosbridge_websocket/max_message_size"12: "/rosbridge_websocket/unregister_timeout"13: "/rosbridge_websocket/use_compression"14: "/rosbridge_websocket/websocket_ping_interval"15: "/rosbridge_websocket/websocket_ping_timeout"16: "/rosbridge_websocket/websocket_external_port"17: "/rosbridge_websocket/topics_glob"18: "/rosbridge_websocket/services_glob"19: "/rosbridge_websocket/params_glob"20: "/rosbridge_websocket/bson_only_mode"21: "/rosbridge_websocket/actual_port"22: "/rosapi/topics_glob"23: "/rosapi/services_glob"24: "/rosapi/params_glob"25: "/favorite_color"26: "/max_vel_y"
​
length: 27<prototype>: Array []
simple.html:133:13
MAX VAL: 0.8 simple.html:145:13
My robot's favorite color is red simple.html:158:13

参考

解决在Ubuntu安装nodejs,roslibjs时出现的问题
关于js和ros进行交互roslibjs
Basic ROS functionality with roslibjs
https://github.com/RobotWebTools/roslibjs/blob/develop/examples/simple.html
SIMPLE TUTORIAL ON ROSBRIDGE AND ROSLIBJS
ROS与javascript入门教程-roslibjs-基本功能

以上是关于ROS的roslibjs基本功能使用测试的主要内容,如果未能解决你的问题,请参考以下文章

如何利用Websocket实现ROS与Web的交互?

ROS2 Humble测试版安装笔记

ROS基本概念

ROS基本概念

ROS2 Humble测试版安装笔记

ros_arduino_bridge功能包的使用