AnsweredAssumed Answered

Encoder configuration giving erroneous speed

Question asked by tariq.furqan on Aug 16, 2014
Latest reply on Aug 18, 2014 by tariq.furqan
Hi, I am trying to measure the rpm of a geared DC motor. The manufacturer claims a speed of 500rpm at 12V but my calculations give a speed of around 1600rpm! The gear ratio is 19:1.

I have written the following code to measure the speed:

//Systick setup for 1ms

void Delay(__IO uint32_t nTime)
{
  TimingDelay = nTime;

  while(TimingDelay != 0);
}
void TimingDelay_Decrement(void)
{
  if (TimingDelay != 0x00)
  {
    TimingDelay--;
  }
}

void SysTick_Init(void){
    while (SysTick_Config(SystemCoreClock / 1000) != 0) {
    }
}

void SysTick_Handler(void){
    TimingDelay_Decrement();
}

//Initializing Timers for encoder interface

void InitENTim(void){
    
    RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM4, ENABLE);
    RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM2, ENABLE);
    
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
    TIM_TimeBaseStructure.TIM_ClockDivision=0;
    TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
    
    TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12,TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
   
    TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12,TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
   
    
    TIM_Cmd(TIM4,ENABLE);
    TIM_Cmd(TIM2,ENABLE);
    encodersReset(); //function to reset counter values
    
}

//The function to read the encoder and get Count


void encoderRead(void){
    
    oldrightEncoder = TIM_GetCounter(TIM4);
    Delay(10); //This should be a 10ms delay (according to my Systick config as written above)
    rightEncoder = TIM_GetCounter(TIM4);
    rightCount = rightEncoder - oldrightEncoder; //I am getting the difference in counts after 10ms
    totalrightCount += rightCount;
    
    
    oldleftEncoder = TIM_GetCounter(TIM4); //Encoder;
    Delay(10);
    leftEncoder = TIM_GetCounter(TIM4);
    leftCount = leftEncoder - oldleftEncoder;
    totalleftCount += leftCount;
    
}

//function to calculate speeds

void encoderCalculate(void){
    volatile float a,b,x,y,z; //just some variables for ensuring I didnt make a mistake in simple maths
    
    x = rightCount/64; //my encoder gives 64 counts per revolution when detecting both edges of both channels.
    y = 1/x;
    z = y*0.01; //This 0.01 is for the 10ms delay that I had for getting the difference in Count
    a = 60/z;
    b = a/19; //accounting for the gear ratio (19:1)
    rightspeed = b;
    
    x = leftCount/64;
    y = 1/x;
    z = y*0.01;
    a = 60/z;
    b = a/19;
    leftspeed = b;
  
}

Is there any chance there is some sort of conflict in timing of the encoder timer and the systick timer? Obviously, there is some sort of a timing problem. Kindly help as I have been stuck on this problem for a quiet a long while.

Outcomes