-
• #2
Equipment: Espruino board, USB cable, WebIDE and SD card.
This program makes use of three objects saved in local modules in a WebIDE project.
The first object “CalObj” makes use of a file “Mycalibrations.cal” located on the SD card in the Espruino board.{"calibrations":[ {"name":"Inlet","units":"psig","pin":"A0","slope":100.13897386464,"Intercept":-0.03419722673}, {"name":"Outlet","units":"psig","pin":"A1","slope":50,"Intercept":-25}, {"name":"Motor","units":"Amps","pin":"A2","slope":1,"Intercept":0}, {"name":"Supply","units":"Volts","pin":"A3","slope":1,"Intercept":0}, {"name":"Flow","units":"Liters/minute","pin":"A4","slope":1,"Intercept":0} ]}
The following link lays out the object used to calibrate the analog inputs into engineering units. It was used to create the file “Mycalibrations.cal” on the SD card.
http://forum.espruino.com/conversations/¬286215/The second object “TimeObj.js” is used for its stopwatch function and ability to format date and time from a pattern stored in an array. See the following link.
http://forum.espruino.com/conversations/286424/The third object “LogObj.js” uses the previous two object modules and a “.cfg” file stored on the SD card. This program uses three different “.cfg” files an produces three different “.csv” files. The program used to create the “.cfg” files is “saveLogCfgXX1.js”
One of the “.cfg” files looks like this:{"Fname":"xx1.csv", "Interval":1000, "items":[ {"header":"N","cmd":"count"}, {"header":"Date","cmd":"DATE","Args":["M","-","D","-","Y"]}, {"header":"Time","cmd":"TIME","Args":["H",":","M",":","S"]}, {"header":"A0","cmd":"SAR","Args":["A0",2]}, {"header":"A1","cmd":"SAR","Args":["A1",2]}, {"header":"A2","cmd":"AR","Args":["A2",2]}, {"header":"Board Temp C","cmd":"BTC","Args":[2]}, {"header":"Board Temp F","cmd":"BTF","Args":[2]} ], "count":0, "N":10 }
The “Fname” is the name of the csv file to create.
The “Interval” is the milliseconds between each data sample.
The “Items” array contains records that set up the data to log to the csv file.
The “Header” is the text at the top of a column in the csv file.
The “cmd” can be “count”,“DATE”,“TIME”, “SAR”,”AR”,”BTC” or “BTF”
Date and time commands use the information in the “Args” to format the data.
AR is analog read without calibrations applied.
SAR is a calibrated analog reading. For SAR the header information comes from the calibration file.
For AR and SAR Args[0] is the pin name, and Args[1] is used in a toFixed(Args[1]) to format the data.
The BTC and BTF are the board temperature in F or C units.
Count is used to count and N is the number of samples to takePrograms to load into Project:
LogCSV6a.js, saveLogCfgXX1.jsModules to load into Modules:
TimeObj.js, LogObj.js and CalObj.js
Files on SD card in the Espruino:
Mycalibrations.cal, Mylog1.cfg, Mylog2.dfg, and Mylog3.cfg
Files created on the SD card by this program:
Xx1.csv, xx2.csv, and xx3.csv.LogCSV6a.js listing:
function ReadObjectFile(fname){ return JSON.parse(require("fs").readFileSync(fname)); }//end ReadObjectFile function TestIt(){ var T= new (require("TimeObj"))(); T.updateTime(); //start the stopwatch T.SWstart(); // output day of the week console.log(T.getDay()); Cal=new (require("CalObj"))(ReadObjectFile("Mycalibrations.cal")); Log=new (require("LogObj"))(ReadObjectFile("Mylog1.cfg"),Cal,T); Log1=new (require("LogObj"))(ReadObjectFile("Mylog2.cfg"),Cal,T); Log2=new (require("LogObj"))(ReadObjectFile("Mylog3.cfg"),Cal,T); //Changes to the onjects can be masde // Log1.A.Interval=3000; // Log1.A.Fname="xx1.csv"; //start the logging Log.doHeaders(); Log.start(); Log1.doHeaders(); Log1.start(); Log2.doHeaders(); Log2.start(); // output the elapsed time console.log(T.SWstop().toFixed(3)," ms"); }//end TestIt setBusyIndicator(LED1); TestIt();
and the console output:
>echo(0); Fri 1680.916 ms =undefined xx1.csv Finished 11005.221 ms xx2.csv Finished 21572.389 ms xx3.csv Finished 3202
14 Attachments
-
• #3
That's really cool stuff. Did you monitor RAM usage (with
process.memory()
)? -
• #4
Trying it with process.memory().usage inserted.
function ReadObjectFile(fname){ return JSON.parse(require("fs").readFileSync(fname)); }//end ReadObjectFile function TestIt(){ var j=0; console.log(j," ",process.memory().usage);j++; var T= new (require("TimeObj"))(); console.log(j," ",process.memory().usage);j++; T.updateTime(); //start the stopwatch T.SWstart(); // output day of the week console.log(T.getDay()); Cal=new (require("CalObj"))(ReadObjectFile("Mycalibrations.cal")); console.log(j," ",process.memory().usage);j++; Log=new (require("LogObj"))(ReadObjectFile("Mylog1.cfg"),Cal,T); console.log(j," ",process.memory().usage);j++; Log1=new (require("LogObj"))(ReadObjectFile("Mylog2.cfg"),Cal,T); console.log(j," ",process.memory().usage);j++; Log2=new (require("LogObj"))(ReadObjectFile("Mylog3.cfg"),Cal,T); console.log(j," ",process.memory().usage);j++; //Changes to the onjects can be masde // Log1.A.Interval=3000; // Log1.A.Fname="xx1.csv"; //start the logging Log.doHeaders(); Log.start(); console.log(j," ",process.memory().usage);j++; Log1.doHeaders(); Log1.start(); console.log(j," ",process.memory().usage);j++; Log2.doHeaders(); Log2.start(); // output the elapsed time console.log(T.SWstop().toFixed(3)," ms"); console.log(j," ",process.memory().usage);j++; }//end TestIt setBusyIndicator(LED1); TestIt();
The output:
>echo(0); 0 445 1 453 Sat 2 571 3 717 4 827 5 937 6 958 7 975 418.646 ms 8 997 =undefined xx1.csv Finished 10472.984 ms xx2.csv Finished 20561.429 ms xx3.csv Finished 30603.546 ms >console.log(process.memory().usage); 957 =undefined >
This project uses two files on the SD card to layout CSV file format for data logging and read analog inputs in engineering units.
Mylog.cfg
Mycalibrations.cal
The following link lays out the object used to calibrate the analog inputs into engineering units. It was used to create the file “Mycalibrations.cal” on the SD card.
http://forum.espruino.com/conversations/286215/
The following code was used to create the log configuration file:
SaveLog.js
The left screen when the log configuration file is created:
The logging program. The test portion opens the calibration file and the configuration file and creates a calibration object and two instances of the configuration. The layout is identical (could be different) except that the filename to write, and logging interval in one is 1 second and the other is 3 seconds.
LogCSV3.js
The console output when the program executes:
The CSV files created are: xxx.csv and xx1.csv
6 Attachments