[examples] Add Snippets for Accelerometers using OnboardIMU (#8087)

This commit is contained in:
sciencewhiz
2025-07-20 22:19:14 -07:00
committed by GitHub
parent 946ab9e98f
commit 09680072ac
18 changed files with 504 additions and 1 deletions

View File

@@ -74,7 +74,7 @@ def tagList = [
/* --- Hardware --- */
"Analog", "Gyro", "Pneumatics", "I2C", "Duty Cycle", "PDP",
"AddressableLEDs", "HAL", "Encoder", "Smart Motor Controller", "Digital Input",
"Digital Output", "Accelerometer",
"Digital Output", "Accelerometer", "IMU",
/* --- HID --- */
"XboxController", "PS4Controller", "PS5Controller", "Joystick",

View File

@@ -68,6 +68,9 @@ COMMANDS_V2_FOLDERS = [
]
SNIPPETS_FOLDERS = [
"ADXLAccelerometers",
"AccelerometerCollision",
"AccelerometerFilter",
"AnalogAccelerometer",
"AnalogEncoder",
"AnalogInput",
@@ -78,6 +81,7 @@ SNIPPETS_FOLDERS = [
"EncoderDrive",
"EncoderHoming",
"LimitSwitch",
"OnboardIMU",
]
TEMPLATES_FOLDERS = [

View File

@@ -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 <frc/ADXL345_I2C.h>
#include <frc/AnalogInput.h>
#include <frc/I2C.h>
#include <frc/TimedRobot.h>
/**
* ADXL346, 362 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() {}
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 ADXL345 accelerometer object on the MXP I2C port
// with a measurement range from -8 to 8 G's
frc::ADXL345_I2C m_accelerometer{frc::I2C::Port::kPort0,
frc::ADXL345_I2C::Range::kRange_8G};
};
#ifndef RUNNING_FRC_TESTS
int main() {
return frc::StartRobot<Robot>();
}
#endif

View File

@@ -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 <frc/OnboardIMU.h>
#include <frc/TimedRobot.h>
#include <frc/smartdashboard/SmartDashboard.h>
#include <units/acceleration.h>
/**
* 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
auto xAccel = m_accelerometer.GetAccelX();
auto yAccel = m_accelerometer.GetAccelY();
// Calculates the jerk in the X and Y directions
auto xJerk = (xAccel - m_prevXAccel) / GetPeriod();
auto yJerk = (yAccel - m_prevYAccel) / GetPeriod();
m_prevXAccel = xAccel;
m_prevYAccel = yAccel;
frc::SmartDashboard::PutNumber("X Jerk", xJerk.value());
frc::SmartDashboard::PutNumber("Y Jerk", yJerk.value());
}
private:
units::meters_per_second_squared_t m_prevXAccel = 0.0_mps_sq;
units::meters_per_second_squared_t m_prevYAccel = 0.0_mps_sq;
frc::OnboardIMU m_accelerometer{frc::OnboardIMU::MountOrientation::kFlat};
};
#ifndef RUNNING_FRC_TESTS
int main() {
return frc::StartRobot<Robot>();
}
#endif

View File

@@ -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 <frc/OnboardIMU.h>
#include <frc/TimedRobot.h>
#include <frc/filter/LinearFilter.h>
#include <frc/smartdashboard/SmartDashboard.h>
#include <units/acceleration.h>
/**
* Accelerometer filtering 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 {
units::meters_per_second_squared_t XAccel = m_accelerometer.GetAccelX();
// Get the filtered X acceleration
units::meters_per_second_squared_t filteredXAccel =
m_xAccelFilter.Calculate(XAccel);
frc::SmartDashboard::PutNumber("X Acceleration", XAccel.value());
frc::SmartDashboard::PutNumber("Filtered X Acceleration",
filteredXAccel.value());
}
private:
frc::OnboardIMU m_accelerometer{frc::OnboardIMU::MountOrientation::kFlat};
frc::LinearFilter<units::meters_per_second_squared_t> m_xAccelFilter =
frc::LinearFilter<units::meters_per_second_squared_t>::MovingAverage(10);
};
#ifndef RUNNING_FRC_TESTS
int main() {
return frc::StartRobot<Robot>();
}
#endif

View File

@@ -0,0 +1,45 @@
// 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 <frc/OnboardIMU.h>
#include <frc/TimedRobot.h>
#include <frc/geometry/Rotation2d.h>
#include <units/acceleration.h>
#include <units/angle.h>
/**
* Onboard IMU 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 TeleopPeriodic() override {
// Gets the current acceleration in the X axis
m_IMU.GetAccelX();
// Gets the current acceleration in the Y axis
m_IMU.GetAccelY();
// Gets the current acceleration in the Z axis
m_IMU.GetAccelZ();
// Gets the current angle in the X axis
m_IMU.GetAngleX();
// Gets the current angle in the Y axis
m_IMU.GetAngleY();
// Gets the current angle in the Z axis
m_IMU.GetAngleZ();
// Gets the current angle as a Rotation2D
m_IMU.GetRotation2d();
}
private:
// Creates an object for the Systemcore IMU
frc::OnboardIMU m_IMU{frc::OnboardIMU::MountOrientation::kFlat};
};
#ifndef RUNNING_FRC_TESTS
int main() {
return frc::StartRobot<Robot>();
}
#endif

View File

@@ -103,5 +103,48 @@
],
"foldername": "AnalogAccelerometer",
"gradlebase": "cpp"
},
{
"name": "ADXLAccelerometers",
"description": "Snippets of ADXL 345 and 362 Accelerometers for frc-docs.",
"tags": [
"Hardware",
"Accelerometer"
],
"foldername": "ADXLAccelerometers",
"gradlebase": "cpp"
},
{
"name": "AccelerometerCollisionDetection",
"description": "Snippets of Accelerometer collision detection for frc-docs.",
"tags": [
"Hardware",
"Accelerometer",
"IMU"
],
"foldername": "AccelerometerCollision",
"gradlebase": "cpp"
},
{
"name": "AccelerometerFilter",
"description": "Snippets of filtering Accelerometer for frc-docs.",
"tags": [
"Hardware",
"Accelerometer",
"IMU"
],
"foldername": "AccelerometerFilter",
"gradlebase": "cpp"
},
{
"name": "OnboardIMU",
"description": "Snippets of OnboardIMU for frc-docs.",
"tags": [
"Hardware",
"Accelerometer",
"IMU"
],
"foldername": "OnboardIMU",
"gradlebase": "cpp"
}
]

View File

@@ -81,7 +81,9 @@ def build_snippets():
name = folder + "-snippet",
srcs = native.glob(["src/main/java/edu/wpi/first/wpilibj/snippets/" + folder + "/**/*.java"]),
deps = [
"//hal:hal-java",
"//wpilibj:wpilibj-java",
"//wpimath:wpimath-java",
],
tags = ["wpi-example"],
)

View File

@@ -67,6 +67,9 @@ COMMANDS_V2_FOLDERS = [
]
SNIPPETS_FOLDERS = [
"accelerometercollision",
"accelerometerfilter",
"adxlaccelerometers",
"analogaccelerometer",
"analogencoder",
"analoginput",
@@ -77,6 +80,7 @@ SNIPPETS_FOLDERS = [
"encoderdrive",
"encoderhoming",
"limitswitch",
"onboardimu",
]
TEMPLATES_FOLDERS = [

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,37 @@
// 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.OnboardIMU;
import edu.wpi.first.wpilibj.OnboardIMU.MountOrientation;
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;
OnboardIMU m_accelerometer = new OnboardIMU(MountOrientation.kFlat);
@Override
public void robotPeriodic() {
// Gets the current accelerations in the X and Y directions
double xAccel = m_accelerometer.getAccelX();
double yAccel = m_accelerometer.getAccelY();
// 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

@@ -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.accelerometerfilter;
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,32 @@
// 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.accelerometerfilter;
import edu.wpi.first.math.filter.LinearFilter;
import edu.wpi.first.wpilibj.OnboardIMU;
import edu.wpi.first.wpilibj.OnboardIMU.MountOrientation;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
/**
* Accelerometer filtering snippets for frc-docs.
* https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/accelerometers-software.html
*/
public class Robot extends TimedRobot {
OnboardIMU m_accelerometer = new OnboardIMU(MountOrientation.kFlat);
// Create a LinearFilter that will calculate a moving average of the measured X acceleration over
// the past 10 iterations of the main loop
LinearFilter m_xAccelFilter = LinearFilter.movingAverage(10);
@Override
public void robotPeriodic() {
double xAccel = m_accelerometer.getAccelX();
// Get the filtered X acceleration
double filteredXAccel = m_xAccelFilter.calculate(xAccel);
SmartDashboard.putNumber("X Acceleration", xAccel);
SmartDashboard.putNumber("Filtered X Acceleration", filteredXAccel);
}
}

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.adxlaccelerometers;
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,32 @@
// 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.adxlaccelerometers;
import edu.wpi.first.wpilibj.ADXL345_I2C;
import edu.wpi.first.wpilibj.I2C;
import edu.wpi.first.wpilibj.TimedRobot;
/**
* ADXL345, 362 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 ADXL345 accelerometer object on the MXP I2C port
// with a measurement range from -8 to 8 G's
ADXL345_I2C m_accelerometer345I2C = new ADXL345_I2C(I2C.Port.kPort0, ADXL345_I2C.Range.k8G);
/** Called once at the beginning of the robot program. */
public Robot() {}
@Override
public void teleopPeriodic() {
// Gets the current acceleration in the X axis
m_accelerometer345I2C.getX();
// Gets the current acceleration in the Y axis
m_accelerometer345I2C.getY();
// Gets the current acceleration in the Z axis
m_accelerometer345I2C.getZ();
}
}

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.onboardimu;
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,41 @@
// 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.onboardimu;
import edu.wpi.first.wpilibj.OnboardIMU;
import edu.wpi.first.wpilibj.OnboardIMU.MountOrientation;
import edu.wpi.first.wpilibj.TimedRobot;
/**
* on board IMU 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 on board IMU
OnboardIMU m_iMU = new OnboardIMU(MountOrientation.kFlat);
/** Called once at the beginning of the robot program. */
public Robot() {}
@Override
public void teleopPeriodic() {
// Gets the current acceleration in the X axis
m_iMU.getAccelX();
// Gets the current acceleration in the Y axis
m_iMU.getAccelY();
// Gets the current acceleration in the Z axis
m_iMU.getAccelZ();
// Gets the current angle in the X axis
m_iMU.getAngleX();
// Gets the current angle in the Y axis
m_iMU.getAngleY();
// Gets the current angle in the Z axis
m_iMU.getAngleZ();
// Gets the current angle as a Rotation2D
m_iMU.getRotation2d();
}
}

View File

@@ -113,5 +113,52 @@
"foldername": "analogaccelerometer",
"gradlebase": "java",
"mainclass": "Main"
},
{
"name": "AccelerometerCollisionDetection",
"description": "Snippets of Accelerometer for collision detection for frc-docs.",
"tags": [
"Hardware",
"Accelerometer",
"IMU"
],
"foldername": "accelerometercollision",
"gradlebase": "java",
"mainclass": "Main"
},
{
"name": "AccelerometerFilter",
"description": "Snippets of filtering Accelerometer for frc-docs.",
"tags": [
"Hardware",
"Accelerometer",
"IMU"
],
"foldername": "accelerometerfilter",
"gradlebase": "java",
"mainclass": "Main"
},
{
"name": "ADXLAccelerometers",
"description": "Snippets of ADXL 345 and 362 accelerometers for frc-docs.",
"tags": [
"Hardware",
"Accelerometer"
],
"foldername": "adxlaccelerometers",
"gradlebase": "java",
"mainclass": "Main"
},
{
"name": "OnboardIMU",
"description": "Snippets of Onboard IMU for frc-docs.",
"tags": [
"Hardware",
"Accelerometer",
"IMU"
],
"foldername": "onboardimu",
"gradlebase": "java",
"mainclass": "Main"
}
]