Rosanswers logo

Hello ROS community, I am new to ROS and python programming but I am trying to write a node to have my turtlebot wander around the room bump into things, backup, turn left and keep going. I want the turtlebot to keep doing this simple task as long as I have not pressed CTRL+C. I am borrowing most of the code from Mark Silliman's goforward.py script and combining that with a bump subscriber code I found on the internet. When I run the script it in the terminal the robot does not move. I have turtlebot_bringup minimal.launch before i run it from the workstation. If someone more experienced could look at my code and point out why my node won't publish commands to the turtlebot that would be great. I am not quite familiar with some python syntax and I am sure i'm getting close.... Here is my code

#!/usr/bin/env python
# A very basic TurtleBot script that allows the TurtleBot to wander indefinitely. Press CTRL + C to stop.  To run:
# On TurtleBot:
# roslaunch turtlebot_bringup minimal.launch
# On work station:
# python goforward_bumpandturn.py

import rospy
from geometry_msgs.msg import Twist
from kobuki_msgs.msg import BumperEvent
from math import radians
class goforward_bumpandturn():
    def __init__(self):
        # initiliaze
        rospy.init_node('goforward_bumpandturn', anonymous=False, log_level=rospy.INFO)

        # tell user how to stop TurtleBot
        rospy.loginfo("To stop TurtleBot CTRL + C")

        # What function to call when you ctrl + c    
    # Create a publisher which can "talk" to TurtleBot and tell it to move
        # Tip: You may need to change cmd_vel_mux/input/navi to /cmd_vel if you're not using TurtleBot2
        self.cmd_vel = rospy.Publisher('cmd_vel_mux/input/navi', Twist, queue_size=100)
        # Twist is a datatype for velocity
        r = rospy.Rate(10);
        move_cmd = Twist()
    # let's go forward at 0.2 m/s
        move_cmd.linear.x = 0.2
    # let's turn at 0 radians/s
        move_cmd.angular.z = 0

    #def processBump(self,data):  (do I need this?)

        self.bump_Subscriber=rospy.Subscriber('mobile_base/events/bumper', BumperEvent,self,queue_size=10)
       global bump 
       if (state == BumperEvent.PRESSED): 
            bump = True 

            bump = False 

        rospy.loginfo("Bumper Event") 



        move_cmd2 = Twist()
        move_cmd2.linear.x = (-0.2)
        move_cmd2.angular.z = 0

        turn_cmd = Twist()
        turn_cmd.linear.x = 0
        turn_cmd.angular.z = radians(45)


    #if bump data is received, process here 
    #data.bumper: LEFT (0), CENTER (1), RIGHT (2) 
    #data.state: RELEASED(0), PRESSED(1) 

 # This is the logic statement that tells the robot to publish wheel comands as long as ctrl C is not pressed
 # as long as you haven't ctrl + c keeping doing...
        while not rospy.is_shutdown():

            if bump==False:
             #45 deg/s in radians/s
            # publish the velocity
            # wait for 0.1 seconds (10 HZ) an
            #d publish again

    def shutdown(self):
         # stop turtlebot
        rospy.loginfo("Stop TurtleBot")
         # a default Twist has linear.x of 0 and angular.z of 0.  So it'll stop TurtleBot
         # sleep just makes sure TurtleBot receives the stop command prior to shutting down the script

if __name__ == '__main__':
        rospy.loginfo("goforward_bumpandturn node terminated.")

Originally posted by SamSam on ROS Answers with karma: 1 on 2016-08-02

Post score: 0

Original comments

Comment by Steven_Daniluk on 2016-08-03:
Two questions since you mentioned you are new to ROS: 1) Do you have the networking setup between the turtlebot and the workstation? 2) Do you have one of the the turtlebot teleop nodes running when you test this (it will have higher priority in the cmd_vel mux and block your messages)?

Comment by SamSam on 2016-08-03:
Yes I have ROS networked to the turtlebot and I have been going through the tutorials on the ROS wiki and the turtlebot website. I can get Mark Sillimans goforward.py to run as well as many other nodes doing other tasks with the xbox connect.

Comment by SamSam on 2016-08-03:
I am getting relatively familiar with ROS I just think my python coding is very unfamiliar. I am an expert in matlab coding so i'm not completely new to programing and logic. When I run the code from the workstation it prompts me the with "to stop turtlebot CTRL+C".

Comment by SamSam on 2016-08-03:
This is what the terminal prompts when the turtlebot is moving when i run other turtlebot tutorial nodes so I figure my code is cycling but no movement happens. Any Ideas? By the way thank you for responding so quickly. The ROS community is great!

Comment by SamSam on 2016-08-03:
All I have running in the terminal is roscore, turtlebot_bringup minimal.launch, and a workstation terminal to run my program directly.

Comment by SamSam on 2016-08-03:
When I run rqt_graph the graph shows that the bump sensor is sending point cloud information... I am pretty sure I am subscribing to bumper events and not a point cloud....


2 Answers 2


Rosanswers logo

rospy.spin() is a convenience function that blocks until rospy shuts down. You don't need to call if if you have a while loop that explicitly checks rospy.is_shutdown() (you need one or the other, but you don't need both).

Originally posted by ahendrix with karma: 47576 on 2016-08-03

This answer was ACCEPTED on the original site

Post score: 1

Original comments

Comment by SamSam on 2016-08-03:
Thank you.I am new to python and im sure my code looks redundant and sloppy but I appreciate all the pointers I can get. One thing I am not used to is the white space in the code and how that effects the order in which it is preformed. I am also confused on how and when to use def and class

Comment by SamSam on 2016-08-03:
I am basicaly learning python via looking at turtlebot tutorial nodes and copying there syntax. Im not sure wht parts of my code are necessary and what kinda syntax is needed. The only example of publisher subscriber nodes on the internet are the talker and listener tutorials and those dont help me!

Comment by ahendrix on 2016-08-03:
I cannot give you a python tutorial in the space of this comment box, and this isn't the forum for it. Perhaps you should start with some basic python tutorials to get familiar with the syntax of Python instead of trying to learn Python and ROS at the same time.

Comment by SamSam on 2016-08-04:
I have been reading a couple of books on python and ROS as well as doing tutorials for both. I feel like this code is close to working but Im out of debugging ideas... do you know of any other good turtle bot code I can look at to try to get an idea of what in intermediate level node looks like?


Rosanswers logo

Basically what you are trying to do is publish velocity messages on /cmd_vel_mux/input/navi, then have the turtlebot listen to them and obey the commands. This requires that the messages are actually being published, and that they are being published under a name the turtlebot knows to listen for, which should be /cmd_vel_mux/input/navi.

So, first step is to check if your script is actually publishing those velocity command messages. While your script is running, run rostopic list, and you should see your topic /cmd_vel_mux/input/navi in the list. If not, then you need to revisit your script to find the issue.

Then, run rostopic echo /cmd_vel_mux/input/navi and watched the published messages, you should see the values hard coded in your script. If you do, then your script is publishing messages properly.

If you are using the default turtlebot launcher minimal.launch then the velocity mux should be running and listening to /cmd_vel_mux/input/navi, so I don't suspect the problem is there.

Between using rosnode info <node_name> and the rostopic commands you should be able to follow the messages between nodes to make sure everything is connected.

Originally posted by Steven_Daniluk with karma: 606 on 2016-08-03

This answer was NOT ACCEPTED on the original site

Post score: 1

Original comments

Comment by SamSam on 2016-08-03:
The only Publication happens when I hit CTRL+C and it publishes the command for x,y,and z to have zero velocity. This publish line is under the definition for shutdown. The only difference between my publish statements and this one is that this publish statement has the argument Twist....


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.