About serial command when esp32 control 2 motors together

Hi Guys~ I made a esp32 simple foc board, for controlling 2 BLDC together. but i encountered some serial command problem when controlling this 2 motors.
i was intended to send “A3B4” from serial monitor window, to control MotorA to position/velocity/angle 3, and MotorB to 4. Before that, i have succeeded running close loop voltage torque control, based on that i modified the code below:

#include <SimpleFOC.h>
MagneticSensorI2C sensor_A = MagneticSensorI2C(AS5600_I2C);
MagneticSensorI2C sensor_B = MagneticSensorI2C(AS5600_I2C);

// BLDC motor & driver instance
BLDCMotor motor_A = BLDCMotor(11);
BLDCMotor motor_B = BLDCMotor(11);

BLDCDriver3PWM driver_A = BLDCDriver3PWM(32, 33, 25, 12);
BLDCDriver3PWM driver_B = BLDCDriver3PWM(14, 26, 27, 2);

// voltage set point variable
float target_voltage_A = 2;
float target_voltage_B = 2;

// instantiate the commander
Commander command = Commander(Serial);

void doTarget_A(char* cmd) { command.scalar(&target_voltage_A, cmd); }
void doTarget_B(char* cmd) { command.scalar(&target_voltage_B, cmd); }

void setup() {
  // initialise magnetic sensor hardware

  Wire1.begin(15, 13, 100000);

  // link the motor to the sensor
  // power supply voltage
  driver_A.voltage_power_supply = 12;

  driver_B.voltage_power_supply = 12;

  // aligning voltage 
  motor_A.voltage_sensor_align = 6;
  motor_B.voltage_sensor_align = 6;
  // choose FOC modulation (optional)
  motor_A.foc_modulation = FOCModulationType::SpaceVectorPWM;
  motor_B.foc_modulation = FOCModulationType::SpaceVectorPWM;
  // set motion control loop to be used
  motor_A.controller = MotionControlType::torque;
  motor_B.controller = MotionControlType::torque;

  // use monitoring with serial 
  // comment out if not needed

  // initialize motor
  // align sensor and start FOC

  // add target command T
  command.add('A', doTarget_A, "target voltage_A");
  command.add('B', doTarget_B, "target voltage_B");

  Serial.println(F("Motors Ready."));
  Serial.println(F("Set the target voltage using serial terminal, like A3B1:"));

void loop() {


  // user communication

i downloaded the code and succeeded the initialization, but these 2 motors shocked a lot, no matter what value i sent to them(except zero), and i only can send “Ax” and “Bx” separately, sending “AxBx” would only run “Ax”.
I succeeded running 2 motor control using"AxBx" based on SFOC lib v2.0 , but when it comes to V2.1, things changed :sneezing_face:… Forgive my poor C++ skills… could anyone give some suggestions about where is the problem?
Attach my esp32 FOC board, just to show off~ :rofl:


A few ideas you could look at:

  1. MotionControlType::torque - this is the most complex mode for motion control. It will probably require tuning your PIDs for the motor to move well. If you can, first check open-loop velocity mode, then try closed-loop velocity mode, and when that is working well, try torque mode.
  2. 100kHz I2C is very slow for FOC-control. If it is working well in open-loop, but less well in closed-loop, this could be your problem. You can try setting the speed to 400kHz (400000) but in the end getting good motion with 2xI2C sensors will be a challenge. SPI sensors or Encoders are much faster. Your ESP32 is a very fast chip. Your sensors are holding it back…
  3. Is it possible you have reversed your sensor connections? I’ve had this problem in multi-motor setups. If you mistakenly switch the sensors of the motors (e.g. by connecting the wrong cables, or using the wrong pin numbers) then you can get some very strange effects.

Hi @runger, thanks for quick reply~
I use voltage control torque, not the current. As you said, I2C set to 100k may be a problem. I’ll try 400k tomorrow.
And the sensor alignment, I messed them at first, too, and the initialization failed. after exchange the sensors, initialization is ok. Anyway, I would check it double.