The linear encoder normally works in the inkjet printer.
It contained a grating strip and the linear encoder,
Here is an example of how to use the linear encoder and grating strip from the old printer to make a closed-loop control 3D printer:
And the programming of knowing motion is very easy, here is an example in ESP32:
Code source, was written by Chinese
#define GPIO_INPUT_IO_ENC_S (5)
#define GPIO_INPUT_IO_ENC_A (4)
#define GPIO_INPUT_IO_ENC_B (3)
#define GPIO_INPUT_PIN_SEL ((1ULL << GPIO_INPUT_IO_ENC_S) | (1ULL << GPIO_INPUT_IO_ENC_A) | (1ULL << GPIO_INPUT_IO_ENC_B))
void app_main(void)
{
gpio_config_t io_conf = {};
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
io_conf.pull_down_en = 0;
io_conf.pull_up_en = 1;
gpio_config(&io_conf);
int flag = 0;
while (1)
{
if (gpio_get_level(GPIO_INPUT_IO_ENC_A) == 0)flag = 1; // 判断是否开始拨动
if (flag)
{
if (gpio_get_level(GPIO_INPUT_IO_ENC_A) == 1) // 判断一次拨动是否结束
{
if (gpio_get_level(GPIO_INPUT_IO_ENC_B) == 1) // 根据B线判断正转反转
{
ESP_LOGI("TEST", "+"); // 正转逻辑
}
else
{
ESP_LOGI("TEST", "-"); // 反转逻辑
}
flag = 0;
}
}
vTaskDelay(1 / portTICK_PERIOD_MS); // 查询间隔1ms
}
}