diff --git a/src/main/java/frc/robot/README b/src/main/java/frc/robot/README index 434d547..700dc4b 100644 --- a/src/main/java/frc/robot/README +++ b/src/main/java/frc/robot/README @@ -68,12 +68,11 @@ util/ Various utility functions used by the Az-RBSI are included in this directory. Most teams will not need to modify these routines, but additional utilities deemed necessary by individual teams may be placed - here for organizational purposes. The EXCEPTIONS are - ``PowerMonitoring.java`` and ``OverrideSwitches.java``, which may be - modified to meet the specific designs of teams. ``PowerMonitoring.java`` - can be modified to add additional mechanisms to the power monitor logging, - and ``OverrideSwitches.java`` can be modified to match any manual override - switches on an operator console as described in that module. + here for organizational purposes. The EXCEPTION is + ``OverrideSwitches.java``, which may be modified to meet the specific + design requirements of teams. ``OverrideSwitches.java`` can be modified + to match any manual override switches on an operator console as described + in that module. -------------------- diff --git a/src/main/java/frc/robot/RobotContainer.java b/src/main/java/frc/robot/RobotContainer.java index 3269512..dfba5be 100644 --- a/src/main/java/frc/robot/RobotContainer.java +++ b/src/main/java/frc/robot/RobotContainer.java @@ -41,6 +41,7 @@ import frc.robot.subsystems.vision.VisionIO; import frc.robot.subsystems.vision.VisionIOPhoton; import frc.robot.util.OverrideSwitches; +import frc.robot.util.PowerMonitoring; import frc.robot.util.RobotDeviceId; import org.littletonrobotics.junction.networktables.LoggedDashboardChooser; @@ -55,8 +56,9 @@ public class RobotContainer { // Declare the robot subsystems here private final SwerveSubsystem m_drivebase; private final Flywheel m_flywheel; - private final Vision m_vision; private final Accelerometer m_accel; + private final Vision m_vision; + private final PowerMonitoring m_power; // Dashboard inputs private final LoggedDashboardChooser autoChooser; @@ -107,6 +109,11 @@ public RobotContainer() { break; } + // ``PowerMonitoring`` takes all the subsystems for which you wish to have + // power monitoring; DO NOT include ``m_drivebase``, as that is already + // included. + m_power = new PowerMonitoring(m_flywheel); + // Configure the trigger bindings configureBindings(); // Define Auto commands diff --git a/src/main/java/frc/robot/subsystems/flywheel_example/Flywheel.java b/src/main/java/frc/robot/subsystems/flywheel_example/Flywheel.java index db24d82..88d9102 100644 --- a/src/main/java/frc/robot/subsystems/flywheel_example/Flywheel.java +++ b/src/main/java/frc/robot/subsystems/flywheel_example/Flywheel.java @@ -19,13 +19,13 @@ import edu.wpi.first.math.controller.SimpleMotorFeedforward; import edu.wpi.first.math.util.Units; import edu.wpi.first.wpilibj2.command.Command; -import edu.wpi.first.wpilibj2.command.SubsystemBase; import edu.wpi.first.wpilibj2.command.sysid.SysIdRoutine; import frc.robot.Constants; +import frc.robot.util.RBSISubsystem; import org.littletonrobotics.junction.AutoLogOutput; import org.littletonrobotics.junction.Logger; -public class Flywheel extends SubsystemBase { +public class Flywheel extends RBSISubsystem { private final FlywheelIO io; private final FlywheelIOInputsAutoLogged inputs = new FlywheelIOInputsAutoLogged(); private final SimpleMotorFeedforward ffModel; @@ -108,4 +108,9 @@ public double getVelocityRPM() { public double getCharacterizationVelocity() { return inputs.velocityRadPerSec; } + + @Override + public int[] getPowerPorts() { + return io.powerPorts; + } } diff --git a/src/main/java/frc/robot/subsystems/flywheel_example/FlywheelIO.java b/src/main/java/frc/robot/subsystems/flywheel_example/FlywheelIO.java index 4830fc3..403c200 100644 --- a/src/main/java/frc/robot/subsystems/flywheel_example/FlywheelIO.java +++ b/src/main/java/frc/robot/subsystems/flywheel_example/FlywheelIO.java @@ -16,6 +16,10 @@ import org.littletonrobotics.junction.AutoLog; public interface FlywheelIO { + + // IMPORTANT: Include here all devices that are part of this mechanism! + public final int[] powerPorts = {}; + @AutoLog public static class FlywheelIOInputs { public double positionRad = 0.0; diff --git a/src/main/java/frc/robot/subsystems/flywheel_example/FlywheelIOSparkMax.java b/src/main/java/frc/robot/subsystems/flywheel_example/FlywheelIOSparkMax.java index cf9b845..5fde4f0 100644 --- a/src/main/java/frc/robot/subsystems/flywheel_example/FlywheelIOSparkMax.java +++ b/src/main/java/frc/robot/subsystems/flywheel_example/FlywheelIOSparkMax.java @@ -37,6 +37,10 @@ public class FlywheelIOSparkMax implements FlywheelIO { new CANSparkMax(Ports.FLYWHEEL_LEADER.getDeviceNumber(), MotorType.kBrushless); private final RelativeEncoder encoder = leader.getEncoder(); private final SparkPIDController pid = leader.getPIDController(); + // IMPORTANT: Include here all devices listed above that are part of this mechanism! + public final int[] powerPorts = { + Ports.FLYWHEEL_LEADER.getPowerPort(), Ports.FLYWHEEL_FOLLOWER.getPowerPort() + }; public FlywheelIOSparkMax() { leader.restoreFactoryDefaults(); diff --git a/src/main/java/frc/robot/subsystems/flywheel_example/FlywheelIOTalonFX.java b/src/main/java/frc/robot/subsystems/flywheel_example/FlywheelIOTalonFX.java index 2e08721..2cb4d84 100644 --- a/src/main/java/frc/robot/subsystems/flywheel_example/FlywheelIOTalonFX.java +++ b/src/main/java/frc/robot/subsystems/flywheel_example/FlywheelIOTalonFX.java @@ -34,6 +34,10 @@ public class FlywheelIOTalonFX implements FlywheelIO { new TalonFX(Ports.FLYWHEEL_LEADER.getDeviceNumber(), Ports.FLYWHEEL_LEADER.getBus()); private final TalonFX follower = new TalonFX(Ports.FLYWHEEL_FOLLOWER.getDeviceNumber(), Ports.FLYWHEEL_FOLLOWER.getBus()); + // IMPORTANT: Include here all devices listed above that are part of this mechanism! + public final int[] powerPorts = { + Ports.FLYWHEEL_LEADER.getPowerPort(), Ports.FLYWHEEL_FOLLOWER.getPowerPort() + }; private final StatusSignal leaderPosition = leader.getPosition(); private final StatusSignal leaderVelocity = leader.getVelocity(); diff --git a/src/main/java/frc/robot/util/PowerMonitoring.java b/src/main/java/frc/robot/util/PowerMonitoring.java index a328384..d9d25c8 100644 --- a/src/main/java/frc/robot/util/PowerMonitoring.java +++ b/src/main/java/frc/robot/util/PowerMonitoring.java @@ -22,6 +22,8 @@ public class PowerMonitoring extends VirtualSubsystem { + private final RBSISubsystem[] subsystems; + /** Define the Power Distribution Hardware */ private PowerDistribution m_powerModule = new PowerDistribution( @@ -39,18 +41,18 @@ public class PowerMonitoring extends VirtualSubsystem { Ports.BR_DRIVE.getPowerPort() }; - // STEER motor power plugged + // STEER motor power ports private final int[] m_steerPowerPorts = { Ports.FL_ROTATION.getPowerPort(), Ports.FR_ROTATION.getPowerPort(), Ports.BL_ROTATION.getPowerPort(), Ports.BR_ROTATION.getPowerPort() }; - // Add additional subsystem port enumerations here for combined monitoring - // Example: - private final int[] m_flywheelPowerPorts = { - Ports.FLYWHEEL_LEADER.getPowerPort(), Ports.FLYWHEEL_FOLLOWER.getPowerPort() - }; + + // Class method definition, including inputs of optional subsystems + public PowerMonitoring(RBSISubsystem... subsystems) { + this.subsystems = subsystems; + } /** Periodic Method */ public void periodic() { @@ -76,13 +78,6 @@ public void periodic() { for (int port : m_steerPowerPorts) { steerCurrent += channelCurrents[port]; } - // Add current monitoring by subsystem here - // Example: - double flywheelCurrent = 0.0; - for (int port : m_flywheelPowerPorts) { - flywheelCurrent += channelCurrents[port]; - } - // Log values to AdvantageKit and to SmartDashboard Logger.recordOutput("PowerMonitor/TotalCurrent", totalCurrent); Logger.recordOutput("PowerMonitor/DriveCurrent", driveCurrent); @@ -90,10 +85,16 @@ public void periodic() { SmartDashboard.putNumber("TotalCurrent", totalCurrent); SmartDashboard.putNumber("DriveCurrent", driveCurrent); SmartDashboard.putNumber("SteerCurrent", steerCurrent); - // Add logging for subsystems here - // Example: - Logger.recordOutput("PowerMonitor/FlywheelCurrent", flywheelCurrent); - SmartDashboard.putNumber("FlywheelCurrent", flywheelCurrent); + + // Compute and log any passed-in subsystems + for (RBSISubsystem subsystem : subsystems) { + double subsystemCurrent = 0.0; + for (int port : subsystem.getPowerPorts()) { + subsystemCurrent += channelCurrents[port]; + } + Logger.recordOutput("PowerMonitor/" + subsystem.getName() + "Current", subsystemCurrent); + SmartDashboard.putNumber(subsystem.getName() + "Current", subsystemCurrent); + } // Do something about setting priorities if drawing too much current diff --git a/src/main/java/frc/robot/util/RBSISubsystem.java b/src/main/java/frc/robot/util/RBSISubsystem.java new file mode 100644 index 0000000..7aa52fa --- /dev/null +++ b/src/main/java/frc/robot/util/RBSISubsystem.java @@ -0,0 +1,29 @@ +// Copyright (c) 2024 Az-FIRST +// http://github.com/AZ-First +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// version 3 as published by the Free Software Foundation or +// available in the root directory of this project. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +package frc.robot.util; + +import edu.wpi.first.wpilibj2.command.SubsystemBase; + +public class RBSISubsystem extends SubsystemBase { + + /** + * Gets the power ports associated with this Subsystem. + * + * @return Array of power distribution module ports + */ + public int[] getPowerPorts() { + int[] retval = {}; + return retval; + } +}