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?
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
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()
#!/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
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):
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: