Skip to content

Commit 7a0e560

Browse files
committed
Lowpass filter input voltage and battery level
1 parent fa3610c commit 7a0e560

10 files changed

+26
-15
lines changed

CHANGELOG

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* Added custom UI support.
2323
* Limit hall sensor angle rate of change based on ERPM.
2424
* Added p_pid_gain_dec_angle parameter.
25+
* Low pass filter input voltage.
2526

2627
=== FW 5.02 ===
2728
* IMU calibration improvement.

comm_can.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,7 @@ static void send_status5(uint8_t id, bool replace) {
17051705
int32_t send_index = 0;
17061706
uint8_t buffer[8];
17071707
buffer_append_int32(buffer, mc_interface_get_tachometer_value(false), &send_index);
1708-
buffer_append_int16(buffer, (int16_t)(GET_INPUT_VOLTAGE() * 1e1), &send_index);
1708+
buffer_append_int16(buffer, (int16_t)(mc_interface_get_input_voltage_filtered() * 1e1), &send_index);
17091709
buffer_append_int16(buffer, 0, &send_index); // Reserved for now
17101710
comm_can_transmit_eid_replace(id | ((uint32_t)CAN_PACKET_STATUS_5 << 8),
17111711
buffer, send_index, replace);

commands.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ void commands_process_packet(unsigned char *data, unsigned int len,
376376
buffer_append_float32(send_buffer, mc_interface_get_rpm(), 1e0, &ind);
377377
}
378378
if (mask & ((uint32_t)1 << 8)) {
379-
buffer_append_float16(send_buffer, GET_INPUT_VOLTAGE(), 1e1, &ind);
379+
buffer_append_float16(send_buffer, mc_interface_get_input_voltage_filtered(), 1e1, &ind);
380380
}
381381
if (mask & ((uint32_t)1 << 9)) {
382382
buffer_append_float32(send_buffer, mc_interface_get_amp_hours(false), 1e4, &ind);
@@ -823,7 +823,7 @@ void commands_process_packet(unsigned char *data, unsigned int len,
823823
buffer_append_float32(send_buffer, mc_interface_get_speed(), 1e3, &ind);
824824
}
825825
if (mask & ((uint32_t)1 << 7)) {
826-
buffer_append_float16(send_buffer, GET_INPUT_VOLTAGE(), 1e1, &ind);
826+
buffer_append_float16(send_buffer, mc_interface_get_input_voltage_filtered(), 1e1, &ind);
827827
}
828828
if (mask & ((uint32_t)1 << 8)) {
829829
buffer_append_float16(send_buffer, battery_level, 1e3, &ind);

conf_general.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#define FW_VERSION_MAJOR 5
2525
#define FW_VERSION_MINOR 03
2626
// Set to 0 for building a release and iterate during beta test builds
27-
#define FW_TEST_VERSION_NUMBER 27
27+
#define FW_TEST_VERSION_NUMBER 28
2828

2929
#include "datatypes.h"
3030

libcanard/canard_driver.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ void canard_driver_init(void) {
336336
static void calculateTotalCurrent(void) {
337337
// Calculate total current being consumed by the ESCs on the system
338338
float totalCurrent = mc_interface_get_tot_current();
339-
float avgVoltage = GET_INPUT_VOLTAGE();
339+
float avgVoltage = mc_interface_get_input_voltage_filtered();
340340
float totalSysCurrent = 0;
341341

342342
uint8_t escTotal = 1;
@@ -409,7 +409,7 @@ static void sendEscStatus(void) {
409409
mc_interface_get_configuration()->l_current_max_scale) * 100.0;
410410
status.rpm = mc_interface_get_rpm();
411411
status.temperature = mc_interface_temp_fet_filtered() + 273.15;
412-
status.voltage = GET_INPUT_VOLTAGE();
412+
status.voltage = mc_interface_get_input_voltage_filtered();
413413

414414
uavcan_equipment_esc_Status_encode(&status, buffer);
415415

mc_interface.c

+11-2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ typedef struct {
8383
float m_motor_current_unbalance;
8484
float m_motor_current_unbalance_error_rate;
8585
float m_f_samp_now;
86+
float m_input_voltage_filtered;
87+
float m_input_voltage_filtered_slower;
8688

8789
// Backup data counters
8890
uint64_t m_odometer_last;
@@ -1092,6 +1094,10 @@ float mc_interface_get_tot_current_in_filtered(void) {
10921094
return ret;
10931095
}
10941096

1097+
float mc_interface_get_input_voltage_filtered(void) {
1098+
return motor_now()->m_input_voltage_filtered;
1099+
}
1100+
10951101
float mc_interface_get_abs_motor_current_unbalance(void) {
10961102
float ret = 0.0;
10971103

@@ -1353,7 +1359,7 @@ float mc_interface_temp_motor_filtered(void) {
13531359
*/
13541360
float mc_interface_get_battery_level(float *wh_left) {
13551361
const volatile mc_configuration *conf = mc_interface_get_configuration();
1356-
const float v_in = GET_INPUT_VOLTAGE();
1362+
const float v_in = motor_now()->m_input_voltage_filtered_slower;
13571363
float battery_avg_voltage = 0.0;
13581364
float battery_avg_voltage_left = 0.0;
13591365
float ah_left = 0;
@@ -1551,6 +1557,7 @@ void mc_interface_mc_timer_isr(bool is_second_motor) {
15511557

15521558
volatile mc_configuration *conf_now = &motor->m_conf;
15531559
const float input_voltage = GET_INPUT_VOLTAGE();
1560+
UTILS_LP_FAST(motor->m_input_voltage_filtered, input_voltage, 0.02);
15541561

15551562
// Check for faults that should stop the motor
15561563
static int wrong_voltage_iterations = 0;
@@ -1865,7 +1872,7 @@ void mc_interface_adc_inj_int_handler(void) {
18651872
static void update_override_limits(volatile motor_if_state_t *motor, volatile mc_configuration *conf) {
18661873
bool is_motor_1 = motor == &m_motor_1;
18671874

1868-
const float v_in = GET_INPUT_VOLTAGE();
1875+
const float v_in = motor->m_input_voltage_filtered;
18691876
float rpm_now = 0.0;
18701877

18711878
if (motor->m_conf.motor_type == MOTOR_TYPE_FOC) {
@@ -2118,6 +2125,8 @@ static void run_timer_tasks(volatile motor_if_state_t *motor) {
21182125
bool is_motor_1 = motor == &m_motor_1;
21192126
mc_interface_select_motor_thread(is_motor_1 ? 1 : 2);
21202127

2128+
UTILS_LP_FAST(motor->m_input_voltage_filtered_slower, motor->m_input_voltage_filtered, 0.01);
2129+
21212130
// Update backup data (for motor 1 only)
21222131
if (is_motor_1) {
21232132
uint64_t odometer = mc_interface_get_distance_abs();

mc_interface.h

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ float mc_interface_get_tot_current_directional(void);
6565
float mc_interface_get_tot_current_directional_filtered(void);
6666
float mc_interface_get_tot_current_in(void);
6767
float mc_interface_get_tot_current_in_filtered(void);
68+
float mc_interface_get_input_voltage_filtered(void);
6869
int mc_interface_get_tachometer_value(bool reset);
6970
int mc_interface_get_tachometer_abs_value(bool reset);
7071
float mc_interface_get_last_inj_adc_isr_duration(void);

mcpwm.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ mc_state mcpwm_get_state(void) {
746746
* The KV value.
747747
*/
748748
float mcpwm_get_kv(void) {
749-
return rpm_now / (GET_INPUT_VOLTAGE() * fabsf(dutycycle_now));
749+
return rpm_now / (mc_interface_get_input_voltage_filtered() * fabsf(dutycycle_now));
750750
}
751751

752752
/**
@@ -1204,7 +1204,7 @@ static void run_pid_control_speed(void) {
12041204
}
12051205
#else
12061206
// Compensation for supply voltage variations
1207-
float scale = 1.0 / GET_INPUT_VOLTAGE();
1207+
float scale = 1.0 / mc_interface_get_input_voltage_filtered();
12081208

12091209
// Compute parameters
12101210
p_term = error * conf->s_pid_kp * scale;

mcpwm_foc.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1761,9 +1761,9 @@ float mcpwm_foc_measure_inductance(float duty, int samples, float *curr, float *
17611761
stop_pwm_hw(motor);
17621762

17631763
motor->m_conf->foc_sensor_mode = FOC_SENSOR_MODE_HFI;
1764-
motor->m_conf->foc_hfi_voltage_start = duty * GET_INPUT_VOLTAGE() * (2.0 / 3.0);
1765-
motor->m_conf->foc_hfi_voltage_run = duty * GET_INPUT_VOLTAGE() * (2.0 / 3.0);
1766-
motor->m_conf->foc_hfi_voltage_max = duty * GET_INPUT_VOLTAGE() * (2.0 / 3.0);
1764+
motor->m_conf->foc_hfi_voltage_start = duty * mc_interface_get_input_voltage_filtered() * (2.0 / 3.0);
1765+
motor->m_conf->foc_hfi_voltage_run = duty * mc_interface_get_input_voltage_filtered() * (2.0 / 3.0);
1766+
motor->m_conf->foc_hfi_voltage_max = duty * mc_interface_get_input_voltage_filtered() * (2.0 / 3.0);
17671767
motor->m_conf->foc_sl_erpm_hfi = 20000.0;
17681768
motor->m_conf->foc_sample_v0_v7 = false;
17691769
motor->m_conf->foc_hfi_samples = HFI_SAMPLES_32;
@@ -2622,7 +2622,7 @@ void mcpwm_foc_adc_int_handler(void *p, uint32_t flags) {
26222622
// Truncating the duty cycle here would be dangerous, so run a PID controller.
26232623

26242624
// Compensation for supply voltage variations
2625-
float scale = 1.0 / GET_INPUT_VOLTAGE();
2625+
float scale = 1.0 / motor_now->m_motor_state.v_bus;
26262626

26272627
// Compute error
26282628
float error = duty_set - motor_now->m_motor_state.duty_now;

terminal.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ void terminal_process_string(char *str) {
198198
commands_printf("Current 1 sample: %u", current1_samp);
199199
commands_printf("Current 2 sample: %u\n", current2_samp);
200200
} else if (strcmp(argv[0], "volt") == 0) {
201-
commands_printf("Input voltage: %.2f\n", (double)GET_INPUT_VOLTAGE());
201+
commands_printf("Input voltage: %.2f\n", (double)mc_interface_get_input_voltage_filtered());
202202
#ifdef HW_HAS_GATE_DRIVER_SUPPLY_MONITOR
203203
commands_printf("Gate driver power supply output voltage: %.2f\n", (double)GET_GATE_DRIVER_SUPPLY_VOLTAGE());
204204
#endif

0 commit comments

Comments
 (0)