没有CCW旋转



代码以CW(flex(运动运行,但当检测到或命中开关时,将不会以CCW(dflex(运动运行。当手动按下或撞击机械限位开关时,电机会尝试一些东西,但它不会停止 CW 运动,并且绝对不会将其方向更改为 CCW。

与具有47Gear减速的双极步进电机配合使用

const int LS = 2; // input pin for the Limit Switch
int detectState = 0; // Variable for reading the encoder status
const int stepPin = 5; 
const int dirPin = 4; 
const int enablePin = 3;
double GReduction = 47;
int T1 = 100;
int T2 = 100;
int Z = 0;
int Walk_flag = 1;
double angle = 0;
//

/flex(int ang,int tm(;a = 角度,TM = 时间

void setup()
{
pinMode(LS, INPUT); //Set pin 2 as input
pinMode(stepPin,OUTPUT); //Set pin 5 as input
pinMode(dirPin,OUTPUT);//Set pin 4 as input
pinMode(enablePin,OUTPUT);//Set pin 3 as input
attachInterrupt(digitalPinToInterrupt(LS), LS_Hit, CHANGE);
////////////homing algorithm 
//T1=100;
dflex();
Serial.begin(9600);
angle=0;
flex(90,1);
}
void loop() {
 flex(30,100);
  delay(1000);
  if( detectState==HIGH)
 {
dflex();
 }
}
///////////////////////////////////// CW motion
void flex(int ang,int tm) // an = angle, tm= time
{
   double ZZ;
   ZZ= (800.0*ang*GReduction/360.0);  
   //String val = String(ang/360);
   Serial.println(ang);
  for(int x = 0; x <ZZ; x++) {    //angle control
    digitalWrite(dirPin,HIGH);
    digitalWrite(enablePin,HIGH);
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(T1);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(T1);
    }
digitalWrite(enablePin,LOW);
  }
/////////CCW motion
void dflex() 
{
   digitalWrite(enablePin,HIGH);
   digitalWrite(dirPin,LOW); //Changes the rotations direction
   int LS_status=digitalRead(LS);
    while(digitalRead(LS)) {
      if(digitalRead(LS)==HIGH)
      {
        break; 
      }
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(T2);
    //delay(100/T); // One second delay
    digitalWrite(stepPin,LOW);
    //delay(T/100); // One second delay
    delayMicroseconds(T2);
}
  }
void LS_Hit()
{
 digitalWrite(enablePin,LOW);
 angle=0; 
  detectState =! detectState;
}

代码的以下部分存在问题:

while(digitalRead(LS))
{
    if(digitalRead(LS) == HIGH)
    {
       break; 
    }
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(T2);
    //delay(100/T); // One second delay
    digitalWrite(stepPin, LOW);
    //delay(T/100); // One second delay
    delayMicroseconds(T2);
}
此 while 循环

中的步骤将永远不会完成,因为 while 循环的进入条件与中断条件相同。

具体来说,控件只有在返回HIGH digitalRead(LS)时才会进入 while 循环,但在它进入 while 循环后,您立即再次执行相同的检查,如果函数返回 HIGH,您将中断 while 循环。这永远不会允许该功能使电机朝相反方向行驶。

您的代码还有其他几个问题,但这个问题最能说明问题。

相关内容

  • 没有找到相关文章

最新更新