Graphing Data from RCX/NQC Datalogs using Mathematica


Specify a path and file name for the RCX/NQC datalog. You can upload the datalog using NQC and then cut and paste the data to a file.


InputFileName = 
    "Macintosh HD:Home:work:CS148:web:control:pid:code:datalog.txt";

The following routine converts an RCX/NQC verbose datalog into a list of lists where each list corresponds to the sequence of all values recorded for a particular variable. A verbose datalog consisting of a set of entries and each entry corresponds to a line in the datalog file of the form "Variable #: #" where the first # is the number of RCX variable and the second # is its value at the time that the corresponding "AddToDatalog" statement in the NQC program is executed. To invoke the following code, you have to know the number of distinct variables in the datalog (or at least an upper bound) but the program distinguishes entries corresponding to the different variables and bins them appropriately.


DatalogConverter[n_Integer] :=
    Module[{data = Table[{}, {n}], i = 0},
        Clear[index]; index[k_Integer] = 0 ;
        ReturnIndex[k_Integer] := 
            If[index[k] == 0, i += 1; index[k] = i, index[k]] ;
        (*Open the input stream.*)
        TextInput = OpenRead[InputFileName] ;
        (*Begin the line-by-line conversion.*)
        Label[beginConversion] ;
        (*Read a line from the input file.*)
        s = Read[TextInput, {Word, Number, Word, Number}] ;
        (*Check for the end of the file.*)
        If[s === EndOfFile, 
            Goto[endConversion]] ;
        (*Ignore blank lines.*)
        If[s === "", 
            Goto[beginConversion]] ;
        (*Handle a new entry.*)
        data[[ReturnIndex[s[[2]]]]] = 
            Append[data[[ReturnIndex[s[[2]]]]], s[[4]]];
        Goto[beginConversion] ;
        Label[endConversion] ;
        (*Close the input stream.*)
        Close[TextInput] ;
        Return[data]]

In the case of the experimental code for the PID controller,the datalog records the values of the variables RIGHT_VELOCITY and RIGHT_POWER at 1 second intervals.To test the controller,initially I held the robot so its tracks were not touching anything allowing the motors to run free.After a few seconds and on approximately ten second intervals,I applied pressure to the right tread thereby slowing the wheels and then releasing it after a couple of seconds.You can clearly see the affect on the RIGHT_VELOCITY variable,the response of the controller in the form of changes to RIGHT_POWER variable,and the lags in the response of the system.


data = DatalogConverter[2];

velocityGraph = ListPlot[data[[1]], PlotJoined -> True] ;
Velocity Graph


powerGraph = ListPlot[data[[2]], PlotJoined -> True] ;
Power Graph


Show[velocityGraph, powerGraph] ;
Velocity Plus Power Graph

I recommend the use of datalogs and graphing utilities for visualizing the data. I don't expect you to know or learn Mathematica but there are lots of other options available. One popular option is to use a scripting language such as Perl or Python to convert the data into a suitable form and then use a plotting utility such as Gnuplot to draw the graphs. You can even use Excel or some other spreadsheet program to do the graphing.