Current sensing issue

I’m using a SimpleFOCShield. Please see below my Cq at a constant speed of 0.1 rad/s. The sinusoid is clearly visible, but there’s a lot of noise. Is that to be expected ? Thanks.

I’m asking about noise because I’m investigating an issue with my current sensing:

  • The dc_current mode works well, with a Cq PID of 2.0, 50.0, 0.0
  • The foc_current mode works only if the Cd PID is as low as 2.0, 0.0, 0.0. Any increase in “I” puts the motor into uncontrolled max speed rotation.

Any idea what’s wrong?

Hi Quentin,

I’m not sure I can help you, but can I ask some more details?

Could you let us know which MCU you are using with the SimpleFOC Shield, and which version of the library?
If you don’t mind, could you also post the code you are using to test?

Could you also post the encoder you’re using?

Optical encoders with high CPR’s are very heavy on the CPU. Not calling LoopFOC() often enough results in exactly the issues you’re describing.

Thanks @runger , thanks @MotorController.

The MCU is a STM32-L476RG (Nucleo board). I’m using a magnetic encoder (AS5048A) connected with SPI. My loop in foc_current mode takes a little more than 1ms.

Here is my code:

    //*********** Setup ************

	// Init and link the magnetic sensor on SPI3 

	// Init and link the driver
	driver.voltage_power_supply = 14.8;

	// Init and link the current sense
	current_sense.gain_b *=-1;
	current_sense.skip_align = true;

	// Control loop type and torque mode
	motor.torque_controller = TorqueControlType::foc_current;
	motor.controller = MotionControlType::angle;

	motor.sensor_offset = -1.9; = 0;

	// SimpleFOCStudio Generated Code -----
	// velocity loop PID
	motor.PID_velocity.P = 0.1;
	motor.PID_velocity.I = 2.0;
	motor.PID_velocity.D = 0;
	motor.PID_velocity.output_ramp = 1000.0;
	motor.PID_velocity.limit = 6;
	// Low pass filtering time constant
	motor.LPF_velocity.Tf = 0.005;
	// angle loop PID
	motor.P_angle.P = 5.0;
	motor.P_angle.I = 0.0;
	motor.P_angle.D = 0.0;
	motor.P_angle.output_ramp = 0;
	motor.P_angle.limit = 50.0;
	// Low pass filtering time constant
	motor.LPF_angle.Tf = 0.005;
	// current q loop PID
	motor.PID_current_q.P = 2.0;
	motor.PID_current_q.I = 50;
	motor.PID_current_q.D = 0;
	motor.PID_current_q.output_ramp = 0.0;
	motor.PID_current_q.limit = 5.0;
	// Low pass filtering time constant
	motor.LPF_current_q.Tf = 0.005;
	// current d loop PID
	motor.PID_current_d.P = 2.0;
	motor.PID_current_d.I = 0;
	motor.PID_current_d.D = 0;
	motor.PID_current_d.output_ramp = 0.0;
	motor.PID_current_d.limit = 5.0;
	// Low pass filtering time constant
	motor.LPF_current_d.Tf = 0.005;
	// Limits
	motor.velocity_limit = 50.0;
	motor.voltage_limit = 5.0;
	//motor.current_limit = 0.2;
	// pwm modulation settings
	motor.foc_modulation = SpaceVectorPWM;
	motor.modulation_centered = 1.0;


	motor.monitor_variables = 0xFF; // Monitor all values
	command.add('M', doMotor, (char*)"motor");
	Serial.println(F("Motor commands sketch | Initial motion control > torque/voltage : target 1V."));

    motor.initFOC(4.3, Direction::CCW);

    //*********** Loop ************


Hey @quentin,
Even though in the graph it seems like the noise is high, it actually isn’t.
You are measuring hte current or ±8miliamps, which is really low current and it is close to the precision of the sensor. Once you get to a bit larger currents you should not have much noise, at least much less than this :smiley:

What you’re seing in the graph is not motor’s sinusoid of one of the phases you’re seing the q current which is the dc current. It does not oscilate.
But as we use current component q as our control signal in foc_current mode, what you are seeing is your control output for the velocity loop.
As 0.1 rad/s is quiet low I imagine that the motor stalls, then then the integrator in the velocity PID builds up the current, creating the upward slope of the curbe, and when your motor starts, it quickly tries to slow it down, decreasing the q current value. This creates the downward slope in your graph.
So this is not sine wave of one of the phase currents, but your control signal. :smiley:

In terms of the setup: Nucleo + SPI AS5048 + shield will work great in currentmode.

Regarding the code and the PID parameters, I’d suggest you to keep the PID parameters of the q qnd d currents the same. If you change one, do the same for the other one.

I am not sure what is the the issue here, but I would definitely do with lower P gains (like 0.5 ) and lower I gains (10) for starters and see how the system reacts.

Aslo I’d really suggest you to start in the torque mode, not in velocity or angle.
Make sure, when you set your torque target to 0, you can spin your motor freely, almost with no resistance. If that works then you can hold the motor with your hand and change the torque (in the case of foc_current mode that will be current_q target value) and you should be able the feel the force your motor is applying to you augmenting linearly with the target you’re seting.

1 Like

@Antun_Skuric , thanks a million for the detailed explanation.

Torque+dc_current and torque+FOC_current have never worked with my setup. As soon as I set these combinations, the motor start spinning at a moderate speed with regular jerks (even with target=0). Does it tell something about a possible issue? All other mode combinations work as expected.

I did some more tests with “torque+dc_current”. I’m unable to achieve null speed with target=0.

Here, in the middle of the timeline, I switched Cq’s PID from (0.1; 0; 0) to (0.1; 1; 0):

Here, in the middle of the timeline, I switched Cq’s PID from (0.1; 0; 0) to (0.1; 0.1; 0):

Whatever I do with Cq’s PID, voltage will always goes up or down and the motor will always start spinning. Does it make sense?

Hey @quentin,
I am not sure what is happening here,especially since the voltages in your plots are really low 0.06V on the upper one and -0.003 V on the lower one.

Can you confirm that in the voltage mode when you set 5V target voltage and let your motor spin, the current q is very low and once you stop the motor by hand you have much higher current cca 0.2 - 1 Amps?
Can you show us the current q and current d in the studio plots when the motor drifts away?

1 Like

Thanks a lot @Antun_Skuric.

Here is what happens when I stop the motor by hand in voltage mode with target=3V:

Each time I stop the motor by hand, the current changes to a different mean value:

Once the motor is stopped by hand, if I turn it, the current is proportional to the motor angle. So, by turning it slowly and continuously, I can draw a nice wave:

Is it the expected behavior?

I assume what I descried above is not the expected behavior. There is definitely something wrong with my setup. I found another way to expose the issue, so I will create another topic.