美文网首页
【树莓派+arduino实验记录20】旋转编码传感器

【树莓派+arduino实验记录20】旋转编码传感器

作者: Geekero | 来源:发表于2020-09-16 14:48 被阅读0次

    Arduino

    //Rotary Encoder
    /*
     * You will see the angular displacement of the rotary encoder printed on Serial Monitor.
     * When you turn the rotary encoder clockwise, the angular displacement is increased;
     * when turn the it counterclockwise, the displacement is decreased.
     * If you press the switch on the rotary encoder, the readings will return to zero.
     */
     
    const int clkPin = 2;
    const int dtPin =  3;
    const int swPin =  4;
    int encoderVal =   0;
    
    void setup()
    {
      //set clkPin, dsPin, swPin as INPUT
      pinMode(clkPin, INPUT);
      pinMode(dtPin, INPUT);
      pinMode(swPin, INPUT);
      digitalWrite(swPin, HIGH);
      Serial.begin(9600);
    }
    
    void loop()
    {
      int change = getEncoderTurn();
      encoderVal = encoderVal + change;
      if (digitalRead(swPin) == LOW) //if button pull down
      {
        encoderVal = 0;
      }
      Serial.print(encoderVal);
    }
    
    int getEncoderTurn(void)
    {
      static int oldA = HIGH; //set the oldA as HIGH
      static int oldB = HIGH; 
      int result = 0;
      int newA = digitalRead(clkPin); //read the value of clkPin to newB
      int newB = digitalRead(dtPin); //read the value of dtPin to newA
      if (newA != oldA || newB != oldB) // if the value of clkPin or dtPin has changed
      {
        //something had changed
        if (oldB == HIGH  && newB == LOW)
        {
          result = (oldA * 2 - 1);
        }
        oldA = newA;
        oldB = newB;
        return result;
      }
    }
    

    树莓派

    C

    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    #include <stdlib.h>
    #include <wiringPi.h>
    
    #define RoAPin  0
    #define RoBPin  1
    #define SWPin   2
    
    static volatile int globalCounter = 0;
    
    unsigned char flag;
    unsigned char Last_RoB_Status;
    unsigned char Current_RoB_Status;
    
    void btnISR(void)
    {
        globalCounter = 0;
    }
    
    void rotaryDeal(void)
    {
        Last_RoB_Status = digitalRead(RoBPin);
    
        while (!digitalRead(RoAPin)){
            Current_RoB_Status = digitalRead(RoBPin);
            flag = 1;
        }
    
        if (flag == 1){
            flag = 0;
            if ((Last_RoB_Status == 0) && (Current_RoB_Status == 1)){
                globalCounter --;
            }
            if ((Last_RoB_Status == 1) && (Current_RoB_Status == 0)){
                globalCounter ++;
            }
        }
    }
    
    int main(void){
        if (wiringPiSetup() < 0){
            fprintf(stderr, "Unanble to setup wiringPi:%s\n", strerror(errno));
            return 1;
        }
    
        pinMode(SWPin,  INPUT);
        pinMode(RoAPin, INPUT);
        pinMode(RoBPin, INPUT);
    
        pullUpDnControl(SWPin, PUD_UP);
    
        if (wiringPiISR(SWPin, INT_EDGE_FALLING, &btnISR) < 0){
            fprintf(stderr, "Unable to init ISR\n", strerror(errno));
            return 1;
        }
        
        int tmp = 0;
    
        while(1){
            rotaryDeal();
            if (tmp != globalCounter){
                printf("%d\n", globalCounter);
                tmp = globalCounter;
            }
        }
        return 0;
    }
    

    Python

    #!/usr/bin/env python
    import RPi.GPIO as GPIO
    import time
    
    RoAPin = 11
    RoBPin = 12
    BtnPin = 13
    
    globalCounter = 0
    
    flag = 0
    Last_RoB_Status = 0
    Current_RoB_Status = 0
    
    def setup():
        GPIO.setmode(GPIO.BOARD) #Numbers GPIOs by physical location
        GPIO.setup(RoAPin, GPIO.IN)
        GPIO.setup(RoBPin, GPIO.IN)
        GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    
    def rotaryDeal():
        global flag
        global Last_RoB_status
        global Current_RoBPin_status
        global globalCounter
    
        Last_RoB_Status = GPIO.input(RoBPin)
        while (not GPIO.input(RoAPin)):
            Current_RoB_Status = GPIO.input(RoBPin)
            flag = 1
    
        if flag == 1:
            flag = 0
            if (Last_RoB_Status == 0) and (Current_RoB_Status == 1):
                globalCounter -= 1
            if (Last_RoB_Status == 1) and (Current_RoB_Status == 0):
                globalCounter += 1
    
    def btnISR(channel):
        global globalCounter
        globalCounter = 0
    
    def loop():
        global globalCounter
        tmp = 0 # Rotary Temperay
    
        GPIO.add_event_detect(BtnPin, GPIO.FALLING, callback=btnISR)
        while True:
            rotaryDeal()
            if tmp != globalCounter:
                print('globalCounter = %d' % globalCounter)
                tmp = globalCounter
    
    def destroy():
        GPIO.cleanup()
    
    
    if __name__ == '__main__':
        setup()
        try:
            loop()
        except KeyboardInterrupt:
            destroy()
    

    相关文章

      网友评论

          本文标题:【树莓派+arduino实验记录20】旋转编码传感器

          本文链接:https://www.haomeiwen.com/subject/ijqfyktx.html