The problem is somehow related with PI gains ...
I made some adjustments here and the motor can keep up the speed ... but after a few minutes it starts to oscillate brutally ...
Could it be related to the encoder?
The motor was running smoothly for about 95 seconds and then began to oscillate ...
What happens when you run it sensorless? Does it run stable over time, does it produce torque over time? If all is yes, then I am sure that it must be related to the encoder feedback.
Is it possible that you have an EMC problem on the encoder signals, that is causing the microcontroller to count incorrectly? You can check this by adding small capacitors to the encoder signals (maybe 100pF to 1nF) and see if the behavior improves.
Generally, when I set up the controller gains for FOC, then I do it like this:
- operate in torque mode, and program a rectanguar torque(current) command, maybe 10 to 50Hz in frequency. Adjust system input voltage to max rated.
- configure virtual rotor position feedback to make sure the motor cannot spin.
- measure current of phase U, preferably with current clamp and oscilloscope.
- set integral term to 0, raise proportional term in steps, until the scope starts showing overshoot of the current signal.
- step back a bit with proportional term (I prefer using 50% of final value)
- start raising integral term, until it starts overshooting again
- step back a bit with integral term (again 50% of final value)
Now you can be sure that the current control loop is stable.
Then, the same for velocity control loop:
- use your real rotor phase feedback now
- add all inertia to the rotor shaft that will be present in the system later on
- make sure that regerative operation does not cause overvoltage, as the following test will do that
- program a rectangular velocity command profile, maybe 0.2 Hz, having the velocity switch between two different values. They can be close to each other, maybe 1000 rpm and 1500 rpm.
- monitor velocity value via STM32 DAC feature
- now tune proportional and integral terms of velocity controller the same way as explained above.
This should result in a rock stable system with highest possible dynamic response.