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.
8 years, 1 month ago.
Frequency of 1000 hertz and complete this process within 5 seconds.
Hi all , I have this working code for a 3 axis accelerator. My question is how to make it at a frequency of 1000 hertz and complete this process within 5 seconds.
Thank you for your time and have a great day.
#include "mbed.h" #include "TextLCD.h" LocalFileSystem local("local"); TextLCD lcd(p24,p26,p27,p28,p29,p30); Serial pc(USBTX,USBRX); Ticker in; AnalogIn x_in(p17); AnalogIn y_in(p18); AnalogIn z_in(p19); const int maxReadings = 1000; float measurmentArray[maxReadings][3]; volatile int readingNumber = 0; void update() { if (readingNumber < maxReadings) { measurmentArray[readingNumber][0]=x_in; measurmentArray[readingNumber][1]=y_in; measurmentArray[readingNumber][2]=z_in; readingNumber++;}} main () { int nextReading = 0; in.attach_us(&update,1000);//1000 hertz??? while (nextReading<maxReadings ) { //process until no more space if (readingNumber > nextReading) { // there is new data nextReading = readingNumber-1; // the index of the last value written to the array float x,y,z; // copy values to a local variables x=measurmentArray[nextReading][0]; y=measurmentArray[nextReading][1]; z=measurmentArray[nextReading][2]; pc.printf("X-%5.2f Y-%5.2f Z-%5.2f \n",x, y, z); nextReading++; } } // write array in the file txt/csv FILE *fp = fopen("/local/Record.csv", "a"); if (fp) { for (int i=0;i<maxReadings;i++) { fprintf(fp, "%f , %f , %f \r ",measurmentArray[i][0],measurmentArray[i][1],measurmentArray[i][2]); } fclose(fp); } else { pc.printf("ERROR. Failed to open output file\r\n"); } }
1 Answer
8 years ago.
Hi Sze Ming,
A couple things. First I fixed some formatting issues. The bracket placement and indenting made it hard to read. The program logic was a little hard to follow in my opinion, so I tried to clean it up. There is only one counter now that tracks what index we are on.
I think the printf output cannot happen in realtime. I think you would struggle to printf 1000 times a second. I think it is better to capture the data first, then print it at once. If you absolutely need to send data out the serial port when it is captured, I suspect that will be more work in order to optimize the time to do data conversion and send it out the serial port. I suspect printf may be too slow for this and you will have to write your own fast data conversion function. I have not tested the timing, that's just a gut feeling.
So I made a printf loop that runs after data is captured. I am using SWV channel to view the data. I am not setup right now to view data on a regular serial channel. SWV is fast, but even here I find I have to slow down the printf statements to avoid getting garbled output data. There is a 5ms wait in there. Adjust based on what works for you. I am not sure where or why the data is getting garbled. More investigation would be needed.
I added an infinite wait loop at the end of main. I think actually exiting main could cause undesirable behavior.
This is mostly working. My micro does not have a file system, so I can't test that function. The whole thing finishes for me in about 5 seconds, 1 second reading data and 4s printing data.
Good luck.
#include "mbed.h" //#include "TextLCD.h" /* Objects */ LocalFileSystem local("local"); Serial pc(USBTX,USBRX); DigitalOut led(PB_14); Ticker in; AnalogIn x_in(p17); AnalogIn y_in(p18); AnalogIn z_in(p19); //TextLCD lcd(p24,p26,p27,p28,p29,p30); /* Variables */ const int MAX = 1000; volatile int count = 0; volatile float measurmentArray[MAX][3]; /*** Update *** * Record new values in array. * If statement guards against chance of 1 extra read. */ void update() { if (count < MAX) { measurmentArray[count][0] = x_in.read(); measurmentArray[count][1] = y_in.read(); measurmentArray[count][2] = z_in.read(); count++; } } /*** Main *** */ int main () { /* Temp Variables */ float x,y,z = 0; /* 1000 Hz = 1000us */ in.attach_us(&update,1000); /* Wait Until Full */ while (count < MAX) { } /* Print Data */ for (int i=0; i<MAX; i++) { x = measurmentArray[i][0]; y = measurmentArray[i][1]; z = measurmentArray[i][2]; pc.printf("X-%5.2f Y-%5.2f Z-%5.2f \r\n",x, y, z); /* Delay may be Needed to Prevent Overloading Channel */ wait_ms(5); } pc.printf("--END--\r\n"); /* Turn Interrupt Off */ in.detach(); /* Write array to file txt/csv */ FILE *fp = fopen("/local/Record.csv", "a"); if (fp) { for (int i=0; i<MAX; i++) { fprintf(fp, "%f , %f , %f \r ",measurmentArray[i][0],measurmentArray[i][1],measurmentArray[i][2]); } fclose(fp); } else { pc.printf("ERROR. Failed to open output file\r\n"); } /* Don't Exit Main */ while(true) { led =! led; wait(1); } }
Graham