@@ -245,6 +245,27 @@ class PhaseMonitors {
245
245
volatile uint32_t * reg_in_ = nullptr ;
246
246
uint32_t mask_ = 0 ;
247
247
};
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
+ };
248
269
}
249
270
250
271
class BldcServo ::Impl {
@@ -459,6 +480,11 @@ class BldcServo::Impl {
459
480
// Update the saved config to match our limits.
460
481
config_.pwm_rate_hz = rate_config_.pwm_rate_hz ;
461
482
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
+
462
488
ConfigurePwmTimer ();
463
489
464
490
const float kv = 0 .5f * 60 .0f / motor_.v_per_hz ;
@@ -1017,7 +1043,7 @@ class BldcServo::Impl {
1017
1043
#endif
1018
1044
motor_position_->ISR_Update (rate_config_.period_s );
1019
1045
1020
- ISR_UpdateFilteredValue (position_.velocity , &status_.velocity_filt , 0 . 01f );
1046
+ velocity_filter_ (position_.velocity , &status_.velocity_filt );
1021
1047
1022
1048
// The temperature sensing should be done by now, but just double
1023
1049
// check.
@@ -1050,11 +1076,11 @@ class BldcServo::Impl {
1050
1076
1051
1077
{
1052
1078
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 );
1054
1080
1055
1081
if (config_.enable_motor_temperature ) {
1056
1082
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 );
1058
1084
} else {
1059
1085
status_.motor_temp_C = status_.filt_motor_temp_C = 0 .0f ;
1060
1086
}
@@ -1068,19 +1094,6 @@ class BldcServo::Impl {
1068
1094
aux2_port_->ISR_EndAnalogSample ();
1069
1095
}
1070
1096
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
-
1084
1097
// This is called from the ISR.
1085
1098
void ISR_CalculateCurrentState (const SinCos& sin_cos) MOTEUS_CCM_ATTRIBUTE {
1086
1099
status_.cur1_A = (status_.adc_cur1_raw - status_.adc_cur1_offset ) * adc_scale_;
@@ -1091,8 +1104,8 @@ class BldcServo::Impl {
1091
1104
}
1092
1105
status_.bus_V = status_.adc_voltage_sense_raw * vsense_adc_scale_;
1093
1106
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 );
1096
1109
1097
1110
DqTransform dq{sin_cos,
1098
1111
status_.cur1_A ,
@@ -2366,6 +2379,11 @@ class BldcServo::Impl {
2366
2379
2367
2380
IRQn_Type pwm_irqn_ = {};
2368
2381
2382
+ ExponentialFilter velocity_filter_;
2383
+ ExponentialFilter temperature_filter_;
2384
+ ExponentialFilter slow_bus_v_filter_;
2385
+ ExponentialFilter fast_bus_v_filter_;
2386
+
2369
2387
const bool family0_rev4_and_older_ = (
2370
2388
g_measured_hw_family == 0 &&
2371
2389
g_measured_hw_rev <= 4 );
0 commit comments