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
Wire.setClock(100000);
Wire1.setClock(100000);
Wire.begin();
Wire1.begin(15, 13, 100000);
sensor_A.init(&Wire1);
sensor_B.init();
// link the motor to the sensor
motor_A.linkSensor(&sensor_A);
motor_B.linkSensor(&sensor_B);
// power supply voltage
driver_A.voltage_power_supply = 12;
driver_A.init();
motor_A.linkDriver(&driver_A);
driver_B.voltage_power_supply = 12;
driver_B.init();
motor_B.linkDriver(&driver_B);
// 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
Serial.begin(115200);
// comment out if not needed
motor_A.useMonitoring(Serial);
motor_B.useMonitoring(Serial);
// initialize motor
motor_A.init();
motor_B.init();
// align sensor and start FOC
motor_A.initFOC();
motor_B.initFOC();
// 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:"));
_delay(1000);
}
void loop() {
motor_A.loopFOC();
motor_B.loopFOC();
motor_A.move(target_voltage_A);
motor_B.move(target_voltage_B);
// user communication
command.run();
}
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
.. Forgive my poor C++ skills… could anyone give some suggestions about where is the problem?
Attach my esp32 FOC board, just to show off~ ![]()
