如何前后移动机器人直到执行在 python 内停止

How to move robot forward and backward until the execution is stop in python

有两个问题想请教一下。首先,我想让机器人在 4 秒内向前移动,在 4 秒内向后移动,直到我自己停止程序。我有 运行 这段代码可以让机器人前后移动,没有错误。但是机器人不动。我可以知道为什么吗?有没有人可以帮助我进行新的编码?

#! /usr/bin/env python

import rospy
from geometry_msgs.msg import Twist
import time

rospy.init_node('lastforward')
rate=rospy.Rate(4)
pub=rospy.Publisher('/cmd_vel', Twist, queue_size=1)

move=Twist()
def move_x_secs(secs):
    move.linear.x = 0.4
    time.sleep(secs)
    move.linear.x = 0.0
    time.sleep(secs)
    move.linear.x = -0.4
    time.sleep(secs)
    move.linear.x = 0.0
    time.sleep(secs)
    return

move_x_secs(4)


while not rospy.is_shutdown():
    pub.publish(move)
    rate.sleep()

但是当我删除一些代码只是为了测试代码时,机器人在等待4秒后可以向前移动,当我中止程序时,机器人继续移动。我可以知道为什么吗?

#! /usr/bin/env python

import rospy
from geometry_msgs.msg import Twist
import time

rospy.init_node('lastforward')
rate=rospy.Rate(4)
pub=rospy.Publisher('/cmd_vel', Twist, queue_size=1)

move=Twist()
def move_x_secs(secs):
    move.linear.x = 0.4
    time.sleep(secs)

move_x_secs(4)

while not rospy.is_shutdown():
    pub.publish(move)
    rate.sleep()

Twist 对象只是您通过发布者发送的东西 - 分配给扭曲对象的属性没有任何作用,让机器人移动的东西是通过发送 Twist 对象出版商。

因此,您的第一个代码片段修改了您的 Twist 对象 movex 组件,使其最终值为 0.0,然后发布该对象,告诉机器人停止的效果。

为了真正让机器人前后移动,每次修改move.linear.x.

后需要做pub.publish(move)

move.linear.x = 0.4 仅在您 publish.

时生效

在第一个示例中,您只发布了最终值。这就是它不会移动的原因。

#! /usr/bin/env python

import rospy
from geometry_msgs.msg import Twist
import time

rospy.init_node('lastforward')
rate=rospy.Rate(4)
pub=rospy.Publisher('/cmd_vel', Twist, queue_size=1)

move=Twist()
def move_x_secs(secs):
    # set speed to 0.4 but not yet letting the robot know
    move.linear.x = 0.4
    time.sleep(secs)

    # set speed to 0.0 but not yet letting the robot know
    move.linear.x = 0.0
    time.sleep(secs)

    # set speed to -0.4 but not yet letting the robot know
    move.linear.x = -0.4
    time.sleep(secs)

    # setting speed to 0.0, the final value
    move.linear.x = 0.0
    time.sleep(secs)
    return

move_x_secs(4)


while not rospy.is_shutdown():
    # after setting the speed from 0.4 to 0 to -0.4 back to 0, letting the robot know its speed is the final value, 0
    pub.publish(move)
    rate.sleep()

在第二个例子中,它确实会移动,因为 0.4 是最终值。

#! /usr/bin/env python

import rospy
from geometry_msgs.msg import Twist
import time

rospy.init_node('lastforward')
rate=rospy.Rate(4)
pub=rospy.Publisher('/cmd_vel', Twist, queue_size=1)

move=Twist()
def move_x_secs(secs):
    move.linear.x = 0.4
    time.sleep(secs)

move_x_secs(4)

while not rospy.is_shutdown():
    pub.publish(move)
    rate.sleep()

之所以先等4秒,是因为你先调用函数,用time.sleep(4),然后发布。

如果你想让你的机器人向前移动 4 秒,向后移动 4 秒,你应该尝试类似下面的代码。

#! /usr/bin/env python

import rospy
from geometry_msgs.msg import Twist
import time

rospy.init_node('lastforward')
rate=rospy.Rate(4)
pub=rospy.Publisher('/cmd_vel', Twist, queue_size=1)


while not rospy.is_shutdown():
    move = Twist()

    # move forward
    move.linear.x = 0.4
    pub.publish(move)
    rospy.sleep(4)

    # move backward
    move.linear.x = -0.4
    pub.publish(move)
    rospy.sleep(4)

    
    # stop
    move.linear.x = 0.0
    pub.publish(move)
    rospy.sleep(4)

请注意,在使用 rospy 模块时不应使用 time.sleep(),而应使用 rospy.sleep()rospy.rate()