blob: ae1d5809afcb373d7f8a116d6ce704d1942fcc36 [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"
8#include "aos/robot_state/robot_state_generated.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"
Sabina Davisadc58542019-02-01 22:23:00 -080012#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
13#include "frc971/wpilib/ahal/DigitalInput.h"
Austin Schuh3b010bc2019-02-24 17:25:37 -080014#include "frc971/wpilib/ahal/DriverStation.h"
Sabina Davis1ffa4172019-02-01 22:38:33 -080015#include "frc971/wpilib/dma.h"
16#include "frc971/wpilib/dma_edge_counting.h"
Austin Schuh54667ac2019-02-02 16:44:49 -080017#include "frc971/wpilib/encoder_and_potentiometer.h"
Sabina Davisadc58542019-02-01 22:23:00 -080018
19using ::aos::monotonic_clock;
20namespace chrono = ::std::chrono;
21
22namespace frc971 {
23namespace wpilib {
24
25class SensorReader {
26 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -080027 SensorReader(::aos::EventLoop *event_loop);
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080028 virtual ~SensorReader() {}
Sabina Davisadc58542019-02-01 22:23:00 -080029
Austin Schuh45a549f2019-02-02 15:43:56 -080030 // Updates the fast and medium encoder filter frequencies.
31 void UpdateFastEncoderFilterHz(int hz);
32 void UpdateMediumEncoderFilterHz(int hz);
33
Sabina Davisb6317b72019-02-01 22:53:23 -080034 // Sets the left drivetrain encoder.
35 void set_drivetrain_left_encoder(::std::unique_ptr<frc::Encoder> encoder);
36
37 // Sets the right drivetrain encoder.
38 void set_drivetrain_right_encoder(::std::unique_ptr<frc::Encoder> encoder);
39
Sabina Davis6292bec2019-02-06 22:53:14 -080040 // Adds a sensor to DMA.
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080041 void AddToDMA(DMASampleHandlerInterface *handler) {
Sabina Davis6292bec2019-02-06 22:53:14 -080042 if (!dma_synchronizer_) {
43 dma_synchronizer_.reset(
44 new ::frc971::wpilib::DMASynchronizer(std::make_unique<DMA>()));
45 }
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080046 dma_synchronizer_->Add(handler);
47 }
48
Austin Schuh3b010bc2019-02-24 17:25:37 -080049 // Sets PWM trigger mode. If true, synchronize the control loops with the PWM
50 // pulses. The sensors are sampled 50 uS after the falling edge of the PWM
51 // pulse.
52 void set_pwm_trigger(bool trigger) { pwm_trigger_ = trigger; }
Sabina Davisadc58542019-02-01 22:23:00 -080053
Sabina Davisa42cc352019-02-01 22:55:50 -080054 // Stops the pwm trigger on the next iteration.
55 void Quit() { run_ = false; }
56
Sabina Davis399dbd82019-02-01 23:06:08 -080057 virtual void RunIteration() = 0;
Sabina Davis6292bec2019-02-06 22:53:14 -080058 // Runs the DMA iteration after the synchronizer. This only gets run if a
59 // sensor has been added to DMA.
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080060 virtual void RunDmaIteration() {}
Sabina Davis399dbd82019-02-01 23:06:08 -080061
Sabina Davisadc58542019-02-01 22:23:00 -080062 protected:
Austin Schuh54667ac2019-02-02 16:44:49 -080063 // Copies a DMAEncoder to a IndexPosition with the correct unit and direction
64 // changes.
65 void CopyPosition(const ::frc971::wpilib::DMAEncoder &encoder,
Alex Perrycb7da4b2019-08-28 19:35:56 -070066 ::frc971::IndexPositionT *position,
Austin Schuh54667ac2019-02-02 16:44:49 -080067 double encoder_counts_per_revolution, double encoder_ratio,
68 bool reverse) {
69 const double multiplier = reverse ? -1.0 : 1.0;
70 position->encoder =
71 multiplier * encoder_translate(encoder.polled_encoder_value(),
72 encoder_counts_per_revolution,
73 encoder_ratio);
74 position->latched_encoder =
75 multiplier * encoder_translate(encoder.last_encoder_value(),
76 encoder_counts_per_revolution,
77 encoder_ratio);
78 position->index_pulses = encoder.index_posedge_count();
79 }
80
81 // Copies a AbsoluteEncoderAndPotentiometer to a PotAndAbsolutePosition with
82 // the correct unit and direction changes.
83 void CopyPosition(
84 const ::frc971::wpilib::AbsoluteEncoderAndPotentiometer &encoder,
Alex Perrycb7da4b2019-08-28 19:35:56 -070085 ::frc971::PotAndAbsolutePositionT *position,
Austin Schuh54667ac2019-02-02 16:44:49 -080086 double encoder_counts_per_revolution, double encoder_ratio,
87 ::std::function<double(double)> potentiometer_translate, bool reverse,
88 double pot_offset) {
89 const double multiplier = reverse ? -1.0 : 1.0;
90 position->pot = multiplier * potentiometer_translate(
91 encoder.ReadPotentiometerVoltage()) +
92 pot_offset;
93 position->encoder =
94 multiplier * encoder_translate(encoder.ReadRelativeEncoder(),
95 encoder_counts_per_revolution,
96 encoder_ratio);
97
98 position->absolute_encoder =
99 (reverse ? (1.0 - encoder.ReadAbsoluteEncoder())
100 : encoder.ReadAbsoluteEncoder()) *
101 encoder_ratio * (2.0 * M_PI);
102 }
103
104 // Copies a DMAEdgeCounter to a HallEffectAndPosition with the correct unit
105 // and direction changes.
106 void CopyPosition(const ::frc971::wpilib::DMAEdgeCounter &counter,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700107 ::frc971::HallEffectAndPositionT *position,
Austin Schuh54667ac2019-02-02 16:44:49 -0800108 double encoder_counts_per_revolution, double encoder_ratio,
109 bool reverse) {
110 const double multiplier = reverse ? -1.0 : 1.0;
111 position->encoder =
112 multiplier * encoder_translate(counter.polled_encoder(),
113 encoder_counts_per_revolution,
114 encoder_ratio);
115 position->current = !counter.polled_value();
116 position->posedge_count = counter.negative_count();
117 position->negedge_count = counter.positive_count();
118 position->posedge_value =
119 multiplier * encoder_translate(counter.last_negative_encoder_value(),
120 encoder_counts_per_revolution,
121 encoder_ratio);
122 position->negedge_value =
123 multiplier * encoder_translate(counter.last_positive_encoder_value(),
124 encoder_counts_per_revolution,
125 encoder_ratio);
126 }
127
Sabina Davis8d8ac0a2019-02-06 23:51:07 -0800128 // Copies a Absolute Encoder with the correct unit
129 // and direction changes.
130 void CopyPosition(
131 const ::frc971::wpilib::AbsoluteEncoder &encoder,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700132 ::frc971::AbsolutePositionT *position,
Sabina Davis8d8ac0a2019-02-06 23:51:07 -0800133 double encoder_counts_per_revolution, double encoder_ratio,
134 bool reverse) {
135 const double multiplier = reverse ? -1.0 : 1.0;
136 position->encoder =
137 multiplier * encoder_translate(encoder.ReadRelativeEncoder(),
138 encoder_counts_per_revolution,
139 encoder_ratio);
140
141 position->absolute_encoder =
142 (reverse ? (1.0 - encoder.ReadAbsoluteEncoder())
143 : encoder.ReadAbsoluteEncoder()) *
144 encoder_ratio * (2.0 * M_PI);
145 }
146
Sabina Davis2243cab2019-02-05 21:45:08 -0800147 void CopyPosition(
148 const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700149 ::frc971::PotAndIndexPositionT *position,
Sabina Davis2243cab2019-02-05 21:45:08 -0800150 ::std::function<double(int32_t)> encoder_translate,
151 ::std::function<double(double)> potentiometer_translate, bool reverse,
152 double pot_offset) {
153 const double multiplier = reverse ? -1.0 : 1.0;
154 position->encoder =
155 multiplier * encoder_translate(encoder.polled_encoder_value());
156 position->pot = multiplier * potentiometer_translate(
157 encoder.polled_potentiometer_voltage()) +
158 pot_offset;
159 position->latched_encoder =
160 multiplier * encoder_translate(encoder.last_encoder_value());
161 position->latched_pot =
162 multiplier *
163 potentiometer_translate(encoder.last_potentiometer_voltage()) +
164 pot_offset;
165 position->index_pulses = encoder.index_posedge_count();
166 }
167
Austin Schuh54667ac2019-02-02 16:44:49 -0800168 double encoder_translate(int32_t value, double counts_per_revolution,
169 double ratio) {
170 return static_cast<double>(value) / counts_per_revolution * ratio *
171 (2.0 * M_PI);
172 }
173
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800174 ::aos::EventLoop *event_loop_;
175 ::aos::Sender<::aos::RobotState> robot_state_sender_;
176
Austin Schuh45a549f2019-02-02 15:43:56 -0800177 frc::DigitalGlitchFilter fast_encoder_filter_, medium_encoder_filter_;
178
179 ::std::unique_ptr<frc::Encoder> drivetrain_left_encoder_,
180 drivetrain_right_encoder_;
181
182 private:
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800183 // Gets called right before the DMA synchronizer is up and running.
184 virtual void Start() {}
185
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700186 // Sets up everything during startup.
187 void DoStart();
188
189 // Runs a single iteration.
190 void Loop(int iterations);
191
Austin Schuh3b010bc2019-02-24 17:25:37 -0800192 // Returns the monotonic time of the start of the first PWM cycle.
193 // Returns min_time if no start time could be calculated.
194 monotonic_clock::time_point GetPWMStartTime();
Sabina Davisadc58542019-02-01 22:23:00 -0800195
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700196 bool pwm_trigger_ = false;
Sabina Davisadc58542019-02-01 22:23:00 -0800197
Sabina Davis1ffa4172019-02-01 22:38:33 -0800198 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
199
Sabina Davisadc58542019-02-01 22:23:00 -0800200 ::std::atomic<bool> run_{true};
Austin Schuh3b010bc2019-02-24 17:25:37 -0800201 ::frc::DriverStation *ds_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700202
203 const int32_t my_pid_;
204
205 // Pointer to the phased loop handler used to modify the wakeup.
206 ::aos::PhasedLoopHandler *phased_loop_handler_;
207
208 // Last time we got called.
209 ::aos::monotonic_clock::time_point last_monotonic_now_ =
210 ::aos::monotonic_clock::min_time;
211 // The current period.
212 chrono::microseconds period_ = chrono::microseconds(5000);
Sabina Davisadc58542019-02-01 22:23:00 -0800213};
214
215} // namespace wpilib
216} // namespace frc971
217
218#endif // FRC971_WPILIB_SENSOR_READER_H_