请叫我小萌兽 发表于 2015-2-10 08:13:23

Arduino_壁障小车

本帖最后由 请叫我小萌兽 于 2015-2-10 08:13 编辑

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


ARDUINO代码:
const int TrigPin = 2;                        //将 Arduino 的 Pin2 连接至 HC-SR04 的 Echo
const int EchoPin = 3;                        //将 Arduino 的 Pin3 连接至 HC-SR04 的 Trig
unsignedlongTime_Echo_us = 0;   //用于记录传感器返回的脉冲宽度
float    cm = 0;                                 //用于存储距离值
intmotorPin1 = 4;                            //将 Arduino 的 Pin4 连接至 L298N 的 IN3
intmotorPin2 = 5;                            //将 Arduino 的 Pin5 连接至 L298N 的 IN4
intmotorPin3 = 6;                            //将 Arduino 的 Pin6 连接至 L298N 的 IN2
intmotorPin4 = 7;                            //将 Arduino 的 Pin7 连接至 L298N 的 IN1
ints1;                                             //用于存储距离值
ints2;                                             //用于存储距离值

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

Servomyservo;                                 //声明一个 Servo 对象
LCD5110myGLCD(8,9,10,11,12);       //Clk → Pin 8, Din → Pin9, DC → Pin 10, CE → Pin 12, RST → 11
externuint8_tSmallFont[];
externuint8_tMediumNumbers[];
externuint8_tBigNumbers[];

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);
}

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

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

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

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

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

voidcontrol()
{
   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();                                                      //清屏
   }
}

voidcomm()
{
   for( intpos = 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(intpos = 600; pos <= 1500; pos++)            //设置舵机到初始位置
   {
   myservo.writeMicroseconds(pos);
   delay(1);
   }
}

voidloop()
{
   control();                  //超声波测距

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


超声波测距,5110显示距离 视频:
http://www.tudou.com/programs/view/kRoqbK5GwE4/

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





5110汉字取模软件和库文件使用方法,详见:
http://www.geek-workshop.com/forum.php?mod=viewthread&tid=5081&highlight=5110

请叫我小萌兽 发表于 2015-2-10 08:25:56

沙发自己坐!!!
漏发了Arduino UNO 转接板的 PCB.
补上…

213菜鸟 发表于 2015-2-10 10:38:27

厉害,学习了

请叫我小萌兽 发表于 2015-2-10 10:56:41

213菜鸟 发表于 2015-2-10 10:38 static/image/common/back.gif
厉害,学习了

谢谢支持。:handshake

GaryChan 发表于 2015-2-16 13:03:17

请问楼主电机不用调速就能走直线?

请叫我小萌兽 发表于 2015-3-2 17:14:49

GaryChan 发表于 2015-2-16 13:03 static/image/common/back.gif
请问楼主电机不用调速就能走直线?

不能。。。“黄香蕉电机”谅解吧!!!

GaryChan 发表于 2015-3-4 14:57:15

请叫我小萌兽 发表于 2015-3-2 17:14 static/image/common/back.gif
不能。。。“黄香蕉电机”谅解吧!!!

楼主好像没有利用电机驱动模块(小生用的是L298N)的两个使能端接口(ENA和ENB)连接UNO控制板进行pwm调速?那这样不会影响小车行走直线或转向的吗?求解……(调速过程小车总是偏离直线运动,另附代码。)int pwm1 = 10;   
int IN1 = 4;
int IN2 = 5;
int pwm2 = 11;                        
int IN3 = 6;
int IN4 = 7;
void setup()
{
Serial.begin(9600);
pinMode(pwm1,OUTPUT);
pinMode(pwm2,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
}
void loop()
{   
   digitalWrite(IN2,HIGH);
   digitalWrite(IN1,LOW);
   digitalWrite(IN4,HIGH);
   digitalWrite(IN3,LOW);
   analogWrite(pwm1, 85);
   analogWrite(pwm2, 79);   
   delay(5000);
   digitalWrite(IN2,LOW);
   digitalWrite(IN1,LOW);
   digitalWrite(IN4,LOW);
   digitalWrite(IN3,LOW);
   delay(2000);
}

请叫我小萌兽 发表于 2015-3-6 14:18:18

GaryChan 发表于 2015-3-4 14:57 static/image/common/back.gif
楼主好像没有利用电机驱动模块(小生用的是L298N)的两个使能端接口(ENA和ENB)连接UNO控制板进行pwm调速? ...

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

plot 发表于 2015-3-6 14:57:49

楼主用的电源规格是多少?

请叫我小萌兽 发表于 2015-3-8 16:23:52

plot 发表于 2015-3-6 14:57 static/image/common/back.gif
楼主用的电源规格是多少?

7.4v、1500mAh、30C

灵感小石头 发表于 2015-4-26 21:53:42

楼主,制作壁障小车需要哪些元件?
页: [1]
查看完整版本: Arduino_壁障小车