From 91f7b54cffd55cadeecea1aca239f5988cac8fa2 Mon Sep 17 00:00:00 2001 From: Rampagy Date: Mon, 16 Jan 2023 08:25:29 -0600 Subject: [PATCH 01/16] Adding null=v0 --- motor/foc_math.c | 171 ++++++++++++++++++---------------------------- util/utils_math.h | 2 + 2 files changed, 68 insertions(+), 105 deletions(-) diff --git a/motor/foc_math.c b/motor/foc_math.c index 7367ac179..0a392d95c 100644 --- a/motor/foc_math.c +++ b/motor/foc_math.c @@ -193,9 +193,14 @@ void foc_pll_run(float phase, float dt, float *phase_var, *speed_var += conf->foc_pll_ki * delta_theta * dt; } +//#define USE_ONBOARD_DSP /** * @brief svm Space vector modulation. Magnitude must not be larger than sqrt(3)/2, or 0.866 to avoid overmodulation. * See https://github.com/vedderb/bldc/pull/372#issuecomment-962499623 for a full description. + * Null=V0 variation (reduces switching losses by up to 33%) + * See the follwoing links for a full description: + * https://youtu.be/5eQyoVMz1dY + * https://go.gale.com/ps/i.do?id=GALE%7CA18320578&sid=sitemap&v=2.1&it=r&p=AONE&sw=w&userGroupName=anon%7E469317a7 * @param alpha voltage * @param beta Park transformed and normalized voltage * @param PWMFullDutyCycle is the peak value of the PWM counter. @@ -205,128 +210,84 @@ void foc_pll_run(float phase, float dt, float *phase_var, */ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, uint32_t* tAout, uint32_t* tBout, uint32_t* tCout, uint32_t *svm_sector) { - uint32_t sector; - - if (beta >= 0.0f) { - if (alpha >= 0.0f) { - //quadrant I - if (ONE_BY_SQRT3 * beta > alpha) { - sector = 2; - } else { - sector = 1; - } - } else { - //quadrant II - if (-ONE_BY_SQRT3 * beta > alpha) { - sector = 3; - } else { - sector = 2; - } - } - } else { - if (alpha >= 0.0f) { - //quadrant IV5 - if (-ONE_BY_SQRT3 * beta > alpha) { - sector = 5; - } else { - sector = 6; - } - } else { - //quadrant III - if (ONE_BY_SQRT3 * beta > alpha) { - sector = 4; - } else { - sector = 5; - } - } - } - - // PWM timings - uint32_t tA, tB, tC; - - switch (sector) { - // sector 1-2 + uint32_t sector; + float angle, T1, T2, magnitude, temp; + uint32_t tA, tB, tC; // PWM timings + + // get angle +#ifdef USE_ONBOARD_DSP + // requires at least v1.10.0 of the cmsis lib + // https://github.com/ARM-software/CMSIS-DSP/releases + (void)arm_atan2_f32(beta, alpha, &angle); +#else + angle = atan2f(beta, alpha); // from math.h +#endif + + // convert from (-pi, pi) to (0, 2*pi) via branchless programming + angle = angle + (angle < 0.0f)*SIX_PI_OVER_3; + sector = ((uint32_t)(angle / PI_OVER_3) % 6u) + 1u; + angle = fmodf(angle, PI_OVER_3); // from math.h + + // determine magnitude +#ifdef USE_ONBOARD_DSP + (void)arm_sqrt_f32(SQ(alpha)+SQ(beta), &magnitude); // using onboard dsp +#else + magnitude = hypotf(alpha, beta); // from math.h +#endif + + // temporary variable for intermediate math + temp = (float)PWMFullDutyCycle * magnitude * TWO_BY_SQRT3; + +#ifdef USE_ONBOARD_DSP + T1 = temp * arm_sin_f32(PI_OVER_3 - angle); // using arm dsp + T2 = temp * arm_sin_f32(angle); // using arm dsp +#else + T1 = temp * sinf(PI_OVER_3 - angle); // from math.h + T2 = temp * sinf(angle); // from math.h +#endif + + + switch(sector) { case 1: { - // Vector on-times - uint32_t t1 = (alpha - ONE_BY_SQRT3 * beta) * PWMFullDutyCycle; - uint32_t t2 = (TWO_BY_SQRT3 * beta) * PWMFullDutyCycle; - - // PWM timings - tA = (PWMFullDutyCycle + t1 + t2) / 2; - tB = tA - t1; - tC = tB - t2; - + tA = (uint32_t)(T1+T2); + tB = (uint32_t)(T2); + tC = (uint32_t)(0); break; } - - // sector 2-3 + case 2: { - // Vector on-times - uint32_t t2 = (alpha + ONE_BY_SQRT3 * beta) * PWMFullDutyCycle; - uint32_t t3 = (-alpha + ONE_BY_SQRT3 * beta) * PWMFullDutyCycle; - - // PWM timings - tB = (PWMFullDutyCycle + t2 + t3) / 2; - tA = tB - t3; - tC = tA - t2; - + tA = (uint32_t)(T1); + tB = (uint32_t)(T1+T2); + tC = (uint32_t)(0); break; } - - // sector 3-4 + case 3: { - // Vector on-times - uint32_t t3 = (TWO_BY_SQRT3 * beta) * PWMFullDutyCycle; - uint32_t t4 = (-alpha - ONE_BY_SQRT3 * beta) * PWMFullDutyCycle; - - // PWM timings - tB = (PWMFullDutyCycle + t3 + t4) / 2; - tC = tB - t3; - tA = tC - t4; - + tA = (uint32_t)(0); + tB = (uint32_t)(T1+T2); + tC = (uint32_t)(T2); break; } - - // sector 4-5 + case 4: { - // Vector on-times - uint32_t t4 = (-alpha + ONE_BY_SQRT3 * beta) * PWMFullDutyCycle; - uint32_t t5 = (-TWO_BY_SQRT3 * beta) * PWMFullDutyCycle; - - // PWM timings - tC = (PWMFullDutyCycle + t4 + t5) / 2; - tB = tC - t5; - tA = tB - t4; - + tA = (uint32_t)(0); + tB = (uint32_t)(T1); + tC = (uint32_t)(T1+T2); break; } - - // sector 5-6 + case 5: { - // Vector on-times - uint32_t t5 = (-alpha - ONE_BY_SQRT3 * beta) * PWMFullDutyCycle; - uint32_t t6 = (alpha - ONE_BY_SQRT3 * beta) * PWMFullDutyCycle; - - // PWM timings - tC = (PWMFullDutyCycle + t5 + t6) / 2; - tA = tC - t5; - tB = tA - t6; - + tA = (uint32_t)(T2); + tB = (uint32_t)(0); + tC = (uint32_t)(T1+T2); break; } - - // sector 6-1 + case 6: { - // Vector on-times - uint32_t t6 = (-TWO_BY_SQRT3 * beta) * PWMFullDutyCycle; - uint32_t t1 = (alpha + ONE_BY_SQRT3 * beta) * PWMFullDutyCycle; - - // PWM timings - tA = (PWMFullDutyCycle + t6 + t1) / 2; - tC = tA - t1; - tB = tC - t6; - + tA = (uint32_t)(T1+T2); + tB = (uint32_t)(0); + tC = (uint32_t)(T1); break; } } diff --git a/util/utils_math.h b/util/utils_math.h index 19c3c4661..0312b5b62 100644 --- a/util/utils_math.h +++ b/util/utils_math.h @@ -119,6 +119,8 @@ void utils_rotate_vector3(float *input, float *rotation, float *output, bool rev #define COS_MINUS_30_DEG (0.86602540378) #define SIN_MINUS_30_DEG (-0.5) #define ONE_BY_SQRT2 (0.7071067811865475) +#define PI_OVER_3 (1.0471975511965977f) // 60 degrees +#define SIX_PI_OVER_3 (6.0f*PI_OVER_3) // 360 degrees // Tables extern const float utils_tab_sin_32_1[]; From c4e145dd54c93cc99aa98ed1157699ad837ee566 Mon Sep 17 00:00:00 2001 From: Rampagy Date: Mon, 16 Jan 2023 09:41:44 -0600 Subject: [PATCH 02/16] Updating foc_svm --- motor/foc_math.c | 27 ++++++++++++++------------- motor/foc_math.h | 1 + 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/motor/foc_math.c b/motor/foc_math.c index 0a392d95c..6bbe722b2 100644 --- a/motor/foc_math.c +++ b/motor/foc_math.c @@ -193,7 +193,6 @@ void foc_pll_run(float phase, float dt, float *phase_var, *speed_var += conf->foc_pll_ki * delta_theta * dt; } -//#define USE_ONBOARD_DSP /** * @brief svm Space vector modulation. Magnitude must not be larger than sqrt(3)/2, or 0.866 to avoid overmodulation. * See https://github.com/vedderb/bldc/pull/372#issuecomment-962499623 for a full description. @@ -216,12 +215,12 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, uint32_t tA, tB, tC; // PWM timings // get angle -#ifdef USE_ONBOARD_DSP - // requires at least v1.10.0 of the cmsis lib +#ifdef USE_MATH_H + angle = atan2f(beta, alpha); +#else + // using onboard dsp (requires at least v1.10.0 of the cmsis lib) // https://github.com/ARM-software/CMSIS-DSP/releases (void)arm_atan2_f32(beta, alpha, &angle); -#else - angle = atan2f(beta, alpha); // from math.h #endif // convert from (-pi, pi) to (0, 2*pi) via branchless programming @@ -230,21 +229,23 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, angle = fmodf(angle, PI_OVER_3); // from math.h // determine magnitude -#ifdef USE_ONBOARD_DSP - (void)arm_sqrt_f32(SQ(alpha)+SQ(beta), &magnitude); // using onboard dsp +#ifdef USE_MATH_H + magnitude = hypotf(alpha, beta); #else - magnitude = hypotf(alpha, beta); // from math.h + // using onboard dsp + (void)arm_sqrt_f32(SQ(alpha)+SQ(beta), &magnitude); #endif // temporary variable for intermediate math temp = (float)PWMFullDutyCycle * magnitude * TWO_BY_SQRT3; -#ifdef USE_ONBOARD_DSP - T1 = temp * arm_sin_f32(PI_OVER_3 - angle); // using arm dsp - T2 = temp * arm_sin_f32(angle); // using arm dsp +#ifdef USE_MATH_H + T1 = temp * sinf(PI_OVER_3 - angle); + T2 = temp * sinf(angle); #else - T1 = temp * sinf(PI_OVER_3 - angle); // from math.h - T2 = temp * sinf(angle); // from math.h + // using onboard dsp + T1 = temp * arm_sin_f32(PI_OVER_3 - angle); + T2 = temp * arm_sin_f32(angle); #endif diff --git a/motor/foc_math.h b/motor/foc_math.h index a2210829a..e7d3cd08e 100644 --- a/motor/foc_math.h +++ b/motor/foc_math.h @@ -20,6 +20,7 @@ #ifndef FOC_MATH_H_ #define FOC_MATH_H_ +#include #include "datatypes.h" // Types From ceb64076fb5d0bf88afddc819836cb56be00c1c5 Mon Sep 17 00:00:00 2001 From: Rampagy Date: Mon, 16 Jan 2023 09:47:01 -0600 Subject: [PATCH 03/16] Adding define --- motor/foc_math.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/motor/foc_math.c b/motor/foc_math.c index 6bbe722b2..7e543dad9 100644 --- a/motor/foc_math.c +++ b/motor/foc_math.c @@ -207,6 +207,7 @@ void foc_pll_run(float phase, float dt, float *phase_var, * @param tBout PWM duty cycle phase B * @param tCout PWM duty cycle phase C */ +#define USE_MATH_H void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, uint32_t* tAout, uint32_t* tBout, uint32_t* tCout, uint32_t *svm_sector) { @@ -220,6 +221,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, #else // using onboard dsp (requires at least v1.10.0 of the cmsis lib) // https://github.com/ARM-software/CMSIS-DSP/releases + // (void)arm_atan2_f32(beta, alpha, &angle); #endif From 23ab62e8d56e07c70bfc0796bc4894a6691266a6 Mon Sep 17 00:00:00 2001 From: Rampagy Date: Mon, 16 Jan 2023 09:52:22 -0600 Subject: [PATCH 04/16] Wrapping arm_math with #ifndef --- motor/foc_math.c | 1 - motor/foc_math.h | 6 ++++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/motor/foc_math.c b/motor/foc_math.c index 7e543dad9..1049067a9 100644 --- a/motor/foc_math.c +++ b/motor/foc_math.c @@ -207,7 +207,6 @@ void foc_pll_run(float phase, float dt, float *phase_var, * @param tBout PWM duty cycle phase B * @param tCout PWM duty cycle phase C */ -#define USE_MATH_H void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, uint32_t* tAout, uint32_t* tBout, uint32_t* tCout, uint32_t *svm_sector) { diff --git a/motor/foc_math.h b/motor/foc_math.h index e7d3cd08e..4ebc7e97a 100644 --- a/motor/foc_math.h +++ b/motor/foc_math.h @@ -20,7 +20,13 @@ #ifndef FOC_MATH_H_ #define FOC_MATH_H_ +// comment this to enable usage of the onboard dsp +#define USE_MATH_H + +#ifndef USE_MATH_H #include +#endif + #include "datatypes.h" // Types From 4c228eaa8a8b4b93bccee1b8e3542ee15206d764 Mon Sep 17 00:00:00 2001 From: Alex <32419459+Rampagy@users.noreply.github.com> Date: Mon, 16 Jan 2023 11:54:30 -0600 Subject: [PATCH 05/16] Updating gale link Co-authored-by: kalvdans --- motor/foc_math.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/motor/foc_math.c b/motor/foc_math.c index 1049067a9..6b04885a8 100644 --- a/motor/foc_math.c +++ b/motor/foc_math.c @@ -199,7 +199,7 @@ void foc_pll_run(float phase, float dt, float *phase_var, * Null=V0 variation (reduces switching losses by up to 33%) * See the follwoing links for a full description: * https://youtu.be/5eQyoVMz1dY - * https://go.gale.com/ps/i.do?id=GALE%7CA18320578&sid=sitemap&v=2.1&it=r&p=AONE&sw=w&userGroupName=anon%7E469317a7 + * https://link.gale.com/apps/doc/A18320578/AONE * @param alpha voltage * @param beta Park transformed and normalized voltage * @param PWMFullDutyCycle is the peak value of the PWM counter. From b1b2325d4b9ad065506fb2e9677ce4e9d32659af Mon Sep 17 00:00:00 2001 From: Rampagy Date: Mon, 16 Jan 2023 12:06:17 -0600 Subject: [PATCH 06/16] renaming define --- motor/foc_math.c | 3 +-- util/utils_math.h | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/motor/foc_math.c b/motor/foc_math.c index 1049067a9..bb95e44e9 100644 --- a/motor/foc_math.c +++ b/motor/foc_math.c @@ -220,12 +220,11 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, #else // using onboard dsp (requires at least v1.10.0 of the cmsis lib) // https://github.com/ARM-software/CMSIS-DSP/releases - // (void)arm_atan2_f32(beta, alpha, &angle); #endif // convert from (-pi, pi) to (0, 2*pi) via branchless programming - angle = angle + (angle < 0.0f)*SIX_PI_OVER_3; + angle = angle + (angle < 0.0f)*TWO_PI; sector = ((uint32_t)(angle / PI_OVER_3) % 6u) + 1u; angle = fmodf(angle, PI_OVER_3); // from math.h diff --git a/util/utils_math.h b/util/utils_math.h index 0312b5b62..cc5065334 100644 --- a/util/utils_math.h +++ b/util/utils_math.h @@ -120,7 +120,7 @@ void utils_rotate_vector3(float *input, float *rotation, float *output, bool rev #define SIN_MINUS_30_DEG (-0.5) #define ONE_BY_SQRT2 (0.7071067811865475) #define PI_OVER_3 (1.0471975511965977f) // 60 degrees -#define SIX_PI_OVER_3 (6.0f*PI_OVER_3) // 360 degrees +#define TWO_PI (6.0f*PI_OVER_3) // 360 degrees // Tables extern const float utils_tab_sin_32_1[]; From 66d7a81b94b33663fa214fd37965d80dc085c620 Mon Sep 17 00:00:00 2001 From: Rampagy Date: Mon, 16 Jan 2023 21:14:57 -0600 Subject: [PATCH 07/16] Redoing the Null=V0 implementation --- motor/foc_math.c | 84 +++++++++++++++++++++++------------------------ motor/foc_math.h | 7 ---- util/utils_math.h | 2 -- 3 files changed, 41 insertions(+), 52 deletions(-) diff --git a/motor/foc_math.c b/motor/foc_math.c index b92f7d21e..06b358524 100644 --- a/motor/foc_math.c +++ b/motor/foc_math.c @@ -210,82 +210,80 @@ void foc_pll_run(float phase, float dt, float *phase_var, void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, uint32_t* tAout, uint32_t* tBout, uint32_t* tCout, uint32_t *svm_sector) { - uint32_t sector; - float angle, T1, T2, magnitude, temp; - uint32_t tA, tB, tC; // PWM timings - - // get angle -#ifdef USE_MATH_H - angle = atan2f(beta, alpha); -#else - // using onboard dsp (requires at least v1.10.0 of the cmsis lib) - // https://github.com/ARM-software/CMSIS-DSP/releases - (void)arm_atan2_f32(beta, alpha, &angle); -#endif - - // convert from (-pi, pi) to (0, 2*pi) via branchless programming - angle = angle + (angle < 0.0f)*TWO_PI; - sector = ((uint32_t)(angle / PI_OVER_3) % 6u) + 1u; - angle = fmodf(angle, PI_OVER_3); // from math.h - - // determine magnitude -#ifdef USE_MATH_H - magnitude = hypotf(alpha, beta); -#else - // using onboard dsp - (void)arm_sqrt_f32(SQ(alpha)+SQ(beta), &magnitude); -#endif - - // temporary variable for intermediate math - temp = (float)PWMFullDutyCycle * magnitude * TWO_BY_SQRT3; - -#ifdef USE_MATH_H - T1 = temp * sinf(PI_OVER_3 - angle); - T2 = temp * sinf(angle); -#else - // using onboard dsp - T1 = temp * arm_sin_f32(PI_OVER_3 - angle); - T2 = temp * arm_sin_f32(angle); -#endif + uint8_t sector; + uint32_t tA, tB, tC, N; // PWM timings + float T1, T2, a, b, c, temp = 0.0f; + const uint8_t sector_LUT[6] = {6u, 2u, 1u, 4u, 5u, 3u}; + a = SQRT3_BY_2*alpha - 0.5f*beta; + b = beta; + + temp = (float)PWMFullDutyCycle/SQRT3_BY_2; + + a *= temp; + b *= temp; + c = -1.0f*(a+b); // kirchhoff current law (current in = current out) + + + N = (((int32_t)a)>=0) + 2u*(((int32_t)b) >= 0) + 4u*(((int32_t)c) >= 0); + sector = sector_LUT[N-1]; + switch(sector) { case 1: { + T1 = a; + T2 = b; + tA = (uint32_t)(T1+T2); tB = (uint32_t)(T2); tC = (uint32_t)(0); break; } - + case 2: { + T1 = -c; + T2 = -a; + tA = (uint32_t)(T1); tB = (uint32_t)(T1+T2); tC = (uint32_t)(0); break; } - + case 3: { + T1 = b; + T2 = c; + tA = (uint32_t)(0); tB = (uint32_t)(T1+T2); tC = (uint32_t)(T2); break; } - + case 4: { + T1 = -a; + T2 = -b; + tA = (uint32_t)(0); tB = (uint32_t)(T1); tC = (uint32_t)(T1+T2); break; } - + case 5: { + T1 = c; + T2 = a; + tA = (uint32_t)(T2); tB = (uint32_t)(0); tC = (uint32_t)(T1+T2); break; } - + case 6: { + T1 = -b; + T2 = -c; + tA = (uint32_t)(T1+T2); tB = (uint32_t)(0); tC = (uint32_t)(T1); @@ -296,7 +294,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, *tAout = tA; *tBout = tB; *tCout = tC; - *svm_sector = sector; + *svm_sector = (uint32_t)sector; } void foc_run_pid_control_pos(bool index_found, float dt, motor_all_state_t *motor) { diff --git a/motor/foc_math.h b/motor/foc_math.h index 4ebc7e97a..a2210829a 100644 --- a/motor/foc_math.h +++ b/motor/foc_math.h @@ -20,13 +20,6 @@ #ifndef FOC_MATH_H_ #define FOC_MATH_H_ -// comment this to enable usage of the onboard dsp -#define USE_MATH_H - -#ifndef USE_MATH_H -#include -#endif - #include "datatypes.h" // Types diff --git a/util/utils_math.h b/util/utils_math.h index cc5065334..19c3c4661 100644 --- a/util/utils_math.h +++ b/util/utils_math.h @@ -119,8 +119,6 @@ void utils_rotate_vector3(float *input, float *rotation, float *output, bool rev #define COS_MINUS_30_DEG (0.86602540378) #define SIN_MINUS_30_DEG (-0.5) #define ONE_BY_SQRT2 (0.7071067811865475) -#define PI_OVER_3 (1.0471975511965977f) // 60 degrees -#define TWO_PI (6.0f*PI_OVER_3) // 360 degrees // Tables extern const float utils_tab_sin_32_1[]; From 53fb62d5ccb68632fbe85b1e38539418a6b05165 Mon Sep 17 00:00:00 2001 From: Rampagy Date: Tue, 17 Jan 2023 22:12:08 -0600 Subject: [PATCH 08/16] Adding additional foc methods --- datatypes.h | 8 +++ motor/foc_math.c | 177 ++++++++++++++++++++++++++++++++++++++-------- motor/foc_math.h | 3 +- motor/mcpwm_foc.c | 5 +- 4 files changed, 160 insertions(+), 33 deletions(-) diff --git a/datatypes.h b/datatypes.h index 5fec09244..4a19a4363 100644 --- a/datatypes.h +++ b/datatypes.h @@ -25,6 +25,14 @@ #include "ch.h" // Data types +typedef enum { + SVM_ARS = 0, // default (previous implementation) + NULL_V0, // flat bottom + NULL_V7, // flat top + V7_ODD_V0_EVEN, // v7 in [1,3,5] v0 in [2,4,6] + V0_ODD_V7_EVEN, // v0 in [1,3,5] v7 in [2,4,6] +} commutation_technique_t; + typedef enum { HW_TYPE_VESC = 0, HW_TYPE_VESC_BMS, diff --git a/motor/foc_math.c b/motor/foc_math.c index 06b358524..b9c450df3 100644 --- a/motor/foc_math.c +++ b/motor/foc_math.c @@ -208,13 +208,11 @@ void foc_pll_run(float phase, float dt, float *phase_var, * @param tCout PWM duty cycle phase C */ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, - uint32_t* tAout, uint32_t* tBout, uint32_t* tCout, uint32_t *svm_sector) { - + uint32_t* tAout, uint32_t* tBout, uint32_t* tCout, + uint32_t *svm_sector, commutation_technique_t comm_tech) { uint8_t sector; uint32_t tA, tB, tC, N; // PWM timings - float T1, T2, a, b, c, temp = 0.0f; - const uint8_t sector_LUT[6] = {6u, 2u, 1u, 4u, 5u, 3u}; - + float T0, T1, T2, a, b, c, temp = 0.0f; a = SQRT3_BY_2*alpha - 0.5f*beta; b = beta; @@ -225,7 +223,6 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, b *= temp; c = -1.0f*(a+b); // kirchhoff current law (current in = current out) - N = (((int32_t)a)>=0) + 2u*(((int32_t)b) >= 0) + 4u*(((int32_t)c) >= 0); sector = sector_LUT[N-1]; @@ -233,60 +230,180 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, case 1: { T1 = a; T2 = b; - - tA = (uint32_t)(T1+T2); - tB = (uint32_t)(T2); - tC = (uint32_t)(0); + T0 = (float)PWMFullDutyCycle - T1 - T2; + + switch (comm_tech) { + case SVM_ARS: + default: + tA = (uint32_t)(T1+T2+0.5f*T0); + tB = (uint32_t)(T2+0.5f*T0); + tC = (uint32_t)(0.5f*T0); + break; + + case V0_ODD_V7_EVEN: + case NULL_V0: + tA = (uint32_t)(T1+T2); + tB = (uint32_t)(T2); + tC = (uint32_t)(0); + break; + + case V7_ODD_V0_EVEN: + case NULL_V7: + tA = (uint32_t)(PWMFullDutyCycle); + tB = (uint32_t)(T0+T2); + tC = (uint32_t)(T0); + break; + } break; } case 2: { T1 = -c; T2 = -a; - - tA = (uint32_t)(T1); - tB = (uint32_t)(T1+T2); - tC = (uint32_t)(0); + T0 = (float)PWMFullDutyCycle - T1 - T2; + + switch (comm_tech) { + case SVM_ARS: + default: + tA = (uint32_t)(T1+0.5f*T0); + tB = (uint32_t)(T1+T2+0.5f*T0); + tC = (uint32_t)(0.5f*T0); + break; + + case V7_ODD_V0_EVEN: + case NULL_V0: + tA = (uint32_t)(T1); + tB = (uint32_t)(T1+T2); + tC = (uint32_t)(0); + break; + + case V0_ODD_V7_EVEN: + case NULL_V7: + tA = (uint32_t)(T0+T1); + tB = (uint32_t)(PWMFullDutyCycle); + tC = (uint32_t)(T0); + break; + } break; } case 3: { T1 = b; T2 = c; - - tA = (uint32_t)(0); - tB = (uint32_t)(T1+T2); - tC = (uint32_t)(T2); + T0 = (float)PWMFullDutyCycle - T1 - T2; + + switch (comm_tech) { + case SVM_ARS: + default: + tA = (uint32_t)(0.5f*T0); + tB = (uint32_t)(T1+T2+0.5f*T0); + tC = (uint32_t)(T2+0.5f*T0); + break; + + case V0_ODD_V7_EVEN: + case NULL_V0: + tA = (uint32_t)(0); + tB = (uint32_t)(T1+T2); + tC = (uint32_t)(T2); + break; + + case V7_ODD_V0_EVEN: + case NULL_V7: + tA = (uint32_t)(T0); + tB = (uint32_t)(PWMFullDutyCycle); + tC = (uint32_t)(T0+T2); + break; + } break; } case 4: { T1 = -a; T2 = -b; - - tA = (uint32_t)(0); - tB = (uint32_t)(T1); - tC = (uint32_t)(T1+T2); + T0 = (float)PWMFullDutyCycle - T1 - T2; + + switch (comm_tech) { + case SVM_ARS: + default: + tA = (uint32_t)(0.5f*T0); + tB = (uint32_t)(T1+0.5f*T0); + tC = (uint32_t)(T1+T2+0.5f*T0); + break; + + case V7_ODD_V0_EVEN: + case NULL_V0: + tA = (uint32_t)(0); + tB = (uint32_t)(T1); + tC = (uint32_t)(T1+T2); + break; + + case V0_ODD_V7_EVEN: + case NULL_V7: + tA = (uint32_t)(T0); + tB = (uint32_t)(T0+T1); + tC = (uint32_t)(PWMFullDutyCycle); + break; + } break; } case 5: { T1 = c; T2 = a; - - tA = (uint32_t)(T2); - tB = (uint32_t)(0); - tC = (uint32_t)(T1+T2); + T0 = (float)PWMFullDutyCycle - T1 - T2; + + switch (comm_tech) { + case SVM_ARS: + default: + tA = (uint32_t)(T2+0.5f*T0); + tB = (uint32_t)(0.5f*T0); + tC = (uint32_t)(T1+T2+0.5f*T0); + break; + + case V0_ODD_V7_EVEN: + case NULL_V0: + tA = (uint32_t)(T2); + tB = (uint32_t)(0); + tC = (uint32_t)(T1+T2); + break; + + case V7_ODD_V0_EVEN: + case NULL_V7: + tA = (uint32_t)(T0+T2); + tB = (uint32_t)(T0); + tC = (uint32_t)(PWMFullDutyCycle); + break; + } break; } case 6: { T1 = -b; T2 = -c; - - tA = (uint32_t)(T1+T2); - tB = (uint32_t)(0); - tC = (uint32_t)(T1); + T0 = (float)PWMFullDutyCycle - T1 - T2; + + switch (comm_tech) { + case SVM_ARS: + default: + tA = (uint32_t)(T1+T2+0.5f*T0); + tB = (uint32_t)(0.5f*T0); + tC = (uint32_t)(T1+0.5f*T0); + break; + + case V7_ODD_V0_EVEN: + case NULL_V0: + tA = (uint32_t)(T1+T2); + tB = (uint32_t)(0); + tC = (uint32_t)(T1); + break; + + case V0_ODD_V7_EVEN: + case NULL_V7: + tA = (uint32_t)(PWMFullDutyCycle); + tB = (uint32_t)(T0); + tC = (uint32_t)(T0+T1); + break; + } break; } } diff --git a/motor/foc_math.h b/motor/foc_math.h index a2210829a..c0eb2321a 100644 --- a/motor/foc_math.h +++ b/motor/foc_math.h @@ -210,7 +210,8 @@ void foc_observer_update(float v_alpha, float v_beta, float i_alpha, float i_bet void foc_pll_run(float phase, float dt, float *phase_var, float *speed_var, mc_configuration *conf); void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, - uint32_t* tAout, uint32_t* tBout, uint32_t* tCout, uint32_t *svm_sector); + uint32_t* tAout, uint32_t* tBout, uint32_t* tCout, + uint32_t *svm_sector, commutation_technique_t comm_tech); void foc_run_pid_control_pos(bool index_found, float dt, motor_all_state_t *motor); void foc_run_pid_control_speed(float dt, motor_all_state_t *motor); float foc_correct_encoder(float obs_angle, float enc_angle, float speed, float sl_erpm, motor_all_state_t *motor); diff --git a/motor/mcpwm_foc.c b/motor/mcpwm_foc.c index 120e3303a..a27e50ddc 100644 --- a/motor/mcpwm_foc.c +++ b/motor/mcpwm_foc.c @@ -4236,7 +4236,8 @@ static void control_current(motor_all_state_t *motor, float dt) { (uint32_t*)&motor->m_duty1_next, (uint32_t*)&motor->m_duty2_next, (uint32_t*)&motor->m_duty3_next, - (uint32_t*)&state_m->svm_sector); + (uint32_t*)&state_m->svm_sector, + SVM_ARS); motor->m_duty_next_set = true; } } else { @@ -4262,7 +4263,7 @@ static void control_current(motor_all_state_t *motor, float dt) { // Calculate the duty cycles for all the phases. This also injects a zero modulation signal to // be able to fully utilize the bus voltage. See https://microchipdeveloper.com/mct5001:start - foc_svm(state_m->mod_alpha_raw, state_m->mod_beta_raw, top, &duty1, &duty2, &duty3, (uint32_t*)&state_m->svm_sector); + foc_svm(state_m->mod_alpha_raw, state_m->mod_beta_raw, top, &duty1, &duty2, &duty3, (uint32_t*)&state_m->svm_sector, SVM_ARS); if (motor == &m_motor_1) { TIMER_UPDATE_DUTY_M1(duty1, duty2, duty3); From e0f8c7226a4dd1f4690b336d5f5fe53b22434cff Mon Sep 17 00:00:00 2001 From: Rampagy Date: Tue, 17 Jan 2023 22:15:19 -0600 Subject: [PATCH 09/16] Adding sector_LUT --- motor/foc_math.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/motor/foc_math.c b/motor/foc_math.c index b9c450df3..6a7fb73f4 100644 --- a/motor/foc_math.c +++ b/motor/foc_math.c @@ -21,6 +21,10 @@ #include "utils_math.h" #include + +const uint8_t sector_LUT[6] = {6u, 2u, 1u, 4u, 5u, 3u}; + + // See http://cas.ensmp.fr/~praly/Telechargement/Journaux/2010-IEEE_TPEL-Lee-Hong-Nam-Ortega-Praly-Astolfi.pdf void foc_observer_update(float v_alpha, float v_beta, float i_alpha, float i_beta, float dt, observer_state *state, float *phase, motor_all_state_t *motor) { From fb3490c7adba2df73db6ff387e639b79d0a069cf Mon Sep 17 00:00:00 2001 From: Rampagy Date: Wed, 15 Feb 2023 21:23:35 -0600 Subject: [PATCH 10/16] Fixing bug when a, b, c are all zero --- motor/foc_math.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/motor/foc_math.c b/motor/foc_math.c index 6a7fb73f4..5f5d36754 100644 --- a/motor/foc_math.c +++ b/motor/foc_math.c @@ -227,7 +227,8 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, b *= temp; c = -1.0f*(a+b); // kirchhoff current law (current in = current out) - N = (((int32_t)a)>=0) + 2u*(((int32_t)b) >= 0) + 4u*(((int32_t)c) >= 0); + N = (((int32_t)a) >= 0) + 2u*(((int32_t)b) >= 0) + 4u*(((int32_t)c) >= 0); + N = (N > 6) ? 3 : N; // if a, b, c are all zero set it to index 3, sector 1 (alpha & beta are zero) sector = sector_LUT[N-1]; switch(sector) { From 8a020da82d05f626f5410c1313a02897cd1cd7a4 Mon Sep 17 00:00:00 2001 From: Rampagy Date: Thu, 16 Feb 2023 20:55:37 -0600 Subject: [PATCH 11/16] code clean up --- motor/foc_math.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/motor/foc_math.c b/motor/foc_math.c index 5f5d36754..682369a3a 100644 --- a/motor/foc_math.c +++ b/motor/foc_math.c @@ -200,7 +200,6 @@ void foc_pll_run(float phase, float dt, float *phase_var, /** * @brief svm Space vector modulation. Magnitude must not be larger than sqrt(3)/2, or 0.866 to avoid overmodulation. * See https://github.com/vedderb/bldc/pull/372#issuecomment-962499623 for a full description. - * Null=V0 variation (reduces switching losses by up to 33%) * See the follwoing links for a full description: * https://youtu.be/5eQyoVMz1dY * https://link.gale.com/apps/doc/A18320578/AONE @@ -208,14 +207,14 @@ void foc_pll_run(float phase, float dt, float *phase_var, * @param beta Park transformed and normalized voltage * @param PWMFullDutyCycle is the peak value of the PWM counter. * @param tAout PWM duty cycle phase A (0 = off all of the time, PWMFullDutyCycle = on all of the time) - * @param tBout PWM duty cycle phase B - * @param tCout PWM duty cycle phase C + * @param tBout PWM duty cycle phase B (0 = off all of the time, PWMFullDutyCycle = on all of the time) + * @param tCout PWM duty cycle phase C (0 = off all of the time, PWMFullDutyCycle = on all of the time) */ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, uint32_t* tAout, uint32_t* tBout, uint32_t* tCout, uint32_t *svm_sector, commutation_technique_t comm_tech) { - uint8_t sector; - uint32_t tA, tB, tC, N; // PWM timings + + uint32_t tA, tB, tC, N, sector = 0u; float T0, T1, T2, a, b, c, temp = 0.0f; a = SQRT3_BY_2*alpha - 0.5f*beta; @@ -228,7 +227,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, c = -1.0f*(a+b); // kirchhoff current law (current in = current out) N = (((int32_t)a) >= 0) + 2u*(((int32_t)b) >= 0) + 4u*(((int32_t)c) >= 0); - N = (N > 6) ? 3 : N; // if a, b, c are all zero set it to index 3, sector 1 (alpha & beta are zero) + N = (N > 6u) ? 3u : N; // if a, b, c are all zero set it to index 3, sector 1 (alpha & beta are zero) sector = sector_LUT[N-1]; switch(sector) { @@ -416,7 +415,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, *tAout = tA; *tBout = tB; *tCout = tC; - *svm_sector = (uint32_t)sector; + *svm_sector = sector; } void foc_run_pid_control_pos(bool index_found, float dt, motor_all_state_t *motor) { From e42ce49944783fc406f53f7f2d5d6903545b9fdc Mon Sep 17 00:00:00 2001 From: Rampagy Date: Thu, 16 Feb 2023 21:01:28 -0600 Subject: [PATCH 12/16] Fixing -Wmaybe-uninitialized warning --- motor/foc_math.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/motor/foc_math.c b/motor/foc_math.c index 682369a3a..72c1d32d0 100644 --- a/motor/foc_math.c +++ b/motor/foc_math.c @@ -214,8 +214,8 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, uint32_t* tAout, uint32_t* tBout, uint32_t* tCout, uint32_t *svm_sector, commutation_technique_t comm_tech) { - uint32_t tA, tB, tC, N, sector = 0u; - float T0, T1, T2, a, b, c, temp = 0.0f; + uint32_t tA = 0u, tB = 0u, tC = 0u, N = 0u, sector = 0u; + float T0 = 0.0f, T1 = 0.0f, T2 = 0.0f, a = 0.0f, b = 0.0f, c = 0.0f, temp = 0.0f; a = SQRT3_BY_2*alpha - 0.5f*beta; b = beta; From bf5f22bf6586c8d2bbf074806fba6f315d962d4a Mon Sep 17 00:00:00 2001 From: Rampagy Date: Sun, 19 Feb 2023 13:29:50 -0600 Subject: [PATCH 13/16] Adding MCCONF parameter for commutation type --- confgenerator.c | 3 +++ datatypes.h | 3 ++- motor/foc_math.c | 12 ++++++------ motor/mcconf_default.h | 3 +++ motor/mcpwm_foc.c | 4 ++-- 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/confgenerator.c b/confgenerator.c index 5ed3e7eab..e71fee4d8 100644 --- a/confgenerator.c +++ b/confgenerator.c @@ -130,6 +130,7 @@ int32_t confgenerator_serialize_mcconf(uint8_t *buffer, const mc_configuration * buffer[ind++] = conf->foc_phase_filter_disable_fault; buffer_append_float32_auto(buffer, conf->foc_phase_filter_max_erpm, &ind); buffer[ind++] = conf->foc_mtpa_mode; + buffer[ind++] = conf->foc_commutation_type; buffer_append_float32_auto(buffer, conf->foc_fw_current_max, &ind); buffer_append_float16(buffer, conf->foc_fw_duty_start, 10000, &ind); buffer_append_float16(buffer, conf->foc_fw_ramp_time, 1000, &ind); @@ -527,6 +528,7 @@ bool confgenerator_deserialize_mcconf(const uint8_t *buffer, mc_configuration *c conf->foc_phase_filter_disable_fault = buffer[ind++]; conf->foc_phase_filter_max_erpm = buffer_get_float32_auto(buffer, &ind); conf->foc_mtpa_mode = buffer[ind++]; + conf->foc_commutation_type = buffer[ind++]; conf->foc_fw_current_max = buffer_get_float32_auto(buffer, &ind); conf->foc_fw_duty_start = buffer_get_float16(buffer, 10000, &ind); conf->foc_fw_ramp_time = buffer_get_float16(buffer, 1000, &ind); @@ -920,6 +922,7 @@ void confgenerator_set_defaults_mcconf(mc_configuration *conf) { conf->foc_phase_filter_disable_fault = MCCONF_FOC_PHASE_FILTER_DISABLE_FAULT; conf->foc_phase_filter_max_erpm = MCCONF_FOC_PHASE_FILTER_MAX_ERPM; conf->foc_mtpa_mode = MCCONF_FOC_MTPA_MODE; + conf->foc_commutation_type = MCCONF_FOC_COMMUTATION_TYPE; conf->foc_fw_current_max = MCCONF_FOC_FW_CURRENT_MAX; conf->foc_fw_duty_start = MCCONF_FOC_FW_DUTY_START; conf->foc_fw_ramp_time = MCCONF_FOC_FW_RAMP_TIME; diff --git a/datatypes.h b/datatypes.h index 4a19a4363..4fc377415 100644 --- a/datatypes.h +++ b/datatypes.h @@ -26,7 +26,7 @@ // Data types typedef enum { - SVM_ARS = 0, // default (previous implementation) + ALT_REV_SEQ = 0, // alternating reversing sequence (previous implementation) NULL_V0, // flat bottom NULL_V7, // flat top V7_ODD_V0_EVEN, // v7 in [1,3,5] v0 in [2,4,6] @@ -466,6 +466,7 @@ typedef struct { bool foc_phase_filter_disable_fault; float foc_phase_filter_max_erpm; MTPA_MODE foc_mtpa_mode; + commutation_technique_t foc_commutation_type; // Field Weakening float foc_fw_current_max; float foc_fw_duty_start; diff --git a/motor/foc_math.c b/motor/foc_math.c index 72c1d32d0..85e37138d 100644 --- a/motor/foc_math.c +++ b/motor/foc_math.c @@ -237,7 +237,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, T0 = (float)PWMFullDutyCycle - T1 - T2; switch (comm_tech) { - case SVM_ARS: + case ALT_REV_SEQ: default: tA = (uint32_t)(T1+T2+0.5f*T0); tB = (uint32_t)(T2+0.5f*T0); @@ -267,7 +267,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, T0 = (float)PWMFullDutyCycle - T1 - T2; switch (comm_tech) { - case SVM_ARS: + case ALT_REV_SEQ: default: tA = (uint32_t)(T1+0.5f*T0); tB = (uint32_t)(T1+T2+0.5f*T0); @@ -297,7 +297,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, T0 = (float)PWMFullDutyCycle - T1 - T2; switch (comm_tech) { - case SVM_ARS: + case ALT_REV_SEQ: default: tA = (uint32_t)(0.5f*T0); tB = (uint32_t)(T1+T2+0.5f*T0); @@ -327,7 +327,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, T0 = (float)PWMFullDutyCycle - T1 - T2; switch (comm_tech) { - case SVM_ARS: + case ALT_REV_SEQ: default: tA = (uint32_t)(0.5f*T0); tB = (uint32_t)(T1+0.5f*T0); @@ -357,7 +357,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, T0 = (float)PWMFullDutyCycle - T1 - T2; switch (comm_tech) { - case SVM_ARS: + case ALT_REV_SEQ: default: tA = (uint32_t)(T2+0.5f*T0); tB = (uint32_t)(0.5f*T0); @@ -387,7 +387,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, T0 = (float)PWMFullDutyCycle - T1 - T2; switch (comm_tech) { - case SVM_ARS: + case ALT_REV_SEQ: default: tA = (uint32_t)(T1+T2+0.5f*T0); tB = (uint32_t)(0.5f*T0); diff --git a/motor/mcconf_default.h b/motor/mcconf_default.h index 8da59c572..5327e9ef9 100644 --- a/motor/mcconf_default.h +++ b/motor/mcconf_default.h @@ -472,6 +472,9 @@ #ifndef MCCONF_FOC_SPEED_SOURCE #define MCCONF_FOC_SPEED_SOURCE SPEED_SRC_OBSERVER // Position source for speed trackers #endif +#ifndef MCCONF_FOC_COMMUTATION_TYPE +#define MCCONF_FOC_COMMUTATION_TYPE ALT_REV_SEQ // Commutation method during FOC +#endif // GPD #ifndef MCCONF_GPD_BUFFER_NOTIFY_LEFT diff --git a/motor/mcpwm_foc.c b/motor/mcpwm_foc.c index a27e50ddc..7e49751af 100644 --- a/motor/mcpwm_foc.c +++ b/motor/mcpwm_foc.c @@ -4237,7 +4237,7 @@ static void control_current(motor_all_state_t *motor, float dt) { (uint32_t*)&motor->m_duty2_next, (uint32_t*)&motor->m_duty3_next, (uint32_t*)&state_m->svm_sector, - SVM_ARS); + motor->m_conf->foc_commutation_type); motor->m_duty_next_set = true; } } else { @@ -4263,7 +4263,7 @@ static void control_current(motor_all_state_t *motor, float dt) { // Calculate the duty cycles for all the phases. This also injects a zero modulation signal to // be able to fully utilize the bus voltage. See https://microchipdeveloper.com/mct5001:start - foc_svm(state_m->mod_alpha_raw, state_m->mod_beta_raw, top, &duty1, &duty2, &duty3, (uint32_t*)&state_m->svm_sector, SVM_ARS); + foc_svm(state_m->mod_alpha_raw, state_m->mod_beta_raw, top, &duty1, &duty2, &duty3, (uint32_t*)&state_m->svm_sector, motor->m_conf->foc_commutation_type); if (motor == &m_motor_1) { TIMER_UPDATE_DUTY_M1(duty1, duty2, duty3); From a120e30a97d4abf829dd60966c605a112784994e Mon Sep 17 00:00:00 2001 From: Rampagy Date: Sun, 19 Feb 2023 19:14:44 -0600 Subject: [PATCH 14/16] Updating option --- confgenerator.c | 6 +++--- confgenerator.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/confgenerator.c b/confgenerator.c index e71fee4d8..48b2b6e2a 100644 --- a/confgenerator.c +++ b/confgenerator.c @@ -130,12 +130,12 @@ int32_t confgenerator_serialize_mcconf(uint8_t *buffer, const mc_configuration * buffer[ind++] = conf->foc_phase_filter_disable_fault; buffer_append_float32_auto(buffer, conf->foc_phase_filter_max_erpm, &ind); buffer[ind++] = conf->foc_mtpa_mode; - buffer[ind++] = conf->foc_commutation_type; buffer_append_float32_auto(buffer, conf->foc_fw_current_max, &ind); buffer_append_float16(buffer, conf->foc_fw_duty_start, 10000, &ind); buffer_append_float16(buffer, conf->foc_fw_ramp_time, 1000, &ind); buffer_append_float16(buffer, conf->foc_fw_q_current_factor, 10000, &ind); buffer[ind++] = conf->foc_speed_soure; + buffer[ind++] = conf->foc_commutation_type; buffer_append_int16(buffer, conf->gpd_buffer_notify_left, &ind); buffer_append_int16(buffer, conf->gpd_buffer_interpol, &ind); buffer_append_float16(buffer, conf->gpd_current_filter_const, 10000, &ind); @@ -528,12 +528,12 @@ bool confgenerator_deserialize_mcconf(const uint8_t *buffer, mc_configuration *c conf->foc_phase_filter_disable_fault = buffer[ind++]; conf->foc_phase_filter_max_erpm = buffer_get_float32_auto(buffer, &ind); conf->foc_mtpa_mode = buffer[ind++]; - conf->foc_commutation_type = buffer[ind++]; conf->foc_fw_current_max = buffer_get_float32_auto(buffer, &ind); conf->foc_fw_duty_start = buffer_get_float16(buffer, 10000, &ind); conf->foc_fw_ramp_time = buffer_get_float16(buffer, 1000, &ind); conf->foc_fw_q_current_factor = buffer_get_float16(buffer, 10000, &ind); conf->foc_speed_soure = buffer[ind++]; + conf->foc_commutation_type = buffer[ind++]; conf->gpd_buffer_notify_left = buffer_get_int16(buffer, &ind); conf->gpd_buffer_interpol = buffer_get_int16(buffer, &ind); conf->gpd_current_filter_const = buffer_get_float16(buffer, 10000, &ind); @@ -922,12 +922,12 @@ void confgenerator_set_defaults_mcconf(mc_configuration *conf) { conf->foc_phase_filter_disable_fault = MCCONF_FOC_PHASE_FILTER_DISABLE_FAULT; conf->foc_phase_filter_max_erpm = MCCONF_FOC_PHASE_FILTER_MAX_ERPM; conf->foc_mtpa_mode = MCCONF_FOC_MTPA_MODE; - conf->foc_commutation_type = MCCONF_FOC_COMMUTATION_TYPE; conf->foc_fw_current_max = MCCONF_FOC_FW_CURRENT_MAX; conf->foc_fw_duty_start = MCCONF_FOC_FW_DUTY_START; conf->foc_fw_ramp_time = MCCONF_FOC_FW_RAMP_TIME; conf->foc_fw_q_current_factor = MCCONF_FOC_FW_Q_CURRENT_FACTOR; conf->foc_speed_soure = MCCONF_FOC_SPEED_SOURCE; + conf->foc_commutation_type = MCCONF_FOC_COMMUTATION_TYPE; conf->gpd_buffer_notify_left = MCCONF_GPD_BUFFER_NOTIFY_LEFT; conf->gpd_buffer_interpol = MCCONF_GPD_BUFFER_INTERPOL; conf->gpd_current_filter_const = MCCONF_GPD_CURRENT_FILTER_CONST; diff --git a/confgenerator.h b/confgenerator.h index 680c3d873..ecac92fd9 100644 --- a/confgenerator.h +++ b/confgenerator.h @@ -8,7 +8,7 @@ #include // Constants -#define MCCONF_SIGNATURE 776184161 +#define MCCONF_SIGNATURE 608083588 #define APPCONF_SIGNATURE 486554156 // Functions From 24745290c9b9332a4ec6de0ecee079c1b0d3061f Mon Sep 17 00:00:00 2001 From: Rampagy Date: Sun, 26 Feb 2023 11:44:02 -0600 Subject: [PATCH 15/16] Adding sector 0 protection and fixing null v7 pwm --- motor/foc_math.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/motor/foc_math.c b/motor/foc_math.c index 85e37138d..1facd3b06 100644 --- a/motor/foc_math.c +++ b/motor/foc_math.c @@ -227,8 +227,8 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, c = -1.0f*(a+b); // kirchhoff current law (current in = current out) N = (((int32_t)a) >= 0) + 2u*(((int32_t)b) >= 0) + 4u*(((int32_t)c) >= 0); - N = (N > 6u) ? 3u : N; // if a, b, c are all zero set it to index 3, sector 1 (alpha & beta are zero) - sector = sector_LUT[N-1]; + N = (N > 6u || N == 0u) ? 2u : N-1; // if a, b, c are all zero (happens when alpha & beta are zero) or negative set it to index 2 (sector 1) + sector = sector_LUT[N]; switch(sector) { case 1: { @@ -253,7 +253,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, case V7_ODD_V0_EVEN: case NULL_V7: - tA = (uint32_t)(PWMFullDutyCycle); + tA = (uint32_t)(PWMFullDutyCycle+1); tB = (uint32_t)(T0+T2); tC = (uint32_t)(T0); break; @@ -284,7 +284,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, case V0_ODD_V7_EVEN: case NULL_V7: tA = (uint32_t)(T0+T1); - tB = (uint32_t)(PWMFullDutyCycle); + tB = (uint32_t)(PWMFullDutyCycle+1); tC = (uint32_t)(T0); break; } @@ -314,7 +314,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, case V7_ODD_V0_EVEN: case NULL_V7: tA = (uint32_t)(T0); - tB = (uint32_t)(PWMFullDutyCycle); + tB = (uint32_t)(PWMFullDutyCycle+1); tC = (uint32_t)(T0+T2); break; } @@ -345,7 +345,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, case NULL_V7: tA = (uint32_t)(T0); tB = (uint32_t)(T0+T1); - tC = (uint32_t)(PWMFullDutyCycle); + tC = (uint32_t)(PWMFullDutyCycle+1); break; } break; @@ -375,7 +375,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, case NULL_V7: tA = (uint32_t)(T0+T2); tB = (uint32_t)(T0); - tC = (uint32_t)(PWMFullDutyCycle); + tC = (uint32_t)(PWMFullDutyCycle+1); break; } break; @@ -403,7 +403,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, case V0_ODD_V7_EVEN: case NULL_V7: - tA = (uint32_t)(PWMFullDutyCycle); + tA = (uint32_t)(PWMFullDutyCycle+1); tB = (uint32_t)(T0); tC = (uint32_t)(T0+T1); break; From 0cc00630c86fba0c37470dfa25adca9ca3a463aa Mon Sep 17 00:00:00 2001 From: Rampagy Date: Wed, 3 May 2023 19:53:04 -0500 Subject: [PATCH 16/16] Removing +1 for full duty cycle --- motor/foc_math.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/motor/foc_math.c b/motor/foc_math.c index 1facd3b06..5aaf94a7b 100644 --- a/motor/foc_math.c +++ b/motor/foc_math.c @@ -253,7 +253,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, case V7_ODD_V0_EVEN: case NULL_V7: - tA = (uint32_t)(PWMFullDutyCycle+1); + tA = (uint32_t)(PWMFullDutyCycle); tB = (uint32_t)(T0+T2); tC = (uint32_t)(T0); break; @@ -284,7 +284,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, case V0_ODD_V7_EVEN: case NULL_V7: tA = (uint32_t)(T0+T1); - tB = (uint32_t)(PWMFullDutyCycle+1); + tB = (uint32_t)(PWMFullDutyCycle); tC = (uint32_t)(T0); break; } @@ -314,7 +314,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, case V7_ODD_V0_EVEN: case NULL_V7: tA = (uint32_t)(T0); - tB = (uint32_t)(PWMFullDutyCycle+1); + tB = (uint32_t)(PWMFullDutyCycle); tC = (uint32_t)(T0+T2); break; } @@ -345,7 +345,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, case NULL_V7: tA = (uint32_t)(T0); tB = (uint32_t)(T0+T1); - tC = (uint32_t)(PWMFullDutyCycle+1); + tC = (uint32_t)(PWMFullDutyCycle); break; } break; @@ -375,7 +375,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, case NULL_V7: tA = (uint32_t)(T0+T2); tB = (uint32_t)(T0); - tC = (uint32_t)(PWMFullDutyCycle+1); + tC = (uint32_t)(PWMFullDutyCycle); break; } break; @@ -403,7 +403,7 @@ void foc_svm(float alpha, float beta, uint32_t PWMFullDutyCycle, case V0_ODD_V7_EVEN: case NULL_V7: - tA = (uint32_t)(PWMFullDutyCycle+1); + tA = (uint32_t)(PWMFullDutyCycle); tB = (uint32_t)(T0); tC = (uint32_t)(T0+T1); break;