Added AbsoluteEncoder alone to wpilib.
Made class to support having just a absolute encoder and added a
copyposition function for it.
Change-Id: I9f3c4354637b8197ed29e8e56649cbe7a61f5197
diff --git a/frc971/wpilib/encoder_and_potentiometer.h b/frc971/wpilib/encoder_and_potentiometer.h
index 3516168..42fb86b 100644
--- a/frc971/wpilib/encoder_and_potentiometer.h
+++ b/frc971/wpilib/encoder_and_potentiometer.h
@@ -271,6 +271,24 @@
::std::unique_ptr<frc::AnalogInput> potentiometer_;
};
+class AbsoluteEncoder {
+ public:
+ void set_absolute_pwm(::std::unique_ptr<frc::DigitalInput> input) {
+ duty_cycle_.set_input(::std::move(input));
+ }
+
+ void set_encoder(::std::unique_ptr<frc::Encoder> encoder) {
+ encoder_ = ::std::move(encoder);
+ }
+
+ double ReadAbsoluteEncoder() const { return duty_cycle_.Read(); }
+ int32_t ReadRelativeEncoder() const { return encoder_->GetRaw(); }
+
+ private:
+ DutyCycleReader duty_cycle_;
+ ::std::unique_ptr<frc::Encoder> encoder_;
+};
+
} // namespace wpilib
} // namespace frc971
diff --git a/frc971/wpilib/sensor_reader.h b/frc971/wpilib/sensor_reader.h
index fb25360..02dea2c 100644
--- a/frc971/wpilib/sensor_reader.h
+++ b/frc971/wpilib/sensor_reader.h
@@ -118,6 +118,25 @@
encoder_ratio);
}
+ // Copies a Absolute Encoder with the correct unit
+ // and direction changes.
+ void CopyPosition(
+ const ::frc971::wpilib::AbsoluteEncoder &encoder,
+ ::frc971::AbsolutePosition *position,
+ double encoder_counts_per_revolution, double encoder_ratio,
+ bool reverse) {
+ const double multiplier = reverse ? -1.0 : 1.0;
+ position->encoder =
+ multiplier * encoder_translate(encoder.ReadRelativeEncoder(),
+ encoder_counts_per_revolution,
+ encoder_ratio);
+
+ position->absolute_encoder =
+ (reverse ? (1.0 - encoder.ReadAbsoluteEncoder())
+ : encoder.ReadAbsoluteEncoder()) *
+ encoder_ratio * (2.0 * M_PI);
+ }
+
double encoder_translate(int32_t value, double counts_per_revolution,
double ratio) {
return static_cast<double>(value) / counts_per_revolution * ratio *