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;
|
2023-09-01 00:21:16 -05:00
|
|
|
import edu.wpi.first.wpilibj.I2C;
|
|
|
|
|
import edu.wpi.first.wpilibj.SPI;
|
2023-02-13 14:37:05 -06:00
|
|
|
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.
|
|
|
|
|
*/
|
2023-03-08 23:34:33 -06:00
|
|
|
private AHRS gyro;
|
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
|
|
|
*/
|
2023-03-08 23:34:33 -06:00
|
|
|
private Rotation3d offset = new Rotation3d();
|
2023-02-13 14:37:05 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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-03-08 23:34:33 -06:00
|
|
|
factoryDefault();
|
2023-02-13 14:37:05 -06:00
|
|
|
SmartDashboard.putData(gyro);
|
|
|
|
|
} catch (RuntimeException ex)
|
|
|
|
|
{
|
2023-09-01 00:21:16 -05:00
|
|
|
DriverStation.reportError("Error instantiating navX: " + ex.getMessage(), true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor for the NavX swerve.
|
|
|
|
|
*
|
|
|
|
|
* @param port SPI Port to connect to.
|
|
|
|
|
*/
|
|
|
|
|
public NavXSwerve(SPI.Port port)
|
|
|
|
|
{
|
|
|
|
|
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. */
|
|
|
|
|
gyro = new AHRS(port);
|
|
|
|
|
factoryDefault();
|
|
|
|
|
SmartDashboard.putData(gyro);
|
|
|
|
|
} catch (RuntimeException ex)
|
|
|
|
|
{
|
|
|
|
|
DriverStation.reportError("Error instantiating navX: " + ex.getMessage(), true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor for the NavX swerve.
|
|
|
|
|
*
|
|
|
|
|
* @param port I2C Port to connect to.
|
|
|
|
|
*/
|
|
|
|
|
public NavXSwerve(I2C.Port port)
|
|
|
|
|
{
|
|
|
|
|
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. */
|
|
|
|
|
gyro = new AHRS(port);
|
|
|
|
|
factoryDefault();
|
|
|
|
|
SmartDashboard.putData(gyro);
|
|
|
|
|
} catch (RuntimeException ex)
|
|
|
|
|
{
|
|
|
|
|
DriverStation.reportError("Error instantiating navX: " + ex.getMessage(), true);
|
2023-02-13 14:37:05 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reset IMU to factory default.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void factoryDefault()
|
|
|
|
|
{
|
|
|
|
|
// gyro.reset(); // Reported to be slow
|
2023-03-08 23:34:33 -06:00
|
|
|
offset = new Rotation3d(new Quaternion(gyro.getQuaternionW(),
|
|
|
|
|
gyro.getQuaternionX(),
|
|
|
|
|
gyro.getQuaternionY(),
|
|
|
|
|
gyro.getQuaternionZ()));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*/
|
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
|
|
|
return new Rotation3d(new Quaternion(gyro.getQuaternionW(),
|
|
|
|
|
gyro.getQuaternionX(),
|
|
|
|
|
gyro.getQuaternionY(),
|
|
|
|
|
gyro.getQuaternionZ()));
|
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()
|
|
|
|
|
{
|
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 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;
|
|
|
|
|
}
|
|
|
|
|
}
|