Simple FOC

这篇博客介绍了如何使用SimpleFOC库对BLDC电机进行FOC(磁场定向控制)配置,包括电机极对数与相电阻设定、三相引脚与使能配置、传感器、电流采样和通讯的详细步骤。示例代码展示了如何进行闭环速度控制,并提醒用户在使用时要根据实际电机参数调整PID等参数。

1. 电机极对数与相电阻配置

BLDCMotor( int pp , float R)

  • pp - 极对数

  • R - 相电阻(可选)

BLDCMotor motor = BLDCMotor(7);//电机极对数

2. 电机三相引脚及使能配置

BLDCDriver3PWM(int phA,int phB,int phC, int en1 = NOT_SET, int en2 = NOT_SET, int en3 = NOT_SET);

  • phA - A相pwm引脚

  • phB - B相pwm引脚

  • phC - C相pwm引脚

  • en1 - A相使能引脚 (可选)

  • en2 - B相使能引脚 (可选)

  • en3 - C相使能引脚 (可选)

BLDCDriver3PWM driver = BLDCDriver3PWM(32, 33, 25, 22);

3. 传感器配置

MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);//AS5600_I2C
TwoWire I2Cone = TwoWire(0);//将esp32上的一个iic通道声明出来

4. 电流采样配置

InlineCurrentSense current_sense = InlineCurrentSense(0.02 ,50.0, 39, 36);//(采样电阻阻值,放大器放大倍率,A相采样的接收引脚,B相采样的接收引脚,C相采样的接收引脚)

5. 通讯配置

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

6. 绑定与初始化

void setup()

{

I2Cone.begin(19,18, 400000UL); //给iic通道引脚和速率(sda,scl,speed)

sensor.init(&I2Cone);//传感器和通道绑定与初始化

motor.linkSensor(&sensor);//电机和传感器绑定

//控制器设置

driver.voltage_power_supply = 12;

driver.init();

// 电机和控制器连接

motor.linkDriver(&driver);

// 初始化电机 motor

motor.init();

// 初始化 FOC

motor.initFOC();

// 在控制台中添加电机及编号

command.add('M', doMotor, "motor");

}

void loop()

{

// 迭代设置FOC相电压

motor.loopFOC();

// 运动目标

motor.move();

// 电机监控

motor.monitor();

// 用户通讯

command.run();

}

/**

//将电机设置于对应的电路板接口相匹配

void BLDCMotor::linkDriver(BLDCDriver* _driver)

// 启用电机驱动器

void BLDCMotor::enable()

* MagneticSensorI2C类构造函数

* @param chip_address I2C芯片地址

* @param bits 传感器分辨率的位数

* @param angle_register_msb angle read register msb

* @param _bits_used_msb number of used bits in msb

*/

MagneticSensorI2C(uint8_t _chip_address, int _bit_resolution, uint8_t _angle_register_msb, int _msb_bits_used);

simplefoc已经将AS5600和AS5048的参数封装,若使用这俩芯片直接填参即可

#include <SimpleFOC.h>

MagneticSensorI2C sensor0 = MagneticSensorI2C(AS5600_I2C);

MagneticSensorI2C sensor1 = MagneticSensorI2C(AS5600_I2C);

TwoWire I2Cone = TwoWire(0);

TwoWire I2Ctwo = TwoWire(1);

void setup() {

Serial.begin(115200);

_delay(750);

//针对最新版本ESP-Arduino 2.0.2,采用下面两句

I2Cone.begin(19,18, 400000UL); //SDA0,SCL0

I2Ctwo.begin(23,5, 400000UL);

sensor0.init(&I2Cone);

sensor1.init(&I2Ctwo);

}

void loop() {

sensor0.update();

sensor1.update();

//_delay(200);

Serial.print(sensor0.getAngle());

Serial.print(" - ");

Serial.print(sensor1.getAngle());

Serial.println();

}

/**

Deng's FOC 闭环速度控制例程 测试库:SimpleFOC 2.1.1 测试硬件:灯哥开源FOC V3.0

在串口窗口中输入:T+速度,就可以使得两个电机闭环转动

比如让两个电机都以 10rad/s 的速度转动,则输入:T10

在使用自己的电机时,请一定记得修改默认极对数,即 BLDCMotor(7) 中的值,设置为自己的极对数数字

程序默认设置的供电电压为 16.8V,用其他电压供电请记得修改 voltage_power_supply , voltage_limit 变量中的值

默认PID针对的电机是 GB6010 ,使用自己的电机需要修改PID参数,才能实现更好效果

*/

#include <SimpleFOC.h>

MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);

MagneticSensorI2C sensor1 = MagneticSensorI2C(AS5600_I2C);

TwoWire I2Cone = TwoWire(0);

TwoWire I2Ctwo = TwoWire(1);

//电机参数

BLDCMotor motor = BLDCMotor(7);//极对数

BLDCDriver3PWM driver = BLDCDriver3PWM(32, 33, 25, 22);//pwmA/pwmB/pwmC/使能引脚(可选)

BLDCMotor motor1 = BLDCMotor(7);//极对数

BLDCDriver3PWM driver1 = BLDCDriver3PWM(26, 27, 14, 12);

//命令设置

float target_velocity = 0;

Commander command = Commander(Serial);

void doTarget(char* cmd) { command.scalar(&target_velocity, cmd); }

void setup() {

I2Cone.begin(19, 18, 400000UL);

I2Ctwo.begin(23, 5, 400000UL);

sensor.init(&I2Cone);

sensor1.init(&I2Ctwo);

//连接motor对象与传感器对象

motor.linkSensor(&sensor);

motor1.linkSensor(&sensor1);

//供电电压设置 [V]

driver.voltage_power_supply = 12;

driver.init();

driver1.voltage_power_supply = 12;

driver1.init();

//连接电机和driver对象

motor.linkDriver(&driver);

motor1.linkDriver(&driver1);

//FOC模型选择

motor.foc_modulation = FOCModulationType::SpaceVectorPWM;

motor1.foc_modulation = FOCModulationType::SpaceVectorPWM;

//运动控制模式设置

motor.controller = MotionControlType::velocity;

motor1.controller = MotionControlType::velocity;

//速度PI环设置

motor.PID_velocity.P = 0.2;

motor1.PID_velocity.P = 0.2;

motor.PID_velocity.I = 20;

motor1.PID_velocity.I = 20;

//最大电机限制电机

motor.voltage_limit = 12;

motor1.voltage_limit = 12;

//速度低通滤波时间常数

motor.LPF_velocity.Tf = 0.01;

motor1.LPF_velocity.Tf = 0.01;

//设置最大速度限制

motor.velocity_limit = 40;

motor1.velocity_limit = 40;

Serial.begin(115200);

motor.useMonitoring(Serial);

motor1.useMonitoring(Serial);

//初始化电机

motor.init();

motor1.init();

//初始化 FOC

motor.initFOC();

motor1.initFOC();

command.add('T', doTarget, "target velocity");

Serial.println(F("Motor ready."));

Serial.println(F("Set the target velocity using serial terminal:"));

}

void loop() {

motor.loopFOC();

motor1.loopFOC();

motor.move(target_velocity);

motor1.move(target_velocity);

command.run();

}

3.电流采样配置

LowsideCurrentSense currentSense = LowsideCurrentSense(0.003, -64.0/7.0, OP1_OUT, OP2_OUT, OP3_OUT); //电流采样配置

@param shunt_resistor 采样电阻阻值

@param gain 增益

@param phA A phase adc pin

@param phB B phase adc pin

@param phC C phase adc pin (可选)

*/

LowsideCurrentSense(float shunt_resistor, float gain, int pinA, int pinB, int pinC = NOT_SET);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值