safwan092

Untitled

May 2nd, 2017
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.93 KB | None | 0 0
  1. /*
  2. * Copyright (c) 2015,小二极客科技有限公司
  3. * All rights reserved.
  4. *
  5. * 文件名称:wifi-robots
  6. * 文件标识:
  7. * 摘    要:wifi机器人智能小车控制
  8. *
  9. * 当前版本:1.0
  10. * 作    者:小R团队
  11. * 完成日期:2015年4月1日
  12. */
  13. #include <Servo.h>
  14. //#include <MsTimer2.h>
  15. #include <EEPROM.h>
  16.  
  17. int ledpin = 13;//设置系统启动指示灯
  18. int ENA = 5;//L298使能A
  19. int ENB = 6;//L298使能B
  20. int INPUT2 = 7;//电机接口1
  21. int INPUT1 = 8;//电机接口2
  22. int INPUT3 = 12;//电机接口3
  23. int INPUT4 = 13;//电机接口4
  24.  
  25. int num;//定义电机标志位
  26. int Echo = A0;  // 定义超音波信号接收脚位  
  27. int Trig = A1;  // 定义超音波信号发射脚位
  28. int Input_Detect_LEFT = A2;    //定义小车左侧红外
  29. int Input_Detect_RIGHT = A3;  //定义小车右侧红外
  30. int Input_Detect = A4;//定义小车前方红外
  31. int Carled = 2;//定义小车车灯接口
  32. int Cruising_Flag = 0;
  33. int Pre_Cruising_Flag = 0 ;
  34. int Left_Speed_Hold = 200;//定义左侧速度变量
  35. int Right_Speed_Hold = 200;//定义右侧速度变量
  36.  
  37. #define MOTOR_GO_FORWARD  {digitalWrite(INPUT1,LOW);digitalWrite(INPUT2,HIGH);digitalWrite(INPUT3,LOW);digitalWrite(INPUT4,HIGH);}    //车体前进                               
  38. #define MOTOR_GO_BACK     {digitalWrite(INPUT1,HIGH);digitalWrite(INPUT2,LOW);digitalWrite(INPUT3,HIGH);digitalWrite(INPUT4,LOW);}    //车体前进
  39. #define MOTOR_GO_RIGHT    {digitalWrite(INPUT1,HIGH);digitalWrite(INPUT2,LOW);digitalWrite(INPUT3,LOW);digitalWrite(INPUT4,HIGH);}    //车体前进
  40. #define MOTOR_GO_LEFT     {digitalWrite(INPUT1,LOW);digitalWrite(INPUT2,HIGH);digitalWrite(INPUT3,HIGH);digitalWrite(INPUT4,LOW);}    //车体前进
  41. #define MOTOR_GO_STOP     {digitalWrite(INPUT1,LOW);digitalWrite(INPUT2,LOW);digitalWrite(INPUT3,LOW);digitalWrite(INPUT4,LOW);}    //车体前进
  42.  
  43.  
  44. void forward(int num)
  45. {
  46.     switch(num)
  47.     {
  48.         case 1:MOTOR_GO_FORWARD;return;
  49.         case 2:MOTOR_GO_FORWARD;return;
  50.         case 3:MOTOR_GO_BACK;return;
  51.         case 4:MOTOR_GO_BACK;return;
  52.         case 5:MOTOR_GO_LEFT;return;
  53.         case 6:MOTOR_GO_LEFT;return;
  54.         case 7:MOTOR_GO_RIGHT;return;
  55.         case 8:MOTOR_GO_RIGHT;return;
  56.         default:return;    
  57.     }
  58. }
  59.  
  60. void back(int num)
  61. {
  62.         switch(num)
  63.     {
  64.         case 1:MOTOR_GO_BACK;return;
  65.         case 2:MOTOR_GO_BACK;return;
  66.         case 3:MOTOR_GO_FORWARD;return;
  67.         case 4:MOTOR_GO_FORWARD;return;
  68.         case 5:MOTOR_GO_RIGHT;return;
  69.         case 6:MOTOR_GO_RIGHT;return;
  70.         case 7:MOTOR_GO_LEFT;return;
  71.         case 8:MOTOR_GO_LEFT;return;
  72.         default:return;    
  73.     }
  74. }
  75. void left(int num)
  76. {
  77.         switch(num)
  78.     {
  79.         case 1:MOTOR_GO_LEFT;return;
  80.         case 2:MOTOR_GO_RIGHT;return;
  81.         case 3:MOTOR_GO_LEFT;return;
  82.         case 4:MOTOR_GO_RIGHT;return;
  83.         case 5:MOTOR_GO_FORWARD;return;
  84.         case 6:MOTOR_GO_BACK;return;
  85.         case 7:MOTOR_GO_FORWARD;return;
  86.         case 8:MOTOR_GO_BACK;return;
  87.         default:return;
  88.     }
  89. }
  90. void right(int num)
  91. {
  92.         switch(num)
  93.     {
  94.         case 1:MOTOR_GO_RIGHT;return;
  95.         case 2:MOTOR_GO_LEFT;return;
  96.         case 3:MOTOR_GO_RIGHT;return;
  97.         case 4:MOTOR_GO_LEFT;return;
  98.         case 5:MOTOR_GO_BACK;return;
  99.         case 6:MOTOR_GO_FORWARD;return;
  100.         case 7:MOTOR_GO_BACK;return;
  101.         case 8:MOTOR_GO_FORWARD;return;
  102.         default:return;
  103.     }
  104. }
  105.  
  106. int Left_Speed[11]={90,106,122,138,154,170,186,203,218,234,255};//左侧速度档位值
  107. int Right_Speed[11]={90,106,122,138,154,170,186,203,218,234,255};//右侧速度档位值
  108.  
  109. Servo servo1;// 创建舵机#1号
  110. Servo servo2;// 创建舵机#2号
  111.  
  112. byte angle1=70;//舵机#1初始值
  113. byte angle2=60;//舵机#2初始值
  114.  
  115. int buffer[3];  //串口接收数据缓存
  116. int rec_flag;   //串口接收标志位
  117. int serial_data;
  118. int Uartcount;
  119. int IR_R;
  120. int IR_L;
  121. int IR;
  122. unsigned long Pretime;
  123. unsigned long Nowtime;
  124. unsigned long Costtime;
  125. float Ldistance;
  126.  
  127. void Open_Light()//开大灯
  128.     {      
  129.       digitalWrite(Carled,HIGH);   //拉低电平,正极接电源,负极接Io口
  130.       delay(1000);            
  131.     }
  132. void Close_Light()//关大灯
  133.     {  
  134.       digitalWrite(Carled, LOW);   //拉低电平,正极接电源,负极接Io口
  135.       delay(1000);            
  136.     }
  137.    
  138. void  Avoiding()//红外避障函数
  139.     {  
  140.       IR = digitalRead(Input_Detect);
  141.        if((IR == HIGH))
  142.        {
  143.           forward(num);//直行
  144.           return;            
  145.        }
  146.        if((IR == LOW))
  147.        {
  148.             MOTOR_GO_STOP;//停止
  149.             return;
  150.        }
  151.     }
  152.            
  153.    
  154.  
  155. void FollowLine()   // 巡线模式
  156.     {  
  157.       IR_L = digitalRead(Input_Detect_LEFT);//读取左边传感器数值
  158.       IR_R = digitalRead(Input_Detect_RIGHT);//读取右边传感器数值
  159.      
  160.       if((IR_L == LOW) && (IR_R == LOW))//两边同时探测到障碍物
  161.       {
  162.         forward(num);//直行
  163.         return;          
  164.        
  165.       }
  166.       if((IR_L == LOW) && (IR_R == HIGH))//右侧遇到障碍  
  167.       {
  168.         left(num);//左转
  169.         return;
  170.        
  171.       }
  172.       if((IR_L == HIGH) &&( IR_R == LOW))//左侧遇到障碍
  173.       {
  174.         right(num);//右转
  175.         return;
  176.        
  177.       }
  178.       if((IR_L == HIGH) && (IR_R == HIGH))//左右都检测到,就如视频中的那样遇到一道横的胶带
  179.       {
  180.         MOTOR_GO_STOP;//停止
  181.         return;
  182.        }
  183.     }    
  184.  
  185. char Get_Distence()//测出距离
  186.  {  
  187.       digitalWrite(Trig, LOW);   // 让超声波发射低电压2μs  
  188.       delayMicroseconds(2);  
  189.       digitalWrite(Trig, HIGH);  // 让超声波发射高电压10μs,这里至少是10μs  
  190.       delayMicroseconds(10);  
  191.       digitalWrite(Trig, LOW);    // 维持超声波发射低电压  
  192.       Ldistance = pulseIn(Echo, HIGH);  // 读差相差时间  
  193.       Ldistance= Ldistance/5.8/10;      // 将时间转为距离距离(单位:公分)    
  194.     //  Serial.println(Ldistance);      //显示距离  
  195.       return Ldistance;
  196.      
  197.   }    
  198.  
  199. void Avoid_wave()//超声波避障函数
  200. {
  201.   Get_Distence();
  202.   if(Ldistance < 15)
  203.       {
  204.           MOTOR_GO_STOP;
  205.       }
  206.       else
  207.       {
  208.           forward(num);
  209.       }
  210. }
  211.  
  212. void Avoid_wave_auto()
  213. {
  214.   Get_Distence();
  215.   if(Ldistance < 15)
  216.   {
  217.     back(num);
  218.     delay(300);
  219.     MOTOR_GO_STOP;
  220.   }
  221. }
  222.  
  223. void Send_Distance()//超声波距离PC端显示
  224. {
  225.   int dis= Get_Distence();
  226.   Serial.write(0xff);
  227.   Serial.write(0x03);
  228.   Serial.write(0x00);
  229.   Serial.write(dis);
  230.   Serial.write(0xff);
  231.   delay(1000);
  232. }
  233. /*
  234. *********************************************************************************************************
  235. ** 函数名称 :Delayed()
  236. ** 函数功能 :延时程序
  237. ** 入口参数 :无
  238. ** 出口参数 :无
  239. *********************************************************************************************************
  240. */
  241. void  Delayed()    //延迟40秒等WIFI模块启动完毕
  242. {
  243.     int i;
  244.     for(i=0;i<20;i++)
  245.     {
  246.         digitalWrite(ledpin,LOW);
  247.         delay(1000);
  248.         digitalWrite(ledpin,HIGH);
  249.         delay(1000);
  250.     }
  251. }
  252.  
  253. /*
  254. *********************************************************************************************************
  255. ** 函数名称 :setup().Init_Steer()
  256. ** 函数功能 :系统初始化(串口、电机、舵机、指示灯初始化)。
  257. ** 入口参数 :无
  258. ** 出口参数 :无
  259. *********************************************************************************************************
  260. */
  261. void Init_Steer()//舵机初始化(角度为上次保存数值)
  262. {
  263.     servo1.write(angle1);
  264.     servo2.write(angle2);
  265.     angle1 = EEPROM.read(0x01);//读取寄存器0x01里面的值
  266.     angle2 = EEPROM.read(0x02);//读取寄存器0x02里面的值
  267.     if(angle1 == 255 && angle2 == 255)
  268.     {
  269.         EEPROM.write(0x01,70);//把初始角度存入地址0x01里面
  270.         EEPROM.write(0x02,60);//把初始角度存入地址0x02里面
  271.         return;
  272.     }
  273.     servo1.write(angle1);//把保存角度赋值给舵机1
  274.     servo2.write(angle2);//把保存角度赋值给舵机2
  275.     num = EEPROM.read(0x10);//读取寄存器0x10里面的值
  276.     if(num==0xff)EEPROM.write(0x10,1);
  277. }
  278.  
  279. void setup()
  280. {
  281.     pinMode(ledpin,OUTPUT);
  282.     pinMode(ENA,OUTPUT);
  283.     pinMode(ENB,OUTPUT);
  284.     pinMode(INPUT1,OUTPUT);
  285.     pinMode(INPUT2,OUTPUT);
  286.     pinMode(INPUT3,OUTPUT);
  287.     pinMode(INPUT4,OUTPUT);
  288.     pinMode(Input_Detect_LEFT,INPUT);
  289.     pinMode(Input_Detect_RIGHT,INPUT);
  290.     pinMode(Carled, OUTPUT);
  291.     pinMode(Input_Detect,INPUT);
  292.     pinMode(Echo,INPUT);
  293.     pinMode(Trig,OUTPUT);
  294.  
  295.     Delayed();//延迟40秒等WIFI模块启动完毕
  296.     analogWrite(ENB,Left_Speed_Hold);//给L298使能端B赋值
  297.     analogWrite(ENA,Right_Speed_Hold);//给L298使能端A赋值
  298.     digitalWrite(ledpin,LOW);
  299.     servo1.attach(9);//定义舵机7控制口
  300.     servo2.attach(10);//定义舵机8控制口
  301.     Serial.begin(9600);//串口波特率设置为9600 bps
  302.     Init_Steer();
  303. }
  304. /*
  305. *********************************************************************************************************
  306. ** 函数名称 :loop()
  307. ** 函数功能 :主函数
  308. ** 入口参数 :无
  309. ** 出口参数 :无
  310. *********************************************************************************************************
  311. */
  312. void Cruising_Mod()//模式功能切换函数
  313.     {
  314.        
  315.      if(Pre_Cruising_Flag != Cruising_Flag)
  316.      {
  317.          if(Pre_Cruising_Flag != 0)
  318.          {
  319.              MOTOR_GO_STOP;
  320.          }
  321.  
  322.          Pre_Cruising_Flag =  Cruising_Flag;
  323.      } 
  324.     switch(Cruising_Flag)
  325.       {
  326.        //case 1:Send_Distance();//超声波测距
  327.        case 2:FollowLine(); return;//巡线模式
  328.        case 3:Avoiding(); return;//避障模式
  329.        case 4:Avoid_wave();return;//超声波避障模式
  330.            case 5:Send_Distance();//超声波距离PC端显示
  331.        default:return;
  332.       }
  333.              
  334. }
  335.  
  336. void loop()
  337.   {  
  338.     while(1)
  339.     {
  340.         Get_uartdata();
  341.         UartTimeoutCheck();
  342.         Cruising_Mod();
  343.      }  
  344.   }
  345.  
  346.  
  347.  
  348. /*
  349. *********************************************************************************************************
  350. ** 函数名称 :Communication_Decode()
  351. ** 函数功能 :串口命令解码
  352. ** 入口参数 :无
  353. ** 出口参数 :无
  354. *********************************************************************************************************
  355. */
  356. void Communication_Decode()
  357. {  
  358.     if(buffer[0]==0x00)
  359.     {
  360.         switch(buffer[1])   //电机命令
  361.         {
  362.             case 0x01:MOTOR_GO_FORWARD; return;
  363.         case 0x02:MOTOR_GO_BACK;    return;
  364.         case 0x03:MOTOR_GO_LEFT;    return;
  365.             case 0x04:MOTOR_GO_RIGHT;   return;
  366.         case 0x00:MOTOR_GO_STOP;    return;
  367.            default: return;
  368.         }  
  369.     }
  370.    else if(buffer[0]==0x01)//舵机命令
  371.     {
  372.         switch(buffer[1])
  373.         {
  374.             case 0x07:if(buffer[2]>160)return;angle1 = buffer[2];servo1.write(angle1);return;
  375.             case 0x08:if(buffer[2]>160)return;angle2 = buffer[2];servo2.write(angle2);return;
  376.             default:return;
  377.         }
  378.     }
  379.    
  380.    else if(buffer[0]==0x02)//调速
  381.     {
  382.                 int i,j;
  383.         if(buffer[2]>10)return;
  384.              
  385.         if(buffer[1]==0x01)//左侧调档
  386.         {
  387.                         i=buffer[2];
  388.             Left_Speed_Hold=Left_Speed[i] ;
  389.                         analogWrite(ENB,Left_Speed_Hold);
  390.         }
  391.                 if(buffer[1]==0x02)//右侧调档
  392.                 {
  393.                         j=buffer[2];
  394.                         Right_Speed_Hold=Right_Speed[j] ;
  395.                         analogWrite(ENA,Right_Speed_Hold);
  396.                 }else return;
  397.         }
  398.     else if(buffer[0]==0x33)//读取舵机角度并赋值
  399.     {
  400.          Init_Steer();return;
  401.         }
  402.     else if(buffer[0]==0x32)//保存命令
  403.     {
  404.         EEPROM.write(0x01,angle1);
  405.         EEPROM.write(0x02,angle2);
  406.         return;
  407.     }
  408.         else if(buffer[0]==0x13)//模式切换开关
  409.     {
  410.         switch(buffer[1])
  411.         {
  412.                   case 0x02: Cruising_Flag = 2; return;//巡线
  413.           case 0x03: Cruising_Flag = 3; return;//避障
  414.           case 0x04: Cruising_Flag = 4; return;//雷达避障
  415.                   case 0x05: Cruising_Flag = 5; return;//超声波距离PC端显示
  416.                   case 0x00: Cruising_Flag = 0; return;//正常模式
  417.           default:Cruising_Flag = 0; return;//正常模式
  418.         }
  419.     }
  420.         else if(buffer[0]==0x05)
  421.     {
  422.         switch(buffer[1])   //
  423.         {
  424.             case 0x00:Open_Light(); return;
  425.         case 0x02:Close_Light(); return;
  426.             default: return;
  427.         }  
  428.     }
  429.        else if(buffer[0]==0x40)//存储电机标志
  430.     {
  431.        num=buffer[1];
  432.        EEPROM.write(0x10,num);
  433.     }
  434.    
  435.    
  436. }
  437. /*
  438. *********************************************************************************************************
  439. ** 函数名称 :Get_uartdata()
  440. ** 函数功能 :读取串口命令
  441. ** 入口参数 :无
  442. ** 出口参数 :无
  443. *********************************************************************************************************
  444. */
  445. void Get_uartdata(void)
  446. {
  447.     static int i;
  448.    
  449.     if (Serial.available() > 0) //判断串口缓冲器是否有数据装入
  450.     {
  451.         serial_data = Serial.read();//读取串口
  452.         if(rec_flag==0)
  453.         {
  454.             if(serial_data==0xff)
  455.             {
  456.                 rec_flag = 1;
  457.                 i = 0;
  458.                Costtime = 0;
  459.             }
  460.         }
  461.         else
  462.         {
  463.             if(serial_data==0xff)
  464.             {
  465.                 rec_flag = 0;
  466.                 if(i==3)
  467.                 {
  468.                     Communication_Decode();
  469.                 }
  470.                 i = 0;
  471.             }
  472.             else
  473.             {
  474.                 buffer[i]=serial_data;
  475.                 i++;
  476.             }
  477.         }
  478.     }
  479. }
  480. /*
  481. *********************************************************************************************************
  482. ** 函数名称 :UartTimeoutCheck()
  483. ** 函数功能 :串口超时检测
  484. ** 入口参数 :无
  485. ** 出口参数 :无
  486. *********************************************************************************************************
  487. */
  488. void UartTimeoutCheck(void)
  489. {
  490.     if(rec_flag == 1)
  491.     {
  492.        Costtime++;  
  493.       if(Costtime == 100000)
  494.       {
  495.            rec_flag = 0;
  496.       }
  497.     }
  498. }
Advertisement
Add Comment
Please, Sign In to add comment