blob: 8efdf75808a49c974d80929f956f33a3809b847b [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"
Alex Perrycb7da4b2019-08-28 19:35:56 -070011#include "frc971/control_loops/control_loops_generated.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070012#include "frc971/input/robot_state_generated.h"
Sabina Davisadc58542019-02-01 22:23:00 -080013#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
14#include "frc971/wpilib/ahal/DigitalInput.h"
Austin Schuh3b010bc2019-02-24 17:25:37 -080015#include "frc971/wpilib/ahal/DriverStation.h"
Sabina Davis1ffa4172019-02-01 22:38:33 -080016#include "frc971/wpilib/dma.h"
17#include "frc971/wpilib/dma_edge_counting.h"
Austin Schuh54667ac2019-02-02 16:44:49 -080018#include "frc971/wpilib/encoder_and_potentiometer.h"
Sabina Davisadc58542019-02-01 22:23:00 -080019
20using ::aos::monotonic_clock;
21namespace chrono = ::std::chrono;
22
23namespace frc971 {
24namespace wpilib {
25
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,
Alex Perrycb7da4b2019-08-28 19:35:56 -070067 ::frc971::IndexPositionT *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;
71 position->encoder =
72 multiplier * encoder_translate(encoder.polled_encoder_value(),
73 encoder_counts_per_revolution,
74 encoder_ratio);
75 position->latched_encoder =
76 multiplier * encoder_translate(encoder.last_encoder_value(),
77 encoder_counts_per_revolution,
78 encoder_ratio);
79 position->index_pulses = encoder.index_posedge_count();
80 }
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,
Alex Perrycb7da4b2019-08-28 19:35:56 -070086 ::frc971::PotAndAbsolutePositionT *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;
91 position->pot = multiplier * potentiometer_translate(
92 encoder.ReadPotentiometerVoltage()) +
93 pot_offset;
94 position->encoder =
95 multiplier * encoder_translate(encoder.ReadRelativeEncoder(),
96 encoder_counts_per_revolution,
97 encoder_ratio);
98
99 position->absolute_encoder =
100 (reverse ? (1.0 - encoder.ReadAbsoluteEncoder())
101 : encoder.ReadAbsoluteEncoder()) *
102 encoder_ratio * (2.0 * M_PI);
103 }
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,
108 ::frc971::AbsoluteAndAbsolutePositionT *position,
109 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;
112 position->encoder =
113 multiplier * encoder_translate(encoder.ReadRelativeEncoder(),
114 encoder_counts_per_revolution,
115 encoder_ratio);
116
117 position->absolute_encoder =
118 (reverse ? (1.0 - encoder.ReadAbsoluteEncoder())
119 : encoder.ReadAbsoluteEncoder()) *
120 encoder_ratio * (2.0 * M_PI);
121
122 position->single_turn_absolute_encoder =
123 (reverse ? (1.0 - encoder.ReadSingleTurnAbsoluteEncoder())
124 : encoder.ReadSingleTurnAbsoluteEncoder()) *
125 single_turn_encoder_ratio * (2.0 * M_PI);
126 }
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,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700131 ::frc971::HallEffectAndPositionT *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;
135 position->encoder =
136 multiplier * encoder_translate(counter.polled_encoder(),
137 encoder_counts_per_revolution,
138 encoder_ratio);
139 position->current = !counter.polled_value();
140 position->posedge_count = counter.negative_count();
141 position->negedge_count = counter.positive_count();
142 position->posedge_value =
143 multiplier * encoder_translate(counter.last_negative_encoder_value(),
144 encoder_counts_per_revolution,
145 encoder_ratio);
146 position->negedge_value =
147 multiplier * encoder_translate(counter.last_positive_encoder_value(),
148 encoder_counts_per_revolution,
149 encoder_ratio);
150 }
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,
155 ::frc971::AbsolutePositionT *position,
156 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;
159 position->encoder =
160 multiplier * encoder_translate(encoder.ReadRelativeEncoder(),
161 encoder_counts_per_revolution,
162 encoder_ratio);
163
164 position->absolute_encoder =
165 (reverse ? (1.0 - encoder.ReadAbsoluteEncoder())
166 : encoder.ReadAbsoluteEncoder()) *
167 encoder_ratio * (2.0 * M_PI);
168 }
169
Ravago Jones937587c2020-12-26 17:21:09 -0800170 void CopyPosition(const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder,
171 ::frc971::PotAndIndexPositionT *position,
172 ::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;
176 position->encoder =
177 multiplier * encoder_translate(encoder.polled_encoder_value());
178 position->pot = multiplier * potentiometer_translate(
179 encoder.polled_potentiometer_voltage()) +
180 pot_offset;
181 position->latched_encoder =
182 multiplier * encoder_translate(encoder.last_encoder_value());
183 position->latched_pot =
184 multiplier *
185 potentiometer_translate(encoder.last_potentiometer_voltage()) +
186 pot_offset;
187 position->index_pulses = encoder.index_posedge_count();
188 }
189
Alex Perry2712c102020-02-17 19:17:11 -0800190 // Copies a relative encoder.
191 void CopyPosition(const ::frc::Encoder &encoder,
192 ::frc971::RelativePositionT *position,
193 double encoder_counts_per_revolution, double encoder_ratio,
194 bool reverse) {
195 const double multiplier = reverse ? -1.0 : 1.0;
196 position->encoder =
197 multiplier * encoder_translate(encoder.GetRaw(),
198 encoder_counts_per_revolution,
199 encoder_ratio);
200 }
201
Austin Schuh54667ac2019-02-02 16:44:49 -0800202 double encoder_translate(int32_t value, double counts_per_revolution,
203 double ratio) {
204 return static_cast<double>(value) / counts_per_revolution * ratio *
205 (2.0 * M_PI);
206 }
207
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800208 ::aos::EventLoop *event_loop_;
209 ::aos::Sender<::aos::RobotState> robot_state_sender_;
210
Austin Schuh45a549f2019-02-02 15:43:56 -0800211 frc::DigitalGlitchFilter fast_encoder_filter_, medium_encoder_filter_;
212
213 ::std::unique_ptr<frc::Encoder> drivetrain_left_encoder_,
214 drivetrain_right_encoder_;
215
216 private:
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800217 // Gets called right before the DMA synchronizer is up and running.
218 virtual void Start() {}
219
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700220 // Sets up everything during startup.
221 void DoStart();
222
223 // Runs a single iteration.
224 void Loop(int iterations);
225
Austin Schuh3b010bc2019-02-24 17:25:37 -0800226 // Returns the monotonic time of the start of the first PWM cycle.
227 // Returns min_time if no start time could be calculated.
228 monotonic_clock::time_point GetPWMStartTime();
Sabina Davisadc58542019-02-01 22:23:00 -0800229
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700230 bool pwm_trigger_ = false;
Sabina Davisadc58542019-02-01 22:23:00 -0800231
Sabina Davis1ffa4172019-02-01 22:38:33 -0800232 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
233
Sabina Davisadc58542019-02-01 22:23:00 -0800234 ::std::atomic<bool> run_{true};
Austin Schuh3b010bc2019-02-24 17:25:37 -0800235 ::frc::DriverStation *ds_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700236
237 const int32_t my_pid_;
238
239 // Pointer to the phased loop handler used to modify the wakeup.
240 ::aos::PhasedLoopHandler *phased_loop_handler_;
241
242 // Last time we got called.
243 ::aos::monotonic_clock::time_point last_monotonic_now_ =
244 ::aos::monotonic_clock::min_time;
245 // The current period.
246 chrono::microseconds period_ = chrono::microseconds(5000);
Sabina Davisadc58542019-02-01 22:23:00 -0800247};
248
249} // namespace wpilib
250} // namespace frc971
251
252#endif // FRC971_WPILIB_SENSOR_READER_H_