ROS2的安装与使用

您所在的位置:网站首页 如何判断ros安装成功 ROS2的安装与使用

ROS2的安装与使用

2023-11-24 23:15| 来源: 网络整理| 查看: 265

安装说明 安装ROS2 $ sudo apt update $ sudo apt install curl gnupg2 lsb-release $ curl http://repo.ros2.org/repos.key | sudo apt-key add - $ sudo sh -c 'echo "deb http://packages.ros.org/ros2/ubuntu `lsb_release -cs` main" > /etc/apt/sources.list.d/ros2-latest.list' $ sudo apt update $ sudo apt install ros-foxy-desktop $ echo "source /opt/ros/foxy/setup.bash" >> ~/.bashrc

重启系统

卸载ROS2 $ sudo apt-get purge ros-* $ sudo rm -rf /etc/ros

删除环境变量

$ gedit ~/.bashrc

找到下面的内容并删除保存

source /opt/ros/foxy/setup.bash turtlesim包 $ sudo apt update $ sudo apt install ros-foxy-turtlesim 确认是否安装成功

如果出现下面的四个执行文件,证明安装成功

$ ros2 pkg executables turtlesim turtlesim draw_square turtlesim mimic turtlesim turtle_teleop_key turtlesim turtlesim_node 使用说明 仿真 开启乌龟仿真节点 $ ros2 run turtlesim turtlesim_node

在这里插入图片描述

开启键盘控制节点

另起一个终端执行下面的命令

$ ros2 run turtlesim turtle_teleop_key

按下键盘的前后左右键可以控制乌龟的移动 在这里插入图片描述

节点node 查看所有节点

在ROS的每个节点应该负责单个模块,实现单个意图,比如控制轮毂电机,比如采集激光数据。 每个节点可以发送和接收其他节点的数据,通过topics, services, actions, parameters等方式。

$ ros2 node list /teleop_turtle /turtlesim

在这里插入图片描述

查看节点信息 $ ros2 node info /turtlesim /turtlesim Subscribers: /parameter_events: rcl_interfaces/msg/ParameterEvent /turtle1/cmd_vel: geometry_msgs/msg/Twist Publishers: /parameter_events: rcl_interfaces/msg/ParameterEvent /rosout: rcl_interfaces/msg/Log /turtle1/color_sensor: turtlesim/msg/Color /turtle1/pose: turtlesim/msg/Pose Service Servers: /clear: std_srvs/srv/Empty /kill: turtlesim/srv/Kill /turtlesim/set_parameters_atomically: rcl_interfaces/srv/SetParametersAtomically Service Clients: Action Servers: /turtle1/rotate_absolute: turtlesim/action/RotateAbsolute Action Clients: 话题topic 查看所有话题

ROS2把复杂的系统划分成很多节点,话题是节点之间交换数据的桥梁。 话题不仅仅是点对点的通信,他还可以实现多对多的通信。

$ ros2 topic list /parameter_events /rosout /turtle1/cmd_vel /turtle1/color_sensor /turtle1/pose

在这里插入图片描述

查看话题数据 $ ros2 topic echo /turtle1/cmd_vel linear: x: 2.0 y: 0.0 z: 0.0 angular: x: 0.0 y: 0.0 z: 0.0 --- 查看话题信息 $ ros2 topic info /turtle1/cmd_vel Type: geometry_msgs/msg/Twist Publisher count: 1 Subscription count: 1 查看话题消息类型参数 $ ros2 interface show geometry_msgs/msg/Twist # This expresses velocity in free space broken into its linear and angular parts. Vector3 linear Vector3 angular 发布话题

执行下面的命令可以让小乌龟一直转圈圈

ros2 topic pub --rate 1 /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"

在这里插入图片描述

服务 查看所有服务

Services是节点之间通信的另一种方法。服务是基于调用-反馈的模型,而不是话题的发布-订阅模型。 话题适合频繁的数据更新,服务是需要的时候才调用,通信量会减少。

$ ros2 service list /clear /teleop_turtle/describe_parameters /teleop_turtle/get_parameter_types /teleop_turtle/get_parameters

在这里插入图片描述

查看服务类型 $ ros2 service type /spawn turtlesim/srv/Spawn 查看服务通信参数 $ ros2 interface show turtlesim/srv/Spawn float32 x float32 y float32 theta string name # Optional. A unique name will be created and returned if this is empty --- string name 调用服务

执行下面的执行可以清除仿真环境的路径

$ ros2 service call /clear std_srvs/srv/Empty action 查看所有action

action机制从原理上看的话,其实是基于topic实现的,相当于制定了一套由目标话题,反馈话题,结果话题组成的高级协议。 action实现了一种类似于service的请求/响应通讯机制,区别在于action带有反馈机制,用来不断向客户端反馈任务的进度,并且还支持在任务中途中止运行。

$ ros2 action list /turtle1/rotate_absolute

在这里插入图片描述

查看action信息 $ ros2 action info /turtle1/rotate_absolute Action: /turtle1/rotate_absolute Action clients: 1 /teleop_turtle Action servers: 1 /turtlesim 查看action类型参数 $ ros2 interface show turtlesim/action/RotateAbsolute.action # The desired heading in radians float32 theta --- # The angular displacement in radians to the starting position float32 delta --- # The remaining rotation in radians float32 remaining 执行action

执行下面的命令可让小乌龟旋转指定的角度,当执行完成后,会收到通知。

$ ros2 action send_goal /turtle1/rotate_absolute turtlesim/action/RotateAbsolute "{theta: 1.57}" Waiting for an action server to become available... Sending goal: theta: 1.57 Goal accepted with ID: 1cb75b567cef4da0899035e9702d69df Result: delta: -1.2800003290176392 Goal finished with status: SUCCEEDED launch

如果需要设计一个复杂的系统,需要越来越多的节点,那么一个一个启动必然会很麻烦,因此ROS提供了launch文件给我们用于配置管理所有的节点和参数,让运行更加便利。

编写launch文件

创建turtlesim_mimic_launch.py文件

from launch import LaunchDescription from launch_ros.actions import Node def generate_launch_description(): return LaunchDescription([ Node( package='turtlesim', namespace='turtlesim1', executable='turtlesim_node', name='sim' ), Node( package='turtlesim', namespace='turtlesim2', executable='turtlesim_node', name='sim' ), Node( package='turtlesim', executable='mimic', name='mimic', remappings=[ ('/input/pose', '/turtlesim1/turtle1/pose'), ('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'), ] ) ]) 执行launch文件 $ ros2 launch turtlesim_mimic_launch.py 执行动作

发布话题让两个小乌龟开始行走

$ ros2 topic pub -r 1 /turtlesim1/turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: -1.8}}" 查看节点关系图 $ rqt_graph

在这里插入图片描述

rqt_console

rqt_console是用来查看日志的工具。用这个工具可以在任意时间搜索查看指定时间的日志。

$ ros2 run rqt_console rqt_console

在这里插入图片描述

记录与播放数据 运行节点 $ ros2 run turtlesim turtlesim_node $ ros2 run turtlesim turtle_teleop_key 记录单个话题 $ ros2 bag record /turtle1/cmd_vel

Ctrl+C可停止记录 在这里插入图片描述

记录多个话题 $ ros2 bag record -o subset /turtle1/cmd_vel /turtle1/pose 播放记录 $ ros2 bag play subset


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3