【已解决】上位机控制步进电机问题请教
本帖最后由 情谊永恒1992 于 2016-11-23 17:17 编辑我使用了大连好人写的库EasyStepper,以及借鉴了JasmineL的程序,现附上传送门:
http://www.geek-workshop.com/thread-3266-1-1.html
http://www.geek-workshop.com/thread-4319-1-1.html
==============
我想用PC上位机去操控一台57步进电机,利用串口通信,让上位机来控制电机运动步数,所以编写了如下程序,单次操作是没有问题的,但是单次运动完后,从串口监视器再输入步数,电机也不运动了,我测试过,感觉是程序一直在库文件的某个函数死循环没出来,因为我输入步数后也不会再返回该步数。。。
附上代码,希望大神帮忙看看问题出在哪里
#include <EasyStepper.h>
// define the pins
#define STEP_PIN 3
#define DIR_PIN 30
#define EN_PIN 33
// define the inverted
#define DIR_PIN_INVERTED true
#define EN_PIN_INVERTED false//HIGH使能
// the EasyStepper instance
EasyStepper stepper1(STEP_PIN, DIR_PIN, EN_PIN, DIR_PIN_INVERTED, EN_PIN_INVERTED);
// the stepps to rotate
int stepps;
int steps;
void setup()
{
Serial.begin(9600);
stepper1.debugMode(false);
stepper1.startup();
}
void loop()
{
while (Serial.available())
{
stepps=Serial.parseInt();
Serial.println(stepps);
steps=stepps*4;//四分数
stepps=0;
stepper1.rotate(400.0,steps);
while(steps !=0 )
{
stepper1.run();
}
}
}
==============================================
5分钟后在JasmineL的另一个帖子里找到了原因,还是自己太不熟练,希望新手借鉴不要犯跟我一样的错
原因是困在第二个while循环里没出来。。。
所以要把steps赋值0,出循环
附上正确代码
#include <EasyStepper.h>
// define the pins
#define STEP_PIN 3
#define DIR_PIN 30
#define EN_PIN 33
// define the inverted
#define DIR_PIN_INVERTED true
#define EN_PIN_INVERTED false
// the EasyStepper instance
EasyStepper stepper1(STEP_PIN, DIR_PIN, EN_PIN, DIR_PIN_INVERTED, EN_PIN_INVERTED);
// the stepps to rotate
int stepps;
int steps;
void setup()
{
Serial.begin(9600);
stepper1.debugMode(false);
stepper1.startup();
}
void loop()
{
while (Serial.available())
{
stepps=Serial.parseInt();
Serial.println(stepps);
steps=stepps*4;
stepps=0;
stepper1.rotate(400.0,steps);
while(steps !=0 )
{
stepper1.run();
if(stepper1.isDone())
{
steps=0;
}
}
}
} 请教楼主,如果速度和脉冲数两个都从串口获得,要怎么写好呢?
页:
[1]