ROS--4 常用的命令(小乌龟实例)

本文详细介绍如何配置ROS环境,包括设置环境变量、启动Master进程、运行Turtlesim节点、列出活动节点和主题,以及获取特定主题信息。通过实际操作,读者将深入理解ROS的工作原理。

1: 配置环境

source /opt/ros/kinetic/setup.bash 

配置env内容如下

ROS_ROOT=/opt/ros/kinetic/share/ros The path where core ros packages are stored

PATH=/opt/ros/kinetic/bin:... The path to the ROS binaries, which we will be using throughout this lesson.

ROS_DISTRO=kinetic Which distribution of ROS is being used. In this case, we are using kinetic

PYTHONPATH=/opt/ros/kinetic/lib/python2.7/dist-packages The path to the ROS python packages, which we will be using next lesson.

More information about the environment variables mentioned above, the ones not mentioned here, and others can be found here.

ROS/EnvironmentVariables - ROS Wiki

命令添加到启动脚本,自动配置

echo “source /opt/ros/kinetic/setup.bash ”  >>  ~/.bashrc

2. Starting the Master process

Before any ROS nodes can be run, the Master process must be started.

The Master process is responsible for the following (and more)

  • Providing naming and registration services to other running nodes
  • Tracking all publishers and subscribers
  • Aggregating log messages generated by the nodes
  • Facilitating connections between nodes
$ roscore

3.running Turtlesim Nodes

Now that the ROS master is running, we can run our first two ROS nodes.

First we will start the turtlesim_node, in the turtlesim package using the following command.

$ rosrun turtlesim turtlesim_node

Next, we will start the turtle_teleop_key node, also from the turtlesim package.

$ rosrun turtlesim turtle_teleop_key

Ps: turtlesim_nodeturtle_teleop_keyturtlesim里创建的两个node, turtlesimROS自带的一个软件包。在ROS里,每个node可以理解为一个独立的进程,有独立运行空间,又与其他node之间可以同步或通信。

4.Listing all Active Nodes

To get a list of all nodes that are active and have been registered with the ROS Master, we can use the command 

rosnode list

ps:列出当前运行系统中所有node

We can see that there are three active nodes that have been registered with the ROS Master, /rosout/teleop_turtle, and /turtlesim.

  • /rosout 
  • This node is launched by roscore. It subscribes to the standard /rosout topic, the topic to which all nodes send log messages.
  • /teleop_turtle 
  • This is our keyboard teleop node. Notice that it’s not named turtle_teleop_key. There’s no requirement that a node’s broadcasted name is the same as the name of it’s associated executable.
  • /turtlesim 
  • The node name associated with the turtlebot_sim node

5. Listing All Topics

In a similar fashion, we are able to query the ROS Master for a list of all topics. To do so, we use the command 

rostopic list.

Ps:列出当前运行系统中所有的topic。

  • /rosout_agg :Aggregated feed of messages published to /rosout.
  • /turtle1/cmd_vel :Topic upon which velocity commands are sent/received. Publishing a velocity message to this topic will command turtle1 to move.

  • /turtle1/color_sensor :Each turtle in turtlesim is equipped with a color sensor, and readings from the sensor are published to this topic.
  • /turtle1/pose :The position and orientation of turtle1 are published to this topic.

6. Get Information About a Specific Topic

If we wish to get information about a specific topic, who is publishing to it, subscribed to it, or the type of message associated with it, we can use the command

 $rostopic info.

Let’s check into the /turtle1/cmd_vel topic:

$rostopic info /turtle1/cmd_vel

Ps:查看/turtle1/cmd_vel主题的详细信息,包括主题发布者/订阅者,消息类型等。

As would be expected, there are two nodes registered on this topic. Our publisher, the teleop_turtle node, and our subscriber, the turtlesim node. Additionally, we can see that the type of message used on this topic is geometry_msgs/Twist.

7.Show Message Information

Let’s get some more information about the geometry_msgs/Twist message on the /turtle1/cmd_vel topic, to do so, we will use the rosmsg info command.

$rosmsg info geometry_msgs/Twist 

We can see that a Twist message consists nothing more than two Vector3 messages. One for linear velocity, and another for angular velocities, with each velocity component being represented by a float64.

Note: Sometimes, the message definition won’t provide an ample amount of detail about a message type. For example, in the example above, how can we be sure that linear and angular vectors above refer to velocities, and not positions? One way to get more detail would be to look at the comments in the message’s definition file. To do so, we can issue the following command: 

rosed geometry_msgs Twist.msg.

Note 2: More information about rosed, including how to select which editor is used by default can be found here.

ROS/Tutorials/UsingRosEd - ROS Wiki

8.Echo Messages on a Topic

Sometimes it may be useful to look at a topic’s published messages in real time.

To do so, we can use the command rostopic echo. Let’s take a look at the /turtle1/cmd_vel topic.

$ rostopic echo /turtle1/cmd_vel

If we then command the turtle to move from the turtle_teleop_key window, we will be able to see the output message in real-time.

Ps:实时查看主题信息

8. Computer Graph

rosrun rqt_graph rqt_graph

可以看到ROS的图形化界面,展示计算图所有结点的关系

### ROS Humble Turtlesim 教程与资源 #### 创建并配置工作空间 为了开始使用Turtlesim,在本地计算机上设置一个新的ROS 2工作区是必要的。这通常涉及创建一个名为`ws_turtlesim`的工作目录,并在其内部建立源代码存储库`src`。通过命令行工具执行这些操作,确保安装了Colcon构建工具来编译软件包[^1]。 ```bash mkdir -p ~/ws_turtlesim/src cd ~/ws_turtlesim/ ``` #### 安装Turtlesim包 对于Humble版本而言,可以直接利用apt-get快速获取官方发布的二进制文件形式的Turtlesim程序集: ```bash sudo apt update && sudo apt install ros-humble-turtlesim ``` 此过程会自动处理依赖关系并将所需的一切部署到位。 #### 启动模拟环境 一旦完成上述准备工作之后,可以通过简单的指令启动Turtlesim节点及其图形界面展示窗口: ```bash ros2 run turtlesim turtlesim_node ``` 该命令将会打开一个显示绿色海龟游动于蓝色背景之上的GUI应用程序实例。 #### 控制海龟运动 为了让用户能够更加直观地了解如何发送消息给特定的话题从而控制虚拟生物的动作,这里提供了一个交互式的键盘控制器例子。只需输入如下shell脚本即可实现基本的方向键操控功能: ```bash ros2 run turtlesim turtle_teleop_key ``` 此时可以在终端里按方向箭头使屏幕中的乌龟能够响应上下左右移动以及旋转动作。 #### 编写自定义Python客户端 除了借助现成的应用之外,学习者还可以尝试编写自己的ROS 2 Python客户端代码片段来进行更深入的学习体验。下面给出了一段用于发布速度命令至`turtle1/cmd_vel`主题的小型示范程序: ```python import rclpy from geometry_msgs.msg import Twist from rclpy.node import Node class MinimalPublisher(Node): def __init__(self): super().__init__('minimal_publisher') self.publisher_ = self.create_publisher(Twist, '/turtle1/cmd_vel', 10) timer_period = 0.5 # seconds self.timer = self.create_timer(timer_period, self.timer_callback) def timer_callback(self): msg = Twist() msg.linear.x = 0.5 msg.angular.z = 0.5 self.publisher_.publish(msg) def main(args=None): rclpy.init(args=args) minimal_publisher = MinimalPublisher() rclpy.spin(minimal_publisher) minimal_publisher.destroy_node() rclpy.shutdown() if __name__ == '__main__': main() ``` 这段代码展示了怎样订阅话题、定时器回调函数的设计思路等内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值