Arduino nano ESP32 not working encoder/angle_control (w/ stepper motor)

Hi!
I’m in a project with a nema 14 stepper motor, amt103 encoder and an arduino nano esp32.
The stepper motor does not work in close loop (simpleFOC). It works fine in open_loop.
Any suggestion?
Thank you for your help.

Could you share the code you are testing?

#include <SimpleFOC.h>

StepperMotor motor = StepperMotor(50);
StepperDriver4PWM driver = StepperDriver4PWM(8, 9, 10, 17, 7, 18);

Encoder encoder = Encoder(38, 47, 2048);

void doA(){encoder.handleA();}
void doB(){encoder.handleB();}

float target_angle = 0;

Commander command = Commander(Serial);
void doTarget(char* cmd) { command.scalar(&target_angle, cmd); }

void setup() {

encoder.init();
encoder.enableInterrupts(doA, doB);
motor.linkSensor(&encoder);
driver.voltage_power_supply = 12;
driver.init();
motor.linkDriver(&driver);
motor.voltage_sensor_align = 6;
motor.controller = MotionControlType::angle;
motor.PID_velocity.P = 0.2f;
motor.PID_velocity.I = 20;
motor.PID_velocity.D = 0;
motor.PID_velocity.output_ramp = 1000;
motor.LPF_velocity.Tf = 0.01f;
motor.P_angle.P = 20;
motor.voltage_limit = 6;
motor.velocity_limit = 17;

Serial.begin(115200);
motor.useMonitoring(Serial);
motor.init();
motor.initFOC();
command.add(‘T’, doTarget, “target angle”);

Serial.println(F(“Motor ready.”));
Serial.println(F(“Set the target angle using serial terminal:”));
_delay(1000);
}

void loop() {

motor.loopFOC();
motor.move(target_angle);
command.run();
}

Hey,

Can it be a problem of PinNames vs Pin Numbers?

Normally on ESP32 the pin numbers used in Arduino are equal to the ESP32 GPIO numbers.

But this is not the case on the Nano ESP32… here Arduino uses pin numbers which correspond to the usual Arduino Nano ones, different from the GPIO.

Due to a bug related to this topic, you have to use the GPIO numbers for the phase pins, I.e. the first four parameters to StepperDriver4PWM.

For all the other parameters like the encoder pins, stepper enable pins, etc you should use Arduino pin numbers.
This will be fixed in the next release, so that Arduino pin numbers are used everywhere.

Hi runger!
Thank you for your reply.
I’ve tried several ways, and it doesn’t work. The result is always this:
MOT: Align sensor.
MOT: Skip dir calib.
MOT: Zero elec. angle: 0.00
MOT: Ready.
Motor ready.
Set the target angle using serial terminal:

and when I enter a value the motor does not rotate.

In open_loop mode, it works with GPIO numbers.

…Nuno

Ah ok! I know what this is, I think.

We have a bug with the initialization of Stepper motors… you could use the previous version of the library or the dev branch version, which contains the necessary fix.

Perfect.
It works!
Thank you very much.
Nuno

1 Like

Hi @runger, I’ve been having the same issue for a few days and only solved it when I came across this post in the community. I’d strongly suggest a bug announcement as many other people might be facing the same issue and simply giving up on the lib.

Thanks!