Skip to content

Subsystems and code structure

leakin24mpa edited this page Feb 8, 2024 · 15 revisions

Subsystems and code structure documentation

The robot has 6 main subsystems which control its various actions. These are:

  • Driving: SwerveSubsystem
  • Intake: IntakeSubsytem
  • Shooter/Indexer: ShooterSubsystem
  • Arm: ArmSubsystem
  • Climbers: ClimberSubsystem
  • LEDs: LEDController

Additionally, the Vision class contains static methods for getting data from the two limelights, and the SwerveModule class contains data about a swerve module and methods to set and get its state.

SwerveSubsystem

Member Variables

  • private SwerveModule[] swerveModules - an array of all four SwerveModules. #0 is the front left wheel, and we count up counterclockwise

  • private SwerveDrivePoseEstimator odometry - WPIlib pose estimator which combines wheel measurements with vision measurements to estimate the robot's location on the field

  • private Field2d field - used to display the robot's pose estimate on Shuffleboard

  • private final Pigeon2 pigeon - CTRE Pigeon sensor which measures changes in the robot's direction

Private Methods

  • SwerveModulePosition[] getPositions(): gets the SwerveModulePosition (distance traveled and current direction) for each module and returns them in an array

  • SwerveModuleState[] getStates(): gets the SwerveModuleState (speed and direction) for each module and returns them in an array

Public Methods

  • void drive() - drive function used during teleop control. Sets the robot to drive at a given speed, and uses open loop control to set the modules' speeds

    • Inputs:
      • double x, double y: the speed to move the robot on the x and y axes in meters/second
      • double rotation: the speed to spin the robot in radians/second
      • boolean isFieldRelative: if set to true, the robot will use the field's coordinate system instead of its own to determine driving speeds
  • void closedLoopDrive() - drive function used by the PathPlanner library while following trajectories. Sets the robot to drive at a given ChassisSpeeds and uses closed loop control to set the modules' speeds

  • void driveFromChassisSpeeds() - the core drive function of the robot, used by both other drive functions and during auto-aligning. Calculates the correct speed and direction for each wheel and passes them to the four modules.

    • Inputs:
      • ChassisSpeeds speeds: the speed to move the robot (ChassisSpeeds contains both translation and rotational speeds)
      • boolean isOpenLoop: whether the modules should control their speed with open- or closed-loop control
  • void resetOdometry(): sets the robot's pose to a given Pose2d

  • void zeroGyro(): sets the pigeon's "zero" direction to be the direction the robot is currently facing

  • Pose2d getPose(): returns the robots pose in meters as estimated by the odometry

  • ChassisSpeeds getRobotRelativeSpeeds() : gets the current velocity and angular velocity of the robot in meters/second and radians/second. Returns the data as a ChassisSpeeds

  • double getYawAsDouble() : gets the current heading measurement in degrees from the pigeon2. This value is not limited to 0-360, so if you spin around twice, the pigeon would read 720 degrees

  • Rotation2d getYaw() : constructs a Rotation2d with the heading measurement from the pigeon2

  • void periodic() : this function gets called every 20ms while the robot is powered (even if it's disabled). Anything that needs to be constantly updated goes here:

    • if an AprilTag is in view of the limelight, update the odometry with the limelight's estimated pose

    • supply info about the drivebase and swerve modules to the driverstation

Clone this wiki locally