then the control fails, the motor does not spin even though the rest of the code is still unchanged.
So, I try looking into the library code. The current_sense.init() calls 3 functions
_configureADCLowSide()
_driverSyncLowSide()
calibrateOffsets()
I think _driverSyncLowSide() function causes the problem. It configures PWM interrupts and the PWM interrupts interfere the interrupt of encoder so that the encoder cannot work.
Later, I implement torque control mode using torque control loop in foc_current mode. I find that the controller can regulate the magnitude of Iq correctly, but with incorrect angle (because the encoder cannot work properly) so the motor does not spin.
Can you share the Arduino code?
The acid interrupts should not interfere with the encoder interrupts. I’ve written the code based exactly on the encoder as a sensor.
So you want to say that the velocity and the angle are blocked and not showing good values?
Or the angle and velocity is correct?
What is the behavior at the startup?
Does your motor start moving and then it stops slowly or it just stays in place?
Maybe it is the issue that the ESP32 is loosing steps of the encoder.
What you can try to do is reduce the number of impulses per revolution to arround 500, 2048 is maybe a bit too much.
Also I’d suggest you to lower the PWM frequencey to 15-20khz.
driver.pwm_frequency = 20000; // from 15000-200000
Actually I still have some images to be uploaded. But yesterday the number of posts per day was limited for new user like me.
Don’t forget that when I implement torque mode using voltage mode, the motor spins continuously at around 65 rad/s. This means that the ESP32 does not lose any steps of encoder at all. 2048 PPR is ok if the speed is not too high. But when I just add current_sense.driverSync(&driver) to the code (but still in torque mode using voltage mode), the motor stops. This means that something goes wrong when synchronization takes place.
It’s ok for not believing me because you have written the library, not me. But I do real experiment and get this result. It will be great if you can setup the same system and do experiment too.
I believe you compleltely.
I just did not have the same experience and that is why I want to make sure that I understand well the issue so we can remove it. Because there can be many different reasons for this. And driverSync is not doing anything but starting the pwm synced interrupts.
This code is not stopping the encoder interrupts in any way, but it might be that it is causing so many interrupts that the encoder interrupts are just not being executed.
To identify the real problem and re resolve this issue so that you can continue with your project I am going to need you to answer some questions:
When you monitor the encoder enable and velocity are the values correnct or they are just frozen?
Please try to lower the PWM frequency and see if the same issue is happening - lowering the pwm frequency will lower the number of adc interrutps and will let potentially more time for exoder interrupts.
I am going to try to replicate your setup this week and I’ll let you know my if I can replicate the issue.
Thank you for your sincerity. These are my answers.
I think the angle value is wrong since the alignment stage because the controller misses some encoder pulses while the rotor is rotating (during alignment). Because of wrong angle, the motor generates little torque at startup (but can start rotating). Because the controller misses some encoder pulses while the rotor is rotating (during startup), the angle error keeps increasing which makes the motor torque to be decreasing until there is no enough torque to rotate the rotor. Finally, the rotor stops and the angle value freezes at one value (wrong value). Then, because the controller relies on the observed angle, it keeps generating voltage (or current) at the same wrong angle all the time, which generates no torque. The consequence is that the motor stops permanently.
When I reduce PWM frequency to 15 kHz, the same issue still happens. But I notice that the update rate of monitoring becomes faster.