Publishers, as the name suggests, serve to publish messages. The message here can be sensor information transmitted from the lower computer to the upper computer, which is then packaged and sent to subscribers who have subscribed to the topic through the upper computer; It can also be the data from the upper computer that has been processed, packaged, and sent to subscribers who subscribe to the topic.
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
xxxxxxxxxx
cd ~/catkin_ws/
catkin_make
xxxxxxxxxx
source devel/setup.bash
xxxxxxxxxx
echo $ROS_PACKAGE_PATH
xxxxxxxxxx
cd ~/catkin_ws/src
catkin_create_pkg learning_topic std_msgs rospy roscpp geometry_msgs turtlesim
xxxxxxxxxx
cd ~/catkin_ws
catkin_make
source ~/catkin_ws/devel/setup.bash
x#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
int main(int argc, char **argv){
ros::init(argc, argv, "turtle_velocity_publisher");
ros::NodeHandle n;
ros::Publisher turtle_vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 10);
ros::Rate loop_rate(10);
while (ros::ok()){
geometry_msgs::Twist turtle_vel_msg;
turtle_vel_msg.linear.x = 0.8;
turtle_vel_msg.angular.z = 0.6;
turtle_vel_pub.publish(turtle_vel_msg);/
ROS_INFO("Publsh turtle velocity command[%0.2f m/s, %0.2f rad/s]", turtle_vel_msg.linear.x, turtle_vel_msg.angular.z);
loop_rate.sleep();/
}
return 0;
}
xxxxxxxxxx
add_executable(turtle_velocity_publisher src/turtle_velocity_publisher.cpp)
target_link_libraries(turtle_velocity_publisher ${catkin_LIBRARIES})
xxxxxxxxxx
cd ~/catkin_ws
catkin_make
source devel/setup.bash
xxxxxxxxxx
roscore
xxxxxxxxxx
rosrun turtlesim turtlesim_node
xxxxxxxxxx
rosrun learning_topic turtle_velocity_publisher
This indicates that Little Turtle is a subscription/tourle1/cmd_ The speed topic is vel, so the publisher keeps sending speed data. After receiving it, the little turtle starts to move according to the speed
4.3.3. Python Language Implementation
xxxxxxxxxx
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import rospy
from geometry_msgs.msg import Twist
def turtle_velocity_publisher():
rospy.init_node('turtle_velocity_publisher', anonymous=True) # ROS节点初始化
turtle_vel_pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=8)
rate = rospy.Rate(10) #设置循环的频率
while not rospy.is_shutdown():
# 初始化geometry_msgs::Twist类型的消息
turtle_vel_msg = Twist()
turtle_vel_msg.linear.x = 0.8
turtle_vel_msg.angular.z = 0.6
# 发布消息
turtle_vel_pub.publish(turtle_vel_msg)
rospy.loginfo("linear is :%0.2f m/s, angular is :%0.2f rad/s",
turtle_vel_msg.linear.x, turtle_vel_msg.angular.z)
rate.sleep()# 按照循环频率延时
if __name__ == '__main__':
try:
turtle_velocity_publisher()
except rospy.ROSInterruptException:
pass
xxxxxxxxxx
roscore
xxxxxxxxxx
rosrun turtlesim turtlesim_node
xxxxxxxxxx
rosrun learning_topic turtle_velocity_publisher
Note: Before running, it is necessary to give the title_ velocity_ Publisher.py adds executable permissions in the title_ velocity_ Open the terminal in the publisher.py folder,
xxxxxxxxxx
sudo chmod a+x turtle_velocity_publisher.py
All Python requires adding execute file permissions, otherwise an error will be reported!5) Refer to 1.3.2 for operational effects and program instructions.