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.
7 years, 8 months ago.
MBED encoder readings not working at higher rpm
Hello.
I am trying to read encoder pulses using the LPC1768 by writing the pulse count to a file stored on the MBED. The encoder I am using produces 24 pulses per wheel revolution. When the wheel on the motor shaft is rotated very slowly by hand, the MBED reads and prints the file correctly. But I am having issues when the wheel rotates quickly (when a PWM is applied to the wheel). The MBED counts a lower number of pulses than expected. I know the encoder is working as I have measured it on the oscilloscope and it produces 24 pulses per rev no matter what speed it is rotated at.
My question is why is the MBED not reading the correct number of pulses when the wheel is rotated quickly? Could it be due to the MBED sampling time?
The code looks like this so far:
encoder_pulse_count
#include "mbed.h" #include "QEI.h" DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); LocalFileSystem local("local"); int main() { QEI encoder1(p11, p12, NC, 24, QEI::X2_ENCODING); // set QEI library for right encoder int pass; FILE *fp1 = fopen("/local/COND.txt","r"); fscanf(fp1, "%d",&pass); led1=1; fclose(fp1); int i=0; if(pass==0) { //Checks if condition file is 0 to enable the code while(1) { i++; if(i==1) { FILE *fp1 = fopen("/local/COND.txt","w"); int a=1; fprintf(fp1,"%d",a); fclose(fp1); } // begin reading the encoder pulses and write the value of pulses to the file FILE *fp2 = fopen("/local/OUT.txt", "at+"); fprintf(fp2, "Reading = %d\r\n", i); fprintf(fp2, "Pulse Count=> e1:%d, \r\n",encoder1.getPulses()); fprintf(fp2,"\r\n"); fclose(fp2); led2=1; } } else { led4=1; }
3 Answers
7 years, 8 months ago.
you are correct, the QEI uses interrupts to count pulses. you are using the local file system. see this https://developer.mbed.org/handbook/LocalFileSystem it states that using the local file system will block interrupts. hence, you will miss pulses when you write to a local file system file.
7 years, 8 months ago.
Not relay sure, it was a crazy long time ago when I used QEI,
but it takes a very long time to write to a file, (relative to the interval between QEI Pulses)
I think that the QEI code, needs to be in an ISR.
Also, you are constantly opening and closing the file (time consuming)
move the block
if(i==1) { FILE *fp1 = fopen("/local/COND.txt","w"); int a=1; fprintf(fp1,"%d",a); fclose(fp1); }
outside of the main loop.
Ceri.
Thanks for the reply. As far as I understand, the QEI library sets the input encoder signal as an interrupt to the MBED and I am just calling the function from the library when I need number of pulses. So I don't think that is the problem but I am still unsure why it does not work.
Even when I move the motor using the encoder signals as feedback it overshoots the demanded distance by a lot due to the encoder reading far less pulses than it should when the wheels spin fast.
posted by 19 Apr 20177 years, 8 months ago.
Hi Sangam. Not sure on if you require the filesystem support which is blocking in nature but can you consider to write your results to a device like the following ?
http://www.microchip.com/design-centers/memory/serial-eeram
In short, this device is like writing to standard SRAM so speed should not be a bottle neck. When you power down, the device automatically writes the SRAM contents to non-volatile memory. It could be the best of both worlds. The 24 pulses is not that fast but as noted by other developers, the write to file system exercise is blocking your progress.
The above referenced Microchip part is interfaced through I2C so should be fine to use with mbed. Be sure to include the required pull-up resistors on SDA & SCL and test away. Consider to perform your counts, etc. in realtime using this Microchip device and then upon exit condition, copy the end results from this non-volatile device to your filesystem since you will then have the proper time to perform the write to sdcard, flash. On this note, why not just alter your logic to save to normal ram inside the LPC mbed using a variable in realtime and upon exit condition, commit to the sdcard / flash write ? :)