Prevent code crash when Camera unplugged.

I am currently using the automatic capture function to display a USB camera feed on the driver station, and I ran into a potential problem.

Since the roborio assigns a name to the USB camera, and the code must call out that name in order to define the correct camera object, when the camera is unplugged the code will crash on Init. This means that if someone accidentally unplugs the camera before a match the robot code will crash and the robot will do nothing for the whole match.

The same thing happens if the camera is replaced by a different camera (since the roborio assigns a different name).

Is there a way to wrap some protective code around the camera initialization so that it will not cause the code to crash in the absence of a camera? Or is there a way to query the names of the devices currently plugged into the roborio?

Yup. Python allows you to catch exceptions using exception handlers.

   ... camera initialization code goes here
    self.logger.error("No camera detected")

Or if you want it to crash when you’re not at a match – but not during a match, you can do this instead:

    if not self.isFmsAttached():

We should probably add a note about this to the documentation, or make sure it doesn’t crash.

Similar issues exist if you don’t test your code well enough, and it crashes somewhere – you’re done for even on simple syntax errors. For that reason, I’ve thought about creating a version of iterative robot that would only crash if the robot wasn’t connected to the FMS. See for my thoughts.