找回密码
 注册

QQ登录

只需一步,快速开始

搜索

STM32小四轴自用调试代码分享 无HMC5883程序

[复制链接]
路漫漫 发表于 2020-5-29 01:26:08 | 显示全部楼层 |阅读模式
四轴测试代码.7z (247.22 KB, 售价: 1 E币)
部分源码:
  1. #include "sys.h"
  2. #include "nrf24l01.h"
  3. #include "mpu6050.h"
  4. #include "motor.h"
  5. #include "led.h"
  6. #include "nrfreport.h"
  7. #include "flycontro.h"
  8. #include "mpudat.h"

  9. /**
  10. 微型四轴代码,2016/1/22,chrain
  11. 无HMC5883程序

  12. */





  13. int U8toInt(u8 a,u8 b);
  14. void FLY_Contro(void);
  15. int main(void)
  16. {
  17.         int PWM;
  18. //        PWMA =PWMB =PWMC =PWMD =0;
  19.         SystemInit();
  20.         delay_init();
  21.         LED_Init();  //LED初始化
  22.   
  23.         if(NRF24L01_Init())
  24.         LED1 = LED2 = LED3 = LED4 = 1;
  25.   delay_ms(1000);
  26.         LED1 = LED2 = LED3 = LED4 = 1;
  27.         MPU_Init();  //陀螺仪初始化

  28.         PID_init();
  29.         delay_ms(1000);
  30.   Motor_Init();//电机初始化
  31.   timer_Init();
  32. //        MPU6050_offset();
  33. //        LED1 = LED2 = LED3 = LED4 = 0;
  34. //      
  35. //        RxBuf[0]=RxBuf[1]=RxBuf[2]=RxBuf[3]=0;
  36. //        PWMA = PWMB = PWMC = PWMD =0;

  37. //        PWM_Init();
  38. //        PID_init();           //PID参数初始化
  39.         while(1)
  40.          {
  41.          if(RxBuf[0]&0xA0)                   LED1 = LED2 = LED3 = LED4 = 1;
  42.    else  LED_Circleflsah ();
  43.                  
  44.                  
  45.         if(RxBuf[2]>140|RxBuf[2]<115)
  46.                 Ycontrdat  =  (125 - RxBuf[2])/12.0;
  47.         else
  48.                 Ycontrdat =0;
  49.       
  50.   if(RxBuf[1]>130|RxBuf[1]<120)
  51.                 Xcontrdat  =  (RxBuf[1] - 125)/12.0;
  52.         else
  53.                 Xcontrdat =0;
  54.       
  55.         TargeContrdat.X        =  U8toInt(RxBuf[5],RxBuf[6])/100.0;
  56.         TargeContrdat.Y        =  U8toInt(RxBuf[7],RxBuf[8])/100.0;
  57.         TargeContrdat.Z        =  U8toInt(RxBuf[9],RxBuf[10])/100.0;
  58.                

  59. if(RxBuf[0] == 0XA1)      
  60. {      
  61.   PID_Kp.X =          U8toInt(RxBuf[11],RxBuf[12])/100.0;
  62.         PID_Kp.Y =          U8toInt(RxBuf[13],RxBuf[14])/100.0;
  63.         PID_Kp.Z =          U8toInt(RxBuf[15],RxBuf[16])/100.0;
  64.                  
  65.   PID_Ki.X =          U8toInt(RxBuf[17],RxBuf[18])/100.0;
  66.         PID_Ki.Y =          U8toInt(RxBuf[19],RxBuf[20])/100.0;
  67.         PID_Ki.Z =          U8toInt(RxBuf[21],RxBuf[22])/100.0;
  68.                
  69.         PID_Kd.X =          U8toInt(RxBuf[23],RxBuf[24])/100.0;
  70.         PID_Kd.Y =          U8toInt(RxBuf[25],RxBuf[26])/100.0;
  71.         PID_Kd.Z =          U8toInt(RxBuf[27],RxBuf[28])/100.0;         
  72. }                 
  73.           delay_ms(20);
  74.                  
  75. //        NRF_Report_STATUS(Angle.X*100,Angle.Y*100,Angle.Z*100,Accel.X,Accel.X,Accel.Z,Gyro.X,Gyro.Y,Gyro.Z);
  76.    NRF_Report_STATUS(Angle.Y*100,Ycontrdat*100,Angle.Z*100,
  77.                                  Angle.Y*100,Ycontrdat*100,Accel.Z,
  78.                                  Gyro.X,Gyro.Y,Gyro.Z);

  79.                 PWM = 256 - RxBuf[3];
  80. if(RxBuf[0]&0xA0)
  81. {
  82.          if(PWM > 30)
  83.                  {
  84.                         if(PWM>250) PWM =249;
  85.          
  86.        Fly_PWM = (PWM-30)*2.2;
  87.      }else
  88.                   {
  89.                                 Fly_PWM =0;
  90.                                 Angle.Z =0;
  91.                         }
  92. }else
  93.                   {
  94.                                 Fly_PWM =0;
  95.                                 Angle.Z =0;
  96.                         }
  97. // Prepare_Data(&Acc,&Acc_AVG);
  98.   //  IMUupdate(&Gyr,&Acc_AVG,&Angle);        

  99. //  LED2 =1;
  100. //  delay_ms(100);
  101.         //        LED1=~LED1;
  102.                  
  103.                  
  104.                  
  105. //          NRF24l01_Report_STATUS((int)(Angle.rol *100),(int)(Angle.pit *100),(int)(Angle.yaw*100),0,0);
  106. //               
  107. //i++;               
  108. //if(i>10)
  109. //{      
  110. //        TxBuf[15] = (Moto_A>>8)&0x00ff;               
  111. //  TxBuf[16] = Moto_A&0x00ff;
  112. //  TxBuf[17] = (Moto_B>>8)&0x00ff;
  113. //  TxBuf[18] = Moto_B&0x00ff;
  114. //  TxBuf[19] = (Moto_C>>8)&0x00ff;
  115. //        TxBuf[20] = Moto_C&0x00ff;
  116. //  TxBuf[21] = (Moto_D>>8)&0x00ff;
  117. //  TxBuf[22] = Moto_D&0x00ff;
  118. //        i=0;
  119. }
  120.                


  121. //   FLY_Contro();


  122. //         if(RxBuf[1]>0)        
  123. //                 Fly_PWM = RxBuf[1]*5;
  124. //                else
  125. //                 Fly_PWM=0;      
  126. //               
  127. //               
  128. //               

  129. // }
  130. }




  131. void FLY_Contro(void)
  132. {
  133. //float RolContro,PitContro;
  134. //float temp;
  135. //static float Rol_weitiao=0,Pit_weitiao=0;

  136. //      
  137. //      
  138. //         
  139. ////PIT控制
  140. //                 if(RxBuf[5]<115)  
  141. //                  {
  142. //                         PitContro = (115-RxBuf[5])/5.0;         
  143. //                         if(PitContro >= 20) PitContro=20.0;                                 
  144. //      }else if(RxBuf[5]>125)        //         if(TxBuf[3]<115) //TxBuf[3]>125
  145. //                  {
  146. //       PitContro = (125-RxBuf[5])/5.0;                       
  147. //       if(PitContro<=-20) PitContro=-20;                              
  148. //      }
  149. ////ROll控制                       
  150. //   if(RxBuf[4]<115)  
  151. //                  {
  152. //                         RolContro = (115-RxBuf[4])/5.0;         
  153. //                         if(RolContro >= 20) RolContro=20.0;                                 
  154. //      }else if(RxBuf[4]>125)        //         if(TxBuf[3]<115) //TxBuf[3]>125
  155. //                  {
  156. //       RolContro = (125- RxBuf[4])/5.0;                       
  157. //       if(RolContro<=-20) RolContro=-20;                              
  158. //      }
  159. //                       

  160. //                       
  161. //         Roll.Targe   =          RolContro + Rol_weitiao;      
  162. //         Pitch.Targe   =  -PitContro + Pit_weitiao;               
  163. //               

  164. //        if(RxBuf[9]==1)
  165. // {
  166. //  temp = U8toInt(RxBuf[10],RxBuf[11]);  //ROll微调
  167. // //       Pit_PID.Kp  = temp/100.0;
  168. //              Rol_weitiao= temp/4.0;  
  169. //        temp = U8toInt(RxBuf[12],RxBuf[13]);  //PIt微调
  170. //                  Pit_weitiao= temp/4.0;  
  171. ////        Pit_PID.Kd= temp/100.0;         
  172. // }
  173. //        RxBuf[9]=0;                                

  174. }












  175. int U8toInt(u8 a,u8 b)
  176. {
  177. int temp=0;
  178. //    b = temp&0x00ff;
  179. //    a = (temp>>8)&0x00ff;                 
  180.                 temp = (a<<8)|b;
  181.     if(temp>32768)temp-=65536;      
  182.     return temp;
  183. }





  184. //void PWM_Init(void)
  185. //{
  186. // GPIO_InitTypeDef GPIO_InitStructure;
  187. ……………………

  188. …………限于本文篇幅 余下代码请下载附件…………
复制代码

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

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-4-19 11:31

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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