2017最新整理匿名四軸上位機和飛控經典代碼分享
0.png (46.58 KB, 下載次數: 84)
下載附件
2018-10-24 16:44 上傳
0.png (18.93 KB, 下載次數: 79)
下載附件
2018-10-24 16:44 上傳
ANO-MR-F1-140104經典pid
stm32單片機源程序如下:
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x.h"
- #include "sysconfig.h"
- #include "bsp.h"
- #include "led.h"
- #include "tim3.h"
- #include "usart.h"
- #include "ANO_TC_STM32F1_I2C.h"
- #include "MPU6050.h"
- #include "moto.h"
- #include "spi.h"
- #include "nrf24l01.h"
- #include "tim_pwm_in.h"
- #include "rc.h"
- #include "imu.h"
- #include "control.h"
- #include "data_transfer.h"
- u8 SYS_INIT_OK=0;
- ////////////////////////////////////////////////////////////////////////////////
- void SYS_INIT(void)
- {
- LED_INIT();
- LED_FLASH();
- Moto_Init();
- Uart1_Init(115200);
- Tim3_Init(500);
- #ifdef CONTROL_USE_RC
- Tim_Pwm_In_Init();
- #endif
- Nvic_Init();
- ANO_TC_I2C2_INIT(0xA6,400000,1,1,3,3);
- MPU6050_Init();
-
- Spi1_Init();
- Nrf24l01_Init(MODEL_TX2,40);
- if(Nrf24l01_Check())
- Uart1_Put_String("NRF24L01 IS OK !\r\n");
- else
- Uart1_Put_String("NRF24L01 IS NOT OK !\r\n");
-
- FLASH_Unlock();
- EE_INIT();
- EE_READ_ACC_OFFSET();
- EE_READ_GYRO_OFFSET();
- EE_READ_PID();
-
- Tim3_Control(1);
- }
- ////////////////////////////////////////////////////////////////////////////////
- u8 FLAG_ATT=0;
- T_int16_xyz Acc,Gyr; //兩次綜合后的傳感器數據
- T_int16_xyz Acc_AVG;
- T_float_angle Att_Angle; //ATT函數計算出的姿態角
- vs32 Alt;
- T_RC_Data Rc_D; //遙控通道數據
- T_RC_Control Rc_C; //遙控功能數據
- int main(void)
- {
- static u8 att_cnt=0;
- static u8 rc_cnt=0;
- static T_int16_xyz mpu6050_dataacc1,mpu6050_dataacc2,mpu6050_datagyr1,mpu6050_datagyr2;
- static u8 senser_cnt=0,status_cnt=0,dt_rc_cnt=0,dt_moto_cnt=0;
-
- SYS_INIT();
-
- while (1)
- {
- if(FLAG_ATT)
- {
- FLAG_ATT = 0;
- att_cnt++;
- rc_cnt++;
-
- if(rc_cnt==20)
- {
- rc_cnt = 0;
- #ifdef CONTROL_USE_RC
- Rc_GetValue(&Rc_D);
- #endif
- Rc_Fun(&Rc_D,&Rc_C);
- }
- if(att_cnt==1)
- MPU6050_Dataanl(&mpu6050_dataacc1,&mpu6050_datagyr1);
- else
- {
- att_cnt = 0;
- MPU6050_Dataanl(&mpu6050_dataacc2,&mpu6050_datagyr2);
- Acc.X = (mpu6050_dataacc1.X+mpu6050_dataacc2.X)/2;
- Acc.Y = (mpu6050_dataacc1.Y+mpu6050_dataacc2.Y)/2;
- Acc.Z = (mpu6050_dataacc1.Z+mpu6050_dataacc2.Z)/2;
- Gyr.X = (mpu6050_datagyr1.X+mpu6050_datagyr2.X)/2;
- Gyr.Y = (mpu6050_datagyr1.Y+mpu6050_datagyr2.Y)/2;
- Gyr.Z = (mpu6050_datagyr1.Z+mpu6050_datagyr2.Z)/2;
- Prepare_Data(&Acc,&Acc_AVG);
- IMUupdate(&Gyr,&Acc_AVG,&Att_Angle);
- Control(&Att_Angle,&Gyr,&Rc_D,Rc_C.ARMED);
- if(Rc_C.ARMED)
- LED1_ONOFF();
- else
- LED1_OFF;
-
- senser_cnt++;
- status_cnt++;
- dt_rc_cnt++;
- dt_moto_cnt++;
- if(senser_cnt==5)
- {
- senser_cnt = 0;
- Send_Senser = 1;
- }
- if(status_cnt==5)
- {
- status_cnt = 0;
- Send_Status = 1;
- }
- if(dt_rc_cnt==10)
- {
- dt_rc_cnt=0;
- Send_RCData = 1;
- }
- if(dt_moto_cnt==10)
- {
- dt_moto_cnt=0;
- Send_MotoPwm = 1;
- }
- }
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////
復制代碼- #include "control.h"
- PID PID_ROL,PID_PIT,PID_YAW,PID_ALT,PID_POS,PID_PID_1,PID_PID_2;
- int16_t getlast_roll=0,geilast_pitch=0;
- float rol_i=0,pit_i=0,yaw_p=0;
- vs16 Moto_PWM_1=0,Moto_PWM_2=0,Moto_PWM_3=0,Moto_PWM_4=0,Moto_PWM_5=0,Moto_PWM_6=0,Moto_PWM_7=0,Moto_PWM_8=0;
- void Control(T_float_angle *att_in,T_int16_xyz *gyr_in, T_RC_Data *rc_in, u8 armed)
- {
- T_float_angle angle;
- angle.rol = att_in->rol - (rc_in->ROLL-1500)/12;
- angle.pit = att_in->pit + (rc_in->PITCH-1500)/12;
-
- rol_i += angle.rol;
- if(rol_i>2000)
- rol_i=2000;
- if(rol_i<-2000)
- rol_i=-2000;
- PID_ROL.pout = PID_ROL.P * angle.rol;
- PID_ROL.dout = -PID_ROL.D * gyr_in->Y;
- PID_ROL.iout = PID_ROL.I * PID_ROL.dout;
- pit_i += angle.pit;
- if(pit_i>2000)
- pit_i=2000;
- if(pit_i<-2000)
- pit_i=-2000;
- PID_PIT.pout = PID_PIT.P * angle.pit;
- PID_PIT.dout = PID_PIT.D * gyr_in->X;
- PID_PIT.iout = PID_PIT.I * pit_i;
- if(rc_in->YAW<1400||rc_in->YAW>1600)
- {gyr_in->Z=gyr_in->Z+(rc_in->YAW-1500)*2;}
- yaw_p+=gyr_in->Z*0.0609756f*0.002f;// +(Rc_Get.YAW-1500)*30
- if(yaw_p>20)
- yaw_p=20;
- if(yaw_p<-20)
- yaw_p=-20;
- PID_YAW.pout=PID_YAW.P*yaw_p;
- PID_YAW.dout = PID_YAW.D * gyr_in->Z;
-
- if(rc_in->THROTTLE<1200)
- {
- pit_i=0;
- rol_i=0;
- yaw_p=0;
- }
- PID_ROL.OUT = (-PID_ROL.pout)-PID_ROL.iout +PID_ROL.dout;//
- PID_PIT.OUT = PID_PIT.pout + PID_PIT.iout + PID_PIT.dout;
- PID_YAW.OUT = PID_YAW.pout + PID_YAW.iout + PID_YAW.dout;
-
- if(rc_in->THROTTLE>1200&&armed)
- {
- Moto_PWM_1 = rc_in->THROTTLE - 1000 - PID_ROL.OUT - PID_PIT.OUT + PID_YAW.OUT;
- Moto_PWM_2 = rc_in->THROTTLE - 1000 + PID_ROL.OUT - PID_PIT.OUT - PID_YAW.OUT;
- Moto_PWM_3 = rc_in->THROTTLE - 1000 + PID_ROL.OUT + PID_PIT.OUT + PID_YAW.OUT;
- Moto_PWM_4 = rc_in->THROTTLE - 1000 - PID_ROL.OUT + PID_PIT.OUT - PID_YAW.OUT;
- }
- else
- {
- Moto_PWM_1 = 0;
- Moto_PWM_2 = 0;
- Moto_PWM_3 = 0;
- Moto_PWM_4 = 0;
- }
- Moto_PwmRflash(Moto_PWM_1,Moto_PWM_2,Moto_PWM_3,Moto_PWM_4);
- }
復制代碼
所有資料51hei提供下載:
2017最新整理匿名四軸上位機和飛控經典代碼分享.rar
(4.84 MB, 下載次數: 158)
2018-10-24 11:57 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|