Robot Vision with OpenCV not working

Hello all,
We are having some issues with trying to implement computer vision into the robot. Currently, when running the robot simulation in WPILIB VS Code, the window is fine and shows a video stream that is able to detect the location and distance of a target object.
The code builds and deploys without any errors.
However, when trying to upload the program to the actual robot, there was initially this error in Driver Station:
No X11 display variable was set java.awt.graphicsenvironment.checkheadless(unknown source)
and the Robot code light was red.
After a bit of tinkering around(no serious changes), there was simply no error message or
anything in the Driver Station - it was totally blank. However, the Robot Code light was still red.
What could be causing this behavior?
Thanks in advance.

For some reason it seems that your code is trying to use Java AWT? There is no video display on the robot so AWT isn’t supported. Are you using OpenCV methods that open a graphics window? Posting a link to your code would be helpful.

Yes, currently the OpenCV is opening up a window that shows the distance.
The code is at https://github.com/FRCTeam7230/2022/blob/master/Timed-Updated/src/main/java/frc/robot/ThresholdInRange.java
Is there a way to access that distance value without needing to open a separate window?
Thanks for your help!

You’re going to need to convert that code to do the following:

  • Change from SwingWorker to java.lang.Thread
  • Remove all AWT references
  • Use a mutex or atomic-protected variable (e.g. AtomicDouble) where you store the distance result so your main robot code can access it

I would also recommend:

  • Publishing your calculated information to NetworkTables using e.g. SmartDashboard.putNumber(). This will let you show that info on a dashboard such as Shuffleboard or Glass.
  • Using CameraServer classes instead of raw OpenCV functions to get access to the camera. This will also provide access to the camera video stream so you can view it on the dashboard (you can also use this to stream the processed images).