[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"
}
]

View File

@@ -0,0 +1,25 @@
// 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.
package edu.wpi.first.wpilibj.snippets.analogpotentiometer;
import edu.wpi.first.wpilibj.RobotBase;
/**
* Do NOT add any static variables to this class, or any initialization at all. Unless you know what
* you are doing, do not modify this file except to change the parameter class to the startRobot
* call.
*/
public final class Main {
private Main() {}
/**
* Main initialization function. Do not perform any initialization here.
*
* <p>If you change your main robot class, change the parameter type.
*/
public static void main(String... args) {
RobotBase.startRobot(Robot::new);
}
}

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.
package edu.wpi.first.wpilibj.snippets.analogpotentiometer;
import edu.wpi.first.wpilibj.AnalogInput;
import edu.wpi.first.wpilibj.AnalogPotentiometer;
import edu.wpi.first.wpilibj.TimedRobot;
/**
* AnalogPotentiometer snippets for frc-docs.
* https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/analog-poteniometers-software.html
*/
public class Robot extends TimedRobot {
// 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.
AnalogPotentiometer m_pot = new AnalogPotentiometer(0, 180, 30);
// Initializes an AnalogInput on port 1
AnalogInput m_input = new AnalogInput(0);
// 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.
AnalogPotentiometer m_pot1 = new AnalogPotentiometer(m_input, 180, 30);
/** Called once at the beginning of the robot program. */
public Robot() {
// Set averaging bits to 2
m_input.setAverageBits(2);
}
@Override
public void teleopPeriodic() {
// Get the value of the potentiometer
m_pot.get();
m_pot1.get();
}
}

View File

@@ -102,5 +102,16 @@
"foldername": "analoginput",
"gradlebase": "java",
"mainclass": "Main"
},
{
"name": "AnalogPotentiometer",
"description": "Snippets of Analog Potentiometer for frc-docs.",
"tags": [
"Hardware",
"Analog"
],
"foldername": "analogpotentiometer",
"gradlebase": "java",
"mainclass": "Main"
}
]