找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3548|回复: 1
打印 上一主题 下一主题
收起左侧

Arduino蓝牙智能检测小车制作

[复制链接]
跳转到指定楼层
楼主
最近自己做了一个智能小车(算是吧),我是拿eclipse开发arduino的,做得不是太好,但还是分享给大家,下面先看看几张实物图:



程序源码:
  1. /*
  2. * ETARobot.cpp
  3. *
  4. *  Created on: 2017年11月
  5. *      Author: KACHEN
  6. */

  7. #include <WProgram.h>
  8. #include <main.h>
  9. #include "OpenContinMotor.h"
  10. #include "OpenBuzzer.h"
  11. #include "OpenGasSensor.h"
  12. #include "OpenFlameSensor.h"
  13. #include "OpenPhotoelectric.h"
  14. #include "OpenUltrasonic.h"
  15. #include "OpenAngleServo.h"
  16. #include "OpenQti.h"

  17. #define leftservo  19   //左私服舵机
  18. #define rightservo  18  //右私服舵机
  19. #define bell 13         //报警器
  20. #define servopin 12     //角度舵机
  21. //传感器引脚定义
  22. const int gaspin[3]={2,3}; //气体传感器
  23. const int flamepin[4]={4,5,6}; //火焰传感器
  24. const int pelectricpin[3]={7,8}; //光电传感器
  25. const int qtipin[3] = {17,16}; //超声波处QTI
  26. const int trig = 15; //超声波Trig引脚
  27. const int echo = 14; //超声波Echo引脚

  28. //子函数
  29. void FastGoForward(){
  30.         PulseOut(leftservo,1700);
  31.         PulseOut(rightservo,1300);
  32.         delay(15);
  33. }
  34. void SlowGoForward(){
  35.         PulseOut(leftservo,1600);
  36.         PulseOut(rightservo,1400);
  37.         delay(20);
  38. }

  39. void GoBack(void){
  40.         PulseOut(leftservo,1300);
  41.         PulseOut(rightservo,1700);
  42.         delay(15);
  43. }
  44. /*
  45. * 左旋转
  46. */
  47. void SpinLeft(void){
  48.         PulseOut(leftservo,1300);
  49.         PulseOut(rightservo,1300);
  50.         delay(20);
  51. }
  52. /*
  53. * 右旋转
  54. */
  55. void SpinRight(void){
  56.         PulseOut(leftservo,1700);
  57.         PulseOut(rightservo,1700);
  58.         delay(20);
  59. }
  60. void TurnLeft(void){
  61.         PulseOut(leftservo,1500);
  62.         PulseOut(rightservo,1300);
  63.         delay(10);
  64. }
  65. void TurnRight(void){
  66.         PulseOut(leftservo,1700);
  67.         PulseOut(rightservo,1500);
  68.         delay(10);
  69. }
  70. void Stop(void){
  71.         PulseOut(leftservo,1500);
  72.         PulseOut(rightservo,1500);
  73.         delay(20);
  74. }
  75. void TurnLeftAnyPulse(int pulses){
  76.         while(pulses--)
  77.         {
  78.                 SpinLeft();
  79.                 delay(2);
  80.         }
  81. }
  82. void TurnRightAnyPulse(int pulses){
  83.         while(pulses--)
  84.         {
  85.                 SpinRight();
  86.                 delay(2);
  87.         }
  88. }

  89. void InitAngle(void){

  90.          servopulse(servopin,100);
  91.          delay(100);
  92.          servopulse(servopin,100);
  93. }
  94. void AngleLeft90(void){

  95.          servopulse(servopin,170);
  96. }
  97. void AngleRight90(void){

  98.          servopulse(servopin,30);
  99. }
  100. void spinRight90To0(int j=100){
  101.      while(j--)
  102.      {
  103.          servopulse(servopin,j);
  104.          if(j==30)
  105.          break;
  106.      }
  107.      while(j++)
  108.         {
  109.             servopulse(servopin,j);
  110.             if(j==100)
  111.             break;
  112.         }
  113. }
  114. void spinLeft90To0(int j=100){
  115.      while(j++)
  116.      {
  117.          servopulse(servopin,j);
  118.          if(j==170)
  119.          break;
  120.      }
  121.      while(j--)
  122.          {
  123.              servopulse(servopin,j);
  124.              if(j==100)
  125.              break;
  126.          }
  127. }

  128. /*
  129. * //Gas传感器是否检测到信号并报警
  130. */
  131. void IsGasSenser(void){
  132.         if(GetGasStatus(gaspin[0])==true && GetGasStatus(gaspin[1])==true){
  133.                 BuzzerOFF(bell);
  134.         }
  135.         else
  136.         {
  137.                 BuzzerON(bell);
  138.                 delay(500);
  139.                 BuzzerOFF(bell);
  140.                 delay(500);
  141.         }
  142. }
  143. /*
  144. * //火焰 传感器是否检测到信号并报警
  145. */
  146. void IsFlameSenser(void){
  147. if(GetFlameStatus(flamepin[0])==true && GetFlameStatus(flamepin[1])==true && GetFlameStatus(flamepin[2])==true){
  148.         BuzzerOFF(bell);
  149.         }
  150.   else
  151.   {
  152.         BuzzerON(bell);
  153.         delay(500);
  154.         BuzzerOFF(bell);
  155.         delay(500);
  156.   }
  157. }
  158. /*
  159. * 前方光电管寻迹避障
  160. */
  161. void IsPhotoelectric(void){

  162.         if(GetPelectricStatus(pelectricpin[0])==false && GetPelectricStatus(pelectricpin[1])==true)
  163.         {
  164.                 for(int i=0;i<20;i++)
  165.                 {
  166.                  TurnRight();
  167.                 }
  168.         }
  169.         else if(GetPelectricStatus(pelectricpin[0])==true && GetPelectricStatus(pelectricpin[1])==false )
  170.         {
  171.                 for(int i=0;i<20;i++)
  172.                 {
  173.                  TurnLeft();
  174.                 }

  175.         }
  176.         else if(GetPelectricStatus(pelectricpin[0])==false && GetPelectricStatus(pelectricpin[1])==false )
  177.                 {
  178.                 for(int i=0;i<60;i++)
  179.                         {
  180.                             GoBack();
  181.                         }
  182.                    TurnLeftAnyPulse(36);
  183.                 }
  184. }
  185. /*
  186. * 前方光电管超声波避障时转向
  187. */
  188. void PhotoelectricTurn(void){
  189. if(GetPelectricStatus(pelectricpin[0])==false && GetPelectricStatus(pelectricpin[1])==true)
  190.                 {
  191.             TurnRightAnyPulse(25);
  192.                 }
  193. else if(GetPelectricStatus(pelectricpin[0])==true && GetPelectricStatus(pelectricpin[1])==false )
  194.                 {
  195.             TurnLeftAnyPulse(25);
  196.                 }
  197. else if(GetPelectricStatus(pelectricpin[0])==false && GetPelectricStatus(pelectricpin[1])==false)
  198. {
  199.         for(int i=0;i<60;i++)
  200.         {
  201.             GoBack();
  202.         }
  203.    TurnLeftAnyPulse(36);
  204. }
  205. else if(GetPelectricStatus(pelectricpin[0])==true && GetPelectricStatus(pelectricpin[1])==true)
  206. {
  207.             TurnLeftAnyPulse(56);
  208. }
  209. }
  210. /*
  211. * //检测中间左QTI是否在黑线内
  212. */
  213. boolean IsMLeftQtiBlack(void){
  214.         if(GetQtiStatus(qtipin[0])==true)
  215.         {
  216.                 delay(2);
  217.                 if(GetQtiStatus(qtipin[0])==true)
  218.                 {
  219.                         return true;
  220.                 }
  221.                 else
  222.                 {
  223.                         return false;
  224.                 }
  225.         }
  226.         else
  227.         {
  228.                 return false;
  229.         }
  230. }
  231. /*
  232. * //检测中间右QTI是否在黑线内
  233. */
  234. boolean IsMRightQtiBlack(void){
  235.         if(GetQtiStatus(qtipin[1])==true)
  236.         {
  237.                 delay(2);
  238.                 if(GetQtiStatus(qtipin[1])==true)
  239.                 {
  240.                         return true;
  241.                 }
  242.                 else
  243.                 {
  244.                         return false;
  245.                 }
  246.         }
  247.         else
  248.         {
  249.                 return false;
  250.         }
  251. }

  252. /*
  253. * 前方中间QTI循线快速
  254. */
  255. boolean MoveMiddleQti(void){
  256.         if(GetQtiStatus(qtipin[0])==false && GetQtiStatus(qtipin[1])==false)
  257.         {
  258.                 FastGoForward();
  259.         }
  260.         else if(GetQtiStatus(qtipin[0])==false && GetQtiStatus(qtipin[1])==true)
  261.         {

  262.                 TurnRight();
  263.         }
  264.         else if(GetQtiStatus(qtipin[0])==true && GetQtiStatus(qtipin[1])==false)
  265.         {
  266.                 TurnLeft();
  267.         }
  268.         else if(GetQtiStatus(qtipin[0])==true && GetQtiStatus(qtipin[1])==true)
  269.         {
  270.                 return true;
  271.         }
  272.         return false;
  273. }

  274. /*
  275. * 前方中间QTI循线慢速
  276. */
  277. boolean MoveQti(void){
  278.         if(GetQtiStatus(qtipin[1])==false && GetQtiStatus(qtipin[2])==false)
  279.         {
  280.                 PulseOut(leftservo,1600);
  281.                 PulseOut(rightservo,1400);
  282.         }
  283.         else if(GetQtiStatus(qtipin[1])==false && GetQtiStatus(qtipin[2])==true)
  284.         {
  285.                 PulseOut(leftservo,1530);
  286.                 PulseOut(rightservo,1500);
  287.         }
  288.         else if(GetQtiStatus(qtipin[1])==true && GetQtiStatus(qtipin[2])==false)
  289.         {
  290.                 PulseOut(leftservo,1500);
  291.                 PulseOut(rightservo,1470);
  292.         }
  293.         else if(GetQtiStatus(qtipin[1])==true && GetQtiStatus(qtipin[2])==true)
  294.         {
  295.                 return true;
  296.         }
  297.         return false;
  298. }

  299. /*
  300. * 前方中间QTI调整
  301. */
  302. void MiddleQtiAdjust(void){
  303.         if(IsMLeftQtiBlack())
  304.         {
  305.         while(IsMLeftQtiBlack())
  306.          {
  307.                 PulseOut(leftservo,1450);
  308.                 PulseOut(rightservo,1450);
  309.                 delay(20);
  310.          }
  311.         }
  312.         else if(IsMRightQtiBlack())
  313.          {
  314.           while(IsMRightQtiBlack())
  315.          {
  316.                   PulseOut(leftservo,1550);
  317.                   PulseOut(rightservo,1550);
  318.                   delay(20);

  319.          }
  320.         }
  321.             Stop();
  322.             delay(100);
  323. }

  324. void SetDistance(void){
  325. if(GetPelectricStatus(pelectricpin[0])==true && GetPelectricStatus(pelectricpin[1])==true)
  326. {
  327.                  if(DistanceDetection()>100){
  328.                  for(int i=0;i<20;i++)
  329.                       FastGoForward();
  330.                                            }
  331.                  else if(DistanceDetection()<100 && DistanceDetection()>10){
  332.                          for(int i=0;i<10;i++)
  333.                          FastGoForward();
  334.                                      }

  335.                   else if(DistanceDetection()<10 && DistanceDetection()>0){
  336.                       Stop();
  337.                       delay(100);
  338.                       PhotoelectricTurn();
  339.                             }
  340. }
  341.             else{
  342.                         IsPhotoelectric();
  343.                 }

  344. }
  345. //初始化
  346. void setup(void){
  347.         Serial.begin(9600);
  348.         InitContinMotorPin(leftservo);
  349.         InitContinMotorPin(rightservo);
  350.         Stop();
  351.         InitBuzzerPin(bell);
  352.         InitGasSensor(gaspin[0]);
  353.         InitGasSensor(gaspin[1]);
  354.         InitFlameSensor(flamepin[0]);
  355.         InitFlameSensor(flamepin[1]);
  356.         InitFlameSensor(flamepin[2]);
  357.         InitPhotoelectric(pelectricpin[0]);
  358.         InitPhotoelectric(pelectricpin[1]);
  359.         InitSingleQti(qtipin[0]);
  360.         InitSingleQti(qtipin[1]);
  361.         InitUltrasonic(trig,echo);
  362.         InitAngleServo(servopin);



  363. }
  364. //loop循环
  365. void loop(void){
  366.         if(Serial.available()>0)
  367.         {
  368.         char ch = Serial.read();
  369.         switch(ch){
  370.         case 'A':
  371.                 Serial.print("A:前进 ");
  372.                 for(int i=0;i<5;i++)
  373.                 {
  374.                 FastGoForward();
  375.                 }
  376.         break;
  377.         case 'B':
  378.                 Serial.print("B:后退");
  379.                 for(int i=0;i<5;i++)
  380.                 {
  381.                 GoBack();
  382.                 }
  383.         break;
  384.         case 'C':
  385.                 Serial.print("C:左转 ");
  386.                 for(int i=0;i<5;i++)
  387.                 {
  388.                 TurnLeft();
  389.                 }
  390.         break;
  391.         case 'D':
  392.                 Serial.print("D:右转");
  393.                 for(int i=0;i<5;i++)
  394.                 {
  395.                 TurnRight();
  396.                 }
  397.         break;
  398.         case 'e':
  399.                 Serial.print("E:QTI寻迹模式");
  400.                 while(1){
  401.                 MoveMiddleQti();
  402.                 char ch = Serial.read();
  403.                 if(ch=='a') break;
  404.                 }
  405.         break;
  406.         case 'f':
  407.                 Serial.print("F:智能避障模式");
  408.                 while(1){
  409.                 SetDistance();
  410.                 char ch = Serial.read();
  411.                 if(ch=='a') break;
  412.                 }
  413.         break;
  414.         default:
  415.                 Stop();
  416.                 break;
  417.             }
  418.         }
  419. }
  420. //主函数
  421. int main(void){
  422.         init();
  423.         setup();
  424.         InitAngle();
  425. while(1){
  426.          IsGasSenser();
  427.      IsFlameSenser();
  428.      loop();
  429.        }
  430. }
复制代码


全部资料51hei下载地址:
Test_Robot.rar (153.69 KB, 下载次数: 13)


评分

参与人数 1黑币 +100 收起 理由
admin + 100 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:87193 发表于 2017-12-19 09:27 | 只看该作者
多谢分享!
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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