>  기사  >  웹 프론트엔드  >  인코더의 절대 위치를 이해하는 다양한 방법

인코더의 절대 위치를 이해하는 다양한 방법

王林
王林원래의
2024-01-18 09:50:07453검색

인코더의 절대 위치를 이해하는 다양한 방법

엔코더는 회전 및 직선 운동의 변위를 측정하고 이를 디지털 신호로 변환하는 데 사용할 수 있는 일반적으로 사용되는 위치 센서입니다. 엔코더의 절대 위치 결정 기능을 통해 물체의 위치를 ​​정확하게 알 수 있어 로봇, 자동차, 의료기기 등 다양한 분야에서 널리 활용되고 있습니다.

인코더의 절대 위치를 이해하는 방법은 여러 가지가 있으며 그 중 가장 일반적인 방법은 다음과 같습니다.

  1. 바이너리 인코딩 방법

바이너리 인코딩 방법은 물리적 동작을 디지털 신호로 변환하는 방법입니다. 엔코더는 위치 센서를 통해 물체의 움직임 여부를 감지하고, 물체의 움직임 위치에 따라 출력되는 디지털 코드를 변경합니다. 각 디지털 코드는 고유한 물리적 위치에 해당하므로 인코더의 출력을 읽어 물체의 위치를 ​​확인할 수 있습니다.

다음은 Arduino로 구현된 이진 인코더의 샘플 코드입니다.

const int encoderPinA = 2;
const int encoderPinB = 3;
volatile int encoderPos = 0;
volatile bool aSet = false;
volatile bool bSet = false;

void setup() {
  pinMode(encoderPinA, INPUT);
  pinMode(encoderPinB, INPUT);
  attachInterrupt(digitalPinToInterrupt(encoderPinA), updateEncoderA, CHANGE);
  attachInterrupt(digitalPinToInterrupt(encoderPinB), updateEncoderB, CHANGE);
}

void loop() {
  // 读取编码器当前位置
  int newPos = encoderPos;
  Serial.println(newPos);
}

void updateEncoderA() {
  aSet = digitalRead(encoderPinA);
  if (aSet && !bSet) {
    encoderPos++;
  } else if (!aSet && bSet) {
    encoderPos--;
  }
  bSet = digitalRead(encoderPinB);
}

void updateEncoderB() {
  bSet = digitalRead(encoderPinB);
  if (bSet && !aSet) {
    encoderPos--;
  } else if (!bSet && aSet) {
    encoderPos++;
  }
  aSet = digitalRead(encoderPinA);
}
  1. 회색 코드 인코딩 방법

회색 코드는 이진 인코딩의 변형이며, 위치만 변경하면 인코딩 비트가 발생한다는 장점이 있습니다. 변화. 그레이 코드 인코더의 출력은 이진 인코더와 유사하지만 인코딩을 디코딩하기 전에 이진 표현으로 변환해야 합니다. 변환 테이블을 찾거나 특정 디코더 칩을 사용하여 자동으로 변환을 수행하면 됩니다.

다음은 시프트 레지스터 74HC595를 사용하여 구현된 그레이 코드 인코더의 샘플 코드입니다.

const int encoderPinClock = 4;
const int encoderPinData = 5;
const int encoderPinLatch = 6;

unsigned int encoderValue = 0;

void setup() {
  pinMode(encoderPinClock, OUTPUT);
  pinMode(encoderPinData, OUTPUT);
  pinMode(encoderPinLatch, OUTPUT);
}

void loop() {
  // 读取编码器当前位置
  unsigned int newPos = 0;
  for (int i = 0; i < 16; i++) {
    digitalWrite(encoderPinLatch, LOW);
    shiftOut(encoderPinData, encoderPinClock, MSBFIRST, 1 << i);
    digitalWrite(encoderPinLatch, HIGH);
    delayMicroseconds(10);
    newPos |= digitalRead(encoderPinData) << i;
  }
  encoderValue = newPos;
  Serial.println(encoderValue);
}
  1. PWM 인코딩 방법

PWM 인코딩 방법은 펄스 폭 변조의 원리를 사용하여 인코더의 출력 신호를 펄스 신호. 각 펄스 폭은 위치에 해당하므로 펄스 폭을 읽어 위치를 결정할 수 있습니다.

다음은 ESP32의 PWM 모듈을 사용하여 구현한 PWM 인코더의 샘플 코드입니다.

const int encoderPin = 5;
volatile int encoderPos = 0;
volatile unsigned long lastPulseTime = 0;

void IRAM_ATTR pulseHandler() {
  unsigned long pulseTime = micros();
  if (pulseTime - lastPulseTime > 10) {
    if (digitalRead(encoderPin) == HIGH) {
      encoderPos--;
    } else {
      encoderPos++;
    }
    lastPulseTime = pulseTime;
  }
}

void setup() {
  pinMode(encoderPin, INPUT);
  attachInterrupt(encoderPin, pulseHandler, CHANGE);
  ledcSetup(0, 5000, 8);
  ledcAttachPin(encoderPin, 0);
}

void loop() {
  // 读取编码器当前位置
  int newPos = map(ledcRead(0), 0, 255, -100, 100);
  encoderPos = newPos;
  Serial.println(encoderPos);
}

요약

위는 세 가지 일반적인 인코더 절대 위치 지정 방법의 코드 예입니다. 엔코더의 작동 방식을 이해하면 엔코더를 적용하여 정확한 위치 결정을 달성하는 방법을 더 잘 이해할 수 있으며 이를 통해 로봇 공학, 자동차, 의료 기기 등의 분야에서 생산 효율성과 품질을 향상시킬 수 있습니다.

위 내용은 인코더의 절대 위치를 이해하는 다양한 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.