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 (0)
{
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
后续再补充。