Problem with prerequisites exam

Hi, i have not been able to go past the main in task3.py, which states that the robot should stop after crossing the walls.I have spend a lot of time on this, and now i guess its time to ask the communities opinion.

The problem i am facing are two:
1. The robot does not move in a straight line and deviates slightly from the path as it moves forward. Is this error programmed into to mimic real-life scenarios or is it a fault in my code.
I have attached pics of the ideal path in red vs the actual path taken in blue.
At the start:

Code used task2.py:

#! usr/bin/env python
import rospy
from robot_control_class import RobotControl
RC=RobotControl()
while(RC.get_laser(360)>=1):
    print(RC.get_laser(360))
    RC.move_straight_time("forward",0.125,2)
#the below line makes it to turn 90 degree approx, but was commented out when running the program since the problem at hand is the variation while moving in straight line.
#RC.turn("anticlockwise",0.25,5.95)


**After **

The line RC.move_straight_time("forward",0.125,2) should have made the robot move straight, but it deviates towards the end. Since these varantions are not the same everytime, RC.turn("anticlockwise",0.25,5.95) also gives different outputs making it difficult to position it straight into the wall opening.

Even though the robot is running at a low speed of 0.125, its still deviating from the path slightly towards the end. Can i know why this happens? Because of this, when i turn 90 degree to face the opening, it does not aligh perfectly with the wall opening, causing it to crash into the side of the opening.

2.under task3, the robot has to move past the opening and stop after crossing the wall. in my case as soon as it reaches the wall, it stops moving, can i know why?.

class ExamControl:
    def __init__(self):
        self.RC=RobotControl()
    def get_laser_readings(self):
        return self.RC.get_laser(719),self.RC.get_laser(0)
    def main(self):
        while(((self.RC.get_laser(0)!=inf) and (self.RC.get_laser(719)!=inf))):
            print("Right=",self.RC.get_laser(0))
            print("Left",self.RC.get_laser(719))
            print("Moving Forward")
            self.RC.move_straight_time("forward",0.3,1)
            print("Moved Forward")
            rospy.sleep(0.2)

Hello @Joseph1001 ,

  1. About question 1, yes, it is normal. You could correct this deviation by using odometry data, but that’s not the purpose of this course (exam). I would just try some combinations that work. If you want a more precise rotation based on degrees, you can make use of the rotate() function used in the Python course project.

  2. In your while loop, you are checking that both conditions are met (using and):

(self.RC.get_laser(0)!=inf) and (self.RC.get_laser(719)!=inf)

Therefore, as soon as one of these conditions are not met, the robot will stop. When you reach the wall, the left laser probably starts returning the inf value, so your robot stops.

Hope this helps,

Thanks @albertoezquerro, I should have gone with OR in this case. Thanks for helping out.

I tried the program again and everything works as it should in simulation but getting an error during auto correction. I double checked that the previous errors don’t repeat, could you share your opinion on this?

The robot final position in simulation
image
The robot final position in auto-correction:
image

It either stops before the opening or continues moving forward even after the opening is reached.

The code used is as follows:

#! usr/bin/env python
import rospy
from robot_control_class import RobotControl
from math import inf,isinf

class ExamControl:
    def __init__(self):
        self.RC=RobotControl()
    def get_laser_readings(self):
        return self.RC.get_laser(719),self.RC.get_laser(0)
    def main(self):
        print(len(self.RC.get_laser_full()))
        while(((self.RC.get_laser(0)!=inf) or (self.RC.get_laser(719)!=inf))):
            print("Right=",self.RC.get_laser(0))
            print("Left",self.RC.get_laser(719))
            print("Moving Forward")
            self.RC.move_straight_time("forward",0.3,1)
            print("Moved Forward")
            if ((self.RC.get_laser(0)==inf) and (self.RC.get_laser(719)==inf)):
                self.RC.stop_robot()
                print("----stop-----")
                break
            rospy.sleep(0.2)

        print("---------SUCCESS-----------")

Hi @albertoezquerro @bayodesegun , I am getting different outputs in simulation and in the auto-correction as shown in detail in the previous post. Is this a problem with autocorrection or is it an error in code?

Hello @Joseph1001 ,

I think this is an issue related to the timeout for the correction of Task 3. In your approach, the robot is taking longer than expected (by the script) to get out of the maze. I’ve increased this timeout in the correction script. Could you check if this solved your issue?

Hi @Joseph1001,

adding to what Alberto has mentioned, a possible reason for the timeout is that in your code you read the laser too many times.

You should ideally save the values in variables instead of doing many laser readings.

Your main method could be optimized to something like:

#! usr/bin/env python
import rospy
from math import inf, isinf
from robot_control_class import RobotControl


class ExamControl:
    def __init__(self):
        self.RC = RobotControl()

    def get_laser_readings(self):
        return self.RC.get_laser(719), self.RC.get_laser(0)

    def main(self):
        print(len(self.RC.get_laser_full()))
        while True:
            laser0 = self.RC.get_laser(0)
            laser719 = self.RC.get_laser(719)

            print("Right=", laser0)
            print("Left", laser719)
            
            if (laser0 != inf) or (laser719 != inf):
                print("Moving Forward")
                self.RC.move_straight_time("forward", 0.3, 1)
            else:
                print("----Stopping----")
                self.RC.stop_robot()
                print("----Stopped-----")
                break

            rospy.sleep(0.2)

        print("---------SUCCESS-----------")

This way you would be reducing the readings from 6 to 2 in each iteration, making your code finish faster.

Hi @ralves , thanks for pointing it out. I wrote the code to get the most updated values from the laser, but it this method saves time and this time it only stops after getting past the wall.

Unfortunately, though the simulation works, the auto correction is still giving me an error.

Hi, I am still getting an error on task3.py at main().

Further, to prevent the timeout, I increased the speed of the robot drastically to around 1 for 1 sec. The robot now “drifts” across the maze, while stopping 1 meter before the wall.

Even though the time to get out of the maze is less, it’s still showing error. I am not sure what seems to be the problem.

Task2.py

#! usr/bin/env python
import rospy
from robot_control_class import RobotControl
RC=RobotControl()
while(RC.get_laser(360)>=1):
    print(RC.get_laser(360))
    RC.move_straight_time("forward",1,1)
# RC.turn("anticlockwise",0.25,5.95)
RC.rotate(40)


Task3.py

#! usr/bin/env python
import rospy
from robot_control_class import RobotControl
from math import inf,isinf

class ExamControl:
    def __init__(self):
        self.RC=RobotControl()
    def get_laser_readings(self):
        return self.RC.get_laser(719),self.RC.get_laser(0)
        
    # def main(self):
    #     while(((self.RC.get_laser(0)!=inf) or (self.RC.get_laser(719)!=inf))):
    #         print("Right=",self.RC.get_laser(0))
    #         print("Left",self.RC.get_laser(719))
    #         print("Moving Forward")
    #         self.RC.move_straight_time("forward",0.3,1)
    #         print("Moved Forward")
    #         if ((self.RC.get_laser(0)==inf) and (self.RC.get_laser(719)==inf)):
    #             self.RC.stop_robot()
    #             print("----stop-----")
    #             break
    #         rospy.sleep(0.2)

    #     print("---------SUCCESS-----------")

    
   
   
    def main(self):
        print(len(self.RC.get_laser_full()))
        while True:
            laser0 = self.RC.get_laser(0)
            laser719 = self.RC.get_laser(719)

            print("Right=", laser0)
            print("Left", laser719)
            
            if (laser0 != inf) or (laser719 != inf):
                print("Moving Forward")
                self.RC.move_straight_time("forward", 0.5, 1)
            else:
                print("----Stopping----")
                self.RC.stop_robot()
                print("----Stopped-----")
                break

            rospy.sleep(0.2)

        print("---------SUCCESS-----------")





# EC=ExamControl()
# EC.main()

Hello @Joseph1001 ,

I’m suspecting it might be caused by the inf variable usage. Could you please replace this variable by, for instance, this:

if (laser0 != float("inf")) or (laser719 != float("inf")):

Remember to also remove the import at the top from math import inf,isinf.

Please let me know if this fixes your issue.

Hi @albertoezquerro , SUCCESS… replacing the inf with float(“inf”) worked. Thanks a lot of helping out.

Can you also share what actually happened when I replaced math.inf with float(“inf”)?
Upon experimenting, i got the same result:

#! usr/bin/env python
import math
print("Regular inf=",float("inf"))
print("math.inf=",math.inf)

Gave the output:

Regular inf= inf
math.inf= inf
1 Like

Hello @Joseph1001 ,

This is a problem on our end, nothing to do with your code. For some reason, the math.inf module is not being imported correctly. So basically we do the same (float("inf")) without having to import/use the math.inf module. I’ll work on solving this bug.

Thanks @albertoezquerro for helping me out through out this topic. I would not have been able to get this topic closed without yours and @ralves active support.

1 Like