程序可以支持MPU6050數(shù)據(jù)讀取、SPL06氣壓計讀取、SBUS遙控數(shù)據(jù)讀取、最多可以控制8路電機或者電調(diào)。寫了PID算法,可以讓無人機在控制保持穩(wěn)定。
飛機是自制的,硬件和機械也是自己做的,
是本人設計一部分,硬件電路會在整理之后發(fā)出來。新人第一次發(fā)帖,還望各位大佬輕噴。
制作出來的實物圖如下:
51hei截圖20230714191119.jpg (49.95 KB, 下載次數(shù): 49)
下載附件
2023-7-14 19:12 上傳
51hei截圖20230714191153.jpg (189.36 KB, 下載次數(shù): 52)
下載附件
2023-7-14 19:12 上傳
單片機源程序如下:
- #include "sys.h"
- u8 report=0; //默認開啟上報
- int motor1,motor2,servo1,servo2;
- float Setpitch=0,Setroll=0;
- float pitch,roll,yaw; //歐拉角
- short aacx,aacy,aacz; //加速度傳感器原始數(shù)據(jù)
- short gyrox,gyroy,gyroz; //陀螺儀原始數(shù)據(jù)
- short temp; //溫度
- int main(void)
- {
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設置NVIC中斷分組2:2位搶占優(yōu)先級,2位響應優(yōu)先級
- Usart_init(); //串口初始化
- delay_init(); //延時初始化 delay_ms(1000);
- BEEP_GPIO_Init();
- Motor_PWM_Init(20000-1, 72-1);
- Servo_PWM_Init(20000-1, 72-1);
- TIM2_Int_Init(100-1,720-1);
- Motor_Unlock(1900,1100);
- BEEP_GPIO_High();
- FrSky_Uart2_Init(100000);
- Adc_Init();
- SPI1_Init();
- SPL06_Init();
- MPU_IIC_Init(); //MPU使用的IIC初始化
- while(MPU_Init())
- {
- printf("mpu error\n");
- delay_ms(200);
- }; //初始化MPU6050
-
- while(mpu_dmp_init())
- {
- printf("mpu dmp error\n");
- delay_ms(200);
- }
- PID_Init();
- BEEP_GPIO_Low();
- while(1)
- {
- int i;
- static float Baro_Buf[20];
- Baro.Org_Alt = SPL06_Get_Altitude();
- //氣壓原始數(shù)據(jù)緩存
- for(i=19;i>0;i--)
- {
- Baro_Buf[i]=Baro_Buf[i-1];
- }
- Baro_Buf[0]=Baro.Org_Alt;
- // printf("%f\n",Baro.Org_Alt );
- // delay_ms(100);
- /********測量電池電壓********/
-
- Get_Bat_Voltage();
- if(Bat_Voltage<11)
- {
- BEEP_GPIO_High();
- }
- else
- {
- BEEP_GPIO_Low();
- }
- ////// printf("電池電壓為 %f \n",Bat_Voltage);
- ///**********陀螺儀數(shù)據(jù)輸出***************/
- if(mpu_dmp_get_data(&pitch,&roll,&yaw)==0)
- {
- // printf("%f,%f,%f\r\n",pitch,roll,yaw);
- temp=MPU_Get_Temperature(); //得到溫度值
- MPU_Get_Accelerometer(&aacx,&aacy,&aacz); //得到加速度傳感器數(shù)據(jù)
- MPU_Get_Gyroscope(&gyrox,&gyroy,&gyroz); //得到陀螺儀數(shù)據(jù)
- if(report)mpu6050_send_data(aacx,aacy,aacz,gyrox,gyroy,gyroz);//用自定義幀發(fā)送加速度和陀螺儀原始數(shù)據(jù)
- if(report)usart1_report_imu(aacx,aacy,aacz,gyrox,gyroy,gyroz,(int)(roll*100),(int)(pitch*100),(int)(yaw*10));
- }
- /******** 根據(jù)無線串口輸入并調(diào)整pid ********/
- PID_Update();
-
- /********更新遙控器數(shù)據(jù)********/
- update_channels();
-
- // printf( "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n",
- // channels[0],
- // channels[1],
- // channels[2],
- // channels[3],
- // channels[4],
- // channels[5],
- // channels[6],
- // channels[7],
- // channels[8],
- // channels[9],
- // channels[10],
- // channels[11]
- // );
- /********遙控器控制電機********/
- motor1=motor2=channels[0]/2+1000;
- //printf("%d\r\n",channels[0]);
- Motor_PWM_Set(motor1,motor2);
- /********遙控器控制舵機********/
- // servo1=channels[1];
- // servo2=channels[2];
- // Servo_PWM_Set(servo1,servo2);
- Setpitch=(channels[1]-988)/30;
- if(channels[2]>1003 | channels[2]<1000){
- Setroll =(channels[2]-1002)/30;
- }
- /********測試舵機********/
- // Servo_PWM_Set(1050,950);
- //printf("%f,%f,%f,%f\n",Setroll,roll,Setpitch,pitch);
- }
- }
-
復制代碼
Keil代碼下載:
程序.7z
(392.28 KB, 下載次數(shù): 24)
2023-7-14 19:56 上傳
點擊文件名下載附件
飛控程序 下載積分: 黑幣 -5
|