ROS Grading system freezing - Code foundation for ROS

Hello,

I have gotten to the end of the prerequisite exam but the grading system has been hanging for a long time so it is unable to mark my last two python scripts and give me my final score. Please I need your help with this so I can obtain my certificate.

Hello @Yelaina ,

The problem is that your program (task2.py) never ends. After the desired robot movement is achieved, the program has to stop.

Hello @albertoezquerro,

Thank you. I have tried to get the program to end all morning but I keep getting confused on how i can automatically initialize the rospy.on_shutdown(). Please see my code 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()

    

    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)

                        

    else:

        pass

def shutdownhook():

    # works better than the rospy.is_shut_down()

    global ctrl_c

    print "shutdown time!"

    ctrl_c = True

rospy.on_shutdown(shutdownhook)

if name == ‘main’:
robotmove_object = RobotMove()
rate = rospy.Rate(2)
ctrl_c = False

while not ctrl_c:
    data = robotmove_object.laser_scan_to_twist()
    rospy.loginfo(data)
    rate.sleep()