Skip to content

Commit

Permalink
Differential adc hotfix (#1245)
Browse files Browse the repository at this point in the history
### Changelist 
<!-- Give a list of the changes covered in this PR. This will help both
you and the reviewer keep this PR within scope. -->
Changed differential ADC on the cube to two single-ended ADCs. 

Accuracy of +/- 8 volts.

### Testing Done
<!-- Outline the testing that was done to demonstrate the changes are
solid. This could be unit tests, integration tests, testing on the car,
etc. Include relevant code snippets, screenshots, etc as needed. -->

### Resolved Tickets
<!-- Link any tickets that this PR resolves. -->
  • Loading branch information
AmirTajaddodi authored May 4, 2024
1 parent ba64330 commit a682af4
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 25 deletions.
3 changes: 2 additions & 1 deletion firmware/chimera/proto/BMS.proto
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ enum AdcNetName {
AUX_TSENSE = 1;
TS_ISENSE_400A = 2;
TS_ISENSE_50A = 3;
TS_VSENSE = 4;
TS_VSENSE_P = 4;
TS_VSENSE_N = 5;
}
22 changes: 13 additions & 9 deletions firmware/quadruna/BMS/src/cubemx/BMS.ioc
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,30 @@ ADC1.Channel-0\#ChannelRegularConversion=ADC_CHANNEL_4
ADC1.Channel-1\#ChannelRegularConversion=ADC_CHANNEL_5
ADC1.Channel-2\#ChannelRegularConversion=ADC_CHANNEL_9
ADC1.Channel-3\#ChannelRegularConversion=ADC_CHANNEL_10
ADC1.Channel-4\#ChannelRegularConversion=ADC_CHANNEL_11
ADC1.ClockPrescaler=ADC_CLOCK_SYNC_PCLK_DIV4
ADC1.ConversionDataManagement=ADC_CONVERSIONDATA_DMA_CIRCULAR
ADC1.ExternalTrigConv=ADC_EXTERNALTRIG_T3_TRGO
ADC1.IPParameters=Rank-0\#ChannelRegularConversion,Channel-0\#ChannelRegularConversion,SamplingTime-0\#ChannelRegularConversion,OffsetNumber-0\#ChannelRegularConversion,OffsetSignedSaturation-0\#ChannelRegularConversion,NbrOfConversionFlag,ClockPrescaler,master,Resolution,ConversionDataManagement,ExternalTrigConv,Rank-1\#ChannelRegularConversion,Channel-1\#ChannelRegularConversion,SamplingTime-1\#ChannelRegularConversion,OffsetNumber-1\#ChannelRegularConversion,Rank-2\#ChannelRegularConversion,Channel-2\#ChannelRegularConversion,SamplingTime-2\#ChannelRegularConversion,OffsetNumber-2\#ChannelRegularConversion,Rank-3\#ChannelRegularConversion,Channel-3\#ChannelRegularConversion,SamplingTime-3\#ChannelRegularConversion,OffsetNumber-3\#ChannelRegularConversion,NbrOfConversion,SingleDiff-3\#ChannelRegularConversion
ADC1.NbrOfConversion=4
ADC1.IPParameters=Rank-0\#ChannelRegularConversion,Channel-0\#ChannelRegularConversion,SamplingTime-0\#ChannelRegularConversion,OffsetNumber-0\#ChannelRegularConversion,OffsetSignedSaturation-0\#ChannelRegularConversion,NbrOfConversionFlag,ClockPrescaler,Resolution,ConversionDataManagement,ExternalTrigConv,Rank-1\#ChannelRegularConversion,Channel-1\#ChannelRegularConversion,SamplingTime-1\#ChannelRegularConversion,OffsetNumber-1\#ChannelRegularConversion,Rank-2\#ChannelRegularConversion,Channel-2\#ChannelRegularConversion,SamplingTime-2\#ChannelRegularConversion,OffsetNumber-2\#ChannelRegularConversion,Rank-3\#ChannelRegularConversion,Channel-3\#ChannelRegularConversion,SamplingTime-3\#ChannelRegularConversion,OffsetNumber-3\#ChannelRegularConversion,NbrOfConversion,master,Rank-4\#ChannelRegularConversion,Channel-4\#ChannelRegularConversion,SamplingTime-4\#ChannelRegularConversion,OffsetNumber-4\#ChannelRegularConversion
ADC1.NbrOfConversion=5
ADC1.NbrOfConversionFlag=1
ADC1.OffsetNumber-0\#ChannelRegularConversion=ADC_OFFSET_NONE
ADC1.OffsetNumber-1\#ChannelRegularConversion=ADC_OFFSET_NONE
ADC1.OffsetNumber-2\#ChannelRegularConversion=ADC_OFFSET_NONE
ADC1.OffsetNumber-3\#ChannelRegularConversion=ADC_OFFSET_NONE
ADC1.OffsetNumber-4\#ChannelRegularConversion=ADC_OFFSET_NONE
ADC1.OffsetSignedSaturation-0\#ChannelRegularConversion=DISABLE
ADC1.Rank-0\#ChannelRegularConversion=1
ADC1.Rank-1\#ChannelRegularConversion=2
ADC1.Rank-2\#ChannelRegularConversion=3
ADC1.Rank-3\#ChannelRegularConversion=4
ADC1.Rank-4\#ChannelRegularConversion=5
ADC1.Resolution=ADC_RESOLUTION_12B
ADC1.SamplingTime-0\#ChannelRegularConversion=ADC_SAMPLETIME_1CYCLE_5
ADC1.SamplingTime-1\#ChannelRegularConversion=ADC_SAMPLETIME_1CYCLE_5
ADC1.SamplingTime-2\#ChannelRegularConversion=ADC_SAMPLETIME_1CYCLE_5
ADC1.SamplingTime-3\#ChannelRegularConversion=ADC_SAMPLETIME_2CYCLES_5
ADC1.SingleDiff-3\#ChannelRegularConversion=ADC_DIFFERENTIAL_ENDED
ADC1.SamplingTime-3\#ChannelRegularConversion=ADC_SAMPLETIME_16CYCLES_5
ADC1.SamplingTime-4\#ChannelRegularConversion=ADC_SAMPLETIME_16CYCLES_5
ADC1.master=1
CAD.formats=[]
CAD.pinconfig=Project naming
Expand Down Expand Up @@ -262,7 +265,7 @@ PC0.Signal=ADCx_INP10
PC1.GPIOParameters=GPIO_Label
PC1.GPIO_Label=TS_VSENSE_N
PC1.Locked=true
PC1.Signal=ADCx_INN10
PC1.Signal=SharedAnalog_PC1
PC13.GPIOParameters=GPIO_PuPd,GPIO_Label
PC13.GPIO_Label=nPROGRAM_3V3
PC13.GPIO_PuPd=GPIO_PULLUP
Expand Down Expand Up @@ -352,7 +355,7 @@ ProjectManager.FreePins=false
ProjectManager.HalAssertFull=false
ProjectManager.HeapSize=0x200
ProjectManager.KeepUserCode=true
ProjectManager.LastFirmware=true
ProjectManager.LastFirmware=false
ProjectManager.LibraryCopy=1
ProjectManager.MainLocation=Core/Src
ProjectManager.NoMain=false
Expand Down Expand Up @@ -453,9 +456,7 @@ RCC.VCO3OutputFreq_Value=32250000
RCC.VCOInput1Freq_Value=8000000
RCC.VCOInput2Freq_Value=8000000
RCC.VCOInput3Freq_Value=250000
SH.ADCx_INN10.0=ADC1_INN10,IN10-Differential
SH.ADCx_INN10.ConfNb=1
SH.ADCx_INP10.0=ADC1_INP10,IN10-Differential
SH.ADCx_INP10.0=ADC1_INP10,IN10-Single-Ended
SH.ADCx_INP10.ConfNb=1
SH.ADCx_INP4.0=ADC1_INP4,IN4-Single-Ended
SH.ADCx_INP4.ConfNb=1
Expand All @@ -469,6 +470,9 @@ SH.S_TIM1_CH1.0=TIM1_CH1,Input_Capture1_from_TI1
SH.S_TIM1_CH1.ConfNb=1
SH.S_TIM1_CH2.0=TIM1_CH2,TriggerSource_TI2FP2
SH.S_TIM1_CH2.ConfNb=1
SH.SharedAnalog_PC1.0=ADC1_INN10
SH.SharedAnalog_PC1.1=ADC1_INP11,IN11-Single-Ended
SH.SharedAnalog_PC1.ConfNb=2
SPI2.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_64
SPI2.CLKPhase=SPI_PHASE_2EDGE
SPI2.CLKPolarity=SPI_POLARITY_HIGH
Expand Down
2 changes: 1 addition & 1 deletion firmware/quadruna/BMS/src/cubemx/BMS.ioc.md5
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7d839e2a929020db932b37269155ff4d
9ac481404aacefa3b81b4487b3400ca5
14 changes: 11 additions & 3 deletions firmware/quadruna/BMS/src/cubemx/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ static void MX_ADC1_Init(void)
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.NbrOfConversion = 4;
hadc1.Init.NbrOfConversion = 5;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T3_TRGO;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
Expand Down Expand Up @@ -389,8 +389,16 @@ static void MX_ADC1_Init(void)
*/
sConfig.Channel = ADC_CHANNEL_10;
sConfig.Rank = ADC_REGULAR_RANK_4;
sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
sConfig.SingleDiff = ADC_DIFFERENTIAL_ENDED;
sConfig.SamplingTime = ADC_SAMPLETIME_16CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}

/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_11;
sConfig.Rank = ADC_REGULAR_RANK_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
Expand Down
2 changes: 2 additions & 0 deletions firmware/quadruna/BMS/src/cubemx/Src/stm32h7xx_hal_msp.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
/**ADC1 GPIO Configuration
PC0 ------> ADC1_INP10
PC1 ------> ADC1_INN10
PC1 ------> ADC1_INP11
PC4 ------> ADC1_INP4
PB0 ------> ADC1_INP9
PB1 ------> ADC1_INP5
Expand Down Expand Up @@ -159,6 +160,7 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)
/**ADC1 GPIO Configuration
PC0 ------> ADC1_INP10
PC1 ------> ADC1_INN10
PC1 ------> ADC1_INP11
PC4 ------> ADC1_INP4
PB0 ------> ADC1_INP9
PB1 ------> ADC1_INP5
Expand Down
6 changes: 4 additions & 2 deletions firmware/quadruna/BMS/src/hw/hw_adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
hw_adcConversions_rawAdcValueToVoltage(hadc, false, raw_adc_values[ADC1_IN5_TS_ISENSE_50A]);
adc_voltages[ADC1_IN9_TS_ISENSE_400A] =
hw_adcConversions_rawAdcValueToVoltage(hadc, false, raw_adc_values[ADC1_IN9_TS_ISENSE_400A]);
adc_voltages[ADC1_IN10_TS_VSENSE_DIFF] =
hw_adcConversions_rawAdcValueToVoltage(hadc, true, raw_adc_values[ADC1_IN10_TS_VSENSE_DIFF]);
adc_voltages[ADC1_IN10_TS_VSENSE_P] =
hw_adcConversions_rawAdcValueToVoltage(hadc, false, raw_adc_values[ADC1_IN10_TS_VSENSE_P]);
adc_voltages[ADC1_IN11_TS_VSENSE_N] =
hw_adcConversions_rawAdcValueToVoltage(hadc, false, raw_adc_values[ADC1_IN11_TS_VSENSE_N]);
}

uint16_t *hw_adc_getRawValuesBuffer(void)
Expand Down
3 changes: 2 additions & 1 deletion firmware/quadruna/BMS/src/hw/hw_adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ typedef enum
ADC1_IN4_AUX_TSENSE,
ADC1_IN5_TS_ISENSE_50A,
ADC1_IN9_TS_ISENSE_400A,
ADC1_IN10_TS_VSENSE_DIFF,
ADC1_IN10_TS_VSENSE_P,
ADC1_IN11_TS_VSENSE_N,
NUM_ADC_CHANNELS
} AdcChannel;

Expand Down
7 changes: 5 additions & 2 deletions firmware/quadruna/BMS/src/io/io_tractiveSystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,18 @@ float io_tractiveSystem_getVoltage()
// Voltage Ratio x Amplifier Gain

// TODO: Test differential ADC for voltage measurement
const float ts_vsense = hw_adc_getVoltage(config->ts_vsense_channel);
const float ts_vsense_P = hw_adc_getVoltage(config->ts_vsense_channel_P);
const float ts_vsense_N = hw_adc_getVoltage(config->ts_vsense_channel_N);
const float ts_vsense = ts_vsense_P - ts_vsense_N;

if (ts_vsense < 0.0f)
{
return NAN;
}
else
{
return ts_vsense * R_ERROR_COMPENSATION / (TS_VOLTAGE_DIV * AMPLIFIER_GAIN);
float real_voltage = ts_vsense * R_ERROR_COMPENSATION / (TS_VOLTAGE_DIV * AMPLIFIER_GAIN);
return real_voltage;
}
}

Expand Down
3 changes: 2 additions & 1 deletion firmware/quadruna/BMS/src/io/io_tractiveSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

typedef struct
{
const AdcChannel ts_vsense_channel;
const AdcChannel ts_vsense_channel_P;
const AdcChannel ts_vsense_channel_N;
const AdcChannel ts_isense_high_res_channel;
const AdcChannel ts_isense_low_res_channel;
} TractiveSystemConfig;
Expand Down
11 changes: 6 additions & 5 deletions firmware/quadruna/BMS/src/tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ static const AirsConfig airs_config = { .air_p_gpio = {
};

// TODO: Test differential ADC for voltage measurement
static const TractiveSystemConfig ts_config = { .ts_vsense_channel = ADC1_IN10_TS_VSENSE_DIFF,
static const TractiveSystemConfig ts_config = { .ts_vsense_channel_P = ADC1_IN10_TS_VSENSE_P,
.ts_vsense_channel_N = ADC1_IN11_TS_VSENSE_N,
.ts_isense_high_res_channel = ADC1_IN5_TS_ISENSE_50A,
.ts_isense_low_res_channel = ADC1_IN9_TS_ISENSE_400A

Expand Down Expand Up @@ -279,10 +280,9 @@ const Gpio *id_to_gpio[] = { [BMS_GpioNetName_ACCEL_BRAKE_OK_3V3] = &accel_b
[BMS_GpioNetName_SPI_CS] = &spi_cs_pin };

const AdcChannel id_to_adc[] = {
[BMS_AdcNetName_AUX_TSENSE] = ADC1_IN4_AUX_TSENSE,
[BMS_AdcNetName_TS_ISENSE_400A] = ADC1_IN9_TS_ISENSE_400A,
[BMS_AdcNetName_TS_ISENSE_50A] = ADC1_IN5_TS_ISENSE_50A,
[BMS_AdcNetName_TS_VSENSE] = ADC1_IN10_TS_VSENSE_DIFF,
[BMS_AdcNetName_AUX_TSENSE] = ADC1_IN4_AUX_TSENSE, [BMS_AdcNetName_TS_ISENSE_400A] = ADC1_IN9_TS_ISENSE_400A,
[BMS_AdcNetName_TS_ISENSE_50A] = ADC1_IN5_TS_ISENSE_50A, [BMS_AdcNetName_TS_VSENSE_P] = ADC1_IN10_TS_VSENSE_P,
[BMS_AdcNetName_TS_VSENSE_N] = ADC1_IN11_TS_VSENSE_N,
};

static UART debug_uart = { .handle = &huart1 };
Expand All @@ -301,6 +301,7 @@ void tasks_init(void)
{
__HAL_DBGMCU_FREEZE_IWDG1();

HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED);
HAL_ADC_Start_DMA(&hadc1, (uint32_t *)hw_adc_getRawValuesBuffer(), hadc1.Init.NbrOfConversion);
HAL_TIM_Base_Start(&htim3);
HAL_TIM_Base_Start(&htim15);
Expand Down

0 comments on commit a682af4

Please sign in to comment.