基于STM32F103C8T6單片機的四軸翼飛行器源碼,2.4G通訊,MPU6050等等資料
單片機源程序如下:
- /***********************************************
- 標題: mian.c
- 作者: 秋陽電子
- 版本:v1.0
- MDK-ARM 版本: v4.12
- ST 庫版本:v3.50
- 功能: 四軸飛控
- 說明:
- *************************************************/
- #include "stm32f10x.h"
- #include "iic.h"
- #include "timer.h"
- #include "usart.h"
- #include "mpu6050.h"
- #include "nrf24l01.h"
- #include "motor.h"
- #include "imu.h"
- #include "pid.h"
- #include <stdio.h>
- #define COM_MODEL 0x00
- #define NORMAL_MODEL 0x01
- #define BUT (GPIOB->IDR & GPIO_Pin_12)
- #define LED_BLINK GPIOA->ODR ^= (1 << 1)
- #define TIME_OUT 1200
- u8 model = NORMAL_MODEL;
- u8 flg_get_senor_data;
- u8 flg_recive_data;
- u8 out[35] = {0x5f, 0x60, 0};
- u8 offset_data[5];
- u8 rx_data_buf[16];
- s16 gx, gy, gz, ax ,ay, az, temperature;
- float set_pitch, set_roll, yaw, yaw1, pitch, roll, left_yaw, right_yaw;
- float f_gx, f_gy, f_gz;
- s16 temp;
- u16 time_out = 0;
- u8 send_fre = 0;
- volatile u8 thr = 0, rud = 0, ele = 0, ail = 0;
- volatile float yaw_offset = 0, pitch_offset = 0, roll_offset = 0;
- s16 gx_offset = 0, gy_offset = 0, gz_offset = 0;
- s32 gx_sum = 0, gy_sum = 0, gz_sum = 0;
- /*************************************************
- 名稱:void delay(u32 count)
- 功能:延時
- 輸入參數:延時數
- 輸出參數:無
- 返回值: 無
- **************************************************/
- void delay(u32 count)
- {
- for(; count != 0; count--);
- }
- /*************************************************
- 名稱:void led_blink()
- 功能:LED閃爍
- 輸入參數:無
- 輸出參數:無
- 返回值: 無
- **************************************************/
- void led_blink()
- {
- u8 j;
- for(j = 0; j < 30; j++)
- {
- LED_BLINK;
- delay(0x80000);
- }
- }
- /*************************************************
- 名稱:receive_control_data()
- 功能:接收遙控器數據
- 輸入參數:無
- 輸出參數:油門控制等信息
- 返回值:無
- **************************************************/
- void receive_control_data()
- {
- u16 check = 0;
- u8 i;
- if(nrf24l01_rx_data(&rx_data_buf[0]))
- {
- for (i = 0; i < 15; ++i)
- {
- check += rx_data_buf[i];
- }
- if((check & 0xff) == rx_data_buf[15])
- {
- LED_BLINK;
- time_out = 0;
- thr = rx_data_buf[0];
- rud = rx_data_buf[1];
- ele = rx_data_buf[2];
- ail = rx_data_buf[3];
- yaw_offset = (rx_data_buf[4] - 0x40) * 1.0;
- pitch_offset = (rx_data_buf[5] - 0x40) * 0.25;
- roll_offset = (rx_data_buf[6] - 0x40) * 0.25;
- }
- }
- else //接收命令超時
- {
- if(time_out++ > TIME_OUT)
- {
- time_out = TIME_OUT;
- thr = 0;
- rud = 0;
- ele = 0;
- ail = 0;
- }
- }
- }
- /*************************************************
- 名稱:send_sensor_data()
- 功能:通過串口發送傳感器及歐拉角數據
- 輸入參數:無
- 輸出參數:無
- 返回值:無
- **************************************************/
- void send_sensor_data()
- {
- u8 j;
- LED_BLINK;
- out[2] = (u8)(gx >> 8);
- out[3] = (u8)(gx);
- out[4] = (u8)(gy >> 8);
- out[5] = (u8)(gy);
- out[6] = (u8)(gz >> 8);
- out[7] = (u8)(gz);
- out[8] = (u8)(ax >> 8);
- out[9] = (u8)(ax);
- out[10] = (u8)(ay >> 8);
- out[11] = (u8)(ay);
- out[12] = (u8)(az >> 8);
- out[13] = (u8)(az);
- temp = (s16)(pitch * 100);
- out[20] = (u8)(temp >> 8);
- out[21] = (u8)(temp);
- temp = (s16)(roll * 100);
- out[22] = (u8)(temp >> 8);
- out[23] = (u8)(temp);
- temp = (s16)(yaw1 * 100);
- out[24] = (u8)(temp >> 8);
- out[25] = (u8)(temp);
- for(j = 0; j < 26; j++)
- {
- USART_SendData(USART1, out[j]);
- while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
- }
- }
- /*************************************************
- 名稱:main(void)
- 功能:主函數
- 輸入參數:無
- 輸出參數:無
- 返回值:無
- **************************************************/
- int main(void)
- {
- u16 k;
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure); // BLUE LED
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure); // BUT
- GPIO_SetBits(GPIOA, GPIO_Pin_1);
- motor_init();
- delay(800000);
- timer_init();
- iic_init();
- mpu6050_init();
- usart_init();
- nrf24l01_init();
- pid_config();
-
- k = 1500;
- while(k) //模式選擇時間
- {
- if(flg_get_senor_data)
- {
- flg_get_senor_data = 0;
- k--;
- mpu6050_get_data(&gx, &gy, &gz, &ax, &ay , &az, &temperature);
- gx_sum += gx;
- gy_sum += gy;
- gz_sum += gz;
- f_gx = gx * GYRO_SCALE;
- f_gy = gy * GYRO_SCALE;
- f_gz = gz * GYRO_SCALE;
- get_euler_angle(f_gx, f_gy, f_gz, ax, ay, az, &pitch, &roll);
- if(BUT)
- {
- model = COM_MODEL;
- led_blink();
- k = 0;
- }
- }
- }
- gx_offset = gx_sum / 1500;
- gy_offset = gy_sum / 1500;
- gz_offset = gz_sum / 1500;
- while (1)
- {
- if(flg_get_senor_data)
- {
- flg_get_senor_data = 0;
- mpu6050_get_data(&gx, &gy, &gz, &ax, &ay , &az, &temperature);
- gx -= gx_offset;
- gy -= gy_offset;
- gz -= gz_offset;
- f_gx = gx * GYRO_SCALE;
- f_gy = gy * GYRO_SCALE;
- f_gz = gz * GYRO_SCALE;
- get_euler_angle(f_gx, f_gy, f_gz, ax, ay, az, &pitch, &roll);
- yaw = -gz;
- yaw1 = -f_gz;
-
- if(model == NORMAL_MODEL) //正常模式
- {
- receive_control_data();
- pitch += pitch_offset;
- roll -= roll_offset;
- yaw += yaw_offset;
-
- calculate(pitch, roll, yaw, thr, rud, ele, ail, f_gx, f_gy, f_gz);
- }
- else if(model == COM_MODEL) //串口傳輸模式
- {
- if(send_fre++ > 4)
- {
- send_fre = 0;
- send_sensor_data();
- }
- }
- }
- } // end while
- }
- /************************END OF FILE************************************************************/
復制代碼
所有資料51hei提供下載:
四軸飛行器源碼V1.rar
(389.66 KB, 下載次數: 9)
2018-3-13 01:35 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|