Bob the Pleo Forums

Pleo Stuff => Pleo Hardware/Software => PDK, Programming, and code => Topic started by: seanstocktonclark on December 26, 2010, 07:04:34 AM

Title: Recording data to text file or how to use Log.inc
Post by: seanstocktonclark on December 26, 2010, 07:04:34 AM
I want to record the values of each properties at a defined rate and store it to a file, so that I can plot them vs. time later.

In main.p, I used set_timer_interval(1000); so that SENSOR_TIMER will trigger every 1 second.  Then in sensors.p, I have a case SENSOR_TIMER:.  IT is here that I want to report the property values. 

I want to write csv output to a text file that I can read later.

for instance, I want my output file to look like this:
time,property1_value,property2_value,...
0,100,30,
1,100,29
2,100,28
3,50,27


Where I need HELP:
I don't understand how to create a formatted string to output to a text file.  I want something like this printf statement, but i want it to go to a file.  printf("%d,%d,%d",time,value1,value2) and that doesn't seem possible.




If this doesn't work, I  fallback is to use Log.inc functionality. 
I have the following code in my main:init()

   device_change(device_sd);       
   new file_name[]="samplefile.txt";
   file_delete(file_name);
   log_set_file(file_name);
   log_enable(MSG_ALL);
                 
   Message(MSG_SCRIPT, "script_message");                       
   Info("info_message");                       
   Message(MSG_SENSOR, "sensor_message");                                                                       

Can someone tell me why my samplefile.txt is blank after I run this?  I can write to the file with file_puts.  Thanks!
Title: Re: Recording data to text file or how to use Log.inc
Post by: MannyNZ on December 30, 2010, 04:01:17 PM
Hi,
there is definately a way to achieve what your aiming at.

For using the Loggin commands, the trick is, that the log doesn't write to your txt File at instant,
you have to use that flush command here and there to get it to write to the text-File.

I've implemted some writing out data into a text file, sothat i can debug my application better, so it definately works
to print the values of the properties there.

Manny
Title: Re: Recording data to text file or how to use Log.inc
Post by: PleoPet on January 07, 2011, 09:09:13 AM
> I want something like this printf statement, but i want it to go to a file.  printf("%d,%d,%d",time,value1,value2) and that doesn't seem possible.
The basic "printf" (log.inc) will output to the console (USB or serial port)

There is a "file_printf" (file.inc) that will output to a file handle. Flush frequently.
Or you can use the string "string_printf" and put the string into a character buffer, and then write it out to a file. NOTE: be sure to specify packed strings.

Suggestion: if you are using the SENSOR_TIMER approach, you may want to save the last tested property values in RAM, and only write to the log file if the property values change (with a time stamp in front).
That way the log file will only include significant changes to the values (and if desired you could increase the polling rate).

---
BTW: What properties are you interested in plotting?
If writing your own Pawn program, it will replace the default personality (PLEOPM). Many of the properties (eg: energy, health, mood etc) will not be changed unless the PLEOPM program is running.

SimplePortal 2.3.5 © 2008-2012, SimplePortal