Running two motors on one ESP32

Hello guys!

Just a quick question for you, if I want to control two motors with one ESP32 is it possible to do it like this or do I need to use RTOS or some other operating system?

Here is the code I was talking about

#include <SimpleFOC.h>

#define M1_PWM_U 16

#define M1_PWM_V 4

#define M1_PWM_W 32

#define M1_HA 25

#define M1_HB 26

#define M1_HC 27

#define M2_PWM_U 27

#define M2_PWM_V 26

#define M2_PWM_W 25

#define M2_HA 4

#define M2_HB 15

#define M2_HC 35

float target_velocity_common = 400.0; // rad/s

BLDCMotor motor1 = BLDCMotor(1);

BLDCMotor motor2 = BLDCMotor(1);

BLDCDriver3PWM driver1 = BLDCDriver3PWM(M1_PWM_U, M1_PWM_V, M1_PWM_W);

BLDCDriver3PWM driver2 = BLDCDriver3PWM(M2_PWM_U, M2_PWM_V, M2_PWM_W);

HallSensor sensor1 = HallSensor(M1_HA, M1_HB, M1_HC, 1);

HallSensor sensor2 = HallSensor(M2_HA, M2_HB, M2_HC, 1);

// void doA1(){sensor1.handleA();}

// void doB1(){sensor1.handleB();}

// void doC1(){sensor1.handleC();}

// void doA2(){sensor2.handleA();}

// void doB2(){sensor2.handleB();}

// void doC2(){sensor2.handleC();}

void setup() {

Serial.begin(115200);

SimpleFOCDebug::enable(&Serial);

sensor1.init();

sensor2.init();

motor1.linkSensor(&sensor1);

motor2.linkSensor(&sensor2);

driver1.voltage_power_supply = 12;

driver2.voltage_power_supply = 12;

if (!driver1.init() || !driver2.init()) {

Serial.println(“Driver init failed!”);

while (1);

}

motor1.linkDriver(&driver1);

motor2.linkDriver(&driver2);

motor1.controller = MotionControlType::velocity;

motor2.controller = MotionControlType::velocity;

motor1.voltage_sensor_align = 1;

motor2.voltage_sensor_align = 1;

motor1.PID_velocity.P = 0.01f;

motor1.PID_velocity.I = 0.1f;

motor1.PID_velocity.D = 0;

motor1.voltage_limit = 5;

motor1.PID_velocity.output_ramp = 100;

motor1.LPF_velocity.Tf = 0.05f;

motor1.sensor_direction = Direction::CW;

motor2.PID_velocity = motor1.PID_velocity;

motor2.voltage_limit = motor1.voltage_limit;

motor2.LPF_velocity.Tf = motor1.LPF_velocity.Tf;

motor2.sensor_direction = Direction::CW;

motor1.init();

motor2.init();

motor1.initFOC();

motor2.initFOC();

}

void loop() {

// FOC Loops

motor1.loopFOC();

motor2.loopFOC();

motor1.move(target_velocity_common);

motor2.move(target_velocity_common);

}

Yes, your approach will work (notice that I haven’t verified the implementation details except for one critical omission: you’re missing the motor.motion_downsample option, which is essential to prevent noise).

Thanks for your answer!

How exactly is the motor.motion_downsample to implement?

It’s a parameter specific to your system, just like the PID values. I usually start at 10 and modify it until my system behaves correctly.

thank you very much!