找回密码
 立即注册

QQ登录

只需一步,快速开始

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

基于STM32F103的mini四轴源码

[复制链接]
跳转到指定楼层
楼主
mpu6050作为姿态传感器
PID适用在mini四轴上 自己DIY后需要再调参数 大四轴正在试

所有资料51hei提供下载:
STM32F103四轴飞行器源码.rar .7z (283.09 KB, 下载次数: 34)


单片机源程序如下:
  1. #include "Maths.h"
  2. #include "Control.h"
  3. #include "struct_all.h"

  4. //RC
  5. uint8_t Rc_Lock=1;//1上锁;0解锁
  6. /******************************************************************************
  7. 函数原型:        void RC_Limit(struct _Rc *rc)
  8. 功    能:        限制遥控指令范围
  9. *******************************************************************************/
  10. void RC_Limit(struct _Rc *rc)
  11. {
  12.         rc->THROTTLE = (rc->THROTTLE<=1000)?1000:rc->THROTTLE;
  13.         rc->THROTTLE = (rc->THROTTLE>=2000)?2000:rc->THROTTLE;
  14.         rc->PITCH = (rc->PITCH<=1000)?1000:rc->PITCH;
  15.         rc->PITCH = (rc->PITCH>=2000)?2000:rc->PITCH;
  16.         rc->ROLL = (rc->ROLL<=1000)?1000:rc->ROLL;
  17.         rc->ROLL = (rc->ROLL>=2000)?2000:rc->ROLL;
  18.         rc->YAW  = (rc->YAW<=1000)?1000:rc->YAW;
  19.         rc->YAW  = (rc->YAW>=2000)?2000:rc->YAW;
  20.         rc->AUX1 = (rc->AUX1<=1000)?1000:rc->AUX1;
  21.         rc->AUX1 = (rc->AUX1>=2000)?2000:rc->AUX1;
  22.         rc->AUX2 = (rc->AUX2<=1000)?1000:rc->AUX2;
  23.         rc->AUX2 = (rc->AUX2>=2000)?2000:rc->AUX2;
  24.         rc->AUX3 = (rc->AUX3<=1000)?1000:rc->AUX3;
  25.         rc->AUX3 = (rc->AUX3>=2000)?2000:rc->AUX3;
  26. }
  27.         
  28. /******************************************************************************
  29. 函数原型:        void RC_LOCK(void)
  30. 功    能:        遥控手势指令及解锁处理(以下注释针对美国手,遥控默认的就是美国手)
  31. *******************************************************************************/
  32. void RC_LOCK(void)
  33. {
  34.         static uint8_t count0,count1,count2;
  35.         if(Rc.THROTTLE<1300 )//&& Rc.YAW>1700 && Rc.PITCH>1400 && Rc.PITCH<1600)
  36.                 count0++;
  37.         else
  38.                 count0 = 0;
  39.         
  40.         if(count0>15 && Rc_Lock==1)
  41.         {
  42.                 Rc_Lock = 0;//左手油门手的摇杆打向右下角,右手摇杆不动,解锁
  43.                 LED3_OFF;
  44.                 Delay_led(100);
  45.                 LED3_ON;
  46.                 Delay_led(100);
  47.         }
  48. ////////////////////////////////////////////////        
  49.         if(Rc.THROTTLE<1300 && Rc.YAW<1300 && Rc.PITCH>1400 && Rc.PITCH<1600)
  50.                 count1++;
  51.         else
  52.                 count1 = 0;
  53.         
  54.         if(count1>150 && Rc_Lock==0)
  55.         {
  56.                 Rc_Lock = 1;//左手油门手的摇杆打向左下角,右手摇杆不动,上锁
  57.                 LED3_OFF;
  58.                 Delay_led(100);
  59.                 LED3_ON;
  60.                 Delay_led(100);
  61.         }
  62. ////////////////////////////////////////////////        
  63.         if(Rc.THROTTLE<1300 && Rc.YAW<1300 && Rc.PITCH<1300)
  64.                 count2++;
  65.         else
  66.                 count2 = 0;
  67.         
  68.         if(count2>100)//&& Rc_Lock)//上锁状态才能校正
  69.         {
  70.                 count2=0;
  71.                 Do_GYRO_Offset();//左手油门手的摇杆打向左下角,右手摇杆俯仰方向拉到最底,校正陀螺仪
  72.                 Do_ACC_Offset(); //左手油门手的摇杆打向左下角,右手摇杆俯仰方向拉到最底,校正加速度计
  73.                
  74.                 LED3_OFF;
  75.                 Delay_led(50);
  76.                 LED3_ON;
  77.                 Delay_led(50);
  78.                 LED3_OFF;
  79.                 Delay_led(50);
  80.                 LED3_ON;
  81.         }
  82. }

  83. //PID
  84. #define angle_max                          10.0f        
  85. #define angle_integral_max         1000.0f        
  86. /******************************************************************************
  87. 函数原型:        void Control_Angle(struct _out_angle *angle,struct _Rc *rc)
  88. 功    能:        PID控制器(外环)
  89. *******************************************************************************/
  90. void Control_Angle(struct _out_angle *angle,struct _Rc *rc)
  91. {
  92.         static struct _out_angle control_angle;
  93.         static struct _out_angle last_angle;
  94. //////////////////////////////////////////////////////////////////
  95. //                        以下为角度环
  96. //////////////////////////////////////////////////////////////////
  97.         if(rc->ROLL>1490 && rc->ROLL<1510)        
  98.                 rc->ROLL=1500;
  99.         if(rc->PITCH>1490 && rc->PITCH<1510)        
  100.                 rc->PITCH=1500;
  101. //////////////////////////////////////////////////////////////////
  102.         if(rc->AUX1>1495 && rc->AUX1<1505)        
  103.                 rc->AUX1=1500;
  104.         if(rc->AUX2>1495 && rc->AUX2<1505)        
  105.                 rc->AUX2=1500;
  106. //////////////////////////////////////////////////////////////////
  107.         control_angle.roll  = angle->roll  - (rc->ROLL  -1500)/13.0f + (rc->AUX2 -1500)/100.0f;
  108.         control_angle.pitch = angle->pitch - (rc->PITCH -1500)/13.0f - (rc->AUX1 -1500)/100.0f;
  109. //////////////////////////////////////////////////////////////////
  110.         if(control_angle.roll >  angle_max)        //ROLL
  111.                 roll.integral +=  angle_max;
  112.         if(control_angle.roll < -angle_max)
  113.                 roll.integral += -angle_max;
  114.         else
  115.                 roll.integral += control_angle.roll;
  116.         
  117.         if(roll.integral >  angle_integral_max)
  118.            roll.integral =  angle_integral_max;
  119.         if(roll.integral < -angle_integral_max)
  120.            roll.integral = -angle_integral_max;
  121. //////////////////////////////////////////////////////////////////
  122.         if(control_angle.pitch >  angle_max)//PITCH
  123.            pitch.integral +=  angle_max;
  124.         if(control_angle.pitch < -angle_max)
  125.            pitch.integral += -angle_max;
  126.         else
  127.                 pitch.integral += control_angle.pitch;

  128.         if(pitch.integral >  angle_integral_max)
  129.            pitch.integral =  angle_integral_max;
  130.         if(pitch.integral < -angle_integral_max)
  131.            pitch.integral = -angle_integral_max;
  132. //////////////////////////////////////////////////////////////////
  133.         if(rc->THROTTLE<1200)//油门较小时,积分清零
  134.         {
  135.                 roll.integral  = 0;
  136.                 pitch.integral = 0;
  137.         }
  138. //////////////////////////////////////////////////////////////////
  139.         roll.output  = roll.kp *control_angle.roll  + roll.ki *roll.integral  + roll.kd *(control_angle.roll -last_angle.roll );
  140.         pitch.output = pitch.kp*control_angle.pitch + pitch.ki*pitch.integral + pitch.kd*(control_angle.pitch-last_angle.pitch);
  141. //////////////////////////////////////////////////////////////////
  142.         last_angle.roll =control_angle.roll;
  143.         last_angle.pitch=control_angle.pitch;
  144. }

  145. #define gyro_max                          50.0f        
  146. #define gyro_integral_max         5000.0f
  147. /******************************************************************************
  148. 函数原型:        void Control_Gyro(struct _SI_float *gyro,struct _Rc *rc,uint8_t Lock)
  149. 功    能:        PID控制器(内环)
  150. *******************************************************************************/
  151. void Control_Gyro(struct _SI_float *gyro,struct _Rc *rc,uint8_t Lock)
  152. {
  153.         static struct _out_angle control_gyro;
  154.         static struct _out_angle last_gyro;
  155.         int16_t throttle1,throttle2,throttle3,throttle4;
  156. //////////////////////////////////////////////////////////////////
  157. //                        以下为角速度环
  158. //////////////////////////////////////////////////////////////////
  159.         if(rc->YAW>1400 && rc->YAW<1600)
  160.                 rc->YAW=1500;
  161.         if(rc->AUX3>1495 && rc->AUX3<1505)        
  162.                 rc->AUX3=1500;
  163. //////////////////////////////////////////////////////////////////
  164.         control_gyro.roll  = -roll.output - gyro->y*Radian_to_Angle;
  165.         control_gyro.pitch = pitch.output - gyro->x*Radian_to_Angle;
  166.         if(rc->AUX4 & Lock_Mode)
  167.                 control_gyro.yaw   = - gyro->z*Radian_to_Angle - (rc->AUX3 -1500)/100.0f;//锁尾模式
  168.         else
  169.                 control_gyro.yaw   = -(rc->YAW-1500)/2.0f - gyro->z*Radian_to_Angle + (rc->AUX3 -1500)/50.0f;//非锁尾模式
  170. //////////////////////////////////////////////////////////////////
  171.         if(control_gyro.roll >  gyro_max)        //GYRO_ROLL
  172.                 gyro_roll.integral +=  gyro_max;
  173.         if(control_gyro.roll < -gyro_max)
  174.                 gyro_roll.integral += -gyro_max;
  175.         else
  176.                 gyro_roll.integral += control_gyro.roll;
  177.         
  178.         if(gyro_roll.integral >  gyro_integral_max)
  179.            gyro_roll.integral =  gyro_integral_max;
  180.         if(gyro_roll.integral < -gyro_integral_max)
  181.            gyro_roll.integral = -gyro_integral_max;
  182. //////////////////////////////////////////////////////////////////
  183.         if(control_gyro.pitch >  gyro_max)//GYRO_PITCH
  184.                 gyro_pitch.integral +=  gyro_max;
  185.         if(control_gyro.pitch < -gyro_max)
  186.                 gyro_pitch.integral += -gyro_max;
  187.         else
  188.                 gyro_pitch.integral += control_gyro.pitch;
  189.         
  190.         if(gyro_pitch.integral >  gyro_integral_max)
  191.            gyro_pitch.integral =  gyro_integral_max;
  192.         if(gyro_pitch.integral < -gyro_integral_max)
  193.            gyro_pitch.integral = -gyro_integral_max;
  194. //////////////////////////////////////////////////////////////////
  195. //        if(control_gyro.yaw >  gyro_max)//GYRO_YAW
  196. //                gyro_yaw.integral +=  gyro_max;
  197. //        if(control_gyro.yaw < -gyro_max)
  198. //                gyro_yaw.integral += -gyro_max;
  199. //        else
  200.                 gyro_yaw.integral += control_gyro.yaw;
  201.         
  202.         if(gyro_yaw.integral >  gyro_integral_max)
  203.            gyro_yaw.integral =  gyro_integral_max;
  204.         if(gyro_yaw.integral < -gyro_integral_max)
  205.            gyro_yaw.integral = -gyro_integral_max;
  206. //////////////////////////////////////////////////////////////////
  207.         if(rc->THROTTLE<1200)//油门较小时,积分清零
  208.         {
  209.                 gyro_yaw.integral  = 0;
  210. ……………………

  211. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码



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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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