From f23c85514975109935f265c00e7319f4d43339f2 Mon Sep 17 00:00:00 2001 From: jsphuebner Date: Sat, 10 Apr 2021 23:04:41 +0200 Subject: [PATCH 1/3] Changed ADC sample time to 7.5 clocks Taking 8 samples from last PWM period instead of 3 rotor movement compensation becomes active above 10 Hz --- include/anain_prj.h | 4 ++-- include/hwdefs.h | 1 - include/param_prj.h | 12 ++++++------ libopeninv | 2 +- src/hwinit.cpp | 5 +++-- src/pwmgeneration-foc.cpp | 14 ++++++++------ 6 files changed, 20 insertions(+), 18 deletions(-) diff --git a/include/anain_prj.h b/include/anain_prj.h index c209a44..793f3f6 100644 --- a/include/anain_prj.h +++ b/include/anain_prj.h @@ -7,8 +7,8 @@ #define NUM_SAMPLES 12 #define SAMPLE_TIME ADC_SMPR_SMP_7DOT5CYC #elif CONTROL == CTRL_FOC -#define NUM_SAMPLES 3 -#define SAMPLE_TIME ADC_SMPR_SMP_1DOT5CYC +#define NUM_SAMPLES 8 +#define SAMPLE_TIME ADC_SMPR_SMP_7DOT5CYC #endif // CONTROL #define ANA_IN_LIST \ diff --git a/include/hwdefs.h b/include/hwdefs.h index b0439eb..e670881 100644 --- a/include/hwdefs.h +++ b/include/hwdefs.h @@ -9,7 +9,6 @@ #define USART_BAUDRATE 115200 //Maximum PWM frequency is 36MHz/2^MIN_PWM_DIGITS #define MIN_PWM_DIGITS 11 -#define RCC_CLOCK_SETUP rcc_clock_setup_in_hse_8mhz_out_72mhz #define PWM_TIMER TIM1 #define PWM_TIMRST RST_TIM1 diff --git a/include/param_prj.h b/include/param_prj.h index 76d306e..3fdc0d8 100644 --- a/include/param_prj.h +++ b/include/param_prj.h @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -#define VER 5.02.R +#define VER 5.03.R /* Entries must be ordered as follows: 1. Saveable parameters (id != 0) @@ -198,12 +198,12 @@ MOTOR_PARAMETERS_SINE \ MOTOR_PARAMETERS_COMMON \ INVERTER_PARAMETERS_COMMON \ - DERATE_PARAMETERS_COMMON \ - DERATE_PARAMETERS_SINE \ - CHARGER_PARAMETERS \ THROTTLE_PARAMETERS_COMMON \ THROTTLE_PARAMETERS_SINE \ REGEN_PARAMETERS \ + DERATE_PARAMETERS_COMMON \ + DERATE_PARAMETERS_SINE \ + CHARGER_PARAMETERS \ AUTOMATION_CONTACT_PWM_COMM_PARAMETERS \ PARAM_ENTRY(CAT_TEST, fslipspnt, "Hz", -100, 1000, 0, 0 ) \ PARAM_ENTRY(CAT_TEST, ampnom, "%", 0, 100, 0, 0 ) \ @@ -218,11 +218,11 @@ MOTOR_PARAMETERS_COMMON \ INVERTER_PARAMETERS_COMMON \ INVERTER_PARAMETERS_FOC \ - DERATE_PARAMETERS_COMMON \ - CHARGER_PARAMETERS \ THROTTLE_PARAMETERS_COMMON \ THROTTLE_PARAMETERS_FOC \ REGEN_PARAMETERS \ + DERATE_PARAMETERS_COMMON \ + CHARGER_PARAMETERS \ AUTOMATION_CONTACT_PWM_COMM_PARAMETERS \ PARAM_ENTRY(CAT_TEST, manualiq, "A", -400, 400, 0, 0 ) \ PARAM_ENTRY(CAT_TEST, manualid, "A", -400, 400, 0, 0 ) \ diff --git a/libopeninv b/libopeninv index aba4a4e..9e90671 160000 --- a/libopeninv +++ b/libopeninv @@ -1 +1 @@ -Subproject commit aba4a4e9fe91d68d817a4a662aa3ed292d3e45b1 +Subproject commit 9e906713af796cbfbadf256ee8bc5f6ba166196d diff --git a/src/hwinit.cpp b/src/hwinit.cpp index cc7147e..e5bc71a 100644 --- a/src/hwinit.cpp +++ b/src/hwinit.cpp @@ -42,8 +42,9 @@ */ void clock_setup(void) { - RCC_CLOCK_SETUP(); + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + //Run ADC at 12 MHz rcc_set_adcpre(RCC_CFGR_ADCPRE_PCLK2_DIV6); //The reset value for PRIGROUP (=0) is not actually a defined @@ -59,7 +60,7 @@ void clock_setup(void) rcc_periph_clock_enable(RCC_TIM2); //Scheduler, over current on blue pill rcc_periph_clock_enable(RCC_TIM3); //Rotor Encoder rcc_periph_clock_enable(RCC_TIM4); //Overcurrent / AUX PWM, scheduler on blue pill - rcc_periph_clock_enable(RCC_DMA1); //ADC, Encoder and UART receive + rcc_periph_clock_enable(RCC_DMA1); //ADC, Encoder and UART3 rcc_periph_clock_enable(RCC_ADC1); rcc_periph_clock_enable(RCC_CRC); rcc_periph_clock_enable(RCC_AFIO); //CAN diff --git a/src/pwmgeneration-foc.cpp b/src/pwmgeneration-foc.cpp index aa6a7dc..b00c899 100644 --- a/src/pwmgeneration-foc.cpp +++ b/src/pwmgeneration-foc.cpp @@ -56,6 +56,7 @@ void PwmGeneration::Run() Encoder::UpdateRotorAngle(dir); CalcNextAngleSync(dir); + FOC::SetAngle(angle); frqFiltered = IIRFILTER(frqFiltered, frq, 8); int moddedKi = curki + kifrqgain * FP_TOINT(frqFiltered); @@ -68,7 +69,7 @@ void PwmGeneration::Run() if (opmode == MOD_RUN && initwait == 0) { - s32fp fwIdRef = idref <= 0 ? fwController.Run(iq) : 0; + s32fp fwIdRef = idref <= 0 ? fwController.RunProportionalOnly(iq) : 0; dController.SetRef(idref + fwIdRef); } else if (opmode == MOD_MANUAL) @@ -82,7 +83,7 @@ void PwmGeneration::Run() int32_t qlimit = FOC::GetQLimit(ud); qController.SetMinMaxY(-qlimit, qlimit); int32_t uq = qController.Run(iq); - FOC::InvParkClarke(ud, uq, angle); + FOC::InvParkClarke(ud, uq); s32fp idc = (iq * uq + id * ud) / FOC::GetMaximumModulationIndex(); idc = FP_MUL(idc, dcCurFac); @@ -229,7 +230,6 @@ void PwmGeneration::PwmInit() s32fp PwmGeneration::ProcessCurrents(s32fp& id, s32fp& iq) { - if (initwait > 0) { initwait--; @@ -239,9 +239,9 @@ s32fp PwmGeneration::ProcessCurrents(s32fp& id, s32fp& iq) s32fp il2 = GetCurrent(AnaIn::il2, ilofs[1], Param::Get(Param::il2gain)); if ((Param::GetInt(Param::pinswap) & SWAP_CURRENTS) > 0) - FOC::ParkClarke(il2, il1, angle); + FOC::ParkClarke(il2, il1); else - FOC::ParkClarke(il1, il2, angle); + FOC::ParkClarke(il1, il2); id = FOC::id; iq = FOC::iq; @@ -259,9 +259,11 @@ void PwmGeneration::CalcNextAngleSync(int dir) { uint16_t syncOfs = Param::GetInt(Param::syncofs); uint16_t rotorAngle = Encoder::GetRotorAngle(); + int syncadv = (frq - FP_FROMINT(10)) * 10; + syncadv = MAX(0, syncadv); //Compensate rotor movement that happened between sampling and processing - syncOfs += FP_TOINT(dir * frq * 10); + syncOfs += FP_TOINT(dir * syncadv); angle = polePairRatio * rotorAngle + syncOfs; frq = polePairRatio * Encoder::GetRotorFrequency(); From 393d619c1750dd999fd73b44bd8e2641a4e6e79f Mon Sep 17 00:00:00 2001 From: jsphuebner Date: Wed, 21 Apr 2021 17:55:56 +0200 Subject: [PATCH 2/3] Auto-detect flash size and write parameters to according address Correction in start sequence --- include/hwdefs.h | 5 +---- libopencm3 | 2 +- libopeninv | 2 +- src/stm32_sine.cpp | 1 + 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/include/hwdefs.h b/include/hwdefs.h index e670881..62d0bd1 100644 --- a/include/hwdefs.h +++ b/include/hwdefs.h @@ -24,10 +24,7 @@ #define OVER_CUR_POS hwRev == HW_BLUEPILL ? TIM_OC2 : TIM_OC3 //Address of parameter block in flash -#define FLASH_PAGE_SIZE 1024 -#define PARAM_ADDRESS 0x0801FC00 -#define PARAM_BLKSIZE FLASH_PAGE_SIZE -#define CANMAP_ADDRESS 0x0801F800 +#define PARAM_BLKSIZE 1024 #define REV_CNT_IC hwRev == HW_REV1 ? TIM_IC3 : TIM_IC1 #define REV_CNT_OC hwRev == HW_REV1 ? TIM_OC3 : TIM_OC1 diff --git a/libopencm3 b/libopencm3 index 4424e4a..54631b9 160000 --- a/libopencm3 +++ b/libopencm3 @@ -1 +1 @@ -Subproject commit 4424e4a528a9131323d4d38dd6450c58814603cb +Subproject commit 54631b91ad15417e8a2dba98d4c564f0937643de diff --git a/libopeninv b/libopeninv index 9e90671..463cc02 160000 --- a/libopeninv +++ b/libopeninv @@ -1 +1 @@ -Subproject commit 9e906713af796cbfbadf256ee8bc5f6ba166196d +Subproject commit 463cc0216886ae02f4fdbcadab07b4eee3037a60 diff --git a/src/stm32_sine.cpp b/src/stm32_sine.cpp index 5b33968..3cfa511 100644 --- a/src/stm32_sine.cpp +++ b/src/stm32_sine.cpp @@ -180,6 +180,7 @@ static void Ms10Task(void) if (newMode != MOD_OFF) { + opmode = newMode; DigIo::dcsw_out.Set(); DigIo::err_out.Clear(); DigIo::prec_out.Clear(); From fc2c185c2e314210fa04330ec539dcb79fa592c4 Mon Sep 17 00:00:00 2001 From: jsphuebner Date: Tue, 4 May 2021 14:22:16 +0200 Subject: [PATCH 3/3] Added idc filter again --- include/param_prj.h | 5 +++-- libopeninv | 2 +- src/pwmgeneration-foc.cpp | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/param_prj.h b/include/param_prj.h index 3fdc0d8..b9a247b 100644 --- a/include/param_prj.h +++ b/include/param_prj.h @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -#define VER 5.03.R +#define VER 5.05.R /* Entries must be ordered as follows: 1. Saveable parameters (id != 0) @@ -78,6 +78,7 @@ PARAM_ENTRY(CAT_DERATE, idcmax, "A", 0, 5000, 5000, 96 ) \ PARAM_ENTRY(CAT_DERATE, idcmin, "A", -5000, 0, -5000, 98 ) \ PARAM_ENTRY(CAT_DERATE, idckp, "dig", 0.1, 20, 2, 130 ) \ + PARAM_ENTRY(CAT_DERATE, idcflt, "dig", 0, 11, 9, 132 ) \ PARAM_ENTRY(CAT_DERATE, tmphsmax, "°C", 50, 150, 85, 125 ) \ PARAM_ENTRY(CAT_DERATE, tmpmmax, "°C", 70, 300, 300, 127 ) \ PARAM_ENTRY(CAT_DERATE, throtmax, "%", 0, 100, 100, 97 ) \ @@ -176,7 +177,7 @@ VALUE_ENTRY(din_ocur, OKERR, 2030 ) \ VALUE_ENTRY(din_desat, OKERR, 2031 ) \ VALUE_ENTRY(din_bms, ONOFF, 2032 ) \ - VALUE_ENTRY(cpuload, "%", 2035 ) + VALUE_ENTRY(cpuload, "%", 2035 ) \ #define VALUES_SINE \ VALUE_ENTRY(ilmax, "A", 2005 ) \ diff --git a/libopeninv b/libopeninv index 463cc02..f089afe 160000 --- a/libopeninv +++ b/libopeninv @@ -1 +1 @@ -Subproject commit 463cc0216886ae02f4fdbcadab07b4eee3037a60 +Subproject commit f089afee699612993410acb4b2e161837bdccf1c diff --git a/src/pwmgeneration-foc.cpp b/src/pwmgeneration-foc.cpp index b00c899..fe75ff6 100644 --- a/src/pwmgeneration-foc.cpp +++ b/src/pwmgeneration-foc.cpp @@ -87,7 +87,7 @@ void PwmGeneration::Run() s32fp idc = (iq * uq + id * ud) / FOC::GetMaximumModulationIndex(); idc = FP_MUL(idc, dcCurFac); - idcFiltered = IIRFILTER(idcFiltered, idc, 10); + idcFiltered = IIRFILTER(idcFiltered, idc, Param::GetInt(Param::idcflt)); Param::SetFlt(Param::fstat, frq); Param::SetFlt(Param::angle, DIGIT_TO_DEGREE(angle));