Home Web Front-end HTML Tutorial Different ways to understand absolute positioning of encoders

Different ways to understand absolute positioning of encoders

Jan 18, 2024 am 09:50 AM

Different ways to understand absolute positioning of encoders

编码器是一种常用的位置传感器,可以用来测量旋转和线性运动的位移,并将其转换为数字信号。编码器的绝对定位功能可以让我们精确地知道物体的位置,因此在许多领域都有广泛的应用,比如机器人、汽车、医疗仪器等等。

理解编码器绝对定位的方法有很多种,其中比较常见的有以下几种:

  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);
}
Copy after login
  1. 格雷码编码方法

格雷码是一种二进制编码的变体,它的优点在于只有一个位置的变化会导致一个编码位的变化。格雷码编码器的输出与二进制编码器类似,但在对编码进行解码之前需要将其转换为二进制表示。这可以通过查找一个转换表来完成,或使用特定的解码器芯片来自动完成转换。

下面是一个使用Shift Register 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);
}
Copy after login
  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);
}
Copy after login

总结

以上是三种常见的编码器绝对定位方法的代码示例。通过理解编码器的工作原理,我们可以更好地了解如何应用它来实现精确定位,从而在机器人、汽车、医疗仪器等领域提高生产效率和质量。

The above is the detailed content of Different ways to understand absolute positioning of encoders. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

See all articles