Ask Your Question
0

Help on my basic turtlbot node

asked 2016-08-02 12:51:28 -0500

SamSam gravatar image

updated 2016-08-03 12:42:26 -0500

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    
        rospy.on_shutdown(self.shutdown)

    # 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


        rospy.spin()


    #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 

        else: 
            bump = False 

        rospy.loginfo("Bumper Event") 
        rospy.loginfo(data.bumper)





        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)

        rospy.spin()




    #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:
                self.cmd_vel.publish(move_cmd)      

            else:
             #45 deg/s in radians/s
            # publish the velocity
                self.cmd_vel.publish(move_cmd2)
                self.cmd_vel.publish(turn_cmd)
            # wait for 0.1 seconds (10 HZ) an
            #d publish again
        r.spin()

    def shutdown(self):
         # stop turtlebot
        rospy.loginfo("Stop TurtleBot")
         # a default Twist has linear.x of 0 and angular.z ...
(more)
edit retag flag offensive close merge delete

Comments

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)?

Steven_Daniluk gravatar imageSteven_Daniluk ( 2016-08-03 09:02:07 -0500 )edit

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.

SamSam gravatar imageSamSam ( 2016-08-03 10:40:36 -0500 )edit

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".

SamSam gravatar imageSamSam ( 2016-08-03 10:45:20 -0500 )edit

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!

SamSam gravatar imageSamSam ( 2016-08-03 10:49:19 -0500 )edit

All I have running in the terminal is roscore, turtlebot_bringup minimal.launch, and a workstation terminal to run my program directly.

SamSam gravatar imageSamSam ( 2016-08-03 10:53:06 -0500 )edit

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....

SamSam gravatar imageSamSam ( 2016-08-03 11:08:49 -0500 )edit

4 Answers

Sort by ยป oldest newest most voted
1

answered 2016-08-03 13:49:17 -0500

ahendrix gravatar image

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).

edit flag offensive delete link more

Comments

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

SamSam gravatar imageSamSam ( 2016-08-03 14:12:53 -0500 )edit

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!

SamSam gravatar imageSamSam ( 2016-08-03 15:09:58 -0500 )edit

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.

ahendrix gravatar imageahendrix ( 2016-08-03 15:36:32 -0500 )edit

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?

SamSam gravatar imageSamSam ( 2016-08-04 09:27:45 -0500 )edit
1

answered 2016-08-03 12:47:42 -0500

Steven_Daniluk gravatar image

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.

edit flag offensive delete link more

Comments

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....

SamSam gravatar imageSamSam ( 2016-08-03 13:26:55 -0500 )edit
0

answered 2016-08-03 13:37:36 -0500

SamSam gravatar image

updated 2016-08-03 13:38:14 -0500

I changed my while not rospy.is_shutdown(): to

while not rospy.is_shutdown():
        self.cmd_vel.publish(move_cmd)
        if bump==True:
                  #45 deg/s in radians/s
            # publish the velocity
            self.cmd_vel.publish(move_cmd2)
            self.cmd_vel.publish(turn_cmd)
            # wait for 0.1 seconds (10 HZ) an
            #d publish again 

        else: 
            continue 

    r.spin()

It seems like this logic is better but Im still not sure why my node will only publish this last command. It seems like my script does not go into this while loop at all.....

edit flag offensive delete link more

Comments

Is there something wrong when I am setting up the publishers and subscribers?

SamSam gravatar imageSamSam ( 2016-08-03 14:03:12 -0500 )edit
0

answered 2016-09-30 09:00:26 -0500

RobB gravatar image

I'm beginner at this too, trying to code the same functionality.

Don't you need something like this at the end of your program?

if __name__ == '__main__':
try:
    goforward_bumpandturn())
except rospy.ServiceException as exc:
   rospy.loginfo("GoForward node terminated.")
   print("Service did not process request: " + str(exc))

You need to create an instance of your class. The "except" bit should give a meaningful error message.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-08-02 12:51:28 -0500

Seen: 1,652 times

Last updated: Aug 03 '16