ros2笔记-6.5 使用ros2_control驱动机器人

embedded/2025/1/15 20:24:43/

ros2_control 是使用ros2进行机器人控制的框架。简化硬件的集成。

6.5.1 ros2_control安装

为什么要用ros2_contrl.书上、视频上小鱼老师介绍的比较清楚,这里放个control框架图。

安装:

sudo apt install ros-$ROS_DISTRO-ros2-control

sudo apt install ros-$ROS_DISTRO-ros2-controllers

6.5.2  使用gazebo 介入ros2_control

安装依赖插件:

sudo apt install ros-$ROS_DISTRO-gazebo-ros2-control

在目录src/fishbot_description/urdf/fishbot/下新建文件fishbot.ros2_control.xacro,代码如下:

<?xml version="1.0"?>
<robot xmlns:xacro="http://www.ros.org/wiki/xacro"><xacro:macro name="fishbot_ros2_control"><ros2_control name="FishBotGazeboSystem" type="system"><hardware><plugin>gazebo_ros2_control/GazeboSystem</plugin></hardware><joint name="left_wheel_joint"><!--控制接口--><command_interface name="velocity"><param name="min">-1</param><param name="max">1</param></command_interface><command_interface name="effort"><param name="min">-0.1</param><param name="max">0.1</param></command_interface><!--状态接口 --><state_interface name="position" /><state_interface name="velocity" /><state_interface name="effort" /></joint><joint name="right_wheel_joint"><command_interface name="velocity"><param name="min">-1</param><param name="max">1</param></command_interface><command_interface name="effort"><param name="min">-0.1</param><param name="max">0.1</param></command_interface><state_interface name="position" /><state_interface name="velocity" /><state_interface name="effort" /></joint></ros2_control><gazebo><plugin filename="libgazebo_ros2_control.so" name="gazebo_ros2_control"><parameters>$(find fishbot_description)/config/fishbot_ros2_controller.yaml</parameters></plugin></gazebo></xacro:macro>
</robot>

定义了一个fishbot_ros2_control 宏,标签ros2_control 描述硬件资源。

下面的关节joint,定义了左右两个轮子,有控制接口跟状态接口。

下面的gazebo标签定义了用来解析ros2_control的标签的插件。里面的参数需要config目录新建文件:fishbot_ros2_controller.yaml

controller_manager:ros__parameters:update_rate: 100  # Hzuse_sim_time: true

 至此,声明了fishbot_ros2_control的宏,我们还需要在fish_robot.urdf.xacro中引用并使用。

注意,这里跟之前的gazebo两轮差速控制有冲突,所以,暂时注释掉。

<?xml version="1.0"?>
<robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="fishbot"><xacro:include filename="$(find fishbot_description)/urdf/fishbot/base.urdf.xacro" /><!-- 传感器组件 --><xacro:include filename="$(find fishbot_description)/urdf/fishbot/sensor/imu.urdf.xacro" /><xacro:include filename="$(find fishbot_description)/urdf/fishbot/sensor/laser.urdf.xacro" /><xacro:include filename="$(find fishbot_description)/urdf/fishbot/sensor/camera.urdf.xacro" /><!-- 执行器组件 --><xacro:include filename="$(find fishbot_description)/urdf/fishbot/actuator/wheel.urdf.xacro" /><xacro:include filename="$(find fishbot_description)/urdf/fishbot/actuator/caster.urdf.xacro" /><!--gazebo插件--><!-- <xacro:include filename="$(find fishbot_description)/urdf/fishbot/plugins/gazebo_control_plugin.xacro" /> --><xacro:include filename="$(find fishbot_description)/urdf/fishbot/plugins/gazebo_sensor_plugin.xacro" /><xacro:include filename="$(find fishbot_description)/urdf/fishbot/fishbot.ros2_control.xacro" /><xacro:base_xacro length="0.12" radius="0.1" /><!-- 传感器 --><xacro:imu_xacro xyz="0 0 0.02" /><xacro:laser_xacro xyz="0 0 0.10" /><xacro:camera_xacro xyz="0.10 0 0.075" /><!-- 执行器主动轮+从动轮 --><xacro:wheel_xacro wheel_name="left_wheel" xyz="0 0.10 -0.06" /><xacro:wheel_xacro wheel_name="right_wheel" xyz="0 -0.10 -0.06" /><xacro:caster_xacro caster_name="front_caster" xyz="0.08 0.0 -0.076" /><xacro:caster_xacro caster_name="back_caster" xyz="-0.08 0.0 -0.076" /><!-- <xacro:gazebo_control_plugin /> --><xacro:gazebo_sensor_plugin /><xacro:fishbot_ros2_control />
</robot>

重新构建并启动后。可以查询控制器管理器服务

bohu@bohu-TM1701:~$ ros2 service list|grep controller
/controller_manager/configure_controller
/controller_manager/describe_parameters
/controller_manager/get_parameter_types
/controller_manager/get_parameters
/controller_manager/list_controller_types
/controller_manager/list_controllers
/controller_manager/list_hardware_components
/controller_manager/list_hardware_interfaces
/controller_manager/list_parameters
/controller_manager/load_controller
/controller_manager/reload_controller_libraries
/controller_manager/set_hardware_component_state
/controller_manager/set_parameters
/controller_manager/set_parameters_atomically
/controller_manager/switch_controller
/controller_manager/unload_controller

也可以查询控制器硬件接口;这个前面标签配置的一样

bohu@bohu-TM1701:~$ ros2 control list_hardware_interfaces 
command interfacesleft_wheel_joint/effort [available] [unclaimed]left_wheel_joint/velocity [available] [unclaimed]right_wheel_joint/effort [available] [unclaimed]right_wheel_joint/velocity [available] [unclaimed]
state interfacesleft_wheel_joint/effortleft_wheel_joint/positionleft_wheel_joint/velocityright_wheel_joint/effortright_wheel_joint/positionright_wheel_joint/velocity

6.5.3 使用关节状态发布控制

在刚做完的6.5.2,此时进入rviz会发现轮子异常,缺少了轮子到base_footprint的TF转换。跟我之前报错一样,这是因为刚注销了gazebo插件的导致的。我们可以使用ros_control的关节状态控制器,发布/joint_states 话题,然后有robot_sate_publiser转换为TF数据发布。

在fishbot_ros2_controller.yaml 添加

controller_manager:ros__parameters:update_rate: 100  # Hzuse_sim_time: truefishbot_joint_state_broadcaster:type: joint_state_broadcaster/JointStateBroadcaster

构建后启动。并使用命令行加载激活fishbot_joint_state_broadcaster控制器。

bohu@bohu-TM1701:~$ ros2 control load_controller fishbot_joint_state_broadcaster --set-state active
Successfully loaded controller fishbot_joint_state_broadcaster into state active

此时,rviz能显示正常,也能通过命令行查看joint_states话题

bohu@bohu-TM1701:~$ ros2 topic echo /joint_states --once
header:stamp:sec: 3278nanosec: 486000000frame_id: ''
name:
- left_wheel_joint
- right_wheel_joint
position:
- -0.00011715882368168451
- 0.00010725719480220164
velocity:
- -3.353251441562582e-06
- -5.942979131065872e-07
effort:
- 0.0
- 0.0
---

rqt 查看

使用命令不方便,可以加入到启动脚本里

import launch
import launch.event_handlers
import launch.launch_description_sources
import launch_ros
from ament_index_python.packages import get_package_share_directorydef generate_launch_description():# 获取功能包默认路径urdf_tutorial_path = get_package_share_directory('fishbot_description')default_model_path = urdf_tutorial_path + '/urdf/fishbot/fish_robot.urdf.xacro'default_world_path = urdf_tutorial_path + '/world/custom_room.world'# 为 Launch 声明参数action_declare_arg_mode_path = launch.actions.DeclareLaunchArgument(name='model', default_value=str(default_model_path),description='加载模型文件的绝对路径')# 获取文件内容生成新的参数robot_description = launch_ros.parameter_descriptions.ParameterValue(launch.substitutions.Command(['xacro ', launch.substitutions.LaunchConfiguration('model')]),value_type=str)# 状态发布节点robot_state_publisher_node = launch_ros.actions.Node(package='robot_state_publisher',executable='robot_state_publisher',parameters=[{'robot_description': robot_description}])# 通过 IncludeLaunchDescription 包含另外一个 launch 文件launch_gazebo = launch.actions.IncludeLaunchDescription(launch.launch_description_sources.PythonLaunchDescriptionSource([get_package_share_directory('gazebo_ros'), '/launch', '/gazebo.launch.py']),# 传递参数launch_arguments=[('world', default_world_path),('verbose','true')])#请求gazebo 加载机器人action_spawn_entity = launch_ros.actions.Node(package= 'gazebo_ros',executable='spawn_entity.py',arguments=['-topic', '/robot_description','-entity', 'fishbot'])# 加载并激活 fishbot_joint_state_broadcaster 控制器load_joint_state_controller = launch.actions.ExecuteProcess(cmd=['ros2', 'control', 'load_controller', '--set-state', 'active','fishbot_joint_state_broadcaster'],output='screen')return launch.LaunchDescription([action_declare_arg_mode_path,robot_state_publisher_node,launch_gazebo,action_spawn_entity,#事件动作,当加载机器人结束后执行launch.actions.RegisterEventHandler(event_handler= launch.event_handlers.OnProcessExit(target_action=action_spawn_entity,on_exit=[load_joint_state_controller],))])

查看已经加载的控制器和状态

bohu@bohu-TM1701:~$ ros2 control list_controllers
fishbot_joint_state_broadcaster joint_state_broadcaster/JointStateBroadcaster  active

bohu@bohu-TM1701:~$ ros2 control set_controller_state fishbot_joint_state_broadcaster inactive
[INFO] [1736821412.447120508] [_ros2cli_80444]: waiting for service /controller_manager/list_controllers to become available...
Successfully deactivated fishbot_joint_state_broadcaster
bohu@bohu-TM1701:~$ ros2 control list_controllers
fishbot_joint_state_broadcaster joint_state_broadcaster/JointStateBroadcaster  inactive
bohu@bohu-TM1701:~$ ros2 control unload_controller fishbot_joint_state_broadcaster
Successfully unloaded controller fishbot_joint_state_broadcaster
bohu@bohu-TM1701:~$ ros2 control list_controllers
No controllers are currently loaded!

有不同的命令来设置状态及卸载控制器。

6.5.4 使用力控制控制轮子

在fishbot_ros2_controller.yaml 添加

controller_manager:ros__parameters:update_rate: 100  # Hzuse_sim_time: truefishbot_joint_state_broadcaster:type: joint_state_broadcaster/JointStateBroadcasterfishbot_effort_controller:type: effort_controllers/JointGroupEffortControllerfishbot_effort_controller:ros__parameters:joints:- left_wheel_joint- right_wheel_jointcommand_interfaces:- effortstate_interfaces:- position- velocity- effort

launch脚本

import launch
import launch.event_handlers
import launch.launch_description_sources
import launch_ros
from ament_index_python.packages import get_package_share_directorydef generate_launch_description():# 获取功能包默认路径urdf_tutorial_path = get_package_share_directory('fishbot_description')default_model_path = urdf_tutorial_path + '/urdf/fishbot/fish_robot.urdf.xacro'default_world_path = urdf_tutorial_path + '/world/custom_room.world'# 为 Launch 声明参数action_declare_arg_mode_path = launch.actions.DeclareLaunchArgument(name='model', default_value=str(default_model_path),description='加载模型文件的绝对路径')# 获取文件内容生成新的参数robot_description = launch_ros.parameter_descriptions.ParameterValue(launch.substitutions.Command(['xacro ', launch.substitutions.LaunchConfiguration('model')]),value_type=str)# 状态发布节点robot_state_publisher_node = launch_ros.actions.Node(package='robot_state_publisher',executable='robot_state_publisher',parameters=[{'robot_description': robot_description}])# 通过 IncludeLaunchDescription 包含另外一个 launch 文件launch_gazebo = launch.actions.IncludeLaunchDescription(launch.launch_description_sources.PythonLaunchDescriptionSource([get_package_share_directory('gazebo_ros'), '/launch', '/gazebo.launch.py']),# 传递参数launch_arguments=[('world', default_world_path),('verbose','true')])#请求gazebo 加载机器人action_spawn_entity = launch_ros.actions.Node(package= 'gazebo_ros',executable='spawn_entity.py',arguments=['-topic', '/robot_description','-entity', 'fishbot'])# 加载并激活 fishbot_joint_state_broadcaster 控制器load_joint_state_controller = launch.actions.ExecuteProcess(cmd=['ros2', 'control', 'load_controller', '--set-state', 'active','fishbot_joint_state_broadcaster'],output='screen')# 加载并激活 fishbot_effort_controller 控制器load_fishbot_effort_controller = launch.actions.ExecuteProcess(cmd=['ros2', 'control', 'load_controller', '--set-state', 'active','fishbot_effort_controller'], output='screen')return launch.LaunchDescription([action_declare_arg_mode_path,robot_state_publisher_node,launch_gazebo,action_spawn_entity,#事件动作,当加载机器人结束后执行launch.actions.RegisterEventHandler(event_handler= launch.event_handlers.OnProcessExit(target_action=action_spawn_entity,on_exit=[load_joint_state_controller],)),launch.actions.RegisterEventHandler(event_handler= launch.event_handlers.OnProcessExit(target_action=load_joint_state_controller,on_exit=[load_fishbot_effort_controller],))])

重新构建,运行,可以查看 力控的话题

bohu@bohu-TM1701:~$ ros2 topic list -t |grep effort
/fishbot_effort_controller/commands [std_msgs/msg/Float64MultiArray]
/fishbot_effort_controller/transition_event [lifecycle_msgs/msg/TransitionEvent]

使用命令控制轮子扭矩

bohu@bohu-TM1701:~$ ros2 topic pub /fishbot_effort_controller/commands  std_msgs/msg/Float64MultiArray "{data: [0.0001, 0.0001]}"
publisher: beginning loop
publishing #1: std_msgs.msg.Float64MultiArray(layout=std_msgs.msg.MultiArrayLayout(dim=[], data_offset=0), data=[0.0001, 0.0001])

publishing #2: std_msgs.msg.Float64MultiArray(layout=std_msgs.msg.MultiArrayLayout(dim=[], data_offset=0), data=[0.0001, 0.0001])

此时,在gazebo机器人会缓缓移动

6.5.5 使用两轮差速控制控制机器人

在fishbot_ros2_controller.yaml 添加

controller_manager:ros__parameters:update_rate: 100  # Hzuse_sim_time: truefishbot_joint_state_broadcaster:type: joint_state_broadcaster/JointStateBroadcasterfishbot_effort_controller:type: effort_controllers/JointGroupEffortControllerfishbot_diff_drive_controller:type: diff_drive_controller/DiffDriveControllerfishbot_effort_controller:ros__parameters:joints:- left_wheel_joint- right_wheel_jointcommand_interfaces:- effortstate_interfaces:- position- velocity- effortfishbot_diff_drive_controller:ros__parameters:left_wheel_names: ["left_wheel_joint"]right_wheel_names: ["right_wheel_joint"]wheel_separation: 0.20#wheels_per_side: 1  # actually 2, but both are controlled by 1 signalwheel_radius: 0.032wheel_separation_multiplier: 1.0left_wheel_radius_multiplier: 1.0right_wheel_radius_multiplier: 1.0publish_rate: 50.0odom_frame_id: odombase_frame_id: base_footprintpose_covariance_diagonal : [0.001, 0.001, 0.0, 0.0, 0.0, 0.01]twist_covariance_diagonal: [0.001, 0.0, 0.0, 0.0, 0.0, 0.01]open_loop: trueenable_odom_tf: truecmd_vel_timeout: 0.5#publish_limited_velocity: trueuse_stamped_vel: false#velocity_rolling_window_size: 10

参数比较多,参见书上表格6-3

修改启动launch脚本,为了防止冲突,注释掉力控制

import launch
import launch.event_handlers
import launch.launch_description_sources
import launch_ros
from ament_index_python.packages import get_package_share_directorydef generate_launch_description():# 获取功能包默认路径urdf_tutorial_path = get_package_share_directory('fishbot_description')default_model_path = urdf_tutorial_path + '/urdf/fishbot/fish_robot.urdf.xacro'default_world_path = urdf_tutorial_path + '/world/custom_room.world'# 为 Launch 声明参数action_declare_arg_mode_path = launch.actions.DeclareLaunchArgument(name='model', default_value=str(default_model_path),description='加载模型文件的绝对路径')# 获取文件内容生成新的参数robot_description = launch_ros.parameter_descriptions.ParameterValue(launch.substitutions.Command(['xacro ', launch.substitutions.LaunchConfiguration('model')]),value_type=str)# 状态发布节点robot_state_publisher_node = launch_ros.actions.Node(package='robot_state_publisher',executable='robot_state_publisher',parameters=[{'robot_description': robot_description}])# 通过 IncludeLaunchDescription 包含另外一个 launch 文件launch_gazebo = launch.actions.IncludeLaunchDescription(launch.launch_description_sources.PythonLaunchDescriptionSource([get_package_share_directory('gazebo_ros'), '/launch', '/gazebo.launch.py']),# 传递参数launch_arguments=[('world', default_world_path),('verbose','true')])#请求gazebo 加载机器人action_spawn_entity = launch_ros.actions.Node(package= 'gazebo_ros',executable='spawn_entity.py',arguments=['-topic', '/robot_description','-entity', 'fishbot'])# 加载并激活 fishbot_joint_state_broadcaster 控制器load_joint_state_controller = launch.actions.ExecuteProcess(cmd=['ros2', 'control', 'load_controller', '--set-state', 'active','fishbot_joint_state_broadcaster'],output='screen')# 加载并激活 fishbot_effort_controller 控制器# load_fishbot_effort_controller = launch.actions.ExecuteProcess(#     cmd=['ros2', 'control', 'load_controller', '--set-state', 'active','fishbot_effort_controller'], #     output='screen')load_diff_drive_controller = launch.actions.ExecuteProcess(cmd=['ros2', 'control', 'load_controller', '--set-state', 'active','fishbot_diff_drive_controller'], output='screen')return launch.LaunchDescription([action_declare_arg_mode_path,robot_state_publisher_node,launch_gazebo,action_spawn_entity,#事件动作,当加载机器人结束后执行launch.actions.RegisterEventHandler(event_handler= launch.event_handlers.OnProcessExit(target_action=action_spawn_entity,on_exit=[load_joint_state_controller],)),launch.actions.RegisterEventHandler(event_handler= launch.event_handlers.OnProcessExit(target_action=load_joint_state_controller,on_exit=[load_diff_drive_controller],))])

重新构建,运行;

打开终端, ros2 run teleop_twist_keyboard teleop_twist_keyboard

使用键盘控制机器人运动。


http://www.ppmy.cn/embedded/154184.html

相关文章

【C++学习篇】红黑树 从入门到进阶

目录 1.红黑树的概念 1.1红黑树的规则 1.2红黑树的效率 2. 红黑树的实现 2.1 红黑树的结构 2.2红黑树的插入 2.2.1红黑树插入&#xff0c;旋转的一些细节 2.2.1.1 u&#xff08;uncle&#xff09;不存在 &#xff0c;c为p的左孩子&#xff08;单旋变色&#xff09; 2.2.…

springmvc的获取请求数据

在使用 SpringMVC 开发 web 应用时&#xff0c;我们经常需要从用户的请求中获取数据。不管是表单提交、查询参数&#xff0c;还是路径上的数据&#xff0c;SpringMVC 都为我们提供了简单而强大的方式来获取这些数据。 1. 使用 RequestParam 获取查询参数 基本使用&#xff1a…

C语言基本知识复习浓缩版:控制语句--分支

选择结构 if语句 if控制语句 #include <stdio.h>int main() { int hour; float money; printf("请输入上网小时数&#xff1a;"); scanf("%d", &hour); if (hour <10) { money 30; }else if (hour >10 && hour <50) { mone…

C# PDF下载地址转图片(Base64 编码)

实现思路&#xff1a; 步骤一、根据PDF地址下载pdf文件保存为临时文件&#xff0c;获得pdf文件的byte[]数组 /// 从指定的 URL 下载 PDF 文件public byte[] DownloadPdf(string url){try{using (WebClient client new WebClient()){return client.DownloadData(url);}}catc…

Codeforces Round 976 (Div. 2) and Divide By Zero 9.0(A-E)

链接&#xff1a;Dashboard - Codeforces Round 976 (Div. 2) and Divide By Zero 9.0 - Codeforces A. Find Minimum Operations 思路 可以观察发现这里有个进制的思想&#xff0c;转换为k进制把每位数相加即可 代码 void solve(){int n,k;cin>>n>>k;if(k1){…

CSS语言的语法

CSS语言的语法与应用 CSS&#xff08;层叠样式表&#xff0c;Cascading Style Sheets&#xff09;是一种用于描述网页文档外观的样式表语言。它主要用于网页的设计和布局&#xff0c;与HTML&#xff08;超文本标记语言&#xff09;相辅相成。通过CSS&#xff0c;我们可以控制网…

用AI技术提升Flutter开发效率:ScriptEcho的力量

引言 在当今快速发展的技术时代&#xff0c;Flutter作为一种跨平台开发框架&#xff0c;正在越来越多的开发者中崭露头角。它不仅能够为开发者提供一套代码同时部署到iOS和Android平台的解决方案&#xff0c;还能帮助企业节省人力成本和开发时间。然而&#xff0c;对于新手开发…

计算机网络之---SSL/TLS协议

SSL/TLS协议 **SSL&#xff08;Secure Sockets Layer&#xff09;和TLS&#xff08;Transport Layer Security&#xff09;**是加密协议&#xff0c;用于确保通过不安全的网络&#xff08;如互联网&#xff09;传输的数据的安全性和隐私。它们通过提供数据加密、身份验证和数据…