[examples] Add remaining Encoder snippets (#7936)

This commit is contained in:
sciencewhiz
2025-04-28 16:25:49 -07:00
committed by GitHub
parent d059cbc157
commit 659710a79a
12 changed files with 396 additions and 2 deletions

View File

@@ -0,0 +1,37 @@
// 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/AnalogEncoder.h>
#include <frc/TimedRobot.h>
/**
* AnalogEncoder 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();
}
private:
// Initializes an analog encoder on Analog Input pin 0
frc::AnalogEncoder m_encoder{0};
// Initializes an analog 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::AnalogEncoder m_encoderFR{0, 4.0, 2.0};
};
#ifndef RUNNING_FRC_TESTS
int main() {
return frc::StartRobot<Robot>();
}
#endif

View File

@@ -0,0 +1,56 @@
// 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/Encoder.h>
#include <frc/TimedRobot.h>
#include <frc/drive/DifferentialDrive.h>
#include <frc/motorcontrol/Spark.h>
/**
* Encoder drive to distance 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() {
// Configures the encoder's distance-per-pulse
// The robot moves forward 1 foot per encoder rotation
// There are 256 pulses per encoder rotation
m_encoder.SetDistancePerPulse(1.0 / 256.0);
// Invert the right side of the drivetrain. You might have to invert the
// other side
rightLeader.SetInverted(true);
// Configure the followers to follow the leaders
leftLeader.AddFollower(leftFollower);
rightLeader.AddFollower(rightFollower);
}
void AutonomousPeriodic() override {
// Drives forward at half speed until the robot has moved 5 feet, then
// stops:
if (m_encoder.GetDistance() < 5) {
drive.TankDrive(0.5, 0.5);
} else {
drive.TankDrive(0, 0);
}
}
private:
// Creates an encoder on DIO ports 0 and 1.
frc::Encoder m_encoder{0, 1};
// Initialize motor controllers and drive
frc::Spark leftLeader{0};
frc::Spark leftFollower{1};
frc::Spark rightLeader{2};
frc::Spark rightFollower{3};
frc::DifferentialDrive drive{[&](double output) { leftLeader.Set(output); },
[&](double output) { rightLeader.Set(output); }};
};
#ifndef RUNNING_FRC_TESTS
int main() {
return frc::StartRobot<Robot>();
}
#endif

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/DigitalInput.h>
#include <frc/Encoder.h>
#include <frc/TimedRobot.h>
#include <frc/motorcontrol/Spark.h>
/**
* Encoder mechanism homing snippets for frc-docs.
* https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/encoders-software.html
*/
class Robot : public frc::TimedRobot {
public:
void AutonomousPeriodic() override {
// Runs the motor backwards at half speed until the limit switch is pressed
// then turn off the motor and reset the encoder
if (!m_limit.Get()) {
m_spark.Set(-0.5);
} else {
m_spark.Set(0);
m_encoder.Reset();
}
}
private:
frc::Encoder m_encoder{0, 1};
frc::Spark m_spark{0};
// Limit switch on DIO 2
frc::DigitalInput m_limit{2};
};
#ifndef RUNNING_FRC_TESTS
int main() {
return frc::StartRobot<Robot>();
}
#endif

View File

@@ -19,5 +19,38 @@
],
"foldername": "DutyCycleEncoder",
"gradlebase": "cpp"
},
{
"name": "AnalogEncoder",
"description": "Snippets of AnalogEncoder class usage for frc-docs.",
"tags": [
"Hardware",
"Encoder",
"Analog"
],
"foldername": "AnalogEncoder",
"gradlebase": "cpp"
},
{
"name": "EncoderDrive",
"description": "Snippets of driving to a distance for frc-docs.",
"tags": [
"Hardware",
"Encoder",
"Differential Drive"
],
"foldername": "EncoderDrive",
"gradlebase": "cpp"
},
{
"name": "EncoderHoming",
"description": "Snippets of homing a mechanism for frc-docs.",
"tags": [
"Hardware",
"Encoder",
"Digital Input"
],
"foldername": "EncoderHoming",
"gradlebase": "cpp"
}
]