[examples] Add BuiltInAccelerometer Snippet

This commit is contained in:
sciencewhiz
2025-05-10 20:11:51 -07:00
committed by Peter Johnson
parent de315947e9
commit 89555383cc
5 changed files with 118 additions and 0 deletions

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.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.
*
* <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,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();
}
}

View File

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