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
9#include "valkyrie/Loggable.h"
10#include "valkyrie/Robot.h"
11#include "valkyrie/util/Profiler.h"
12
13namespace valor {
14namespace sensors {
15
22class BaseSensorCore {
23 public:
24 static constexpr units::millisecond_t LOOP_PERIOD = frc::TimedRobot::kDefaultPeriod / 2;
25
26 BaseSensorCore() {
27 valor::Robot::GetRobot().AddPeriodic(
28 [this] {
29 Refresh();
30 Calculate();
31 },
32 LOOP_PERIOD, 5_ms);
33 }
34
35 virtual void Refresh() = 0;
36
37 virtual void Calculate() {}
38};
39
51template <class T>
52class BaseSensor : public virtual BaseSensorCore, public valor::Loggable {
53 public:
61 virtual void Reset() { prevState = currState = T{}; }
62
71 void SetGetter(std::function<T()> _lambda) { sensorLambda = _lambda; }
72
81 void ApplyPostProcessing(std::function<T(T)> func) { postProcessor = func; }
82
91 inline T Get() const { return currState; }
92
99 void Refresh() override {
100 prevState = currState;
101 if (sensorLambda) {
102 currState = sensorLambda();
103 if (postProcessor)
104 currState = postProcessor(currState);
105 }
106 }
107
108 protected:
114 std::function<T()> sensorLambda;
115
121 std::function<T(T)> postProcessor;
122
129 T prevState, currState;
130};
131
132} // namespace sensors
133} // namespace valor
Base helper for publishing and subscribing values to NetworkTables.
Definition Loggable.h:218
static Robot & GetRobot()
Abstract class that all Valor sensors should implement.
Definition BaseSensor.h:52
void SetGetter(std::function< T()> _lambda)
Set the lambda function to fetch sensor data.
Definition BaseSensor.h:71
std::optional< PoseEstimate > prevState
Definition BaseSensor.h:129
std::function< std::optional< PoseEstimate >()> sensorLambda
Definition BaseSensor.h:114
std::function< std::optional< PoseEstimate >(std::optional< PoseEstimate >)> postProcessor
Definition BaseSensor.h:121
void Refresh() override
Refresh the sensor state.
Definition BaseSensor.h:99
void ApplyPostProcessing(std::function< T(T)> func)
Set a post-processing function for sensor data.
Definition BaseSensor.h:81
virtual void Reset()
Reset the sensor state.
Definition BaseSensor.h:61
T Get() const
Get the current sensor state.
Definition BaseSensor.h:91