mirror of
https://github.com/EFeru/hoverboard-sideboard-hack-STM.git
synced 2026-02-18 07:51:15 +00:00
Added Hovercar variant
- hovercar variant - iBUS receiver support - updated baud rate to 115200
This commit is contained in:
127
Src/main.c
127
Src/main.c
@@ -18,7 +18,6 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
#include "dma.h"
|
||||
@@ -66,25 +65,9 @@ void SystemClock_Config(void);
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
extern UART_HandleTypeDef huart2;
|
||||
|
||||
#ifdef SERIAL_CONTROL
|
||||
extern SerialSideboard Sideboard;
|
||||
#endif
|
||||
uint32_t main_loop_counter; // main loop counter to perform task scheduling inside main()
|
||||
|
||||
#ifdef SERIAL_FEEDBACK
|
||||
extern SerialFeedback Feedback;
|
||||
extern uint16_t timeoutCntSerial; // Timeout counter for Rx Serial command
|
||||
extern uint8_t timeoutFlagSerial; // Timeout Flag for Rx Serial command: 0 = OK, 1 = Problem detected (line disconnected or wrong Rx data)
|
||||
#endif
|
||||
|
||||
extern MPU_Data mpu; // holds the MPU-6050 data
|
||||
extern ErrorStatus mpuStatus; // holds the MPU-6050 status: SUCCESS or ERROR
|
||||
|
||||
GPIO_PinState sensor1, sensor2; // holds the sensor1 and sensor 2 values
|
||||
GPIO_PinState sensor1_read, sensor2_read; // holds the instantaneous Read for sensor1 and sensor 2
|
||||
|
||||
static uint32_t main_loop_counter; // main loop counter to perform task squeduling inside main()
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
@@ -118,108 +101,25 @@ int main(void)
|
||||
MX_DMA_Init();
|
||||
MX_USART2_UART_Init();
|
||||
MX_I2C1_Init();
|
||||
MX_USART1_UART_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
input_init(); // Input initialization
|
||||
input_init(); // Input initialization
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
{
|
||||
|
||||
HAL_Delay(DELAY_IN_MAIN_LOOP);
|
||||
|
||||
// ==================================== LEDs Handling ====================================
|
||||
// HAL_GPIO_TogglePin(LED4_GPIO_Port, LED4_Pin); // Toggle BLUE1 LED
|
||||
#ifdef SERIAL_FEEDBACK
|
||||
if (!timeoutFlagSerial) {
|
||||
if (Feedback.cmdLed & LED1_SET) { HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET); } else { HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET); }
|
||||
if (Feedback.cmdLed & LED2_SET) { HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET); } else { HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_RESET); }
|
||||
if (Feedback.cmdLed & LED3_SET) { HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, GPIO_PIN_SET); } else { HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, GPIO_PIN_RESET); }
|
||||
if (Feedback.cmdLed & LED4_SET) { HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, GPIO_PIN_SET); } else { HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, GPIO_PIN_RESET); }
|
||||
if (Feedback.cmdLed & LED5_SET) { HAL_GPIO_WritePin(LED5_GPIO_Port, LED5_Pin, GPIO_PIN_SET); } else { HAL_GPIO_WritePin(LED5_GPIO_Port, LED5_Pin, GPIO_PIN_RESET); }
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
// ==================================== MPU-6050 Handling ====================================
|
||||
#if defined(MPU_SENSOR_ENABLE) && defined(SERIAL_DEBUG)
|
||||
// Get MPU data. Because the MPU-6050 interrupt pin is not wired we have to check DMP data by pooling periodically
|
||||
if (SUCCESS == mpuStatus) {
|
||||
mpu_get_data();
|
||||
} else if (ERROR == mpuStatus && main_loop_counter % 100 == 0) {
|
||||
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin); // Toggle the Red LED every 100 ms
|
||||
}
|
||||
// Print MPU data to Console
|
||||
if (main_loop_counter % 50 == 0) {
|
||||
mpu_print_to_console();
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
// ==================================== SENSORS Handling ====================================
|
||||
sensor1_read = HAL_GPIO_ReadPin(SENSOR1_GPIO_Port, SENSOR1_Pin);
|
||||
sensor2_read = HAL_GPIO_ReadPin(SENSOR2_GPIO_Port, SENSOR2_Pin);
|
||||
|
||||
// SENSOR1
|
||||
if (sensor1 == GPIO_PIN_RESET && sensor1_read == GPIO_PIN_SET) {
|
||||
sensor1 = GPIO_PIN_SET;
|
||||
// Sensor ACTIVE: Do something here (one time task on activation)
|
||||
HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, GPIO_PIN_SET);
|
||||
consoleLog("-- SENSOR 1 Active --\n");
|
||||
} else if(sensor1 == GPIO_PIN_SET && sensor1_read == GPIO_PIN_RESET) {
|
||||
// Sensor DEACTIVE: Do something here (one time task on deactivation)
|
||||
sensor1 = GPIO_PIN_RESET;
|
||||
HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, GPIO_PIN_RESET);
|
||||
consoleLog("-- SENSOR 1 Deactive --\n");
|
||||
}
|
||||
|
||||
// SENSOR2
|
||||
if (sensor2 == GPIO_PIN_RESET && sensor2_read == GPIO_PIN_SET) {
|
||||
sensor2 = GPIO_PIN_SET;
|
||||
// Sensor ACTIVE: Do something here (one time task on activation)
|
||||
HAL_GPIO_WritePin(LED5_GPIO_Port, LED5_Pin, GPIO_PIN_SET);
|
||||
consoleLog("-- SENSOR 2 Active --\n");
|
||||
} else if (sensor2 == GPIO_PIN_SET && sensor2_read == GPIO_PIN_RESET) {
|
||||
// Sensor DEACTIVE: Do something here (one time task on deactivation)
|
||||
sensor2 = GPIO_PIN_RESET;
|
||||
HAL_GPIO_WritePin(LED5_GPIO_Port, LED5_Pin, GPIO_PIN_RESET);
|
||||
consoleLog("-- SENSOR 2 Deactive --\n");
|
||||
}
|
||||
|
||||
if (sensor1 == GPIO_PIN_SET) {
|
||||
// Sensor ACTIVE: Do something here (continuous task)
|
||||
}
|
||||
if (sensor2 == GPIO_PIN_SET) {
|
||||
// Sensor ACTIVE: Do something here (continuous task)
|
||||
}
|
||||
|
||||
|
||||
// ==================================== SERIAL Tx/Rx Handling ====================================
|
||||
#ifdef SERIAL_CONTROL
|
||||
if (main_loop_counter % 5 == 0 && __HAL_DMA_GET_COUNTER(huart2.hdmatx) == 0) { // Check if DMA channel counter is 0 (meaning all data has been transferred)
|
||||
Sideboard.start = (uint16_t)SERIAL_START_FRAME;
|
||||
Sideboard.roll = (int16_t)mpu.euler.roll;
|
||||
Sideboard.pitch = (int16_t)mpu.euler.pitch;
|
||||
Sideboard.yaw = (int16_t)mpu.euler.yaw;
|
||||
Sideboard.sensors = (uint16_t)(sensor1 | (sensor2 << 1) | (mpuStatus << 2));
|
||||
Sideboard.checksum = (uint16_t)(Sideboard.start ^ Sideboard.roll ^ Sideboard.pitch ^ Sideboard.yaw ^ Sideboard.sensors);
|
||||
|
||||
HAL_UART_Transmit_DMA(&huart2, (uint8_t *)&Sideboard, sizeof(Sideboard));
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SERIAL_FEEDBACK
|
||||
if (timeoutCntSerial++ >= SERIAL_TIMEOUT) { // Timeout qualification
|
||||
timeoutFlagSerial = 1; // Timeout detected
|
||||
timeoutCntSerial = SERIAL_TIMEOUT; // Limit timout counter value
|
||||
}
|
||||
if (timeoutFlagSerial && main_loop_counter % 100 == 0) { // In case of timeout bring the system to a Safe State and indicate error if desired
|
||||
HAL_GPIO_TogglePin(LED3_GPIO_Port, LED3_Pin); // Toggle the Yellow LED every 100 ms
|
||||
}
|
||||
#endif
|
||||
handle_mpu6050(); // Handle of the MPU-6050 IMU sensor
|
||||
handle_sensors(); // Handle of the optical sensors
|
||||
handle_usart(); // Handle of the USART data
|
||||
handle_leds(); // Handle of the sideboard LEDs
|
||||
|
||||
main_loop_counter++;
|
||||
|
||||
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
@@ -236,7 +136,8 @@ void SystemClock_Config(void)
|
||||
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
||||
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
|
||||
|
||||
/** Initializes the CPU, AHB and APB busses clocks
|
||||
/** Initializes the RCC Oscillators according to the specified parameters
|
||||
* in the RCC_OscInitTypeDef structure.
|
||||
*/
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
|
||||
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
||||
@@ -248,7 +149,7 @@ void SystemClock_Config(void)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/** Initializes the CPU, AHB and APB busses clocks
|
||||
/** Initializes the CPU, AHB and APB buses clocks
|
||||
*/
|
||||
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|
||||
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
|
||||
@@ -288,7 +189,7 @@ void Error_Handler(void)
|
||||
* @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) */
|
||||
|
||||
Reference in New Issue
Block a user