2024-10-20 09:14:32 -05:00
|
|
|
package swervelib.imu;
|
|
|
|
|
|
2024-12-17 18:49:55 +00:00
|
|
|
import static edu.wpi.first.units.Units.RotationsPerSecond;
|
|
|
|
|
|
2024-10-20 09:14:32 -05:00
|
|
|
import com.reduxrobotics.sensors.canandgyro.Canandgyro;
|
|
|
|
|
import edu.wpi.first.math.geometry.Rotation3d;
|
|
|
|
|
import edu.wpi.first.math.geometry.Translation3d;
|
2024-12-17 18:49:55 +00:00
|
|
|
import edu.wpi.first.units.measure.MutAngularVelocity;
|
2024-10-20 09:14:32 -05:00
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
|
|
/**
|
2024-11-06 00:10:07 +00:00
|
|
|
* SwerveIMU interface for the Boron {@link Canandgyro} by Redux Robotics
|
2024-10-20 09:14:32 -05:00
|
|
|
*/
|
|
|
|
|
public class CanandgyroSwerve extends SwerveIMU
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wait time for status frames to show up.
|
|
|
|
|
*/
|
2024-12-17 18:49:55 +00:00
|
|
|
public static double STATUS_TIMEOUT_SECONDS = 0.04;
|
2024-10-20 09:14:32 -05:00
|
|
|
/**
|
2024-11-06 00:10:07 +00:00
|
|
|
* Boron {@link Canandgyro} by Redux Robotics.
|
2024-10-20 09:14:32 -05:00
|
|
|
*/
|
2024-12-17 18:49:55 +00:00
|
|
|
private final Canandgyro imu;
|
|
|
|
|
/**
|
2025-02-22 06:15:56 +00:00
|
|
|
* Mutable {@link MutAngularVelocity} for readings.
|
2024-12-17 18:49:55 +00:00
|
|
|
*/
|
|
|
|
|
private final MutAngularVelocity yawVel = new MutAngularVelocity(0, 0, RotationsPerSecond);
|
2024-10-20 09:14:32 -05:00
|
|
|
/**
|
2024-11-06 00:10:07 +00:00
|
|
|
* Offset for the Boron {@link Canandgyro}.
|
2024-10-20 09:14:32 -05:00
|
|
|
*/
|
2024-12-17 18:49:55 +00:00
|
|
|
private Rotation3d offset = new Rotation3d();
|
2024-10-20 09:14:32 -05:00
|
|
|
/**
|
|
|
|
|
* Inversion for the gyro
|
|
|
|
|
*/
|
2024-12-17 18:49:55 +00:00
|
|
|
private boolean invertedIMU = false;
|
2024-10-20 09:14:32 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate the SwerveIMU for {@link Canandgyro}.
|
|
|
|
|
*
|
2024-11-06 00:10:07 +00:00
|
|
|
* @param canid CAN ID for the Boron {@link Canandgyro}
|
2024-10-20 09:14:32 -05:00
|
|
|
*/
|
|
|
|
|
public CanandgyroSwerve(int canid)
|
|
|
|
|
{
|
|
|
|
|
imu = new Canandgyro(canid);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-22 06:15:56 +00:00
|
|
|
@Override
|
|
|
|
|
public void close()
|
|
|
|
|
{
|
|
|
|
|
imu.close();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-20 09:14:32 -05:00
|
|
|
/**
|
2024-11-06 00:10:07 +00:00
|
|
|
* Reset {@link Canandgyro} to factory default.
|
2024-10-20 09:14:32 -05:00
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void factoryDefault()
|
|
|
|
|
{
|
|
|
|
|
imu.resetFactoryDefaults(STATUS_TIMEOUT_SECONDS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-11-06 00:10:07 +00:00
|
|
|
* Clear sticky faults on {@link Canandgyro}.
|
2024-10-20 09:14:32 -05:00
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void clearStickyFaults()
|
|
|
|
|
{
|
|
|
|
|
imu.clearStickyFaults();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Rotation3d getRawRotation3d()
|
|
|
|
|
{
|
|
|
|
|
Rotation3d reading = imu.getRotation3d();
|
|
|
|
|
return invertedIMU ? reading.unaryMinus() : reading;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch the {@link Rotation3d} from the IMU. Robot relative.
|
|
|
|
|
*
|
|
|
|
|
* @return {@link Rotation3d} from the IMU.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Rotation3d getRotation3d()
|
|
|
|
|
{
|
2025-03-19 03:45:47 +00:00
|
|
|
return getRawRotation3d().rotateBy(offset.unaryMinus());
|
2024-10-20 09:14:32 -05: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 as an {@link Optional}.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Optional<Translation3d> getAccel()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
return Optional.of(new Translation3d(imu.getAccelerationFrame().getValue()).times(9.81 / 16384.0));
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-17 18:49:55 +00:00
|
|
|
@Override
|
|
|
|
|
public MutAngularVelocity getYawAngularVelocity()
|
2024-10-20 09:14:32 -05:00
|
|
|
{
|
2024-12-17 18:49:55 +00:00
|
|
|
return yawVel.mut_setMagnitude(imu.getAngularVelocityYaw());
|
2024-10-20 09:14:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-11-06 00:10:07 +00:00
|
|
|
* Get the instantiated {@link Canandgyro} IMU object.
|
2024-10-20 09:14:32 -05:00
|
|
|
*
|
|
|
|
|
* @return IMU object.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object getIMU()
|
|
|
|
|
{
|
|
|
|
|
return imu;
|
|
|
|
|
}
|
|
|
|
|
}
|