mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[examples] Add Accelerometer filtering snippets
This commit is contained in:
committed by
Peter Johnson
parent
0877d130be
commit
79a9d7f987
@@ -0,0 +1,35 @@
|
||||
// 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/BuiltInAccelerometer.h>
|
||||
#include <frc/TimedRobot.h>
|
||||
#include <frc/filter/LinearFilter.h>
|
||||
#include <frc/smartdashboard/SmartDashboard.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 {
|
||||
double XAccel = m_accelerometer.GetX();
|
||||
// Get the filtered X acceleration
|
||||
double filteredXAccel = m_xAccelFilter.Calculate(XAccel);
|
||||
|
||||
frc::SmartDashboard::PutNumber("X Acceleration", XAccel);
|
||||
frc::SmartDashboard::PutNumber("Filtered X Acceleration", filteredXAccel);
|
||||
}
|
||||
|
||||
private:
|
||||
frc::BuiltInAccelerometer m_accelerometer;
|
||||
frc::LinearFilter<double> m_xAccelFilter =
|
||||
frc::LinearFilter<double>::MovingAverage(10);
|
||||
};
|
||||
|
||||
#ifndef RUNNING_FRC_TESTS
|
||||
int main() {
|
||||
return frc::StartRobot<Robot>();
|
||||
}
|
||||
#endif
|
||||
@@ -144,5 +144,15 @@
|
||||
],
|
||||
"foldername": "AccelerometerCollision",
|
||||
"gradlebase": "cpp"
|
||||
},
|
||||
{
|
||||
"name": "AccelerometerFilter",
|
||||
"description": "Snippets of filterning Accelerometer for frc-docs.",
|
||||
"tags": [
|
||||
"Hardware",
|
||||
"Accelerometer"
|
||||
],
|
||||
"foldername": "AccelerometerFilter",
|
||||
"gradlebase": "cpp"
|
||||
}
|
||||
]
|
||||
|
||||
@@ -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,31 @@
|
||||
// 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.BuiltInAccelerometer;
|
||||
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 {
|
||||
BuiltInAccelerometer m_accelerometer = new BuiltInAccelerometer();
|
||||
// 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.getX();
|
||||
// Get the filtered X acceleration
|
||||
double filteredXAccel = m_xAccelFilter.calculate(xAccel);
|
||||
|
||||
SmartDashboard.putNumber("X Acceleration", xAccel);
|
||||
SmartDashboard.putNumber("Filtered X Acceleration", filteredXAccel);
|
||||
}
|
||||
}
|
||||
@@ -158,5 +158,16 @@
|
||||
"foldername": "accelerometercollision",
|
||||
"gradlebase": "java",
|
||||
"mainclass": "Main"
|
||||
},
|
||||
{
|
||||
"name": "AccelerometerFilter",
|
||||
"description": "Snippets of filtering Accelerometer for frc-docs.",
|
||||
"tags": [
|
||||
"Hardware",
|
||||
"Accelerometer"
|
||||
],
|
||||
"foldername": "accelerometerfilter",
|
||||
"gradlebase": "java",
|
||||
"mainclass": "Main"
|
||||
}
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user