Me and my team all understand multithreading, but we’re all confused on how you would attempt multithreading with this control system. We wish to put the camera on a separate thread so it doesn’t slow down the rest of the code. We have the camera code set up as well but since it has to wait for the camera to update its picture, it has a huge amount of input lag from the controller. Any help would be appreciated.
Make a new type – say ImageAnalyzer – that is a Runnable. This type will do the image analysis.
Create another new type ImageAnalyzerResult that has properties:
estimatedToteDistance
estimatedToteAngle
This type is used to return the results of the analysis to your main robot logic.
The ImageAnalyzer.run() method would have a loop that would repeatedly run your image analysis, likely with some delay between runs to not completely eat a CPU.
Your image analysis code would look like this:
a. Acquire an image from the camera
b. Perform some analysis of the image to determine the robot’s angle relative to the vision target on a tote.
c. Create a new ImageAnalyzerResult, and set the two properties to the calculated values from your image analysis. You want to make a new one of these each time you do your image analysis, so that you don’t have synchronization issues with the analyzer and the code that uses the result.
Add a method to ImageAnalyzer named something like getResult() that returns the current ImageAnalyzerResult if you’re seeing a tote, and null if not.
How to share information between thread and your main program flow
The next step is to take the results of the camera logic and save them to some member variables in the class type, so that the “main” thread in Robot can safely poll them out using sychronized accessors. http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html