I’m trying to FOC Current Mode of Torque Control using Arduino Mega Board and BL5057 motor. When I run the code below, the current is too large, and the current gradually decreases and the motor stops running. What’s the problem? Which part should I modify?
#include <SimpleFOC.h>
// BLDC motor & driver instance
BLDCMotor motor = BLDCMotor(2);
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);
// encoder instance
Encoder encoder = Encoder(2, 3, 512);
// channel A and B callbacks
void doA(){encoder.handleA();}
void doB(){encoder.handleB();}
// current sensor
InlineCurrentSense current_sense = InlineCurrentSense(0.01, 50, A0, A2);
// instantiate the commander
Commander command = Commander(Serial);
void doTarget(char* cmd) { command.scalar(&motor.target, cmd); }
void setup() {
// initialize encoder sensor hardware
encoder.init();
encoder.enableInterrupts(doA, doB);
// link the motor to the sensor
motor.linkSensor(&encoder);
// driver config
// power supply voltage [V]
driver.voltage_power_supply = 24;
driver.init();
// link driver
motor.linkDriver(&driver);
// current sense init hardware
current_sense.init();
// link the current sense to the motor
motor.linkCurrentSense(¤t_sense);
current_sense.gain_b *= -1;
current_sense.skip_align = true;
motor.current_limit = 0.5; // amps
// motor.PID_velocity.limit = motor.current_limit;
motor.voltage_limit = 5; //V
// motor.PID_velocity.limit = motor.voltage_limit;
// set torque mode:
motor.torque_controller = TorqueControlType::foc_current;
// set motion control loop to be used
motor.controller = MotionControlType::torque;
// foc current control parameters (Arduino UNO/Mega)
motor.PID_current_q.P = 5;
motor.PID_current_q.I = 10;
motor.PID_current_d.P = 5;
motor.PID_current_d.I = 10;
motor.LPF_current_q.Tf = 0.01;
motor.LPF_current_d.Tf = 0.01;
// use monitoring with serial
Serial.begin(115200);
// comment out if not needed
motor.useMonitoring(Serial);
// motor.monitor_variables = _MON_CURR_D | _MON_CURR_Q;
// downsampling
// motor.monitor_downsample = 100; // default 10
// initialize motor
motor.init();
// align sensor and start FOC
motor.initFOC();
// add target command T
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();
// motor.monitor();
// user communication
command.run();
}
I don’t know, but what happens if you start with voltage for torque control? I would stick with the examples and get things working in a basic manner before trying to jump in and try something off the beaten track. This stuff is a long way from plug and play, there seems to be a lot of people that think it’s a lot more plug and play than it is.
Well, if you have a problem with your current sense, it could cause the motor to stop moving…
Is the current sense embedded on the board? The MEGA does not have this capability, so did you add some sense resistors ?
Do you have an extra PCB for current sensing?
Oh, I’m sorry. I didn’t know I didn’t say that. At first, the motor will run well, but as the current gradually decreases, it will stop in the middle and then operate again. I’ve added and modified some other codes in the code above, is there anything else I should add or modify?
PID settings are individual to the setup, and can depend on many factors. The MCU speed is definitely one of them, as it will change the time required for an iteration, and thereby quite significantly affect the result. Generally speaking control is better on faster systems.
Often the loop speed will affect the value you want to use for the LPF constant too.
Yes, I think we’re already discussing it in the other thread. For foc_current mode you have to tune the current PIDs, but first you should make sure the other, easier modes are working for you.
There are some better resources out there and there are some forum posts on this already, on tuning PID controllers. Beware it is a bit laborious and complicated, there are automated systems and there are autotune libraries for Arduino however I was never able to get them working.
and I want to ask, now I have successfully run the closed loop mode voltage, but if we want to change the motor, we have to setting the PP motor, can we replace the motor without resetting the PP motor (to autotune PP)?.
The number of pole pairs is a required input parameter. If you want to discover the PP automatically, this is possible in some applications, if you have an encoder or position sensor, but you’ll have to add the code for it yourself…