Philipp Schrader | 7861dce | 2015-02-23 00:27:59 +0000 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | import sys |
| 4 | import numpy |
Dave Smith | acbca19 | 2016-03-06 15:27:23 -0800 | [diff] [blame^] | 5 | from plotter import Plotter |
Philipp Schrader | 7861dce | 2015-02-23 00:27:59 +0000 | [diff] [blame] | 6 | import argparse |
| 7 | |
| 8 | def ReadPlotDefinitions(filename): |
| 9 | """ |
| 10 | Read a file with plotting definitions. |
| 11 | |
| 12 | A plotting definition is a single line that defines what data to search for |
| 13 | in order to plot it. The following in a file would duplicate the default |
| 14 | behaviour: |
| 15 | |
| 16 | fridge goal height |
| 17 | fridge goal angle |
| 18 | fridge goal velocity |
| 19 | fridge goal angular_velocity |
| 20 | fridge output left_arm |
| 21 | fridge output right_arm |
| 22 | fridge output left_elevator |
| 23 | fridge output right_elevator |
| 24 | |
| 25 | Lines are ignored if they start with a hash mark (i.e. '#'). |
| 26 | |
Philipp Schrader | 26681bc | 2015-04-02 04:25:30 +0000 | [diff] [blame] | 27 | Lines that end with a "-b X" where X is a number then it designates that line |
| 28 | as plotting a boolean value. X is the value plotted when the boolean is true. |
| 29 | When the boolean is false then the values is plotted as zero. For example, |
| 30 | the following boolean value is drawn to toggle between 2.0 and 0 when the |
| 31 | boolean is True and False, respectively: |
| 32 | |
| 33 | fridge status zeroed -b 2.0 |
| 34 | |
Philipp Schrader | 7861dce | 2015-02-23 00:27:59 +0000 | [diff] [blame] | 35 | Args: |
| 36 | filename: The name of the file to read the definitions from. |
| 37 | |
| 38 | Returns: |
| 39 | [[str]]: The definitions in the specified file. |
| 40 | """ |
| 41 | defs = [] |
| 42 | with open(filename) as fd: |
| 43 | for line in fd: |
| 44 | raw_defs = line.split() |
| 45 | |
| 46 | # Only add to the list of definitions if the line's not empty and it |
| 47 | # doesn't start with a hash. |
| 48 | if raw_defs and not raw_defs[0].startswith('#'): |
| 49 | defs.append(raw_defs) |
| 50 | |
| 51 | return defs |
| 52 | |
| 53 | def main(): |
| 54 | # Parse all command line arguments. |
| 55 | arg_parser = argparse.ArgumentParser(description='Log Plotter') |
| 56 | arg_parser.add_argument('log_file', metavar='LOG_FILE', type=str, \ |
| 57 | help='The file from which to read logs and plot.') |
Philipp Schrader | 26681bc | 2015-04-02 04:25:30 +0000 | [diff] [blame] | 58 | arg_parser.add_argument('--plot-defs', '-p', action='store', type=str, \ |
Philipp Schrader | 7861dce | 2015-02-23 00:27:59 +0000 | [diff] [blame] | 59 | help='Read the items to plot from this file.') |
Philipp Schrader | 0db6045 | 2015-03-15 07:32:38 +0000 | [diff] [blame] | 60 | arg_parser.add_argument('--no-binary', '-n', action='store_true', \ |
| 61 | help='Don\'t print the binary name in the legend.') |
Philipp Schrader | 7861dce | 2015-02-23 00:27:59 +0000 | [diff] [blame] | 62 | |
| 63 | args = arg_parser.parse_args(sys.argv[1:]) |
| 64 | |
Dave Smith | acbca19 | 2016-03-06 15:27:23 -0800 | [diff] [blame^] | 65 | p = Plotter() |
Philipp Schrader | 7861dce | 2015-02-23 00:27:59 +0000 | [diff] [blame] | 66 | |
| 67 | # If the user defines the list of data to plot in a file, read it from there. |
| 68 | if args.plot_defs: |
| 69 | defs = ReadPlotDefinitions(args.plot_defs) |
| 70 | for definition in defs: |
| 71 | p.Add(definition[0], definition[1], *definition[2:]) |
| 72 | |
| 73 | # Otherwise use a pre-defined set of data to plot. |
| 74 | else: |
| 75 | p.Add('fridge', 'goal', 'height') |
| 76 | p.Add('fridge', 'goal', 'angle') |
| 77 | p.Add('fridge', 'goal', 'velocity') |
| 78 | p.Add('fridge', 'goal', 'angular_velocity') |
| 79 | |
| 80 | p.Add('fridge', 'output', 'left_arm') |
| 81 | p.Add('fridge', 'output', 'right_arm') |
| 82 | p.Add('fridge', 'output', 'left_elevator') |
| 83 | p.Add('fridge', 'output', 'right_elevator') |
| 84 | |
Philipp Schrader | 0db6045 | 2015-03-15 07:32:38 +0000 | [diff] [blame] | 85 | p.PlotFile(args.log_file, args.no_binary) |
Philipp Schrader | 7861dce | 2015-02-23 00:27:59 +0000 | [diff] [blame] | 86 | |
| 87 | if __name__ == '__main__': |
| 88 | main() |