B-G431-ESC1 SimpleFOC Studio

Hi Sir,
This is related to this one.
https://community.simplefoc.com/t/b-g431b-esc1-simplefoc-driver/3066/10
I am trying to use simpleFoC Studio to fine tune PID parameter.
While, the SimpleFoC studio GUI did not reflash data.


I can manually send commands through GUI to B-G431-ESC1 board to control BLDC motor, but the GUI cannot receive correct data, showing unknow cmd err.

My code shows as below.

/**

  • B-G431B-ESC1 position motion control example with encoder

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

// Motor instance
BLDCMotor motor = BLDCMotor(7);
BLDCDriver6PWM driver = BLDCDriver6PWM(A_PHASE_UH, A_PHASE_UL, A_PHASE_VH, A_PHASE_VL, A_PHASE_WH, A_PHASE_WL);
LowsideCurrentSense currentSense = LowsideCurrentSense(0.003, -64.0/7.0, A_OP1_OUT, A_OP2_OUT, A_OP3_OUT);

// encoder instance
Encoder encoder = Encoder(A_ENCODER_A, A_ENCODER_B, 1000);

// Interrupt routine intialisation
// channel A and B callbacks
void doA(){encoder.handleA();}
void doB(){encoder.handleB();}
void doC(){encoder.handleIndex();}

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

void doMotor(char* cmd) { command.motor(&motor,cmd);}

void setup() {

// 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 = 12;
driver.init();
// link the motor and the driver
motor.linkDriver(&driver);

motor.foc_modulation = FOCModulationType::SpaceVectorPWM;

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

// current sensing
currentSense.init();
// no need for aligning
// currentSense.skip_align = true;
motor.linkCurrentSense(&currentSense);

// index search velocity [rad/s]
// motor.velocity_index_search = 3;
// aligning voltage [V]
motor.voltage_sensor_align = 1.2;
// if you are not using aligning voltage, you can set current limitation
motor.phase_resistance = 0.4; // [Ohm]
motor.current_limit = 3; // [Amps] - if phase res is defined

// set motion control loop to be used
motor.torque_controller = TorqueControlType::foc_current;

motor.controller = MotionControlType::angle;

// contoller configuration
// default parameters in defaults.h

// velocity PI controller parameters
motor.PID_velocity.P = 0.04;
motor.PID_velocity.I = 2.1;
// default voltage_power_supply
motor.voltage_limit = 1;
// 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 time constant
motor.LPF_velocity.Tf = 0.01;

// angle P controller
motor.P_angle.P = 17.2;
//motor.P_angle.I = 0.07;
motor.P_angle.D =0.005;

// maximal velocity of the position control
motor.velocity_limit = 15;

// foc currnet control parameters (stm/esp/due/teensy)
//motor.PID_current_q.P = 5;
//motor.PID_current_q.I= 1000;
//motor.PID_current_d.P= 5;
//motor.PID_current_d.I = 1000;
//motor.LPF_current_q.Tf = 0.002f; // 1ms default
//motor.LPF_current_d.Tf = 0.002f; // 1ms default

// use monitoring with serial
Serial.begin(115200);
// comment out if not needed
motor.useMonitoring(Serial);
motor.monitor_downsample = 100; // setting sample rate, can up to 100+
// motor.monitor_variables = _MON_TARGET | _MON_CURR_Q | _MON_CURR_D | _MON_VEL | _MON_ANGLE;

// initialize motor
motor.init();
// align encoder and start FOC
motor.initFOC();

motor.target = 30;

// add target command T
// command.add(‘T’, doTarget, “target angle”);
command.add(‘M’, doMotor, “motor”);
// command.add(‘A’,doMotor,“my motor”);
// Serial.println(F(“Motor ready.”));
// Serial.println(F(“Set the target angle using serial terminal:”));
_delay(1000);

}

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

// Motion control function
motor.move();

// function intended to be used with serial plotter to monitor motor variables
// significantly slowing the execution down!!!
motor.monitor();

// user communication
command.run();

// Debug encoder, done, everything is correct
// iterative function updating the sensor internal variables
// it is usually called in motor.loopFOC()
// not doing much for the encoder though
// encoder.update();
// display the angle and the angular velocity to the terminal
// Serial.print(encoder.getAngle());
// Serial.print(“\t”);
// Serial.println(encoder.getVelocity());

// driver.setPwm(0.3,0.6,0.5);
}

Hi @kuqn ,

In the code you use letter “M” for the doMotor() so you have to enter this letter into the box labeled “Command” in SimpleFOCStudio. Then it should work! :slight_smile:

1 Like