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

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

# rospy.spin()
rate = rospy.Rate(2)
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
  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

accept rate: 0%


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


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!')
signal.signal(signal.SIGINT, signal_handler)
print('Press Ctrl+C')
permanent link

answered 24 Mar, 18:27

marcoarruda's gravatar image

marcoarruda ♦♦
accept rate: 20%

Your answer
toggle preview

Follow this question

By Email:

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



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:


question asked: 05 Mar, 22:36

question was seen: 594 times

last updated: 24 Mar, 18:27

powered by OSQA