我基本上是想得到一个既发布又订阅的节点
假设我有两个节点,node1发布,count_节点订阅并发布
当node1发布“开始计数”时,我希望count\u节点开始计数并发布计数值
如果节点1在计数节点计数时发布为“停止计数”,我希望计数节点停止活动计数
下面是我的尝试代码。我在Ubuntu 18.04上使用ROS Melodic。这个{a1}中给出了一个类似的问题,但我想要答案的部分从未得到回答
到目前为止,我的尝试失败了,因为当我收到启动计数的消息时,回调函数调用了一个函数(startCount),该函数使用while循环递增
因此,在计数完成之前,count_节点无法处理消息以停止计数,并且在计数完成后调用stopCount函数,而不是在计数时调用
有没有办法做我想做的事
以下是我在count_节点的尝试:
import rospy
from std_msgs.msg import String
from std_msgs.msg import Int32
def callback(data):
rospy.loginfo(rospy.get_caller_id() + ' I heard: %s', data.data)
if (data.data=="start count"):
startSanitization()
elif (data.data=="stop count"):
stopSanitization()
def startCount():
percent = 0
while percent < 101 :
rospy.loginfo(percent)
pub.publish(percent)
percent = percent + 1
rate.sleep()
def stopCount():
percent = 0
rospy.loginfo(percent)
pub.publish(percent)
def listener():
# In ROS, nodes are uniquely named. If two nodes with the same
# name are launched, the previous one is kicked off. The
# anonymous=True flag means that rospy will choose a unique
# name for our 'listener' node so that multiple listeners can
# run simultaneously.
#check for message on Topic
rospy.Subscriber('brwsrButtons', String, callback)
rospy.loginfo("hello")
# spin() simply keeps python from exiting until this node is stopped
rospy.spin()
if __name__ == '__main__':
#create a unique node
rospy.init_node('count_node', anonymous=True)
#create a publisher object
pub = rospy.Publisher('progress', Int32, queue_size=10)
rate = rospy.Rate(10) #10Hz
#start the subscribing and publishing process
listener()
假设您的目标是:
brwsrButtons
,它发布一个字符串,其值可以是start count
或stop count
start count
,则需要增加percent
值,直到它达到100
(不确定之后要做什么)stop count
,则需要将percent
重置为0
progress
主题上发布此百分比同样假设
startSanitisation
和stopSanitisation
是指startCount
和stopCount
方法这可以通过使用}变量来实现,该变量跟踪百分比。在
global
{callback
方法中,就像检查字符串值一样,还要检查百分比是否小于100,如果小于100,则将其递增1,如果不小于,则将其置为0。 然后,在main
方法中,有一个循环一直运行到关闭节点,该节点将percent
发布到/progress
主题以下是修改后的代码:
更好的方法是使用OOP:
相关问题 更多 >
编程相关推荐