This project mainly includes program code, written in NXC. My NXT tachometer software:
- measures speed with a NXT motor (using the built-in tachometer) or a RCX legacy rotation sensor.
- displays speed in RPM average over the last second and the average over the last minute.
- displays in clearly readable large digits (18 x 28 pixels).
- writes log data to a comma separated file, which can be used to create a graph in Excel.
The speed (RPM) is displayed on the NXT screen in large digits. As you can see, there are two numbers displayed on the screen. The upper speed indicates the revolutions per minute over the last second. The lower speed indicates the average rpm over the last minute.
The average over the last minute is calculated by 60 sample periods of one second made in the last minute. The legacy RCX rotation sensor measures 16 ticks per rotation. The tachometer inside the NXT motor measures 360 degree per rotation. This way of measuring is much more accurate then the LEGO speed computer (part no: 32467). If you like to know more about the inside of the rotation sensors, please look at Philo's site: RCX legacy rotation sensor internals and the NXT motor internals.
The program records how many rotations are made in one second by counting:
the rotation sensor ticks, 16 ticks every rotation (in case of a RCX rotation sensor).
the rotation degrees, 360 every rotation (in case of a NXT motor).
Every result of a measurement during 1 second is called a sample. The program saves the results of the last 60 samples. The sum of all the ticks or degrees over the last 60 seconds divide by the amount of ticks or degrees per rotation is the speed in revolutions per minute (RPM).
The program runs in a loop. After 1 second (1000ms) of measuring, the speed is calculated, displayed and then written the a log file. After this it starts all over again. To calculated the average RPM over the last minute, it doesn't sum all the 60 samples every second (cycle) again. It just adds the last measurement to the result and then subtracts the oldest sample (61 seconds old) from the result.
Cycles of measuring, calculating, displaying and log writing
During the time that the speed is calculated, send to the NXT screen and written to a log file, this program doesn't measures the rotation. Of course the time that this period lasts, when no rotation measurement takes place, is not taken into the calculation. Luckily this is only for a relative short time (about 60ms) in comparison with the 1000ms period that the measurement takes place. Therefor the output of this tachometer is quite accurate even when you will have speed fluctuations during that 60ms period. And it is unlikely that you will have speed fluctuations during that 60ms period that doesn't reflect the measuring period.
Displaying large digits on NXT screen
I made a little routine that displays the large digits on the NXT screen. That makes it possible to display 2 rows with both 4 digits. Each digits is made of 7 lines, that I call bits in my program. At the start of the program the bit pattern of each digit (0, 1, 2 ,3 ,4 ,5 ,6 ,7 ,8 and 9) is load into an arrays. This makes it simple and fast to display the digits.
I have made an example program that shows how to display the digits (4 figures on 2 rows) on your NXT screen. It can also display a decimal point between the numbers. The program uses two functions. You can use this code for your own purpose:
DisplayInitiation(); // to initialize display function.
DisplayNumber(value,decimal_point,row); // to display the large digits. (<value to be displayed>, <position of the decimal point>, <first or second row>)
Writing a log file
The tachometer program writes the rotation results to a log file. At the start it tries to create a tachometer0-0.csv file. If this file name already exists it will increment the file number and tries to create tachometer1-0.csv. The program will keep on trying this until it succeeds.
A file on a NXT has a fixed size. The moment you create one you need to tell the NXT API how many bytes long it will be. It is not possible to expand the file size, for instance when a file is almost full. Ones the end of the file is reached my program will create a new file. The new file will have the same file number, but the file part number will be increased: tachometer0-1.csv (syntax: tachometer<file number>-<file part number>.csv)
The results are written in a comma separated file format (.csv). The values (RPM and average RPM) are separated by a comma and each line is ends by a EOL (end of line). You can use a tool like Microsoft Excel to convert the data to a diagram. If a measurement creates several files you can simply copy the results of all the files to one file one your PC with an text editor.
Don't forget to remove the log files from the NXT memory. Before you know it, your NXT memory is full. Use NXT Explorer from the BricxCC menu bar to (re)move the files.
note: The values in the file are 10 times higher then the actual RPM. The last (most right) digit is the decimal: (example: 1234 = 123.4)
I have made an example program that shows how the log file is created an how data is written to the file. The program uses three functions to do so. You can use this code for your own purpose: InitWriteToFile(); // to initialize the WriteToFile function (determine the file name and create a file)
WriteToFile(text); // to write a line to the file
StopWriteToFile(); // to close the file
Click here to download the example NXC code that explain how to create and write a file: write_to_csv_file.nxc
note: If you don't want any log files generated while you use the program, just search for the three commands: InitWriteToFile(); WriteToFile(text); StopWriteToFile();
Change the lines into comment by starting these lines with //.
note: To close the log file and write the log file to the NXT memory you must stop a running program by pressing the orange button. It may take a while (max one second) before the program ends.
I have made a video of my tachometer in action. I set up a test with an LPE (LEGO pneumatic engine). I tested with both sensors. The RCX doesn't give any resistance. It can rotate free. A NXT motor takes quite some force to let it rotate. Therefor the LPE run not that fast as it should go when the NXT motor is attached. Watch my video at Youtube:
Optional: Deceleration gearing factor
As you can see in the video, I have used two gears to gear down the revolutions. One 12 tooth gear is assembled to the LPE (drive axle) and one 36 tooth is mounted to the rotation sensors.
Engine (drive axle)
This deceleration gives a bit more power to drive the tachometer inside the NXT motor. This is also necessary, because my NXT tachometer is not able to measure revolutions above 800 RPM. It is possible to set this deceleration gearing factor in my program which makes it able to display the actual speed of your drive source (electric motor of LPE). If you use such a gearing between your engine and the rotation sensor you must set the following variables: long drive_gear=1; // <number of touth, gear assembled to source engine> is default set to 1;
long rotation_sensor_gear=1; // <number of tooth, gear assembled to the rotation sensor> is default set to 1;