Skip to content

Commit 2194c07

Browse files
committed
Pre-calculate exponential filter alpha constants
1 parent f50f14f commit 2194c07

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

fw/bldc_servo.cc

+36-18
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,27 @@ class PhaseMonitors {
245245
volatile uint32_t* reg_in_ = nullptr;
246246
uint32_t mask_ = 0;
247247
};
248+
249+
class ExponentialFilter {
250+
public:
251+
ExponentialFilter() {}
252+
253+
ExponentialFilter(float rate_hz, float period_s)
254+
: alpha_(1.0f / (rate_hz * period_s)),
255+
one_minus_alpha_(1.0f - alpha_) {}
256+
257+
void operator()(float input, float* filtered) {
258+
if (std::isnan(*filtered)) {
259+
*filtered = input;
260+
} else {
261+
*filtered = alpha_ * input + one_minus_alpha_ * *filtered;
262+
}
263+
}
264+
265+
private:
266+
float alpha_ = 1.0;
267+
float one_minus_alpha_ = 0.0;
268+
};
248269
}
249270

250271
class BldcServo::Impl {
@@ -459,6 +480,11 @@ class BldcServo::Impl {
459480
// Update the saved config to match our limits.
460481
config_.pwm_rate_hz = rate_config_.pwm_rate_hz;
461482

483+
velocity_filter_ = ExponentialFilter(rate_config_.pwm_rate_hz, 0.01f);
484+
temperature_filter_ = ExponentialFilter(rate_config_.pwm_rate_hz, 0.01f);
485+
slow_bus_v_filter_ = ExponentialFilter(rate_config_.pwm_rate_hz, 0.5f);
486+
fast_bus_v_filter_ = ExponentialFilter(rate_config_.pwm_rate_hz, 0.001f);
487+
462488
ConfigurePwmTimer();
463489

464490
const float kv = 0.5f * 60.0f / motor_.v_per_hz;
@@ -1017,7 +1043,7 @@ class BldcServo::Impl {
10171043
#endif
10181044
motor_position_->ISR_Update(rate_config_.period_s);
10191045

1020-
ISR_UpdateFilteredValue(position_.velocity, &status_.velocity_filt, 0.01f);
1046+
velocity_filter_(position_.velocity, &status_.velocity_filt);
10211047

10221048
// The temperature sensing should be done by now, but just double
10231049
// check.
@@ -1050,11 +1076,11 @@ class BldcServo::Impl {
10501076

10511077
{
10521078
status_.fet_temp_C = fet_thermistor_.Calculate(status_.adc_fet_temp_raw);
1053-
ISR_UpdateFilteredValue(status_.fet_temp_C, &status_.filt_fet_temp_C, 0.01f);
1079+
temperature_filter_(status_.fet_temp_C, &status_.filt_fet_temp_C);
10541080

10551081
if (config_.enable_motor_temperature) {
10561082
status_.motor_temp_C = motor_thermistor_.Calculate(status_.adc_motor_temp_raw);
1057-
ISR_UpdateFilteredValue(status_.motor_temp_C, &status_.filt_motor_temp_C, 0.01f);
1083+
temperature_filter_(status_.motor_temp_C, &status_.filt_motor_temp_C);
10581084
} else {
10591085
status_.motor_temp_C = status_.filt_motor_temp_C = 0.0f;
10601086
}
@@ -1068,19 +1094,6 @@ class BldcServo::Impl {
10681094
aux2_port_->ISR_EndAnalogSample();
10691095
}
10701096

1071-
void ISR_UpdateFilteredValue(float input, float* filtered, float period_s) const MOTEUS_CCM_ATTRIBUTE {
1072-
if (std::isnan(*filtered)) {
1073-
*filtered = input;
1074-
} else {
1075-
const float alpha = 1.0f / (rate_config_.rate_hz * period_s);
1076-
*filtered = alpha * input + (1.0f - alpha) * *filtered;
1077-
}
1078-
}
1079-
1080-
void ISR_UpdateFilteredBusV(float* filtered, float period_s) const MOTEUS_CCM_ATTRIBUTE {
1081-
ISR_UpdateFilteredValue(status_.bus_V, filtered, period_s);
1082-
}
1083-
10841097
// This is called from the ISR.
10851098
void ISR_CalculateCurrentState(const SinCos& sin_cos) MOTEUS_CCM_ATTRIBUTE {
10861099
status_.cur1_A = (status_.adc_cur1_raw - status_.adc_cur1_offset) * adc_scale_;
@@ -1091,8 +1104,8 @@ class BldcServo::Impl {
10911104
}
10921105
status_.bus_V = status_.adc_voltage_sense_raw * vsense_adc_scale_;
10931106

1094-
ISR_UpdateFilteredBusV(&status_.filt_bus_V, 0.5f);
1095-
ISR_UpdateFilteredBusV(&status_.filt_1ms_bus_V, 0.001f);
1107+
slow_bus_v_filter_(status_.bus_V, &status_.filt_bus_V);
1108+
fast_bus_v_filter_(status_.bus_V, &status_.filt_1ms_bus_V);
10961109

10971110
DqTransform dq{sin_cos,
10981111
status_.cur1_A,
@@ -2366,6 +2379,11 @@ class BldcServo::Impl {
23662379

23672380
IRQn_Type pwm_irqn_ = {};
23682381

2382+
ExponentialFilter velocity_filter_;
2383+
ExponentialFilter temperature_filter_;
2384+
ExponentialFilter slow_bus_v_filter_;
2385+
ExponentialFilter fast_bus_v_filter_;
2386+
23692387
const bool family0_rev4_and_older_ = (
23702388
g_measured_hw_family == 0 &&
23712389
g_measured_hw_rev <= 4);

0 commit comments

Comments
 (0)