Code foundation for ROS - task3.py

Hello,

I have gotten my code foundation for ROS certificate at 90 percent but I really want to know why my task3.py runs perfectly but when the grading system runs it, I get this error “get_laser_readings method does not work properly” and the robot continues moving into the environment for a long time before it eventually stops. Please can you help me with this problem? My code is shown below, thank you.

#!/usr/bin/env python

import rospy

from geometry_msgs.msg import Twist

from sensor_msgs.msg import LaserScan

import time

class ExamControl(object):

def __init__(self):

    rospy.init_node('Robot_move_2')

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

    self.ctrl_c = False

    rospy.spin()

    rospy.on_shutdown(self.shutdownhook)

     


def callback(self, msg):

    self.laser_msg = msg

    self.get_laser_readings()

    self.wait()

def get_laser_readings(self):

    return [self.laser_msg.ranges[719], self.laser_msg.ranges[0]] 

def wait(self):

    self.main()

           

def main(self):

            

    self._twist_move.linear.x = 0.4

    self._twist_move.angular.z = -0.01

    self._cmd_pub.publish(self._twist_move)

        

    self.laser_msg.ranges[719]

    self.laser_msg.ranges[0]



    if self.laser_msg.ranges[0] > 6.9:

        self._twist_move.linear.x = 0.0

        self._twist_move.angular.z = 0.0

        self._cmd_pub.publish(self._twist_move)

        self.shutdownhook()

            

    else:
       pass

   

def shutdownhook(self):

    # works better than the rospy.is_shut_down()

    global ctrl_c

    time.sleep(10)

    rospy.signal_shutdown("Robot Execution done")

    #self.stop()

    print "shutdown time!"

    self.ctrl_c = True 

if name == ‘main’:

rospy.loginfo("Start Process")

#ExamControl_object = ExamControl()

rate = rospy.Rate(2)

Hello @Yelaina ,

The program you show has several logic errors (related with ROS), which are causing the grader to mark it as incorrect. In any case, the main idea of your program is wrong, since you are using ROS (you are creating the publishers, subscribers, etc…).

The purpose of this Exam (and task) is to use Python only, not ROS (ROS will be addressed in the ROS-related courses). So, you should create this task3.py script by just using Python functions, in this case, the Python functions provided by the RobotControl class (which already deals with ROS in the background), as you have been doing during the whole Python course.

You can have a look at this other post to see what I’m talking about: Got stuck again python exam task3.py

Best,

Thank you for your response @albertoezquerro. I was studying ROS for beginners before I saw this code foundation for ROS so I was mixing things up. My score is now 10/10, thank you!

3 Likes