Help on my basic turtlbot node
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 ...
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)?
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.
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".
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!
All I have running in the terminal is roscore, turtlebot_bringup minimal.launch, and a workstation terminal to run my program directly.
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....