1 Star 1 Fork 0

Dapenson / PotentiometerEncoder

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

PotentiometerEncoder库

PotentiometerEncoder 是一个Arduino库,允许您轻松将任何360°电位器转换为编码器。该库包括启用卡尔曼滤波器以获取更平滑读数的选项。提供的示例演示了如何使用该库以及如何使用Ticker库进行定期更新。

安装

  1. 下载 SimpleKalmanFilter 库(版本0.1.0),并在Arduino IDE中安装它。
  2. 下载 PotentiometerEncoder 库,并将其放置在Arduino库文件夹中。

使用方法

初始化

#include "PotentiometerEncoder.h"
#include <Ticker.h>

PotentiometerEncoder encoder(32);  // 将 '32' 替换为连接到电位器的模拟引脚
Ticker timer1;

void timer1_callback(void)
{
    encoder.update();
}

void setup()
{
    Serial.begin(115200);
    encoder.begin();
    timer1.attach_ms(50, timer1_callback);  // 每50毫秒更新一次编码器
}

主循环

void loop()
{
    delay(5);

    int currentPosition = encoder.getCurrentPosition();
    long accumulatedPosition = encoder.getAccumulatedPosition();

    static unsigned long lastPrintTime = 0;
    if (millis() - lastPrintTime > 100)
    {
        Serial.print("当前位置: ");
        Serial.print(currentPosition);
        Serial.print(" 累计位置: ");
        Serial.print(accumulatedPosition);
        Serial.print(" 圈数: ");
        Serial.print(encoder.getRevolutions());
        Serial.print(" 速度(r/min): ");
        Serial.println(encoder.getSpeed());

        lastPrintTime = millis();
    }
}

常用函数

  • PotentiometerEncoder(int pin, bool enableKalmanFilter = true): 构造函数,用于初始化PotentiometerEncoder实例。
  • void begin(int maxRawValue = 255, unsigned char resolution = 8): 使用可选的最大原始值和分辨率参数初始化电位器编码器。
  • void update(): 更新当前位置。在主循环中调用此函数。
  • int getCurrentPosition(): 获取电位器的当前位置。
  • long getAccumulatedPosition(): 获取累计位置。
  • void resetPosition(int32_t position): 将累计位置重置为自定义值。
  • int getRevolutions(): 获取圈数。
  • float getSpeed(): 获取速度,单位为每分钟的旋转数(r/min)。适用于低转速。

概要

使用PotentiometerEncoder库,您可以轻松地将任何360°电位器转换为编码器,提供准确的位置、圈数和速度信息。该库包括一个可选的卡尔曼滤波器,以获得更平滑的读数。在您的项目中享受使用PotentiometerEncoder!

MIT License Copyright (c) 2024 Dapenson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

使用PotentiometerEncoder可以将任意的360°电位器转换为编码器 展开 收起
Arduino
MIT
取消

贡献者

全部

近期动态

加载更多
不能加载更多了
Arduino
1
https://gitee.com/x223222981/PotentiometerEncoder.git
git@gitee.com:x223222981/PotentiometerEncoder.git
x223222981
PotentiometerEncoder
PotentiometerEncoder
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891