DC current torque control on ESP32-S3 problem

This is on custom hardware board with ESP32-S3-WROOM, DRV8323 gate driver (3 pin PWM mode) with integrated shunt amplifiers (set to gain=20 and midpoint 3.3V/2) for three 3mOhm low side shunts. Testing with a 1-PP industrial geared BLDC with hall sensors and a hoverboard motor (hall sensors as well). Open-loop works fine, as does closed loop voltage-torque control. Reading the DC current (current_sense.getDCCurrent()) in this mode seems to work well. Sensor and current sense alignemnt is hardcoded with correct parameters to keep everything consistent during testing. Also tried on two different drive boards to elliminate some random HW issue / defect.

The problem is, DC current torque control works well, but only sometimes. When it does, the motor behaves as expected and the DC current reading hovers around the expected set value (0.5A for my testing). This works correctly for both rotation directions.

When it does not work, SimpleFOC applies max voltage almost imedietely, the motor spins very quickly and the DC current readings are very high (5A-10A). When direction change is commanded (same torque but with - sign), motor does not change direction and aplied voltge (reported by SimpleFOC) stays the same.
I have not determined exactly what causes it to work or not work, sometimes it’s different just after a reboot. It does apear that the frequency at which loopFOC() is called has an effect on it. Calling it alone in the main loop makes it work most of the time (execution frequency is around 35kHz in this case) but adding monitoring (>8kHz) or a 1ms delay (1Khz exactly) makes it not work most of the time.
I have tried changing the PID_current_q PIDs without success. The best I can achieve is a slower ramp to the maximum voltage.

Some issues I discovered while debugging that might or might not be related:

  • Without executing an analogRead() on current sense pins before current_sense.init() the ESP crashes (WDT reset that backtraces to waiting for conversion in adc driver). This seems to be a bug in ADC initialization code?
  • If I print the current sense offset_ia/b/c values in LowsideCurrentSense::calibrateOffsets(), they are consistently around 4.95. Is this expected? The shunt amp output at this time (with no current) is around 1.65V (confirmed with measuring).

Any ideas?



With low side current sensing, SimpleFOC needs a minimum ON time of the low side mosfets to be able to sample the phase current.
At high duty cycle, there is not enough time, so the phase currents might be completely wrong, and the PI control tries to compensate it, leading to a motor run away, fortunately it’s limited by the PIs output limit.
One solution is to limit the driver’s voltage by doing this for example

driver.voltage_limit = driver.voltage_power_supply * 0.95;

Not sure if this is it. driver.voltage_limit is set to about 80% of driver.voltage_power_supply. I’m also expecting a pretty low duty cycle at such low currents (when working, voltage to the motor is only somewhere around 3V).

You must have a problem with the current sensing for sure.
I cannot comment about ESP32, but I know analogread messes up with the current sampling for STM32, it’s not meant to be used together.

We recently had a bug with the original ESP32 that caused it to crash in a similar location, because the ADC sampling was not trigered for the second ADC. Perhaps this applies to the S3 as well?
Which GPIO pins do you use for current sensing?

I’m using pins IO4, IO5, IO6, all on ADC1 channels specifically to avoid ADC2/WiFi confilcts. I’m also not doing any other analogRead() calls that could mess with SimpleFOC sampling (apart from those at the start, to avoid crashing).

I found a bug on the current sense for the ESP32, I reported the bug here and proposed a temporary hot fix which is easy to implement.

Hmm this could be it. I am measuring current on all three phases though, will your hotfix work for that? Will do some further testing to confirm that is the problem. What is the best way to confirm that current sensing is not synced to PWM? Closed loop voltage-torque mode and use monitoring to plot d and q currents?