2023-02-15 22:18:27 -06:00
|
|
|
package swervelib.imu;
|
|
|
|
|
|
2023-03-06 20:45:54 -06:00
|
|
|
import edu.wpi.first.math.geometry.Rotation3d;
|
|
|
|
|
import edu.wpi.first.math.geometry.Translation3d;
|
2023-02-15 22:18:27 -06:00
|
|
|
import edu.wpi.first.wpilibj.ADIS16448_IMU;
|
|
|
|
|
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
|
2023-03-06 20:45:54 -06:00
|
|
|
import java.util.Optional;
|
2023-02-15 22:18:27 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* IMU Swerve class for the {@link ADIS16448_IMU} device.
|
|
|
|
|
*/
|
|
|
|
|
public class ADIS16448Swerve extends SwerveIMU
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@link ADIS16448_IMU} device to read the current headings from.
|
|
|
|
|
*/
|
|
|
|
|
private final ADIS16448_IMU imu;
|
|
|
|
|
/**
|
2023-03-08 23:34:33 -06:00
|
|
|
* Offset for the ADIS16448.
|
2023-02-15 22:18:27 -06:00
|
|
|
*/
|
2023-03-08 23:34:33 -06:00
|
|
|
private Rotation3d offset = new Rotation3d();
|
2023-02-15 22:18:27 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct the ADIS16448 imu and reset default configurations. Publish the gyro to the SmartDashboard.
|
|
|
|
|
*/
|
|
|
|
|
public ADIS16448Swerve()
|
|
|
|
|
{
|
|
|
|
|
imu = new ADIS16448_IMU();
|
|
|
|
|
factoryDefault();
|
|
|
|
|
SmartDashboard.putData(imu);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reset IMU to factory default.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void factoryDefault()
|
|
|
|
|
{
|
2023-03-08 23:34:33 -06:00
|
|
|
offset = new Rotation3d(
|
|
|
|
|
Math.toRadians(imu.getYComplementaryAngle()), Math.toRadians(imu.getXComplementaryAngle()),
|
|
|
|
|
Math.toRadians(imu.getAngle()));
|
2023-02-15 22:18:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear sticky faults on IMU.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void clearStickyFaults()
|
|
|
|
|
{
|
|
|
|
|
// Do nothing.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-03-08 23:34:33 -06:00
|
|
|
* Set the gyro offset.
|
2023-02-15 22:18:27 -06:00
|
|
|
*
|
2023-03-08 23:34:33 -06:00
|
|
|
* @param offset gyro offset as a {@link Rotation3d}.
|
2023-02-15 22:18:27 -06:00
|
|
|
*/
|
2023-03-08 23:34:33 -06:00
|
|
|
public void setOffset(Rotation3d offset)
|
2023-02-15 22:18:27 -06:00
|
|
|
{
|
2023-03-08 23:34:33 -06:00
|
|
|
offset = getRotation3d();
|
2023-02-15 22:18:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-03-08 23:34:33 -06:00
|
|
|
* Fetch the {@link Rotation3d} from the IMU without any zeroing. Robot relative.
|
2023-02-15 22:18:27 -06:00
|
|
|
*
|
2023-03-08 23:34:33 -06:00
|
|
|
* @return {@link Rotation3d} from the IMU.
|
2023-02-15 22:18:27 -06:00
|
|
|
*/
|
2023-03-08 23:34:33 -06:00
|
|
|
public Rotation3d getRawRotation3d()
|
2023-02-15 22:18:27 -06:00
|
|
|
{
|
2023-03-08 23:34:33 -06:00
|
|
|
return new Rotation3d(
|
|
|
|
|
Math.toRadians(imu.getYComplementaryAngle()), Math.toRadians(imu.getXComplementaryAngle()),
|
|
|
|
|
Math.toRadians(imu.getAngle()));
|
2023-02-15 22:18:27 -06:00
|
|
|
}
|
|
|
|
|
|
2023-03-06 20:45:54 -06:00
|
|
|
/**
|
|
|
|
|
* Fetch the {@link Rotation3d} from the IMU. Robot relative.
|
|
|
|
|
*
|
|
|
|
|
* @return {@link Rotation3d} from the IMU.
|
|
|
|
|
*/
|
2023-03-08 23:34:33 -06:00
|
|
|
@Override
|
2023-03-06 20:45:54 -06:00
|
|
|
public Rotation3d getRotation3d()
|
|
|
|
|
{
|
2023-03-08 23:34:33 -06:00
|
|
|
return getRawRotation3d().minus(offset);
|
2023-03-06 20:45:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Optional<Translation3d> getAccel()
|
|
|
|
|
{
|
|
|
|
|
return Optional.of(new Translation3d(imu.getAccelX(), imu.getAccelY(), imu.getAccelZ()));
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 22:18:27 -06:00
|
|
|
/**
|
|
|
|
|
* Get the instantiated IMU object.
|
|
|
|
|
*
|
|
|
|
|
* @return IMU object.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object getIMU()
|
|
|
|
|
{
|
|
|
|
|
return imu;
|
|
|
|
|
}
|
|
|
|
|
}
|