mirror of
https://github.com/BroncBotz3481/YAGSL
synced 2026-06-19 06:21:40 +00:00
136 lines
3.0 KiB
Java
136 lines
3.0 KiB
Java
package swervelib.imu;
|
|
|
|
import static edu.wpi.first.units.Units.DegreesPerSecond;
|
|
|
|
import edu.wpi.first.math.geometry.Rotation3d;
|
|
import edu.wpi.first.math.geometry.Translation3d;
|
|
import edu.wpi.first.units.measure.AngularVelocity;
|
|
import edu.wpi.first.units.measure.MutAngularVelocity;
|
|
import edu.wpi.first.wpilibj.ADXRS450_Gyro;
|
|
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
|
|
import java.util.Optional;
|
|
|
|
/**
|
|
* IMU Swerve class for the {@link ADXRS450_Gyro} device.
|
|
*/
|
|
public class ADXRS450Swerve extends SwerveIMU
|
|
{
|
|
|
|
/**
|
|
* {@link ADXRS450_Gyro} device to read the current headings from.
|
|
*/
|
|
private final ADXRS450_Gyro imu;
|
|
/**
|
|
* Mutable {@link AngularVelocity} for readings.
|
|
*/
|
|
private final MutAngularVelocity yawVel = new MutAngularVelocity(0, 0, DegreesPerSecond);
|
|
/**
|
|
* Offset for the ADXRS450.
|
|
*/
|
|
private Rotation3d offset = new Rotation3d();
|
|
/**
|
|
* Inversion for the gyro
|
|
*/
|
|
private boolean invertedIMU = false;
|
|
|
|
/**
|
|
* Construct the ADXRS450 imu and reset default configurations. Publish the gyro to the SmartDashboard.
|
|
*/
|
|
public ADXRS450Swerve()
|
|
{
|
|
imu = new ADXRS450_Gyro();
|
|
factoryDefault();
|
|
SmartDashboard.putData(imu);
|
|
}
|
|
|
|
/**
|
|
* Reset IMU to factory default.
|
|
*/
|
|
@Override
|
|
public void factoryDefault()
|
|
{
|
|
imu.calibrate();
|
|
offset = new Rotation3d(0, 0, 0);//Math.toRadians(-imu.getAngle()));
|
|
}
|
|
|
|
/**
|
|
* Clear sticky faults on IMU.
|
|
*/
|
|
@Override
|
|
public void clearStickyFaults()
|
|
{
|
|
// Do nothing.
|
|
}
|
|
|
|
/**
|
|
* Set the gyro offset.
|
|
*
|
|
* @param offset gyro offset as a {@link Rotation3d}.
|
|
*/
|
|
public void setOffset(Rotation3d offset)
|
|
{
|
|
this.offset = offset;
|
|
}
|
|
|
|
/**
|
|
* Set the gyro to invert its default direction
|
|
*
|
|
* @param invertIMU invert gyro direction
|
|
*/
|
|
public void setInverted(boolean invertIMU)
|
|
{
|
|
invertedIMU = invertIMU;
|
|
}
|
|
|
|
/**
|
|
* Fetch the {@link Rotation3d} from the IMU without any zeroing. Robot relative.
|
|
*
|
|
* @return {@link Rotation3d} from the IMU.
|
|
*/
|
|
public Rotation3d getRawRotation3d()
|
|
{
|
|
Rotation3d reading = new Rotation3d(0, 0, Math.toRadians(-imu.getAngle()));
|
|
return invertedIMU ? reading.unaryMinus() : reading;
|
|
}
|
|
|
|
/**
|
|
* Fetch the {@link Rotation3d} from the IMU. Robot relative.
|
|
*
|
|
* @return {@link Rotation3d} from the IMU.
|
|
*/
|
|
@Override
|
|
public Rotation3d getRotation3d()
|
|
{
|
|
return getRawRotation3d().minus(offset);
|
|
}
|
|
|
|
/**
|
|
* Fetch the acceleration [x, y, z] from the IMU in meters per second squared. If acceleration isn't supported returns
|
|
* empty.
|
|
*
|
|
* @return {@link Translation3d} of the acceleration as an {@link Optional}.
|
|
*/
|
|
@Override
|
|
public Optional<Translation3d> getAccel()
|
|
{
|
|
return Optional.empty();
|
|
}
|
|
|
|
@Override
|
|
public MutAngularVelocity getYawAngularVelocity()
|
|
{
|
|
return yawVel.mut_setMagnitude(imu.getRate());
|
|
}
|
|
|
|
/**
|
|
* Get the instantiated IMU object.
|
|
*
|
|
* @return IMU object.
|
|
*/
|
|
@Override
|
|
public Object getIMU()
|
|
{
|
|
return imu;
|
|
}
|
|
}
|