Hello,
please i need help with the right code to stop my program after the robot performs the desired movement. It seems that my program never ends so this affects the grading system. I have spent hours on this but not making any progress. My code is shown below, Thank you.
#! /usr/bin/env python
import rospy
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist
class RobotMove(object):
def __init__(self):
rospy.init_node('Robot_move')
#self._topic_name = topic_name
self._laser_sub = rospy.Subscriber('/kobuki/laser/scan', LaserScan, self.callback)
self._laser_scan = LaserScan()
self._cmd_pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
self._twist_move = Twist()
self.ctrl_c = False
rospy.on_shutdown(self.shutdownhook)
#rospy.spin()
def callback(self, msg):
self._laser_scan = msg
self.laser_scan_to_twist()
return self._laser_scan
def laser_scan_to_twist(self):
if self._laser_scan.ranges[0]<5:
self._twist_move.linear.x = 0.2
self._twist_move.angular.z = 0
self._cmd_pub.publish(self._twist_move)
if self._laser_scan.ranges[360]<=1.7:
self._twist_move.linear.x = 0.0
self._twist_move.angular.z = -2.5
self._cmd_pub.publish(self._twist_move)
if self._laser_scan.ranges[360]>7:
self._twist_move.linear.x = 0.0
self._twist_move.angular.z = 0.0
self._cmd_pub.publish(self._twist_move)
def shutdownhook(self):
# works better than the rospy.is_shut_down()
#global ctrl_c
print "shutdown time!"
self.stop()
self.ctrl_c = True
if name == ‘main’:
robotmove_object = RobotMove()
rate = rospy.Rate(2)
rospy.spin()
def stop(self):
self._twist_move.linear.x = 0.0
self._twist_move.angular.z = 0.0
self._cmd_pub.publish(self._twist_move)