极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 20357|回复: 10

Arduino_壁障小车

[复制链接]
发表于 2015-2-10 08:13:23 | 显示全部楼层 |阅读模式
本帖最后由 请叫我小萌兽 于 2015-2-10 08:13 编辑

笔者文采不好,不废话,直接上图!


ARDUINO代码:
const int TrigPin = 2;                          //将 Arduino 的 Pin2 连接至 HC-SR04 的 Echo
const int EchoPin = 3;                        //将 Arduino 的 Pin3 连接至 HC-SR04 的 Trig  
unsigned  long  Time_Echo_us = 0;   //用于记录传感器返回的脉冲宽度
float    cm = 0;                                   //用于存储距离值
int  motorPin1 = 4;                            //将 Arduino 的 Pin4 连接至 L298N 的 IN3
int  motorPin2 = 5;                            //将 Arduino 的 Pin5 连接至 L298N 的 IN4
int  motorPin3 = 6;                            //将 Arduino 的 Pin6 连接至 L298N 的 IN2
int  motorPin4 = 7;                            //将 Arduino 的 Pin7 连接至 L298N 的 IN1
int  s1;                                               //用于存储距离值
int  s2;                                               //用于存储距离值

#include <Servo.h>
#include <LCD5110_CN.h>

Servo  myservo;                                   //声明一个 Servo 对象
LCD5110  myGLCD(8,9,10,11,12);       //Clk → Pin 8, Din → Pin9, DC → Pin 10, CE → Pin 12, RST → 11
extern  uint8_t  SmallFont[];
extern  uint8_t  MediumNumbers[];
extern  uint8_t  BigNumbers[];

void setup()
{   
   myGLCD.InitLCD();                           //液晶初始化
   myservo.attach(1, 500, 2500);         //引脚 1 上的舵机连接到 myservo 对象
   myservo.writeMicroseconds(1500);   //设置舵机到初始位置
   pinMode(TrigPin, OUTPUT);
   pinMode(EchoPin, INPUT);
   pinMode(motorPin1, OUTPUT);
   pinMode(motorPin2, OUTPUT);
   pinMode(motorPin3, OUTPUT);
   pinMode(motorPin4, OUTPUT);
}

void  forward()                         //向后
{
   digitalWrite(motorPin1, HIGH);
   digitalWrite(motorPin2, LOW);
   digitalWrite(motorPin3, HIGH);
   digitalWrite(motorPin4, LOW);
}

void  stop()                               //刹停
{
   digitalWrite(motorPin1, HIGH);
   digitalWrite(motorPin2, HIGH);
   digitalWrite(motorPin3, HIGH);
   digitalWrite(motorPin4, HIGH);
}

void  backward()                      //向后
{
   digitalWrite(motorPin1, LOW);
   digitalWrite(motorPin2, HIGH);
   digitalWrite(motorPin3, LOW);
   digitalWrite(motorPin4, HIGH);
}

void  turn_right()                     //右转
{
   digitalWrite(motorPin1, HIGH);
   digitalWrite(motorPin2, LOW);
   digitalWrite(motorPin3, LOW);
   digitalWrite(motorPin4, HIGH);
}

void  turn_left()                       //左转
{
   digitalWrite(motorPin1, LOW);
   digitalWrite(motorPin2, HIGH);
   digitalWrite(motorPin3, HIGH);
   digitalWrite(motorPin4, LOW);
}

void  control()
{
   digitalWrite(TrigPin, LOW);        //低高低电平发一个短时间脉冲去TrigPin
   delayMicroseconds(2);
   digitalWrite(TrigPin, HIGH);       //开始通过 TrigPin 发送脉冲
   delayMicroseconds(10);           //设置脉冲宽度为 10us
   digitalWrite(TrigPin, LOW);      //结束脉冲
   
   Time_Echo_us = pulseIn(EchoPin, HIGH);                      //获取 HC-SR04 返回的脉冲宽度
   if((Time_Echo_us < 23200) && (Time_Echo_us > 1))    //脉冲有限范围(1, 23200)
   {
     cm = Time_Echo_us / 58.0;                                       //将回波时间换算成cm
     myGLCD.setFont(BigNumbers);                                  //大号字体
     myGLCD.printNumF(cm,2, RIGHT, 24);                      //输出结果至 myGLCD
     delay(10);                              
     myGLCD.clrScr();                                                      //清屏
   }
}  

void  comm()
{
   for( int  pos = 1500; pos <= 2400; pos++)               //舵机转至 左侧
   {
     myservo.writeMicroseconds(pos);         
     delay(1);
   }
   control();                                                                   //超声波测距
   s1 = cm;                                                                   //s1 存储小车左侧所距障碍物距离
  
   for(int pos = 2400; pos >= 600; pos--)                    //舵机转至 右侧      
   {   
     myservo.writeMicroseconds(pos);
     delay(1);
   }
   control();                                          //超声波测距
   s2 = cm;                                          //s2 存储小车右侧所距障碍物距离
  
   if(s1 > s2)                                       
   {
     turn_left();                                    
     delay(150);
   }
else  
   if(s1 < s2)
   {
     turn_right();
     delay(150);
   }
else
   if((s1 <= 20) || (s2 <= 20))
   {
     backward();
     delay(100);
   }
   
   stop();
   delay(50);
  
   for(int  pos = 600; pos <= 1500; pos++)              //设置舵机到初始位置
   {
     myservo.writeMicroseconds(pos);
     delay(1);
   }
}
  
void  loop()
{
   control();                  //超声波测距

   if(cm <= 20)
   {
     stop();                   //刹停
     comm();                //壁障处理
   }
   else
   {
     forward();               //前进
   }
}


超声波测距,5110显示距离 视频:


壁障小车 视频:
http://www.tudou.com/programs/view/JfsmhDzMrpY/





5110汉字取模软件和库文件使用方法,详见:
http://www.geek-workshop.com/for ... &highlight=5110

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

 楼主| 发表于 2015-2-10 08:25:56 | 显示全部楼层
沙发自己坐!!!
漏发了Arduino UNO 转接板的 PCB.
补上…

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2015-2-10 10:38:27 | 显示全部楼层
厉害,学习了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-2-10 10:56:41 | 显示全部楼层
213菜鸟 发表于 2015-2-10 10:38
厉害,学习了

谢谢支持。
回复 支持 反对

使用道具 举报

发表于 2015-2-16 13:03:17 | 显示全部楼层
请问楼主电机不用调速就能走直线?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-3-2 17:14:49 | 显示全部楼层
GaryChan 发表于 2015-2-16 13:03
请问楼主电机不用调速就能走直线?

不能。。。“黄香蕉电机”谅解吧!!!
回复 支持 反对

使用道具 举报

发表于 2015-3-4 14:57:15 | 显示全部楼层
请叫我小萌兽 发表于 2015-3-2 17:14
不能。。。“黄香蕉电机”谅解吧!!!

楼主好像没有利用电机驱动模块(小生用的是L298N)的两个使能端接口(ENA和ENB)连接UNO控制板进行pwm调速?那这样不会影响小车行走直线或转向的吗?求解……(调速过程小车总是偏离直线运动,另附代码。)
  1. int pwm1 = 10;   
  2. int IN1 = 4;
  3. int IN2 = 5;
  4. int pwm2 = 11;                        
  5. int IN3 = 6;
  6. int IN4 = 7;
  7. void setup()
  8. {
  9.   Serial.begin(9600);
  10.   pinMode(pwm1,OUTPUT);
  11.   pinMode(pwm2,OUTPUT);
  12.   pinMode(IN1,OUTPUT);
  13.   pinMode(IN2,OUTPUT);
  14.   pinMode(IN3,OUTPUT);
  15.   pinMode(IN4,OUTPUT);
  16. }
  17. void loop()
  18. {   
  19.    digitalWrite(IN2,HIGH);
  20.    digitalWrite(IN1,LOW);
  21.    digitalWrite(IN4,HIGH);
  22.    digitalWrite(IN3,LOW);
  23.    analogWrite(pwm1, 85);
  24.    analogWrite(pwm2, 79);   
  25.    delay(5000);
  26.    digitalWrite(IN2,LOW);
  27.    digitalWrite(IN1,LOW);
  28.    digitalWrite(IN4,LOW);
  29.    digitalWrite(IN3,LOW);
  30.    delay(2000);
  31. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-3-6 14:18:18 | 显示全部楼层
GaryChan 发表于 2015-3-4 14:57
楼主好像没有利用电机驱动模块(小生用的是L298N)的两个使能端接口(ENA和ENB)连接UNO控制板进行pwm调速? ...

我的确没用pwm调速,这样会影响小车行走直线或转向。
若您的小车用了pwm调速还总是偏离直线运动的话,那只能说您目前所附的pwm值还不正确。
回复 支持 反对

使用道具 举报

发表于 2015-3-6 14:57:49 | 显示全部楼层
楼主用的电源规格是多少?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-3-8 16:23:52 | 显示全部楼层
plot 发表于 2015-3-6 14:57
楼主用的电源规格是多少?

7.4v、1500mAh、30C
回复 支持 反对

使用道具 举报

发表于 2015-4-26 21:53:42 | 显示全部楼层
楼主,制作壁障小车需要哪些元件?
回复 支持 反对

使用道具 举报

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

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

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 01:29 , Processed in 0.052088 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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