Skip to content

Commit

Permalink
redo stream index handling, issue still not fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
S1ink committed Jul 1, 2022
1 parent a76fe1a commit 3cbb25b
Show file tree
Hide file tree
Showing 20 changed files with 241 additions and 181 deletions.
4 changes: 2 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{
"label": "RELEASE",
"type": "shell",
"command": "make mode=release",
"command": "make -C lib-vs OPT=release",
"problemMatcher": [],
"group": {
"kind": "build",
Expand All @@ -16,7 +16,7 @@
{
"label": "DEBUG",
"type": "shell",
"command": "make mode=debug",
"command": "make -C lib-vs OPT=debug",
"problemMatcher": [],
"group": {
"kind": "build",
Expand Down
24 changes: 12 additions & 12 deletions lib-vs/obj/tfmodel.cpp.pic.d
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ obj/tfmodel.cpp.pic.o: src\core/tfmodel.cpp src\core/tfmodel.h \
include/ntcore_cpp.h include/networktables/NetworkTableValue.h \
include/networktables/NetworkTableEntry.inc \
include/networktables/TableEntryListener.h \
include/networktables/TableListener.h \
include/networktables/TableListener.h additions/cpp-tools/src/types.h \
src\core/visioncamera.h include/networktables/NetworkTableInstance.h \
include/networktables/NetworkTableInstance.inc \
include/cameraserver/CameraServer.h include/wpi/deprecated.h \
include/cscore.h include/cscore_c.h include/cscore_cpp.h \
include/cscore_oo.h include/cscore_oo.inc include/cscore_cv.h \
include/cameraserver/CameraServer.inc additions/cpp-tools/src/types.h \
src\core/visioncamera.h include/networktables/NetworkTableInstance.h \
include/networktables/NetworkTableInstance.inc include/wpi/json.h \
include/cameraserver/CameraServer.inc include/wpi/json.h \
src\core/visionserver2.inc src\core/tfmodel.inc

src\core/tfmodel.h:
Expand Down Expand Up @@ -482,6 +482,14 @@ include/networktables/TableEntryListener.h:

include/networktables/TableListener.h:

additions/cpp-tools/src/types.h:

src\core/visioncamera.h:

include/networktables/NetworkTableInstance.h:

include/networktables/NetworkTableInstance.inc:

include/cameraserver/CameraServer.h:

include/wpi/deprecated.h:
Expand All @@ -500,14 +508,6 @@ include/cscore_cv.h:

include/cameraserver/CameraServer.inc:

additions/cpp-tools/src/types.h:

src\core/visioncamera.h:

include/networktables/NetworkTableInstance.h:

include/networktables/NetworkTableInstance.inc:

include/wpi/json.h:

src\core/visionserver2.inc:
Expand Down
Binary file modified lib-vs/obj/vision.cpp.pic.o
Binary file not shown.
Binary file modified lib-vs/obj/visioncamera.cpp.pic.o
Binary file not shown.
24 changes: 12 additions & 12 deletions lib-vs/obj/visionserver2.cpp.pic.d
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ obj/visionserver2.cpp.pic.o: src\core/visionserver2.cpp \
include/ntcore_cpp.h include/networktables/NetworkTableValue.h \
include/networktables/NetworkTableEntry.inc \
include/networktables/TableEntryListener.h \
include/networktables/TableListener.h \
include/networktables/TableListener.h additions/cpp-tools/src/types.h \
src\core/visioncamera.h include/networktables/NetworkTableInstance.h \
include/networktables/NetworkTableInstance.inc \
include/cameraserver/CameraServer.h include/wpi/deprecated.h \
include/cscore.h include/cscore_c.h include/cscore_cpp.h \
include/cscore_oo.h include/cscore_oo.inc include/cscore_cv.h \
include/cameraserver/CameraServer.inc additions/cpp-tools/src/types.h \
src\core/visioncamera.h include/networktables/NetworkTableInstance.h \
include/networktables/NetworkTableInstance.inc include/wpi/json.h \
include/cameraserver/CameraServer.inc include/wpi/json.h \
src\core/visionserver2.inc

src\core/visionserver2.h:
Expand Down Expand Up @@ -382,6 +382,14 @@ include/networktables/TableEntryListener.h:

include/networktables/TableListener.h:

additions/cpp-tools/src/types.h:

src\core/visioncamera.h:

include/networktables/NetworkTableInstance.h:

include/networktables/NetworkTableInstance.inc:

include/cameraserver/CameraServer.h:

include/wpi/deprecated.h:
Expand All @@ -400,14 +408,6 @@ include/cscore_cv.h:

include/cameraserver/CameraServer.inc:

additions/cpp-tools/src/types.h:

src\core/visioncamera.h:

include/networktables/NetworkTableInstance.h:

include/networktables/NetworkTableInstance.inc:

include/wpi/json.h:

src\core/visionserver2.inc:
Binary file modified lib-vs/obj/visionserver2.cpp.pic.o
Binary file not shown.
8 changes: 4 additions & 4 deletions lib-vs/out/include/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ namespace vs2 {
*/
class Target {
public:
inline static const std::shared_ptr<nt::NetworkTable>
target_table{VisionServer::base_table->GetSubTable("Targets")};
// inline static const std::shared_ptr<nt::NetworkTable>
// target_table{VisionServer::getTable()->GetSubTable("Targets")};

Target() = delete;
inline Target(const std::string& n) : name(n), table(target_table->GetSubTable(this->name)) {}
inline Target(std::string&& n) : name(std::move(n)), table(target_table->GetSubTable(this->name)) {}
inline Target(const std::string& n) : name(n), table(VisionServer::targetsTable()->GetSubTable(this->name)) {}
inline Target(std::string&& n) : name(std::move(n)), table(VisionServer::targetsTable()->GetSubTable(this->name)) {}
Target(const Target&) = default;
Target(Target&&) = default;

Expand Down
14 changes: 2 additions & 12 deletions lib-vs/out/include/visioncamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,7 @@ class VisionCamera final: public cs::VideoCamera {
* Get the internal CvSink for aquiring frames from the camera
* @return a const reference to the internal sink
*/
inline const cs::CvSink& getSink() const { return this->source; }
/**
* Creates a cs::CvSource with correct options as to be used for the camera
* @return A cs::CvSource object that could be used to stream frames from the camera. This object is not connected to the source feed of the camera in anyway unless passed frames are passed manually
*/
cs::CvSource generateServer() const;
inline const cs::CvSink& getSink() const { return this->raw; }

/**
* Get the width of a frame output by the camera
Expand Down Expand Up @@ -216,11 +211,6 @@ class VisionCamera final: public cs::VideoCamera {
void deleteEntries();

protected:
/**
* Creates a cs::CvSink for the camera and updates stream configuration if it has any
* @return A cs::CvSink object connected to the current camera
*/
cs::CvSink getVideo() const;
/**
* Publish an adjustable networktable entry for the camera's brightness
*/
Expand All @@ -236,7 +226,7 @@ class VisionCamera final: public cs::VideoCamera {

private:
wpi::json config, calibration;
cs::CvSink source;
cs::CvSink raw;
cv::Mat_<float> camera_matrix{default_matrix}, distortion{default_distort};
cs::VideoMode properties;

Expand Down
44 changes: 31 additions & 13 deletions lib-vs/out/include/visionserver2.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <opencv2/opencv.hpp>

#include <networktables/NetworkTable.h>
#include <cameraserver/CameraServer.h>

#include "cpp-tools/src/types.h"
#include "visioncamera.h"
Expand All @@ -21,8 +20,8 @@ namespace vs2 {
class VisionServer final {
struct OutputStream;
public:
inline static const std::shared_ptr<nt::NetworkTable>
base_table{nt::NetworkTableInstance::GetDefault().GetTable("Vision Server")};
// inline static const std::shared_ptr<nt::NetworkTable>
// base_table{nt::NetworkTableInstance::GetDefault().GetTable("Vision Server")};

inline static void Init() {
getInstance();
Expand All @@ -31,13 +30,17 @@ class VisionServer final {
static VisionServer instance;
return instance;
}
inline static std::shared_ptr<nt::NetworkTable> getTable() { return inst().base; }
inline static std::shared_ptr<nt::NetworkTable> pipesTable() { return inst().pipes; }
inline static std::shared_ptr<nt::NetworkTable> streamsTable() { return inst().outputs; }
inline static std::shared_ptr<nt::NetworkTable> targetsTable() { return inst().targets; }


class BasePipe : public cs::CvSource {
friend class VisionServer;
public:
inline static const std::shared_ptr<nt::NetworkTable>
pipe_table{VisionServer::base_table->GetSubTable("Pipelines")};
// inline static const std::shared_ptr<nt::NetworkTable>
// pipe_table{VisionServer::base_table->GetSubTable("Pipelines")};

inline const std::string& getName() const { return this->name; }
inline const std::shared_ptr<nt::NetworkTable> getTable() const { return this->table; }
Expand All @@ -47,13 +50,13 @@ class VisionServer final {
protected:
inline BasePipe(const char* name) : /* start enable/disable callback for more efficent threading */
CvSource(name, cs::VideoMode()), name(name),
input(this->name), table(pipe_table->GetSubTable(this->name)) {}
input(this->name), table(pipesTable()->GetSubTable(this->name)) {}
inline BasePipe(const std::string& name) :
CvSource(name, cs::VideoMode()), name(name),
input(this->name), table(pipe_table->GetSubTable(this->name)) {}
input(this->name), table(pipesTable()->GetSubTable(this->name)) {}
inline BasePipe(std::string&& name) :
CvSource(name, cs::VideoMode()), name(name),
input(this->name), table(pipe_table->GetSubTable(this->name)) {}
input(this->name), table(pipesTable()->GetSubTable(this->name)) {}
BasePipe() = delete;


Expand Down Expand Up @@ -100,10 +103,12 @@ class VisionServer final {
static void addStream();
static void addStream(std::string_view);
static void addStream(std::string_view, int port);
static void addStream(const cs::MjpegServer&);
static void addStream(cs::MjpegServer&&);
static void addStreams(size_t = 2);
static void addStreams(std::initializer_list<std::string_view>);
static void addStreams(std::initializer_list<std::pair<std::string_view, int> >);
static void addStreams(std::vector<cs::MjpegServer>&&);
static void addStreams(const std::vector<cs::MjpegServer>&);
static const std::vector<OutputStream>& getStreams();
static size_t numStreams();

Expand Down Expand Up @@ -141,27 +146,40 @@ class VisionServer final {
std::vector<OutputStream> streams;
std::vector<std::unique_ptr<BasePipe> > heap_allocated;

std::shared_ptr<nt::NetworkTable>
base, pipes, outputs, targets
;

std::thread head;
std::atomic<bool> is_running{false};

struct OutputStream : public cs::MjpegServer {
friend class VisionServer;
public:
inline static const std::shared_ptr<nt::NetworkTable>
streams_table = base_table->GetSubTable("Streams");
// inline static std::shared_ptr<nt::NetworkTable> streamsTable() {
// static std::shared_ptr<nt::NetworkTable> st{VisionServer::base_table->GetSubTable("Streams")};
// return st;
// }

inline OutputStream(std::string_view n) :
OutputStream(frc::CameraServer::AddServer(n)) {}
inline OutputStream(std::string_view n, int p) :
OutputStream(frc::CameraServer::AddServer(n, p)) {}
OutputStream(cs::MjpegServer&& s); // setup ntable values
inline OutputStream(const OutputStream& o) : table(o.table), local_idx(o.local_idx.load()) {}
~OutputStream();

inline void setSourceIdx(int i) { this->table->GetEntry("Source Index").SetDouble(i); }
void setSourceIdx(int i);
inline void setPipelineIdx(uint16_t i) { this->setSourceIdx(i); }
inline void setCameraIdx(uint16_t i) { this->setSourceIdx(-(int)i); }

protected:
void updateNt();

private:
// try storing MjpegServer instead of extending?
const std::shared_ptr<nt::NetworkTable> table;
NT_EntryListener listener;
std::atomic_int local_idx{0};

};

Expand Down
Binary file modified lib-vs/out/libvs3407.so
Binary file not shown.
8 changes: 4 additions & 4 deletions lib-vs/src/core/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ namespace vs2 {
*/
class Target {
public:
inline static const std::shared_ptr<nt::NetworkTable>
target_table{VisionServer::base_table->GetSubTable("Targets")};
// inline static const std::shared_ptr<nt::NetworkTable>
// target_table{VisionServer::getTable()->GetSubTable("Targets")};

Target() = delete;
inline Target(const std::string& n) : name(n), table(target_table->GetSubTable(this->name)) {}
inline Target(std::string&& n) : name(std::move(n)), table(target_table->GetSubTable(this->name)) {}
inline Target(const std::string& n) : name(n), table(VisionServer::targetsTable()->GetSubTable(this->name)) {}
inline Target(std::string&& n) : name(std::move(n)), table(VisionServer::targetsTable()->GetSubTable(this->name)) {}
Target(const Target&) = default;
Target(Target&&) = default;

Expand Down
1 change: 1 addition & 0 deletions lib-vs/src/core/vision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ bool readConfig(std::vector<VisionCamera>& cameras, const char* file) {
// }
// }

wpi::outs().flush();
return true;
}

Expand Down
36 changes: 16 additions & 20 deletions lib-vs/src/core/visioncamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,27 @@


VisionCamera::VisionCamera(CS_Source handle) :
VideoCamera(handle), source(this->getVideo()), properties(this->GetVideoMode())
{}
VideoCamera(handle), raw(this->GetName() + "_cvraw"), properties(this->GetVideoMode())
{
this->raw.SetSource(*this);
}
VisionCamera::VisionCamera(const cs::VideoSource& source, const wpi::json& config) :
VideoCamera(source.GetHandle()), config(config), source(this->getVideo()), properties(this->GetVideoMode())
VideoCamera(source.GetHandle()), config(config), raw(this->GetName() + "_cvraw"), properties(this->GetVideoMode())
{
if(this->properties) { this->properties = ::getJsonVideoMode(config); }
this->raw.SetSource(*this);
}
VisionCamera::VisionCamera(const cs::UsbCamera& source, const wpi::json& config) :
VideoCamera(source.GetHandle()), config(config), source(this->getVideo()), properties(this->GetVideoMode())
VideoCamera(source.GetHandle()), config(config), raw(this->GetName() + "_cvraw"), properties(this->GetVideoMode())
{
if(this->properties) { this->properties = ::getJsonVideoMode(config); }
this->raw.SetSource(*this);
}
VisionCamera::VisionCamera(const cs::HttpCamera& source, const wpi::json& config) :
VideoCamera(source.GetHandle()), config(config), source(this->getVideo()), properties(this->GetVideoMode())
VideoCamera(source.GetHandle()), config(config), raw(this->GetName() + "_cvraw"), properties(this->GetVideoMode())
{
if(this->properties) { this->properties = ::getJsonVideoMode(config); }
this->raw.SetSource(*this);
}
VisionCamera::VisionCamera(const wpi::json& source_config, const wpi::json& calibration) :
config(source_config), calibration(calibration)
Expand All @@ -38,7 +43,8 @@ VisionCamera::VisionCamera(const wpi::json& source_config, const wpi::json& cali
swap(*this, cam); // this should work
this->getJsonCameraMatrix(this->camera_matrix);
this->getJsonDistortionCoefs(this->distortion);
this->source = this->getVideo();
this->raw = cs::CvSink(this->GetName() + "_cvraw");
this->raw.SetSource(*this);
this->properties = this->GetVideoMode();
if(this->properties) { this->properties = ::getJsonVideoMode(config); }
}
Expand All @@ -54,7 +60,8 @@ VisionCamera::VisionCamera(const wpi::json& source_config) :
cam.SetConnectionStrategy(cs::VideoSource::kConnectionKeepOpen);
// print confirmation
swap(*this, cam); // this should work
this->source = this->getVideo();
this->raw = cs::CvSink(this->GetName() + "_cvraw");
this->raw.SetSource(*this);
this->properties = this->GetVideoMode();
if(!this->properties) { this->properties = ::getJsonVideoMode(config); }
}
Expand Down Expand Up @@ -171,32 +178,21 @@ bool VisionCamera::getJsonDistortionCoefs(cv::Mat_<float>& array) const {

uint64_t VisionCamera::getFrame(cv::Mat& o_frame, double timeout) const {
if(this->IsConnected()) {
return this->source.GrabFrame(o_frame, timeout);
return this->raw.GrabFrame(o_frame, timeout);
} else {
o_frame = cv::Mat::zeros(this->getResolution(), CV_8UC3);
return 0;
}
}
uint64_t VisionCamera::getFrameNoTmO(cv::Mat& o_frame) const {
if(this->IsConnected()) {
return this->source.GrabFrameNoTimeout(o_frame);
return this->raw.GrabFrameNoTimeout(o_frame);
} else {
o_frame = cv::Mat::zeros(this->getResolution(), CV_8UC3);
return 0;
}
}

cs::CvSink VisionCamera::getVideo() const {
cs::CvSink video = frc::CameraServer::GetVideo(*this);
if(this->config.count("stream") > 0) {
video.SetConfigJson(this->config.at("stream"));
}
return video;
}
cs::CvSource VisionCamera::generateServer() const {
return frc::CameraServer::PutVideo(("Camera stream: " + this->GetName()), this->getWidth(), this->getHeight());
}

int VisionCamera::getWidth() const {
return this->properties.width;
}
Expand Down
Loading

0 comments on commit 3cbb25b

Please sign in to comment.