Valkyrie 2026
Loading...
Searching...
No Matches
BaseSensor.h
1#pragma once
2
3#include <functional>
4#include <string>
5
6#include <frc/TimedRobot.h>
7#include <frc/Timer.h>
8#include <valkyrie/Loggable.h>
9#include <valkyrie/Robot.h>
10
11namespace valor {
12namespace sensors {
13
20class BaseSensorCore {
21 public:
22 static constexpr units::millisecond_t LOOP_PERIOD = frc::TimedRobot::kDefaultPeriod / 2;
23
24 BaseSensorCore() {
25 valor::Robot::GetRobot().AddPeriodic(
26 [this] {
27 Refresh();
28 Calculate();
29 },
30 LOOP_PERIOD, 5_ms);
31 }
32
33 virtual void Refresh() = 0;
34
35 virtual void Calculate() {}
36};
37
49template <class T>
50class BaseSensor : public virtual BaseSensorCore, public valor::Loggable {
51 public:
59 virtual void Reset() { prevState = currState = T{}; }
60
69 void SetGetter(std::function<T()> _lambda) { sensorLambda = _lambda; }
70
79 void ApplyPostProcessing(std::function<T(T)> func) { postProcessor = func; }
80
89 inline T Get() const { return currState; }
90
97 void Refresh() override {
98 prevState = currState;
99 if (sensorLambda) {
100 currState = sensorLambda();
101 if (postProcessor)
102 currState = postProcessor(currState);
103 }
104 }
105
106 protected:
112 std::function<T()> sensorLambda;
113
119 std::function<T(T)> postProcessor;
120
127 T prevState, currState;
128};
129
130} // namespace sensors
131} // namespace valor
Base helper for publishing and subscribing values to NetworkTables.
Definition Loggable.h:53
static Robot & GetRobot()
Abstract class that all Valor sensors should implement.
Definition BaseSensor.h:50
void SetGetter(std::function< T()> _lambda)
Set the lambda function to fetch sensor data.
Definition BaseSensor.h:69
std::optional< PoseEstimate > prevState
Definition BaseSensor.h:127
std::function< std::optional< PoseEstimate >()> sensorLambda
Definition BaseSensor.h:112
std::function< std::optional< PoseEstimate >(std::optional< PoseEstimate >)> postProcessor
Definition BaseSensor.h:119
void Refresh() override
Refresh the sensor state.
Definition BaseSensor.h:97
void ApplyPostProcessing(std::function< T(T)> func)
Set a post-processing function for sensor data.
Definition BaseSensor.h:79
virtual void Reset()
Reset the sensor state.
Definition BaseSensor.h:59
T Get() const
Get the current sensor state.
Definition BaseSensor.h:89