Iq and Id currents are not correct. what I'm doing wrong?

Hi all,
Are several weeks that I’m trying to figure out what’s wrong with the Iq/Id currents on my project and I’m still not knowing what’s wrong with the board or the code.
I have two current sensors one on the phase A and the second on the phase B, the current readed from the sensors seem to be correct (I get some sinewaves) but when I run the simplefoc in torque control with foc_current I can’t tune the PID and the Iq and the Id are not dc but a kind of sine.

The green and red lines are the Vq and Vd of the current PID (P = 0.001 , I = 0.05 , D = 0). Looking at the PID output (Vq and Vd) I think the PID isn’t the problem, there is no oscillation on that part, so maybe is somewhere else the problem. The PID values are too weak (maybe?) but if I increase the P to 0.5 and the I to 0.1 the oscillations on the Iq and Id start going crazy.

When I hold the motor the Iq current reach the target and Id goes to 0.

I’m running simplefoc on a custom board with a stm32G4 mcu running at 164MHz, the shunt resistors are 0.002 ohm and a gain of 50 V/V.

PS: To set and test the PID I leave the motor with no load, is that correct? or I have to load the motor?

Can you help me find the problem and solve it?

Can you try with the dev branch?

It’s not easy to change the library in the project because I’m using stm32cube, so I adapted some part to be used on the stm32 ide, but using the oscilloscope I checked the two currents and to me they seems correct.

The yellow signal is the timer that generates the pwm for the drv8302 (3pwm mode) and the same timer is used to trigger the adcs (the adcs are triggered on the lvel change of the yellow signal, so on both edges). The red and cyan signals are the phase A and phase B low gates output generated by the drv8302.

From the images I think the adc (with the dma transfer without interrupt) is triggered in the correct point (am I wrong?)

In the last image the green signal is one of the two current opamp outputs and you can see the yellow signal (adc convertion trigger) is aligned.

Check the latest commit to the dev branch

1 Like

I changed modified the code as in the fix…but I’m still having problems.

When I stop the motor by hand the Iq increase almost to the target but the Vq (green signal) decrease (why?) I expect also the Vq to raise .

As you can see the Iq and Id still some kind of sinewave. If I increase the target current the amplitude of the Id increase (from 1A to 2A).

Maybe is the low pass filter in the Iq/Id too low? (now is 0.01).

How are the phase currents looking like

The current phases

The values are plotted every 5 ms

Iq looks much better after the fix.
Id not being flat could be due to your position sensor.

Here is an example I was simulating.
You are using a hall sensor without smoothing ?

Yes Iq it’s better, the issue still present on the Id current.
I’m using the AS5048 sensor with the smoothing part, I initialize the “parts” in this way

    smoothSensor = new SmoothingSensor(spiAngleSensor, motor);


	// Initialize motor hardware

	motor->PID_current_q.P 			= 0.2f;
	motor->PID_current_d.P 			= 0.2f;

	motor->PID_current_q.I 			= 0.0f;
	motor->PID_current_d.I 			= 0.0f;

	motor->PID_current_q.D 			= 0.0f;
	motor->PID_current_d.D 			= 0.0f;

	motor->LPF_current_q.Tf 		= 0.01f;
	motor->LPF_current_d.Tf 		= 0.01f;

	motor->PID_current_q.limit 		= 20.0f;
	motor->PID_current_d.limit 		= 20.0f;

	motor->PID_current_q.output_ramp 	= 1000.0f;
	motor->PID_current_d.output_ramp 	= 1000.0f;

	motor->velocity_limit 	= RPM_LIMIT * _RPM_TO_RADS;
	motor->current_limit 	= CURRENT_LIMIT; // [A]

	//current_sense->gain_a *= -1;
	//current_sense->gain_b *= -1;
	//current_sense->skip_align = true;

	// Initialize hardware current sense

	// link motor and current sense

	// Initialize motor FOC

There is something wrong with my “initiliaze” sequence?

Can you plot the angle also together with id and iq?

yes, sure…
the green line is the mechanical angle readed from the sensor with the smoothing. I noticed that also the Iq goes better or worst depending on what electrical angle the library find at the initialization of the system.

The mechanical angle seems ok.
Is your number of pole pairs correct ?

yes, the motor ha 40 magnets so 20 pole pair, also during the initial check I Pass the pp check. The motor is high power bldc (eaglepower LA8308 160kv)

What modulation are you using?

KIndly, can you share how did you smooth the hall sensors? Thanks

I’m using the SpaceVectorPWM

ok then I am confused
I am not sure why Id is like that

ah wait, shouldn’t it be this:


instead of this:

my “smoothSensor” variable it’s a pointr, so it’s correct to pass it without the “&”… I tried without the smoothing part and the errors on the Iq/Id still there, so maybeit’s not a problem with the smoothing part. A wrong zero electric angle could create this kind of problem?