找回密码
 立即注册

QQ登录

只需一步,快速开始

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

秋阳电子四轴翼飞行器源码

[复制链接]
跳转到指定楼层
楼主
ID:290668 发表于 2018-3-12 11:03 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
基于STM32F103C8T6单片机的四轴翼飞行器源码,2.4G通讯,MPU6050等等资料

单片机源程序如下:
  1. /***********************************************

  2. 标题: mian.c
  3. 作者: 秋阳电子
  4. 版本:v1.0
  5. MDK-ARM 版本: v4.12
  6. ST 库版本:v3.50  
  7. 功能: 四轴飞控
  8. 说明:
  9. *************************************************/
  10. #include "stm32f10x.h"
  11. #include "iic.h"
  12. #include "timer.h"
  13. #include "usart.h"
  14. #include "mpu6050.h"
  15. #include "nrf24l01.h"
  16. #include "motor.h"
  17. #include "imu.h"
  18. #include "pid.h"
  19. #include <stdio.h>

  20. #define  COM_MODEL           0x00
  21. #define  NORMAL_MODEL  0x01
  22. #define  BUT  (GPIOB->IDR  & GPIO_Pin_12)
  23. #define  LED_BLINK  GPIOA->ODR ^= (1 << 1)
  24. #define  TIME_OUT  1200

  25. u8 model = NORMAL_MODEL;
  26. u8 flg_get_senor_data;
  27. u8 flg_recive_data;
  28. u8 out[35] = {0x5f, 0x60, 0};
  29. u8 offset_data[5];
  30. u8 rx_data_buf[16];
  31. s16 gx, gy, gz, ax ,ay, az, temperature;
  32. float set_pitch, set_roll, yaw, yaw1, pitch, roll, left_yaw, right_yaw;
  33. float f_gx, f_gy, f_gz;
  34. s16 temp;
  35. u16 time_out = 0;
  36. u8 send_fre = 0;
  37. volatile u8 thr = 0, rud = 0, ele = 0, ail = 0;
  38. volatile float yaw_offset = 0, pitch_offset = 0, roll_offset = 0;
  39. s16 gx_offset = 0, gy_offset = 0, gz_offset = 0;
  40. s32 gx_sum = 0, gy_sum = 0, gz_sum = 0;
  41. /*************************************************

  42. 名称:void delay(u32 count)
  43. 功能:延时
  44. 输入参数:延时数
  45. 输出参数:无
  46. 返回值:  无
  47. **************************************************/
  48. void delay(u32 count)
  49. {
  50.   for(; count != 0; count--);
  51. }
  52. /*************************************************

  53. 名称:void led_blink()
  54. 功能:LED闪烁
  55. 输入参数:无
  56. 输出参数:无
  57. 返回值:  无
  58. **************************************************/
  59. void led_blink()
  60. {
  61.   u8 j;
  62.   for(j = 0; j < 30; j++)
  63.   {
  64.     LED_BLINK;
  65.         delay(0x80000);
  66.   }
  67. }
  68. /*************************************************

  69. 名称:receive_control_data()
  70. 功能:接收遥控器数据
  71. 输入参数:无
  72. 输出参数:油门控制等信息
  73. 返回值:无
  74. **************************************************/
  75. void receive_control_data()
  76. {
  77.   u16 check = 0;
  78.   u8 i;
  79.   if(nrf24l01_rx_data(&rx_data_buf[0]))   
  80.   {
  81.      for (i = 0; i < 15; ++i)
  82.          {
  83.            check += rx_data_buf[i];
  84.          }
  85.          if((check & 0xff) == rx_data_buf[15])
  86.          {
  87.        LED_BLINK;
  88.        time_out = 0;
  89.        thr = rx_data_buf[0];
  90.        rud = rx_data_buf[1];
  91.            ele = rx_data_buf[2];
  92.            ail = rx_data_buf[3];

  93.            yaw_offset = (rx_data_buf[4] - 0x40) * 1.0;
  94.            pitch_offset = (rx_data_buf[5] - 0x40) * 0.25;
  95.            roll_offset = (rx_data_buf[6] - 0x40) * 0.25;
  96.          }
  97.   }
  98.   else  //接收命令超时
  99.   {
  100.      if(time_out++ > TIME_OUT)
  101.      {
  102.            time_out = TIME_OUT;
  103.            thr = 0;
  104.            rud = 0;
  105.            ele = 0;
  106.            ail = 0;
  107.      }
  108.   }
  109. }
  110. /*************************************************

  111. 名称:send_sensor_data()
  112. 功能:通过串口发送传感器及欧拉角数据
  113. 输入参数:无
  114. 输出参数:无
  115. 返回值:无
  116. **************************************************/
  117. void send_sensor_data()
  118. {
  119.   u8 j;

  120.   LED_BLINK;

  121.   out[2] = (u8)(gx >> 8);
  122.   out[3] = (u8)(gx);
  123.   out[4] = (u8)(gy >> 8);
  124.   out[5] = (u8)(gy);
  125.   out[6] = (u8)(gz >> 8);
  126.   out[7] = (u8)(gz);
  127.   out[8] = (u8)(ax >> 8);
  128.   out[9] = (u8)(ax);
  129.   out[10] = (u8)(ay >> 8);
  130.   out[11] = (u8)(ay);
  131.   out[12] = (u8)(az >> 8);
  132.   out[13] = (u8)(az);

  133.   temp = (s16)(pitch * 100);
  134.   out[20] = (u8)(temp >> 8);
  135.   out[21] = (u8)(temp);

  136.   temp = (s16)(roll * 100);
  137.   out[22] = (u8)(temp >> 8);
  138.   out[23] = (u8)(temp);

  139.   temp = (s16)(yaw1 * 100);
  140.   out[24] = (u8)(temp >> 8);
  141.   out[25] = (u8)(temp);

  142.   for(j = 0; j < 26; j++)
  143.   {
  144.     USART_SendData(USART1, out[j]);
  145.     while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  146.   }
  147. }
  148. /*************************************************

  149. 名称:main(void)
  150. 功能:主函数
  151. 输入参数:无
  152. 输出参数:无
  153. 返回值:无
  154. **************************************************/
  155. int main(void)
  156. {
  157.   u16 k;
  158.   GPIO_InitTypeDef GPIO_InitStructure;

  159.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
  160.   
  161.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  162.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  163.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  164.   GPIO_Init(GPIOA, &GPIO_InitStructure);  // BLUE LED

  165.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  166.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  167.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  168.   GPIO_Init(GPIOB, &GPIO_InitStructure);  // BUT

  169.   GPIO_SetBits(GPIOA, GPIO_Pin_1);

  170.   motor_init();

  171.   delay(800000);

  172.   timer_init();
  173.   iic_init();
  174.   mpu6050_init();
  175.   usart_init();
  176.   nrf24l01_init();
  177.   pid_config();
  178.   
  179.   k = 1500;
  180.   while(k)  //模式选择时间
  181.   {
  182.     if(flg_get_senor_data)
  183.     {
  184.       flg_get_senor_data = 0;
  185.           k--;

  186.           mpu6050_get_data(&gx, &gy, &gz, &ax, &ay , &az, &temperature);

  187.           gx_sum += gx;
  188.           gy_sum += gy;
  189.           gz_sum += gz;

  190.           f_gx = gx * GYRO_SCALE;
  191.           f_gy = gy * GYRO_SCALE;
  192.           f_gz = gz * GYRO_SCALE;

  193.           get_euler_angle(f_gx, f_gy, f_gz, ax, ay, az, &pitch, &roll);

  194.           if(BUT)
  195.       {
  196.             model = COM_MODEL;
  197.             led_blink();
  198.             k = 0;
  199.       }
  200.     }
  201.   }

  202.   gx_offset = gx_sum / 1500;
  203.   gy_offset        = gy_sum / 1500;
  204.   gz_offset        = gz_sum / 1500;

  205.   while (1)
  206.   {
  207.     if(flg_get_senor_data)
  208.     {
  209.       flg_get_senor_data = 0;

  210.           mpu6050_get_data(&gx, &gy, &gz, &ax, &ay , &az, &temperature);

  211.           gx -= gx_offset;
  212.           gy -= gy_offset;
  213.           gz -= gz_offset;

  214.           f_gx = gx * GYRO_SCALE;
  215.           f_gy = gy * GYRO_SCALE;
  216.           f_gz = gz * GYRO_SCALE;

  217.           get_euler_angle(f_gx, f_gy, f_gz, ax, ay, az, &pitch, &roll);

  218.           yaw = -gz;
  219.           yaw1 = -f_gz;
  220.    
  221.       if(model == NORMAL_MODEL)  //正常模式
  222.       {
  223.         receive_control_data();

  224.                 pitch += pitch_offset;
  225.         roll -= roll_offset;
  226.             yaw += yaw_offset;
  227.       
  228.             calculate(pitch, roll, yaw, thr, rud, ele, ail, f_gx, f_gy, f_gz);
  229.       }
  230.       else if(model == COM_MODEL)  //串口传输模式
  231.           {
  232.             if(send_fre++ > 4)
  233.                 {
  234.                   send_fre = 0;
  235.               send_sensor_data();
  236.                 }
  237.           }
  238.         }                                                          
  239.   }  // end while
  240. }
  241. /************************END OF FILE************************************************************/
复制代码

所有资料51hei提供下载:
四轴飞行器源码V1.rar (389.66 KB, 下载次数: 9)


评分

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

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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