LOL... ChatGPT writing SimpleFOC code

Brave new world.

The last one with the angle sensor in a closed loop is particularly funny.



I’m too busy to test the actual code but it got the basics correctly.

Cheers,
Valentine

4 Likes

I guess the learning curve to teach chatGPT to do the right thing is much longer than the learning curve to write the simpleFOC code?

It would be cool to have a request template, were the user only has to fill in blanks and then send it to chatGPT.

You can ask directly in github now

#include <Arduino.h>
#include <SimpleFOC.h>

// Motor instance
BLDCMotor motor = BLDCMotor(7);

// Motor driver instance
BLDCDriver3PWM driver = BLDCDriver3PWM(PA8, PA9, PA10, PB6);

// Encoder instance
Encoder encoder = Encoder(PA0, PA1, 2048);

// Interrupt routine initialization
void doA(){encoder.handleA();}
void doB(){encoder.handleB();}

void setup() {
    // Motor driver configuration
    driver.voltage_power_supply = 12; // Power supply voltage
    driver.init();
    motor.linkDriver(&driver);

    // Encoder configuration
    encoder.init();
    encoder.enableInterrupts(doA, doB);
    motor.linkSensor(&encoder);

    // Controller configuration
    motor.controller = MotionControlType::angle;
    motor.PID_velocity.P = 0.5;
    motor.PID_velocity.I = 10;
    motor.PID_velocity.D = 0;
    motor.PID_velocity.output_ramp = 1000;
    motor.LPF_velocity.Tf = 0.01;
    motor.P_angle.P = 20;
    motor.velocity_limit = 20;
    motor.voltage_limit = 3;

    // Initialize motor
    motor.init();
    motor.initFOC();

    Serial.begin(115200);
    Serial.println("Motor ready.");
    _delay(1000);
}

void loop() {
    // User input for target angle
    static float target_angle = 0;
    if (Serial.available()) {
        target_angle = Serial.parseFloat();
    }

    // Move the motor to target angle
    motor.move(target_angle);
    motor.loopFOC();

    // Print current angle
    Serial.println(motor.shaft_angle);
}

2 Likes