Hello everyone,
I am working on a custom BLDC motor controller based on an ESP32-S3 and a DRV8302 using SimpleFOC. I am using shunts to measure current on phases A and B only. Phase C does not have a current sensor (no shunt). When running the code, I get this error:
CS: Switch A-(C)NC
CS: Err B - all currents same magnitude!
I’m using BGM-5208-200 bldc motor. Could someone please help me with this issue?
Here is my code:
#include <SimpleFOC.h>
// Motor instance
BLDCMotor motor = BLDCMotor(7);
// Driver instance
BLDCDriver6PWM driver = BLDCDriver6PWM(7, 8, 14, 9, 16, 15, 6);
// Magnetic sensor instance
MagneticSensorSPI AS5x4x = MagneticSensorSPI(10, 14, 0x3FFF);
// DRV8302 board has 0.005 Ohm shunt resistors and a gain of 12.22 V/V
LowsideCurrentSense cs = LowsideCurrentSense(0.005f, 40.0f, 17, 18, _NC);
// Target velocity
float target_velocity = 30;
// Commander instance
Commander command = Commander(Serial);
// Commander command functions
void onTarget(char* cmd) {
command.scalar(&target_velocity, cmd);
}
void doMotor(char* cmd) {
command.motor(&motor, cmd);
}
void setup() {
// Sensor init
pinMode(5, OUTPUT);
digitalWrite(5, HIGH);
SimpleFOCDebug::enable(&Serial);
AS5x4x.init();
motor.linkSensor(&AS5x4x);
// Driver config
driver.pwm_frequency = 50000;
driver.dead_zone = 0.03;
driver.voltage_power_supply = 19;
driver.voltage_limit = 19;
// driver.voltage_limit = 50;
driver.init();
motor.linkDriver(&driver);
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
motor.controller = MotionControlType::velocity;
motor.voltage_limit = 11;
motor.velocity_limit = 300;
cs.linkDriver(&driver);
motor.voltage_sensor_align = 2.5;
motor.current_limit = 200;
// PID velocity
motor.PID_velocity.P = 0.8;
motor.PID_velocity.I = 2;
motor.PID_velocity.D = 0;
motor.PID_velocity.output_ramp = 10;
// Velocity filter
motor.LPF_velocity.Tf = 0.1;
// Current limit
// motor.current_limit = 10;
Serial.begin(115200);
motor.useMonitoring(Serial);
// Motor + FOC init
motor.init();
cs.init();
// drv8302 has inverted gains on all channels
cs.gain_a *= -1;
cs.gain_b *= -1;
cs.gain_c *= -1;
motor.linkCurrentSense(&cs);
motor.initFOC();
// Monitoring variables
motor.monitor_variables = _MON_TARGET | _MON_VEL | _MON_VOLT_Q;
motor.monitor_downsample = 500;
// Add target command 'T'
command.add('T', onTarget, "target velocity");
command.add('M', doMotor, "Motor");
Serial.println("Motor ready.");
_delay(1000);
}
void loop() {
motor.loopFOC();
motor.move(target_velocity);
motor.monitor();
command.run();
}
Thank you !

