Cameras
Description
Cameras are a great way to get information during a match, whether it’s though relaying the image to the driver station or using vision processing to track targets on the field.
There are a number of different kinds of cameras that can be used on the robot, including USB cameras plugged into the roboRIO, and cameras on the network that are accessed through a MJPG stream.
USB Cameras
USB Cameras get plugged into the roboRIO, and can be accessed through the cs::UsbCamera class.
Initialization
Initializing a cs::UsbCamera
object requires a name, and the device id of the camera.
Hosting a MJPG Stream
In order to transmit the camera’s image to the driver station, it must be hosted on a MJPG stream. This can be done by using the frc::CameraServer::PutVideo method. This will host the a MJPG stream on port 1181. Subsequent calls to PutVideo
will increment the port number up to 1190.
Submitting Images to the MJPG Stream
Using the cs::CvSource
object returned from the call to PutVideo()
, it is possible to submit images to the MJPG stream. This is usually done after vision processing has been done on the image to visualize the results. This only needs to be done if the camera image is being modified in any way; otherwise, the image will be automatically submitted to the stream. Using an OpenCV cv::Mat
image matrix representing the frame (Getting camera frames section), the image can be submitted to the stream using the PutFrame method.
MJPG Streams
MJPG Camera Streams hosted by devices other than the roboRIO (ex. Limelight, Raspberry Pi) can be accessed through the cs::HttpCamera class.
Initialization
Initializing a cs::HttpCamera
object requires a name, and the URL of the MJPG stream.
Getting Camera Images
To do vision processing with the camera, you need to use the OpenCV vision library. Images in OpenCV are stored using the cv::Mat class. To get the camera’s image, you need to create an instance of the cs::CvSink
class using the camera object (cs::UsbCamera
, cs::HttpCamera
, etc.).