文章目录
ESP32 simple foc 移植odriver anti-cogging.
1.硬件,在淘宝买的。esp32 +simple foc(最新). 下电阻三采样。
2. 效果,见视频https://www.bilibili.com/video/BV1xg4y1X7Yr/?vd_source=4fd70d693021f289fb2d339c6c040719
3.代码添加(生成前馈数组,就与补偿齿槽效应的扭矩)
void loop() {
static int ccc,changed_tar,dis_cc;
static float tar = 0;
// iterative setting FOC phase voltage
motor.loopFOC();
// iterative function setting the outter loop target
motor.move();
if (1)
{
if (fabs(motor.shaftVelocity())<0.0001 && fabs(motor.shaftAngle()-tar)<0.0003 && ccc>100 && changed_tar)
{
char bufcmd1[50];
if (changed_tar == 1)
{
ccc = 0;
changed_tar = 2;
}
else
{
sprintf(bufcmd1,"stop:%f %f %f %f %f",tar*180/3.14159,motor.shaftAngle()*180/3.14159,fabs(motor.shaftAngle()-tar)*180/3.14159,motor.shaftVelocity(),motor.current_sp);
Serial.println(bufcmd1);
ccc = 10001;
changed_tar = 0;
}
}
if(ccc++>10000 && changed_tar == 0)
{
char bufcmd[50];
unsigned int buf;
changed_tar = 1;
ccc = 0;
tar +=0.0035;//0.2 degree
motor.target = tar;
}
}
// monitoring the state variables
// motor.monitor(); //会影响程序执行速度,与studio交互的配置3共3处
// user communication
command.run();
}
补偿位置 ,在BLDCMotor.cpp下的。void BLDCMotor::move(float new_target)
case MotionControlType::velocity:
static int ffccc;
int ttt;
// velocity set point - sensor precision: this calculation is numerically precise.
shaft_velocity_sp = target;
// calculate the torque command
if (shaft_angle<0)
{
ind = floor(fmod(-shaft_angle,2*_PI)*286.0f);
}
else
{
ind = floor(fmod(shaft_angle,2*_PI)*286.0f);
}
ttt = ind;
if (ind>=1797)ind = 1796;
if (ind<=0)ind = 0;
current_sp = anti_cogging_map[ind]*anti_cogging_coef+PID_velocity(shaft_velocity_sp - shaft_velocity); // if current/foc_current torque control
if (ffccc++>20000){
char buf[40];
ffccc = 0;
sprintf(buf,"V:%f %f %f %d %d",target,current_sp,shaft_angle,ttt,torque_controller);
Serial.println(buf);
}
// if torque controlled through voltage control
基本原理
- 就是每次步进一个很小的角度(我这里是0.2度),选择360度,然后生成一个大的数组,以后在到达的角度固定前馈进去就行了,主要是保存上面的motor.current_sp.存成一个数组就行了。
后续再补充。