Resurgence (PY2022)
Codebase for the Husky Robotics 2021-2022 rover Resurgence
Loading...
Searching...
No Matches
data.h
1#pragma once
2
3#include "../navtypes.h"
4
5#include <bitset>
6#include <chrono>
7#include <optional>
8#include <stdexcept>
9#include <type_traits>
10#include <vector>
11
12#include <frozen/string.h>
13#include <frozen/unordered_map.h>
14#include <frozen/unordered_set.h>
15
16// forward declare cv::Mat instead of importing OpenCV
17// we do this to avoid unnecessarily including OpenCV in all build targets
18namespace cv {
19class Mat;
20}
21
26namespace robot::types {
27
29using dataclock = std::chrono::steady_clock;
32
33template <typename T>
34class DataPoint;
35
42
47
49enum class indication_t {
50 off,
51 autonomous,
52 teleop,
53 arrivedAtDest
54};
55
57enum class motorid_t {
58 frontLeftWheel,
59 frontLeftSwerve,
60 frontRightWheel,
61 frontRightSwerve,
62 rearLeftWheel,
63 rearLeftSwerve,
64 rearRightWheel,
65 rearRightSwerve,
66 armBase,
67 shoulder,
68 elbow,
69 forearm,
70 wristDiffRight,
71 wristDiffLeft,
72 hand,
73 activeSuspension,
74 drillActuator,
75 drillMotor
76};
77
80 none,
81 arm,
82 armServo,
83 scienceStation,
84 lidar
85};
86
87enum class jointid_t {
88 frontLeftSwerve,
89 frontRightSwerve,
90 rearLeftSwerve,
91 rearRightSwerve,
92 armBase,
93 shoulder,
94 elbow,
95 forearm,
96 wristPitch,
97 wristRoll,
98 hand,
99 activeSuspension,
100 ikForward,
101 ikUp
102};
103
104constexpr auto all_jointid_t = frozen::make_unordered_set<jointid_t>(
105 {jointid_t::armBase, jointid_t::shoulder, jointid_t::elbow, jointid_t::forearm,
106 jointid_t::wristRoll, jointid_t::wristPitch, jointid_t::hand, jointid_t::activeSuspension,
107 jointid_t::ikForward, jointid_t::ikUp});
108
109constexpr auto name_to_jointid = frozen::make_unordered_map<frozen::string, jointid_t>(
110 {{"frontLeftSwerve", jointid_t::frontLeftSwerve},
111 {"frontRightSwerve", jointid_t::frontRightSwerve},
112 {"rearLeftSwerve", jointid_t::rearLeftSwerve},
113 {"rearRightSwerve", jointid_t::rearRightSwerve},
114 {"armBase", jointid_t::armBase},
115 {"shoulder", jointid_t::shoulder},
116 {"elbow", jointid_t::elbow},
117 {"forearm", jointid_t::forearm},
118 {"wristPitch", jointid_t::wristPitch},
119 {"wristRoll", jointid_t::wristRoll},
120 {"hand", jointid_t::hand},
121 {"activeSuspension", jointid_t::activeSuspension},
122 {"ikForward", jointid_t::ikForward},
123 {"ikUp", jointid_t::ikUp}});
124
125class bad_datapoint_access : public std::runtime_error {
126public:
127 bad_datapoint_access() : std::runtime_error("bad_datapoint_access") {}
128};
129
135template <typename T>
137public:
141 DataPoint() : datapoint() {}
142
148 DataPoint(T data) : DataPoint(dataclock::now(), data) {}
149
156 DataPoint(datatime_t time, T data) : datapoint({time, data}) {}
157
163 operator bool() const {
164 return isValid();
165 }
166
174 bool isFresh(std::chrono::milliseconds duration) const {
175 return isValid() && dataclock::now() - duration <= getTime();
176 }
177
183 bool isValid() const {
184 return datapoint.has_value();
185 }
186
194 return datapoint.value().first;
195 }
196
203 T getData() const {
204 if (isValid()) {
205 return datapoint.value().second;
206 } else {
207 throw bad_datapoint_access();
208 }
209 }
210
218 T getDataOrElse(T defaultData) {
219 return isValid() ? getData() : defaultData;
220 }
221
231 template <typename F>
233 if (isValid()) {
235 } else {
236 return {};
237 }
238 }
239
240private:
244 std::optional<std::pair<datatime_t, T>> datapoint;
245};
246
250constexpr size_t N_LIMIT_SWITCH = 8;
251
252constexpr int LIMIT_SWITCH_LIM_MIN_IDX = 0;
253constexpr int LIMIT_SWITCH_LIM_MAX_IDX = 1;
254
259public:
268 LimitSwitchData(unsigned long long data);
269
278 bool isOpen(size_t idx);
279
288 bool isClosed(size_t idx);
289
295 bool isAnyOpen();
296
302 bool isAnyClosed();
303
314
315private:
317};
318
319} // namespace robot::types
320
321namespace util {
322std::string to_string(robot::types::jointid_t joint);
323std::string to_string(const robot::types::CameraID& id);
325} // namespace util
_GLIBCXX_END_NAMESPACE_CXX11 typedef basic_string< char > string
duration< int64_t, milli > milliseconds
DataPoint()
Construct an invalid DataPoint, holding no data.
Definition data.h:141
LimitSwitchData(unsigned long long data)
Construct a new LimitSwitchData object, from the given bits.
Definition data.cpp:5
Represents data measured using a sensor at a given time.
Definition data.h:136
bool isFresh(std::chrono::milliseconds duration) const
Check if this measurement was taken recently.
Definition data.h:174
DataPoint(T data)
Construct a new DataPoint object, measured now.
Definition data.h:148
T getData() const
Get the value of this data point.
Definition data.h:203
DataPoint(datatime_t time, T data)
Construct a new DataPoint object, measured at the given time.
Definition data.h:156
datatime_t getTime() const
Get the time at which the measurement was taken.
Definition data.h:193
bool isValid() const
Check if this measurement is valid.
Definition data.h:183
DataPoint()
Construct an invalid DataPoint, holding no data.
Definition data.h:141
DataPoint< std::invoke_result_t< F, T > > transform(const F &f)
Transforms the data in this datapoint by the given function.
Definition data.h:232
T getDataOrElse(T defaultData)
Get the value of this data point, defaulting to a given value if this data point is invalid.
Definition data.h:218
std::bitset< N_LIMIT_SWITCH > diff(const LimitSwitchData &other)
Check which indices differ between this data and other.
Definition data.cpp:23
LimitSwitchData(unsigned long long data)
Construct a new LimitSwitchData object, from the given bits.
Definition data.cpp:5
bool isAnyClosed()
Check if any index is closed.
Definition data.cpp:15
bool isAnyOpen()
Check if any index is open.
Definition data.cpp:19
bool isClosed(size_t idx)
Check if the given index is closed.
Definition data.cpp:11
bool isOpen(size_t idx)
Check if the given index is open.
Definition data.cpp:7
Types for use in the world interface.
Definition data.cpp:3
indication_t
An indication enum, used to command the LED to flash different signals.
Definition data.h:49
std::chrono::time_point< dataclock > datatime_t
A point in time as measured by dataclock.
Definition data.h:31
std::string CameraID
The type of a camera id.
Definition data.h:46
motorid_t
The motors on the robot.
Definition data.h:57
std::vector< DataPoint< navtypes::point_t > > landmarks_t
A data structure that represents when each landmark was seen.
Definition data.h:41
mountedperipheral_t
the mounted peripheral on the robot.
Definition data.h:79
std::pair< cv::Mat, uint32_t > CameraFrame
A pair of a camera frame and its corresponding frame number.
Definition data.h:44
std::chrono::steady_clock dataclock
The clock used for time measurements for data.
Definition data.h:29
constexpr size_t N_LIMIT_SWITCH
The maximum number of limit switches associated with any motor.
Definition data.h:250
A collection of utility functions and classes with common use-cases.
Definition SwerveController.cpp:145