Hello,
I am trying to run a BLDC motor using esp32s. I tried using the example program “find pole pairs number” in the code I have modified lines
BLDCDriver6PWM driver = BLDCDriver6PWM(12, 14, 27, 26, 25, 33);
HallSensor sensor = HallSensor(32, 35, 34, 1);
driver.pwm_frequency = 25000;
driver.voltage_power_supply = 11;
driver.dead_zone = 0.20f; //4us dead time
Other than the above lines the program is same. In serial terminal I get pp= 1080.00/ -5672519.50 i.e pp=-0.00 and since pole pair number cannot be negative try changing search_voltage/ sensor configuration. Should the voltage_power_supply value given driver supply voltage or supply voltage given to the motor and should the pp_search_voltage given half the voltage_power_supply. Moreover I have also tried interchanging hall sensor cables but the same result occurs. I have also uploaded code for your reference. Kindly help
#include <SimpleFOC.h>
void serialReceiveUserCommand();
float target_voltage = 2;
// utility function enabling serial communication with the user to set the target values
// this function can be implemented in serialEvent function as well
void serialReceiveUserCommand() {
// a string to hold incoming data
static String received_chars;
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the string buffer:
received_chars += inChar;
// end of user input
if (inChar == '\n') {
// change the motor target
target_voltage = received_chars.toFloat();
Serial.print("Target voltage: ");
Serial.println(target_voltage);
// reset the command buffer
received_chars = "";
}
}
}
// BLDC motor instance
// its important to put pole pairs number as 1!!!
BLDCMotor motor = BLDCMotor(1);
// BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);
//esp32 pins
BLDCDriver6PWM driver = BLDCDriver6PWM(12, 14, 27, 26, 25, 33);
// Stepper motor instance
// its important to put pole pairs number as 1!!!
//StepperMotor motor = StepperMotor(1);
//StepperDriver4PWM driver = StepperDriver4PWM(9, 5, 10, 6, 8);
// magnetic sensor instance - SPI
// MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);
// magnetic sensor instance - I2C
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0X0C, 4);
// magnetic sensor instance - analog output
// MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);
//digital hall sensors
HallSensor sensor = HallSensor(32, 35, 34, 1);
// Interrupt routine intialisation
// channel A and B callbacks
void doA(){sensor.handleA();}
void doB(){sensor.handleB();}
void doC(){sensor.handleC();}
void setup() {
// check if you need internal pullups
// sensor.pullup = Pullup::USE_EXTERN;
sensor.pullup = Pullup::USE_INTERN;
// initialise magnetic sensor hardware
sensor.init();
// hardware interrupt enable
sensor.enableInterrupts(doA, doB, doC);
// link the motor to the sensor
motor.linkSensor(&sensor);
// pwm frequency to be used [Hz]
// for atmega328 fixed to 32kHz
// esp32/stm32/teensy configurable
driver.pwm_frequency = 25000;
// power supply voltage
// default 12V
driver.voltage_power_supply = 11;
driver.dead_zone = 0.20f; //0.10==2us //max is 0.04 for 25k Hz which gives 8us
driver.init();
motor.linkDriver(&driver);
// initialize motor hardware
motor.init();
// monitoring port
Serial.begin(115200);
// pole pairs calculation routine
Serial.println(“Pole pairs (PP) estimator”);
Serial.println("-\n");
// float pp_search_voltage = 4; // maximum power_supply_voltage/2
float pp_search_voltage = 6; // maximum power_supply_voltage/2
float pp_search_angle = 6*_PI; // search electrical angle to turn
// move motor to the electrical angle 0
motor.controller = MotionControlType::angle_openloop;
motor.voltage_limit=pp_search_voltage;
motor.move(0);
_delay(1000);
// read the sensor angle
sensor.update();
float angle_begin = sensor.getAngle();
_delay(50);
// move the motor slowly to the electrical angle pp_search_angle
float motor_angle = 0;
while(motor_angle <= pp_search_angle){
motor_angle += 0.01f;
sensor.update(); // keep track of the overflow
motor.move(motor_angle);
_delay(1);
}
_delay(1000);
// read the sensor value for 180
sensor.update();
float angle_end = sensor.getAngle();
_delay(50);
// turn off the motor
motor.move(0);
_delay(1000);
// calculate the pole pair number
int pp = round((pp_search_angle)/(angle_end-angle_begin));
Serial.print(F("Estimated PP : "));
Serial.println(pp);
Serial.println(F("PP = Electrical angle / Encoder angle "));
Serial.print(pp_search_angle*180/_PI);
Serial.print(F("/"));
Serial.print((angle_end-angle_begin)*180/_PI);
Serial.print(F(" = "));
Serial.println((pp_search_angle)/(angle_end-angle_begin));
Serial.println();
// a bit of monitoring the result
if(pp <= 0 ){
Serial.println(F("PP number cannot be negative"));
Serial.println(F(" - Try changing the search_voltage value or motor/sensor configuration."));
return;
}else if(pp > 30){
Serial.println(F("PP number very high, possible error."));
}else{
Serial.println(F("If PP is estimated well your motor should turn now!"));
Serial.println(F(" - If it is not moving try to relaunch the program!"));
Serial.println(F(" - You can also try to adjust the target voltage using serial terminal!"));
}
// set motion control loop to be used
motor.controller = MotionControlType::torque;
// set the pole pair number to the motor
motor.pole_pairs = pp;
//align sensor and start FOC
motor.initFOC();
_delay(1000);
Serial.println(F("\n Motor ready."));
Serial.println(F(“Set the target voltage using serial terminal:”));
}
// uq voltage
// float target_voltage = 2;
void loop() {
// main FOC algorithm function
// the faster you run this function the better
// Arduino UNO loop ~1kHz
// Bluepill loop ~10kHz
motor.loopFOC();
// Motion control function
// velocity, position or voltage (defined in motor.controller)
// this function can be run at much lower frequency than loopFOC() function
// You can also use motor.move() and set the motor.target in the code
motor.move(target_voltage);
// communicate with the user
serialReceiveUserCommand();
}