Hi,
OK I've tried your program and I understand what is happening. Here is a slightly modified version of your program to help you understand what is going on...
- Code: Select all
import lejos.nxt.*;
import java.io.*;
import lejos.util.Delay;
class LogRunnable implements Runnable {
private static FileOutputStream initFileOutput(String fileName) {
File file = new File(fileName);
if (file.exists()) {
file.delete();
}
try {
file.createNewFile();
}
catch (IOException e) {
System.out.println("No logger!");
}
// for freeing the resources if a big file was deleted
File.freeMemory();
return new FileOutputStream(file);
}
@Override
public void run() {
FileOutputStream output = initFileOutput("test.txt");
long maxTime = 0;
try {
byte currentMessage;
while (true) {
try {
currentMessage = new Long(System.currentTimeMillis())
.byteValue();
System.out.println(currentMessage);
if (File.freeMemory() > 145000) {
long s = System.currentTimeMillis();
output.write(currentMessage);
long t = System.currentTimeMillis() - s;
if (t > maxTime) maxTime = t;
//output.flush();
//Delay.msDelay(10);
}
else {
throw new Exception("NO SPACE");
}
}
catch (IOException e) {
}
}
}
catch (Throwable e) {
System.out.println(e.getMessage());
}
finally {
try {
output.close();
}
catch (IOException e) {
}
System.out.println("closed");
System.out.println("Time: " + maxTime);
synchronized (this) {
try {
this.wait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public class FileTest {
public static void main(String[] args) {
LogRunnable logRunnable = new LogRunnable();
Thread logThread = new Thread(logRunnable, "LogThread");
Motor.A.forward();
Delay.msDelay(10000);
logThread.start();
Button.waitForPress();
synchronized (logRunnable) {
logRunnable.notify();
}
}
You will see that I've made a few changes.
1. I allow the motor to run for 10 seconds before starting the logging thread. This is to show what the correctly regulated speed is.
2. I've added code to record the maximum time taken by a write to the file.
3. I've commented out the flush statement.
Ok so when this version is run what happens is that the motor will run for a while relatively slowly. This is the correct regulated default speed (you can use the setSpeed call to make it faster if that is what you want). Then when the logger starts you will probably notice that the speed of the motor jumps a little from time to time, you will also notice that the file is written much faster than with your version. Finally you will see at the end that the motor returns to the original speed and the time for the longest write is displayed, on my NXT this is 9ms.
So what is going on here. Well as you can see it can take up to 9ms to write a file system page to flash memory and during that time nothing else can run on the NXT. This means that during that time the speed of the motor is unregulated. In my test program I let the system decide when to flush the file data to flash (by removing the flush call) and so the 9ms delay only happens every 256 writes (each flash page is 256 bytes in size). When the write takes place the motor speed will "glitch" a little. In your program you flushed the data after every write. This means that the file was being written to flash every time and each write was taking approx 9ms. This will mean in effect that the motor is unregulated pretty much all of the time, which means that it will run at some uncontrolled speed, which seems to be full speed. Then when the file is full, the regulator will get chance to run and will once again reduce the speed...
So what can you do if you want to use logging to a file. Well obviously don't call flush all of the time. If you don't do this then you will find that the logging thread will run much faster so you may want to add a delay after each write. In my test program I added a 10ms delay to give about the same data rate as your original program. With this the speed of the motor was pretty constant with just a slight glitch every time the file was written. An alternative (which I use a lot), would be to use a Bluetooth (or USB) connection and log to that, this way you avoid the issues with the file system and you can see the data in real time on your PC.
Hope the above makes some sort of sense. If not please ask for more details....
Andy