Skip to content

Commit

Permalink
Make power monitoring easier
Browse files Browse the repository at this point in the history
Simply pass in subsystems to be monitored in RobotContainer.

	modified:   src/main/java/frc/robot/README
	modified:   src/main/java/frc/robot/RobotContainer.java
	modified:   src/main/java/frc/robot/subsystems/flywheel_example/Flywheel.java
	modified:   src/main/java/frc/robot/subsystems/flywheel_example/FlywheelIO.java
	modified:   src/main/java/frc/robot/subsystems/flywheel_example/FlywheelIOSparkMax.java
	modified:   src/main/java/frc/robot/subsystems/flywheel_example/FlywheelIOTalonFX.java
	modified:   src/main/java/frc/robot/util/PowerMonitoring.java
	new file:   src/main/java/frc/robot/util/RBSISubsystem.java
  • Loading branch information
tbowers7 committed Nov 6, 2024
1 parent 589eda4 commit 8e64838
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 26 deletions.
11 changes: 5 additions & 6 deletions src/main/java/frc/robot/README
Original file line number Diff line number Diff line change
Expand Up @@ -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.


--------------------
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Command> autoChooser;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -108,4 +108,9 @@ public double getVelocityRPM() {
public double getCharacterizationVelocity() {
return inputs.velocityRadPerSec;
}

@Override
public int[] getPowerPorts() {
return io.powerPorts;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Double> leaderPosition = leader.getPosition();
private final StatusSignal<Double> leaderVelocity = leader.getVelocity();
Expand Down
35 changes: 18 additions & 17 deletions src/main/java/frc/robot/util/PowerMonitoring.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

public class PowerMonitoring extends VirtualSubsystem {

private final RBSISubsystem[] subsystems;

/** Define the Power Distribution Hardware */
private PowerDistribution m_powerModule =
new PowerDistribution(
Expand All @@ -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() {
Expand All @@ -76,24 +78,23 @@ 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);
Logger.recordOutput("PowerMonitor/SteerCurrent", steerCurrent);
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

Expand Down
29 changes: 29 additions & 0 deletions src/main/java/frc/robot/util/RBSISubsystem.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 8e64838

Please sign in to comment.