首页 > 其他 > 详细

ros21讲学习-发布话题

时间:2020-05-23 23:12:44      阅读:82      评论:0      收藏:0      [点我收藏+]

topic发布

-----------------------------------------------------------------

创建功能包

 

cd /home/jo/workspace/catkin_ws/src

catkin_create_pkg learning_topic roscpp rospy std_msgs geometry_msgs turtlesim

 

代码:velocity_publisher.cpp

技术分享图片
/***********************************************************************

Copyright 2020 GuYueHome (www.guyuehome.com).

***********************************************************************/

 

/**

* 该例程将发布turtle1/cmd_vel话题,消息类型geometry_msgs::Twist

*/

 

#include <ros/ros.h>

#include <geometry_msgs/Twist.h>

 

int main(int argc, char **argv)

{

// ROS节点初始化,定义topic名字"velocity_publisher",topic名字不可重复

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

 

// 创建节点句柄,用句柄管理节点调用

ros::NodeHandle n;

 

// 创建一个Publisher,发布名为/turtle1/cmd_vel的topic,消息类型为geometry_msgs::Twist,队列长度10

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

 

// 设置循环的频率

ros::Rate loop_rate(10);

 

int count = 0;

//发布数据

while (ros::ok())

{

// 初始化geometry_msgs::Twist类型的消息

geometry_msgs::Twist vel_msg;

vel_msg.linear.x = 0.5;

vel_msg.angular.z = 0.2;

 

// 发布消息

turtle_vel_pub.publish(vel_msg);

ROS_INFO("Publsh turtle velocity command[%0.2f m/s, %0.2f rad/s]",

vel_msg.linear.x, vel_msg.angular.z);

 

// 按照循环频率延时

loop_rate.sleep();

}

 

return 0;

}
View Code

 

编译:

cmake中加入(注意找对位置,在build下面)

add_executable(velocity_publisher src/velocity_publisher.cpp)

target_link_libraries(velocity_publisher ${catkin_LIBRARIES})

 

cd ~/workspace/catkin_ws/src

catkin_make

source ~/workspace/catkin_ws/devel/setup.bash

执行:

终端1: roscore

终端2: rosrun turtlesim turtlesim_node

终端3: rosrun learning_topic velocity_publisher

 

现象:小乌龟画圆圈

Python版本:不用编译

直接在终端3

python /home/jo/class/ros_21_tutorials/learning_topic/scripts/velocity_publisher.py

代码:

技术分享图片
#!/usr/bin/env python
# -*- coding: utf-8 -*-

########################################################################
####          Copyright 2020 GuYueHome (www.guyuehome.com).          ###
########################################################################

# 该例程将发布turtle1/cmd_vel话题,消息类型geometry_msgs::Twist

import rospy
from geometry_msgs.msg import Twist

def velocity_publisher():
    # ROS节点初始化
    rospy.init_node(velocity_publisher, anonymous=True)

    # 创建一个Publisher,发布名为/turtle1/cmd_vel的topic,消息类型为geometry_msgs::Twist,队列长度10
    turtle_vel_pub = rospy.Publisher(/turtle1/cmd_vel, Twist, queue_size=10)

    #设置循环的频率
    rate = rospy.Rate(10) 

    while not rospy.is_shutdown():
        # 初始化geometry_msgs::Twist类型的消息
        vel_msg = Twist()
        vel_msg.linear.x = 0.5
        vel_msg.angular.z = 0.2

        # 发布消息
        turtle_vel_pub.publish(vel_msg)
        rospy.loginfo("Publsh 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
View Code

 

ros21讲学习-发布话题

原文:https://www.cnblogs.com/polipolu/p/12944847.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!