Hi there,
I’ve just set up ESP32 with L298N motor driver and AS5600 sensor.
The sensor reads perfectly well using the basic getting started code for sensor only, so there’s no problem there.
I’m using the Example code for the Backdrivable Stepper Motor (slightly modified for AS5600 sensor) and I’m not sure how to control it.
When I plug in the ESP32, the motor aligns to the sensor successfully. But what now?
I’ve tried setting targets on the serial monitor using commands like M0.5 M10 M200 but all it does is increase power supply current.
- How do I make it spin in one direction?
- How do I set position in torque control mode?
- How do I set speed in torque control mode?
I’ve tried switching to Velocity control mode (MC3) but I get some very erratic results with horrible noise and vibrations going backwards and forwards… I had to unplug it for fear of frying something (already smoked up one L298N by driving it using 24v using this library!)
The only value that produces forward motion in velocity mode seems to be M10 where I get a smooth spin in one direction. But any other value (M0.5, M100, M15) produces erratic backwards and forwards motion.
M-10 also doesn’t change direction in velocity mode… only spins in one direction.
M6 seems to make it spin in the opposite direction, and then M6.1 and M6.2 changes the speed slightly. But that’s about it… no other values work.
My L298N was extremely hot throughout the test. Motor voltages all set to 12v. Power supply is currently 12v.
EDIT: I changed the motor voltage to 3.7 (stepper model datasheet voltage) and the driver is now running cooler. Excessive noise is also gone. But I still have no idea how to set direction, angle, etc! It all just goes in random directions and spins the other way when you try to stop it manually…
All I want is to just get the wheel spinning in one direction for a start, then to be able to set speed and position.
Thanks!
TwinLizzie
//Code:
#include <SimpleFOC.h>
// Stepper motor instance
StepperMotor motor = StepperMotor(50);
// Stepper driver instance
StepperDriver4PWM driver = StepperDriver4PWM(34, 35, 32, 33, 18, 19);
// encoder instance
//Encoder encoder = Encoder(A1, A2, 2048);
// channel A and B callbacks
//void doA(){encoder.handleA();}
//void doB(){encoder.handleB();}
MagneticSensorI2C as5600 = MagneticSensorI2C(0x36, 12, 0x0E, 4);
// commander interface
Commander command = Commander(Serial);
void onMotor(char* cmd){ command.motor(&motor, cmd); }
void setup() {
// initialize encoder sensor hardware
as5600.init();
//encoder.enableInterrupts(doA, doB);
// link the motor to the sensor
motor.linkSensor(&as5600);
// choose FOC modulation
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
// power supply voltage [V]
driver.voltage_power_supply = 12;
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.2;
motor.PID_velocity.I = 20;
motor.PID_velocity.D = 0;
// default voltage_power_supply
motor.voltage_limit = 3.7;
// velocity low pass filtering time constant
motor.LPF_velocity.Tf = 0.01;
// angle loop controller
motor.P_angle.P = 20;
// 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', onMotor, "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();
// 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();
}