Skip to content

Commit

Permalink
Fixed Apply Configs for SparkMXFX_interm and TalonFX_interm
Browse files Browse the repository at this point in the history
  • Loading branch information
VyaasBaskar committed Jan 11, 2025
1 parent f5561b8 commit 51b8ace
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 37 deletions.
File renamed without changes.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ To undo the going back:

## CppCheck Warnings
```
src/frc846/cpp/frc846/robot/swerve/drivetrain.cc:160:64: warning: Variable 'accel_target' is assigned a value that is never used. [unreadVariable]
src/frc846/cpp/frc846/math/collection.cc:25:0: warning: The function 'VerticalDeadband' is never used. [unusedFunction]
src/frc846/cpp/frc846/math/collection.cc:39:0: warning: The function 'CoterminalDifference' is never used. [unusedFunction]
src/frc846/cpp/frc846/math/collection.cc:52:0: warning: The function 'CoterminalSum' is never used. [unusedFunction]
src\frc846\cpp\frc846\robot\swerve\drivetrain.cc:160:64: warning: Variable 'accel_target' is assigned a value that is never used. [unreadVariable]
src\frc846\cpp\frc846\math\collection.cc:25:0: warning: The function 'VerticalDeadband' is never used. [unusedFunction]
src\frc846\cpp\frc846\math\collection.cc:39:0: warning: The function 'CoterminalDifference' is never used. [unusedFunction]
src\frc846\cpp\frc846\math\collection.cc:52:0: warning: The function 'CoterminalSum' is never used. [unusedFunction]
```
8 changes: 5 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ deploy {
frcStaticFileDeploy(getArtifactTypeClass('FileTreeArtifact')) {
files = project.fileTree('src/y2025/deploy')
directory = '/home/lvuser/deploy'
deleteOldFiles = false // Change to true to delete files on roboRIO that no
// longer exist in deploy directory of this project


// Set deleteOldFiles to true to clear files in deploy/ on RIO that aren't in this project
deleteOldFiles = true
}
}
}
Expand Down Expand Up @@ -115,7 +117,7 @@ spotless {
}
def selectedClangVersion = project.hasProperty('fromCI') ? '18.1.8' : '18.1.8'

def styleFile = file('style.standard')
def styleFile = file('.clang-format')
def styleConfig = styleFile.text.trim()

clangFormat(selectedClangVersion).style(styleConfig)
Expand Down
52 changes: 26 additions & 26 deletions src/frc846/cpp/frc846/control/hardware/SparkMXFX_interm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,23 @@
namespace frc846::control::hardware {

#define set_last_error(code) last_error_ = getErrorCode(code)
#define set_last_error_and_break(code) \
set_last_error(code); \
if (last_error_ != ControllerErrorCodes::kAllOK) return

#define CONFIG_RESET rev::spark::SparkBase::ResetMode::kResetSafeParameters
#define NO_CONFIG_RESET rev::spark::SparkBase::ResetMode::kNoResetSafeParameters

#define PERSIST_PARAMS rev::spark::SparkBase::PersistMode::kPersistParameters
#define NO_PERSIST_PARAMS \
rev::spark::SparkBase::PersistMode::kNoPersistParameters

#define APPLY_CONFIG_NO_RESET() \
set_last_error(esc_->Configure(configs, NO_CONFIG_RESET, NO_PERSIST_PARAMS))

bool SparkMXFX_interm::VerifyConnected() {
if (esc_ == nullptr) return false;
esc_->GetFirmwareVersion();
return esc_->GetFirmwareVersion() != 0;
}

// This will make all soft configs reset
#define RESET rev::spark::SparkBase::ResetMode::kResetSafeParameters
// This will make all soft configs not reset
#define DONT_RESET rev::spark::SparkBase::ResetMode::kNoResetSafeParameters

// This will make configs persist through power cycles
#define PERSIST_PARAMS rev::spark::SparkBase::PersistMode::kPersistParameters
// Configs will not persist through power cycles
#define DONT_PERSIST_PARAMS \
rev::spark::SparkBase::PersistMode::kNoPersistParameters

SparkMXFX_interm::SparkMXFX_interm(int can_id,
units::millisecond_t max_wait_time, bool is_controller_spark_flex) {
esc_ = is_controller_spark_flex
Expand All @@ -36,8 +32,9 @@ SparkMXFX_interm::SparkMXFX_interm(int can_id,
encoder_ = new rev::spark::SparkRelativeEncoder{esc_->GetEncoder()};
pid_controller_ = new rev::spark::SparkClosedLoopController{
esc_->GetClosedLoopController()};
esc_->Configure(configs, RESET, PERSIST_PARAMS);
esc_->SetCANTimeout(max_wait_time.to<int>());

set_last_error(esc_->Configure(configs, CONFIG_RESET, NO_PERSIST_PARAMS));
set_last_error(esc_->SetCANTimeout(max_wait_time.to<int>()));
}

void SparkMXFX_interm::Tick() {
Expand All @@ -59,19 +56,22 @@ void SparkMXFX_interm::Tick() {

void SparkMXFX_interm::SetInverted(bool inverted) {
configs.Inverted(inverted);
set_last_error(esc_->Configure(configs, DONT_RESET, PERSIST_PARAMS));

APPLY_CONFIG_NO_RESET();
}

void SparkMXFX_interm::SetNeutralMode(bool brake_mode) {
configs.SetIdleMode(brake_mode
? rev::spark::SparkBaseConfig::IdleMode::kBrake
: rev::spark::SparkBaseConfig::IdleMode::kCoast);
set_last_error(esc_->Configure(configs, DONT_RESET, PERSIST_PARAMS));

APPLY_CONFIG_NO_RESET();
}

void SparkMXFX_interm::SetCurrentLimit(units::ampere_t current_limit) {
configs.SmartCurrentLimit(current_limit.to<int>());
set_last_error(esc_->Configure(configs, DONT_RESET, PERSIST_PARAMS));

APPLY_CONFIG_NO_RESET();
}

void SparkMXFX_interm::SetSoftLimits(
Expand All @@ -80,19 +80,22 @@ void SparkMXFX_interm::SetSoftLimits(
.ForwardSoftLimit(forward_limit.to<double>())
.ReverseSoftLimitEnabled(true)
.ReverseSoftLimit(reverse_limit.to<double>());
set_last_error(esc_->Configure(configs, DONT_RESET, PERSIST_PARAMS));

APPLY_CONFIG_NO_RESET();
}

void SparkMXFX_interm::SetVoltageCompensation(
units::volt_t voltage_compensation) {
configs.VoltageCompensation(voltage_compensation.to<double>());
set_last_error(esc_->Configure(configs, DONT_RESET, PERSIST_PARAMS));

APPLY_CONFIG_NO_RESET();
}

void SparkMXFX_interm::SetGains(frc846::control::base::MotorGains gains) {
gains_ = gains;
configs.closedLoop.Pidf(gains_.kP, gains_.kI, gains_.kD, gains_.kFF);
set_last_error(esc_->Configure(configs, DONT_RESET, PERSIST_PARAMS));

APPLY_CONFIG_NO_RESET();
}

void SparkMXFX_interm::WriteDC(double duty_cycle) {
Expand All @@ -107,8 +110,6 @@ void SparkMXFX_interm::WritePosition(units::radian_t position) {

void SparkMXFX_interm::EnableStatusFrames(
std::vector<frc846::control::config::StatusFrame> frames) {
rev::REVLibError last_status_code;

if (vector_has(frames, config::StatusFrame::kLeader) ||
vector_has(frames, config::StatusFrame::kFaultFrame)) {
configs.signals.FaultsPeriodMs(20);
Expand Down Expand Up @@ -143,8 +144,7 @@ void SparkMXFX_interm::EnableStatusFrames(
configs.signals.AnalogPositionAlwaysOn(false);
}

set_last_error_and_break(
esc_->Configure(configs, DONT_RESET, PERSIST_PARAMS));
APPLY_CONFIG_NO_RESET();
}

bool SparkMXFX_interm::IsDuplicateControlMessage(double duty_cycle) {
Expand Down
8 changes: 6 additions & 2 deletions src/frc846/cpp/frc846/control/hardware/TalonFX_interm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ void TalonFX_interm::Tick() {
}

void TalonFX_interm::SetInverted(bool inverted) {
config.MotorOutput.Inverted = inverted;
last_error_ = getErrorCode(talon_.GetConfigurator().Apply(config));
ctre::phoenix6::configs::MotorOutputConfigs motor_output_config{};
motor_output_config.WithInverted(inverted);
last_error_ =
getErrorCode(talon_.GetConfigurator().Apply(motor_output_config));
}

void TalonFX_interm::SetNeutralMode(bool brake_mode) {
talon_.SetNeutralMode(brake_mode
? ctre::phoenix6::signals::NeutralModeValue::Brake
: ctre::phoenix6::signals::NeutralModeValue::Coast);
}

void TalonFX_interm::SetCurrentLimit(units::ampere_t current_limit) {
ctre::phoenix6::configs::CurrentLimitsConfigs configs{};
configs.WithSupplyCurrentLimitEnable(false);
Expand Down
2 changes: 0 additions & 2 deletions src/frc846/include/frc846/control/hardware/TalonFX_interm.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,6 @@ class TalonFX_interm : public IntermediateController {

ctre::phoenix6::hardware::TalonFX talon_;

ctre::phoenix6::configs::TalonFXConfiguration config{};

frc846::control::hardware::ControllerErrorCodes last_error_;

units::millisecond_t max_wait_time_;
Expand Down

0 comments on commit 51b8ace

Please sign in to comment.