diff --git a/wpilibcExamples/src/main/cpp/snippets/AccelerometerCollision/cpp/Robot.cpp b/wpilibcExamples/src/main/cpp/snippets/AccelerometerCollision/cpp/Robot.cpp new file mode 100644 index 0000000000..e5d2d83f10 --- /dev/null +++ b/wpilibcExamples/src/main/cpp/snippets/AccelerometerCollision/cpp/Robot.cpp @@ -0,0 +1,40 @@ +// 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 + +/** + * Collision detection snippets for frc-docs. + * https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/accelerometers-software.html + */ +class Robot : public frc::TimedRobot { + public: + void RobotPeriodic() override { + // 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; + + frc::SmartDashboard::PutNumber("X Jerk", xJerk); + frc::SmartDashboard::PutNumber("Y Jerk", yJerk); + } + + private: + double m_prevXAccel = 0.0; + double m_prevYAccel = 0.0; + 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 e3a8ba57bb..43d37a4867 100644 --- a/wpilibcExamples/src/main/cpp/snippets/snippets.json +++ b/wpilibcExamples/src/main/cpp/snippets/snippets.json @@ -134,5 +134,15 @@ ], "foldername": "BuiltInAccelerometer", "gradlebase": "cpp" + }, + { + "name": "AccelerometerCollisionDetection", + "description": "Snippets of Accelerometer collision detection for frc-docs.", + "tags": [ + "Hardware", + "Accelerometer" + ], + "foldername": "AccelerometerCollision", + "gradlebase": "cpp" } ] diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/accelerometercollision/Main.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/accelerometercollision/Main.java new file mode 100644 index 0000000000..66cda16a11 --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/accelerometercollision/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.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. + * + *

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/accelerometercollision/Robot.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/accelerometercollision/Robot.java new file mode 100644 index 0000000000..86d343c8c2 --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/accelerometercollision/Robot.java @@ -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); + } +} 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 04d49e34f7..1edef3dc4a 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 @@ -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" } ]