mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[examples] Add Snippets for Accelerometers using OnboardIMU (#8087)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user