Unit 4 Exercise 4.1 robot doesn't move & shuts down

I copied the code provided by the notebook into the test_process.py and start the process with

rosrun move_bb8_pkg test_process.py

but the simulation does not run and differently than in the pictures of the notebook the output of the system is not just telling me

[INFO] [1700753315.894701, 4170.406000]: Moving BB-8 forward!!
...

but also

[INFO] [1700753315.897402, 4170.408000]: shutdown time! Stop the robot

Code in my file:

#!/usr/bin/env python

import rospy
from geometry_msgs.msg import Twist

class MoveBB8():

        def __init__(self):
            self.bb8_vel_publisher = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
            self.cmd = Twist()
            self.ctrl_c = False
            self.rate = rospy.Rate(1)
            rospy.on_shutdown(self.shutdownhook)
            
        def publish_once_in_cmd_vel(self):
            """
            This is because publishing in topics sometimes fails the first time you publish.
            In continuous publishing systems, this is no big deal, but in systems that publish only
            once, it IS very important.
            """
            while not self.ctrl_c:
                connections = self.bb8_vel_publisher.get_num_connections()
                if connections > 0:
                    self.bb8_vel_publisher.publish(self.cmd)
                    break
                else:
                    self.rate.sleep()

        def shutdownhook(self):
            # works better than the rospy.is_shutdown()
            self.stop_bb8()
            self.ctrl_c = True
            
        def stop_bb8(self):
            rospy.loginfo("shutdown time! Stop the robot")
            self.cmd.linear.x = 0.0
            self.cmd.angular.z = 0.0
            self.publish_once_in_cmd_vel()
            
        def move_bb8(self, linear_speed=0.2):
            
            self.cmd.linear.x = -linear_speed
            self.cmd.angular.z = 0
            
            while not self.ctrl_c:
                self.publish_once_in_cmd_vel()
                rospy.loginfo("Moving BB-8 forward!!")
                self.rate.sleep()
                
                self.stop_bb8()
                
if __name__ == '__main__':
    rospy.init_node('process_test', anonymous=True)
    movebb8_object = MoveBB8()
    try:
        movebb8_object.move_bb8()
    except rospy.ROSInterruptException:
        pass


Code I copied from the textbook:

#!/usr/bin/env python

import rospy
from geometry_msgs.msg import Twist

class MoveBB8():

    def __init__(self):
        self.bb8_vel_publisher = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
        self.cmd = Twist()
        self.ctrl_c = False
        self.rate = rospy.Rate(1)
        rospy.on_shutdown(self.shutdownhook)

    def publish_once_in_cmd_vel(self):
        """
        This is because publishing in topics sometimes fails the first time you publish.
        In continuous publishing systems, this is no big deal, but in systems that publish only
        once, it IS very important.
        """
        while not self.ctrl_c:
            connections = self.bb8_vel_publisher.get_num_connections()
            if connections > 0:
                self.bb8_vel_publisher.publish(self.cmd)
                break
            else:
                self.rate.sleep()

    def shutdownhook(self):
        # works better than the rospy.is_shutdown()
        self.stop_bb8()
        self.ctrl_c = True

    def stop_bb8(self):
        rospy.loginfo("shutdown time! Stop the robot")
        self.cmd.linear.x = 0.0
        self.cmd.angular.z = 0.0
        self.publish_once_in_cmd_vel()

    def move_bb8(self, linear_speed=0.2):

        self.cmd.linear.x = -linear_speed
        self.cmd.angular.z = 0

        while not self.ctrl_c:
            self.publish_once_in_cmd_vel()
            rospy.loginfo("Moving BB-8 forward!!")
            self.rate.sleep()

        self.stop_bb8()

if __name__ == '__main__':
    rospy.init_node('process_test', anonymous=True)
    movebb8_object = MoveBB8()
    try:
        movebb8_object.move_bb8()
    except rospy.ROSInterruptException:
        pass

What is wrong?

Your code has an error right here:

        while not self.ctrl_c:
            self.publish_once_in_cmd_vel()
            rospy.loginfo("Moving BB-8 forward!!")
            self.rate.sleep()
            
           [u] self.stop_bb8()[/u]

It has to be:

    while not self.ctrl_c:
        self.publish_once_in_cmd_vel()
        rospy.loginfo("Moving BB-8 forward!!")
        self.rate.sleep()

   [u] self.stop_bb8()[/u]

if I change it to:

        while not self.ctrl_c:
            self.publish_once_in_cmd_vel()
            rospy.loginfo("Moving BB-8 forward!!")
            self.rate.sleep()
            
            [u] self.stop_bb8()[/u]

it tells me:

  File "/home/user/catkin_ws/src/linux_course_files/move_bb8_pkg/my_scripts/test_process.py", line 50
    [u] self.stop_bb8()[/u]
        ^
SyntaxError: invalid syntax

Hi,
I looked into this problem and I think problem is indentation.
In Python, indentation is important. In your case, every code indented will be treated as a part of while loop.

Your code:

      while not self.ctrl_c:
            self.publish_once_in_cmd_vel()
            rospy.loginfo("Moving BB-8 forward!!")
            self.rate.sleep()
            self.stop_bb8()   # this is inside of while loop

Textbook:

      while not self.ctrl_c:
            self.publish_once_in_cmd_vel()
            rospy.loginfo("Moving BB-8 forward!!")
            self.rate.sleep()
      self.stop_bb8()   # this is outside of while loop

Hope this helps you to fix your problem.

Regards,
Sangtaek

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.