[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

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