blob: 455ed0ebc3c397eb4a222e1639ab883c7658453d [file] [log] [blame]
Sabina Davisadc58542019-02-01 22:23:00 -08001#ifndef FRC971_WPILIB_SENSOR_READER_H_
2#define FRC971_WPILIB_SENSOR_READER_H_
3
4#include <atomic>
5#include <chrono>
6
Alex Perrycb7da4b2019-08-28 19:35:56 -07007#include "aos/events/event_loop.h"
Austin Schuh217a9782019-12-21 23:02:50 -08008#include "aos/events/shm_event_loop.h"
Sabina Davisadc58542019-02-01 22:23:00 -08009#include "aos/stl_mutex/stl_mutex.h"
10#include "aos/time/time.h"
Maxwell Hendersoncb78f352024-01-15 00:27:16 -080011#include "frc971/control_loops/control_loops_static.h"
Maxwell Hendersonf382b4b2024-01-14 12:31:41 -080012#include "frc971/control_loops/drivetrain/drivetrain_position_static.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070013#include "frc971/input/robot_state_generated.h"
Sabina Davisadc58542019-02-01 22:23:00 -080014#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
15#include "frc971/wpilib/ahal/DigitalInput.h"
Austin Schuh3b010bc2019-02-24 17:25:37 -080016#include "frc971/wpilib/ahal/DriverStation.h"
Sabina Davis1ffa4172019-02-01 22:38:33 -080017#include "frc971/wpilib/dma.h"
18#include "frc971/wpilib/dma_edge_counting.h"
Austin Schuh54667ac2019-02-02 16:44:49 -080019#include "frc971/wpilib/encoder_and_potentiometer.h"
Sabina Davisadc58542019-02-01 22:23:00 -080020
21using ::aos::monotonic_clock;
22namespace chrono = ::std::chrono;
23
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080024namespace frc971::wpilib {
Sabina Davisadc58542019-02-01 22:23:00 -080025
26class SensorReader {
27 public:
Austin Schuh217a9782019-12-21 23:02:50 -080028 SensorReader(::aos::ShmEventLoop *event_loop);
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080029 virtual ~SensorReader() {}
Sabina Davisadc58542019-02-01 22:23:00 -080030
Austin Schuh45a549f2019-02-02 15:43:56 -080031 // Updates the fast and medium encoder filter frequencies.
32 void UpdateFastEncoderFilterHz(int hz);
33 void UpdateMediumEncoderFilterHz(int hz);
34
Sabina Davisb6317b72019-02-01 22:53:23 -080035 // Sets the left drivetrain encoder.
36 void set_drivetrain_left_encoder(::std::unique_ptr<frc::Encoder> encoder);
37
38 // Sets the right drivetrain encoder.
39 void set_drivetrain_right_encoder(::std::unique_ptr<frc::Encoder> encoder);
40
Sabina Davis6292bec2019-02-06 22:53:14 -080041 // Adds a sensor to DMA.
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080042 void AddToDMA(DMASampleHandlerInterface *handler) {
Sabina Davis6292bec2019-02-06 22:53:14 -080043 if (!dma_synchronizer_) {
44 dma_synchronizer_.reset(
45 new ::frc971::wpilib::DMASynchronizer(std::make_unique<DMA>()));
46 }
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080047 dma_synchronizer_->Add(handler);
48 }
49
Austin Schuh3b010bc2019-02-24 17:25:37 -080050 // Sets PWM trigger mode. If true, synchronize the control loops with the PWM
51 // pulses. The sensors are sampled 50 uS after the falling edge of the PWM
52 // pulse.
53 void set_pwm_trigger(bool trigger) { pwm_trigger_ = trigger; }
Sabina Davisadc58542019-02-01 22:23:00 -080054
Sabina Davisa42cc352019-02-01 22:55:50 -080055 // Stops the pwm trigger on the next iteration.
56 void Quit() { run_ = false; }
57
Sabina Davis399dbd82019-02-01 23:06:08 -080058 virtual void RunIteration() = 0;
Sabina Davis6292bec2019-02-06 22:53:14 -080059 // Runs the DMA iteration after the synchronizer. This only gets run if a
60 // sensor has been added to DMA.
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080061 virtual void RunDmaIteration() {}
Sabina Davis399dbd82019-02-01 23:06:08 -080062
Sabina Davisadc58542019-02-01 22:23:00 -080063 protected:
Austin Schuh54667ac2019-02-02 16:44:49 -080064 // Copies a DMAEncoder to a IndexPosition with the correct unit and direction
65 // changes.
66 void CopyPosition(const ::frc971::wpilib::DMAEncoder &encoder,
Maxwell Hendersoncb78f352024-01-15 00:27:16 -080067 ::frc971::IndexPositionStatic *position,
Austin Schuh54667ac2019-02-02 16:44:49 -080068 double encoder_counts_per_revolution, double encoder_ratio,
69 bool reverse) {
70 const double multiplier = reverse ? -1.0 : 1.0;
Maxwell Hendersoncb78f352024-01-15 00:27:16 -080071 position->set_encoder(multiplier *
72 encoder_translate(encoder.polled_encoder_value(),
73 encoder_counts_per_revolution,
74 encoder_ratio));
75 position->set_latched_encoder(
Austin Schuh54667ac2019-02-02 16:44:49 -080076 multiplier * encoder_translate(encoder.last_encoder_value(),
77 encoder_counts_per_revolution,
Maxwell Hendersoncb78f352024-01-15 00:27:16 -080078 encoder_ratio));
79 position->set_index_pulses(encoder.index_posedge_count());
Austin Schuh54667ac2019-02-02 16:44:49 -080080 }
81
82 // Copies a AbsoluteEncoderAndPotentiometer to a PotAndAbsolutePosition with
83 // the correct unit and direction changes.
84 void CopyPosition(
85 const ::frc971::wpilib::AbsoluteEncoderAndPotentiometer &encoder,
Maxwell Hendersoncb78f352024-01-15 00:27:16 -080086 ::frc971::PotAndAbsolutePositionStatic *position,
Austin Schuh54667ac2019-02-02 16:44:49 -080087 double encoder_counts_per_revolution, double encoder_ratio,
88 ::std::function<double(double)> potentiometer_translate, bool reverse,
89 double pot_offset) {
90 const double multiplier = reverse ? -1.0 : 1.0;
Maxwell Hendersoncb78f352024-01-15 00:27:16 -080091 position->set_pot(multiplier * potentiometer_translate(
92 encoder.ReadPotentiometerVoltage()) +
93 pot_offset);
94 position->set_encoder(multiplier *
95 encoder_translate(encoder.ReadRelativeEncoder(),
96 encoder_counts_per_revolution,
97 encoder_ratio));
Austin Schuh54667ac2019-02-02 16:44:49 -080098
Maxwell Hendersoncb78f352024-01-15 00:27:16 -080099 position->set_absolute_encoder((reverse
100 ? (1.0 - encoder.ReadAbsoluteEncoder())
101 : encoder.ReadAbsoluteEncoder()) *
102 encoder_ratio * (2.0 * M_PI));
Austin Schuh54667ac2019-02-02 16:44:49 -0800103 }
104
Ravago Jones937587c2020-12-26 17:21:09 -0800105 // Copies an AbsoluteEncoderAndPotentiometer to an AbsoluteAndAbsolutePosition
106 // with the correct unit and direction changes.
107 void CopyPosition(const ::frc971::wpilib::AbsoluteAndAbsoluteEncoder &encoder,
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800108 ::frc971::AbsoluteAndAbsolutePositionStatic *position,
Ravago Jones937587c2020-12-26 17:21:09 -0800109 double encoder_counts_per_revolution, double encoder_ratio,
110 double single_turn_encoder_ratio, bool reverse) {
111 const double multiplier = reverse ? -1.0 : 1.0;
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800112 position->set_encoder(multiplier *
113 encoder_translate(encoder.ReadRelativeEncoder(),
114 encoder_counts_per_revolution,
115 encoder_ratio));
Ravago Jones937587c2020-12-26 17:21:09 -0800116
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800117 position->set_absolute_encoder((reverse
118 ? (1.0 - encoder.ReadAbsoluteEncoder())
119 : encoder.ReadAbsoluteEncoder()) *
120 encoder_ratio * (2.0 * M_PI));
Ravago Jones937587c2020-12-26 17:21:09 -0800121
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800122 position->set_single_turn_absolute_encoder(
Ravago Jones937587c2020-12-26 17:21:09 -0800123 (reverse ? (1.0 - encoder.ReadSingleTurnAbsoluteEncoder())
124 : encoder.ReadSingleTurnAbsoluteEncoder()) *
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800125 single_turn_encoder_ratio * (2.0 * M_PI));
Ravago Jones937587c2020-12-26 17:21:09 -0800126 }
127
Austin Schuh54667ac2019-02-02 16:44:49 -0800128 // Copies a DMAEdgeCounter to a HallEffectAndPosition with the correct unit
129 // and direction changes.
130 void CopyPosition(const ::frc971::wpilib::DMAEdgeCounter &counter,
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800131 ::frc971::HallEffectAndPositionStatic *position,
Austin Schuh54667ac2019-02-02 16:44:49 -0800132 double encoder_counts_per_revolution, double encoder_ratio,
133 bool reverse) {
134 const double multiplier = reverse ? -1.0 : 1.0;
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800135 position->set_encoder(multiplier *
136 encoder_translate(counter.polled_encoder(),
137 encoder_counts_per_revolution,
138 encoder_ratio));
139 position->set_current(!counter.polled_value());
140 position->set_posedge_count(counter.negative_count());
141 position->set_negedge_count(counter.positive_count());
142 position->set_posedge_value(
Austin Schuh54667ac2019-02-02 16:44:49 -0800143 multiplier * encoder_translate(counter.last_negative_encoder_value(),
144 encoder_counts_per_revolution,
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800145 encoder_ratio));
146 position->set_negedge_value(
Austin Schuh54667ac2019-02-02 16:44:49 -0800147 multiplier * encoder_translate(counter.last_positive_encoder_value(),
148 encoder_counts_per_revolution,
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800149 encoder_ratio));
Austin Schuh54667ac2019-02-02 16:44:49 -0800150 }
151
Sabina Davis8d8ac0a2019-02-06 23:51:07 -0800152 // Copies a Absolute Encoder with the correct unit
153 // and direction changes.
Ravago Jones937587c2020-12-26 17:21:09 -0800154 void CopyPosition(const ::frc971::wpilib::AbsoluteEncoder &encoder,
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800155 ::frc971::AbsolutePositionStatic *position,
Ravago Jones937587c2020-12-26 17:21:09 -0800156 double encoder_counts_per_revolution, double encoder_ratio,
157 bool reverse) {
Sabina Davis8d8ac0a2019-02-06 23:51:07 -0800158 const double multiplier = reverse ? -1.0 : 1.0;
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800159 position->set_encoder(multiplier *
160 encoder_translate(encoder.ReadRelativeEncoder(),
161 encoder_counts_per_revolution,
162 encoder_ratio));
Sabina Davis8d8ac0a2019-02-06 23:51:07 -0800163
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800164 position->set_absolute_encoder((reverse
165 ? (1.0 - encoder.ReadAbsoluteEncoder())
166 : encoder.ReadAbsoluteEncoder()) *
167 encoder_ratio * (2.0 * M_PI));
Sabina Davis8d8ac0a2019-02-06 23:51:07 -0800168 }
169
Ravago Jones937587c2020-12-26 17:21:09 -0800170 void CopyPosition(const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder,
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800171 ::frc971::PotAndIndexPositionStatic *position,
Ravago Jones937587c2020-12-26 17:21:09 -0800172 ::std::function<double(int32_t)> encoder_translate,
173 ::std::function<double(double)> potentiometer_translate,
174 bool reverse, double pot_offset) {
Sabina Davis2243cab2019-02-05 21:45:08 -0800175 const double multiplier = reverse ? -1.0 : 1.0;
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800176 position->set_encoder(multiplier *
177 encoder_translate(encoder.polled_encoder_value()));
178 position->set_pot(multiplier * potentiometer_translate(
179 encoder.polled_potentiometer_voltage()) +
180 pot_offset);
181 position->set_latched_encoder(
182 multiplier * encoder_translate(encoder.last_encoder_value()));
183 position->set_latched_pot(
Sabina Davis2243cab2019-02-05 21:45:08 -0800184 multiplier *
185 potentiometer_translate(encoder.last_potentiometer_voltage()) +
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800186 pot_offset);
187 position->set_index_pulses(encoder.index_posedge_count());
Sabina Davis2243cab2019-02-05 21:45:08 -0800188 }
189
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800190 // Copies a relative digital encoder.
Alex Perry2712c102020-02-17 19:17:11 -0800191 void CopyPosition(const ::frc::Encoder &encoder,
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800192 ::frc971::RelativePositionStatic *position,
Alex Perry2712c102020-02-17 19:17:11 -0800193 double encoder_counts_per_revolution, double encoder_ratio,
194 bool reverse) {
195 const double multiplier = reverse ? -1.0 : 1.0;
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800196 position->set_encoder(multiplier *
197 encoder_translate(encoder.GetRaw(),
198 encoder_counts_per_revolution,
199 encoder_ratio));
Alex Perry2712c102020-02-17 19:17:11 -0800200 }
201
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800202 // Copies a potentiometer
203 void CopyPosition(const ::frc::AnalogInput &input,
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800204 ::frc971::RelativePositionStatic *position,
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800205 ::std::function<double(double)> potentiometer_translate,
206 bool reverse, double pot_offset) {
207 const double multiplier = reverse ? -1.0 : 1.0;
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800208 position->set_encoder(
209 multiplier * potentiometer_translate(input.GetVoltage()) + pot_offset);
Siddhant Kanwar0e37f592022-02-21 19:26:50 -0800210 }
211
Austin Schuh54667ac2019-02-02 16:44:49 -0800212 double encoder_translate(int32_t value, double counts_per_revolution,
213 double ratio) {
214 return static_cast<double>(value) / counts_per_revolution * ratio *
215 (2.0 * M_PI);
216 }
217
Maxwell Hendersond5ae85b2024-01-08 16:14:28 -0800218 void SendDrivetrainPosition(
Maxwell Hendersonf382b4b2024-01-14 12:31:41 -0800219 aos::Sender<control_loops::drivetrain::PositionStatic>::StaticBuilder
220 builder,
Maxwell Hendersond5ae85b2024-01-08 16:14:28 -0800221 std::function<double(double input)> velocity_translate,
222 std::function<double(double input)> encoder_to_meters, bool left_inverted,
223 bool right_inverted);
224
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800225 ::aos::EventLoop *event_loop_;
226 ::aos::Sender<::aos::RobotState> robot_state_sender_;
227
Austin Schuh45a549f2019-02-02 15:43:56 -0800228 frc::DigitalGlitchFilter fast_encoder_filter_, medium_encoder_filter_;
229
230 ::std::unique_ptr<frc::Encoder> drivetrain_left_encoder_,
231 drivetrain_right_encoder_;
232
233 private:
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800234 // Gets called right before the DMA synchronizer is up and running.
235 virtual void Start() {}
236
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700237 // Sets up everything during startup.
238 void DoStart();
239
240 // Runs a single iteration.
milind-u61227f22021-08-29 15:58:33 -0700241 void Loop();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700242
Austin Schuh3b010bc2019-02-24 17:25:37 -0800243 // Returns the monotonic time of the start of the first PWM cycle.
244 // Returns min_time if no start time could be calculated.
245 monotonic_clock::time_point GetPWMStartTime();
Sabina Davisadc58542019-02-01 22:23:00 -0800246
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700247 bool pwm_trigger_ = false;
Sabina Davisadc58542019-02-01 22:23:00 -0800248
Sabina Davis1ffa4172019-02-01 22:38:33 -0800249 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
250
Sabina Davisadc58542019-02-01 22:23:00 -0800251 ::std::atomic<bool> run_{true};
Austin Schuh3b010bc2019-02-24 17:25:37 -0800252 ::frc::DriverStation *ds_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700253
254 const int32_t my_pid_;
255
milind-u61227f22021-08-29 15:58:33 -0700256 // Pointer to the timer handler used to modify the wakeup.
257 ::aos::TimerHandler *timer_handler_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700258
259 // Last time we got called.
260 ::aos::monotonic_clock::time_point last_monotonic_now_ =
261 ::aos::monotonic_clock::min_time;
262 // The current period.
263 chrono::microseconds period_ = chrono::microseconds(5000);
Sabina Davisadc58542019-02-01 22:23:00 -0800264};
265
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800266} // namespace frc971::wpilib
Sabina Davisadc58542019-02-01 22:23:00 -0800267
268#endif // FRC971_WPILIB_SENSOR_READER_H_