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 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | #include "aos/events/event_loop.h" |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 8 | #include "aos/events/shm_event_loop.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" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 11 | #include "frc971/control_loops/control_loops_generated.h" |
Maxwell Henderson | f382b4b | 2024-01-14 12:31:41 -0800 | [diff] [blame^] | 12 | #include "frc971/control_loops/drivetrain/drivetrain_position_static.h" |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 13 | #include "frc971/input/robot_state_generated.h" |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 14 | #include "frc971/wpilib/ahal/DigitalGlitchFilter.h" |
| 15 | #include "frc971/wpilib/ahal/DigitalInput.h" |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 16 | #include "frc971/wpilib/ahal/DriverStation.h" |
Sabina Davis | 1ffa417 | 2019-02-01 22:38:33 -0800 | [diff] [blame] | 17 | #include "frc971/wpilib/dma.h" |
| 18 | #include "frc971/wpilib/dma_edge_counting.h" |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 19 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 20 | |
| 21 | using ::aos::monotonic_clock; |
| 22 | namespace chrono = ::std::chrono; |
| 23 | |
| 24 | namespace frc971 { |
| 25 | namespace wpilib { |
| 26 | |
| 27 | class SensorReader { |
| 28 | public: |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 29 | SensorReader(::aos::ShmEventLoop *event_loop); |
Austin Schuh | 2c2cc2e | 2019-02-02 20:19:45 -0800 | [diff] [blame] | 30 | virtual ~SensorReader() {} |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 31 | |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 32 | // Updates the fast and medium encoder filter frequencies. |
| 33 | void UpdateFastEncoderFilterHz(int hz); |
| 34 | void UpdateMediumEncoderFilterHz(int hz); |
| 35 | |
Sabina Davis | b6317b7 | 2019-02-01 22:53:23 -0800 | [diff] [blame] | 36 | // Sets the left drivetrain encoder. |
| 37 | void set_drivetrain_left_encoder(::std::unique_ptr<frc::Encoder> encoder); |
| 38 | |
| 39 | // Sets the right drivetrain encoder. |
| 40 | void set_drivetrain_right_encoder(::std::unique_ptr<frc::Encoder> encoder); |
| 41 | |
Sabina Davis | 6292bec | 2019-02-06 22:53:14 -0800 | [diff] [blame] | 42 | // Adds a sensor to DMA. |
Austin Schuh | 2c2cc2e | 2019-02-02 20:19:45 -0800 | [diff] [blame] | 43 | void AddToDMA(DMASampleHandlerInterface *handler) { |
Sabina Davis | 6292bec | 2019-02-06 22:53:14 -0800 | [diff] [blame] | 44 | if (!dma_synchronizer_) { |
| 45 | dma_synchronizer_.reset( |
| 46 | new ::frc971::wpilib::DMASynchronizer(std::make_unique<DMA>())); |
| 47 | } |
Austin Schuh | 2c2cc2e | 2019-02-02 20:19:45 -0800 | [diff] [blame] | 48 | dma_synchronizer_->Add(handler); |
| 49 | } |
| 50 | |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 51 | // Sets PWM trigger mode. If true, synchronize the control loops with the PWM |
| 52 | // pulses. The sensors are sampled 50 uS after the falling edge of the PWM |
| 53 | // pulse. |
| 54 | void set_pwm_trigger(bool trigger) { pwm_trigger_ = trigger; } |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 55 | |
Sabina Davis | a42cc35 | 2019-02-01 22:55:50 -0800 | [diff] [blame] | 56 | // Stops the pwm trigger on the next iteration. |
| 57 | void Quit() { run_ = false; } |
| 58 | |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 59 | virtual void RunIteration() = 0; |
Sabina Davis | 6292bec | 2019-02-06 22:53:14 -0800 | [diff] [blame] | 60 | // Runs the DMA iteration after the synchronizer. This only gets run if a |
| 61 | // sensor has been added to DMA. |
Austin Schuh | 2c2cc2e | 2019-02-02 20:19:45 -0800 | [diff] [blame] | 62 | virtual void RunDmaIteration() {} |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 63 | |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 64 | protected: |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 65 | // Copies a DMAEncoder to a IndexPosition with the correct unit and direction |
| 66 | // changes. |
| 67 | void CopyPosition(const ::frc971::wpilib::DMAEncoder &encoder, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 68 | ::frc971::IndexPositionT *position, |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 69 | double encoder_counts_per_revolution, double encoder_ratio, |
| 70 | bool reverse) { |
| 71 | const double multiplier = reverse ? -1.0 : 1.0; |
| 72 | position->encoder = |
| 73 | multiplier * encoder_translate(encoder.polled_encoder_value(), |
| 74 | encoder_counts_per_revolution, |
| 75 | encoder_ratio); |
| 76 | position->latched_encoder = |
| 77 | multiplier * encoder_translate(encoder.last_encoder_value(), |
| 78 | encoder_counts_per_revolution, |
| 79 | encoder_ratio); |
| 80 | position->index_pulses = encoder.index_posedge_count(); |
| 81 | } |
| 82 | |
| 83 | // Copies a AbsoluteEncoderAndPotentiometer to a PotAndAbsolutePosition with |
| 84 | // the correct unit and direction changes. |
| 85 | void CopyPosition( |
| 86 | const ::frc971::wpilib::AbsoluteEncoderAndPotentiometer &encoder, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 87 | ::frc971::PotAndAbsolutePositionT *position, |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 88 | double encoder_counts_per_revolution, double encoder_ratio, |
| 89 | ::std::function<double(double)> potentiometer_translate, bool reverse, |
| 90 | double pot_offset) { |
| 91 | const double multiplier = reverse ? -1.0 : 1.0; |
| 92 | position->pot = multiplier * potentiometer_translate( |
| 93 | encoder.ReadPotentiometerVoltage()) + |
| 94 | pot_offset; |
| 95 | position->encoder = |
| 96 | multiplier * encoder_translate(encoder.ReadRelativeEncoder(), |
| 97 | encoder_counts_per_revolution, |
| 98 | encoder_ratio); |
| 99 | |
| 100 | position->absolute_encoder = |
| 101 | (reverse ? (1.0 - encoder.ReadAbsoluteEncoder()) |
| 102 | : encoder.ReadAbsoluteEncoder()) * |
| 103 | encoder_ratio * (2.0 * M_PI); |
| 104 | } |
| 105 | |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 106 | // Copies an AbsoluteEncoderAndPotentiometer to an AbsoluteAndAbsolutePosition |
| 107 | // with the correct unit and direction changes. |
| 108 | void CopyPosition(const ::frc971::wpilib::AbsoluteAndAbsoluteEncoder &encoder, |
| 109 | ::frc971::AbsoluteAndAbsolutePositionT *position, |
| 110 | double encoder_counts_per_revolution, double encoder_ratio, |
| 111 | double single_turn_encoder_ratio, bool reverse) { |
| 112 | const double multiplier = reverse ? -1.0 : 1.0; |
| 113 | position->encoder = |
| 114 | multiplier * encoder_translate(encoder.ReadRelativeEncoder(), |
| 115 | encoder_counts_per_revolution, |
| 116 | encoder_ratio); |
| 117 | |
| 118 | position->absolute_encoder = |
| 119 | (reverse ? (1.0 - encoder.ReadAbsoluteEncoder()) |
| 120 | : encoder.ReadAbsoluteEncoder()) * |
| 121 | encoder_ratio * (2.0 * M_PI); |
| 122 | |
| 123 | position->single_turn_absolute_encoder = |
| 124 | (reverse ? (1.0 - encoder.ReadSingleTurnAbsoluteEncoder()) |
| 125 | : encoder.ReadSingleTurnAbsoluteEncoder()) * |
| 126 | single_turn_encoder_ratio * (2.0 * M_PI); |
| 127 | } |
| 128 | |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 129 | // Copies a DMAEdgeCounter to a HallEffectAndPosition with the correct unit |
| 130 | // and direction changes. |
| 131 | void CopyPosition(const ::frc971::wpilib::DMAEdgeCounter &counter, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 132 | ::frc971::HallEffectAndPositionT *position, |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 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(counter.polled_encoder(), |
| 138 | encoder_counts_per_revolution, |
| 139 | encoder_ratio); |
| 140 | position->current = !counter.polled_value(); |
| 141 | position->posedge_count = counter.negative_count(); |
| 142 | position->negedge_count = counter.positive_count(); |
| 143 | position->posedge_value = |
| 144 | multiplier * encoder_translate(counter.last_negative_encoder_value(), |
| 145 | encoder_counts_per_revolution, |
| 146 | encoder_ratio); |
| 147 | position->negedge_value = |
| 148 | multiplier * encoder_translate(counter.last_positive_encoder_value(), |
| 149 | encoder_counts_per_revolution, |
| 150 | encoder_ratio); |
| 151 | } |
| 152 | |
Sabina Davis | 8d8ac0a | 2019-02-06 23:51:07 -0800 | [diff] [blame] | 153 | // Copies a Absolute Encoder with the correct unit |
| 154 | // and direction changes. |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 155 | void CopyPosition(const ::frc971::wpilib::AbsoluteEncoder &encoder, |
| 156 | ::frc971::AbsolutePositionT *position, |
| 157 | double encoder_counts_per_revolution, double encoder_ratio, |
| 158 | bool reverse) { |
Sabina Davis | 8d8ac0a | 2019-02-06 23:51:07 -0800 | [diff] [blame] | 159 | const double multiplier = reverse ? -1.0 : 1.0; |
| 160 | position->encoder = |
| 161 | multiplier * encoder_translate(encoder.ReadRelativeEncoder(), |
| 162 | encoder_counts_per_revolution, |
| 163 | encoder_ratio); |
| 164 | |
| 165 | position->absolute_encoder = |
| 166 | (reverse ? (1.0 - encoder.ReadAbsoluteEncoder()) |
| 167 | : encoder.ReadAbsoluteEncoder()) * |
| 168 | encoder_ratio * (2.0 * M_PI); |
| 169 | } |
| 170 | |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 171 | void CopyPosition(const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder, |
| 172 | ::frc971::PotAndIndexPositionT *position, |
| 173 | ::std::function<double(int32_t)> encoder_translate, |
| 174 | ::std::function<double(double)> potentiometer_translate, |
| 175 | bool reverse, double pot_offset) { |
Sabina Davis | 2243cab | 2019-02-05 21:45:08 -0800 | [diff] [blame] | 176 | const double multiplier = reverse ? -1.0 : 1.0; |
| 177 | position->encoder = |
| 178 | multiplier * encoder_translate(encoder.polled_encoder_value()); |
| 179 | position->pot = multiplier * potentiometer_translate( |
| 180 | encoder.polled_potentiometer_voltage()) + |
| 181 | pot_offset; |
| 182 | position->latched_encoder = |
| 183 | multiplier * encoder_translate(encoder.last_encoder_value()); |
| 184 | position->latched_pot = |
| 185 | multiplier * |
| 186 | potentiometer_translate(encoder.last_potentiometer_voltage()) + |
| 187 | pot_offset; |
| 188 | position->index_pulses = encoder.index_posedge_count(); |
| 189 | } |
| 190 | |
Siddhant Kanwar | 0e37f59 | 2022-02-21 19:26:50 -0800 | [diff] [blame] | 191 | // Copies a relative digital encoder. |
Alex Perry | 2712c10 | 2020-02-17 19:17:11 -0800 | [diff] [blame] | 192 | void CopyPosition(const ::frc::Encoder &encoder, |
| 193 | ::frc971::RelativePositionT *position, |
| 194 | double encoder_counts_per_revolution, double encoder_ratio, |
| 195 | bool reverse) { |
| 196 | const double multiplier = reverse ? -1.0 : 1.0; |
| 197 | position->encoder = |
| 198 | multiplier * encoder_translate(encoder.GetRaw(), |
| 199 | encoder_counts_per_revolution, |
| 200 | encoder_ratio); |
| 201 | } |
| 202 | |
Siddhant Kanwar | 0e37f59 | 2022-02-21 19:26:50 -0800 | [diff] [blame] | 203 | // Copies a potentiometer |
| 204 | void CopyPosition(const ::frc::AnalogInput &input, |
| 205 | ::frc971::RelativePositionT *position, |
| 206 | ::std::function<double(double)> potentiometer_translate, |
| 207 | bool reverse, double pot_offset) { |
| 208 | const double multiplier = reverse ? -1.0 : 1.0; |
| 209 | position->encoder = |
| 210 | multiplier * potentiometer_translate(input.GetVoltage()) + pot_offset; |
| 211 | } |
| 212 | |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 213 | double encoder_translate(int32_t value, double counts_per_revolution, |
| 214 | double ratio) { |
| 215 | return static_cast<double>(value) / counts_per_revolution * ratio * |
| 216 | (2.0 * M_PI); |
| 217 | } |
| 218 | |
Maxwell Henderson | d5ae85b | 2024-01-08 16:14:28 -0800 | [diff] [blame] | 219 | void SendDrivetrainPosition( |
Maxwell Henderson | f382b4b | 2024-01-14 12:31:41 -0800 | [diff] [blame^] | 220 | aos::Sender<control_loops::drivetrain::PositionStatic>::StaticBuilder |
| 221 | builder, |
Maxwell Henderson | d5ae85b | 2024-01-08 16:14:28 -0800 | [diff] [blame] | 222 | std::function<double(double input)> velocity_translate, |
| 223 | std::function<double(double input)> encoder_to_meters, bool left_inverted, |
| 224 | bool right_inverted); |
| 225 | |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 226 | ::aos::EventLoop *event_loop_; |
| 227 | ::aos::Sender<::aos::RobotState> robot_state_sender_; |
| 228 | |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 229 | frc::DigitalGlitchFilter fast_encoder_filter_, medium_encoder_filter_; |
| 230 | |
| 231 | ::std::unique_ptr<frc::Encoder> drivetrain_left_encoder_, |
| 232 | drivetrain_right_encoder_; |
| 233 | |
| 234 | private: |
Austin Schuh | 2c2cc2e | 2019-02-02 20:19:45 -0800 | [diff] [blame] | 235 | // Gets called right before the DMA synchronizer is up and running. |
| 236 | virtual void Start() {} |
| 237 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 238 | // Sets up everything during startup. |
| 239 | void DoStart(); |
| 240 | |
| 241 | // Runs a single iteration. |
milind-u | 61227f2 | 2021-08-29 15:58:33 -0700 | [diff] [blame] | 242 | void Loop(); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 243 | |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 244 | // Returns the monotonic time of the start of the first PWM cycle. |
| 245 | // Returns min_time if no start time could be calculated. |
| 246 | monotonic_clock::time_point GetPWMStartTime(); |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 247 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 248 | bool pwm_trigger_ = false; |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 249 | |
Sabina Davis | 1ffa417 | 2019-02-01 22:38:33 -0800 | [diff] [blame] | 250 | ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_; |
| 251 | |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 252 | ::std::atomic<bool> run_{true}; |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 253 | ::frc::DriverStation *ds_; |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 254 | |
| 255 | const int32_t my_pid_; |
| 256 | |
milind-u | 61227f2 | 2021-08-29 15:58:33 -0700 | [diff] [blame] | 257 | // Pointer to the timer handler used to modify the wakeup. |
| 258 | ::aos::TimerHandler *timer_handler_; |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 259 | |
| 260 | // Last time we got called. |
| 261 | ::aos::monotonic_clock::time_point last_monotonic_now_ = |
| 262 | ::aos::monotonic_clock::min_time; |
| 263 | // The current period. |
| 264 | chrono::microseconds period_ = chrono::microseconds(5000); |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | } // namespace wpilib |
| 268 | } // namespace frc971 |
| 269 | |
| 270 | #endif // FRC971_WPILIB_SENSOR_READER_H_ |