Stop Ros Program - Task2.py Code foundation for ROS

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)

Hi,
Could you confirm what you ask in Task2?
It appears to me to just make the robot stop at a distance of less than a meter and then rotate 90 degrees.

Hello,

The grading system doesnt move past task2.py so i cant get my result. I was told that the reason is because my program never ends. I need to make my program end after achieving the desired robot movement but i dont know how to. Thank you.

Hi Clistenes,

Please don’t bother with my question, I figured out how to solve the issue so I am now debugging task3.py. Thank you

1 Like

:+1:

have good studies

1 Like