首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >机器人臂寻的

机器人臂寻的
EN

Stack Overflow用户
提问于 2022-11-10 14:51:24
回答 1查看 20关注 0票数 0

我正在做一个仿人机器人手臂,到目前为止,我已经设计了肩膀。对于旋转,我一直使用高扭矩直流电机与编码器,它有一个霍尔传感器,并使用17个脉冲,一个完整的旋转,连同齿轮箱的齿轮比为1:625,使总脉冲到(625x17)。现在,机器人手臂肩部只能移动90度左右,产生脉冲(625x17x0.25 = 6256.25)。我用一个Arduino和一个L298 H桥模块来控制这个直流电机。我已经使用Arduino IDE编写了一段代码,我仍然是这方面的初学者。

这里的主要前提是做肩膀的归航。基本上,当机器人首先打开时,它沿着逆时针方向移动,寻找极限开关,然后从一开始就将脉冲数到90度(6256.25脉冲)时钟方向。

这里的主要问题是,在按下限位开关后,电机在碰到限位开关后开始顺时针方向旋转,但在达到90度后才停止。

任何帮助都是非常感谢的。我对编码和堆栈溢出都很陌生。

代码语言:javascript
复制
#define Encoder_output_A 2
#define Encoder_output_B 3
int y;
int pos = 0;
int motorpin1=5;
int motorpin2=4;
float Count_pulses = 0;
float Count_rounds = 0;
int Limswitch = 7;



void setup() {
Serial.begin(9600); // activates the serial communication
pinMode(Encoder_output_A,INPUT); // sets the Encoder_output_A pin as the input
pinMode(Encoder_output_B,INPUT); // sets the Encoder_output_B pin as the input
attachInterrupt(digitalPinToInterrupt(Encoder_output_A),DC_Motor_Encoder,RISING);
pinMode(motorpin1,OUTPUT);
pinMode(motorpin2,OUTPUT);
pinMode(Limswitch,INPUT);
homing();



}
 
void loop() {
  Serial.print("Result: ");
  Serial.println(Count_pulses); 
  Serial.println(Count_rounds);
  delay(100);

y=digitalRead(Limswitch);

if(y==LOW ||Count_pulses>=2656.25)// 17 x 625 x 0.25 = 2656.25
{digitalWrite(motorpin1,LOW);
digitalWrite(motorpin2,LOW);
} else{
digitalWrite(motorpin1,HIGH);
digitalWrite(motorpin2,LOW);
}
}
  
//The purpose of this code is to count the ouput pulses or  the encoder outputs as you rotate the Motor shaft.

void DC_Motor_Encoder(){
  int b = digitalRead(Encoder_output_B);
  if(b > 0){
    Count_pulses++;
    Count_rounds = Count_pulses/17;
  }
  else{
    Count_pulses--;
    
  }


}

void homing() {
y=digitalRead(Limswitch);
if(y==HIGH && Count_pulses==0)
{while(y==HIGH){digitalWrite(motorpin1,HIGH);
digitalWrite(motorpin2,LOW);
y=digitalRead(Limswitch); 
}
} 
else if(y==LOW){
while(Count_pulses < 2657){
digitalWrite(motorpin1,LOW);
digitalWrite(motorpin2,HIGH);
}
}
if(Count_pulses>=2656.25)
digitalWrite(motorpin1,LOW);
digitalWrite(motorpin2,LOW);
}
EN

回答 1

Stack Overflow用户

发布于 2022-11-10 20:14:16

您的代码有点难以阅读,您的格式化方式,我建议您遵循一个更典型的标准。这将使问题更容易解决。下面是如何格式化homing()函数:

代码语言:javascript
复制
void homing() {
   y=digitalRead(Limswitch);
   if(y==HIGH && Count_pulses==0) {
     while(y==HIGH) { 
        digitalWrite(motorpin1,HIGH);
        digitalWrite(motorpin2,LOW);
        y=digitalRead(Limswitch); 
     }
   } else if(y==LOW){
      while(Count_pulses < 2657) {
        digitalWrite(motorpin1,LOW);
        digitalWrite(motorpin2,HIGH);
      }
   }

   if(Count_pulses>=2656.25) {
     digitalWrite(motorpin1,LOW);
     digitalWrite(motorpin2,LOW);
   }
}

问题可能是,在最后一个if语句之后,您缺少了一个卷曲括号。我把它加在上面了。

这个:if(Count_pulses>=2656.25) {

翻阅原作:if(Count_pulses>=2656.25)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74390942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档