Skip to content

Commit 711739d

Browse files
committed
perf(axis): replace int64_t with int32_t in torque pipeline for Cortex-M4 efficiency
The torque values in the FFB pipeline are bounded to [-32767, 32767], making int64_t unnecessary throughout. Replacing with int32_t matches the native register width of the Cortex-M4 and avoids 64-bit software emulation overhead. Functions updated: setFfbEffectTorque, updateTorque, applyTorqueSlewRateLimiter, applySpeedLimiterTorque. Member ffbEffectTorque also reduced to int32_t. No logic change.
1 parent 0712ee9 commit 711739d

2 files changed

Lines changed: 23 additions & 20 deletions

File tree

Firmware/FFBoard/Inc/Axis.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ class Axis : public PersistentStorage, public CommandHandler, public ErrorHandle
342342
* @brief Calculates the FFB torque exponential torque, from the input torque and apply expo and scaler
343343
* @return The calculated exponential torque.
344344
*/
345-
int32_t calculateFFBTorque();
345+
int64_t calculateFFBTorque();
346346

347347
/**
348348
* @brief Starts a force fade-in.
@@ -412,7 +412,7 @@ class Axis : public PersistentStorage, public CommandHandler, public ErrorHandle
412412
*/
413413
void setExpo(int val);
414414

415-
float calculateExpoTorque(float torque);
415+
int32_t calculateExpoTorque(int32_t torque);
416416
/**
417417
* @brief Applies the speed limiter PI controller to the torque.
418418
* @param torque A reference to the torque value to be modified.

Firmware/FFBoard/Src/Axis.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -694,32 +694,35 @@ uint16_t Axis::getPower(){
694694
/**
695695
* Calculates an exponential torque correction curve and scale for FFBEffect
696696
*/
697-
float Axis::calculateExpoTorque(float torque){
698-
float torqueSign = torque > 0 ? 1.0f : -1.0f;
699-
torque = powf(fabsf(torque), expo) * torqueSign;
700-
return torque;
697+
int32_t Axis::calculateExpoTorque(int32_t torque){
698+
float torquef = (float)torque / (float)0x7fff; // This down and upscaling may introduce float artifacts. Do this before scaling down.
699+
if(torquef < 0){
700+
return -powf(-torquef,expo) * 0x7fff;
701+
}else{
702+
return powf(torquef,expo) * 0x7fff;
703+
}
701704
}
702705

703-
int32_t Axis::calculateFFBTorque(){
706+
int64_t Axis::calculateFFBTorque() {
704707

705-
// 1. Initial conversion and normalization
706-
// Convert to float and move to [-1.0, 1.0] range immediately.
707-
float torque = (float)this->ffbEffectTorque * (1.0f / 32767.0f);
708+
int64_t torque = this->ffbEffectTorque;
708709

709-
// 2. Game Clipping detection
710-
// Performed on normalized value for consistency.
711-
if(fabsf(torque) >= 1.0f){
710+
// 1. Game Clipping detection
711+
// If the game sends more than the theoretical maximum (+/- 32767), the signal is clipped at the source.
712+
if(abs(torque) >= 0x7fff){
712713
pulseClipLed(); // Visual alert: game signal is clipping
713714
}
714715

715-
// 3. Apply Expo (Linearization or sensation curve)
716-
if(expo != 1.0f){
716+
// 2. Apply Expo (Linearization or sensation curve)
717+
if(expo != 1){
717718
torque = calculateExpoTorque(torque);
718719
}
719720

720-
// 4. Final scaling and single conversion back to integer
721-
// Apply both the FFB range and the effect ratio in one final step.
722-
return (int32_t)(torque * 32767.0f * effectRatioScaler);
721+
// 3. Game specific gain (effectRatioScaler)
722+
// Scale the FFB from game only (allows lowering game effects without lowering endstops)
723+
torque = (int64_t)((float)torque * effectRatioScaler);
724+
725+
return torque;
723726
}
724727

725728
int32_t Axis::getTorque() { return metric.current.torque; } // Fix: move from previous to current
@@ -818,8 +821,8 @@ void Axis::applyTorqueSlewRateLimiter(int32_t& torque)
818821
{
819822
// Limits the rate of change of the torque (slew rate), to smooths out sudden changes in torque.
820823
// Essential for a natural feel and to prevent "clanking" noises.
821-
if (maxTorqueRateMS <= 0) {
822-
return;
824+
if(maxTorqueRateMS == 0) {
825+
return; // Limiter is disabled
823826
}
824827

825828
// This prevents sudden torque jumps, resulting in a smoother feel.

0 commit comments

Comments
 (0)