WPILibC++ 2024.3.2
VisionRunner.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <atomic>
8#include <functional>
9#include <memory>
10
11#include "cscore.h"
12#include "cscore_cv.h"
14
15namespace frc {
16
17/**
18 * Non-template base class for VisionRunner.
19 */
21 public:
22 /**
23 * Creates a new vision runner. It will take images from the {@code
24 * videoSource}, and call the virtual DoProcess() method.
25 *
26 * @param videoSource the video source to use to supply images for the
27 * pipeline
28 */
29 explicit VisionRunnerBase(cs::VideoSource videoSource);
30
32
35
36 /**
37 * Runs the pipeline one time, giving it the next image from the video source
38 * specified in the constructor. This will block until the source either has
39 * an image or throws an error. If the source successfully supplied a frame,
40 * the pipeline's image input will be set, the pipeline will run, and the
41 * listener specified in the constructor will be called to notify it that the
42 * pipeline ran. This must be run in a dedicated thread, and cannot be used in
43 * the main robot thread because it will freeze the robot program.
44 *
45 * <p>This method is exposed to allow teams to add additional functionality or
46 * have their own ways to run the pipeline. Most teams, however, should just
47 * use RunForever() in its own thread using a std::thread.</p>
48 */
49 void RunOnce();
50
51 /**
52 * A convenience method that calls runOnce() in an infinite loop. This must be
53 * run in a dedicated thread, and cannot be used in the main robot thread
54 * because it will freeze the robot program.
55 *
56 * <strong>Do not call this method directly from the main thread.</strong>
57 */
58 void RunForever();
59
60 /**
61 * Stop a RunForever() loop.
62 */
63 void Stop();
64
65 protected:
66 virtual void DoProcess(cv::Mat& image) = 0;
67
68 private:
69 std::unique_ptr<cv::Mat> m_image;
70 cs::CvSink m_cvSink;
71 std::atomic_bool m_enabled;
72};
73
74/**
75 * A vision runner is a convenient wrapper object to make it easy to run vision
76 * pipelines from robot code. The easiest way to use this is to run it in a
77 * std::thread and use the listener to take snapshots of the pipeline's outputs.
78 *
79 * @see VisionPipeline
80 */
81template <typename T>
83 public:
84 VisionRunner(cs::VideoSource videoSource, T* pipeline,
85 std::function<void(T&)> listener);
86 virtual ~VisionRunner() = default;
87
88 protected:
89 void DoProcess(cv::Mat& image) override;
90
91 private:
92 T* m_pipeline;
93 std::function<void(T&)> m_listener;
94};
95} // namespace frc
96
97#include "VisionRunner.inc"
A sink for user code to accept video frames as OpenCV images.
Definition: cscore_cv.h:119
A source for video that provides a sequence of frames.
Definition: cscore_oo.h:208
Non-template base class for VisionRunner.
Definition: VisionRunner.h:20
void RunOnce()
Runs the pipeline one time, giving it the next image from the video source specified in the construct...
virtual void DoProcess(cv::Mat &image)=0
VisionRunnerBase & operator=(const VisionRunnerBase &)=delete
void Stop()
Stop a RunForever() loop.
VisionRunnerBase(const VisionRunnerBase &)=delete
void RunForever()
A convenience method that calls runOnce() in an infinite loop.
VisionRunnerBase(cs::VideoSource videoSource)
Creates a new vision runner.
A vision runner is a convenient wrapper object to make it easy to run vision pipelines from robot cod...
Definition: VisionRunner.h:82
VisionRunner(cs::VideoSource videoSource, T *pipeline, std::function< void(T &)> listener)
Creates a new vision runner.
Definition: VisionRunner.inc:24
void DoProcess(cv::Mat &image) override
Definition: VisionRunner.inc:31
virtual ~VisionRunner()=default
Definition: AprilTagPoseEstimator.h:15