diff --git a/wpilibcExamples/src/main/cpp/snippets/BuiltInAccelerometer/cpp/Robot.cpp b/wpilibcExamples/src/main/cpp/snippets/BuiltInAccelerometer/cpp/Robot.cpp new file mode 100644 index 0000000000..adfc831b3c --- /dev/null +++ b/wpilibcExamples/src/main/cpp/snippets/BuiltInAccelerometer/cpp/Robot.cpp @@ -0,0 +1,38 @@ +// 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 + +/** + * BuiltIn Accelerometer snippets for frc-docs. + * https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/accelerometers-software.html + */ +class Robot : public frc::TimedRobot { + public: + Robot() { + // Sets the accelerometer to measure between -8 and 8 G's + m_accelerometer.SetRange(frc::BuiltInAccelerometer::Range::kRange_8G); + } + + void TeleopPeriodic() override { + // Gets the current acceleration in the X axis + m_accelerometer.GetX(); + // Gets the current acceleration in the Y axis + m_accelerometer.GetY(); + // Gets the current acceleration in the Z axis + m_accelerometer.GetZ(); + } + + private: + // Creates an object for the built-in accelerometer + // Range defaults to +- 8 G's + frc::BuiltInAccelerometer m_accelerometer; +}; + +#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 2577b0078c..e3a8ba57bb 100644 --- a/wpilibcExamples/src/main/cpp/snippets/snippets.json +++ b/wpilibcExamples/src/main/cpp/snippets/snippets.json @@ -124,5 +124,15 @@ ], "foldername": "ADXLAccelerometers", "gradlebase": "cpp" + }, + { + "name": "BuiltInAccelerometer", + "description": "Snippets of BuiltIn Accelerometer for frc-docs.", + "tags": [ + "Hardware", + "Accelerometer" + ], + "foldername": "BuiltInAccelerometer", + "gradlebase": "cpp" } ] diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/builtinaccelerometer/Main.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/builtinaccelerometer/Main.java new file mode 100644 index 0000000000..f19955661f --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/builtinaccelerometer/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.builtinaccelerometer; + +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/builtinaccelerometer/Robot.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/builtinaccelerometer/Robot.java new file mode 100644 index 0000000000..3b0b93f65a --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/builtinaccelerometer/Robot.java @@ -0,0 +1,34 @@ +// 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.builtinaccelerometer; + +import edu.wpi.first.wpilibj.BuiltInAccelerometer; +import edu.wpi.first.wpilibj.TimedRobot; + +/** + * Built-In Accelerometer snippets for frc-docs. + * https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/accelerometers-software.html + */ +public class Robot extends TimedRobot { + // Creates an object for the built-in accelerometer + // Range defaults to +- 8 G's + BuiltInAccelerometer m_accelerometer = new BuiltInAccelerometer(); + + /** Called once at the beginning of the robot program. */ + public Robot() { + // Sets the accelerometer to measure between -8 and 8 G's + m_accelerometer.setRange(BuiltInAccelerometer.Range.k8G); + } + + @Override + public void teleopPeriodic() { + // Gets the current acceleration in the X axis + m_accelerometer.getX(); + // Gets the current acceleration in the Y axis + m_accelerometer.getY(); + // Gets the current acceleration in the Z axis + m_accelerometer.getZ(); + } +} 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 b8d8d3e38c..04d49e34f7 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 @@ -136,5 +136,16 @@ "foldername": "adxlaccelerometers", "gradlebase": "java", "mainclass": "Main" + }, + { + "name": "BuiltInAccelerometer", + "description": "Snippets of BuiltIn accelerometer for frc-docs.", + "tags": [ + "Hardware", + "Accelerometer" + ], + "foldername": "builtinaccelerometer", + "gradlebase": "java", + "mainclass": "Main" } ]