FOC_Current Mode Issue

Hi all, currently I’m trying to use FOC current mode, with a velocity motion controller.
Right now im using:
SimpleFOC Board v2.04

Motor: Ipower GM 3506 Motor
Encoder : AS5048 SPI
MCU : Arduino Mega

Here is My code:

#include <SimpleFOC.h>

// BLDC motor & driver instance
float target_current = 0;

BLDCMotor motor = BLDCMotor(11);
BLDCDriver3PWM driver = BLDCDriver3PWM(5, 9, 6, 8);
InlineCurrentSense current_sense = InlineCurrentSense(0.01, 50.0, A1, A3);
MagneticSensorSPI sensor = MagneticSensorSPI(48, 14, 0x3FFF);

// instantiate the commander
Commander command = Commander(Serial);
void doMotor(char* cmd) { command.motor(&motor, cmd); }
void doTarget(char* cmd) { command.scalar(&target_current, cmd); }

void setup() {
// initialize encoder sensor hardware
// link the motor to the sensor

current_sense.skip_align = true;
current_sense.gain_b *= -1;
// driver config
// power supply voltage [V]
driver.voltage_power_supply = 12;
driver.pwm_frequency = 8000;
// link driver

// set the torque control type
motor.phase_resistance = 5.57;
motor.torque_controller = TorqueControlType::foc_current;

// set motion control loop to be used
motor.controller = MotionControlType::velocity;

  motor.PID_current_q.P = 3;
  motor.PID_current_q.I= 10;
  motor.PID_current_q.D= 0;
  motor.LPF_current_q.Tf = 0.01;

  motor.PID_current_d.P= 3;
  motor.PID_current_d.I = 10;
  motor.PID_current_d.D = 0;

  motor.LPF_current_d.Tf = 0.005; 

  motor.PID_velocity.P = 0.2f;
  motor.PID_velocity.I = 10;
  motor.PID_velocity.D = 0.001f;
  motor.PID_velocity.output_ramp = 1000;
  motor.LPF_velocity.Tf = 0.01;

motor.voltage_limit = 5;
// setting the limits
motor.current_limit = 0.5;


motor.monitor_variables = _MON_CURR_D| _MON_CURR_Q | _MON_VOLT_D | _MON_VOLT_Q | _MON_TARGET | _MON_VEL | _MON_ANGLE;
// initialize motor
// align sensor and start FOC

// add target command M
command.add(‘M’, doMotor, “motor”);
command.add(‘T’, doTarget, “target current”);

Serial.println(F(“Motor ready.”));
Serial.println(F(“Set the target current using serial terminal:”));

void loop() {

// main FOC algorithm function

// Motion control function
// user communication;

I already tried my motor with close loop velocity and angle, all is working fine. But now with this foc_current mode, I cannot achieve the maximum torque and the motor is very vibrating when I tried to raise the current I.

I also already tried with voltage mode, but the motor is not spinning smoothly and every time after initializing, the motor is spinning even though the target_current is zero. After i hold the motor with hand for a moment, the motor stop spinning.

Please correct me if I’m there is any mistake in my coding or if my posting is not clear.
Thank you!

I have the exact same problem.

Only difference is that I use:

  • SimpleFOC board 2.03
  • Arduino Uno


Hello and welcome to the forum!

On a first glance I don’t really see problems, let’s see if others could read your code and see somethings.

May I suggest that you use the SimpleFOC Studio tuner to tune your parameters?


1 Like