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() {
Serial.begin(115200);
// initialize encoder sensor hardware
sensor.init();
// link the motor to the sensor
motor.linkSensor(&sensor);current_sense.init();
motor.linkCurrentSense(¤t_sense);
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;
driver.init();
// link driver
motor.linkDriver(&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.useMonitoring(Serial);
motor.monitor_variables = _MON_CURR_D| _MON_CURR_Q | _MON_VOLT_D | _MON_VOLT_Q | _MON_TARGET | _MON_VEL | _MON_ANGLE;
// initialize motor
motor.init();
// align sensor and start FOC
motor.initFOC();// 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:”));
_delay(1000);
}void loop() {
// main FOC algorithm function
motor.loopFOC();// Motion control function
motor.move(target_current);
motor.monitor();
// user communication
command.run();
}
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!