2023-02-21 22:38:14 -06:00
package swervelib.telemetry ;
2024-12-09 23:26:04 +00:00
import edu.wpi.first.math.geometry.Rotation2d ;
import edu.wpi.first.math.kinematics.ChassisSpeeds ;
import edu.wpi.first.math.kinematics.SwerveModuleState ;
import edu.wpi.first.networktables.NetworkTableInstance ;
import edu.wpi.first.networktables.StructArrayPublisher ;
import edu.wpi.first.networktables.StructPublisher ;
import edu.wpi.first.wpilibj.Alert ;
import edu.wpi.first.wpilibj.Alert.AlertType ;
2023-02-24 19:11:05 -06:00
import edu.wpi.first.wpilibj.RobotBase ;
2023-02-21 22:38:14 -06:00
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard ;
/ * *
* Telemetry to describe the { @link swervelib . SwerveDrive } following frc - web - components . ( Which follows AdvantageKit )
* /
public class SwerveDriveTelemetry
{
2024-02-12 18:59:40 -06:00
/ * *
* An { @link Alert } for if the CAN ID is greater than 40 .
* /
2024-12-09 23:26:04 +00:00
public static final Alert canIdWarning = new Alert ( " JSON " ,
" CAN IDs greater than 40 can cause undefined behaviour, please use a CAN ID below 40! " ,
AlertType . kWarning ) ;
2024-02-12 18:59:40 -06:00
/ * *
* An { @link Alert } for if there is an I2C lockup issue on the roboRIO .
* /
2024-12-09 23:26:04 +00:00
public static final Alert i2cLockupWarning = new Alert ( " IMU " ,
" I2C lockup issue detected on roboRIO. Check console for more information. " ,
AlertType . kWarning ) ;
2024-02-12 18:59:40 -06:00
/ * *
* NavX serial comm issue .
* /
2024-12-09 23:26:04 +00:00
public static final Alert serialCommsIssueWarning = new Alert ( " IMU " ,
" Serial comms is interrupted with USB and other serial traffic and causes intermittent connected/disconnection issues. Please consider another protocol or be mindful of this. " ,
AlertType . kWarning ) ;
/ * *
* Measured swerve module states object .
* /
public static SwerveModuleState [ ] measuredStatesObj = new SwerveModuleState [ 4 ] ;
/ * *
* Desired swerve module states object
* /
public static SwerveModuleState [ ] desiredStatesObj = new SwerveModuleState [ 4 ] ;
/ * *
* The maximum achievable angular velocity of the robot . This is used to visualize the angular velocity from the
* chassis speeds properties .
* /
public static ChassisSpeeds measuredChassisSpeedsObj = new ChassisSpeeds ( ) ;
/ * *
* Describes the desired forward , sideways and angular velocity of the robot .
* /
public static ChassisSpeeds desiredChassisSpeedsObj = new ChassisSpeeds ( ) ;
/ * *
* The robot ' s current rotation based on odometry or gyro readings
* /
public static Rotation2d robotRotationObj = new Rotation2d ( ) ;
2023-02-24 19:11:05 -06:00
/ * *
* The current telemetry verbosity level .
* /
2024-12-09 23:26:04 +00:00
public static TelemetryVerbosity verbosity = TelemetryVerbosity . MACHINE ;
2023-02-24 19:11:05 -06:00
/ * *
* State of simulation of the Robot , used to optimize retrieval .
* /
2024-12-09 23:26:04 +00:00
public static boolean isSimulation = RobotBase . isSimulation ( ) ;
2023-02-21 22:38:14 -06:00
/ * *
* The number of swerve modules
* /
2024-12-09 23:26:04 +00:00
public static int moduleCount ;
2023-02-21 22:38:14 -06:00
/ * *
2024-09-03 14:46:24 -05:00
* The Locations of the swerve drive wheels .
2023-02-21 22:38:14 -06:00
* /
2024-12-09 23:26:04 +00:00
public static double [ ] wheelLocations ;
2023-02-21 22:38:14 -06:00
/ * *
* An array of rotation and velocity values describing the measured state of each swerve module
* /
2024-12-09 23:26:04 +00:00
public static double [ ] measuredStates ;
2023-02-21 22:38:14 -06:00
/ * *
* An array of rotation and velocity values describing the desired state of each swerve module
* /
2024-12-09 23:26:04 +00:00
public static double [ ] desiredStates ;
2023-02-21 22:38:14 -06:00
/ * *
* The robot ' s current rotation based on odometry or gyro readings
* /
2024-12-09 23:26:04 +00:00
public static double robotRotation = 0 ;
2023-02-21 22:38:14 -06:00
/ * *
* The maximum achievable speed of the modules , used to adjust the size of the vectors .
* /
2024-12-09 23:26:04 +00:00
public static double maxSpeed ;
2023-02-21 22:38:14 -06:00
/ * *
* The units of the module rotations and robot rotation
* /
2024-12-09 23:26:04 +00:00
public static String rotationUnit = " degrees " ;
2023-02-21 22:38:14 -06:00
/ * *
* The distance between the left and right modules .
* /
2024-12-09 23:26:04 +00:00
public static double sizeLeftRight ;
2023-02-21 22:38:14 -06:00
/ * *
* The distance between the front and back modules .
* /
2024-12-09 23:26:04 +00:00
public static double sizeFrontBack ;
2023-02-21 22:38:14 -06:00
/ * *
* The direction the robot should be facing when the " Robot Rotation " is zero or blank . This option is often useful to
* align with odometry data or match videos . ' up ' , ' right ' , ' down ' or ' left '
* /
2024-12-09 23:26:04 +00:00
public static String forwardDirection = " up " ;
2023-02-21 22:38:14 -06:00
/ * *
* The maximum achievable angular velocity of the robot . This is used to visualize the angular velocity from the
* chassis speeds properties .
* /
2024-12-09 23:26:04 +00:00
public static double maxAngularVelocity ;
2023-02-21 22:38:14 -06:00
/ * *
* The maximum achievable angular velocity of the robot . This is used to visualize the angular velocity from the
* chassis speeds properties .
* /
2024-12-09 23:26:04 +00:00
public static double [ ] measuredChassisSpeeds = new double [ 3 ] ;
2023-02-21 22:38:14 -06:00
/ * *
* Describes the desired forward , sideways and angular velocity of the robot .
* /
2024-12-09 23:26:04 +00:00
public static double [ ] desiredChassisSpeeds = new double [ 3 ] ;
/ * *
* Struct publisher for AdvantageScope swerve widgets .
* /
private static StructArrayPublisher < SwerveModuleState > measuredStatesStruct
= NetworkTableInstance . getDefault ( )
. getStructArrayTopic (
" swerve/advantagescope/currentStates " ,
SwerveModuleState . struct )
. publish ( ) ;
/ * *
* Struct publisher for AdvantageScope swerve widgets .
* /
private static StructArrayPublisher < SwerveModuleState > desiredStatesStruct
= NetworkTableInstance . getDefault ( )
. getStructArrayTopic (
" swerve/advantagescope/desiredStates " ,
SwerveModuleState . struct )
. publish ( ) ;
/ * *
* Measured { @link ChassisSpeeds } for NT4 AdvantageScope swerve widgets .
* /
private static StructPublisher < ChassisSpeeds > measuredChassisSpeedsStruct
= NetworkTableInstance . getDefault ( )
. getStructTopic (
" swerve/advantagescope/measuredChassisSpeeds " ,
ChassisSpeeds . struct )
. publish ( ) ;
/ * *
* Desired { @link ChassisSpeeds } for NT4 AdvantageScope swerve widgets .
* /
private static StructPublisher < ChassisSpeeds > desiredChassisSpeedsStruct
= NetworkTableInstance . getDefault ( )
. getStructTopic (
" swerve/advantagescope/desiredChassisSpeeds " ,
ChassisSpeeds . struct )
. publish ( ) ;
/ * *
* Robot { @link Rotation2d } for AdvantageScope swerve widgets .
* /
private static StructPublisher < Rotation2d > robotRotationStruct
= NetworkTableInstance . getDefault ( )
. getStructTopic (
" swerve/advantagescope/robotRotation " ,
Rotation2d . struct )
. publish ( ) ;
2023-02-21 22:38:14 -06:00
/ * *
* Upload data to smartdashboard
* /
public static void updateData ( )
{
2024-12-09 23:26:04 +00:00
measuredChassisSpeeds [ 0 ] = measuredChassisSpeedsObj . vxMetersPerSecond ;
measuredChassisSpeeds [ 1 ] = measuredChassisSpeedsObj . vxMetersPerSecond ;
measuredChassisSpeeds [ 2 ] = Math . toDegrees ( measuredChassisSpeedsObj . omegaRadiansPerSecond ) ;
desiredChassisSpeeds [ 0 ] = desiredChassisSpeedsObj . vxMetersPerSecond ;
desiredChassisSpeeds [ 1 ] = desiredChassisSpeedsObj . vyMetersPerSecond ;
desiredChassisSpeeds [ 2 ] = Math . toDegrees ( desiredChassisSpeedsObj . omegaRadiansPerSecond ) ;
robotRotation = robotRotationObj . getDegrees ( ) ;
for ( int i = 0 ; i < measuredStatesObj . length ; i + + )
{
SwerveModuleState state = measuredStatesObj [ i ] ;
if ( state ! = null )
{
measuredStates [ i * 2 ] = state . angle . getDegrees ( ) ;
measuredStates [ i * 2 + 1 ] = state . speedMetersPerSecond ;
}
}
for ( int i = 0 ; i < desiredStatesObj . length ; i + + )
{
SwerveModuleState state = desiredStatesObj [ i ] ;
if ( state ! = null )
{
desiredStates [ i * 2 ] = state . angle . getDegrees ( ) ;
desiredStates [ i * 2 + 1 ] = state . speedMetersPerSecond ;
}
}
2023-02-22 20:50:16 -06:00
SmartDashboard . putNumber ( " swerve/moduleCount " , moduleCount ) ;
SmartDashboard . putNumberArray ( " swerve/wheelLocations " , wheelLocations ) ;
SmartDashboard . putNumberArray ( " swerve/measuredStates " , measuredStates ) ;
SmartDashboard . putNumberArray ( " swerve/desiredStates " , desiredStates ) ;
SmartDashboard . putNumber ( " swerve/robotRotation " , robotRotation ) ;
SmartDashboard . putNumber ( " swerve/maxSpeed " , maxSpeed ) ;
SmartDashboard . putString ( " swerve/rotationUnit " , rotationUnit ) ;
SmartDashboard . putNumber ( " swerve/sizeLeftRight " , sizeLeftRight ) ;
SmartDashboard . putNumber ( " swerve/sizeFrontBack " , sizeFrontBack ) ;
SmartDashboard . putString ( " swerve/forwardDirection " , forwardDirection ) ;
SmartDashboard . putNumber ( " swerve/maxAngularVelocity " , maxAngularVelocity ) ;
SmartDashboard . putNumberArray ( " swerve/measuredChassisSpeeds " , measuredChassisSpeeds ) ;
SmartDashboard . putNumberArray ( " swerve/desiredChassisSpeeds " , desiredChassisSpeeds ) ;
2024-12-09 23:26:04 +00:00
desiredStatesStruct . set ( desiredStatesObj ) ;
measuredStatesStruct . set ( measuredStatesObj ) ;
desiredChassisSpeedsStruct . set ( desiredChassisSpeedsObj ) ;
measuredChassisSpeedsStruct . set ( measuredChassisSpeedsObj ) ;
robotRotationStruct . set ( robotRotationObj ) ;
2023-02-21 22:38:14 -06:00
}
2023-02-24 19:11:05 -06:00
/ * *
* Verbosity of telemetry data sent back .
* /
public enum TelemetryVerbosity
{
/ * *
* No telemetry data is sent back .
* /
NONE ,
/ * *
* Low telemetry data , only post the robot position on the field .
* /
LOW ,
2024-06-12 15:10:29 -05:00
/ * *
* Medium telemetry data , swerve directory
* /
INFO ,
/ * *
* Info level + field info
* /
POSE ,
2023-02-24 19:11:05 -06:00
/ * *
* Full swerve drive data is sent back in both human and machine readable forms .
* /
HIGH ,
/ * *
* Only send the machine readable data related to swerve drive .
* /
MACHINE
}
2023-02-21 22:38:14 -06:00
}