FPV F411
FPV F411
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stdio.h"
#include "string.h"
#include "AT24C08.h"
#include "FS-iA6B.h"
#include "MPU6050_IIR.h"
// tim10_100ms_count++;
// if(tim10_100ms_count == 100)
// {
// tim10_100ms_count = 0;
// tim10_100ms_flag = 1;
// }
MPU6050_IIR_t MPU6050_IIR;
void ESC_Calibration(void);
void Is_ibus_Received(void); // check connect Ibus
void Read_PID_EP_Start(void);
void RX_PID_Gain_Request(void);
void Encode_AHRS(unsigned char* hc06_tx_buf); // Read MPU + SetPoint -->
hc06_tx_buf
void Encode_PID_Gain(unsigned char* hc06_tx_buf, unsigned char id, float p, float
i, float d); // Read PID_EP --> hc06_tx_buf
UART_HandleTypeDef huart1;
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
// ESC_Calibration();
AT24C08_Init(&hi2c1);
Read_PID_EP_Start();
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
RX_PID_Gain_Request();
MPU6050_IIR_Read_All(&hi2c3, &MPU6050_IIR);
MPU6050_IIR_Read_All_Benim(&hi2c3, &MPU6050_IIR);
// if(ibus_rx_cplt_flag == 1)
// {
// ibus_rx_cplt_flag = 0;
// if(iBus_Check_CHKSUM(&ibus_rx_buf[0], 32) == 1)
// {
// iBus_Parsing(&ibus_rx_buf[0], &iBus);
// if(iBus_isActiveFailsafe(&iBus) == 1)
// {
// LL_GPIO_SetOutputPin(GPIOC, LL_GPIO_PIN_13);
// HAL_Delay(100);
// LL_GPIO_ResetOutputPin(GPIOC, LL_GPIO_PIN_13);
// HAL_Delay(100);
// }
// else
// {
// LL_GPIO_ResetOutputPin(GPIOC, LL_GPIO_PIN_13);
// }
// printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
// iBus.RH, iBus.RV, iBus.LV, iBus.LH, iBus.SwA,
iBus.SwC, iBus.SwD, iBus.FailSafe);
// HAL_Delay(100);
// }
// }
// while(Is_iBus_Received() == 0)
// {
// LL_GPIO_SetOutputPin(GPIOC, LL_GPIO_PIN_13);
// HAL_Delay(100);
// LL_GPIO_ResetOutputPin(GPIOC, LL_GPIO_PIN_13);
// HAL_Delay(100);
// }
// if(iBus.SwC == 2000)
// {
// ESC_Calibration();
// while(iBus.SwC != 1000)
// {
// Is_iBus_Received();
// }
// }
// else if(iBus.SwC == 1500)
// {
// memset(&MPU6050_IIR, 0, sizeof(MPU6050_IIR));
// Calibrate_MPU6050_IIR(&hi2c3, &MPU6050_IIR);
// while(iBus.SwC != 1000)
// {
// Is_iBus_Received();
// LL_GPIO_SetOutputPin(GPIOC, LL_GPIO_PIN_13);
// HAL_Delay(100);
// LL_GPIO_ResetOutputPin(GPIOC, LL_GPIO_PIN_13);
// HAL_Delay(100);
// }
// }
//
//
// while(Is_iBus_Throttle_Min() == 0)
// {
// LL_GPIO_SetOutputPin(GPIOC, LL_GPIO_PIN_13);
// HAL_Delay(100);
// LL_GPIO_ResetOutputPin(GPIOC, LL_GPIO_PIN_13);
// HAL_Delay(100);
// }
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/**
* @brief I2C1 Initialization Function
* @param None
* @retval None
*/
static void MX_I2C1_Init(void)
{
/**
* @brief I2C3 Initialization Function
* @param None
* @retval None
*/
static void MX_I2C3_Init(void)
{
/**
* @brief TIM3 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM3_Init(void)
{
GPIO_InitStruct.Pin = LL_GPIO_PIN_0|LL_GPIO_PIN_1;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
GPIO_InitStruct.Alternate = LL_GPIO_AF_2;
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/**
* @brief TIM10 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM10_Init(void)
{
/**
* @brief USART1 Initialization Function
* @param None
* @retval None
*/
static void MX_USART1_UART_Init(void)
{
/**
* @brief USART6 Initialization Function
* @param None
* @retval None
*/
static void MX_USART6_UART_Init(void)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
/**USART6 GPIO Configuration
PA11 ------> USART6_TX
PA12 ------> USART6_RX
*/
GPIO_InitStruct.Pin = LL_GPIO_PIN_11|USART6_RX_IA6B_Pin;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
GPIO_InitStruct.Alternate = LL_GPIO_AF_8;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/**/
LL_GPIO_ResetOutputPin(GPIOC, LL_GPIO_PIN_13);
/**/
GPIO_InitStruct.Pin = LL_GPIO_PIN_13;
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
if(huart->Instance == USART1)
{
HAL_UART_Receive_IT(&huart1, &uart1_rx_data, 1);
switch(cnt)
{
case 0:
if(uart1_rx_data == 0x47)
{
hc06_rx_buf[cnt] = uart1_rx_data;
cnt++;
}
break;
case 1:
if(uart1_rx_data == 0x53)
{
hc06_rx_buf[cnt] = uart1_rx_data;
cnt++;
}
else
cnt = 0;
break;
case 19:
hc06_rx_buf[cnt] = uart1_rx_data;
cnt = 0;
hc06_rx_cplt_flag = 1;
break;
default:
hc06_rx_buf[cnt] = uart1_rx_data;
cnt++;
break;
}
}
}
return 0;
}
hc06_tx_buf[2] = 0x10;
hc06_tx_buf[3] = (short)(MPU6050_IIR.angle_pitch_output*100);
hc06_tx_buf[4] = ((short)(MPU6050_IIR.angle_pitch_output*100))>>8;
hc06_tx_buf[5] = (short)(MPU6050_IIR.angle_roll_output*100);
hc06_tx_buf[6] = ((short)(MPU6050_IIR.angle_roll_output*100))>>8;
hc06_tx_buf[11] = (short)((iBus.RH-1500)*0.1f*100);
hc06_tx_buf[12] = ((short)((iBus.RH-1500)*0.1f*100))>>8;
hc06_tx_buf[13] = (short)((iBus.RV-1500)*0.1f*100);
hc06_tx_buf[14] = ((short)((iBus.RV-1500)*0.1f*100))>>8;
hc06_tx_buf[17] = 0x00;
hc06_tx_buf[18] = 0x00;
hc06_tx_buf[19] = 0xff;
// hc06_tx_buf[3] = 0;
// hc06_tx_buf[4] = 0>>8;
// hc06_tx_buf[5] = 0>>16;
// hc06_tx_buf[6] = 0>>24;
// hc06_tx_buf[7] = 0;
// hc06_tx_buf[8] = 0>>8;
// hc06_tx_buf[9] = 0>>16;
// hc06_tx_buf[10] = 0>>24;
// hc06_tx_buf[15] = iBus_isActiveFailsafe(&iBus);
// hc06_tx_buf[16] = 0x00;
// hc06_tx_buf[17] = 0x00;
// hc06_tx_buf[18] = 0x00;
// hc06_tx_buf[19] = 0xff;
hc06_tx_buf[2] = id;
// Kp
unsigned char* p_bytes = (unsigned char*)&p;
hc06_tx_buf[3] = p_bytes[0];
hc06_tx_buf[4] = p_bytes[1];
hc06_tx_buf[5] = p_bytes[2];
hc06_tx_buf[6] = p_bytes[3];
// Ki
unsigned char* i_bytes = (unsigned char*)&i;
hc06_tx_buf[7] = i_bytes[0];
hc06_tx_buf[8] = i_bytes[1];
hc06_tx_buf[9] = i_bytes[2];
hc06_tx_buf[10] = i_bytes[3];
// Kd
unsigned char* d_bytes = (unsigned char*)&d;
hc06_tx_buf[11] = d_bytes[0];
hc06_tx_buf[12] = d_bytes[1];
hc06_tx_buf[13] = d_bytes[2];
hc06_tx_buf[14] = d_bytes[3];
hc06_tx_buf[15] = 0x00;
hc06_tx_buf[16] = 0x00;
hc06_tx_buf[17] = 0x00;
hc06_tx_buf[18] = 0x00;
hc06_tx_buf[19] = 0xff;
void Read_PID_EP_Start(void)
{
if(EP_PIDGain_Read(0, &roll_in_kp, &roll_in_ki, &roll_in_kd) != 0 ||
EP_PIDGain_Read(1, &roll_out_kp, &roll_out_ki, &roll_out_kd) != 0 ||
EP_PIDGain_Read(2, &pitch_in_kp, &pitch_in_ki, &pitch_in_kd) != 0 ||
EP_PIDGain_Read(3, &pitch_out_kp, &pitch_out_ki, &pitch_out_kd) != 0
||
EP_PIDGain_Read(4, &yaw_heading_kp, &yaw_heading_ki,
&yaw_heading_kd) != 0 ||
EP_PIDGain_Read(5, &yaw_rate_kp, &yaw_rate_ki, &yaw_rate_kd) != 0)
{
LL_GPIO_SetOutputPin(GPIOC, LL_GPIO_PIN_13);
HAL_Delay(100);
LL_GPIO_ResetOutputPin(GPIOC, LL_GPIO_PIN_13);
HAL_Delay(100);
}
else
{
Encode_PID_Gain(&hc06_tx_buf[0], 0, roll_in_kp, roll_in_ki, roll_in_kd);
HAL_UART_Transmit(&huart1, &hc06_tx_buf[0], 20, 10);
Encode_PID_Gain(&hc06_tx_buf[0], 1, roll_out_kp, roll_out_ki, roll_out_kd);
HAL_UART_Transmit(&huart1, &hc06_tx_buf[0], 20, 10);
Encode_PID_Gain(&hc06_tx_buf[0], 2, pitch_in_kp, pitch_in_ki, pitch_in_kd);
HAL_UART_Transmit(&huart1, &hc06_tx_buf[0], 20, 10);
Encode_PID_Gain(&hc06_tx_buf[0], 3, pitch_out_kp, pitch_out_ki,
pitch_out_kd);
HAL_UART_Transmit(&huart1, &hc06_tx_buf[0], 20, 10);
Encode_PID_Gain(&hc06_tx_buf[0], 4, yaw_heading_kp, yaw_heading_ki,
yaw_heading_kd);
HAL_UART_Transmit(&huart1, &hc06_tx_buf[0], 20, 10);
Encode_PID_Gain(&hc06_tx_buf[0], 5, yaw_rate_kp, yaw_rate_ki, yaw_rate_kd);
HAL_UART_Transmit(&huart1, &hc06_tx_buf[0], 20, 10);
}
}
void RX_PID_Gain_Request(void)
{
if(hc06_rx_cplt_flag == 1)
{
hc06_rx_cplt_flag = 0;
if(iBus.SwA == 1000)
{
unsigned char chksum = 0xff;
for(int i=0;i<19;i++) chksum = chksum - hc06_rx_buf[i];
if(chksum == hc06_rx_buf[19])
{
LL_GPIO_SetOutputPin(GPIOC, LL_GPIO_PIN_13);
HAL_Delay(100);
LL_GPIO_ResetOutputPin(GPIOC, LL_GPIO_PIN_13);
switch(hc06_rx_buf[2])
{
case 0:
roll_in_kp = *(float*)&hc06_rx_buf[3];
roll_in_ki = *(float*)&hc06_rx_buf[7];
roll_in_kd = *(float*)&hc06_rx_buf[11];
EP_PIDGain_Write(hc06_rx_buf[2], roll_in_kp,
roll_in_ki, roll_in_kd);
EP_PIDGain_Read(hc06_rx_buf[2], &roll_in_kp,
&roll_in_ki, &roll_in_kd);
Encode_PID_Gain(&hc06_tx_buf[0], hc06_rx_buf[2],
roll_in_kp, roll_in_ki, roll_in_kd);
HAL_UART_Transmit_IT(&huart1, &hc06_tx_buf[0], 20);
break;
case 1:
roll_out_kp = *(float*)&hc06_rx_buf[3];
roll_out_ki = *(float*)&hc06_rx_buf[7];
roll_out_kd = *(float*)&hc06_rx_buf[11];
EP_PIDGain_Write(hc06_rx_buf[2], roll_out_kp,
roll_out_ki, roll_out_kd);
EP_PIDGain_Read(hc06_rx_buf[2], &roll_out_kp,
&roll_out_ki, &roll_out_kd);
Encode_PID_Gain(&hc06_tx_buf[0], hc06_rx_buf[2],
roll_out_kp, roll_out_ki, roll_out_kd);
HAL_UART_Transmit_IT(&huart1, &hc06_tx_buf[0], 20);
break;
case 2:
pitch_in_kp = *(float*)&hc06_rx_buf[3];
pitch_in_ki = *(float*)&hc06_rx_buf[7];
pitch_in_kd = *(float*)&hc06_rx_buf[11];
EP_PIDGain_Write(hc06_rx_buf[2], pitch_in_kp,
pitch_in_ki, pitch_in_kd);
EP_PIDGain_Read(hc06_rx_buf[2], &pitch_in_kp,
&pitch_in_ki, &pitch_in_kd);
Encode_PID_Gain(&hc06_tx_buf[0], hc06_rx_buf[2],
pitch_in_kp, pitch_in_ki, pitch_in_kd);
HAL_UART_Transmit_IT(&huart1, &hc06_tx_buf[0], 20);
break;
case 3:
pitch_out_kp = *(float*)&hc06_rx_buf[3];
pitch_out_ki = *(float*)&hc06_rx_buf[7];
pitch_out_kd = *(float*)&hc06_rx_buf[11];
EP_PIDGain_Write(hc06_rx_buf[2], pitch_out_kp,
pitch_out_ki, pitch_out_kd);
EP_PIDGain_Read(hc06_rx_buf[2], &pitch_out_kp,
&pitch_out_ki, &pitch_out_kd);
Encode_PID_Gain(&hc06_tx_buf[0], hc06_rx_buf[2],
pitch_out_kp, pitch_out_ki, pitch_out_kd);
HAL_UART_Transmit_IT(&huart1, &hc06_tx_buf[0], 20);
break;
case 4:
yaw_heading_kp = *(float*)&hc06_rx_buf[3];
yaw_heading_ki = *(float*)&hc06_rx_buf[7];
yaw_heading_kd = *(float*)&hc06_rx_buf[11];
EP_PIDGain_Write(hc06_rx_buf[2], yaw_heading_kp,
yaw_heading_ki, yaw_heading_kd);
EP_PIDGain_Read(hc06_rx_buf[2], &yaw_heading_kp,
&yaw_heading_ki, &yaw_heading_kd);
Encode_PID_Gain(&hc06_tx_buf[0], hc06_rx_buf[2],
yaw_heading_kp, yaw_heading_ki, yaw_heading_kd);
HAL_UART_Transmit_IT(&huart1, &hc06_tx_buf[0], 20);
break;
case 5:
yaw_rate_kp = *(float*)&hc06_rx_buf[3];
yaw_rate_ki = *(float*)&hc06_rx_buf[7];
yaw_rate_kd = *(float*)&hc06_rx_buf[11];
EP_PIDGain_Write(hc06_rx_buf[2], yaw_rate_kp,
yaw_rate_ki, yaw_rate_kd);
EP_PIDGain_Read(hc06_rx_buf[2], &yaw_rate_kp,
&yaw_rate_ki, &yaw_rate_kd);
Encode_PID_Gain(&hc06_tx_buf[0], hc06_rx_buf[2],
yaw_rate_kp, yaw_rate_ki, yaw_rate_kd);
HAL_UART_Transmit_IT(&huart1, &hc06_tx_buf[0], 20);
break;
case 0x10:
switch(hc06_rx_buf[3])
{
case 0:
Encode_PID_Gain(&hc06_tx_buf[0],
hc06_rx_buf[3], roll_in_kp, roll_in_ki, roll_in_kd);
HAL_UART_Transmit(&huart1, &hc06_tx_buf[0],
20, 10);
break;
case 1:
Encode_PID_Gain(&hc06_tx_buf[0],
hc06_rx_buf[3], roll_out_kp, roll_out_ki, roll_out_kd);
HAL_UART_Transmit(&huart1, &hc06_tx_buf[0],
20, 10);
break;
case 2:
Encode_PID_Gain(&hc06_tx_buf[0],
hc06_rx_buf[3], pitch_in_kp, pitch_in_ki, pitch_in_kd);
HAL_UART_Transmit(&huart1, &hc06_tx_buf[0],
20, 10);
break;
case 3:
Encode_PID_Gain(&hc06_tx_buf[0],
hc06_rx_buf[3], pitch_out_kp, pitch_out_ki, pitch_out_kd);
HAL_UART_Transmit(&huart1, &hc06_tx_buf[0],
20, 10);
break;
case 4:
Encode_PID_Gain(&hc06_tx_buf[0],
hc06_rx_buf[3], yaw_heading_kp, yaw_heading_ki, yaw_heading_kd);
HAL_UART_Transmit(&huart1, &hc06_tx_buf[0],
20, 10);
break;
case 5:
Encode_PID_Gain(&hc06_tx_buf[0],
hc06_rx_buf[3], yaw_rate_kp, yaw_rate_ki, yaw_rate_kd);
HAL_UART_Transmit(&huart1, &hc06_tx_buf[0],
20, 10);
break;
case 6:
Encode_PID_Gain(&hc06_tx_buf[0], 0,
roll_in_kp, roll_in_ki, roll_in_kd);
HAL_UART_Transmit(&huart1, &hc06_tx_buf[0],
20, 10);
Encode_PID_Gain(&hc06_tx_buf[0], 1,
roll_out_kp, roll_out_ki, roll_out_kd);
HAL_UART_Transmit(&huart1, &hc06_tx_buf[0],
20, 10);
Encode_PID_Gain(&hc06_tx_buf[0], 2,
pitch_in_kp, pitch_in_ki, pitch_in_kd);
HAL_UART_Transmit(&huart1, &hc06_tx_buf[0],
20, 10);
Encode_PID_Gain(&hc06_tx_buf[0], 3,
pitch_out_kp, pitch_out_ki, pitch_out_kd);
HAL_UART_Transmit(&huart1, &hc06_tx_buf[0],
20, 10);
Encode_PID_Gain(&hc06_tx_buf[0], 4,
yaw_heading_kp, yaw_heading_ki, yaw_heading_kd);
HAL_UART_Transmit(&huart1, &hc06_tx_buf[0],
20, 10);
Encode_PID_Gain(&hc06_tx_buf[0], 5,
yaw_rate_kp, yaw_rate_ki, yaw_rate_kd);
HAL_UART_Transmit(&huart1, &hc06_tx_buf[0],
20, 10);
break;
}
break;
}
}
}
}
}
/**
* @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 */
__disable_irq();
while (1)
{
LL_GPIO_SetOutputPin(GPIOC, LL_GPIO_PIN_13);
HAL_Delay(100);
LL_GPIO_ResetOutputPin(GPIOC, LL_GPIO_PIN_13);
HAL_Delay(100);
}
/* 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,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */