Current control using low-side current sense does not work because encoder cannot work


I’m using SimpleFOC library V2.2.1 with

  1. Wemos D1 R32 (ESP32) controller board
  2. DRV8302 driver board
  3. A2212 (1000kv) BLDC motor
  4. AMT103 encoder

DRV8302 has low-side current sense support so I try to implement current control.

First, I implement torque control mode using torque control loop in voltage mode. It works fine.

But when I include the following code

LowsideCurrentSense current_sense = LowsideCurrentSense(0.005, 12.222, 34, 36, 39);

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

  1. _configureADCLowSide()
  2. _driverSyncLowSide()
  3. 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.

Any help will be appreciated.

Thank you.

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. :slight_smile:

Please download the sketch from this link

Thank you.

Hey @Nuno86,

I see you’ve commented these lines:

  // inverse current sensing gain
  //current_sense.gain_a *= -1;
  //current_sense.gain_b *= -1;
  //current_sense.gain_c *= -1;

  // skip alignment
  //current_sense.skip_align = true;

You’re current sense calibration is probably failing. Can you show us the monitoring output from your serial terminal?

Also do call current_sense.init() after motor.init().

You can see the code in the examples

I’m sorry for late reply. I just woke up from sleep (Thailand time).

Align current sense.
Success: 3 (with gains inverted)

The Iq is approximately 1000 mA. But the motor does not spin.

I’ve tried putting current_sense.init() after motor.init(), but I’ve got the same result.

I notice that in the example code, TorqueControlType is voltage, not foc_current.

This is serial plot of Iq and Id control

The controller can regulate Iq and Id. But the motor does not spin.

Hey @Nuno86,

If you uncomment these lines do you still get the same behavior?

Can you monitor the motor shaft angle and the velocity to see if your sensor is working?

  motor.monitor_variables = _MON_CURR_Q | _MON_CURR_D | _MON_VEL | _MON_ANGLE; 

These are the results when those lines are commented.

Success: 3, and you can see Iq and Id at startup

Iq and Id in long term

Change to velocity and angle. You can see the values at startup

velocity and angle in steady state (with a little fluctuation)

velocity and angle in long term

These are the results when those lines are uncommented

Success: 1, and you can see Iq and Id at startup

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.

Thank you.

Hey Nuno,

I believe you compleltely. :smiley:
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:

  1. When you monitor the encoder enable and velocity are the values correnct or they are just frozen?
  2. 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.

  1. 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.

  2. When I reduce PWM frequency to 15 kHz, the same issue still happens. But I notice that the update rate of monitoring becomes faster.