2023-02-13 17:21:24 -06:00
|
|
|
package swervelib.imu;
|
2023-02-13 14:37:05 -06:00
|
|
|
|
|
|
|
|
import com.kauailabs.navx.frc.AHRS;
|
2023-03-06 20:45:54 -06:00
|
|
|
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.DriverStation;
|
|
|
|
|
import edu.wpi.first.wpilibj.SerialPort;
|
|
|
|
|
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
|
2023-03-06 20:45:54 -06:00
|
|
|
import java.util.Optional;
|
2023-02-13 14:37:05 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Communicates with the NavX as the IMU.
|
|
|
|
|
*/
|
|
|
|
|
public class NavXSwerve extends SwerveIMU
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* NavX IMU.
|
|
|
|
|
*/
|
|
|
|
|
private AHRS gyro;
|
|
|
|
|
/**
|
|
|
|
|
* Offset for the NavX yaw reading.
|
|
|
|
|
*/
|
|
|
|
|
private double yawOffset = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor for the NavX swerve.
|
2023-02-22 23:10:57 -06:00
|
|
|
*
|
|
|
|
|
* @param port Serial Port to connect to.
|
2023-02-13 14:37:05 -06:00
|
|
|
*/
|
2023-02-22 23:10:57 -06:00
|
|
|
public NavXSwerve(SerialPort.Port port)
|
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. */
|
2023-02-22 23:10:57 -06:00
|
|
|
gyro = new AHRS(port);
|
2023-02-13 14:37:05 -06:00
|
|
|
SmartDashboard.putData(gyro);
|
|
|
|
|
} catch (RuntimeException ex)
|
|
|
|
|
{
|
|
|
|
|
DriverStation.reportError("Error instantiating navX-MXP: " + ex.getMessage(), true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reset IMU to factory default.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void factoryDefault()
|
|
|
|
|
{
|
|
|
|
|
// gyro.reset(); // Reported to be slow
|
2023-02-27 21:59:12 -06:00
|
|
|
yawOffset = gyro.getYaw() % 360;
|
2023-02-13 14:37:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear sticky faults on IMU.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void clearStickyFaults()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the yaw in degrees.
|
|
|
|
|
*
|
|
|
|
|
* @param yaw Yaw angle in degrees.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void setYaw(double yaw)
|
|
|
|
|
{
|
|
|
|
|
// gyro.reset(); // Reported to be slow using the offset.
|
2023-02-27 21:59:12 -06:00
|
|
|
yawOffset = (yaw % 360) + (gyro.getYaw() % 360);
|
2023-02-13 14:37:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch the yaw/pitch/roll from the IMU.
|
|
|
|
|
*
|
|
|
|
|
* @param yprArray Array which will be filled with {yaw, pitch, roll} in degrees.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void getYawPitchRoll(double[] yprArray)
|
|
|
|
|
{
|
2023-02-24 19:11:05 -06:00
|
|
|
|
2023-02-27 21:59:12 -06:00
|
|
|
yprArray[0] = (gyro.getYaw() % 360) - yawOffset;
|
|
|
|
|
yprArray[1] = (gyro.getPitch() % 360);
|
|
|
|
|
yprArray[2] = (gyro.getRoll() % 360);
|
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.
|
|
|
|
|
*/
|
|
|
|
|
public Rotation3d getRotation3d()
|
|
|
|
|
{
|
|
|
|
|
return new Rotation3d(new Quaternion(gyro.getQuaternionW(),
|
|
|
|
|
gyro.getQuaternionX(),
|
|
|
|
|
gyro.getQuaternionY(),
|
|
|
|
|
gyro.getQuaternionZ()))
|
|
|
|
|
.minus(new Rotation3d(0, 0, Math.toRadians(yawOffset)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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(
|
|
|
|
|
gyro.getWorldLinearAccelX(),
|
|
|
|
|
gyro.getWorldLinearAccelY(),
|
|
|
|
|
gyro.getWorldLinearAccelZ())
|
|
|
|
|
.times(9.81));
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 14:37:05 -06:00
|
|
|
/**
|
|
|
|
|
* Get the instantiated IMU object.
|
|
|
|
|
*
|
|
|
|
|
* @return IMU object.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object getIMU()
|
|
|
|
|
{
|
|
|
|
|
return gyro;
|
|
|
|
|
}
|
|
|
|
|
}
|