Skip to content

Commit

Permalink
Use dual ADC
Browse files Browse the repository at this point in the history
Reduced sample time to 1.5 clocks as it doesn't seem to decrease SNR
  • Loading branch information
jsphuebner committed Apr 3, 2024
1 parent 730dfa3 commit 5355da4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
8 changes: 5 additions & 3 deletions include/anain_prj.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

#include "hwdefs.h"

#define ADC_COUNT 2

#if CONTROL == CTRL_SINE
#define NUM_SAMPLES 12
#define NUM_SAMPLES 3
#define SAMPLE_TIME ADC_SMPR_SMP_7DOT5CYC
#elif CONTROL == CTRL_FOC
#define NUM_SAMPLES 3
#define SAMPLE_TIME ADC_SMPR_SMP_7DOT5CYC
#define SAMPLE_TIME ADC_SMPR_SMP_1DOT5CYC
#endif // CONTROL

#define ANA_IN_LIST \
Expand All @@ -19,7 +21,7 @@
ANA_IN_ENTRY(tmphs, GPIOC, 4) \
ANA_IN_ENTRY(uaux, GPIOA, 3) \
ANA_IN_ENTRY(il1, GPIOA, 5) \
ANA_IN_ENTRY(il2, GPIOB, 0)
ANA_IN_ENTRY(il2, GPIOB, 0) \

//Alternative list. Must contain exactly the same names and number of
//entries as ANA_IN_LIST but may contain different IO pins
Expand Down
1 change: 1 addition & 0 deletions src/hwinit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ void clock_setup(void)
rcc_periph_clock_enable(RCC_TIM4); //Overcurrent / AUX PWM, scheduler on blue pill
rcc_periph_clock_enable(RCC_DMA1); //ADC, Encoder and UART3
rcc_periph_clock_enable(RCC_ADC1);
rcc_periph_clock_enable(RCC_ADC2);
rcc_periph_clock_enable(RCC_CRC);
rcc_periph_clock_enable(RCC_AFIO); //CAN
rcc_periph_clock_enable(RCC_CAN1); //CAN
Expand Down
33 changes: 18 additions & 15 deletions src/inc_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static bool seenNorthSignal = false;
static int32_t turnsSinceLastSample = 0;
static int32_t distance = 0;
static int32_t resolverMin = 0, resolverMax = 0, startupDelay;
static int32_t sinChan = 3, cosChan = 2;
static uint32_t sinAdc = ADC2, cosAdc = ADC1;
static int32_t detectedDirection = 0;
static uint16_t sincosoffs = 2048;

Expand Down Expand Up @@ -158,13 +158,13 @@ void Encoder::SwapSinCos(bool swap)
{
if (swap)
{
sinChan = 2;
cosChan = 3;
sinAdc = ADC1;
cosAdc = ADC2;
}
else
{
sinChan = 3;
cosChan = 2;
sinAdc = ADC2;
cosAdc = ADC1;
}
}

Expand Down Expand Up @@ -446,13 +446,16 @@ void Encoder::InitTimerABZMode()

void Encoder::InitResolverMode()
{
//The first injected channel is always noisy, so we insert one dummy channel
uint8_t channels[3] = { 6, 6, 7 };
//The first injected sample is always noisy, so we insert one dummy sample
uint8_t channels1[3] = { 6, 6 };
uint8_t channels2[3] = { 7, 7 };

adc_set_injected_sequence(ADC1, sizeof(channels), channels);
adc_set_injected_sequence(ADC1, sizeof(channels1), channels1);
adc_set_injected_sequence(ADC2, sizeof(channels2), channels2);
adc_enable_external_trigger_injected(ADC1, ADC_CR2_JEXTSEL_JSWSTART);
adc_enable_external_trigger_injected(ADC2, ADC_CR2_JEXTSEL_JSWSTART);
adc_set_sample_time(ADC1, 6, ADC_SMPR_SMP_1DOT5CYC);
adc_set_sample_time(ADC1, 7, ADC_SMPR_SMP_1DOT5CYC);
adc_set_sample_time(ADC2, 7, ADC_SMPR_SMP_1DOT5CYC);

gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO6 | GPIO7);
exti_disable_request(NORTH_EXC_EXTI);
Expand All @@ -470,16 +473,16 @@ void Encoder::InitResolverMode()
timer_generate_event(REV_CNT_TIMER, TIM_EGR_UG);
gpio_set_mode(NORTH_EXC_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, NORTH_EXC_PIN);
adc_set_injected_offset(ADC1, 2, 0);
adc_set_injected_offset(ADC1, 3, 0);
adc_set_injected_offset(ADC2, 2, 0);

adc_start_conversion_injected(ADC1); //Determine offset

while (!adc_eoc_injected(ADC1));

int ch1 = adc_read_injected(ADC1, 2);
int ch2 = adc_read_injected(ADC1, 3);
int ch2 = adc_read_injected(ADC2, 2);
adc_set_injected_offset(ADC1, 2, ch1);
adc_set_injected_offset(ADC1, 3, ch2);
adc_set_injected_offset(ADC2, 2, ch2);
adc_enable_external_trigger_injected(ADC1, ADC_CR2_JEXTSEL_TIM3_CC4);

if (CHK_BIPOLAR_OFS(ch1) || CHK_BIPOLAR_OFS(ch2))
Expand All @@ -493,7 +496,7 @@ void Encoder::InitResolverMode()
//on my hardware, min is 0.465V, max is 2.510v, so offset is 1.4875v, or 1846
//this should be a parameter?
adc_set_injected_offset(ADC1, 2, sincosoffs);
adc_set_injected_offset(ADC1, 3, sincosoffs);
adc_set_injected_offset(ADC2, 2, sincosoffs);
}

seenNorthSignal = true;
Expand Down Expand Up @@ -562,8 +565,8 @@ uint16_t Encoder::GetAngleSinCos()
*/
uint16_t Encoder::DecodeAngle(bool invert)
{
int sin = adc_read_injected(ADC1, sinChan);
int cos = adc_read_injected(ADC1, cosChan);
int sin = adc_read_injected(sinAdc, 2);
int cos = adc_read_injected(cosAdc, 2);

//Wait for signal to reach usable amplitude
if ((resolverMax - resolverMin) > MIN_RES_AMP)
Expand Down

0 comments on commit 5355da4

Please sign in to comment.