From 15d55529a8c5773bde6d1a7c61b3d033dfb3c414 Mon Sep 17 00:00:00 2001 From: zurizadae Date: Sat, 1 Mar 2025 15:44:20 -0500 Subject: [PATCH 1/2] Changes for the ADC voltage reader --- .../soccer_firmware/Core/Inc/main.h | 19 +- .../soccer_firmware/Core/Inc/update_loop.h | 1 + .../soccer_firmware/Core/Src/main.c | 227 +++++++++++++++++- .../soccer_firmware/Core/Src/update_loop.c | 71 ++++++ 4 files changed, 312 insertions(+), 6 deletions(-) diff --git a/soccer_hardware/soccer_firmware/Core/Inc/main.h b/soccer_hardware/soccer_firmware/Core/Inc/main.h index 206fa2125..21225fd2b 100644 --- a/soccer_hardware/soccer_firmware/Core/Inc/main.h +++ b/soccer_hardware/soccer_firmware/Core/Inc/main.h @@ -34,6 +34,7 @@ extern "C" { #include extern I2C_HandleTypeDef hi2c1; +extern ADC_HandleTypeDef hadc; extern uint8_t usb_received; extern uint32_t usbRxBufferCount; @@ -46,7 +47,7 @@ typedef struct { DMA_HandleTypeDef *hdma_uart_rx; GPIO_TypeDef *pinPort; uint16_t dirPinNum; // pin for setting buffer read/write direction - + //ADC_HandleTypeDef *hadc; uint8_t rxPacketLen; // received packet length should be smaller then max buffer size uint8_t rxBuffer[100]; // 100bytes is enough to store packets from Dynamixel bool dmaDoneReading; @@ -62,9 +63,21 @@ typedef struct { uint16_t currMotorPositions[10]; } MotorPort; +typedef struct { + ADC_HandleTypeDef *hadc; // ADC configuration for pins + uint16_t readValue; + float v_read; //IN 8 + //float v_read;//IN 7 + float intensity; // intensity +}Voltage; + extern MotorPort* motorPorts[6]; extern MotorPort port1, port2, port3, port4, port5, port6; +extern Voltage* voltage[2]; +extern Voltage adc_1,adc_2; + + typedef enum { BUFFER_READ = 0, @@ -102,6 +115,10 @@ void Error_Handler(void); #define USART4_DIR_GPIO_Port GPIOC #define USART2_DIR_Pin GPIO_PIN_4 #define USART2_DIR_GPIO_Port GPIOA +#define V_shunt_read_Pin GPIO_PIN_7 +#define V_shunt_read_GPIO_Port GPIOA +#define V_bat_read_Pin GPIO_PIN_0 +#define V_bat_read_GPIO_Port GPIOB #define USART3_DIR_Pin GPIO_PIN_2 #define USART3_DIR_GPIO_Port GPIOB #define USART6_DIR_Pin GPIO_PIN_15 diff --git a/soccer_hardware/soccer_firmware/Core/Inc/update_loop.h b/soccer_hardware/soccer_firmware/Core/Inc/update_loop.h index 8bab0089d..6bfd259fc 100644 --- a/soccer_hardware/soccer_firmware/Core/Inc/update_loop.h +++ b/soccer_hardware/soccer_firmware/Core/Inc/update_loop.h @@ -11,6 +11,7 @@ void update(void); void command_motors(void); void read_imu(uint8_t *rxBuf); void read_motors(uint8_t *rxBuf); +void update_voltage(void); #endif /* INC_UPDATE_LOOP_H_ */ diff --git a/soccer_hardware/soccer_firmware/Core/Src/main.c b/soccer_hardware/soccer_firmware/Core/Src/main.c index 9914f693c..c19b2d30e 100644 --- a/soccer_hardware/soccer_firmware/Core/Src/main.c +++ b/soccer_hardware/soccer_firmware/Core/Src/main.c @@ -23,11 +23,15 @@ /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "BMI088.h" +#include "stm32f4xx_hal.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ +// LCD commands + + /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ @@ -41,6 +45,9 @@ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ +ADC_HandleTypeDef hadc1; +ADC_HandleTypeDef hadc2; + I2C_HandleTypeDef hi2c1; UART_HandleTypeDef huart4; @@ -70,6 +77,17 @@ uint8_t usbRxBuffer[100]; MotorPort* motorPorts[6]; MotorPort port1, port2, port3, port4, port5, port6; + +Voltage* voltage[2]; +Voltage adc_1,adc_2; + +//ZSM STUFF*/ +uint16_t readValue; +float v_bat_read; //IN 8 +float v_shunt_read;//IN 7 +float intensity_shunt; // intensity + + /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ @@ -83,6 +101,8 @@ static void MX_USART2_UART_Init(void); static void MX_USART3_UART_Init(void); static void MX_USART6_UART_Init(void); static void MX_I2C1_Init(void); +static void MX_ADC1_Init(void); +static void MX_ADC2_Init(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ @@ -90,6 +110,25 @@ static void MX_I2C1_Init(void); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ // test various functionalities +//ZSM stuff +void init_voltage(){ + adc_1 = (Voltage){ + .hadc = &hadc1, + .readValue = 0, + .v_read = 0, + .intensity = 0 // intensity + }; + adc_2 = (Voltage){ + .hadc = &hadc2, + .readValue = 0, + .v_read = 0, + .intensity = 0 // intensity + }; + + voltage[0] = &adc_1; + voltage[1] = &adc_2; +} + void init_ports() { // port1 => UART1 ==> J2 on new PCB port1 = (MotorPort){ @@ -207,7 +246,6 @@ void init_motors() { // slowly bring motors to zero position } } - /* USER CODE END 0 */ /** @@ -247,6 +285,8 @@ int main(void) MX_USART3_UART_Init(); MX_USART6_UART_Init(); MX_I2C1_Init(); + MX_ADC1_Init(); + MX_ADC2_Init(); /* USER CODE BEGIN 2 */ // give some time for motors to power on and initialize, before we try to talk to them HAL_Delay(1000); @@ -258,10 +298,14 @@ int main(void) BMI088 imu; BMI088_Init(&imu, &hi2c1); + HAL_ADC_Start(&hadc1); //IN8 + HAL_ADC_Start(&hadc2); // IN7 + // uint16_t angle = 0; // uint16_t angle_lo = 0; // uint16_t angle_hi = 0; + /* USER CODE END 2 */ /* Infinite loop */ @@ -269,7 +313,7 @@ int main(void) while (1) { - update(); + //update(); // angle += 1; // angle %= 0x3FF; @@ -328,14 +372,66 @@ int main(void) // test Dynamixel 1.0 // test_motor_sweep1(&port2, 13); // _motor_ping_p1(&port6, 13); -// HAL_GPIO_TogglePin(GPIOA, GREEN_LED_Pin); -// HAL_Delay(50); + HAL_GPIO_TogglePin(GPIOA, GREEN_LED_Pin); + HAL_Delay(50); + + /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ + + + /* //while (1) + //{ + //ZSM CODE + // HAL_ADC_Start(&hadc1); + hadc1.Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;// |= allows the continuous display + + //Commands the start of the continuous display + //ZSM stuff + // HAL_ADC_PollForConversion(&hadc1,1000); // The poll takes the value but does not allow to see the continuous variable change + readValue = HAL_ADC_GetValue(&hadc1); // Reads the value from the ADC + v_bat_read = (float)readValue/4095*3.3;//3.3; //4095*16.5; // values toogle between each other between + + //HAL_Delay(100); // time between each change + + hadc2.Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;// |= allows the continuous display + + readValue = HAL_ADC_GetValue(&hadc2); // Reads the value from the ADC + v_shunt_read= (float)readValue/4095*3.3; + + intensity_shunt = (float)v_shunt_read/(20)/0.01; //3.3; //4095*16.5; + HAL_Delay(100); // time between each change + //}*/ + //while(1){ + update_voltage(); + v_bat_read = voltage[0]->v_read; //IN 8 + if (isnan(v_bat_read)){ + printf("error from v_bat_read"); + exit(1); + } + v_shunt_read = voltage[1]->v_read; //IN 7 + + if (isnan(v_shunt_read)){ + printf("error from v_shunt_read"); + exit(1); + } + intensity_shunt = voltage[1]->intensity;// intensity + if(isnan(intensity_shunt)){ + printf("error from v_bat_read"); + exit(1); + } + // printf("v_bat_read: %f, v_shunt_read: %f, intensity_shunt: %f\n", v_bat_read, v_shunt_read, intensity_shunt); + + // }*/ + + + } + } + /* USER CODE END 3 */ -} + /** * @brief System Clock Configuration @@ -383,6 +479,110 @@ void SystemClock_Config(void) } } +/** + * @brief ADC1 Initialization Function + * @param None + * @retval None + */ +static void MX_ADC1_Init(void) +{ + + /* USER CODE BEGIN ADC1_Init 0 */ + + /* USER CODE END ADC1_Init 0 */ + + ADC_ChannelConfTypeDef sConfig = {0}; + + /* USER CODE BEGIN ADC1_Init 1 */ + + /* USER CODE END ADC1_Init 1 */ + + /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) + */ + hadc1.Instance = ADC1; + hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2; + hadc1.Init.Resolution = ADC_RESOLUTION_12B; + hadc1.Init.ScanConvMode = DISABLE; + hadc1.Init.ContinuousConvMode = DISABLE; + hadc1.Init.DiscontinuousConvMode = DISABLE; + hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; + hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc1.Init.NbrOfConversion = 1; + hadc1.Init.DMAContinuousRequests = DISABLE; + hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + if (HAL_ADC_Init(&hadc1) != HAL_OK) + { + Error_Handler(); + } + + /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. + */ + sConfig.Channel = ADC_CHANNEL_8; + sConfig.Rank = 1; + sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN ADC1_Init 2 */ + + /* USER CODE END ADC1_Init 2 */ + +} + +/** + * @brief ADC2 Initialization Function + * @param None + * @retval None + */ +static void MX_ADC2_Init(void) +{ + + /* USER CODE BEGIN ADC2_Init 0 */ + + /* USER CODE END ADC2_Init 0 */ + + ADC_ChannelConfTypeDef sConfig = {0}; + + /* USER CODE BEGIN ADC2_Init 1 */ + + /* USER CODE END ADC2_Init 1 */ + + /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) + */ + hadc2.Instance = ADC2; + hadc2.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2; + hadc2.Init.Resolution = ADC_RESOLUTION_12B; + hadc2.Init.ScanConvMode = DISABLE; + hadc2.Init.ContinuousConvMode = DISABLE; + hadc2.Init.DiscontinuousConvMode = DISABLE; + hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START; + hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc2.Init.NbrOfConversion = 1; + hadc2.Init.DMAContinuousRequests = DISABLE; + hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + if (HAL_ADC_Init(&hadc2) != HAL_OK) + { + Error_Handler(); + } + + /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. + */ + sConfig.Channel = ADC_CHANNEL_7; + sConfig.Rank = 1; + sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; + if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN ADC2_Init 2 */ + + /* USER CODE END ADC2_Init 2 */ + +} + /** * @brief I2C1 Initialization Function * @param None @@ -713,12 +913,29 @@ static void MX_GPIO_Init(void) GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + /*Configure GPIO pin : PB14 */ + GPIO_InitStruct.Pin = GPIO_PIN_14; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF15_EVENTOUT; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /*Configure GPIO pin : PC9 */ + GPIO_InitStruct.Pin = GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF9_QSPI; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + /* USER CODE BEGIN MX_GPIO_Init_2 */ /* USER CODE END MX_GPIO_Init_2 */ } /* USER CODE BEGIN 4 */ + /* USER CODE END 4 */ /** diff --git a/soccer_hardware/soccer_firmware/Core/Src/update_loop.c b/soccer_hardware/soccer_firmware/Core/Src/update_loop.c index da5f78108..ffe2e70f4 100644 --- a/soccer_hardware/soccer_firmware/Core/Src/update_loop.c +++ b/soccer_hardware/soccer_firmware/Core/Src/update_loop.c @@ -249,3 +249,74 @@ void read_motors(uint8_t *rxBuf) { if(numMotorsReceived == numMotorsRequested) return; // all motors serviced, peace out } } +//ZSM code +void update_voltage(void){ + Voltage* v_19; + v_19 = &voltage[0]; + + Voltage* v_13 = voltage[0]; + Voltage *v_1 = &voltage[0]; + Voltage *v_2 = &voltage[1]; + + if(HAL_ADC_Init(v_1->hadc) != HAL_OK){ + printf("error from initialization v_1"); + exit(1); + } + + if(HAL_ADC_Init(v_2->hadc) != HAL_OK){ + printf("error from initialization v_2"); + exit(1); + } + + v_1->hadc->Instance->CR2 |= (uint32_t)ADC_CR2_CONT; + v_2->hadc->Instance->CR2 |= (uint32_t)ADC_CR2_CONT; + uint16_t readValue_1; + uint16_t readValue_2; + + //uint16_t hadHAL_ADC_GetValue(ADC_HandleTypeDef* hadc); + //ADC_HandleTypeDef hadc1 = v_1-> hadc; + // #uint32_t hadc1 = v_1-> hadc; + //HAL_ADC_Start(&hadc1); //IN8 + //HAL_ADC_Start(&hadc2); // IN7 + + float v_bat_read; //IN 8 + float v_shunt_read;// = v_2->v_read; //IN 7 + float intensity_shunt;// = v_2->intensity;// intensity + + while(1){ + // HAL_ADC_Start(&hadc1); + (*v_1->hadc).Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;// |= allows the continuous display + + //Commands the start of the continuous display + //ZSM stuff + // HAL_ADC_PollForConversion(&hadc1,1000); // The poll takes the value but does not allow to see the continuous variable change + readValue_1 = HAL_ADC_GetValue(&(*v_1->hadc)); // Reads the value from the ADC + v_bat_read = (float)readValue_1 / 4095 * 3.3; + + //v_1 -> v_read = 3; + //v_bat_read = 3; + //(float)readValue_1/4095*3.3;//3.3; //4095*16.5; // values toogle between each other between + //printf('%f',v_bat_read); + // = v_bat_read; + //printf('%f', v_1->v_read); + //HAL_Delay(100); // time between each change + + (*v_2->hadc).Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;// |= allows the continuous display + readValue_2 = HAL_ADC_GetValue(&(*v_2->hadc)); + v_shunt_read = (float)readValue_2 / 4095 * 3.3; + intensity_shunt = (float)v_shunt_read / (20) / 0.01; + + //readValue_2 = HAL_ADC_GetValue(&(*v_2->hadc)); // Reads the value from the ADC + // v_shunt_read= (float)readValue_2/4095*3.3; + //v_1->v_read = v_shunt_read; + + v_1->v_read = v_bat_read; + v_2->v_read = v_shunt_read; + v_2->intensity = intensity_shunt; + + + + HAL_Delay(100); // time between each change +} + // return v_bat_read, v_shunt_read, intensity_shunt; +} From e87ad01fd3ec009b97ff88ab4e2fa41853bcc614 Mon Sep 17 00:00:00 2001 From: zurizadae Date: Sat, 5 Apr 2025 16:06:35 -0400 Subject: [PATCH 2/2] Fix bugs after entering the update_loop --- soccer_hardware/.project | 11 + .../com.st.stm32cube.ide.mcu.sfrview.prefs | 2 + soccer_hardware/soccer_firmware/.cproject | 22 +- soccer_hardware/soccer_firmware/.mxproject | 47 +- .../.settings/language.settings.xml | 8 +- .../.settings/stm32cubeide.project.prefs | 4 +- .../Core/Inc/stm32f4xx_hal_conf.h | 2 +- .../soccer_firmware/Core/Src/BMI088.c | 2 +- .../soccer_firmware/Core/Src/dynamixel_p1.c | 1 - .../soccer_firmware/Core/Src/main.c | 39 +- .../Core/Src/stm32f4xx_hal_msp.c | 101 + .../soccer_firmware/Core/Src/update_loop.c | 66 +- .../Inc/stm32f4xx_hal_adc.h | 898 ++++ .../Inc/stm32f4xx_hal_adc_ex.h | 407 ++ .../Inc/stm32f4xx_ll_adc.h | 4779 +++++++++++++++++ .../Src/stm32f4xx_hal_adc.c | 2110 ++++++++ .../Src/stm32f4xx_hal_adc_ex.c | 1112 ++++ .../Src/stm32f4xx_ll_adc.c | 922 ++++ .../soccer_firmware/STM32F446RCTX_FLASH.ld | 18 +- .../soccer_firmware Debug.launch | 2 +- .../soccer_firmware/soccer_firmware.ioc | 117 +- .../soccer_firmware_interface/CMakeLists.txt | 0 .../config/bez2.yaml | 0 .../config/motor_types.yaml | 0 .../launch/sensors.launch | 0 .../soccer_firmware_interface/package.xml | 0 .../soccer_firmware_interface/setup.py | 0 .../src/soccer_firmware_interface/__init__.py | 0 .../firmware_interface.py | 0 .../src/soccer_firmware_interface/main.py | 0 .../test/test_firmware_interface.py | 0 31 files changed, 10506 insertions(+), 164 deletions(-) create mode 100644 soccer_hardware/.project create mode 100644 soccer_hardware/.settings/com.st.stm32cube.ide.mcu.sfrview.prefs create mode 100644 soccer_hardware/soccer_firmware/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc.h create mode 100644 soccer_hardware/soccer_firmware/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc_ex.h create mode 100644 soccer_hardware/soccer_firmware/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_adc.h create mode 100644 soccer_hardware/soccer_firmware/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c create mode 100644 soccer_hardware/soccer_firmware/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c create mode 100644 soccer_hardware/soccer_firmware/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_adc.c rename soccer_hardware/{ => soccer_firmware}/soccer_firmware_interface/CMakeLists.txt (100%) rename soccer_hardware/{ => soccer_firmware}/soccer_firmware_interface/config/bez2.yaml (100%) rename soccer_hardware/{ => soccer_firmware}/soccer_firmware_interface/config/motor_types.yaml (100%) rename soccer_hardware/{ => soccer_firmware}/soccer_firmware_interface/launch/sensors.launch (100%) rename soccer_hardware/{ => soccer_firmware}/soccer_firmware_interface/package.xml (100%) rename soccer_hardware/{ => soccer_firmware}/soccer_firmware_interface/setup.py (100%) rename soccer_hardware/{ => soccer_firmware}/soccer_firmware_interface/src/soccer_firmware_interface/__init__.py (100%) rename soccer_hardware/{ => soccer_firmware}/soccer_firmware_interface/src/soccer_firmware_interface/firmware_interface.py (100%) rename soccer_hardware/{ => soccer_firmware}/soccer_firmware_interface/src/soccer_firmware_interface/main.py (100%) mode change 100755 => 100644 rename soccer_hardware/{ => soccer_firmware}/soccer_firmware_interface/test/test_firmware_interface.py (100%) diff --git a/soccer_hardware/.project b/soccer_hardware/.project new file mode 100644 index 000000000..ef389a004 --- /dev/null +++ b/soccer_hardware/.project @@ -0,0 +1,11 @@ + + + soccer_hardware + + + + + + + + diff --git a/soccer_hardware/.settings/com.st.stm32cube.ide.mcu.sfrview.prefs b/soccer_hardware/.settings/com.st.stm32cube.ide.mcu.sfrview.prefs new file mode 100644 index 000000000..98a69fc76 --- /dev/null +++ b/soccer_hardware/.settings/com.st.stm32cube.ide.mcu.sfrview.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +sfrviewstate={"fFavorites"\:{"fLists"\:{}},"fProperties"\:{"fNodeProperties"\:{}}} diff --git a/soccer_hardware/soccer_firmware/.cproject b/soccer_hardware/soccer_firmware/.cproject index d341b2e02..ef3088599 100644 --- a/soccer_hardware/soccer_firmware/.cproject +++ b/soccer_hardware/soccer_firmware/.cproject @@ -23,7 +23,7 @@