Skip to content

Commit bbd6e8b

Browse files
committed
Current and voltage limit ADC fixes. 1khz ADC reads
1 parent 644a162 commit bbd6e8b

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

Src/main.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ uint16_t ADC_raw_temp;
432432
uint16_t ADC_raw_volts;
433433
uint16_t ADC_raw_current;
434434
uint16_t ADC_raw_input;
435-
uint8_t adc_counter = 0;
435+
uint8_t PROCESS_ADC_FLAG = 0;
436436
char send_telemetry = 0;
437437
char telemetry_done = 0;
438438
char prop_brake_active = 0;
@@ -447,10 +447,10 @@ uint16_t adjusted_input = 0;
447447
#define TEMP110_CAL_VALUE ((uint16_t*)((uint32_t)0x1FFFF7C2))
448448

449449
uint16_t smoothedcurrent = 0;
450-
const uint8_t numReadings = 100; // the readings from the analog input
450+
const uint8_t numReadings = 50; // the readings from the analog input
451451
uint8_t readIndex = 0; // the index of the current reading
452452
uint32_t total = 0;
453-
uint16_t readings[100];
453+
uint16_t readings[50];
454454

455455
uint8_t bemf_timeout_happened = 0;
456456
uint8_t changeover_step = 5;
@@ -711,9 +711,9 @@ void loadEEpromSettings()
711711
use_current_limit = 1;
712712
}
713713

714-
currentPid.Kp = eepromBuffer.current_P;
714+
currentPid.Kp = eepromBuffer.current_P*2;
715715
currentPid.Ki = eepromBuffer.current_I;
716-
currentPid.Kd = eepromBuffer.current_D;
716+
currentPid.Kd = eepromBuffer.current_D*2;
717717

718718
if (eepromBuffer.sine_mode_power == 0 || eepromBuffer.sine_mode_power > 10) {
719719
eepromBuffer.sine_mode_power = 5;
@@ -1348,6 +1348,7 @@ void tenKhzRoutine()
13481348
}
13491349
#endif
13501350
if (one_khz_loop_counter > PID_LOOP_DIVIDER) { // 1khz PID loop
1351+
PROCESS_ADC_FLAG = 1; // set flag to do new adc read at lower priority
13511352
one_khz_loop_counter = 0;
13521353
if (use_current_limit && running) {
13531354
use_current_limit_adjust -= (int16_t)(doPidCalculations(&currentPid, actual_current,
@@ -1789,7 +1790,6 @@ int main(void)
17891790
#endif
17901791

17911792
while (1) {
1792-
17931793
e_com_time = ((commutation_intervals[0] + commutation_intervals[1] + commutation_intervals[2] + commutation_intervals[3] + commutation_intervals[4] + commutation_intervals[5]) + 4) >> 1; // COMMUTATION INTERVAL IS 0.5US INCREMENTS
17941794
#if defined(FIXED_DUTY_MODE) || defined(FIXED_SPEED_MODE)
17951795
setInput();
@@ -1966,8 +1966,7 @@ if(zero_crosses < 5){
19661966
send_telem_DMA(49);
19671967
send_esc_info_flag = 0;
19681968
}
1969-
adc_counter++;
1970-
if (adc_counter > 200) { // for adc and telemetry
1969+
if (PROCESS_ADC_FLAG == 1) { // for adc and telemetry set adc counter at 1khz loop rate
19711970
#if defined(STMICRO)
19721971
ADC_DMA_Callback();
19731972
LL_ADC_REG_StartConversion(ADC1);
@@ -2013,7 +2012,7 @@ if(zero_crosses < 5){
20132012
}
20142013
}
20152014
}
2016-
if (low_voltage_count > (20000 - (stepper_sine * 18000))) {
2015+
if (low_voltage_count > (10000 - (stepper_sine * 9900))) { // 10 second wait before cut-off for low voltage
20172016
LOW_VOLTAGE_CUTOFF = 1;
20182017
input = 0;
20192018
allOff();
@@ -2023,7 +2022,7 @@ if(zero_crosses < 5){
20232022
armed = 0;
20242023
}
20252024

2026-
adc_counter = 0;
2025+
PROCESS_ADC_FLAG = 0;
20272026
#ifdef USE_ADC_INPUT
20282027
if (ADC_raw_input < 10) {
20292028
zero_input_count++;

0 commit comments

Comments
 (0)