VisionServer  v2.1.1-1-g21dc5465
FRC vision library
target.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <array>
5#include <string>
6
7#include <opencv2/opencv.hpp>
8#include <networktables/NetworkTable.h>
9
10#include "cpp-tools/src/types.h"
11
12#include "visionserver2.h"
13
14
15namespace vs2 {
16
20class Target {
21 template<typename T> friend class UniqueTarget;
22public:
23 inline static const std::shared_ptr<nt::NetworkTable>& ntable() {
24 static std::shared_ptr<nt::NetworkTable> targets{VisionServer::ntable()->GetSubTable("Targets")};
25 return targets;
26 }
27
28 enum Status {
29 INVALID = -1,
31 VALID = 1
32 };
33
34 Target() = delete;
35 inline Target(const std::string& n) : name(n), table(Target::ntable()->GetSubTable(this->name))
36 { this->table->PutNumber("Status", EXPIRED); }
37 inline Target(std::string&& n) : name(std::move(n)), table(Target::ntable()->GetSubTable(this->name))
38 { this->table->PutNumber("Status", EXPIRED); }
39 Target(const Target&) = default;
40 Target(Target&&) = default;
41 inline virtual ~Target()
42 { this->table->PutNumber("Status", INVALID); }
43
44 inline const std::string& getName() const { return this->name; }
45
46protected:
47 inline void setPos(double x, double y, double z) {
48 this->table->PutNumber("x", x);
49 this->table->PutNumber("y", y);
50 this->table->PutNumber("z", z);
51 }
52 inline void setAngle(double ud, double lr) {
53 this->table->PutNumber("up-down", ud);
54 this->table->PutNumber("left-right", lr);
55 }
56 inline void setDist(double d) {
57 this->table->PutNumber("distance", d);
58 }
59
60 inline void setExpired() {
61 this->table->PutNumber("Status", EXPIRED);
62 }
63 inline void setValid() {
64 this->table->PutNumber("Status", VALID);
65 }
66
67 const std::string name;
68 const std::shared_ptr<nt::NetworkTable> table;
69
70
71};
75template<class derived_t = void>
76class UniqueTarget : public Instanced<UniqueTarget<derived_t> >, public Target {
77 typedef struct UniqueTarget<derived_t> This_t;
78 inline Target* super() { return static_cast<Target*>(this); }
79public:
80 UniqueTarget() = delete;
81 inline UniqueTarget(const std::string& n) : Instanced<This_t>(), Target(n + "/Detection " + std::to_string(this->instance)) {}
82 inline UniqueTarget(std::string&& n) : Instanced<This_t>(), Target(n + "/Detection " + std::to_string(this->instance)) {}
83 UniqueTarget(const UniqueTarget&) = default;
85
86protected:
87 inline void setPos(double x, double y, double z) { super()->setPos(x, y, z); }
88 inline void setAngle(double ud, double lr) { super()->setAngle(ud, lr); }
89 inline void setDist(double d) { super()->setDist(d); }
90 inline void setExpired() { super()->setExpired(); }
91 inline void setValid() { super()->setValid(); }
92
93};
94
95// /**
96// * A target that is generated from OpenCV pipelines (and solved using solvePnP)
97// */
98// template<size_t points>
99// class CvTarget : public Target {
100// public:
101// const std::array<cv::Point3f, points> world;
102// static inline const size_t size{points};
103
104// CvTarget() = delete;
105// inline CvTarget(std::array<cv::Point3f, points>&& world, const std::string& name) :
106// Target(name), world(world) {}
107// inline CvTarget(std::array<cv::Point3f, points>&& world, std::string&& name) :
108// Target(std::move(name)), world(world) {}
109// CvTarget(const CvTarget&) = delete;
110// CvTarget(CvTarget&&) = delete;
111
112// protected:
113// std::array<cv::Point2f, points> point_buff;
114
115
116// };
117// /**
118// * A target that is generated from TensorFlow pipelines
119// */
120// class TfTarget : public Target {
121
122// };
123
124} // namespace vs2
void setExpired()
Definition: target.h:60
const std::string name
Definition: target.h:67
void setPos(double x, double y, double z)
Definition: target.h:47
static const std::shared_ptr< nt::NetworkTable > & ntable()
Definition: target.h:23
void setAngle(double ud, double lr)
Definition: target.h:52
void setValid()
Definition: target.h:63
virtual ~Target()
Definition: target.h:41
Target()=delete
Target(const std::string &n)
Definition: target.h:35
Target(Target &&)=default
Target(const Target &)=default
void setDist(double d)
Definition: target.h:56
Target(std::string &&n)
Definition: target.h:37
@ EXPIRED
Definition: target.h:30
@ INVALID
Definition: target.h:29
const std::string & getName() const
Definition: target.h:44
const std::shared_ptr< nt::NetworkTable > table
Definition: target.h:68
void setPos(double x, double y, double z)
Definition: target.h:87
UniqueTarget(const UniqueTarget &)=default
void setExpired()
Definition: target.h:90
UniqueTarget(const std::string &n)
Definition: target.h:81
UniqueTarget()=delete
void setDist(double d)
Definition: target.h:89
Target * super()
Definition: target.h:78
UniqueTarget(UniqueTarget &&)=default
void setValid()
Definition: target.h:91
UniqueTarget(std::string &&n)
Definition: target.h:82
void setAngle(double ud, double lr)
Definition: target.h:88
static const std::shared_ptr< nt::NetworkTable > & ntable()
Definition: visionserver2.h:22
Definition: extensions.h:9