|
发表于 2013-4-22 00:16:29
|
显示全部楼层
请问好人,如何让两个电机 同时 按不同速度、走不同的步数?
还有stepper.run()和stepper.rotate(speed,step)的关系是什么?是否run的函数需要一直循环?
按例子中的程序写了两个电机同时运动的代码,但发现速度可以改变,但运行的时间以最后停止的为准,另一电机走的步数会多于设定的
代码如下:- #include <EasyStepper.h>
- #define STEP1_PIN 2
- #define STEP2_PIN 4
- #define DIR1_PIN 6
- #define DIR2_PIN 8
- #define EN1_PIN 12
- #define EN2_PIN 13//两个EN端悬空
- #define DIR1_PIN_INVERTED true
- #define DIR2_PIN_INVERTED true
- #define EN1_PIN_INVERTED true
- #define EN2_PIN_INVERTED true
- EasyStepper stepper1(STEP1_PIN, DIR1_PIN, EN1_PIN, DIR1_PIN_INVERTED, EN1_PIN_INVERTED);
- EasyStepper stepper2(STEP2_PIN, DIR2_PIN, EN2_PIN, DIR2_PIN_INVERTED, EN2_PIN_INVERTED);
- int times=0;
- int stepps=0;
- void setup()
- {
- Serial.begin(9600);
- stepper1.debugMode(false);
- stepper1.startup();
- stepper2.debugMode(false);
- stepper2.startup();
- }
- void loop()
- {
- if (Serial.available() > 0)
- {
- stepps=Serial.parseInt();
- Serial.println(stepps);
- while(stepps!=0)
- {
- if (times < 2)
- {
- if (stepper1.isDone())
- {
- stepper1.rotate(200,stepps);//速度可变,步数不由人...
- }
- stepper1.run();
- if (stepper2.isDone())
- {
- stepper2.rotate(400,stepps);
- times++;
- }
- stepper2.run();
- if(times==2)
- {
- stepps=0;
- times=0;
- }
- }
- }
- }
- }
复制代码 此外,当第二次输入的时候,步进电机会先走第一次输入的那个值,然后再走第二次输入的值,这个是为什么? |
|