Go to Post Teams that TRY to win and TRY to do their best end up having students that learn more, learn the value of hard work, and are inspired by the process. - Chris Hibner [more]
Home
Go Back   Chief Delphi > Technical > Programming > Python
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 11-02-2015, 17:49
razar51 razar51 is offline
Registered User
AKA: John Faulkner
FRC #1288 (Raven Robotics)
Team Role: Mentor
 
Join Date: Jan 2015
Rookie Year: 2007
Location: St. Charles, MO
Posts: 10
razar51 is an unknown quantity at this point
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?
Reply With Quote
  #2   Spotlight this post!  
Unread 11-02-2015, 19:52
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,032
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
Thumbs up Re: Prevent code crash when Camera unplugged.

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

Code:
try:
   ... camera initialization code goes here
except:
    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:

Code:
try:
    ... 
except:
    if not self.isFmsAttached():
        raise
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 https://github.com/robotpy/robotpy-wpilib/issues/96 for my thoughts.
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 21:36.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi