B-G431B-ESC1: ADC reading of Temp, pot, VBUS while foc_current

Setup:
board = disco_b_g431b_esc1
framework = arduino
lib_archive = false
lib_deps =
GitHub - simplefoc/Arduino-FOC: Arduino FOC for BLDC and Stepper motors - Arduino Based Field Oriented Control Algorithm Library @ 2.2
build_flags =
-D PIO_FRAMEWORK_ARDUINO_NANOLIB_FLOAT_PRINTF
-D HAL_OPAMP_MODULE_ENABLED
-D HAL_TIM_MODULE_ENABLED

Motor control seems working. It can spin a lot faster than what it can with modes different to foc_current. That is good.

But I would like to read temperature, VBUS and pot. I cannot get it to work with analogRead. They just read 0. Using digitalRead I can see observe if the pot is all the way CCW or CV.

float Pot = (float)analogRead(A_POTENTIOMETER);
float PCBtemp = ((float)analogRead(A_TEMPERATURE) / 1024.0 * 3300.0 - 1055.0) / 22.7 + 25.0;
float VBUS = ((float)analogRead(A_VBUS) / 1024.0 * 3.3) / 18.0 * 187.0;

I have had it compile with different pulls of Simple FOC, where I could use analogRead, but then there would be no current feedback.

Thanks.

1 Like

Hi @HoeckDK ,

I’m not sure what’s going on there, but I have a hunch: the STM32G4 code for the ESC-1 board sets up the ADC in a specific way, and probably this interferes with using analogRead().

I don’t have a quick solution for you here. You can either play with the library code to (for example) add the analog inputs you need to what is read by the current sensing code, or figure out a way to use them in parallel.

If you’re willing to wait a few weeks to months, the ADC code in SimpleFOC is being refactored, so after this task is completed, then there may be a cleaner way to use the other ADC pins.

Sorry I can’t be more helpful on this!

Hi

I figured out that it would be the case. Analog read stops working after CurrentSensor.init(). When DMA is configured it dies.

I’ll try to look into configuring the other ADCs under DMA also. I guess I could be able to get hints from CubeIDE generated code?!

I also tried to configure the current sensor pins as pot, temp and VBUS, but that does not work. So I guess that something is hard coded for the actual current pins.

Thanks the reply.

So, some progress. However not tested with the motor working yet, but I get ADC readings parallel to current readings:

stm32g4_mcu.cpp:

#define ADC_BUF_LEN_1 5 (was 2)

float _readADCVoltageInline(const int pin){
uint32_t raw_adc = 0;
if (pin == PA2) // = ADC1_IN3 = phase U (OP1_OUT) on B-G431B-ESC1
raw_adc = adcBuffer1[1];
else if (pin == PA6) // = ADC2_IN3 = phase V (OP2_OUT) on B-G431B-ESC1
raw_adc = adcBuffer2[0];
else if (pin == PB1) // = ADC1_IN12 = phase W (OP3_OUT) on B-G431B-ESC1
raw_adc = adcBuffer1[0];
else if (pin == A_POTENTIOMETER)
raw_adc = adcBuffer1[2];
else if (pin == A_TEMPERATURE)
raw_adc = adcBuffer1[3];
else if (pin == A_VBUS)
raw_adc = adcBuffer1[4];

stm32g4_hal.cpp:

hadc1->Init.NbrOfConversion = 5; (was 2)

added at line 135:

/* USER CODE BEGIN ADC1_Init 2 /
/
* Configure Regular Channel (PB12, Potentiometer)
*/
sConfig.Channel = ADC_CHANNEL_11;
sConfig.Rank = ADC_REGULAR_RANK_3;
sConfig.SamplingTime = ADC_SAMPLETIME_47CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}

/* USER CODE BEGIN ADC1_Init 2 */

/** Configure Regular Channel (PB14, Temperature)
*/
sConfig.Channel = ADC_CHANNEL_5;
sConfig.Rank = ADC_REGULAR_RANK_4;
sConfig.SamplingTime = ADC_SAMPLETIME_47CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}

/* USER CODE BEGIN ADC1_Init 2 */

/** Configure Regular Channel (PB14, Temperature)
*/
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = ADC_REGULAR_RANK_5;
sConfig.SamplingTime = ADC_SAMPLETIME_47CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}

Tomorrow: test if it breaks current readings…

2 Likes

Just to confirm, that I also implemented your changes on top of V2.21 and they seem to work just fine! Thanks a lot, @ HoeckDK this helped me a tremedously!

  • Chris