[examples] Add Analog Potentiometer Snippets (#7957)

This commit is contained in:
sciencewhiz
2025-05-10 07:18:19 -07:00
committed by GitHub
parent b1f7e6d6f2
commit 55a97f0c11
6 changed files with 137 additions and 1 deletions

View File

@@ -6,7 +6,7 @@
#include <frc/TimedRobot.h>
/**
* AnalogEncoder snippets for frc-docs.
* AnalogInput snippets for frc-docs.
* https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/analog-input-software.html
*/
class Robot : public frc::TimedRobot {

View File

@@ -0,0 +1,45 @@
// 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 <frc/AnalogInput.h>
#include <frc/AnalogPotentiometer.h>
#include <frc/TimedRobot.h>
/**
* AnalogPotentiometer snippets for frc-docs.
* https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/analog-potentiometers-software.html
*/
class Robot : public frc::TimedRobot {
public:
Robot() {
// Set averaging bits to 2
m_input.SetAverageBits(2);
}
void TeleopPeriodic() override {
// Get the value of the potentiometer
m_pot.Get();
}
private:
// Initializes an AnalogPotentiometer on analog port 0
// The full range of motion (in meaningful external units) is 0-180 (this
// could be degrees, for instance) The "starting point" of the motion, i.e.
// where the mechanism is located when the potentiometer reads 0v, is 30.
frc::AnalogPotentiometer m_pot{0, 180, 30};
// Initializes an AnalogInput on port 1
frc::AnalogInput m_input{1};
// Initializes an AnalogPotentiometer with the given AnalogInput
// The full range of motion (in meaningful external units) is 0-180 (this
// could be degrees, for instance) The "starting point" of the motion, i.e.
// where the mechanism is located when the potentiometer reads 0v, is 30.
frc::AnalogPotentiometer m_pot1{&m_input, 180, 30};
};
#ifndef RUNNING_FRC_TESTS
int main() {
return frc::StartRobot<Robot>();
}
#endif

View File

@@ -93,5 +93,15 @@
],
"foldername": "AnalogInput",
"gradlebase": "cpp"
},
{
"name": "AnalogPotentiometer",
"description": "Snippets of Analog Potentiometer for frc-docs.",
"tags": [
"Hardware",
"Analog"
],
"foldername": "AnalogPotentiometer",
"gradlebase": "cpp"
}
]