Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 1 month ago. This question has been closed. Reason: Off Topic
How to get the right encoder pulses?
Hello Aaron!
Now I'm using your QEI.h files to get the encoder pulses.(encoder's pulse per revolution is 1200)
However now I've just kept getting wrong encoder pulses like half of right answer.(only 600 pulses per one revolution)
And I also tried to use the X2_ENCODER, in that case, I got the same value but the value(600) was measured only one direction.
So I'm just wondering you've ever had this error.
In order to clear view, I also attached my code here
Thanks!
Charlie.
--code --
PWM motor driving example on LPC1768
- include "QEI.h"
- include "mbed.h"
- define pi 3.14159265358979323846
AnalogIn ain(p20);
PwmOut INA(p21); InputA(Clockwise rotating)
PwmOut INB(p23); InputB(Counterclockwise rotating)
PwmOut PWM(p22); pwm outputs
Serial pc(USBTX,USBRX); tx,rx
QEI Wheel(p13, p14, NC, 1200, QEI::X4_ENCODING); (encoder channel 1, encoder channel 2, index (n/a here), counts per revolution, mode (X4, X2))
Timer timer;
int steps_per_rev = 1200; Encoder CPR * gear ratio
float pulsesToRadians(float pulses) Encoder steps to revolutions of output shaft in radians
{
return ((pulses/steps_per_rev)*(2*pi));
}
float pulsesToDegrees(float pulses) Encoder steps to revolutions of output shaft in degrees
{
return ((pulses/steps_per_rev)*360);
}
/* Function prototype */
void motorControl(bool dir, float pwmVal);
int main()
{
int data;
while(1)
{
float i; PWMduty
i = ain;
data = ain *100;
motorControl(1,i); Clockwise rotating
pc.printf("Pulses is: %d PWMduty : %d %% \n\r",Wheel.getPulses(),data);
wait_ms(100);
}
}
This is a motor control function which controls
the speed and direction of a motor
void motorControl(bool dir, float pwmVal)
{
PWM.write(0); Duty cycles are initially zero
PWM.period(0.00005);
if(dir==1) forward direction
{
INA.write(1);
INB.write(0);
PWM.write(pwmVal);
}
else if(dir==0) reverse direction
{
INA.write(0);
INB.write(1);
PWM.write(pwmVal);
}
}