Skip to content

Commit f3fbb72

Browse files
committed
multiple streams now working, update syntax
1 parent 3cbb25b commit f3fbb72

14 files changed

Lines changed: 444 additions & 363 deletions

lib-vs/obj/visioncamera.cpp.pic.o

4 Bytes
Binary file not shown.

lib-vs/obj/visionserver2.cpp.pic.d

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ obj/visionserver2.cpp.pic.o: src\core/visionserver2.cpp \
118118
include/cscore.h include/cscore_c.h include/cscore_cpp.h \
119119
include/cscore_oo.h include/cscore_oo.inc include/cscore_cv.h \
120120
include/cameraserver/CameraServer.inc include/wpi/json.h \
121-
src\core/visionserver2.inc
121+
src\core/visionserver2.inc additions/cpp-tools/src/resources.h
122122

123123
src\core/visionserver2.h:
124124

@@ -411,3 +411,5 @@ include/cameraserver/CameraServer.inc:
411411
include/wpi/json.h:
412412

413413
src\core/visionserver2.inc:
414+
415+
additions/cpp-tools/src/resources.h:

lib-vs/obj/visionserver2.cpp.pic.o

5.07 KB
Binary file not shown.

lib-vs/out/include/target.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
#include <string>
66

77
#include <opencv2/opencv.hpp>
8-
98
#include <networktables/NetworkTable.h>
109

1110
#include "cpp-tools/src/types.h"
11+
1212
#include "visionserver2.h"
1313

1414

@@ -19,12 +19,14 @@ namespace vs2 {
1919
*/
2020
class Target {
2121
public:
22-
// inline static const std::shared_ptr<nt::NetworkTable>
23-
// target_table{VisionServer::getTable()->GetSubTable("Targets")};
22+
inline static const std::shared_ptr<nt::NetworkTable>& ntable() {
23+
static std::shared_ptr<nt::NetworkTable> targets{VisionServer::ntable()->GetSubTable("Targets")};
24+
return targets;
25+
}
2426

2527
Target() = delete;
26-
inline Target(const std::string& n) : name(n), table(VisionServer::targetsTable()->GetSubTable(this->name)) {}
27-
inline Target(std::string&& n) : name(std::move(n)), table(VisionServer::targetsTable()->GetSubTable(this->name)) {}
28+
inline Target(const std::string& n) : name(n), table(Target::ntable()->GetSubTable(this->name)) {}
29+
inline Target(std::string&& n) : name(std::move(n)), table(Target::ntable()->GetSubTable(this->name)) {}
2830
Target(const Target&) = default;
2931
Target(Target&&) = default;
3032

lib-vs/out/include/visioncamera.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class VisionCamera final: public cs::VideoCamera {
200200
* Set the root networktable in which the camera's own networktable should reside
201201
* @param table The root table (the camera resides in ~table~/Cameras)
202202
*/
203-
void setNetworkBase(std::shared_ptr<nt::NetworkTable> table);
203+
void setNetworkBase(const std::shared_ptr<nt::NetworkTable>& table);
204204
/**
205205
* Publish networktables-adjustable settings for exposure, brightness, and whitebalance under the camera's networktable
206206
*/

lib-vs/out/include/visionserver2.h

Lines changed: 53 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
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 {
2019
class VisionServer final {
2120
struct OutputStream;
2221
public:
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

lib-vs/out/include/visionserver2.inc

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <sstream>
55

66

7-
//using namespace vs2;
87
namespace vs2 {
98

109
inline const std::vector<VisionCamera>& VisionServer::getCameras() { return inst().cameras; }
@@ -16,37 +15,46 @@ inline size_t VisionServer::numStreams() { return inst().streams.size(); }
1615
inline bool VisionServer::isRunning() { return inst().is_running; }
1716

1817
template<class pipeline>
19-
void VisionServer::addPipeline() {
18+
bool VisionServer::addPipeline() {
2019
static_assert(std::is_base_of<VisionServer::BasePipe, pipeline>::value, "Template argument (pipeline) must inherit from BasePipe");
2120
static_assert(std::is_default_constructible<pipeline>::value, "Template arguement (pipeline) must be default constructible");
22-
if(!inst().is_running) {
23-
inst().heap_allocated.emplace_back(std::make_unique<pipeline>());
24-
inst().pipelines.push_back(inst().heap_allocated.back().get());
21+
if(!VisionServer::isRunning()) {
22+
VisionServer& _inst = getInstance();
23+
_inst.heap_allocated.emplace_back(std::make_unique<pipeline>());
24+
_inst.pipelines.push_back(_inst.heap_allocated.back().get());
25+
return true;
2526
}
27+
return false;
2628
}
2729
template<class... pipelines_t>
28-
void VisionServer::addPipelines() {
29-
if(!inst().is_running) {
30-
inst().heap_allocated.reserve(inst().heap_allocated.size() + sizeof...(pipelines_t));
31-
inst().pipelines.reserve(inst().pipelines.size() + sizeof...(pipelines_t));
30+
bool VisionServer::addPipelines() {
31+
if(!VisionServer::isRunning()) {
32+
VisionServer& _inst = getInstance();
33+
_inst.heap_allocated.reserve(_inst.heap_allocated.size() + sizeof...(pipelines_t));
34+
_inst.pipelines.reserve(_inst.pipelines.size() + sizeof...(pipelines_t));
3235
size_t alloc = VisionServer::pipeExpander<pipelines_t...>(inst().heap_allocated);
3336
for(size_t i = alloc; i >= 0; --i) {
34-
inst().pipelines.push_back(inst().heap_allocated.at(inst().heap_allocated.size() - i - 1).get());
37+
_inst.pipelines.push_back(_inst.heap_allocated.at(_inst.heap_allocated.size() - i - 1).get());
3538
}
39+
return true;
3640
}
41+
return false;
3742
}
3843
template<class... pipelines_t>
39-
void VisionServer::setPipelines() {
40-
if(!inst().is_running) {
41-
inst().heap_allocated.clear();
42-
inst().heap_allocated.reserve(sizeof...(pipelines_t));
43-
inst().pipelines.clear();
44-
inst().pipelines.reserve(sizeof...(pipelines_t));
45-
VisionServer::pipeExpander<pipelines_t...>(inst().heap_allocated);
46-
for(size_t i = 0; i < inst().heap_allocated.size(); i++) {
47-
inst().pipelines.push_back(inst().heap_allocated.at(i).get());
44+
bool VisionServer::setPipelines() {
45+
if(!VisionServer::isRunning()) {
46+
VisionServer& _inst = getInstance();
47+
_inst.heap_allocated.clear();
48+
_inst.heap_allocated.reserve(sizeof...(pipelines_t));
49+
_inst.pipelines.clear();
50+
_inst.pipelines.reserve(sizeof...(pipelines_t));
51+
VisionServer::pipeExpander<pipelines_t...>(_inst.heap_allocated);
52+
for(size_t i = 0; i < _inst.heap_allocated.size(); i++) {
53+
_inst.pipelines.push_back(_inst.heap_allocated.at(i).get());
4854
}
55+
return true;
4956
}
57+
return false;
5058
}
5159

5260
template<class pipeline, class... pipelines>
@@ -96,4 +104,4 @@ void SequentialPipeline<pipelines_t...>::process(cv::Mat& io_frame) {
96104
}
97105
}
98106

99-
}
107+
} // namespace vs2;

lib-vs/out/libvs3407.so

1.96 KB
Binary file not shown.

lib-vs/src/core/target.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
#include <string>
66

77
#include <opencv2/opencv.hpp>
8-
98
#include <networktables/NetworkTable.h>
109

1110
#include "cpp-tools/src/types.h"
11+
1212
#include "visionserver2.h"
1313

1414

@@ -19,12 +19,14 @@ namespace vs2 {
1919
*/
2020
class Target {
2121
public:
22-
// inline static const std::shared_ptr<nt::NetworkTable>
23-
// target_table{VisionServer::getTable()->GetSubTable("Targets")};
22+
inline static const std::shared_ptr<nt::NetworkTable>& ntable() {
23+
static std::shared_ptr<nt::NetworkTable> targets{VisionServer::ntable()->GetSubTable("Targets")};
24+
return targets;
25+
}
2426

2527
Target() = delete;
26-
inline Target(const std::string& n) : name(n), table(VisionServer::targetsTable()->GetSubTable(this->name)) {}
27-
inline Target(std::string&& n) : name(std::move(n)), table(VisionServer::targetsTable()->GetSubTable(this->name)) {}
28+
inline Target(const std::string& n) : name(n), table(Target::ntable()->GetSubTable(this->name)) {}
29+
inline Target(std::string&& n) : name(std::move(n)), table(Target::ntable()->GetSubTable(this->name)) {}
2830
Target(const Target&) = default;
2931
Target(Target&&) = default;
3032

lib-vs/src/core/visioncamera.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ void VisionCamera::setExposure(int8_t val) {
233233
this->exposure = (val > 100 ? 100 : val);
234234
}
235235

236-
void VisionCamera::setNetworkBase(std::shared_ptr<nt::NetworkTable> table) {
236+
void VisionCamera::setNetworkBase(const std::shared_ptr<nt::NetworkTable>& table) {
237237
this->camera = table->GetSubTable("Cameras")->GetSubTable(this->GetName());
238238
}
239239
void VisionCamera::setNetworkAdjustable() {

0 commit comments

Comments
 (0)