diff --git a/wpilibcExamples/src/main/cpp/snippets/AnalogInput/cpp/Robot.cpp b/wpilibcExamples/src/main/cpp/snippets/AnalogInput/cpp/Robot.cpp new file mode 100644 index 0000000000..d92be69132 --- /dev/null +++ b/wpilibcExamples/src/main/cpp/snippets/AnalogInput/cpp/Robot.cpp @@ -0,0 +1,75 @@ +// 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 + +/** + * AnalogEncoder snippets for frc-docs. + * https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/analog-input-software.html + */ +class Robot : public frc::TimedRobot { + public: + Robot() { + // Sets the AnalogInput to 4-bit oversampling. 16 samples will be added + // together. + // Thus, the reported values will increase by about a factor of 16, and the + // update rate will decrease by a similar amount. + m_analog.SetOversampleBits(4); + + // Sets the AnalogInput to 4-bit averaging. 16 samples will be averaged + // together. The update rate will decrease by a factor of 16. + m_analog.SetAverageBits(4); + + // Gets the raw instantaneous measured value from the analog input, without + // applying any calibration and ignoring oversampling and averaging + // settings. + m_analog.GetValue(); + + // Gets the instantaneous measured voltage from the analog input. + // Oversampling and averaging settings are ignored + m_analog.GetVoltage(); + + // Gets the averaged value from the analog input. The value is not + // rescaled, but oversampling and averaging are both applied. + m_analog.GetAverageValue(); + + // Gets the averaged voltage from the analog input. Rescaling, + // oversampling, and averaging are all applied. + m_analog.GetAverageVoltage(); + + // Sets the initial value of the accumulator to 0 + // This is the "starting point" from which the value will change over time + m_analog.SetAccumulatorInitialValue(0); + // Sets the "center" of the accumulator to 0. This value is subtracted from + // all measured values prior to accumulation. + m_analog.SetAccumulatorCenter(0); + // Returns the number of accumulated samples since the accumulator was last + // started/reset + m_analog.GetAccumulatorCount(); + // Returns the value of the accumulator. Return type is long. + m_analog.GetAccumulatorValue(); + // Resets the accumulator to the initial value + m_analog.ResetAccumulator(); + } + + void TeleopPeriodic() override { + // Fill the count and value variables with the matched measurements + m_analog.GetAccumulatorOutput(count, value); + } + + private: + // Initializes an AnalogInput on port 0 + frc::AnalogInput m_analog{0}; + + // The count and value variables to fill + int64_t count; + int64_t value; +}; + +#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 4ccb059c0c..540544ba7f 100644 --- a/wpilibcExamples/src/main/cpp/snippets/snippets.json +++ b/wpilibcExamples/src/main/cpp/snippets/snippets.json @@ -83,5 +83,15 @@ ], "foldername": "AnalogTrigger", "gradlebase": "cpp" + }, + { + "name": "AnalogInput", + "description": "Snippets of Analog Input for frc-docs.", + "tags": [ + "Hardware", + "Analog" + ], + "foldername": "AnalogInput", + "gradlebase": "cpp" } ] diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analoginput/Main.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analoginput/Main.java new file mode 100644 index 0000000000..767b0c3bce --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analoginput/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.analoginput; + +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/analoginput/Robot.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analoginput/Robot.java new file mode 100644 index 0000000000..10bfcdf44d --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analoginput/Robot.java @@ -0,0 +1,76 @@ +// 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.analoginput; + +import edu.wpi.first.hal.AccumulatorResult; +import edu.wpi.first.wpilibj.AnalogInput; +import edu.wpi.first.wpilibj.TimedRobot; +import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; + +/** + * AnalogInput snippets for frc-docs. + * https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/analog-inputs-software.html + */ +public class Robot extends TimedRobot { + // Initializes an AnalogInput on port 0 + AnalogInput m_analog = new AnalogInput(0); + + // Instantiate an AccumulatorResult object to hold the matched measurements + AccumulatorResult m_result = new AccumulatorResult(); + + /** Called once at the beginning of the robot program. */ + public Robot() { + // Sets the AnalogInput to 4-bit oversampling. 16 samples will be added together. + // Thus, the reported values will increase by about a factor of 16, and the update + // rate will decrease by a similar amount. + m_analog.setOversampleBits(4); + + // Sets the AnalogInput to 4-bit averaging. 16 samples will be averaged together. + // The update rate will decrease by a factor of 16. + m_analog.setAverageBits(4); + } + + @Override + public void teleopPeriodic() { + // Gets the raw instantaneous measured value from the analog input, without + // applying any calibration and ignoring oversampling and averaging + // settings. + m_analog.getValue(); + + // Gets the instantaneous measured voltage from the analog input. + // Oversampling and averaging settings are ignored + m_analog.getVoltage(); + + // Gets the averaged value from the analog input. The value is not + // rescaled, but oversampling and averaging are both applied. + m_analog.getAverageValue(); + + // Gets the averaged voltage from the analog input. Rescaling, + // oversampling, and averaging are all applied. + m_analog.getAverageVoltage(); + + // Sets the initial value of the accumulator to 0 + // This is the "starting point" from which the value will change over time + m_analog.setAccumulatorInitialValue(0); + // Sets the "center" of the accumulator to 0. This value is subtracted from + // all measured values prior to accumulation. + m_analog.setAccumulatorCenter(0); + // Returns the number of accumulated samples since the accumulator was last started/reset + m_analog.getAccumulatorCount(); + // Returns the value of the accumulator. Return type is long. + m_analog.getAccumulatorValue(); + // Resets the accumulator to the initial value + m_analog.resetAccumulator(); + + // Fill the AccumulatorResult with the matched measurements + m_analog.getAccumulatorOutput(m_result); + // Read the values from the AccumulatorResult + long count = m_result.count; + long value = m_result.value; + + SmartDashboard.putNumber("Accumulator Count", count); + SmartDashboard.putNumber("Accumulator Value", value); + } +} 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 4fada01e0b..a9bcdc5ba7 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 @@ -91,5 +91,16 @@ "foldername": "analogtrigger", "gradlebase": "java", "mainclass": "Main" + }, + { + "name": "AnalogInput", + "description": "Snippets of Analog Input for frc-docs.", + "tags": [ + "Hardware", + "Analog" + ], + "foldername": "analoginput", + "gradlebase": "java", + "mainclass": "Main" } ]