按键滤波算法

一、状态稳定才改变键值

/*******************************************************************************
	capture sensors value
*******************************************************************************/
static int RobotSensors_CRobotcapture(CRobotSensors_typedef* pRobot)
{
#define SENSORS_IO_AVRANGE_TIME   5//200
	static uint32_t sLeftLimitTimeOut = 0, sRightLimitTimeOut = 0,
                    sEmercyButtonTimeOut = 0;
	static uint8_t sLeftlimitAction = Bit_SET, sRightlimitAction = Bit_SET,\
                   sEmercyButtonAction = Bit_SET;

	/* Left limit sensor */
	if(sLeftlimitAction != CROBOT_LEFT_LIMIT_READ){
		sLeftlimitAction = CROBOT_LEFT_LIMIT_READ;
		sLeftLimitTimeOut = 0;
	}else{
		sLeftLimitTimeOut++;
		if(sLeftLimitTimeOut > 5){
			pRobot->L_SideLimit = (SwitchSensorValue_typedef)sLeftlimitAction;
			sLeftLimitTimeOut = 0;
		}
	}	

	/* Right limit sensor */
	if(sRightlimitAction != CROBOT_RIGHT_LIMIT_READ){
		sRightlimitAction = CROBOT_RIGHT_LIMIT_READ;
		sRightLimitTimeOut = 0;
	}else{
		sRightLimitTimeOut++;
		if(sRightLimitTimeOut > 5){
			pRobot->R_SideLimit = (SwitchSensorValue_typedef)sRightlimitAction;
			sRightLimitTimeOut = 0;
		}
	}
    
	/* Emercy button check */
	if(sEmercyButtonAction != CROBOT_EMECY_STOP_KEY_READ){
		sEmercyButtonAction = CROBOT_EMECY_STOP_KEY_READ;
		sEmercyButtonTimeOut = 0;
	}else{
		sEmercyButtonTimeOut++;
		if(sEmercyButtonTimeOut > 5){
			pRobot->EmercyButton = (SwitchSensorValue_typedef)sEmercyButtonAction;
			sEmercyButtonTimeOut = 0;
		}
	}

	/* Intergrate check by logical  */
	if(RobotTaskMag.CRobot.RoCtl.ParkOnDir == PARK_ON_RIGHT){
		if(pRobot->R_SideLimit == ON){
			pRobot->Integrate = ON;	
		}else{		
			pRobot->Integrate = OFF;
		}
	}else if(RobotTaskMag.CRobot.RoCtl.ParkOnDir == PARK_ON_LEFT){
		if(pRobot->L_SideLimit == ON){
			pRobot->Integrate = ON;	
		}else{		
			pRobot->Integrate = OFF;
		}
	}else if(RobotTaskMag.CRobot.RoCtl.ParkOnDir == PARK_ON_BOTH){
		if((pRobot->L_SideLimit == ON)||(pRobot->R_SideLimit == ON)){
			pRobot->Integrate = ON;	
		}else{		
			pRobot->Integrate = OFF;
		}
	}
	
	/* Check Emergency Button */
	if(pRobot->EmercyButton == ON){
		RoSafety.SafetyStatus.SafetyFlagBit.bit_EmergencyButton = 1;
	}else{
		RoSafety.SafetyStatus.SafetyFlagBit.bit_EmergencyButton = 0;	
	}
	
	return 0;
}

二、按键滤波,按键按下保持一段时间才改变键值

static int RobotSensors_TRobotcapture(TRobotSensors_typedef* pRobot)
{
#define SENSORS_IO_AVRANGE_TIME   5
	static uint32_t sAlignHighSensorTimeout = 0;
	static uint32_t sAlignLowSensorTimeout = 0;
	static uint32_t sAheadSensorTimeout = 0;
	static uint32_t sTailSensorTimeout = 0;
	static uint32_t sIntegrateSensorTimeout = 0;
    static uint32_t sEmercyButtonTimeout = 0;
    static uint32_t sParkedOnSensorTimeout = 0;
    static uint32_t sSpeedChangeSensorTimeout = 0;

	
    /* Ahead limit */
	if((SwitchSensorValue_typedef)TROBOT_AHEAD_LIMIT_READ == ON){
		if(sAheadSensorTimeout < SENSORS_IO_AVRANGE_TIME){
			sAheadSensorTimeout++;
		}else{
			pRobot->LimitAhead = ON;
		}
	}else{
		sAheadSensorTimeout = 0;
		pRobot->LimitAhead = OFF;
	}
	
    /* Tail limit */
	if((SwitchSensorValue_typedef)TROBOT_TAIL_LIMIT_READ == ON){
		if(sTailSensorTimeout < SENSORS_IO_AVRANGE_TIME){
			sTailSensorTimeout++;
		}else{
			pRobot->LimitTail = ON;
		}
	}else{
		sTailSensorTimeout = 0;
		pRobot->LimitTail = OFF;
	}  
    
    /* Emercy Button */
	if((SwitchSensorValue_typedef)TROBOT_EMECY_STOP_KEY_READ == ON){
		if(sEmercyButtonTimeout < SENSORS_IO_AVRANGE_TIME){
			sEmercyButtonTimeout++;
		}else{
			pRobot->EmercyButton = ON;
		}
	}else{
		sEmercyButtonTimeout = 0;
		pRobot->EmercyButton = OFF;
	}  
    
    /* Packing limit */
	if((SwitchSensorValue_typedef)TROBOT_PARKING_LIMIT_READ == ON){
		if(sParkedOnSensorTimeout < SENSORS_IO_AVRANGE_TIME){
			sParkedOnSensorTimeout++;
		}else{
			pRobot->ParkedOn = ON;
		}
	}else{
		pRobot->ParkedOn = OFF;
		sParkedOnSensorTimeout = 0;
	}
    
    /* Intergrate limit */	
    if((SwitchSensorValue_typedef)TROBOT_INTERGRATE_READ == ON){
		if(sIntegrateSensorTimeout < SENSORS_IO_AVRANGE_TIME){
			sIntegrateSensorTimeout++;
		}else{
			pRobot->Integrate = ON;
		}
	}else{
		sIntegrateSensorTimeout = 0;
		pRobot->Integrate = OFF;
	}
    
    /* Speed change sensor */
	if((SwitchSensorValue_typedef)TROBOT_SPEED_CHANGE_LIMIT_READ == ON){
		if(sSpeedChangeSensorTimeout < SENSORS_IO_AVRANGE_TIME){
			sSpeedChangeSensorTimeout++;
		}else{
			pRobot->SpeedChange = ON;
		}
	}else{
		pRobot->SpeedChange = OFF;
		sSpeedChangeSensorTimeout = 0;
	}
	
    /* Align high */
	if((SwitchSensorValue_typedef)ALIGN_HIGH_LIMIT_READ == ON){
		if(sAlignHighSensorTimeout < SENSORS_IO_AVRANGE_TIME){
			sAlignHighSensorTimeout++;
		}else{
			pRobot->AlignHighside = ON;
		}
	}else{
		sAlignHighSensorTimeout = 0;
		pRobot->AlignHighside = OFF;
	}
	
    /* Align low */
	if((SwitchSensorValue_typedef)ALIGN_LOW_LIMIT_READ == ON){
		if(sAlignLowSensorTimeout < SENSORS_IO_AVRANGE_TIME){
			sAlignLowSensorTimeout++;
		}else{
			pRobot->AlignLowside = ON;
		}
	}else{
		sAlignLowSensorTimeout = 0;
		pRobot->AlignLowside = OFF;
	}

	/* Check Emergency Button */
	if(pRobot->EmercyButton == ON){
		RoSafety.SafetyStatus.SafetyFlagBit.bit_EmergencyButton = 1;
	}else{
		RoSafety.SafetyStatus.SafetyFlagBit.bit_EmergencyButton = 0;	
	}
	
	return 0;
}



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值