极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 19679|回复: 10

新人求助,,arduino uno的板子

[复制链接]
发表于 2016-3-7 21:29:24 | 显示全部楼层 |阅读模式
     arduino uno的板子,电机驱动板,还有一个舵机驱动板。小车由两个步进电机驱动,前置一个hcsr04超声波传感器。电机驱动板电源电压24v,uno和舵机驱动板由单独电源供电,5到6v。想实现的功能是,当前方障碍物距离大于40cm时,前行。小于或等于40cm时,停止,然后舵机转动,复位,再退后。目前只控制一个舵机,以后可能要同时控制6个,程序如下:
发现启动后,小车前方障碍物距离大于40cm时小车长时间后退,
   #include <Servo.h>
Servo myservo1;
Servo myservo;


int Left_motor_P=3;   
int Left_motor_N=6;     
int Right_motor_P=5;   
int Right_motor_N=11;   
int Ultrasonic_Echo = A0;  
int Ultrasonic_Trig =A1;  
int Front_Distance = 0;      

void setup()
{
        Serial.begin(9600);   
        pinMode(Left_motor_P,OUTPUT);                
        pinMode(Left_motor_N,OUTPUT);                
        pinMode(Right_motor_P,OUTPUT);        
        pinMode(Right_motor_N,OUTPUT);        
        pinMode(Ultrasonic_Echo, INPUT);   
        pinMode(Ultrasonic_Trig, OUTPUT);     
        myservo1.attach(2);
        myservo.attach(12);
        
  }
void go(int a)   
{
        digitalWrite(Right_motor_P,HIGH);  
        digitalWrite(Right_motor_N,LOW);     
        analogWrite(Right_motor_P,80);
        analogWrite(Right_motor_N,0);
        digitalWrite(Left_motor_P,LOW);  
        digitalWrite(Left_motor_N,HIGH);
        analogWrite(Left_motor_P,0);
        analogWrite(Left_motor_N,90);
        delay(a * 100);   
}

void Stop (int f)         
{
        digitalWrite(Right_motor_P,LOW);
        digitalWrite(Right_motor_N,LOW);
        digitalWrite(Left_motor_P,LOW);
        digitalWrite(Left_motor_N,LOW);
        delay(f * 100);
}
void Reverse(int g)      
{
     digitalWrite(Right_motor_P,LOW);  
     digitalWrite(Right_motor_N,HIGH);
     analogWrite(Right_motor_P,0);
      analogWrite(Right_motor_N,80);
     digitalWrite(Left_motor_P,HIGH);  
     digitalWrite(Left_motor_N,LOW);
      analogWrite(Left_motor_P,90);
      analogWrite(Left_motor_N,0);
     delay(g * 100);
}

void ask_pin_F()   
{
      myservo.write(90);
      digitalWrite(Ultrasonic_Trig, LOW);   
      delayMicroseconds(2);
      digitalWrite(Ultrasonic_Trig, HIGH);  
      delayMicroseconds(10);
      digitalWrite(Ultrasonic_Trig, LOW);   
      float Fdistance = pulseIn(Ultrasonic_Echo, HIGH);   
      Fdistance= Fdistance/59;      
      Front_Distance = Fdistance;     
      
}  

void loop()
{
    ask_pin_F();
    if(Front_Distance>=40)
    {
      go(1);
    }
    else
    {
      Stop(1);
      myservo1.attach(45);
      myservo1.attach(90);
      Reverse(1);
    }
   
}
      
     
     

        
              
              
      

                  
回复

使用道具 举报

发表于 2016-3-7 22:30:04 | 显示全部楼层
     digitalWrite(Right_motor_P,LOW);  
     digitalWrite(Right_motor_N,HIGH);
     analogWrite(Right_motor_P,0);
      analogWrite(Right_motor_N,80);
     digitalWrite(Left_motor_P,HIGH);  
     digitalWrite(Left_motor_N,LOW);
      analogWrite(Left_motor_P,90);
      analogWrite(Left_motor_N,0);
这部分存在问题,如果不太会用Arduino的PWM的输出可以先不使用它,我知道你是想要改变车子的速度,analogWrite(Right_motor_P,0)本身就等价digitalWrite(Right_motor_P,LOW);所以有些地方可以删去,先不使用anglog,仅仅控制车子的前进和后退,看看是否还会出现这种状况。另外不排除你的车子处于反转的状态(应该是大于40向前,但现在是向后)
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-8 15:03:20 | 显示全部楼层
164335413 发表于 2016-3-7 22:30
digitalWrite(Right_motor_P,LOW);  
     digitalWrite(Right_motor_N,HIGH);
     analogWrite(Ri ...

谢谢指点,我再改改
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-10 10:04:16 | 显示全部楼层
164335413 发表于 2016-3-7 22:30
digitalWrite(Right_motor_P,LOW);  
     digitalWrite(Right_motor_N,HIGH);
     analogWrite(Ri ...

大神,我现在的问题是,接上电源开启后,距离大于40cm时车子往前走,同时舵机也转动,并未按照void loop()
{ ask_pin_F();
if(Front_Distance < 40){Stop(1); myservo6.write(145);}   
else {go(10);}
}执行。
回复 支持 反对

使用道具 举报

发表于 2016-3-10 11:04:33 | 显示全部楼层
你现在ask_pin_F() 函数中是否有让舵机转动的代码?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-10 11:24:05 | 显示全部楼层
164335413 发表于 2016-3-10 11:04
你现在ask_pin_F() 函数中是否有让舵机转动的代码?

有,超声波传感器有一个9g舵机控制,我让他保持在90度,然后另外一个Mg996R舵机的动作总是不能满足我设定的距离条件,开启后就转动
回复 支持 反对

使用道具 举报

发表于 2016-3-11 19:09:17 | 显示全部楼层
问题不好找,你后来贴的代码不全,也没看到你修改后的代码。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-17 10:25:11 | 显示全部楼层
164335413 发表于 2016-3-11 19:09
问题不好找,你后来贴的代码不全,也没看到你修改后的代码。

不好意思今天才看到,前几天课还比较多,没弄这个。问题还是这样启动后,舵机立刻转,并不是小车先走,前方设定范围内有障碍物时停下来,舵机再转 。我买了个32路舵机控制板,用来控制三个mg996。超声波模块的sg90舵机用uno控制。步进电机的analog write我删掉后发现转速太快了,也没有解决问题。所以还是加上了
int Left_motor_P=6;   
int Left_motor_N=3;     
int Right_motor_P=5;   
int Right_motor_N=11;   
int Ultrasonic_Echo = A0;  
int Ultrasonic_Trig =A1;  
int Front_Distance = 0;      
      
int pos = 0;

        

int servopin=9;
int myangle;
int pulsewidth;
int val;
void servopulse(int servopin,int myangle)
{
        pulsewidth=(myangle*11)+500;
        digitalWrite(servopin,HIGH);
        delayMicroseconds(pulsewidth);
        digitalWrite(servopin,LOW);
        delay(20-pulsewidth/1000);
}

void setup()
{
        Serial.begin(115200);   
        pinMode(Left_motor_P,OUTPUT);                
        pinMode(Left_motor_N,OUTPUT);                
        pinMode(Right_motor_P,OUTPUT);        
        pinMode(Right_motor_N,OUTPUT);        
        pinMode(Ultrasonic_Echo, INPUT);   
        pinMode(Ultrasonic_Trig, OUTPUT);
        pinMode(servopin,OUTPUT);
}
void go(int a)   
{
        digitalWrite(Right_motor_P,HIGH);  
        digitalWrite(Right_motor_N,LOW);     
        analogWrite(Right_motor_P,80);
        analogWrite(Right_motor_N,0);
        digitalWrite(Left_motor_P,HIGH);  
        digitalWrite(Left_motor_N,LOW);
        analogWrite(Left_motor_P,90);
        analogWrite(Left_motor_N,0);
        delay(a * 100);   
}


void left(int c)      
{
        digitalWrite(Right_motor_P,LOW);       
        digitalWrite(Right_motor_N,LOW);
        digitalWrite(Left_motor_P,LOW);   
        digitalWrite(Left_motor_N,HIGH);
        analogWrite(Left_motor_P,0);
        analogWrite(Left_motor_N,80);
        delay(c * 100);         
}

   
void Stop (int f)         
{
        digitalWrite(Right_motor_P,LOW);
        digitalWrite(Right_motor_N,LOW);
        digitalWrite(Left_motor_P,LOW);
        digitalWrite(Left_motor_N,LOW);
        delay(f * 100);
}


void ask_pin_F()   
{
     for(int i=0;i<=25;i++) {
     servopulse(servopin,90);
     }
      
      digitalWrite(Ultrasonic_Trig, LOW);   // 讓超聲波發射低電壓2μs
      delayMicroseconds(2);
      digitalWrite(Ultrasonic_Trig, HIGH);  // 讓超聲波發射高電壓10μs,這裡至少是10μs
      delayMicroseconds(10);
      digitalWrite(Ultrasonic_Trig, LOW);    // 維持超聲波發射低電壓
      float Fdistance = pulseIn(Ultrasonic_Echo, HIGH);  // 讀差相差時間
      Fdistance= Fdistance/59;       // 將時間轉為距離距离(單位:公分)
      Front_Distance = Fdistance;
      
}

void loop()
{
    while(1)
    {
     ask_pin_F();
     if(Front_Distance<32)
      {
        Stop(1);
        Serial.print("#9P2500T1000\r\n");
        delay(1000);
        Serial.print("#9P1500T1000\r\n");
        delay(1000);
        left(1);
       }  
     else
      {
       go(8);
      }
      
      }
}


         
        
         
        
回复 支持 反对

使用道具 举报

发表于 2016-3-17 19:59:54 | 显示全部楼层
最好分步进行调试,将串口线连接到电脑,使用serial.print();打印出超声波的实际距离值,
另外,ask_pin_F();我不确定他起了什么作用,因为他一直保持在90度,如果你只打算让他保持90度,可以放在setup()函数中.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-17 21:01:30 | 显示全部楼层
164335413 发表于 2016-3-17 19:59
最好分步进行调试,将串口线连接到电脑,使用serial.print();打印出超声波的实际距离值,
另外,ask_pin_F ...

ask_pin_F就是测量正前方距离的,hcsr04好像真的有问题。。。我让它保持在同一个地方,结果距离忽大忽小,
49cm
5cm
#15P2000T1000
#15P1500T1000
0cm
#15P2000T1000
#15P1500T1000
32cm
0cm
#15P2000T1000
#15P1500T1000
26cm
#15P2000T1000
#15P1500T1000
54cm
25cm
#15P2000T1000
#15P1500T1000
19cm
#15P2000T1000
#15P1500T1000
46cm
27cm
#15P2000T1000
#15P1500T1000
23cm
回复 支持 反对

使用道具 举报

发表于 2016-3-18 18:45:05 | 显示全部楼层
两次测量的时间间隔要在60ms以上,另外要保持Trig大于10us,尽量到20us也许会好一些
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-3 13:18 , Processed in 0.039508 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表