数码之家

 找回密码
 立即注册
搜索
查看: 1498|回复: 1

[Arduino] Arduino:制作一个最少连线的避障智能小车

[复制链接]
发表于 2020-2-20 21:47:58 | 显示全部楼层 |阅读模式
之前做过一个避障小车,直接用arduino uno板子各种连线,今天突然想起前段时间玩剩下的L293驱动板也就是下图这块板子


这块驱动板支持最多4个直流电机,2个舵机同时使用,另外利用板子上的空闲端口a0-a5模拟口用作数字口,用来接超声波雷达组件,最终做出如下图的最少连线版超声波避障小车






以下为arduino代码
  1. #include <Servo.h>
  2. #include <AFMotor.h>

  3. Servo headservo;  
  4. AF_DCMotor motor_left(1);
  5. AF_DCMotor motor_right(2);

  6. const int EchoPin = 14;
  7. const int TrigPin = 15;

  8. const int HeadServopin = 9;
  9. const int maxStart = 5000;  

  10. // Variables
  11. int isStart = maxStart;   
  12. int currDist = 0;   
  13. boolean running = false;

  14. void setup() {
  15.   // put your setup code here, to run once:
  16.   Serial.begin(9600);
  17.   
  18.   pinMode(EchoPin, INPUT);
  19.   pinMode(TrigPin, OUTPUT);


  20.   headservo.attach(HeadServopin);
  21.   
  22.   headservo.write(50);
  23.   delay(1500);

  24.   headservo.write(130);
  25.   delay(1500);
  26.   
  27.   headservo.write(90);
  28.   delay(1500);

  29.   motor_left.setSpeed(130);  
  30.   motor_right.setSpeed(130);
  31. }

  32. void loop() {
  33.   // put your main code here, to run repeatedly:
  34.   
  35.   if (isStart > 0 )
  36.   {
  37.     isStart--;
  38.     Serial.println(isStart);
  39.    
  40.     currDist = MeasuringDistance();
  41.   
  42.     //Serial.println(currDist);
  43.     if(currDist > 30) {
  44.       carrun();
  45.     }
  46.     else if(currDist < 15){
  47.       backup();
  48.       randTrun();
  49.     }
  50.   }else
  51.   {
  52.     totalhalt();
  53.     }
  54. }

  55. long MeasuringDistance() {

  56.   long duration;

  57.   digitalWrite(TrigPin, LOW);
  58.   delayMicroseconds(2);

  59.   digitalWrite(TrigPin, HIGH);
  60.   delayMicroseconds(5);

  61.   digitalWrite(TrigPin, LOW);

  62.   duration = pulseIn(EchoPin, HIGH);

  63.   return duration / 29 / 2;
  64. }

  65. void carrun() {
  66.   running = true;
  67.   
  68.   motor_left.run(FORWARD)
  69.   motor_right.run(FORWARD)

  70.   return;
  71. }  

  72. void backup() {

  73.   running = true;

  74.   motor_left.run(BACKWARD);  
  75.   motor_right.run(BACKWARD);  

  76.   delay(1000);
  77. }

  78. void randTrun(){

  79.   long randNumber;
  80.   randomSeed(analogRead(0));
  81.   randNumber = random(0, 10);

  82.   if(randNumber > 5) {
  83.     body_rturn();
  84.   }
  85.   else{
  86.     body_lturn();
  87.   }
  88. }

  89. void body_lturn() {
  90.   headservo.write(60);  //left look
  91.   running = true;

  92.   motor_left.run(BACKWARD);  
  93.   motor_right.run(FORWARD);

  94.   delay(1500);

  95.   totalhalt();
  96. }  

  97. void body_rturn() {
  98.   headservo.write(120);  
  99.   running = true;

  100.   motor_left.run(FORWARD);  
  101.   motor_right.run(BACKWARD);

  102.   delay(1500);
  103.   totalhalt();
  104. }  

  105. void totalhalt() {

  106.   motor_left.run(RELEASE);
  107.   motor_right.run(RELEASE);

  108.   headservo.write(90);

  109.   running = false;
  110.   return;
  111.   delay(1000);
  112. }
复制代码


本帖子中包含更多资源

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

x

打赏

参与人数 3家元 +87 收起 理由
数码家园 + 12
人艰不拆了 + 15
家睦 + 60

查看全部打赏

发表于 2020-2-22 09:55:28 | 显示全部楼层
看成了能避智障小车。。。
回复 支持 反对

使用道具 举报

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

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2024-4-26 01:35 , Processed in 0.109200 second(s), 12 queries , Redis On.

Powered by Discuz!

© 2006-2023 smzj.net

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