1.下载
到github页面直接下载即可
https://github.com/JorgeMaker/SimpleFOCStudio/tree/main
2.安装
进入下载解压出来的目录运行
pip install -r "requirements.txt"
然后等待安装完成
悲剧
解决:
python版本不对造成的,最好安装python3.6或3.7的
然后就可以成功了:
3.硬件端程序
#include <SimpleFOC.h>
// magnetic sensor instance - IIC
MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);
// BLDC motor & driver instance
BLDCMotor motor = BLDCMotor(11); // 电机极对数
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8); // 电机ABC相PWM引脚,使能引脚
float target_angle = 0; // 初始目标角度为0
Commander command = Commander(Serial);
void doTarget(char* cmd) { command.scalar(&target_angle, cmd); } // 串口控制指令:目标值
void onMotor(char* cmd){ command.motor(&motor,cmd); } // 串口控制指令:电机
void setup() {
// 编码器设置
sensor.init();
motor.linkSensor(&sensor);
// 驱动设置
driver.voltage_power_supply = 12;
driver.init();
motor.linkDriver(&driver);
// 选择调制方式为SVPWM
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
// 控制模式为角度模式
motor.controller = MotionControlType::angle;
// PID参数
motor.PID_velocity.P = 0.3;
motor.PID_velocity.I = 10;
motor.PID_velocity.D = 0;
motor.P_angle.P = 0.5;
//其他参数
motor.voltage_limit = 6; //最大电压
motor.velocity_limit = 20; //最大速度,rad/s
motor.LPF_velocity.Tf = 0.01; //速度的滤波时间常数
// 串口设置
Serial.begin(115200);
motor.useMonitoring(Serial); //使用串口监视器
//初始化
motor.init();
motor.initFOC();
// 添加串口命令
command.add('T', doTarget, "target angle");
command.add('M',onMotor,"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
// 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_angle);
// function intended to be used with serial plotter to monitor motor variables
// significantly slowing the execution down!!!!
motor.monitor(); //使用simpleFOC Studio上位机设置的时候,这句一定要打开。但是会影响程序执行速度
// user communication
command.run();
}