欢迎光临
我们一直在努力

MotorHallInterrupt,GetMotorRunTime函数

电机霍尔中断
void MotorHallInterrupt (uint8 facility){
        if(facility >= len){
        return;
        }
        GetMotorRunTime(Run->rundir, Speed, hallAdjoinSectors, Hall);
}

static void GetMotorRunTime(MotorRundirection rundir, MotorSpeedType *motorspeedbuf, uint8 (*hallAdjoinSectors)[8], MotorHallType *hallbuf){
        motorspeedbuf->hallnow = H_UVW(hallbuf);
        if( (motorspeedbuf->hallnow >= 1) && (motorspeedbuf->hallnow <= 6) ){
                if(motorspeedbuf->hallnow != motorspeedbuf->halllast){
                        if( (motorspeedbuf->halllast>= 1) && (motorspeedbuf->halllast<= 6) ){
                                if(rundir != Cease){
                                        if( motorspeedbuf->starthall == 0){
                                                motorspeedbuf->starthall = motorspeedbuf->hallnow;
                                                motorspeedbuf->starttime = GetSysTime();
                                                motorspeedbuf->oneflag = 0;
                                        }
                                        else{
                                                if( motorspeedbuf -> hallnow == hallAdjoinSectors[rundir][motorspeedbuf -> halllast]){
                                                        if( motorspeedbuf -> hallnow == motorspeedbuf -> starthall){
                                                        motorspeedbuf->endtime = GetSysTime();
                                                        motorspeedbuf->runtime = motorspeedbuf->endtime – motorspeedbuf->starttime;
                                                        motorspeedbuf->starttime = motorspeedbuf->endtime;
                                                        motorspeedbuf->oneflag = 1;
                                                        motorspeedbuf->time = 0;
                                                        }
                                                }
                                                else{
                                                       motorspeedbuf->starthall = motorspeedbuf->hallnow; 
                                                       motorspeedbuf->starttime = GetSysTime(); 
                                                       motorspeedbuf->oneflag = 0; 
                                                }
                                        }
                                }
                               if( motorspeedbuf -> hallnow == hallAdjoinSectors[Forward][motorspeedbuf -> halllast]){
                                        motorspeedbuf -> route++; 
                                }
                                else if( motorspeedbuf -> hallnow == hallAdjoinSectors[Reversal][motorspeedbuf -> halllast]){
                                        motorspeedbuf -> route–; 
                                }
                                else{
                                        motorspeedbuf -> errroute++; 
                                }
                        }
                        motorspeedbuf -> halllast = motorspeedbuf -> hallnow;
                }
        }
}
 

该函数在每次霍尔信号跳变时被调用,主要完成以下任务:

  • 读取当前霍尔值并验证有效性。

  • 判断是否发生有效跳变(避免重复处理)。

  • 若电机正在运行,则捕获一个完整电周期的时间:

    • 记录起始霍尔和时间。

    • 按照方向序列检查霍尔变化是否正确。

    • 当回到起始霍尔时,计算周期时间并置标志。

    • 若序列错误,则重置起始点。

  • 无论电机运行与否,都根据霍尔序列更新转子位置计数:

    • 正转一步则 route++。

    • 反转一步则 route–。

    • 非法跳变则 errroute++。

  • 更新上次霍尔值供下次使用。

  • 该函数是实现霍尔传感器测速和转子位置估算的基础,为上层控制提供转速(通过 runtime)和位置(通过 route)信息,同时检测霍尔故障。

    赞(0)
    未经允许不得转载:171主机测评 » MotorHallInterrupt,GetMotorRunTime函数
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址