2023-02-13 17:21:24 -06:00
|
|
|
package swervelib.imu;
|
2023-02-13 14:37:05 -06:00
|
|
|
|
2024-12-17 18:49:55 +00:00
|
|
|
import static edu.wpi.first.units.Units.DegreesPerSecond;
|
|
|
|
|
|
2024-12-09 23:26:04 +00:00
|
|
|
import com.studica.frc.AHRS;
|
|
|
|
|
import com.studica.frc.AHRS.NavXComType;
|
2023-03-06 20:45:54 -06:00
|
|
|
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-12-09 23:26:04 +00:00
|
|
|
import edu.wpi.first.wpilibj.Alert;
|
|
|
|
|
import edu.wpi.first.wpilibj.Alert.AlertType;
|
2023-03-06 20:45:54 -06:00
|
|
|
import java.util.Optional;
|
2023-02-13 14:37:05 -06:00
|
|
|
|
|
|
|
|
/**
|
2024-11-06 00:10:07 +00:00
|
|
|
* Communicates with the NavX({@link AHRS}) as the IMU.
|
2023-02-13 14:37:05 -06:00
|
|
|
*/
|
|
|
|
|
public class NavXSwerve extends SwerveIMU
|
|
|
|
|
{
|
|
|
|
|
|
2024-12-17 18:49:55 +00:00
|
|
|
/**
|
2025-02-22 06:15:56 +00:00
|
|
|
* Mutable {@link MutAngularVelocity} for readings.
|
2024-12-17 18:49:55 +00:00
|
|
|
*/
|
2025-03-19 03:45:47 +00:00
|
|
|
private final MutAngularVelocity yawVel = new MutAngularVelocity(0, 0, DegreesPerSecond);
|
2023-02-13 14:37:05 -06:00
|
|
|
/**
|
|
|
|
|
* NavX IMU.
|
|
|
|
|
*/
|
2024-12-17 18:49:55 +00:00
|
|
|
private AHRS imu;
|
2023-02-13 14:37:05 -06:00
|
|
|
/**
|
2023-03-08 23:34:33 -06:00
|
|
|
* Offset for the NavX.
|
2023-02-13 14:37:05 -06:00
|
|
|
*/
|
2025-03-19 03:45:47 +00:00
|
|
|
private Rotation3d offset = new Rotation3d();
|
2024-01-17 09:17:39 -06:00
|
|
|
/**
|
|
|
|
|
* An {@link Alert} for if there is an error instantiating the NavX.
|
|
|
|
|
*/
|
2024-12-17 18:49:55 +00:00
|
|
|
private Alert navXError;
|
2025-03-19 03:45:47 +00:00
|
|
|
/**
|
|
|
|
|
* Inversion state of the {@link AHRS}.
|
|
|
|
|
*/
|
|
|
|
|
private boolean inverted = false;
|
2023-02-13 14:37:05 -06:00
|
|
|
|
|
|
|
|
/**
|
2024-11-06 00:10:07 +00:00
|
|
|
* Constructor for the NavX({@link AHRS}) swerve.
|
2023-02-22 23:10:57 -06:00
|
|
|
*
|
|
|
|
|
* @param port Serial Port to connect to.
|
2023-02-13 14:37:05 -06:00
|
|
|
*/
|
2024-12-09 23:26:04 +00:00
|
|
|
public NavXSwerve(NavXComType port)
|
2023-02-13 14:37:05 -06:00
|
|
|
{
|
2024-12-09 23:26:04 +00:00
|
|
|
navXError = new Alert("IMU", "Error instantiating NavX.", AlertType.kError);
|
2023-02-13 14:37:05 -06:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
/* Communicate w/navX-MXP via the MXP SPI Bus. */
|
|
|
|
|
/* Alternatively: I2C.Port.kMXP, SerialPort.Port.kMXP or SerialPort.Port.kUSB */
|
|
|
|
|
/* See http://navx-mxp.kauailabs.com/guidance/selecting-an-interface/ for details. */
|
2024-08-24 17:27:03 -05:00
|
|
|
imu = new AHRS(port);
|
2023-03-08 23:34:33 -06:00
|
|
|
factoryDefault();
|
2023-02-13 14:37:05 -06:00
|
|
|
} catch (RuntimeException ex)
|
|
|
|
|
{
|
2024-01-17 09:17:39 -06:00
|
|
|
navXError.setText("Error instantiating NavX: " + ex.getMessage());
|
|
|
|
|
navXError.set(true);
|
2023-09-01 00:21:16 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-22 06:15:56 +00:00
|
|
|
@Override
|
2025-03-19 03:45:47 +00:00
|
|
|
public void close()
|
|
|
|
|
{
|
2025-02-22 06:15:56 +00:00
|
|
|
imu.close();
|
|
|
|
|
}
|
2023-02-13 14:37:05 -06:00
|
|
|
|
|
|
|
|
/**
|
2024-11-06 00:10:07 +00:00
|
|
|
* Reset offset to current gyro reading. Does not call NavX({@link AHRS#reset()}) because it has been reported to be
|
|
|
|
|
* too slow.
|
2023-02-13 14:37:05 -06:00
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void factoryDefault()
|
|
|
|
|
{
|
|
|
|
|
// gyro.reset(); // Reported to be slow
|
2024-08-24 17:27:03 -05:00
|
|
|
offset = imu.getRotation3d();
|
2023-02-13 14:37:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear sticky faults on IMU.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void 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
|
|
|
{
|
2023-03-09 21:48:47 -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)
|
|
|
|
|
{
|
2025-03-19 03:45:47 +00:00
|
|
|
inverted = invertIMU;
|
|
|
|
|
// setOffset(getRawRotation3d());
|
2024-01-22 15:11:18 -06:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
*/
|
2024-01-15 14:37:13 -06:00
|
|
|
@Override
|
2023-03-08 23:34:33 -06:00
|
|
|
public Rotation3d getRawRotation3d()
|
2023-02-13 14:37:05 -06:00
|
|
|
{
|
2025-03-19 03:45:47 +00:00
|
|
|
return inverted ? imu.getRotation3d().unaryMinus() : imu.getRotation3d();
|
2023-02-13 14:37:05 -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()
|
|
|
|
|
{
|
2025-03-19 03:45:47 +00:00
|
|
|
return getRawRotation3d().rotateBy(offset.unaryMinus());
|
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 as an {@link Optional}.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Optional<Translation3d> getAccel()
|
|
|
|
|
{
|
|
|
|
|
return Optional.of(
|
|
|
|
|
new Translation3d(
|
2024-08-24 17:27:03 -05:00
|
|
|
imu.getWorldLinearAccelX(),
|
|
|
|
|
imu.getWorldLinearAccelY(),
|
|
|
|
|
imu.getWorldLinearAccelZ())
|
2023-03-06 20:45:54 -06:00
|
|
|
.times(9.81));
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-17 18:49:55 +00:00
|
|
|
@Override
|
|
|
|
|
public MutAngularVelocity getYawAngularVelocity()
|
2024-08-24 17:40:19 -05:00
|
|
|
{
|
2024-12-17 18:49:55 +00:00
|
|
|
return yawVel.mut_setMagnitude(imu.getRate());
|
2024-08-24 17:27:03 -05:00
|
|
|
}
|
|
|
|
|
|
2023-02-13 14:37:05 -06:00
|
|
|
/**
|
2024-11-06 00:10:07 +00:00
|
|
|
* Get the instantiated NavX({@link AHRS}) IMU object.
|
2023-02-13 14:37:05 -06:00
|
|
|
*
|
|
|
|
|
* @return IMU object.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object getIMU()
|
|
|
|
|
{
|
2024-08-24 17:27:03 -05:00
|
|
|
return imu;
|
2023-02-13 14:37:05 -06:00
|
|
|
}
|
|
|
|
|
}
|