Skip to content

Commit 6a08cf6

Browse files
committed
refactor: rename constants to UPPERCASE and use constexpr
1 parent 05b965e commit 6a08cf6

2 files changed

Lines changed: 15 additions & 15 deletions

File tree

Firmware/FFBoard/UserExtensions/Inc/TMC4671.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ friend class TMCDebugBridge;
545545
uint16_t maxPowerAxis = 0;
546546

547547
int16_t controlFluxDissipate();
548-
const float fluxDissipationLimit = 1000;
548+
static constexpr float FLUX_DISSIPATION_LIMIT = 1000.0f;
549549

550550
void setTorque(int16_t torque);
551551

@@ -680,10 +680,10 @@ friend class TMCDebugBridge;
680680
private:
681681
uint8_t drv_address = 0;
682682
OutputPin enablePin = OutputPin(*DRV_ENABLE_GPIO_Port,DRV_ENABLE_Pin);
683-
const Error indexNotHitError = Error(ErrorCode::encoderIndexMissed,ErrorType::critical,"Encoder index missed");
684-
const Error lowVoltageError = Error(ErrorCode::undervoltage,ErrorType::warning,"Low motor voltage");
685-
const Error communicationError = Error(ErrorCode::tmcCommunicationError, ErrorType::warning, "TMC not responding");
686-
const Error estopError = Error(ErrorCode::emergencyStop, ErrorType::critical, "TMC emergency stop triggered");
683+
const Error INDEX_NOT_HIT_ERROR = Error(ErrorCode::encoderIndexMissed,ErrorType::critical,"Encoder index missed");
684+
const Error LOW_VOLTAGE_ERROR = Error(ErrorCode::undervoltage,ErrorType::warning,"Low motor voltage");
685+
const Error COMMUNICATION_ERROR = Error(ErrorCode::tmcCommunicationError, ErrorType::warning, "TMC not responding");
686+
const Error ESTOP_ERROR = Error(ErrorCode::emergencyStop, ErrorType::critical, "TMC emergency stop triggered");
687687

688688
TMC_ControlState state = TMC_ControlState::uninitialized;
689689
TMC_ControlState laststate = TMC_ControlState::uninitialized;
@@ -721,7 +721,7 @@ friend class TMCDebugBridge;
721721
bool recalibrationRequired = false;
722722

723723
uint8_t enc_retry = 0;
724-
uint8_t enc_retry_max = 3;
724+
static constexpr uint8_t ENC_RETRY_MAX = 3;
725725

726726
uint32_t lastStatTime = 0;
727727

@@ -786,7 +786,7 @@ friend class TMCDebugBridge;
786786

787787
TMC4671Biquad_conf torqueFilterConf;
788788
TMC4671BiquadFilters curFilters;
789-
const float fluxFilterFreq = 350.0;
789+
static constexpr float FLUX_FILTER_FREQ = 350.0f;
790790

791791
// External encoder timer fires interrupts to trigger a new commutation position update
792792
#ifdef TIM_TMC

Firmware/FFBoard/UserExtensions/Src/TMC4671.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ bool TMC4671::initialize(){
302302
// }
303303
// Check if a TMC4671 is active and replies correctly
304304
if(!pingDriver()){
305-
ErrorHandler::addError(communicationError);
305+
ErrorHandler::addError(COMMUNICATION_ERROR);
306306
return false;
307307
}
308308

@@ -345,7 +345,7 @@ bool TMC4671::initialize(){
345345
setAdcOffset(conf.adc_I0_offset, conf.adc_I1_offset);
346346
setAdcScale(conf.adc_I0_scale, conf.adc_I1_scale);
347347
setTorqueFilter(torqueFilterConf);
348-
setBiquadFlux(TMC4671Biquad(Biquad(BiquadType::lowpass, fluxFilterFreq / getPwmFreq(), 0.7,0.0), true)); // Create flux filter
348+
setBiquadFlux(TMC4671Biquad(Biquad(BiquadType::lowpass, FLUX_FILTER_FREQ / getPwmFreq(), 0.7,0.0), true)); // Create flux filter
349349

350350
// Initial adc calibration and check without PWM if power off to get basic offsets. PWM is off!
351351
if(!hasPower()){
@@ -542,7 +542,7 @@ void TMC4671::Run(){
542542
bool tmc_en = ((pins >> 15) & 0x01) && pins != 0xffffffff;
543543
if(tmc_en){
544544
// Emergency stop reset
545-
ErrorHandler::clearError(estopError);
545+
ErrorHandler::clearError(ESTOP_ERROR);
546546
this->estopTriggered = false; // TODO resume correctly
547547
changeState(TMC_ControlState::uninitialized,true);
548548
}
@@ -625,7 +625,7 @@ void TMC4671::Run(){
625625
if(!hasPower() && state != TMC_ControlState::waitPower && initialized && powerInitialized){ // low voltage or overvoltage
626626

627627
requestedState = state;
628-
ErrorHandler::addError(lowVoltageError);
628+
ErrorHandler::addError(LOW_VOLTAGE_ERROR);
629629
setMotionMode(MotionMode::stop,true); // Disable tmc
630630
changeState(TMC_ControlState::waitPower,true);
631631
allowStateChange = false;
@@ -753,7 +753,7 @@ bool TMC4671::findEncoderIndex(int32_t speed, uint16_t power,bool offsetPhiM,boo
753753
runOpenLoop(0, 0, 0, 10, true);
754754
if(!encoderIndexHitFlag){
755755
pulseErrLed();
756-
ErrorHandler::addError(indexNotHitError);
756+
ErrorHandler::addError(INDEX_NOT_HIT_ERROR);
757757
}
758758

759759
// If zero count on index write a phiM offset so that phiM is 0 on index and we don't need to change the raw encoder count (possible timing danger)
@@ -1368,7 +1368,7 @@ void TMC4671::encoderInit(){
13681368

13691369
// Check encoder
13701370
if(!checkEncoder()){
1371-
if(++enc_retry > enc_retry_max){
1371+
if(++enc_retry > ENC_RETRY_MAX){
13721372
encoderAligned = false;
13731373
Error err = Error(ErrorCode::encoderAlignmentFailed,ErrorType::critical,"Encoder alignment failed");
13741374
ErrorHandler::addError(err);
@@ -1613,7 +1613,7 @@ void TMC4671::emergencyStop(bool reset){
16131613
int16_t TMC4671::controlFluxDissipate(){
16141614

16151615
int32_t vDiff = getIntV() - getExtV();
1616-
if(vDiff > fluxDissipationLimit){
1616+
if(vDiff > FLUX_DISSIPATION_LIMIT){
16171617
// Reaches limit at +5v if scaler is 1
16181618
return(clip<int32_t,int32_t>(vDiff * conf.hwconf.fluxDissipationScaler * curLimits.pid_torque_flux * 0.0002,0,curLimits.pid_torque_flux));
16191619
}
@@ -3236,7 +3236,7 @@ void TMC4671::handleStateRunning() {
32363236
if (!tmc_en && motorEnabledRequested) { // Hardware emergency.
32373237
this->estopTriggered = true;
32383238
this->emergencyStop(false);
3239-
ErrorHandler::addError(estopError);
3239+
ErrorHandler::addError(ESTOP_ERROR);
32403240
}
32413241

32423242
// Temperature sense

0 commit comments

Comments
 (0)