[examples] Add DutyCycleEncoder Snippets (#7930)

This commit is contained in:
sciencewhiz
2025-04-25 21:47:53 -07:00
committed by GitHub
parent 828199befb
commit 90ee11a9e0
5 changed files with 124 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include <numbers>
#include <frc/DutyCycleEncoder.h>
#include <frc/TimedRobot.h>
/**
* DutyCycleEncoder snippets for frc-docs.
* https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/encoders-software.html
*/
class Robot : public frc::TimedRobot {
public:
Robot() {}
void TeleopPeriodic() override {
// Gets the rotation
m_encoder.Get();
// Gets if the encoder is connected
m_encoder.IsConnected();
}
private:
// Initializes a duty cycle encoder on DIO pins 0
frc::DutyCycleEncoder m_encoder{0};
// Initializes a duty cycle encoder on DIO pins 0 to return a value of 4 for
// a full rotation, with the encoder reporting 0 half way through rotation (2
// out of 4)
frc::DutyCycleEncoder m_encoderFR{0, 4.0, 2.0};
};
#ifndef RUNNING_FRC_TESTS
int main() {
return frc::StartRobot<Robot>();
}
#endif

View File

@@ -8,5 +8,16 @@
],
"foldername": "Encoder",
"gradlebase": "cpp"
},
{
"name": "DutyCycleEncoder",
"description": "Snippets of DutyCycleEncoder class usage for frc-docs.",
"tags": [
"Hardware",
"Encoder",
"Duty Cycle"
],
"foldername": "DutyCycleEncoder",
"gradlebase": "cpp"
}
]