From 55a97f0c11b072d18809e160a6f2bfc64f228e8d Mon Sep 17 00:00:00 2001 From: sciencewhiz Date: Sat, 10 May 2025 07:18:19 -0700 Subject: [PATCH] [examples] Add Analog Potentiometer Snippets (#7957) --- .../cpp/snippets/AnalogInput/cpp/Robot.cpp | 2 +- .../AnalogPotentiometer/cpp/Robot.cpp | 45 +++++++++++++++++++ .../src/main/cpp/snippets/snippets.json | 10 +++++ .../snippets/analogpotentiometer/Main.java | 25 +++++++++++ .../snippets/analogpotentiometer/Robot.java | 45 +++++++++++++++++++ .../wpi/first/wpilibj/snippets/snippets.json | 11 +++++ 6 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 wpilibcExamples/src/main/cpp/snippets/AnalogPotentiometer/cpp/Robot.cpp create mode 100644 wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogpotentiometer/Main.java create mode 100644 wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogpotentiometer/Robot.java diff --git a/wpilibcExamples/src/main/cpp/snippets/AnalogInput/cpp/Robot.cpp b/wpilibcExamples/src/main/cpp/snippets/AnalogInput/cpp/Robot.cpp index d92be69132..3585b68c15 100644 --- a/wpilibcExamples/src/main/cpp/snippets/AnalogInput/cpp/Robot.cpp +++ b/wpilibcExamples/src/main/cpp/snippets/AnalogInput/cpp/Robot.cpp @@ -6,7 +6,7 @@ #include /** - * 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 { diff --git a/wpilibcExamples/src/main/cpp/snippets/AnalogPotentiometer/cpp/Robot.cpp b/wpilibcExamples/src/main/cpp/snippets/AnalogPotentiometer/cpp/Robot.cpp new file mode 100644 index 0000000000..7cf15bf55d --- /dev/null +++ b/wpilibcExamples/src/main/cpp/snippets/AnalogPotentiometer/cpp/Robot.cpp @@ -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 +#include +#include + +/** + * 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(); +} +#endif diff --git a/wpilibcExamples/src/main/cpp/snippets/snippets.json b/wpilibcExamples/src/main/cpp/snippets/snippets.json index 540544ba7f..fb3733cacb 100644 --- a/wpilibcExamples/src/main/cpp/snippets/snippets.json +++ b/wpilibcExamples/src/main/cpp/snippets/snippets.json @@ -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" } ] diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogpotentiometer/Main.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogpotentiometer/Main.java new file mode 100644 index 0000000000..b64b880a95 --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogpotentiometer/Main.java @@ -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. + * + *

If you change your main robot class, change the parameter type. + */ + public static void main(String... args) { + RobotBase.startRobot(Robot::new); + } +} diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogpotentiometer/Robot.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogpotentiometer/Robot.java new file mode 100644 index 0000000000..cbb836cec7 --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogpotentiometer/Robot.java @@ -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(); + } +} diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/snippets.json b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/snippets.json index a9bcdc5ba7..b82c3a9130 100644 --- a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/snippets.json +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/snippets.json @@ -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" } ]