c++ - 如何使用 Arduino 步进电机前后移动?
问题描述
我想为具有三个按钮和一个步进电机的系统创建一个 Arduino 程序。如果按下按钮 1,步进器应该向前移动 50 步。如果按下按钮 2,步进器应后退 50 步。如果按下按钮 3,则步进器应在向后 50 步后向前移动 50 步。
我使用了 Arduino 的步进器库并编写了以下代码。功能Forward()
,Backward()
并Continuous()
实现每个按钮要执行的操作。每个功能逐步移动电机并将动作记录在串行输出上。
但我无法达到我想要的结果:步进器不会后退,只会前进。更确切地说:
Forward()
按预期工作Backward()
产生预期的记录输出(最多计数 50 步),但电机只向前移动而不是向后移动。Continuous()
功能也不起作用:向前迈出 50 步(移动并记录步数)后,它继续向前移动,只为计数器记录 1。
我需要你的帮助。我怎样才能使电机倒退Backward()
?以及如何正确Continuous()
实现前进后退,并产生正确的后退步数?
这是我的代码:
#include <Stepper.h>
int forward_button = 2;
int backward_button = 3;
int cont_button = 4;
int button_cond1;
int button_cond2;
int button_cond3;
int del = 50;
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int stepCount = 0; // number of steps the motor has taken
int steps;
void Forward() { // should go forward by 50 steps
int stepCount = 0;
while (stepCount < 50) {
steps = 1;
myStepper.step(steps);
stepCount++;
delay(del);
Serial.print("Forward steps :");
Serial.println(stepCount);
}
}
void Backward() { // should go backward by 50 steps
int stepCount = 0;
while (stepCount < 50) {
steps = 1;
myStepper.step(steps);
stepCount++;
delay(del);
Serial.print("Backward steps :");
Serial.println(stepCount);
}
}
void Continuous() { // should go forward by 50 steps, then backwards
int stepCount = 0;
while (stepCount < 50) {
steps = 1;
myStepper.step(steps);
stepCount++;
delay(del);
Serial.print("Continuous steps :");
Serial.println(stepCount);
}
while (50 < stepCount <= 200) {
int stepCount = 0;
steps = 1;
myStepper.step(steps);
stepCount++;
delay(del);
Serial.print("Continuous steps :");
Serial.println(stepCount);
}
}
void setup() {
// initialize the serial port:
Serial.begin(9600);
pinMode(forward_button, INPUT_PULLUP);
pinMode(backward_button, INPUT_PULLUP);
pinMode(cont_button, INPUT_PULLUP);
//myStepper.setSpeed(60);
}
void loop() {
// step one step:
button_cond1 = digitalRead(forward_button);
button_cond2 = digitalRead(backward_button);
button_cond3 = digitalRead(cont_button);
if ((button_cond1 == LOW) && (button_cond2 == HIGH) && (button_cond3 == HIGH)) {
Forward();
}
else if ((button_cond1 == HIGH) && (button_cond2 == LOW) && (button_cond3 == HIGH)) {
Backward();
}
else if ((button_cond1 == HIGH) && (button_cond2 == HIGH) && (button_cond3 == LOW)) {
Continuous();
}
}
解决方案
步进器方向问题:
根据文档, myStepper.step()
转动电机:
以最近一次调用 setSpeed() 确定的速度将电机转动特定的步数。
更重要的是,为了双向移动,它使用了一个带符号的参数:
转动电机的步数 -正转一个方向,负转另一个(int)
Backward()
在您的代码中,和之间没有区别Forward()
:您在两种情况下都提供正值。所以我建议Backwards()
改用负数:
myStepper.step(-steps);
逻辑问题Continuous()
还需要在Continuous()
函数的第二个循环中更正方向,您想要向后移动。
但首先,您需要更正此循环的条件:
while (50 < stepCount <= 200) { //OUCH compares a boolean and an integer
C++ 并没有像您想象的那样链接比较运算符。因此,将其分解为两个不同的比较,并用逻辑and分组:
while (50 < stepCount && stepCount <= 200) {
不幸的是,在第一个循环结束时stepCount
正是50
. 而且 50 并非严格小于( <
) 小于 50。您可以改用更小或等于( <=
),但由于循环不会递减计数器,您可以将条件简化为:
while (stepCount <= 200) { // we know that it will stay 50 or above
您还需要删除stepCount
此循环块中的重新定义,因为它创建了一个隐藏初始定义的新局部变量(即一个名称,两个变量,这非常令人困惑)。
如果还是不行
经过长时间的评论交流,这里有一些建议(解决问题的粗体字):
- 确保最新版本的代码在 arduino 上运行并且没有编译错误或警告。
- 确保不同的按钮执行预期的功能。
- 如果电机运动不符合预期,请确保您
Stepper
的对象配置了与电机兼容的每转步数。 - 如果动作不稳定,或者正负步骤之间没有区别(根据文档,这应该朝相反的方向移动),然后交叉检查
Stepper
' 的构造函数是否以正确的顺序获取引脚号:这很重要,因为Stepper
会以给定的顺序向不同的引脚发送一系列信号以实现正确的运动。
如果它仍然不起作用,则可能是硬件问题,或者与库不兼容(参见例如这里,作者通过提供另一个信号序列解决了这个问题,这不是最简单的方法)。
推荐阅读
- ionic-framework - Mobile First 8 应用程序在后台运行一段时间后未重新连接
- ms-access - Microsoft 访问存储日期/时间信息的格式是什么?
- r - 如何计算犯罪密度?
- python - Python len() 不起作用,但 __len__ 对持有 numpy 数组的类起作用
- sql-server - SQL Server 中的日期时间转换错误
- php - Laravel 打破并结合 Eloquent 查询
- css - 空间不好的Webfont以及如何修复它
- git - 在 Git 中标记文件
- debugging - 我可以在 Bazel 测试中使用 Python 调试器吗
- angular - “检查后表达式已更改” Angular 弹出错误