You are right. I forgot to update CCU80_1_IRQHandler. There are probably to many changes in this version (increasing precision with Q8_8, addding pll, ...) I think I found a bug in PLL code. I will try to fix it and to make PLL only an option (to see if it already works without PLL).
I wonder if its possible, but i had a thought about a potential architectural improvement that might make testing and debugging new features easier for the community.Would it be possible to encapsulate major experimental features (like the different DYNAMIC_LEAD_ANGLE strategies) into their own separate .c and .h files?Then, in the core files like motor.c, we could use the preprocessor directives to conditionally include and call the appropriate functions. For example, instead of having all the logic for options 1 and 2 within #if blocks inside motor.c, the main interrupt could look something like this:C
#if DYNAMIC_LEAD_ANGLE == 1
#include "foc_pid.h"
// ... then call a function from foc_pid.c
foc_pid_apply_angle();
#elif DYNAMIC_LEAD_ANGLE == 2
#include "foc_esc.h"
// ... then call a function from foc_esc.c
foc_esc_apply_angle();
#else
// ... use the original, stable logic that's already in motor.c
apply_original_simple_foc();
#endif
The main benefits I see to this approach would be
Modularity: Experimental code would be completely isolated, preventing accidental conflicts with the stable codebase.
Easier Debugging: It would be trivial to enable or disable entire features just by changing an #include, making it easier to pinpoint issues.
Readability: It would keep the core files like motor.c much cleaner and focused on their primary function.Just a thought! It might make it easier for people like me to test new branches and provide better feedback. Thanks for all your amazing work on this project.
On another note, i made a fun tweak to the default lead angle, it nudges up a lil depending on motor duty cycle, this allows me to gain so much more speed when in a lower gear and duty cycle maxed with low amps (it gets so tiring shifting with 11 speed 11-42t casette, its like driving a wagon :s) it works a bit like field weakening, i currently have no speed limit guardrail tho so it just keeps accelerating with no load xD
my "motor foc" as shown on display currently climbs to 19 from duty cycle which is probably a bit high, but i think it will have some good potential and its really simple
if (ui8_g_duty_cycle > 0) {
// calculate phase current.
if (ui8_g_duty_cycle > 10) {
ui16_adc_motor_phase_current = (uint16_t)(((uint32_t)ui16_adc_battery_current_filtered << 8) / ui8_g_duty_cycle);
} else {
ui16_adc_motor_phase_current = ui16_adc_battery_current_filtered;
}
if (ui8_foc_flag) { // We still use this flag to run the logic once per rotation
// --- Grenny gear foc angle ---
// 1. Calculate advance from Battery Amps (our old, reliable way)
uint8_t foc_from_current = (((uint16_t)ui16_adc_battery_current_filtered * (uint16_t)ui8_foc_angle_multiplicator) + 128) >> 8;
// 2. Calculate advance from Duty Cycle
// This will scale from 0 up to a max of, say, 15 degrees of advance.
// You can play with the '15' to make it more or less aggressive.
const uint8_t MAX_DUTY_CYCLE_ADVANCE = 15;
uint8_t foc_from_duty = ((uint16_t)ui8_g_duty_cycle * MAX_DUTY_CYCLE_ADVANCE) / 255;
// 3. Take the bigger of the two, so they don't "double buff"
uint8_t final_foc_angle;
if (foc_from_current > foc_from_duty) {
final_foc_angle = foc_from_current;
} else {
final_foc_angle = foc_from_duty;
}
// 4. Apply a final safety limit (same as before)
if (final_foc_angle > 29) {
final_foc_angle = 29;
}
ui8_g_foc_angle = final_foc_angle;
also, one more VERY IMPORTANT note i have for you,
i really think amp calculation is off. since i installed my watt meter (battery==meter==load) i see on it between 25-26.4amps
while on display i see 30. ive got it in an awkward position though because i dont want to make the DC leads too long since its the main load line, so i havent got exact numbers. Im gonna do some testing and move it into a better position over the weekend. If im right (95% sure of it) that will explain so much,
why ppl feel like theyve lost some power vs factory
EBikeBuilder on youtube, hes got a watt meter also and saw peaks of only 1150 watts
my motor running quite cool considering my firmware is saying 30bat amps, 50 phase amps(barely went over 40 as id hity duty cap pretty fast accelerating)
That I got the same amount of vdrop as i do on factory firmware
I get the same mileage as factory firmware (with 30a on battery and 29 foc cap)
so now i believe im running pretty much the same actual amps as factory firmware
may also skew your calculations and experiments up id look into it yourself too before committing too much to your new code