Looking for support for FOC control of a NEMA 17 Stepper Motor, with as5600 magnetic sensor, L298N motor driver, and Arduino Mega 2560 MCU.
Open loop control works, (but noisy), but when I switch over to closed loop, the motor jitters.
I have been working in FOC Studio to tune the PID values, but with no luck. I have ran the magnetic encoder standalone example code and have received great values from the sensor when manually turning the motor shaft. What am I missing?
See the code below:
#include <SimpleFOC.h>
StepperMotor motor = StepperMotor(50);
StepperDriver4PWM driver = StepperDriver4PWM(5,6,9,10);
// Example of AS5600 configuration
MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);
// commander interface
Commander command = Commander(Serial);
void doMotor(char* cmd){ command.motor(&motor, cmd); }
void setup() {
// initialize encoder sensor hardware
sensor.init();
// link the motor to the sensor
motor.linkSensor(&sensor);
// choose FOC modulation
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
// power supply voltage [V]
driver.voltage_power_supply = 24;
driver.init();
// link the motor to the sensor
motor.linkDriver(&driver);
// set control loop type to be used
motor.controller = MotionControlType::torque;
// controller configuration based on the control type
motor.PID_velocity.P = 0;
motor.PID_velocity.I = 0;
motor.PID_velocity.D = 0;
// default voltage_power_supply
motor.voltage_limit = 24;
// velocity low pass filtering time constant
motor.LPF_velocity.Tf = 0.01;
// angle loop controller
motor.P_angle.P = 0;
// angle loop velocity limit
motor.velocity_limit = 50;
// use monitoring with serial for motor init
// monitoring port
Serial.begin(115200);
// comment out if not needed
motor.useMonitoring(Serial);
// initialise motor
motor.init();
// align encoder and start FOC
motor.initFOC();
// set the initial target value
motor.target = 2;
// define the motor id
command.add('M', doMotor, "motor");
// Run user commands to configure and the motor (find the full command list in docs.simplefoc.com)
Serial.println(F("Motor commands sketch | Initial motion control > torque/voltage : target 2V."));
_delay(1000);
}
void loop() {
// iterative setting FOC phase voltage
motor.loopFOC();
motor.monitor();
// iterative function setting the outter loop target
// velocity, position or voltage
// if tatget not set in parameter uses motor.target variable
motor.move();
// user communication
command.run();
}