Coding error turtle movement

why does this not move my turtle when i do the rosrun command. It doesnt give me an error but it also doesnt move my robot

If I understand correctly, I am setting it to be able to publish values to “/cmd_vel”, I am setting the values I want with the function turtlemovement and then I am telling it to actually publish by command j.turtlemovement(). Shouldnt this move my robot?

Thank you!

Is this a better structure?
It is still not working if I use this structure

Hi, can you please try removing the ‘rospy.spin()’ from ‘move_robot’ and add the following to the end of your script.

rate = rospy.Rate(20)
if name == ‘main’:
while not rospy.is_shutdown():
vel_pub = CmdVelPub()
vel_pub.move_robot(‘forward’)
rate.sleep()

I hope this helps :sunny:

The method defined to publish in ‘/cmd/vel’ is never called in the first example

I did this (except changed the ’ to ") and defined a random string to the var “name” before starting the class. the program seems to “run” without error but there is no movement of the robot

in regards to: The method defined to publish in ‘/cmd/vel’ is never called in the first example

how do I do this? I tried adding a function later but it said: “publisher” object is not callable

Please copy/paste the raw code here so we can easily correct it.

With reference to the very first code you shared, the last part of your code should look like this:

# Bring everything under "if name == main"
if __name__ == ‘__main__’:
  j = moveturt()
  j.turtlemovement()
  rospy.spin()  # this is optional, since the program publishes only once and does nothing after that

Furthermore, in def turtlemovement(self, ...):

  • Modify the method argument angular_speed = 0, unless you want your turtle to turn as it moves
  • add a new line after print("moving"): self.publish_once_in_cmd_vel()
  • remove the line rospy.spin()
#!/usr/bin/env python

import rospy

from geometry_msgs.msg import Twist

rospy.init_node('topic_publisher')

class moveturt():

    

    def __init__(self):

        self.turtpublish = rospy.Publisher('/cmd_vel', Twist, queue_size=1)

        self.cmd = Twist()

        self.rate = rospy.Rate(10) # 10hz

        self.publish_once_in_cmd_vel()

        rospy.on_shutdown(self.shutdownhook)

        

    def shutdownhook(self):

        # works better than the rospy.is_shutdown()

        self.ctrl_c = True

    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.turtpublish.get_num_connections()

            if connections > 0:

                self.turtpublish.publish(self.cmd)

                rospy.loginfo("Cmd Published")

                break

            else:

                self.rate.sleep()

    def turtlemovement(self, linear_speed=2, angular_speed=2):

        self.cmd.linear.x = linear_speed

        self.cmd.angular.z = angular_speed

        angular_speed = 0,

        rospy.loginfo("Moving")

        print("moving")

        self.publish_once_in_cmd_vel()

                    

if __name__ == '__main__':

  j = moveturt()

  j.turtlemovement()

  rospy.spin()  # this is optional since the program publishes only once and does nothing after that

the formating isnt great when I copy and paste so here is teh screen shot for reference:

I get the following error:

Traceback (most recent call last):
File “/home/user/catkin_ws/src/my_turtlebot_topics/scripts/step1bb.py”, line 47, in
j = moveturt()
File “/home/user/catkin_ws/src/my_turtlebot_topics/scripts/step1bb.py”, line 14, in init
self.publish_once_in_cmd_vel()
File “/home/user/catkin_ws/src/my_turtlebot_topics/scripts/step1bb.py”, line 27, in publish_once_in_cmd_vel
while not self.ctrl_c:
AttributeError: ‘moveturt’ object has no attribute ‘ctrl_c’

Ive tried to do things like
def shutdownhook(self, ctrl_c = True):
or
def shutdownhook(self, True):

but still get errors

update: Ive used the following and have been able to get some movement. However, i get the error:

user:~/catkin_ws$ rosrun my_turtlebot_topics step1bbc.py
[INFO] [1621188274.283000, 0.000000]: Moving
moving
Traceback (most recent call last):
File “/home/user/catkin_ws/src/my_turtlebot_topics/scripts/step1bbc.py”, line 33, in
j.turtpublish()
TypeError: ‘Publisher’ object is not callable

To get proper formatting, paste your code between a set of opening and closing tildes (I edited the code for you):

``` → opening tilde

← your code here →

``` → closing tilde

Now to the code. It should be something like:

#!/usr/bin/env python

import rospy
from geometry_msgs.msg import Twist
rospy.init_node('topic_publisher')

class moveturt():
    # This is equal to `self.ctrl_c = False` in def __init__(self)
    ctrl_c = False

    def __init__(self):
        self.turtpublish = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
        self.cmd = Twist()
        self.rate = rospy.Rate(10) # 10hz
        self.publish_once_in_cmd_vel()
        rospy.on_shutdown(self.shutdownhook)       

    def shutdownhook(self):

        # works better than the rospy.is_shutdown()
        self.ctrl_c = True

    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.turtpublish.get_num_connections()
            if connections > 0:
                self.turtpublish.publish(self.cmd)
                rospy.loginfo("Cmd Published")
                break
            else:
                self.rate.sleep()

    def turtlemovement(self, linear_speed=0.2, angular_speed=0):

        self.cmd.linear.x = linear_speed
        self.cmd.angular.z = angular_speed
        # angular_speed = 0  # remove this line
        rospy.loginfo("Moving")
        print("moving")

        self.publish_once_in_cmd_vel()
                    

if __name__ == '__main__':

  j = moveturt()
  j.turtlemovement()
  rospy.spin()  # this is optional since the program publishes only once and does nothing after that

I recommend that you look at the following resources to build your knowledge of Python and some ROS concepts:

  1. Python 3 for Robotics
  2. What is rospy.spin(), ros::spin(), ros::spinOnce(), and what are they for?
  3. Proper use of rospy.Rate or ros::Rate

removing rospy.spin() solved my problem

Im not sure why it creates a problem however. Shouldnt it just keep publishing into the same arguments to the /cmd_vel if it is continously running?

Also, I tried
self.ctrl_c = False in def init(self)
on a prior occastion but it gives me the following error:

AttributeError: ‘moveturt’ object has no attribute ‘ctrl_c’

Thank you!!
Very helpful support

1 Like

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