Skip to content

Commit

Permalink
Use signalmanager to make only one JNI call for all CTRE signals
Browse files Browse the repository at this point in the history
  • Loading branch information
superpenguin612 committed Mar 8, 2024
1 parent 132237a commit 7e3c7a4
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 194 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.techhounds.houndutil.houndlib.robots;

import edu.wpi.first.wpilibj.Threads;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
Expand All @@ -9,7 +10,9 @@

import com.techhounds.houndutil.houndauto.AutoManager;
import com.techhounds.houndutil.houndlib.TriConsumer;
import com.techhounds.houndutil.houndlog.FaultLogger;
import com.techhounds.houndutil.houndlog.LoggingManager;
import com.techhounds.houndutil.houndlog.SignalManager;

public class HoundRobot extends TimedRobot {
public HoundRobot() {
Expand All @@ -34,14 +37,16 @@ public void robotInit() {
// sets the LoggingManager to run every 100ms and on an offset of 10ms from the
// main thread
LoggingManager.getInstance().init();
addPeriodic(LoggingManager.getInstance()::run, 0.100, 0.010);
addPeriodic(FaultLogger::update, 0.100, 0.010);
LiveWindow.disableAllTelemetry();
SignalManager.registerAll();
Threads.setCurrentThreadPriority(true, 99);
}

@Override
public void robotPeriodic() {
CommandScheduler.getInstance().run();
// LoggingManager.getInstance().run();
LoggingManager.getInstance().run();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.techhounds.houndutil.houndlib.swerve;

import com.ctre.phoenix6.BaseStatusSignal;
import com.ctre.phoenix6.StatusSignal;
import com.ctre.phoenix6.configs.CurrentLimitsConfigs;
import com.ctre.phoenix6.configs.MagnetSensorConfigs;
import com.ctre.phoenix6.configs.MotorOutputConfigs;
Expand All @@ -17,6 +18,7 @@
import com.ctre.phoenix6.signals.NeutralModeValue;
import com.ctre.phoenix6.signals.SensorDirectionValue;
import com.techhounds.houndutil.houndlib.MotorHoldMode;
import com.techhounds.houndutil.houndlog.SignalManager;
import com.techhounds.houndutil.houndlog.interfaces.Log;
import com.techhounds.houndutil.houndlog.interfaces.LoggedObject;

Expand Down Expand Up @@ -50,6 +52,15 @@ public class KrakenCoaxialSwerveModule implements CoaxialSwerveModule {

private SwerveModuleState previousState = new SwerveModuleState();

private final StatusSignal<Double> drivePosition;
private final StatusSignal<Double> driveVelocity;
private final StatusSignal<Double> driveAcceleration;
private final StatusSignal<Double> driveMotorVoltage;
private final StatusSignal<Double> steerPosition;
private final StatusSignal<Double> steerVelocity;
private final StatusSignal<Double> steerAcceleration;
private final StatusSignal<Double> steerMotorVoltage;

/**
* Initalizes a SwerveModule.
*
Expand Down Expand Up @@ -137,30 +148,37 @@ public KrakenCoaxialSwerveModule(
SWERVE_CONSTANTS.STEER_GEARING,
SWERVE_CONSTANTS.STEER_MOI);

drivePosition = driveMotor.getPosition();
driveVelocity = driveMotor.getVelocity();
driveAcceleration = driveMotor.getAcceleration();
driveMotorVoltage = driveMotor.getMotorVoltage();
steerPosition = steerMotor.getPosition();
steerVelocity = steerMotor.getVelocity();
steerAcceleration = steerMotor.getAcceleration();
steerMotorVoltage = steerMotor.getMotorVoltage();

BaseStatusSignal.setUpdateFrequencyForAll(250,
driveMotor.getPosition(),
driveMotor.getVelocity(),
driveMotor.getAcceleration(),
driveMotor.getMotorVoltage(),
steerMotor.getPosition(),
steerMotor.getVelocity(),
steerMotor.getAcceleration(),
steerMotor.getMotorVoltage());
drivePosition, driveVelocity, driveAcceleration, driveMotorVoltage,
steerPosition, steerVelocity, steerAcceleration, steerMotorVoltage);

SignalManager.register(
drivePosition, driveVelocity, driveAcceleration, driveMotorVoltage,
steerPosition, steerVelocity, steerAcceleration, steerMotorVoltage);
}

@Override
public double getDriveMotorPosition() {
return BaseStatusSignal.getLatencyCompensatedValue(driveMotor.getPosition(), driveMotor.getVelocity());
return BaseStatusSignal.getLatencyCompensatedValue(drivePosition, driveVelocity);
}

@Override
public double getDriveMotorVelocity() {
return BaseStatusSignal.getLatencyCompensatedValue(driveMotor.getVelocity(), driveMotor.getAcceleration());
return BaseStatusSignal.getLatencyCompensatedValue(driveVelocity, driveAcceleration);
}

@Override
public double getDriveMotorVoltage() {
return driveMotor.getMotorVoltage().getValue();
return driveMotorVoltage.getValue();
}

public TalonFX getDriveMotor() {
Expand All @@ -169,26 +187,25 @@ public TalonFX getDriveMotor() {

@Override
public double getSteerMotorPosition() {
return BaseStatusSignal.getLatencyCompensatedValue(steerMotor.getPosition(), steerMotor.getVelocity());
return BaseStatusSignal.getLatencyCompensatedValue(steerPosition, steerVelocity);
}

@Override
public double getSteerMotorVelocity() {
return BaseStatusSignal.getLatencyCompensatedValue(steerMotor.getVelocity(), steerMotor.getAcceleration());
return BaseStatusSignal.getLatencyCompensatedValue(steerVelocity, steerAcceleration);
}

@Override
public double getSteerMotorVoltage() {
return steerMotor.getMotorVoltage().getValue();
return steerMotorVoltage.getValue();
}

public TalonFX getSteerMotor() {
return steerMotor;
}

public BaseStatusSignal[] getSignals() {
return new BaseStatusSignal[] { driveMotor.getPosition(), driveMotor.getVelocity(),
steerMotor.getPosition(), steerMotor.getVelocity() };
return new BaseStatusSignal[] { drivePosition, driveVelocity, steerPosition, steerVelocity };
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.networktables.StringArrayPublisher;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.PowerDistribution;
import edu.wpi.first.wpilibj.RobotBase;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashSet;
Expand Down Expand Up @@ -266,6 +263,8 @@ public static void register(TalonFX talon) {
talon.getStickyFault_UnstableSupplyV(),
talon.getStickyFault_UsingFusedCANcoderWhileUnlicensed());

faultSignals.forEach((s) -> SignalManager.register(s));

for (StatusSignal<Boolean> signal : faultSignals) {
register(signal::getValue, "Talon FX [" + talon.getDeviceID() + "]", signal.getName(), FaultType.ERROR);
}
Expand Down Expand Up @@ -294,6 +293,8 @@ public static void register(CANcoder cancoder) {
cancoder.getStickyFault_Undervoltage(),
cancoder.getStickyFault_UnlicensedFeatureInUse());

faultSignals.forEach((s) -> SignalManager.register(s));

for (StatusSignal<Boolean> signal : faultSignals) {
register(signal::getValue, "CANcoder [" + cancoder.getDeviceID() + "]", signal.getName(),
FaultType.ERROR);
Expand Down Expand Up @@ -334,6 +335,8 @@ public static void register(Pigeon2 pigeon) {
pigeon.getStickyFault_Undervoltage(),
pigeon.getStickyFault_UnlicensedFeatureInUse());

faultSignals.forEach((s) -> SignalManager.register(s));

for (StatusSignal<Boolean> signal : faultSignals) {
register(signal::getValue, "Pigeon 2 [" + pigeon.getDeviceID() + "]", signal.getName(),
FaultType.ERROR);
Expand Down
Loading

0 comments on commit 7e3c7a4

Please sign in to comment.