simplefoc / Arduino-FOC

Arduino FOC for BLDC and Stepper motors - Arduino Based Field Oriented Control Algorithm Library
https://docs.simplefoc.com
MIT License
2.06k stars 531 forks source link

[BUG] WANNA HELP Motor not rotating, `Init FOC failed` error on both esp32sheild & arduino uno simplefocshield v2.04 #441

Closed tianrking closed 6 days ago

tianrking commented 6 days ago

Description: I am using an AS5600 magnetic encoder and a BLDC motor with SimpleFOC library to control the motor, but it is not rotating as expected. The motor does not react to changes in the target angle and there are no significant errors, but the following logs indicate issues:

software env arduino v2.0.17 + simplefoc 2.3.3

Code:

#include <SimpleFOC.h>

// Magnetic sensor setup
MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);

// Motor setup
BLDCMotor motor = BLDCMotor(7);
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);

// Target angle and velocity variables
float target_angle = 0;
float target_velocity = 0;

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

// Set the target angle through serial
void doTarget(char* cmd) {
    command.scalar(&target_angle, cmd);
}

// Motor control command
void onMotor(char* cmd) {
    command.motor(&motor, cmd);
}

void setup() {
    // Initialize magnetic sensor
    sensor.init();
    motor.linkSensor(&sensor);

    // Driver configuration
    driver.voltage_power_supply = 12;
    driver.init();

    // Link motor and driver
    motor.linkDriver(&driver);

    // Set FOC modulation
    motor.foc_modulation = FOCModulationType::SpaceVectorPWM;

    // Motion control loop setup
    motor.controller = MotionControlType::angle;

    // Controller configuration
    motor.PID_velocity.P = 0.2f;
    motor.PID_velocity.I = 20;
    motor.PID_velocity.D = 0;
    motor.voltage_limit = 6;
    motor.LPF_velocity.Tf = 0.01f;
    motor.P_angle.P = 20;
    motor.velocity_limit = 20;

    // Serial monitor initialization
    Serial.begin(115200);
    motor.useMonitoring(Serial);

    // Initialize motor
    motor.init();

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

    // Command configuration
    command.add('T', doTarget, "target angle");
    Serial.println(F("Motor ready."));
    Serial.println(F("Set the target angle using serial terminal:"));
    _delay(1000);
}

void loop() {
    motor.loopFOC();
    motor.move(target_angle);
    motor.monitor();
    command.run();
}

Log 1 (Without calling motor.initFOC()):

MOT: Monitor enabled!
MOT: Init
MOT: Enable driver.
Motor ready.
Set the target angle using serial terminal:
0.0000    0.0000    0.0000    0.0000
0.0000    0.0000    0.0000    0.0000
2.0000    6.0000    0.0000    0.0000
2.0000    6.0000    0.0000    0.0000
2.0000    6.0000    0.0000    0.0000
2.0000    6.0000    0.0000    0.0000
Warn: \r detected!
1.000
1.0000    6.0000    0.0000    0.0000
Warn: \r detected!
1.000
Warn: \r detected!
1.000
1.0000    6.0000    0.0000    0.0000

Log 2 (With motor.initFOC()):

MOT: Monitor enabled!
MOT: Init
MOT: Enable driver.
MOT: Align sensor.
MOT: Failed to notice movement
MOT: Init FOC failed.
Motor ready.
Set the target angle using serial terminal:
0.0000    0.0000    0.0000    0.0000
0.0000    0.0000    0.0000    0.0000

Problem:

What I've tried:

Expected behavior:

Additional information:

Can anyone provide assistance or insights into what could be causing the issue?

runger1101001 commented 6 days ago

For support, please post to our forums.

Based on the output, I can't really see an error - in the case of initFOC() we can see that no movement is detected, so this means either the sensor or the driver are not working as expected.

If you remove the initFOC() from this code example, then you also need to change the mode to an open loop mode.

In general, angle mode is the most complicated one, and can only work once you have tuned your PID settings. Please start in torque mode and make sure the motor is moving well in this mode, then use velocity mode and tune the velocity PID, and then finally try angle mode last.

I'll close this for now as there is no real "bug" that we have found yet. To get support with your setup, please use our Discord or community forums.