[examples] Add Accelerometer collision detection snippets

This commit is contained in:
sciencewhiz
2025-05-10 21:19:23 -07:00
committed by Peter Johnson
parent 89555383cc
commit 0877d130be
5 changed files with 122 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.accelerometercollision;
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,36 @@
// 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.accelerometercollision;
import edu.wpi.first.wpilibj.BuiltInAccelerometer;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
/**
* Collision detection snippets for frc-docs.
* https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/accelerometers-software.html
*/
@SuppressWarnings("checkstyle:VariableDeclarationUsageDistance")
public class Robot extends TimedRobot {
double m_prevXAccel;
double m_prevYAccel;
BuiltInAccelerometer m_accelerometer = new BuiltInAccelerometer();
@Override
public void robotPeriodic() {
// Gets the current accelerations in the X and Y directions
double xAccel = m_accelerometer.getX();
double yAccel = m_accelerometer.getY();
// Calculates the jerk in the X and Y directions
// Divides by .02 because default loop timing is 20ms
double xJerk = (xAccel - m_prevXAccel) / 0.02;
double yJerk = (yAccel - m_prevYAccel) / 0.02;
m_prevXAccel = xAccel;
m_prevYAccel = yAccel;
SmartDashboard.putNumber("X Jerk", xJerk);
SmartDashboard.putNumber("Y Jerk", yJerk);
}
}

View File

@@ -147,5 +147,16 @@
"foldername": "builtinaccelerometer",
"gradlebase": "java",
"mainclass": "Main"
},
{
"name": "AccelerometerCollisionDetection",
"description": "Snippets of Accelerometer for collision detection for frc-docs.",
"tags": [
"Hardware",
"Accelerometer"
],
"foldername": "accelerometercollision",
"gradlebase": "java",
"mainclass": "Main"
}
]