33#include < string>
44#include < atomic>
55#include < thread>
6- #include < mutex>
76#include < chrono>
87#include < initializer_list>
98
109#include < opencv2/opencv.hpp>
11-
1210#include < networktables/NetworkTable.h>
1311
1412#include " cpp-tools/src/types.h"
13+
1514#include " visioncamera.h"
1615
1716
@@ -20,8 +19,10 @@ namespace vs2 {
2019class VisionServer final {
2120 struct OutputStream ;
2221public:
23- // inline static const std::shared_ptr<nt::NetworkTable>
24- // base_table{nt::NetworkTableInstance::GetDefault().GetTable("Vision Server")};
22+ inline static const std::shared_ptr<nt::NetworkTable>& ntable () {
23+ static std::shared_ptr<nt::NetworkTable> base{nt::NetworkTableInstance::GetDefault ().GetTable (" Vision Server" )};
24+ return base;
25+ }
2526
2627 inline static void Init () {
2728 getInstance ();
@@ -30,33 +31,31 @@ class VisionServer final {
3031 static VisionServer instance;
3132 return instance;
3233 }
33- inline static std::shared_ptr<nt::NetworkTable> getTable () { return inst ().base ; }
34- inline static std::shared_ptr<nt::NetworkTable> pipesTable () { return inst ().pipes ; }
35- inline static std::shared_ptr<nt::NetworkTable> streamsTable () { return inst ().outputs ; }
36- inline static std::shared_ptr<nt::NetworkTable> targetsTable () { return inst ().targets ; }
3734
3835
3936 class BasePipe : public cs ::CvSource {
4037 friend class VisionServer ;
4138 public:
42- // inline static const std::shared_ptr<nt::NetworkTable>
43- // pipe_table{VisionServer::base_table->GetSubTable("Pipelines")};
39+ inline static const std::shared_ptr<nt::NetworkTable>& ntable () {
40+ static std::shared_ptr<nt::NetworkTable> pipes{VisionServer::ntable ()->GetSubTable (" Pipelines" )};
41+ return pipes;
42+ }
4443
4544 inline const std::string& getName () const { return this ->name ; }
46- inline const std::shared_ptr<nt::NetworkTable> getTable () const { return this ->table ; }
45+ inline const std::shared_ptr<nt::NetworkTable>& getTable () const { return this ->table ; }
4746
4847 virtual void process (cv::Mat& io_frame) = 0;
4948
5049 protected:
5150 inline BasePipe (const char * name) : /* start enable/disable callback for more efficent threading */
5251 CvSource(name, cs::VideoMode()), name(name),
53- input(this ->name), table(pipesTable ()->GetSubTable(this ->name)) {}
52+ input(this ->name), table(BasePipe::ntable ()->GetSubTable(this ->name)) {}
5453 inline BasePipe (const std::string& name) :
5554 CvSource(name, cs::VideoMode()), name(name),
56- input(this ->name), table(pipesTable ()->GetSubTable(this ->name)) {}
55+ input(this ->name), table(BasePipe::ntable ()->GetSubTable(this ->name)) {}
5756 inline BasePipe (std::string&& name) :
5857 CvSource(name, cs::VideoMode()), name(name),
59- input(this ->name), table(pipesTable ()->GetSubTable(this ->name)) {}
58+ input(this ->name), table(BasePipe::ntable ()->GetSubTable(this ->name)) {}
6059 BasePipe () = delete ;
6160
6261
@@ -80,47 +79,49 @@ class VisionServer final {
8079 };
8180
8281
83- static void addCamera (VisionCamera&&);
84- static void addCameras (std::vector<VisionCamera>&&);
85- static void setCameras (std::vector<VisionCamera>&&);
82+ static bool addCamera (VisionCamera&&);
83+ static bool addCameras (std::vector<VisionCamera>&&);
84+ static bool setCameras (std::vector<VisionCamera>&&);
8685 static const std::vector<VisionCamera>& getCameras ();
8786 static size_t numCameras ();
8887
8988 template <class pipeline >
90- static void addPipeline ();
91- static void addPipeline (BasePipe*);
89+ static bool addPipeline ();
90+ static bool addPipeline (BasePipe*);
9291 template <class ... pipelines>
93- static void addPipelines ();
94- static void addPipelines (std::vector<BasePipe*>&&);
95- static void addPipelines (std::initializer_list<BasePipe*>);
92+ static bool addPipelines ();
93+ static bool addPipelines (std::vector<BasePipe*>&&);
94+ static bool addPipelines (std::initializer_list<BasePipe*>);
9695 template <class ... pipelines>
97- static void setPipelines ();
98- static void setPipelines (std::vector<BasePipe*>&&);
99- static void setPipelines (std::initializer_list<BasePipe*>);
96+ static bool setPipelines ();
97+ static bool setPipelines (std::vector<BasePipe*>&&);
98+ static bool setPipelines (std::initializer_list<BasePipe*>);
10099 static const std::vector<BasePipe*>& getPipelines ();
101100 static size_t numPipelines ();
102101
103- static void addStream ();
104- static void addStream (std::string_view);
105- static void addStream (std::string_view, int port);
106- static void addStream (const cs::MjpegServer&);
107- static void addStream (cs::MjpegServer&&);
108- static void addStreams (size_t = 2 );
109- static void addStreams (std::initializer_list<std::string_view>);
110- static void addStreams (std::initializer_list<std::pair<std::string_view, int > >);
111- static void addStreams (const std::vector<cs::MjpegServer>&);
102+ static bool addStream ();
103+ static bool addStream (std::string_view);
104+ static bool addStream (std::string_view, int port);
105+ static bool addStream (const cs::MjpegServer&);
106+ static bool addStream (cs::MjpegServer&&);
107+ static bool addStreams (size_t = 2 );
108+ static bool addStreams (std::initializer_list<std::string_view>);
109+ static bool addStreams (std::initializer_list<std::pair<std::string_view, int > >);
110+ static bool addStreams (const std::vector<cs::MjpegServer>&);
112111 static const std::vector<OutputStream>& getStreams ();
113112 static size_t numStreams ();
114113
115- static void compensate (); // attempts to attach all pipelines to the first available camera, and all outputs to the first available source
114+ static bool compensate (); // attempts to attach all pipelines to the first available camera, and all outputs to the first available source
115+ static bool runRaw (); // run stream index handling for raw inputs (no processing, just camera feeds) - stops when processing is started
116+ static bool runRawThread (); // run stream index handling in a separate thread - stops when processing is started
116117
117- static bool run (uint16_t rps = 50 );
118- static bool runSingle (uint16_t rps = 50 ); // runs in vs1 mode
119- static bool runThread (uint16_t rps = 50 );
120- static bool runSingleThread (uint16_t rps = 50 );
118+ static bool run (uint16_t rps = 50 ); // start multithreaded processing - blocks until stop() is called [in another thread]
119+ static bool runSingle (uint16_t rps = 50 ); // start singlethreaded processing - blocks until stop() is called [ in another thread]
120+ static bool runThread (uint16_t rps = 50 ); // start multithreaded processing in a separate thread - does not block
121+ static bool runSingleThread (uint16_t rps = 50 ); // start singlethreaded processing in a separate thread - does not block
121122 static bool isRunning ();
122- static bool stop ();
123- static inline void stopExit () { stop (); }
123+ static bool stop (); // stop processing - returns if any instances were actually stopped
124+ static inline void stopExit () { stop (); } // a void-returning wrapper for stop() to be used within 'atexit'
124125
125126
126127 template <class pipeline = void , class ... pipelines>
@@ -139,45 +140,41 @@ class VisionServer final {
139140 void operator =(const VisionServer&) = delete ;
140141 void operator =(VisionServer&&) = delete ;
141142
142- // inline static VisionServer* _inst{nullptr};
143-
144143 std::vector<VisionCamera> cameras;
145144 std::vector<BasePipe*> pipelines;
146- std::vector<OutputStream> streams;
147145 std::vector<std::unique_ptr<BasePipe> > heap_allocated;
148-
149- std::shared_ptr<nt::NetworkTable>
150- base, pipes, outputs, targets
151- ;
146+ std::vector<OutputStream> streams;
152147
153148 std::thread head;
154149 std::atomic<bool > is_running{false };
155150
156151 struct OutputStream : public cs ::MjpegServer {
157152 friend class VisionServer ;
158153 public:
159- // inline static std::shared_ptr<nt::NetworkTable> streamsTable () {
160- // static std::shared_ptr<nt::NetworkTable> st {VisionServer::base_table ->GetSubTable("Streams")};
161- // return st ;
162- // }
154+ inline static const std::shared_ptr<nt::NetworkTable>& ntable () {
155+ static std::shared_ptr<nt::NetworkTable> streams {VisionServer::ntable () ->GetSubTable (" Streams" )};
156+ return streams ;
157+ }
163158
159+ OutputStream (cs::MjpegServer&& s);
164160 inline OutputStream (std::string_view n) :
165161 OutputStream(frc::CameraServer::AddServer(n)) {}
166162 inline OutputStream (std::string_view n, int p) :
167163 OutputStream(frc::CameraServer::AddServer(n, p)) {}
168- OutputStream (cs::MjpegServer&& s); // setup ntable values
169- inline OutputStream (const OutputStream& o) : table(o.table), local_idx(o.local_idx.load()) {}
164+ inline OutputStream (const OutputStream& o) :
165+ MjpegServer(o), table(o.table), local_idx(o.local_idx.load()) {}
166+ inline OutputStream (OutputStream&& o) :
167+ MjpegServer(std::move(o)), table(std::move(o.table)), local_idx(o.local_idx.load()) {}
170168 ~OutputStream ();
171169
172170 void setSourceIdx (int i);
173171 inline void setPipelineIdx (uint16_t i) { this ->setSourceIdx (i); }
174172 inline void setCameraIdx (uint16_t i) { this ->setSourceIdx (-(int )i); }
175173
176174 protected:
177- void updateNt ();
175+ void syncIdx ();
178176
179177 private:
180- // try storing MjpegServer instead of extending?
181178 const std::shared_ptr<nt::NetworkTable> table;
182179 std::atomic_int local_idx{0 };
183180
0 commit comments