目录
参考学习资料:赵虚左的课程+古月的ROS机器人开发实践P56的例程。
准备工作
打开ROS乌龟仿真器
roscore
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key
实现可以通过键盘上下左右控制海龟。
目的是通过话题发布实现对如下控制器的替代。
rosrun turtlesim turtle_teleop_key
之前话题通信提到,进行通信的条件就是发布者和订阅者话题名必须相同,必须知道乌龟运动的订阅者话题名。查看所有的话题:
rostopic list
但是现在不知道如下话题的作用。
rosmelodic@rosmelodic-virtual-machine:~$ rostopic list
/rosout
/rosout_agg
/turtle1/cmd_vel
/turtle1/color_sensor
/turtle1/pose
查看计算图:
rqt_graph
知道了两个节点之间使用的话题名cmd_vel。
知道话题名了,但是不知道话题消息的数据类型,如果话题消息类型与订阅者消息类型对应不上也是不行的。由之前ROS常用命令的学习可知,如何查询话题的详细信息。
12.ROS编程学习:ROS常用指令_机械专业的计算机小白的博客-CSDN博客https://blog.csdn.net/wzfafabga/article/details/127439536
rostopic info 话题名
rostopic info /turtle1/cmd_vel
介绍了话题的消息类型为几何消息类型。
rosmelodic@rosmelodic-virtual-machine:~$ rostopic info /turtle1/cmd_vel
Type: geometry_msgs/Twist
Publishers:
* /teleop_turtle (http://rosmelodic-virtual-machine:37281/)
Subscribers:
* /turtlesim (http://rosmelodic-virtual-machine:41291/)
同样type也可以。
rosmelodic@rosmelodic-virtual-machine:~$ rostopic type /turtle1/cmd_vel
geometry_msgs/Twist
消息类型知道了,但不知道Twist消息格式。
rosmsg info 数据类型名
rosmsg info geometry_msgs/Twist
rosmelodic@rosmelodic-virtual-machine:~$ rosmsg info geometry_msgs/Twist
geometry_msgs/Vector3 linear
float64 x
float64 y
float64 z
geometry_msgs/Vector3 angular
float64 x
float64 y
float64 z
线速度和角速度的向量。学过机器人学很好理解xyz三个方向的线速度,Rx,Ry,Rz三个旋转方向的角速度,方向由叉乘的右手定则判断。此乌龟只有x和Rz。
rosmelodic@rosmelodic-virtual-machine:~$ rostopic echo /turtle1/cmd_vel
按下前和右。
linear:
x: 2.0
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: 0.0
---
linear:
x: 0.0
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: -2.0
---
根据测试x为小乌龟的前进方向,Rz为从屏幕里指向屏幕外。
通过控制台命令实现:
rosmelodic@rosmelodic-virtual-machine:~$ $ rtopic pub -r 10 /turtle1/cmd_vel geometry_msgs/Twist "linear:
x: 2.0
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: 2.0"
c++控制乌龟运动
这次的依赖:rospy roscpp geometry_msgs std_msgs
test01_pub_twist.cpp
#include "ros/ros.h"
#include "geometry_msgs/Twist.h"
/*
话题:/turtle1/cmd_vel
消息:geometry_msgs/Twist
1.头文件
2.初始化ROS节点
3.创建节点句柄
4.创建发布对象
5.发布逻辑
6.spinOnce()
*/
int main(int argc, char *argv[])
{
// 防止中文乱码
setlocale(LC_ALL,"");
// 初始化ROS节点
ros::init(argc,argv,"gui_control");
// 初始化ROS句柄
ros::NodeHandle n;
// 创建发布者
ros::Publisher pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel",1000);
// 声命变量类型,几何消息类型
geometry_msgs::Twist msg;
// 给变量赋值,线速度和角速度
msg.linear.x = 1.0;
msg.linear.y = 0.0;
msg.linear.z = 0.0;
msg.angular.x = 0.0;
msg.angular.y = 0.0;
msg.angular.z = 2.0;
// 设置发布频率,10Hz
ros::Rate r(10);
while (ros::ok())
{
// 发布消息
pub.publish(msg);
// 添加延时函数,达到设置发布频率目的
r.sleep();
// 官方建议的回头函数,回头一次
ros::spinOnce();
}
return 0;
}
CMakeList.txt配置
add_executable(test01_pub_twist src/test01_pub_twist.cpp)
add_dependencies(test01_pub_twist ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(test01_pub_twist
${catkin_LIBRARIES}
)
其中如下并没有用到第三方依赖,如自定义消息类型,如果非要加上依赖,那就取消注释,修改对应第一行的文件名后并不要有其他修改。
add_dependencies(test01_pub_twist ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
启动乌龟
roscore
rosrun turtlesim turtlesim_node
source ./devel/setup.bash
rosrun wugui_ttest test01_pub_twist
python控制乌龟运动
test01_pub_twist_p.py
#! /usr/bin/env python
# -*- coding: UTF-8 -*-
"""
发布方:发布速度信息
话题:/turtle1/cmd_vel
消息:geometry_msgs/Twist
1.导包
2.初始化ROS节点
3.创建发布者
4.组织数据
5.发布数据
"""
from audioop import ratecv
import rospy
from geometry_msgs.msg import Twist
if __name__ == "__main__":
#初始化ROS节点
rospy.init_node("wugui_control_p")
#创建发布者
pub = rospy.Publisher(name="/turtle1/cmd_vel",data_class=Twist,queue_size=100)
#组织数据,设置参数
msg = Twist()
msg.linear.x = 1.0
msg.linear.y = 0.0
msg.linear.z = 0.0
msg.angular.x = 0.0
msg.angular.y = 0.0
msg.angular.z = 0.5
#设置循环频率
rate = rospy.Rate(hz=10)
#利用while循环,和发布者下的发布函数发布数据
while not rospy.is_shutdown():
#发布组织好的数据
pub.publish(msg)
#通过设置延时达到循环频率的设置
rate.sleep()
CMakeList.txt配置
catkin_install_python(PROGRAMS
scripts/test01_pub_twist_p.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
进入scripts目录,添加可执行权限
chmod +x *.py
启动乌龟
rosrun turtlesim turtlesim_node
启动乌龟线速度和角速度发布者
rosrun wugui_ttest test01_pub_twist_p.py
结果:
查看计算图
rqt_graph