Files
YAGSL/swervelib/imu/PigeonSwerve.java

139 lines
3.0 KiB
Java
Raw Normal View History

2023-02-13 17:21:24 -06:00
package swervelib.imu;
2023-02-13 14:37:05 -06:00
import com.ctre.phoenix.sensors.WPI_PigeonIMU;
import edu.wpi.first.math.geometry.Quaternion;
import edu.wpi.first.math.geometry.Rotation3d;
import edu.wpi.first.math.geometry.Translation3d;
2023-02-13 14:37:05 -06:00
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import java.util.Optional;
2023-02-13 14:37:05 -06:00
/**
* SwerveIMU interface for the Pigeon.
*/
public class PigeonSwerve extends SwerveIMU
{
/**
* Pigeon v1 IMU device.
*/
2023-02-13 14:37:05 -06:00
WPI_PigeonIMU imu;
2023-03-08 23:34:33 -06:00
/**
* Offset for the Pigeon.
*/
2024-01-22 15:11:18 -06:00
private Rotation3d offset = new Rotation3d();
/**
* Inversion for the gyro
*/
private boolean invertedIMU = false;
2023-02-13 14:37:05 -06:00
/**
* Generate the SwerveIMU for pigeon.
*
* @param canid CAN ID for the pigeon, does not support CANBus.
*/
public PigeonSwerve(int canid)
{
imu = new WPI_PigeonIMU(canid);
2023-03-08 23:34:33 -06:00
offset = new Rotation3d();
2023-02-13 14:37:05 -06:00
SmartDashboard.putData(imu);
}
/**
* Reset IMU to factory default.
*/
@Override
public void factoryDefault()
{
imu.configFactoryDefault();
}
/**
* Clear sticky faults on IMU.
*/
@Override
public void clearStickyFaults()
{
imu.clearStickyFaults();
}
/**
2023-03-08 23:34:33 -06:00
* Set the gyro offset.
2023-02-13 14:37:05 -06:00
*
2023-03-08 23:34:33 -06:00
* @param offset gyro offset as a {@link Rotation3d}.
2023-02-13 14:37:05 -06:00
*/
2023-03-08 23:34:33 -06:00
public void setOffset(Rotation3d offset)
2023-02-13 14:37:05 -06:00
{
this.offset = offset;
2023-02-13 14:37:05 -06:00
}
2024-01-22 15:11:18 -06:00
/**
* Set the gyro to invert its default direction
*
* @param invertIMU invert gyro direction
*/
public void setInverted(boolean invertIMU)
{
invertedIMU = invertIMU;
}
2023-02-13 14:37:05 -06:00
/**
2023-03-08 23:34:33 -06:00
* Fetch the {@link Rotation3d} from the IMU without any zeroing. Robot relative.
2023-02-13 14:37:05 -06:00
*
2023-03-08 23:34:33 -06:00
* @return {@link Rotation3d} from the IMU.
2023-02-13 14:37:05 -06:00
*/
@Override
2023-03-08 23:34:33 -06:00
public Rotation3d getRawRotation3d()
2023-02-13 14:37:05 -06:00
{
2023-03-08 23:34:33 -06:00
double[] wxyz = new double[4];
imu.get6dQuaternion(wxyz);
2024-01-22 15:11:18 -06:00
Rotation3d reading = new Rotation3d(new Quaternion(wxyz[0], wxyz[1], wxyz[2], wxyz[3]));
return invertedIMU ? reading.unaryMinus() : reading;
2023-02-13 14:37:05 -06:00
}
/**
* Fetch the {@link Rotation3d} from the IMU. Robot relative.
*
* @return {@link Rotation3d} from the IMU.
*/
@Override
public Rotation3d getRotation3d()
{
2023-03-08 23:34:33 -06:00
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()
{
short[] initial = new short[3];
imu.getBiasedAccelerometer(initial);
return Optional.of(new Translation3d(initial[0], initial[1], initial[2]).times(9.81 / 16384.0));
}
2024-08-24 17:40:19 -05:00
/**
2024-08-24 17:27:03 -05:00
* Fetch the rotation rate from the IMU in degrees per second. If rotation rate isn't supported returns empty.
2024-08-24 17:40:19 -05:00
*
2024-08-24 17:27:03 -05:00
* @return {@link Double} of the rotation rate as an {@link Optional}.
*/
2024-08-24 17:40:19 -05:00
public double getRate()
{
2024-08-24 17:27:03 -05:00
return imu.getRate();
}
2023-02-13 14:37:05 -06:00
/**
* Get the instantiated IMU object.
*
* @return IMU object.
*/
@Override
public Object getIMU()
{
return imu;
}
}