I am following the ROS in 5 days course. I am running a basic node which is publishing movement to the turtlebot. The bot behaves as I expect, however when I kill the instance that is publishing the node (ctrl+C), the robot keeps on spinning. I have no idea why nor how to kill the process... Here is my code:

#! /usr/bin/env python

import rospy
import signal
from std_msgs.msg import Int32 
from geometry_msgs.msg import Twist
from nav_msgs.msg import Odometry
from sensor_msgs.msg import LaserScan

rospy.init_node('guide_robot')
sub = rospy.Subscriber('/kobuki/laser/scan', LaserScan)

# rospy.spin()
rate = rospy.Rate(2)
rate.sleep()
count = Int32()
count.data = 0
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)

var = Twist()
while not rospy.is_shutdown(): 
  var.angular.z = 1
  var.linear.x = 0
  pub.publish(var)
  count.data += 1

The only way I've found to stop the robot is by running the code again, but publishing var.angular.z = 0. What am I missing?

Thank you

asked 05 Mar, 22:36

jer.pint%40gmail.com's gravatar image

jer.pint@gma...
0
accept rate: 0%


Hello,

Actually the problem is that the robot uses the last message you sent in the /cmd_vel topic. Even if you stop the node, the last message was a non-zero value, that's the reason it keeps moving.

I suggest two possibilities:

1: ROS node on_shutdown callback You can use the rospy.on_shutdown event callback to send a twist message equals to zero. Take a look at this reference: http://wiki.ros.org/rospy/Overview/Initialization%20and%20Shutdown

The code would be like that:

def myhook():
  print "shutdown time!"
  msg = Twist()
  publisher.publish(msg)

rospy.on_shutdown(myhook)

But ROS does not guarantee the messages are going to be publish (Yes, it is written in their documentation)

Python sigint detector

You can use python sigint detector. It would be something like: There is a topic discussing about this (https://stackoverflow.com/questions/1112343/how-do-i-capture-sigint-in-python)

#!/usr/bin/env python
import signal
import sys
def signal_handler(signal, frame):
        print('You pressed Ctrl+C!')
        sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
print('Press Ctrl+C')
signal.pause()
permanent link

answered 24 Mar, 18:27

marcoarruda's gravatar image

marcoarruda ♦♦
1764
accept rate: 21%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×17
×4
×1

question asked: 05 Mar, 22:36

question was seen: 413 times

last updated: 24 Mar, 18:27

powered by OSQA