找回密码
 立即注册

QQ登录

只需一步,快速开始

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

stm32f103单片机自制飞控代码分享

[复制链接]
跳转到指定楼层
楼主
程序可以支持MPU6050数据读取、SPL06气压计读取、SBUS遥控数据读取、最多可以控制8路电机或者电调。写了PID算法,可以让无人机在控制保持稳定。
飞机是自制的,硬件和机械也是自己做的,
是本人设计一部分,硬件电路会在整理之后发出来。新人第一次发帖,还望各位大佬轻喷。
制作出来的实物图如下:


单片机源程序如下:
  1. #include "sys.h"

  2. u8 report=0;                        //默认开启上报
  3. int motor1,motor2,servo1,servo2;
  4. float Setpitch=0,Setroll=0;
  5. float pitch,roll,yaw;                 //欧拉角
  6. short aacx,aacy,aacz;                //加速度传感器原始数据
  7. short gyrox,gyroy,gyroz;        //陀螺仪原始数据
  8. short temp;                                        //温度       
  9. int main(void)
  10. {       
  11.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);         //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
  12.         Usart_init();                 //串口初始化
  13.         delay_init();          //延时初始化 delay_ms(1000);
  14.         BEEP_GPIO_Init();
  15.         Motor_PWM_Init(20000-1, 72-1);
  16.         Servo_PWM_Init(20000-1, 72-1);
  17.         TIM2_Int_Init(100-1,720-1);
  18.         Motor_Unlock(1900,1100);
  19.         BEEP_GPIO_High();
  20.         FrSky_Uart2_Init(100000);
  21.         Adc_Init();
  22.         SPI1_Init();
  23.         SPL06_Init();
  24.         MPU_IIC_Init(); //MPU使用的IIC初始化
  25.         while(MPU_Init())
  26.         {
  27.                 printf("mpu error\n");
  28.                 delay_ms(200);
  29.         };                                        //初始化MPU6050
  30.        
  31.         while(mpu_dmp_init())
  32.         {
  33.                 printf("mpu dmp error\n");
  34.                 delay_ms(200);
  35.         }
  36.   PID_Init();
  37.         BEEP_GPIO_Low();

  38.         while(1)
  39.         {

  40.                 int i;
  41.                 static float Baro_Buf[20];
  42.                 Baro.Org_Alt = SPL06_Get_Altitude();
  43.                 //气压原始数据缓存
  44.     for(i=19;i>0;i--)
  45.     {
  46.         Baro_Buf[i]=Baro_Buf[i-1];
  47.     }
  48.     Baro_Buf[0]=Baro.Org_Alt;

  49. //                printf("%f\n",Baro.Org_Alt );
  50. //                delay_ms(100);
  51. /********测量电池电压********/
  52.                
  53.                 Get_Bat_Voltage();
  54.                 if(Bat_Voltage<11)
  55.                 {
  56.                         BEEP_GPIO_High();
  57.                 }
  58.                 else
  59.                 {
  60.                         BEEP_GPIO_Low();
  61.                 }
  62. //////                printf("电池电压为 %f \n",Bat_Voltage);
  63. ///**********陀螺仪数据输出***************/               
  64. if(mpu_dmp_get_data(&pitch,&roll,&yaw)==0)
  65. {
  66. //        printf("%f,%f,%f\r\n",pitch,roll,yaw);
  67.         temp=MPU_Get_Temperature();        //得到温度值
  68.         MPU_Get_Accelerometer(&aacx,&aacy,&aacz);        //得到加速度传感器数据
  69.         MPU_Get_Gyroscope(&gyrox,&gyroy,&gyroz);        //得到陀螺仪数据
  70.         if(report)mpu6050_send_data(aacx,aacy,aacz,gyrox,gyroy,gyroz);//用自定义帧发送加速度和陀螺仪原始数据
  71.         if(report)usart1_report_imu(aacx,aacy,aacz,gyrox,gyroy,gyroz,(int)(roll*100),(int)(pitch*100),(int)(yaw*10));

  72. }
  73. /********     根据无线串口输入并调整pid   ********/
  74.         PID_Update();       
  75.                
  76. /********更新遥控器数据********/               
  77.                 update_channels();
  78.                
  79. //                printf( "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n",
  80. //                                channels[0],
  81. //                                channels[1],
  82. //                                channels[2],
  83. //                                channels[3],
  84. //                                channels[4],
  85. //                                channels[5],
  86. //                                channels[6],
  87. //                                channels[7],
  88. //                                channels[8],
  89. //                                channels[9],
  90. //                                channels[10],
  91. //                                channels[11]
  92. //                         );                                                                                       
  93. /********遥控器控制电机********/                       
  94.                 motor1=motor2=channels[0]/2+1000;       
  95.                 //printf("%d\r\n",channels[0]);
  96.                 Motor_PWM_Set(motor1,motor2);
  97. /********遥控器控制舵机********/               
  98. //                servo1=channels[1];
  99. //                servo2=channels[2];
  100. //    Servo_PWM_Set(servo1,servo2);

  101. Setpitch=(channels[1]-988)/30;
  102. if(channels[2]>1003 | channels[2]<1000){
  103. Setroll =(channels[2]-1002)/30;
  104. }

  105. /********测试舵机********/       
  106. //        Servo_PWM_Set(1050,950);

  107. //printf("%f,%f,%f,%f\n",Setroll,roll,Setpitch,pitch);
  108.         }        
  109. }

复制代码

Keil代码下载:
程序.7z (392.28 KB, 下载次数: 24)

评分

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

查看全部评分

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

使用道具 举报

沙发
ID:336378 发表于 2023-8-4 12:19 | 只看该作者
这样子能飞起来吗??。
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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