Files
YAGSL/swervelib/imu/NavXSwerve.java

97 lines
2.1 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.kauailabs.navx.frc.AHRS;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.SerialPort;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
/**
* 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
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.
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)
{
yprArray[0] = (gyro.getYaw() % 360) - yawOffset;
yprArray[1] = gyro.getPitch() % 360;
yprArray[2] = gyro.getRoll() % 360;
2023-02-13 14:37:05 -06:00
}
/**
* Get the instantiated IMU object.
*
* @return IMU object.
*/
@Override
public Object getIMU()
{
return gyro;
}
}