采用了模拟IIC,读取mpu6050数据,以及滤波
人懒,不想附图。
cubeMX配置的STM32F103ZETx的文件
单片机源程序如下:
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "usart.h"
- #include "mpu6050.h"
- #include "mpu_i2c.h"
- #include "stm32f1xx_it.h"
- #include "OLED.h"
- #include "OLED_I2C.h"
- #include "filter.h"
- #include "math.h"
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- extern unsigned int Task_Delay[NumOfTask];
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- UART_HandleTypeDef huart1;
- /* USER CODE BEGIN PV */
- #define TASK_ENABLE 0
- #define PI 3.14159265
- float Angle_Balance,Gyro_Balance,Gyro_Turn; //平衡环控制相关变量
- float Encoder_left,Encoder_right; //速度环控制相关变量
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- void MX_USART1_UART_Init(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- short Accel[3];
- short Gyro [3];
- short Temp;
- int Accel_Y,Accel_X,Accel_Z,Gyro_X,Gyro_Y,Gyro_Z;
- float Acceleration_Z; //Z轴加速度计
- //uint8_t i;
-
- /* USER CODE END 1 */
-
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_USART1_UART_Init();
- /* USER CODE BEGIN 2 */
-
- //********OLED和OLED对应的iic初始化*******//
- OLED_Init();
-
- /*MPU_I2C初始化*/
- MPU_I2C_InitGPIO();
-
- /*MPU6050初始化*/
- MPU6050_Init();
- HAL_Delay(10);
- /**********OLED清屏**************/
- OLED_CLS();
-
- /* 调用格式化输出函数打印输出数据 */
- //检测MPU6050
- if (MPU6050ReadID() == 0)
- {
- OLED_ShowStr(3,0,(unsigned char*)"can't find mpu6050!",1);
- while(1);
- }
-
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- if(Task_Delay[0]==TASK_ENABLE)
- {
- Task_Delay[0]=1000;
- }
-
- if(Task_Delay[1]==0)
- {
- MPU6050ReadAcc(Accel);
- //dprintf("\r\n加速度: %8d%8d%8d ",Accel[0],Accel[1],Accel[2]);
- Accel_X = Accel[0];
- Accel_Y = Accel[1];
- Accel_Z = Accel[2];
-
- //OLED_ShowNum(40,1,Accel[0],5,20);
- MPU6050ReadGyro(Gyro);
- //printf("陀螺仪: %8d%8d%8d ",Gyro[0],Gyro[1],Gyro[2]);
- Gyro_X = Gyro[0];
- Gyro_Y = Gyro[1];
- Gyro_Z = Gyro[2];
-
- MPU6050_ReturnTemp(&Temp);
- //printf("温度: %d",Temp);
-
- if(Gyro_Y>32768) Gyro_Y-=65536; //数据类型转换 也可通过short强制类型转换
- if(Gyro_Z>32768) Gyro_Z-=65536; //数据类型转换
- if(Accel_X>32768) Accel_X-=65536; //数据类型转换
- if(Accel_Z>32768) Accel_Z-=65536; //数据类型转换
- //Gyro_Balance=-Gyro_Y; //更新平衡角速度
- Accel_Y=atan2(Accel_X,Accel_Z)*180/PI; //计算倾角
- Gyro_Y =Gyro_Y/16.4; //陀螺仪量程转换
- Kalman_Filter(Accel_Y,-Gyro_Y); //卡尔曼滤波
-
-
- //Angle_Balance = angle; //更新平衡倾角
- //Gyro_Turn=Gyro_Z; //更新转向角速度
- //Acceleration_Z=Accel_Z; //跟新z轴加速度
- printf("\r\nY轴加速度和pitch: %8d,%8d",Gyro_Y,Accel_Y);
- //printf("%8d\n",Accel_Y);
- //Angle_Balance=angle; //更新平衡倾角
- //Gyro_Turn=Gyro_Z; //更新转向角速度
- //Acceleration_Z=Accel_Z; //===更新Z轴加速度计
- //Gyro_Balance=-Gyro_Y;
- //printf("卡尔曼滤波值%8f,%8f\n",Angle_Balance,Gyro_Turn);
-
-
-
- //OLED_ShowNum(40,2,Temp,5,10);
- Task_Delay[1]=100;//此值每1ms会减1,减到0才可以重新进来这里,所以执行的周期是100ms
- }
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- /** Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /**
- * @brief USART1 Initialization Function
- * @param None
- * @retval None
- */
- void MX_USART1_UART_Init(void)
- {
- /* USER CODE BEGIN USART1_Init 0 */
- /* USER CODE END USART1_Init 0 */
- /* USER CODE BEGIN USART1_Init 1 */
- /* USER CODE END USART1_Init 1 */
- huart1.Instance = USART1;
- huart1.Init.BaudRate = 115200;
- huart1.Init.WordLength = UART_WORDLENGTH_8B;
- huart1.Init.StopBits = UART_STOPBITS_1;
- huart1.Init.Parity = UART_PARITY_NONE;
- huart1.Init.Mode = UART_MODE_TX_RX;
- huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart1.Init.OverSampling = UART_OVERSAMPLING_16;
- if (HAL_UART_Init(&huart1) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN USART1_Init 2 */
- /* USER CODE END USART1_Init 2 */
- }
- /**
- * @brief GPIO Initialization Function
- * @param None
- * @retval None
- */
- static void MX_GPIO_Init(void)
- {
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOA_CLK_ENABLE();
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
所有资料51hei提供下载:
mpu_fliter_oled.7z
(271.84 KB, 下载次数: 143)
|