找回密码
 立即注册

QQ登录

只需一步,快速开始

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

IAR for STM32平衡车源程序

[复制链接]
跳转到指定楼层
楼主
ID:362707 发表于 2018-7-1 21:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
IARfor STM32平衡车源程序
  1. /****************************************************************************/
  2. //车轮直径 6.7cm   周长21cm  电机转一圈接收到1496个脉冲
  3. //则一个脉冲对应0.14mm
  4. /****************************************************************************/
  5. #include "bsp.h"



  6. extern uint16_t Speed_Left,Speed_Right;
  7. uint16_t a;
  8. uint16_t b=0;

  9. float situ_pitch;
  10. int16_t situ_gyroy;//全局变量以便直立PID使用


  11. //串口1发送1个字符
  12. //c:要发送的字符
  13. void usart1_send_char(u8 c)
  14. {           
  15.         while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET); //循环发送,直到发送完毕   
  16.         USART_SendData(USART1,c);  
  17. }
  18. //传送数据给匿名四轴上位机软件(V2.6版本)
  19. //fun:功能字. 0XA0~0XAF
  20. //data:数据缓存区,最多28字节!!
  21. //len:data区有效数据个数
  22. void usart1_niming_report(u8 fun,u8*data,u8 len)
  23. {
  24.         u8 send_buf[32];
  25.         u8 i;
  26.         if(len>28)return;        //最多28字节数据
  27.         send_buf[len+3]=0;        //校验数置零
  28.         send_buf[0]=0X88;        //帧头
  29.         send_buf[1]=fun;        //功能字
  30.         send_buf[2]=len;        //数据长度
  31.         for(i=0;i<len;i++)send_buf[3+i]=data[i];                        //复制数据
  32.         for(i=0;i<len+3;i++)send_buf[len+3]+=send_buf[i];        //计算校验和        
  33.         for(i=0;i<len+4;i++)usart1_send_char(send_buf[i]);        //发送数据到串口1
  34. }
  35. //发送加速度传感器数据和陀螺仪数据
  36. //aacx,aacy,aacz:x,y,z三个方向上面的加速度值
  37. //gyrox,gyroy,gyroz:x,y,z三个方向上面的陀螺仪值
  38. void mpu6050_send_data(short aacx,short aacy,short aacz,short gyrox,short gyroy,short gyroz,short roll,short pitch,short yaw,uint16_t Left,uint16_t Right)
  39. {
  40.         u8 tbuf[22];
  41.         tbuf[0]=(aacx>>8)&0XFF;
  42.         tbuf[1]=aacx&0XFF;
  43.         tbuf[2]=(aacy>>8)&0XFF;
  44.         tbuf[3]=aacy&0XFF;
  45.         tbuf[4]=(aacz>>8)&0XFF;
  46.         tbuf[5]=aacz&0XFF;
  47.         tbuf[6]=(gyrox>>8)&0XFF;
  48.         tbuf[7]=gyrox&0XFF;
  49.         tbuf[8]=(gyroy>>8)&0XFF;
  50.         tbuf[9]=gyroy&0XFF;
  51.         tbuf[10]=(gyroz>>8)&0XFF;
  52.         tbuf[11]=gyroz&0XFF;
  53.         
  54.         tbuf[12]=(roll>>8)&0XFF;
  55.         tbuf[13]=roll&0XFF;
  56.         tbuf[14]=(pitch>>8)&0XFF;
  57.         tbuf[15]=pitch&0XFF;
  58.         tbuf[16]=(yaw>>8)&0XFF;
  59.         tbuf[17]=yaw&0XFF;
  60.         
  61.         tbuf[18]=(Left>>8)&0XFF;
  62.         tbuf[19]=Left&0XFF;
  63.         tbuf[20]=(Right>>8)&0XFF;
  64.         tbuf[21]=Right&0XFF;
  65.         
  66.         usart1_niming_report(0XA1,tbuf,22);//自定义帧,0XA1
  67. }        

  68. //发送左右电机速度
  69. //left right左右电机速度值
  70. void motor_send_speed(uint16_t left,uint16_t right)
  71. {
  72.         u8 tbuf[4];
  73.         tbuf[0]=(left>>8)&0XFF;
  74.         tbuf[1]=left&0XFF;
  75.         tbuf[2]=(right>>8)&0XFF;
  76.         tbuf[3]=right&0XFF;
  77.         usart1_niming_report(0XA1,tbuf,4);//自定义帧,0XA1
  78. }        
  79. //通过串口1上报结算后的姿态数据给电脑
  80. //aacx,aacy,aacz:x,y,z三个方向上面的加速度值
  81. //gyrox,gyroy,gyroz:x,y,z三个方向上面的陀螺仪值
  82. //roll:横滚角.单位0.01度。 -18000 -> 18000 对应 -180.00  ->  180.00度
  83. //pitch:俯仰角.单位 0.01度。-9000 - 9000 对应 -90.00 -> 90.00 度
  84. //yaw:航向角.单位为0.1度 0 -> 3600  对应 0 -> 360.0度
  85. void usart1_report_imu(short aacx,short aacy,short aacz,short gyrox,short gyroy,short gyroz,short roll,short pitch,short yaw)
  86. {
  87.         u8 tbuf[28];
  88.         u8 i;
  89.         for(i=0;i<28;i++)tbuf[i]=0;//清0
  90.         tbuf[0]=(aacx>>8)&0XFF;
  91.         tbuf[1]=aacx&0XFF;
  92.         tbuf[2]=(aacy>>8)&0XFF;
  93.         tbuf[3]=aacy&0XFF;
  94.         tbuf[4]=(aacz>>8)&0XFF;
  95.         tbuf[5]=aacz&0XFF;
  96.         tbuf[6]=(gyrox>>8)&0XFF;
  97.         tbuf[7]=gyrox&0XFF;
  98.         tbuf[8]=(gyroy>>8)&0XFF;
  99.         tbuf[9]=gyroy&0XFF;
  100.         tbuf[10]=(gyroz>>8)&0XFF;
  101.         tbuf[11]=gyroz&0XFF;        
  102.         tbuf[18]=(roll>>8)&0XFF;
  103.         tbuf[19]=roll&0XFF;
  104.         tbuf[20]=(pitch>>8)&0XFF;
  105.         tbuf[21]=pitch&0XFF;
  106.         tbuf[22]=(yaw>>8)&0XFF;
  107.         tbuf[23]=yaw&0XFF;
  108.         
  109.         usart1_niming_report(0XAF,tbuf,28);//飞控显示帧,0XAF
  110. }  




  111. /*********************************************************************************************************************/
  112.          
  113. int main(void)
  114. {        
  115. /******************************************************************************/
  116. //                              变量声明
  117. /******************************************************************************/  
  118.           GPIO_InitTypeDef  GPIOB_InitStructure;
  119.         GPIO_InitTypeDef  GPIOC_InitStructure;
  120.         float pitch,roll,yaw;                 //欧拉角
  121.         short aacx,aacy,aacz;                //加速度传感器原始数据
  122.         short gyrox,gyroy,gyroz;        //陀螺仪原始数据
  123. //        short temp;                                        //温度        
  124.         
  125. /******************************************************************************/
  126. //                              模块初始化
  127. /******************************************************************************/         
  128.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);         //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
  129.         uart_init(115200);         //串口初始化
  130.         delay_init();                //延时初始化
  131.         All_GPIO_Init();
  132.         OLED_Init();                //OLED初始化
  133.         OLED_Clear();                //OLED清屏
  134.         MPU_Init();                //初始化MPU6050
  135.         TIM3_Cap_Init();        //左编码器初始化
  136.         TIM4_Cap_Init();        //右编码器初始化
  137.         Motor_Left_Init(100);    //左电机初始化
  138.         Motor_Right_Init(100);        //右电机初始化
  139.         TIM1_Int_Init(10);      //定时器初始化 定时10ms
  140.         
  141.         
  142.          RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB  |
  143.                                 RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD  |
  144.                                 RCC_APB2Periph_GPIOE, ENABLE);                 //使能A,B,C,D,E端口时钟

  145. /***************************************灯的IO口*****************************************/        
  146.         GPIOC_InitStructure.GPIO_Pin = GPIO_Pin_13;         
  147.          GPIOC_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                  //推挽输出
  148.         GPIOC_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
  149.          GPIO_Init(GPIOC, &GPIOC_InitStructure);          //初始化GPIOC13
  150.         
  151. /**************************************电机的IO口****************************************/        
  152.         GPIOB_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_8|GPIO_Pin_9;         
  153.          GPIOB_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                  //推挽输出
  154.         GPIOB_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
  155.          GPIO_Init(GPIOB, &GPIOB_InitStructure);          //初始化B0 B1 B8 B9        
  156.         
  157.         
  158.         PCout(13)=0;//亮灯        
  159.         PBout(0)=1;
  160.         PBout(1)=0;//A端 右轮        
  161.         PBout(8)=1;
  162.         PBout(9)=0;//B端 左轮
  163. /******************************************************************************/
  164. //                              主程序
  165. /******************************************************************************/
  166.         
  167.         while(mpu_dmp_init());
  168.         while(1)
  169.         {

  170.           if(mpu_dmp_get_data(&pitch,&roll,&yaw)==0)
  171.           {
  172.         //      temp=MPU_Get_Temperature();        //得到温度值
  173.         
  174.               MPU_Get_Accelerometer(&aacx,&aacy,&aacz);        //得到加速度传感器数据

  175.               MPU_Get_Gyroscope(&gyrox,&gyroy,&gyroz);        //得到陀螺仪数据
  176.               situ_pitch=pitch;
  177.               situ_gyroy=gyroy;
  178.              mpu6050_send_data(aacx,aacy,aacz,gyrox,gyroy,gyroz,(int)(roll*100),(int)((pitch+4)*100),(int)(yaw*10),Speed_Left*10,Speed_Right*10);//用自定义帧发送加速度和陀螺仪原始数据
  179.         //      usart1_report_imu(aacx,aacy,aacz,gyrox,gyroy,gyroz,(int)(roll*100),(int)(pitch*100),(int)(yaw*10));
  180.          //     OLED_ShowNum(0,0,(int)(pitch*100),10,16);
  181.             
  182.             
  183.                 Motor_Right_Duty(PID_Stand(situ_pitch,situ_gyroy));
  184.                 Motor_Left_Duty(PID_Stand(situ_pitch,situ_gyroy));
  185.               
  186.           }
  187.         }

  188.         
  189.         
  190.         
  191.         
  192.         
  193.         
  194.         

  195. }




  196.                


  197.         
  198.         
  199. /******************************************************************************/
  200. //                              定时器2中断服务程序
  201. /******************************************************************************/
  202. void TIM1_UP_IRQHandler(void)
  203. {
  204.         if(TIM_GetITStatus(TIM1,TIM_IT_Update)!=RESET)
  205.         {         
  206.                 TIM_ClearFlag(TIM1,TIM_IT_Update);//清中断标志位
  207.                 Speed_Left = Read_TIM3_Encoder()*14;//单位:cm/s * 10
  208.                 Speed_Right = Read_TIM4_Encoder()*14;//单位:cm/s * 10
  209.                

  210.         //        Motor_Right_Duty(PID_RIGHT(50)/14);
  211.         //        Motor_Left_Duty(PID_LEFT(0)/14);
  212.         //        motor_send_speed(Speed_Left,Speed_Right);
  213.                 TIM4->CNT=0;
  214.                 TIM3->CNT=0;
  215.                
  216.                
  217.         }
  218. }        
复制代码


IAR_STM32_平衡车.rar

9.83 MB, 下载次数: 38, 下载积分: 黑币 -5

评分

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

查看全部评分

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

使用道具 举报

沙发
ID:370117 发表于 2018-7-15 18:14 | 只看该作者
很好,很受用
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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