ROS发布多个pointstampe点 python实现

在芝加哥大学的这个教程代码基础上改的

https://classes.cs.uchicago.edu/archive/2021/spring/20600-1/ros_intro.html

#!/usr/bin/env python3
# encoding: utf-8

#code copy from https://classes.cs.uchicago.edu/archive/2021/spring/20600-1/ros_intro.html
import rospy
from geometry_msgs.msg import PointStamped
from std_msgs.msg import Header
from geometry_msgs.msg import Point

def pub_pointStamped(x,y,z):

    
    my_header = Header(stamp=rospy.Time.now(), frame_id="map")
    my_point = Point(x, y, z)
    my_point_stamped = PointStamped(header=my_header, point=my_point)

    


    # # rospy.Rate specifies the rate of the loop (in this case 2 Hz)
    r = rospy.Rate(0.5)
    if not rospy.is_shutdown():
        my_point_stamped.header.stamp = rospy.Time.now()    # update timestamp
        publisher.publish(my_point_stamped)
        r.sleep()
        # break

if __name__ == '__main__':

   
    rospy.loginfo_once('starting task .....................')
    rospy.sleep(1)
    rospy.init_node('send_point_location')    # initialize our ROS node
    publisher = rospy.Publisher('/clicked_point', PointStamped, queue_size=10)
    rospy.sleep(1)
    rospy.loginfo_once('send_point 1.....................')
    pub_pointStamped (12.731, -9.136, 0.0) # I dont knowe why it is needed to send twice and one time doesnt work
    # pub_pointStamped (12.731, -9.136, 0.0)
    rospy.loginfo_once('send_point 2.....................')
    rospy.sleep(1)
    pub_pointStamped (9.915, -8.788, 0.0)
    rospy.loginfo_once('send_point 3.....................')
    rospy.sleep(1)
    pub_pointStamped (9.878, -9.889, 0.0)
    rospy.loginfo_once('send_point 4.....................')
    rospy.sleep(1)
    pub_pointStamped (12.484, -9.954, 0.0)

原文是用while 一直发送 改成了用 if 只发一次,以便发送多个点

调试过程中第一个点需要发两遍才能生效,后来发现是因为init_node 和生成publisher放在了子函数中!!!

还有一个问题是改成if 后 rospy.Rate()不能删,不知道为什么 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值