blob: 76c12137da42f39b91d72933404113d3b0a085f4 [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2016-2017. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#pragma once
9
10#include <functional>
11#include <memory>
12
13#include "ErrorBase.h"
14#include "cscore.h"
15#include "vision/VisionPipeline.h"
16
17namespace frc {
18
19/**
20 * Non-template base class for VisionRunner.
21 */
22class VisionRunnerBase : public ErrorBase {
23 public:
24 explicit VisionRunnerBase(cs::VideoSource videoSource);
25 ~VisionRunnerBase() override;
26
27 VisionRunnerBase(const VisionRunnerBase&) = delete;
28 VisionRunnerBase& operator=(const VisionRunnerBase&) = delete;
29
30 void RunOnce();
31
32 void RunForever();
33
34 protected:
35 virtual void DoProcess(cv::Mat& image) = 0;
36
37 private:
38 std::unique_ptr<cv::Mat> m_image;
39 cs::CvSink m_cvSink;
40};
41
42/**
43 * A vision runner is a convenient wrapper object to make it easy to run vision
44 * pipelines from robot code. The easiest way to use this is to run it in a
45 * std::thread and use the listener to take snapshots of the pipeline's outputs.
46 *
47 * @see VisionPipeline
48 */
49template <typename T>
50class VisionRunner : public VisionRunnerBase {
51 public:
52 VisionRunner(cs::VideoSource videoSource, T* pipeline,
53 std::function<void(T&)> listener);
54 virtual ~VisionRunner() = default;
55
56 protected:
57 void DoProcess(cv::Mat& image) override;
58
59 private:
60 T* m_pipeline;
61 std::function<void(T&)> m_listener;
62};
63} // namespace frc
64
65#include "VisionRunner.inc"