ESP32S3 + DRV8302 + Current Sense = CRASH

Hello guys,

I’ve promised to myself to solve this issue…but no way! I really need your help. Here below my configuration:

  • ESP32S3
  • DRV8302 driver
  • Motor with phase resistance 2Ohm
  • Encoder: 500ppr

Here the schematics:

Here below the code:

#include <Arduino.h>
// SE ABILITI SimpleFOC deve abilitarlo anche nel platformio.ini!!!!!!
#include <SimpleFOC.h>

// DRV8302 pins connections
#define   INH_A 11
#define   INH_B 10
#define   INH_C 9
#define   EN_GATE 8
#define   M_PWM 2 
#define   M_OC 1
#define   OC_ADJ 3

#define IOUTA 15
#define IOUTB 16
#define IOUTC 17

// motor instance
BLDCMotor motor = BLDCMotor(1);

// driver instance
BLDCDriver3PWM driver = BLDCDriver3PWM(INH_A, INH_B, INH_C, EN_GATE);

// DRV8302 board has 0.005Ohm shunt resistors and the gain of 12.22 V/V
LowsideCurrentSense cs = LowsideCurrentSense(0.005f, 12.22f, IOUTA, IOUTB, IOUTC);

// encoder instance
Encoder encoder = Encoder(4, 5, 500);
// channel A and B callbacks
void doA(){encoder.handleA();}
void doB(){encoder.handleB();}

// instantiate the commander
Commander command = Commander(Serial);
void doTarget(char* cmd) { command.scalar(&motor.target, cmd); }
void doMotor(char* cmd) { command.motor(&motor, cmd); }

void setup() { 

  // DRV8302 specific code
  // M_OC  - enable over-current protection
  pinMode(M_OC,OUTPUT);
  digitalWrite(M_OC,LOW);
  // M_PWM  - enable 3pwm mode
  pinMode(M_PWM,OUTPUT);
  digitalWrite(M_PWM,HIGH);
  // OD_ADJ - set the maximum over-current limit possible
  // Better option would be to use voltage divisor to set exact value
  pinMode(OC_ADJ,OUTPUT);
  digitalWrite(OC_ADJ,HIGH);
  
  // initialize encoder sensor hardware
  encoder.init();
  encoder.enableInterrupts(doA, doB); 

  // link the motor to the sensor
  motor.linkSensor(&encoder);

  // driver config
  // power supply voltage [V]
  driver.voltage_power_supply = 24;
  driver.init();
  // link driver
  motor.linkDriver(&driver);

  // link current sense and the driver
  cs.linkDriver(&driver);

  // limiting motor movements
  motor.phase_resistance = 2.0; // [Ohm]
  motor.current_limit = 0.3;   // [Amps] - if phase resistance defined
  //motor.voltage_limit = 1;   // [V] - if phase resistance not defined
  motor.velocity_limit = 5; // [rad/s] cca 50rpm

  // aligning voltage
  motor.voltage_sensor_align = 1;

  // set motion control loop to be used
  //motor.torque_controller = TorqueControlType::voltage;
  motor.controller = MotionControlType::velocity;

  // controller configuration based on the control type 
  // velocity PID controller parameters
  // default P=0.5 I = 10 D = 0
  motor.PID_velocity.P = 0.025;
  motor.PID_velocity.I = 0.2;
  motor.PID_velocity.D = 0.0;
  // jerk control using voltage voltage ramp
  // default value is 300 volts per sec  ~ 0.3V per millisecond
  motor.PID_velocity.output_ramp = 1000;

  // velocity low pass filtering
  // default 5ms - try different values to see what is the best. 
  // the lower the less filtered
  motor.LPF_velocity.Tf = 0.01;


  // use monitoring with serial 
  Serial.begin(115200);
  // comment out if not needed
  motor.useMonitoring(Serial);

  // initialize motor
  motor.init();

  // initialize current sense
  cs.init();
  motor.linkCurrentSense(&cs);  
  // driver 8302 has inverted gains on all channels
  cs.gain_a *= -1;
  cs.gain_b *= -1;
  cs.gain_c *= -1;
  cs.skip_align = true;

  // align sensor and start FOC
  motor.initFOC();

  // set the initial motor target
  // motor.target = 0.2; // Amps - if phase resistance defined  
  motor.target = 1; // Volts 

  // add target command T
  // command.add('T', doTarget, "target current"); // - if phase resistance defined
  command.add('T', doTarget, "target velocity");
  command.add('M',doMotor, "motor");

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

void loop() {
  // main FOC algorithm function
  motor.loopFOC();

  // Motion control function
  motor.move();

  motor.monitor();
  // user communication
  command.run();
}

If I do not use the current sense, everything works perfectly (PID parameters tuned accordingly).
The problem arises when the cs.init() (current sense initialization) happens: the ESP32S3 is crashing with core dump, exactly if I uncomment this function. I’ve searched everywhere inside the community forum but I really have not found any solution on this…I’m struggling since 4 days now but no way…thank you!!!

Hey @Davide_Parati,

This might be a bug in the library, we have few reported issues with the esp32s3 and current sensing.

Did you maybe see the issues: [BUG]ESP32-S3 block in InlineCurrentSense init · Issue #320 · simplefoc/Arduino-FOC · GitHub and [BUG] ESP32-S3 Stuck in adc functions · Issue #295 · simplefoc/Arduino-FOC · GitHub

There might be a solution to this bug

Oh! Thank you so much @Antun_Skuric!!