Philipp Schrader | 7861dce | 2015-02-23 00:27:59 +0000 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | import sys |
| 4 | import numpy |
| 5 | import analysis |
| 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 | |
| 27 | Args: |
| 28 | filename: The name of the file to read the definitions from. |
| 29 | |
| 30 | Returns: |
| 31 | [[str]]: The definitions in the specified file. |
| 32 | """ |
| 33 | defs = [] |
| 34 | with open(filename) as fd: |
| 35 | for line in fd: |
| 36 | raw_defs = line.split() |
| 37 | |
| 38 | # Only add to the list of definitions if the line's not empty and it |
| 39 | # doesn't start with a hash. |
| 40 | if raw_defs and not raw_defs[0].startswith('#'): |
| 41 | defs.append(raw_defs) |
| 42 | |
| 43 | return defs |
| 44 | |
| 45 | def main(): |
| 46 | # Parse all command line arguments. |
| 47 | arg_parser = argparse.ArgumentParser(description='Log Plotter') |
| 48 | arg_parser.add_argument('log_file', metavar='LOG_FILE', type=str, \ |
| 49 | help='The file from which to read logs and plot.') |
| 50 | arg_parser.add_argument('--plot-defs', action='store', type=str, \ |
| 51 | help='Read the items to plot from this file.') |
Philipp Schrader | 0db6045 | 2015-03-15 07:32:38 +0000 | [diff] [blame^] | 52 | arg_parser.add_argument('--no-binary', '-n', action='store_true', \ |
| 53 | help='Don\'t print the binary name in the legend.') |
Philipp Schrader | 7861dce | 2015-02-23 00:27:59 +0000 | [diff] [blame] | 54 | |
| 55 | args = arg_parser.parse_args(sys.argv[1:]) |
| 56 | |
| 57 | p = analysis.Plotter() |
| 58 | |
| 59 | # If the user defines the list of data to plot in a file, read it from there. |
| 60 | if args.plot_defs: |
| 61 | defs = ReadPlotDefinitions(args.plot_defs) |
| 62 | for definition in defs: |
| 63 | p.Add(definition[0], definition[1], *definition[2:]) |
| 64 | |
| 65 | # Otherwise use a pre-defined set of data to plot. |
| 66 | else: |
| 67 | p.Add('fridge', 'goal', 'height') |
| 68 | p.Add('fridge', 'goal', 'angle') |
| 69 | p.Add('fridge', 'goal', 'velocity') |
| 70 | p.Add('fridge', 'goal', 'angular_velocity') |
| 71 | |
| 72 | p.Add('fridge', 'output', 'left_arm') |
| 73 | p.Add('fridge', 'output', 'right_arm') |
| 74 | p.Add('fridge', 'output', 'left_elevator') |
| 75 | p.Add('fridge', 'output', 'right_elevator') |
| 76 | |
Philipp Schrader | 0db6045 | 2015-03-15 07:32:38 +0000 | [diff] [blame^] | 77 | p.PlotFile(args.log_file, args.no_binary) |
Philipp Schrader | 7861dce | 2015-02-23 00:27:59 +0000 | [diff] [blame] | 78 | |
| 79 | if __name__ == '__main__': |
| 80 | main() |