找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5810|回复: 2
收起左侧

STM32智能小车超声波跟随的程序源码

[复制链接]
ID:257401 发表于 2018-7-16 09:29 | 显示全部楼层 |阅读模式
  1. #include "stm32f10x.h"
  2. #include "delay.h"
  3. #include "motor.h"
  4. #include "keysacn.h"
  5. #include "IRSEARCH.h"
  6. #include "IRAvoid.h"
  7. #include "usart.h"
  8. #include "UltrasonicWave.h"
  9. #include "timer.h"
  10. #include "Server.h"



  11. char ctrl_comm1 = COMM_STOP;//控制指令
  12. char ctrl_comm1_last = COMM_STOP;//上一次的指令
  13. int Q_temp,Q_temp_last,L_temp,R_temp;

  14. // 超声波转头函数
  15. int front_detection()
  16. {

  17. //        ZYSTM32_brake(0);
  18.         SetJointAngle(90);
  19.         delay_ms(100);
  20.         return UltrasonicWave_StartMeasure();
  21. }
  22. int left_detection()
  23. {

  24. //        ZYSTM32_brake(0);
  25.         SetJointAngle(175);
  26.         delay_ms(300);
  27.         return UltrasonicWave_StartMeasure();
  28. }
  29. int right_detection()
  30. {

  31. //        ZYSTM32_brake(0);
  32.         SetJointAngle(5);
  33.         delay_ms(300);
  34.         return UltrasonicWave_StartMeasure();
  35. }


  36. void BarrierProc(void)
  37. {
  38.    delay_ms(10);
  39.          Q_temp_last = Q_temp;
  40.          Q_temp = front_detection();
  41.          printf("测到的距离值为:%d\n",Q_temp);
  42.       
  43.          if( Q_temp > 45)
  44.         {
  45.                 ctrl_comm1 = COMM_STOP;//防震荡
  46.                 return;
  47.         }
  48.       
  49.         if((Q_temp_last > Q_temp)&&(Q_temp<=15)) ctrl_comm1 = COMM_DOWN;
  50.         //if(Q_temp<=15) ctrl_comm1 = COMM_DOWN;
  51.         if(Q_temp >=30) ctrl_comm1 = COMM_UP;
  52. }
  53. //extern int U_temp;
  54. /*
  55. void ZYSTM32_run(int speed,int time);       //前进函数
  56. void ZYSTM32_brake(int time);               //刹车函数
  57. void ZYSTM32_Left(int speed,int time);      //左转函数
  58. void ZYSTM32_Spin_Left(int speed,int time); //左旋转函数
  59. void ZYSTM32_Right(int speed,int time);     //右转函数
  60. void ZYSTM32_Spin_Right(int speed,int time);//右旋转函数
  61. void ZYSTM32_back(int speed,int time);      //后退函数
  62. */

  63. int main(void)
  64. {      

  65.          delay_init();
  66.          KEY_Init();
  67.          IRSearchInit();
  68.          IRAvoidInit();
  69.    Timerx_Init(5000,7199);                //10Khz的计数频率,计数到5000为500ms
  70.          UltrasonicWave_Configuration();        //对超声波模块初始化
  71.          uart_init(115200);
  72.          TIM4_PWM_Init(7199,0);                 //初始化PWM
  73.          TIM5_PWM_Init(9999,143);               //不分频,PWM频率=72*10^6/(9999+1)/(143+1)=50Hz         
  74.          ZYSTM32_brake(500);
  75.          keysacn();               
  76.         while(1)
  77.         {         
  78.     BarrierProc();
  79.                
  80.         //        Q_temp = front_detection();
  81.         //  printf("测到的距离值为:%d\n",Q_temp);
  82.         //Q_temp_last = Q_temp;
  83.                 //Q_temp = front_detection();
  84.                         if(ctrl_comm1_last != ctrl_comm1)
  85.                         {
  86.                                 ctrl_comm1_last = ctrl_comm1;
  87.                                 switch(ctrl_comm1)
  88.                                 {
  89.                                         case COMM_UP:   ZYSTM32_run(50,10);break;
  90.                                         case COMM_DOWN:  ZYSTM32_back(50,500);        break;
  91.                                         case COMM_LEFT:  ZYSTM32_Left(50,700);break;
  92.                                         case COMM_RIGHT:ZYSTM32_Right(50,700);break;
  93.                                         case COMM_STOP:  ZYSTM32_brake(500);break;
  94.                                         default : break;
  95.                                 }
  96.                         }
  97.                
  98.                
  99.                 }
  100. }


复制代码

全部资料51hei下载地址:
ZYSTM32-A0 智能小车超声波避障实验(有舵机).rar (311.8 KB, 下载次数: 89)
回复

使用道具 举报

ID:370913 发表于 2018-12-9 20:58 | 显示全部楼层
新手求教 1.u_temp = UltrasonicWave_Distance*10;u_temp难道不是距离吗?为什么不是直接赋值UltrasonicWave_Distance,还有乘以10
2.         Q_temp_last = Q_temp;
         Q_temp = front_detection();没明白
3.为啥同样是转头SetJointAngle()里的值还不一样
4。SetJointAngle();这个函数没看懂
回复

使用道具 举报

ID:434764 发表于 2018-12-24 20:02 | 显示全部楼层
参考学习下
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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