-
Notifications
You must be signed in to change notification settings - Fork 0
feat(hardware/gyro): implement gyroscopes #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 9 commits
626bfd6
a699ec7
f2445de
1b9402f
045bb2c
d180bee
4f91bc7
a49c171
2e5b57d
3881104
e26558e
2b1123a
a95c639
a39601e
77d2f72
fe3c8b6
6556a2a
8dd30a7
935a74f
426ea9c
d4743f9
0837d8d
73d6fd0
f48f6c7
648adab
28dfa6c
5722c6b
213fecf
898776e
64eb036
9ba4834
7108bbc
336dd89
fa12c52
9669e48
ed1a44c
4e72ffa
7ae15eb
e6b51db
2de1188
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| package net.frc5183.librobot.hardware.gyro.imu; | ||
|
|
||
| import edu.wpi.first.math.geometry.Rotation3d; | ||
| import edu.wpi.first.math.geometry.Translation3d; | ||
| import edu.wpi.first.wpilibj.ADIS16448_IMU; | ||
| import edu.wpi.first.wpilibj.SPI; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.util.Optional; | ||
|
Check warning on line 9 in src/main/java/net/frc5183/librobot/hardware/gyro/imu/ADIS16448IMU.java
|
||
Baconing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Represents an ADIS16448 {@link IMU}. | ||
| */ | ||
| public class ADIS16448IMU extends IMU { | ||
| /** | ||
| * The ADIS16448 IMU. | ||
| */ | ||
| @NotNull | ||
| private final ADIS16448_IMU imu; | ||
|
|
||
| /** | ||
| * The yaw axis. | ||
| */ | ||
| @NotNull | ||
| private final IMUAxis yaw; | ||
Baconing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * The pitch axis. | ||
| */ | ||
| @NotNull | ||
| private final IMUAxis pitch; | ||
Baconing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * The roll axis. | ||
| */ | ||
| private final IMUAxis roll; | ||
|
Check warning on line 36 in src/main/java/net/frc5183/librobot/hardware/gyro/imu/ADIS16448IMU.java
|
||
Baconing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Baconing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Creates a new {@link ADIS16448IMU} using the RIO's Onboard MXP port, 500ms calibration time, and Z axis as yaw. | ||
| * @see ADIS16448_IMU#ADIS16448_IMU() | ||
| */ | ||
| public ADIS16448IMU() { | ||
| this(new ADIS16448_IMU()); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new {@link ADIS16448IMU} from an existing ADIS16448 IMU instance. | ||
| * @param imu the ADIS16448 IMU to use. | ||
| */ | ||
| public ADIS16448IMU(@NotNull ADIS16448_IMU imu) { | ||
| yaw = toIMUAxis(imu.getYawAxis()); | ||
|
|
||
| //todo: confirm this is correct i had copilot do it | ||
| if (yaw == IMUAxis.X) { | ||
| pitch = IMUAxis.Y; | ||
| roll = IMUAxis.Z; | ||
| } else if (yaw == IMUAxis.Y) { | ||
| pitch = IMUAxis.X; | ||
| roll = IMUAxis.Z; | ||
| } else { | ||
| pitch = IMUAxis.X; | ||
| roll = IMUAxis.Y; | ||
| } | ||
Baconing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Baconing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| this.imu = imu; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new {@link ADIS16448IMU} with a specified yaw axis. | ||
| * Uses default RIO's Onboard MXP port and 512ms calibration time. | ||
| * @param yaw the YAW axis. | ||
| */ | ||
| public ADIS16448IMU(@NotNull IMUAxis yaw) { | ||
| this(yaw, SPI.Port.kMXP); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new {@link ADIS16448IMU} with a specified yaw axis and SPI port. | ||
| * Uses default 512ms calibration time. | ||
| * @param yaw the YAW axis. | ||
| * @param port the SPI port. | ||
| */ | ||
| public ADIS16448IMU(@NotNull IMUAxis yaw, @NotNull SPI.Port port) { | ||
| this(yaw, port, ADIS16448_IMU.CalibrationTime._512ms); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new {@link ADIS16448IMU} with a specified yaw axis, SPI port, and calibration time. | ||
| * @param yaw the YAW axis. | ||
| * @param port the SPI port. | ||
| * @param calibrationTime the calibration time. | ||
| */ | ||
| public ADIS16448IMU(@NotNull IMUAxis yaw, @NotNull SPI.Port port, @NotNull ADIS16448_IMU.CalibrationTime calibrationTime) { | ||
| this.yaw = yaw; | ||
|
|
||
| //todo: confirm this is correct i had copilot do it | ||
| if (yaw == IMUAxis.X) { | ||
| pitch = IMUAxis.Y; | ||
| roll = IMUAxis.Z; | ||
| } else if (yaw == IMUAxis.Y) { | ||
| pitch = IMUAxis.X; | ||
| roll = IMUAxis.Z; | ||
| } else { | ||
| pitch = IMUAxis.X; | ||
| roll = IMUAxis.Y; | ||
| } | ||
Baconing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| imu = new ADIS16448_IMU(fromIMUAxis(yaw), port, calibrationTime); | ||
| } | ||
Baconing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Baconing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public @NotNull Rotation3d getRawRotation3d() { | ||
| return new Rotation3d( | ||
| Math.toRadians(imu.getGyroAngleX()), | ||
| Math.toRadians(imu.getGyroAngleY()), | ||
| Math.toRadians(imu.getGyroAngleZ()) | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull Translation3d getAccelerationMetersPerSecondSquared() { | ||
| return new Translation3d(imu.getAccelX(), imu.getAccelY(), imu.getAccelZ()); | ||
| } | ||
Baconing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public void calibrate() { | ||
| imu.calibrate(); | ||
| } | ||
|
|
||
| @Override | ||
| public void reset() { | ||
| imu.reset(); | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull ADIS16448_IMU getRawIMU() { | ||
| return imu; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a new {@link ADIS16448_IMU.IMUAxis} from an {@link IMUAxis}. | ||
| * @param axis the {@link IMUAxis} to convert. | ||
| * @return the converted {@link ADIS16448_IMU.IMUAxis}. | ||
| */ | ||
| @NotNull | ||
| public static ADIS16448_IMU.IMUAxis fromIMUAxis(@NotNull IMUAxis axis) { | ||
| return switch (axis) { | ||
| case X -> ADIS16448_IMU.IMUAxis.kX; | ||
| case Y -> ADIS16448_IMU.IMUAxis.kY; | ||
| case Z -> ADIS16448_IMU.IMUAxis.kZ; | ||
| }; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Returns a new {@link IMUAxis} from an {@link ADIS16448_IMU.IMUAxis}. | ||
| * @param axis the {@link ADIS16448_IMU.IMUAxis} to convert. | ||
| * @return the converted {@link IMUAxis}. | ||
| */ | ||
| @NotNull | ||
| public static IMUAxis toIMUAxis(@NotNull ADIS16448_IMU.IMUAxis axis) { | ||
| return switch (axis) { | ||
| case kX -> IMUAxis.X; | ||
| case kY -> IMUAxis.Y; | ||
| case kZ -> IMUAxis.Z; | ||
| }; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| package net.frc5183.librobot.hardware.gyro.imu; | ||
|
|
||
| import edu.wpi.first.math.geometry.Rotation3d; | ||
| import edu.wpi.first.math.geometry.Translation3d; | ||
| import edu.wpi.first.wpilibj.ADIS16470_IMU; | ||
| import edu.wpi.first.wpilibj.SPI; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| /** | ||
| * Represents an ADIS16470 {@link IMU}. | ||
| */ | ||
| public class ADIS16470IMU extends IMU { | ||
| /** | ||
| * The ADIS16470 IMU. | ||
| */ | ||
| @NotNull | ||
| private final ADIS16470_IMU imu; | ||
|
|
||
| /** | ||
| * The YAW axis. | ||
| */ | ||
| private final IMUAxis yaw; | ||
|
Check warning on line 22 in src/main/java/net/frc5183/librobot/hardware/gyro/imu/ADIS16470IMU.java
|
||
|
|
||
| /** | ||
| * The PITCH axis. | ||
| */ | ||
| private final IMUAxis pitch; | ||
|
Check warning on line 27 in src/main/java/net/frc5183/librobot/hardware/gyro/imu/ADIS16470IMU.java
|
||
|
|
||
| /** | ||
| * The ROLL axis. | ||
| */ | ||
| private final IMUAxis roll; | ||
|
Check warning on line 32 in src/main/java/net/frc5183/librobot/hardware/gyro/imu/ADIS16470IMU.java
|
||
Baconing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Creates a new {@link ADIS16470IMU} using the RIO's Onboard MXP port and 500ms calibration time, and yaw pitch roll as ZXY respectively. | ||
| * @see ADIS16470_IMU#ADIS16470_IMU() | ||
| */ | ||
| public ADIS16470IMU() { | ||
| this(new ADIS16470_IMU()); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new {@link ADIS16470IMU} from an existing ADIS16470 IMU instance. | ||
| * @param imu the ADIS16470 IMU to use. | ||
| */ | ||
| public ADIS16470IMU(@NotNull ADIS16470_IMU imu) { | ||
| this.imu = imu; | ||
|
|
||
| yaw = toIMUAxis(imu.getYawAxis()); | ||
| pitch = toIMUAxis(imu.getPitchAxis()); | ||
| roll = toIMUAxis(imu.getRollAxis()); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new {@link ADIS16470IMU} with a specified yaw, pitch, and roll axis. | ||
| * Uses default RIO's Onboard MXP port and 500ms calibration time. | ||
| * @param yaw the YAW axis. | ||
| * @param pitch the PITCH axis. | ||
| * @param roll the ROLL axis. | ||
| */ | ||
| public ADIS16470IMU(IMUAxis yaw, IMUAxis pitch, IMUAxis roll) { | ||
| this(yaw, pitch, roll, SPI.Port.kMXP); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new {@link ADIS16470IMU} with a specified yaw, pitch, roll axis, and SPI port. | ||
| * Uses default 4s calibration time. | ||
| * @param yaw the YAW axis. | ||
| * @param pitch the PITCH axis. | ||
| * @param roll the ROLL axis. | ||
| * @param port the SPI port. | ||
| */ | ||
| public ADIS16470IMU(IMUAxis yaw, IMUAxis pitch, IMUAxis roll, SPI.Port port) { | ||
| this(yaw, pitch, roll, port, ADIS16470_IMU.CalibrationTime._4s); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new {@link ADIS16470IMU} with a specified yaw, pitch, roll axis, SPI port, and calibration time. | ||
| * @param yaw the YAW axis. | ||
| * @param pitch the PITCH axis. | ||
| * @param roll the ROLL axis. | ||
| * @param port the SPI port. | ||
| * @param calibrationTime the calibration time. | ||
| */ | ||
| public ADIS16470IMU(IMUAxis yaw, IMUAxis pitch, IMUAxis roll, SPI.Port port, ADIS16470_IMU.CalibrationTime calibrationTime) { | ||
| imu = new ADIS16470_IMU(fromIMUAxis(yaw), fromIMUAxis(pitch), fromIMUAxis(roll), port, calibrationTime); | ||
|
|
||
| this.yaw = yaw; | ||
| this.pitch = pitch; | ||
| this.roll = roll; | ||
| } | ||
Baconing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public @NotNull Rotation3d getRawRotation3d() { | ||
| return new Rotation3d( | ||
| Math.toRadians(imu.getAngle(fromIMUAxis(IMUAxis.X))), | ||
| Math.toRadians(imu.getAngle(fromIMUAxis(IMUAxis.Y))), | ||
| Math.toRadians(imu.getAngle(fromIMUAxis(IMUAxis.Z))) | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull Translation3d getAccelerationMetersPerSecondSquared() { | ||
| return new Translation3d(imu.getAccelX(), imu.getAccelY(), imu.getAccelZ()); | ||
| } | ||
|
|
||
| @Override | ||
| public void calibrate() { | ||
| imu.calibrate(); | ||
| } | ||
|
|
||
| @Override | ||
| public void reset() { | ||
| imu.reset(); | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull ADIS16470_IMU getRawIMU() { | ||
| return imu; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a new {@link ADIS16470_IMU.IMUAxis} from an {@link IMUAxis}. | ||
| * @param axis the {@link IMUAxis} to convert. | ||
| * @return the converted {@link ADIS16470_IMU.IMUAxis}. | ||
| */ | ||
| public static ADIS16470_IMU.IMUAxis fromIMUAxis(IMUAxis axis) { | ||
| return switch (axis) { | ||
| case X -> ADIS16470_IMU.IMUAxis.kX; | ||
| case Y -> ADIS16470_IMU.IMUAxis.kY; | ||
| case Z -> ADIS16470_IMU.IMUAxis.kZ; | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a new {@link IMUAxis} from an {@link ADIS16470_IMU.IMUAxis}. | ||
| * @param axis the {@link IMUAxis} to convert. | ||
| * @return the converted {@link ADIS16470_IMU.IMUAxis}. | ||
| */ | ||
| public static IMUAxis toIMUAxis(ADIS16470_IMU.IMUAxis axis) { | ||
| return switch (axis) { | ||
| case kX -> IMUAxis.X; | ||
| case kY -> IMUAxis.Y; | ||
| case kZ -> IMUAxis.Z; | ||
| default -> throw new IllegalArgumentException("IMU axis must be one of " + axis); | ||
| }; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not just remove single axis gyroscopes. They can be useful for tracking rotating parts like arms or grabbers. In addition, you could "get" a single axis gyroscope from IMUs to allow more adaptability.