Search code examples
callbackdelayrospublishersubscriber

Why is the turtlebot not moving continously?


if __name__ == '__main__':
    rospy.init_node('gray')
    settings = termios.tcgetattr(sys.stdin)
    pub = rospy.Publisher('cmd_vel', Twist, queue_size=1)
    x = 0
    th = 0
    node = Gray()
    node.main()

We make the publisher(cmd_vel) in main, and run the main function of class gray.

 def __init__(self):
        self.r = rospy.Rate(10)
        self.selecting_sub_image = "compressed"  # you can choose image type "compressed", "raw"
        if self.selecting_sub_image == "compressed":
            self._sub = rospy.Subscriber('/raspicam_node/image/compressed', CompressedImage, self.callback, queue_size=1)
        else:
            self._sub = rospy.Subscriber('/usb_cam/image_raw', Image, self.callback, queue_size=1)
        self.bridge = CvBridge()

init function makes a subscriber, which runs 'callback' when it gets data.

def main(self):
    rospy.spin()

Then it runs the spin() function.

v, ang = vel_select(lvalue, rvalue, left_angle_num, right_angle_num, left_down, red_dots)
self.sendv(v, ang)

Inside the callback function, it gets a linear speed and angular speed value, and runs a sendv function to send it to the subscribers.

 def sendv(self, lin_v, ang_v):
    twist = Twist()
    speed = rospy.get_param("~speed", 0.5)
    turn = rospy.get_param("~turn", 1.0)
    twist.linear.x = lin_v * speed
    twist.angular.z = ang_v * turn
    twist.linear.y, twist.linear.z, twist.angular.x, twist.angular.y = 0, 0, 0, 0
    pub.publish(twist)

and... sendv function sends it to the turtlebot. It has to move continuously, because if we do not publish data, it still has to move with the speed it got from the last publish. Also, callback function runs every 0.1 seconds, so it keeps sending data.

But it does not move continously. It stops for a few seconds, and go for a very short time, and stops again, and go for a very short time, and so on. The code which selects the speed works correctly, but the code who sents it to the turtlebot does not work well. Can anyone help?


Solution

  • Also, callback function runs every 0.1 seconds.

    I believe this is incorrect. I see that you have made a self.r object but never used it anywhere in the code to achieve an update rate of 10hz. If you want to run the main loop at every 0.1 seconds, you will have to call your commands within the following loop (see rospy-rates) before calling rospy.spin():

    self.r = rospy.Rate(10)
    while not rospy.is_shutdown():
      <user commands>
      self.r.sleep()
    

    However, this would not help you either since your code is publishing to /cmd_vel within the subscriber callback which gets called only on receiving some data from the subscriber. So basically, your /cmd_vel is not being published at the rate of 10hz but rather at the rate at which you are receiving the data from the subscribed topic ('/raspicam_node/image/compressed'). Since these are image topics, they might be taking a lot of time to be updated hence the delay in your velocity commands to the robot.