DexterInd / GoPiGo3

The GoPiGo3 is a Raspberry Pi Robot!
https://gopigo.io
Other
98 stars 85 forks source link

ObstacleAvoidanceRobot Does Not Fail Correctly For No Distance Sensor Case #317

Closed slowrunner closed 2 years ago

slowrunner commented 2 years ago

A student with a Grove Ultrasonic Ranger "distance sensor" attempted to run Dexter/GoPiGo3/Projects/ObjectAvoidanceRobot/object_avoidance_robot.py program.

The program is designed to catch IOError to mean "GoPiGo3 robot not detected or DistanceSensor not installed", but the easygopigo3.EasyGoPiGo3().init_distance_sensor() eats the distance sensor missing exception, returns "None" for the distance sensor instantiation, and continues on as planned for the normal flow.

This later causes the object_avoidance_robot.py program to error stating "NoneType has no read_mm() attribute".

object_avoidance_robot.py needs to include something like:

...
    gopigo3 = EasyGoPiGo3()
    distance_sensor = gopigo3.init_distance_sensor()
    if (distance_sensor == None):              # <--- NEW ERROR CASE
        print("I2C connected Time Of Flight Distance Sensor not detected")
        sys.exit(1)
jharris1993 commented 2 years ago

Perhaps a try-except(value error) block would be a better way of handling that exception case?

slowrunner commented 2 years ago

Perhaps a try-except(value error) block would be a better way of handling that exception case?

The easygopigo3.EasyGoPiGo3().init_distance_sensor() eats the exception, so there is no exception raised:

        try:
            d = easy_distance_sensor.EasyDistanceSensor(port=port, use_mutex=self.use_mutex)
        except Exception as e:
            # print(e)
            d = None

        return d

While I don't understand that choice, it is the design of the API, and it is not possible to know what effects an API change would cause.

Therefore, my recommendation is that this user of the existing API (object_avoidance_robot.py) properly protect itself.

CleoQc commented 2 years ago

The choice here is strictly educational. While it is the Python way to raise exceptions, it does send total beginners in a panic. It is also harder to code with exceptions, when you're starting. That's why gopigo3 raises an error, but easygopigo3 eats it. easygopigo3 is a wrapper intended to be friendly to beginners, kids who are just getting started.

jharris1993 commented 2 years ago

Suggestion: Maybe easygopigo can have "modes"? A "standard" (default) mode for beginners that eats scary warnings and an "advanced" mode where everything is propagated to the user?

This would accommodate both the beginning python roboticist and the more advanced classes where error reporting is important.