按键滤波算法

来源:互联网 发布:android多线程编程 编辑:程序博客网 时间:2024/06/05 20:46

一、状态稳定才改变键值

/*******************************************************************************capture sensors value*******************************************************************************/static int RobotSensors_CRobotcapture(CRobotSensors_typedef* pRobot){#define SENSORS_IO_AVRANGE_TIME   5//200static 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   5static 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;}



原创粉丝点击