旋转编码器(rotary encoder)旋转方向的判断

您所在的位置:网站首页 齿轮旋转方向怎么判断 旋转编码器(rotary encoder)旋转方向的判断

旋转编码器(rotary encoder)旋转方向的判断

#旋转编码器(rotary encoder)旋转方向的判断| 来源: 网络整理| 查看: 265

     旋转编码器又称为轴编码器,它是一种将轴的角度位置或运动方向的信息转换为模拟或数字信号的电机械设备。有两种类型的编码器:绝对式的和增量式的。绝对式旋转编码器输出的是轴的精确位置,其被作为一种角度换能器。增量式旋转编码器输出的只是轴的大概旋转方向,但是不能确定其准确的角度位置信息。这一段内容参考于维基百科。      以下要讲的是一种增量式旋转编码器,如图1所示。图1来至于这里。以下的相关原理介绍以及源码等均参考与这里。

  图1.

     对于以上所示的旋转编码器,我们主要关注其并排的三个引脚,其中一个接地,另外两个我们标记为引脚A和引脚B。当转动旋转编码器的轴时引脚A和引脚B的电位的高低会发生变化,并且顺时针转动和逆时针转动时引脚A和引脚B的电位组合的序列有区别,我们可以根据这种差异来辨别旋转的方向。如图2和图3所示。

  图2.   图3.

     从图4和图5可以看出当旋转编码器顺指针和逆时针旋转时脚A和引脚B的电位组合的序列是不同的。顺时针旋转时脚A和引脚B的电位组合的序列是 { { 1 , 1 } , { 0 , 1 } , { 0 , 0 } , { 1 , 0 } } \{\{1,1\},\{0,1\},\{0,0\},\{1,0\}\} {{1,1},{0,1},{0,0},{1,0}}序列的循环,逆时针旋转时脚A和引脚B的电位组合的序列是 { { 1 , 1 } , { 1 , 0 } , { 0 , 0 } , { 0 , 1 } } \{\{1,1\},\{1,0\},\{0,0\},\{0,1\}\} {{1,1},{1,0},{0,0},{0,1}}序列的循环。并且当引脚A从低电平变为高电平时(即引脚A从圆盘上的灰色区域到接触到圆盘上的白色区域时),在顺时针旋转的情况下引脚B为低电平(即此时引脚A和引脚B的电位不相同),在逆时针旋转的情况下引脚B为高电平(即此时引脚A和引脚B的电位相同)。我们可以利用这两种方式实现算法来辨别旋转编码器的旋转方向。实现算法见算法一和算法二。在实际使用时需要不断调用函数 g e t R o t a r y E n c o d e r D i r e c t i o n ( b o o l p i n A C u r r e n t S t a t u s , b o o l p i n B C u r r e n t S t a t u s ) getRotaryEncoderDirection(bool\quad pinACurrentStatus, bool\quad pinBCurrentStatus) getRotaryEncoderDirection(boolpinACurrentStatus,boolpinBCurrentStatus)来获取旋转编码器的旋转方向,其中参数 p i n A C u r r e n t S t a t u s pinACurrentStatus pinACurrentStatus和 p i n B C u r r e n t S t a t u s pinBCurrentStatus pinBCurrentStatus引脚A和引脚B的高低电平状态(1代表高电平,0代表低电平),引脚A和引脚B的高低电平状态可以通过GPIO口来获取。

  图4.   图5. //算法一 enum rotaryEncoderDirection { clockWise = 0, antiClockWise, stop }; enum rotaryEncoderDirection getRotaryEncoderDirection(bool pinACurrentStatus, bool pinBCurrentStatus) { enum rotaryEncoderDirection currentDir = stop; static bool pinALastStatus = 0; static bool pinBLastStatus = 0; static int clockWiseCounter = 0; static int counterClockWiseCounter = 0; if (pinACurrentStatus != pinALastStatus || pinBCurrentStatus != pinBLastStatus) { if (pinALastStatus == 0 && pinBLastStatus == 0) { if (pinACurrentStatus == 0 && pinBCurrentStatus == 1) { counterClockWiseCounter++; } else if (pinACurrentStatus == 1 && pinBCurrentStatus == 0) { clockWiseCounter++; } } else if (pinALastStatus == 0 && pinBLastStatus == 1) { if (pinACurrentStatus == 1 && pinBCurrentStatus == 1) { counterClockWiseCounter++; } else if (pinACurrentStatus == 0 && pinBCurrentStatus == 0) { clockWiseCounter++; } } else if (pinALastStatus == 1 && pinBLastStatus == 1) { if (pinACurrentStatus == 1 && pinBCurrentStatus == 0) { counterClockWiseCounter++; } else if (pinACurrentStatus == 0 && pinBCurrentStatus == 1) { clockWiseCounter++; } } else if (pinALastStatus == 1 && pinBLastStatus == 0) { if (pinACurrentStatus == 0 && pinBCurrentStatus == 0) { counterClockWiseCounter++; } else if (pinACurrentStatus == 1 && pinBCurrentStatus == 1) { clockWiseCounter++; } } pinALastStatus = pinACurrentStatus; pinBLastStatus= pinBCurrentStatus; if (clockWiseCounter >= 2) { clockWiseCounter = 0; counterClockWiseCounter=0; currentDir = clockWise; } else if (counterClockWiseCounter >= 2) { counterClockWiseCounter = 0; clockWiseCounter = 0; currentDir = antiClockWise; } } return currentDir; } //算法二 enum rotaryEncoderDirection {clockWise=0, antiClockWise,stop}; enum rotaryEncoderDirection getRotaryEncoderDirection(bool pinACurrentStatus, bool pinBCurrentStatus) { enum rotaryEncoderDirection currentDir = stop; static bool pinALastStatus = 0; static int clockWiseRotaryEncoderCounter=0; static int antiClockWiseRotaryEncoderCounter=0; // If last and current state of pinA are different, then pulse occurred // React to only 1 state change to avoid double count if (pinACurrentStatus != pinALastStatus && pinACurrentStatus == 1) { // If the pinB state is different than the pinA state then // the encoder is rotating CW if (pinBCurrentStatus != pinACurrentStatus) { clockWiseRotaryEncoderCounter++; } else { // Encoder is rotating CCW antiClockWiseRotaryEncoderCounter++; } } // Remember last pinA status pinALastStatus = pinACurrentStatus; if(clockWiseRotaryEncoderCounter >=4) { clockWiseRotaryEncoderCounter = 0; antiClockWiseRotaryEncoderCounter = 0; currentDir = clockWise; } else if (antiClockWiseRotaryEncoderCounter>=4) { clockWiseRotaryEncoderCounter = 0; antiClockWiseRotaryEncoderCounter = 0; currentDir = antiClockWise; } return currentDir; }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3