Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 1 | #ifndef FRC971_WPILIB_SENSOR_READER_H_ |
| 2 | #define FRC971_WPILIB_SENSOR_READER_H_ |
| 3 | |
| 4 | #include <atomic> |
| 5 | #include <chrono> |
| 6 | |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 7 | #include "aos/events/event-loop.h" |
| 8 | #include "aos/robot_state/robot_state.q.h" |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 9 | #include "aos/stl_mutex/stl_mutex.h" |
| 10 | #include "aos/time/time.h" |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 11 | #include "frc971/control_loops/control_loops.q.h" |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 12 | #include "frc971/wpilib/ahal/DigitalGlitchFilter.h" |
| 13 | #include "frc971/wpilib/ahal/DigitalInput.h" |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 14 | #include "frc971/wpilib/ahal/DriverStation.h" |
Sabina Davis | 1ffa417 | 2019-02-01 22:38:33 -0800 | [diff] [blame] | 15 | #include "frc971/wpilib/dma.h" |
| 16 | #include "frc971/wpilib/dma_edge_counting.h" |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 17 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 18 | |
| 19 | using ::aos::monotonic_clock; |
| 20 | namespace chrono = ::std::chrono; |
| 21 | |
| 22 | namespace frc971 { |
| 23 | namespace wpilib { |
| 24 | |
| 25 | class SensorReader { |
| 26 | public: |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 27 | SensorReader(::aos::EventLoop *event_loop); |
Austin Schuh | 2c2cc2e | 2019-02-02 20:19:45 -0800 | [diff] [blame] | 28 | virtual ~SensorReader() {} |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 29 | |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 30 | // Updates the fast and medium encoder filter frequencies. |
| 31 | void UpdateFastEncoderFilterHz(int hz); |
| 32 | void UpdateMediumEncoderFilterHz(int hz); |
| 33 | |
Sabina Davis | b6317b7 | 2019-02-01 22:53:23 -0800 | [diff] [blame] | 34 | // 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 Davis | 6292bec | 2019-02-06 22:53:14 -0800 | [diff] [blame] | 40 | // Adds a sensor to DMA. |
Austin Schuh | 2c2cc2e | 2019-02-02 20:19:45 -0800 | [diff] [blame] | 41 | void AddToDMA(DMASampleHandlerInterface *handler) { |
Sabina Davis | 6292bec | 2019-02-06 22:53:14 -0800 | [diff] [blame] | 42 | if (!dma_synchronizer_) { |
| 43 | dma_synchronizer_.reset( |
| 44 | new ::frc971::wpilib::DMASynchronizer(std::make_unique<DMA>())); |
| 45 | } |
Austin Schuh | 2c2cc2e | 2019-02-02 20:19:45 -0800 | [diff] [blame] | 46 | dma_synchronizer_->Add(handler); |
| 47 | } |
| 48 | |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 49 | // 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 Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 53 | |
Sabina Davis | a42cc35 | 2019-02-01 22:55:50 -0800 | [diff] [blame] | 54 | // Stops the pwm trigger on the next iteration. |
| 55 | void Quit() { run_ = false; } |
| 56 | |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 57 | virtual void RunIteration() = 0; |
Sabina Davis | 6292bec | 2019-02-06 22:53:14 -0800 | [diff] [blame] | 58 | // Runs the DMA iteration after the synchronizer. This only gets run if a |
| 59 | // sensor has been added to DMA. |
Austin Schuh | 2c2cc2e | 2019-02-02 20:19:45 -0800 | [diff] [blame] | 60 | virtual void RunDmaIteration() {} |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 61 | |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 62 | protected: |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 63 | // Copies a DMAEncoder to a IndexPosition with the correct unit and direction |
| 64 | // changes. |
| 65 | void CopyPosition(const ::frc971::wpilib::DMAEncoder &encoder, |
| 66 | ::frc971::IndexPosition *position, |
| 67 | 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, |
| 85 | ::frc971::PotAndAbsolutePosition *position, |
| 86 | 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, |
| 107 | ::frc971::HallEffectAndPosition *position, |
| 108 | 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 Davis | 8d8ac0a | 2019-02-06 23:51:07 -0800 | [diff] [blame] | 128 | // Copies a Absolute Encoder with the correct unit |
| 129 | // and direction changes. |
| 130 | void CopyPosition( |
| 131 | const ::frc971::wpilib::AbsoluteEncoder &encoder, |
| 132 | ::frc971::AbsolutePosition *position, |
| 133 | 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 Davis | 2243cab | 2019-02-05 21:45:08 -0800 | [diff] [blame] | 147 | void CopyPosition( |
| 148 | const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder, |
| 149 | ::frc971::PotAndIndexPosition *position, |
| 150 | ::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 Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 168 | 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 Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 174 | ::aos::EventLoop *event_loop_; |
| 175 | ::aos::Sender<::aos::RobotState> robot_state_sender_; |
| 176 | |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 177 | 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 Schuh | 2c2cc2e | 2019-02-02 20:19:45 -0800 | [diff] [blame] | 183 | // Gets called right before the DMA synchronizer is up and running. |
| 184 | virtual void Start() {} |
| 185 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 186 | // Sets up everything during startup. |
| 187 | void DoStart(); |
| 188 | |
| 189 | // Runs a single iteration. |
| 190 | void Loop(int iterations); |
| 191 | |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 192 | // 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 Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 195 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 196 | bool pwm_trigger_ = false; |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 197 | |
Sabina Davis | 1ffa417 | 2019-02-01 22:38:33 -0800 | [diff] [blame] | 198 | ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_; |
| 199 | |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 200 | ::std::atomic<bool> run_{true}; |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 201 | ::frc::DriverStation *ds_; |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame^] | 202 | |
| 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 Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | } // namespace wpilib |
| 216 | } // namespace frc971 |
| 217 | |
| 218 | #endif // FRC971_WPILIB_SENSOR_READER_H_ |