BlackCat 发表于 2012-6-28 01:55:55

步进电机驱动问题,懂的帮忙解决下

本帖最后由 BlackCat 于 2012-6-29 17:15 编辑

先上代码。#include <Stepper.h>
const int stepsPR = 200;

Stepper myStepper1(stepsPR,30,31,32,33);
Stepper myStepper2(stepsPR,50,51,52,53);

int xpotPin = 2;
int ypotPin = 3;
int bpotPin = 4;
int xval = 0;
int yval = 0;
int bval = 0;

void setup(){
myStepper1.setSpeed(60);
myStepper2.setSpeed(60);
Serial.begin(9600);
}

void loop(){
xval = analogRead(xpotPin);
yval = analogRead(ypotPin);
bval = analogRead(bpotPin);

if (xval<250){
    Serial.print("go");
   myStepper1.step(stepsPR);
   myStepper2.step(stepsPR);
}
if (xval>650){
    Serial.print("back");
   myStepper1.step(-stepsPR);
   myStepper2.step(-stepsPR);
}
if (yval<250){
    Serial.print("left");
   myStepper1.step(stepsPR);
   myStepper2.step(-stepsPR);
}
   if (yval>650){
    Serial.print("right");
   myStepper1.step(-stepsPR);
   myStepper2.step(stepsPR);
}
}目的,PS2摇杆控制2个步进电机A和B
PS2      电机A    电机B
x+       +            +
x-      -            -
y+       +            -
y-         -             +


也就是说
摇杆x轴向上,A,B电机同时正向转动
摇杆X轴向下,A,B电机同时反向转动
摇杆Y轴向左,A电机反转,B电机正转
摇杆Y轴向右,A电机正转,B电机反转

但是实际操作中,当A电机 转动完成后B电机才转。

问题:如何让两个电机同步转动。

希望懂的朋友帮忙解答一下,谢谢。

pww999 发表于 2012-6-28 09:35:40

算法不对,冲突了吧?

BlackCat 发表于 2012-6-28 12:17:28

能解释的详细点吗?

pww999 发表于 2012-6-28 19:31:10

本帖最后由 pww999 于 2012-6-28 19:43 编辑

假设这2个同时工作,摇杆x轴向上摇杆Y轴向左
会出现什么问题?

改成这样试试
if

else if

else if


else

BlackCat 发表于 2012-6-28 20:34:33

pww999 发表于 2012-6-28 19:31 static/image/common/back.gif
假设这2个同时工作,摇杆x轴向上摇杆Y轴向左
会出现什么问题?



没效果,还是一个转完了另一个才转。

BlackCat 发表于 2012-6-28 20:37:58

自己写成这样的话,两个步进电机可以同时转动,估计是库的运行问题吧

不知道谁能帮忙,用自带库写出同时驱动两个电机的办法,不然得自己写库了。#include "Arduino.h"

void setup (){

pinMode(50,OUTPUT);
pinMode(51,OUTPUT);
pinMode(52,OUTPUT);
pinMode(53,OUTPUT);
pinMode(30,OUTPUT);
pinMode(31,OUTPUT);
pinMode(32,OUTPUT);
pinMode(33,OUTPUT);

}

void loop(){
int i = 4;
while(1)
{
    unsigned char z;
    for(z=0;z<4;z++){
      if (z=1){
      digitalWrite(50, HIGH);
      digitalWrite(51, LOW);
      digitalWrite(52, HIGH);
      digitalWrite(53, LOW);
      digitalWrite(30, HIGH);
      digitalWrite(31, LOW);
      digitalWrite(32, HIGH);
      digitalWrite(33, LOW);
      delay(i);
      }
      if (z=2){
      digitalWrite(50, LOW);
      digitalWrite(51, HIGH);
      digitalWrite(52, HIGH);
      digitalWrite(53, LOW);
      digitalWrite(30, LOW);
      digitalWrite(31, HIGH);
      digitalWrite(32, HIGH);
      digitalWrite(33, LOW);
      delay(i);
      }
      if (z=3){
      digitalWrite(50, LOW);
      digitalWrite(51, HIGH);
      digitalWrite(52, LOW);
      digitalWrite(53, HIGH);
      digitalWrite(30, LOW);
      digitalWrite(31, HIGH);
      digitalWrite(32, LOW);
      digitalWrite(33, HIGH);
      delay(i);
      }
      if (z=4){
      digitalWrite(50, HIGH);
      digitalWrite(51, LOW);
      digitalWrite(52, LOW);
      digitalWrite(53, HIGH);
      digitalWrite(30, HIGH);
      digitalWrite(31, LOW);
      digitalWrite(32, LOW);
      digitalWrite(33, HIGH);
      delay(i);
      }
    }
}
}

BlackCat 发表于 2012-6-29 17:13:41

整了会,这样用自带库也可以,并且附带了PS2摇杆控制。#include "Arduino.h"
#include <Stepper.h>

const int stepsPerRevolution = 200;
int buttonPinX = 2;
int buttonPinY = 3;
Stepper myStepper1(stepsPerRevolution, 50,51,52,53);   
Stepper myStepper2(stepsPerRevolution, 33,32,31,30);
int stepCount = 0;         // number of steps the motor has taken
int buttonState = 0;

void setup(){
Serial.begin(9600);
pinMode(buttonPinX,INPUT);
pinMode(buttonPinY,INPUT);

}

void loop(){
int i,j;
i = analogRead(buttonPinX);
j = analogRead(buttonPinY);
if (i>850){
    myStepper1.step(-1);
    myStepper2.step(-1);
    Serial.print("go:" );
    Serial.println(stepCount);
    stepCount++;
    delay(1);
}
if (i<250){
    myStepper1.step(1);
    myStepper2.step(1);
    Serial.print("back:" );
    Serial.println(stepCount);
    stepCount++;
    delay(5);
}
if (j>650){
    myStepper1.step(-1);
    myStepper2.step(1);
    Serial.print("left:" );
    Serial.println(stepCount);
    stepCount++;
    delay(5);
}
if (j<250){
    myStepper1.step(1);
    myStepper2.step(-1);
    Serial.print("right:" );
    Serial.println(stepCount);
    stepCount++;
    delay(5);
}
}

BlackCat 发表于 2012-6-29 17:14:43

问题已解决,希望能帮到用得着的朋友。:lol

pww999 发表于 2012-6-29 17:36:54

本帖最后由 pww999 于 2012-6-29 17:49 编辑

我也在弄,回头试下

stepCount++;
这个是速度递加吗?

BlackCat 发表于 2012-6-30 11:05:14

我也是初学:L,不过串口发送的数据来看,应该是步数。你可以翻步进电机的库文件看看。

8200 发表于 2013-4-19 10:33:39

记号起来好学习。
谢谢分享!
页: [1]
查看完整版本: 步进电机驱动问题,懂的帮忙解决下