Hi everyone.
First a bit of background - I'm writing a program to control a number of stepper motors. Their movement is scripted using a text file where each line is a "frame" that contains each motor's position.
The text file is stored on a USB stick which is accessed using the MSCFileSystem library.
As each script could potentially last a number of minutes, and there are 25 frames per second this adds up to a lot of lines and a lot of numbers.
I therefore don't want to read the whole text file at once, store all the positions, and then play it through, as the memory this array would use would soon become a limiting factor.
To fix this problem I thought I would read the text file line by line (frame by frame) in real time. Do do this, I set up a ticker at 25 Hz, open a text file and the function attached to the ticker reads the text file. Simple right? Here's my code:
#include "mbed.h"
#include "MSCFileSystem.h"
#define FSNAME "stick"
MSCFileSystem msc(FSNAME);
Serial pc(USBTX, USBRX);
Ticker animate;
FILE *fp = fopen("/" FSNAME "/show.txt", "r");
int index = 0;
void test()
{
int data = fgetc(fp);
pc.printf("%d %c\n", index, data);
index++;
}
int main()
{
pc.printf("START\n");
int data = fgetc(fp);
animate.attach(&test, 0.04);
}
Now the first thing I'm sure you've noticed is the seemingly stupid code at line 25 (The second line in main). This is the first bug.
Don't ask me why, but unless you start reading the text file from main, it won't read from the test function. As I work mainly with applications the first thing I thought of was a thread issue, but I don't think threading exists with microcontrollers. But that line fixes the issue.
So ignoring that little bug, the code above appears (to me at least) like it should simply print out each character in the text file along with a counter, just so I know which character its got to.
Which is exactly what it does do... for the about the first 500 characters... then it stops.
The fgetc command freezes.
From what I can gather, this is a memory issue. The program uses all the mbed's memory and crashes.
How? I'm not storing anything. Am I?
Is there some clean up command I need to be running?
Or does the fgetc command have a memory leak?
If anyone can shed some light on this, it would be greatly appreciated.
Many Thanks.
Hi everyone.
First a bit of background - I'm writing a program to control a number of stepper motors. Their movement is scripted using a text file where each line is a "frame" that contains each motor's position. The text file is stored on a USB stick which is accessed using the MSCFileSystem library. As each script could potentially last a number of minutes, and there are 25 frames per second this adds up to a lot of lines and a lot of numbers.
I therefore don't want to read the whole text file at once, store all the positions, and then play it through, as the memory this array would use would soon become a limiting factor. To fix this problem I thought I would read the text file line by line (frame by frame) in real time. Do do this, I set up a ticker at 25 Hz, open a text file and the function attached to the ticker reads the text file. Simple right? Here's my code:
Now the first thing I'm sure you've noticed is the seemingly stupid code at line 25 (The second line in main). This is the first bug.
Don't ask me why, but unless you start reading the text file from main, it won't read from the test function. As I work mainly with applications the first thing I thought of was a thread issue, but I don't think threading exists with microcontrollers. But that line fixes the issue.
So ignoring that little bug, the code above appears (to me at least) like it should simply print out each character in the text file along with a counter, just so I know which character its got to. Which is exactly what it does do... for the about the first 500 characters... then it stops. The fgetc command freezes.
From what I can gather, this is a memory issue. The program uses all the mbed's memory and crashes. How? I'm not storing anything. Am I? Is there some clean up command I need to be running? Or does the fgetc command have a memory leak?
If anyone can shed some light on this, it would be greatly appreciated.
Many Thanks.