失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > ROS学习笔记publisher的编程实现c++详解

ROS学习笔记publisher的编程实现c++详解

时间:2021-02-24 08:10:58

相关推荐

ROS学习笔记publisher的编程实现c++详解

前言

基于B站ROS公开课:【古月居】古月·ROS入门21讲

基于Ubuntu 20.04.1、Noetic版本

提示:以下是本篇文章正文内容,下面案例可供参考

一、ros publisher C++

代码如下(示例):

#include <ros/ros.h>

#include <geometry_msgs/Twist.h>

int main(int argc, char **argv)

{

//ros setup

ros::init(argc, argv, "velocity_publisher");

//creat ros nodehandle name:ns

ros::NodeHandle ns;

//creat publisher turtle_vel_pub and viod a topic /turtle1/cmd_vel line 10

ros::Publisher turtle_vel_pub = ns.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 10);

//set loop rate 10hz

ros::Rate loop_rate(10);

int count = 0;

while (ros::ok())

{

//geometry_msg::Twist setup

geometry_msgs::Twist vel_msg;

vel_msg.linear.x = 0.5;

vel_msg.angular.z = 0.2;

//publish the msg

turtle_vel_pub.publish(vel_msg);

ROS_INFO("publish turtle velocity command[%0.2f m/s,%0.2 rad/s]",vel_msg.linear,vel_msg.angular);

//follow loop hz to deley

loop_rate.sleep();

}

return 0;

}

2.python

import rospy

from geometry_msgs.msg import Twist

def velocity_publisher():

#ros node setup

rospy.init_node('velocity_publisher', anonymous=True)

#creat publisher topic name /turtle1/cmd_vel msg type is geometry::Twist line 10

turtle1_vel_pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)

#set loop rate

rate = rospy.Rate(10)

while not rospy.is_shutdown():

#set up geometry_msgs::Twist type

vel_msg = Twist()

vel_msg.linear.x = 0.5

vel_msg.angular.z = 0.2

#pub msgs

turtle1_vel_pub.publish(vel_msg)

rospy.loginfo("publish turtle velocity command[%0.2f m/s,%0.2f rad/s]",vel_msg.linear.x, vel_msg.angular.z)

rate.sleep()

if __name__=='__main__':

try:

velocity_publisher()

except rospy.ROSInterruptException:

pass

如果觉得《ROS学习笔记publisher的编程实现c++详解》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。