正文
1.准备工作
学习之前需要安装一个简单的模拟器

sudo apt-get install ros-你的ROS版本号-ros-tutorials

我用的版本是noetic,因此打开一个terminal输入指令

sudo apt-get install ros-noetic-ros-tutorials

2.一些基本概念

  • Nodes:一个可以与其他Nodes通过ROS通信的可执行代码
  • Messages:用来对着一个topic接收也叫订阅(subscribe)或者发布(publish)消息的数据结构
  • Topics:Nodes可以订阅(subscribe)一个Topic以接收数据,也可以对着一个Topic发布(publish)消息来发送数据
  • Master:Name service for ROS (i.e. helps nodes find each other)
  • rosout:ROS equivalent of stdout/stderr
  • roscore:Master + rosout + parameter server (parameter server will be introduced later)

3.节点

节点不过是ROS package里的一个可执行文件,ROS node 通过ROS client library 与其他节点通信。节点可订阅一个Topic或者向一个Topic发布消息,Node也可以提供或者使用service。

4.Client Libraries
编写节点程序用到的库

  • rospy = python client library
  • roscpp = c++ client library

5.roscore
在使用ROS前,第一件事就是运行roscore

roscore

正确的输出如下

... logging to /home/julis/.ros/log/57bf05f6-eed3-11ec-83ae-81bf2ea2ba9b/roslaunch-julis-PC-23532.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://julis-PC:33913/
ros_comm version 1.15.14


SUMMARY
========

PARAMETERS
 * /rosdistro: noetic
 * /rosversion: 1.15.14

NODES

auto-starting new master
process[master]: started with pid [23556]
ROS_MASTER_URI=http://julis-PC:11311/

setting /run_id to 57bf05f6-eed3-11ec-83ae-81bf2ea2ba9b
process[rosout-1]: started with pid [23566]
started core service [/rosout]

6.使用rosnode
开一个新的terminal,运行rosnode list查看正在运行的节点。会发现只有rosout节点正在运行,这个节点是用来收集日志以及debug输出结果的,所以会一直运行。要查看某个节点的具体信息,比如这里想看下rosout的信息,运行rosnode info /rosout得到输出

--------------------------------------------------------------------------------
Node [/rosout]
Publications: 
 * /rosout_agg [rosgraph_msgs/Log]

Subscriptions: 
 * /rosout [unknown type]

Services: 
 * /rosout/get_loggers
 * /rosout/set_logger_level


contacting node http://julis-PC:46093/ ...
Pid: 23566

--------------------------------------------------------------------------------
Node [/out]
Publications: None

Subscriptions: None

Services: None

cannot contact [/out]: unknown node

7.使用rosrun
rosrun允许我们直接运行某个package里的node,这里我们运行一下package turtlesim里的turtlesim_node.

rosrun turtlesim turtlesim_node

会弹出一个小乌龟窗口
在这里插入图片描述
这时候再打开一个新的terminal查看一下有哪些正在运行的节点rosnode list,会看到
在这里插入图片描述
可以看到果然多了一个运行的节点。

8.其他tips

  • 给节点改名
rosrun turtlesim turtlesim_node __name:=my_turtle

此时再次使用rosnode list,会看到

/my_turtle
/rosout

如果仍然能看到 /turtlesim 说明你是用 Ctril+c 而不是关闭终端窗口来中止的上一个node,这样会导致节点清理不干净,可以用 rosnode cleanup来清理未关闭完全的节点,操作如下:

julis@julis-PC:~$ rosnode cleanup 
ERROR: connection refused to [http://julis-PC:39721/]
Unable to contact the following nodes:
 * /turtlesim
Warning: these might include alive and functioning nodes, e.g. in unstable networks.
Cleanup will purge all information about these nodes from the master.
Please type y or n to continue:
y
Unregistering /turtlesim
done
julis@julis-PC:~$ rosnode list
/my_turtle
/rosout

  • 查看节点ping值
rosnode ping my_turtle

输出如下

julis@julis-PC:~$ rosnode ping my_turtle
rosnode: node is [/my_turtle]
pinging /my_turtle with a timeout of 3.0s
xmlrpc reply from http://julis-PC:37019/	time=0.615120ms
xmlrpc reply from http://julis-PC:37019/	time=0.684977ms
xmlrpc reply from http://julis-PC:37019/	time=0.629187ms
xmlrpc reply from http://julis-PC:37019/	time=0.747681ms
xmlrpc reply from http://julis-PC:37019/	time=0.836134ms
xmlrpc reply from http://julis-PC:37019/	time=0.809193ms
xmlrpc reply from http://julis-PC:37019/	time=0.757456ms
^Cping average: 0.725678ms

总结:

roscore = ros+core : master (provides name service for ROS) + rosout (stdout/stderr) + parameter server (parameter server will be introduced later)
rosnode = ros+node : ROS tool to get information about a node.
rosrun = ros+run : runs a node from a given package.

  1. 跑ros之前要运行roscore
  2. 查看正在运行的节点rosnode list
  3. 运行package里的节点rosrun [package_name] [node_name]
Logo

助力广东及东莞地区开发者,代码托管、在线学习与竞赛、技术交流与分享、资源共享、职业发展,成为松山湖开发者首选的工作与学习平台

更多推荐