Exemplo n.º 1
0
def main():
    """Parse command line options
    
    """

    usage = "usage: %(prog)s [options]"
    parser = ArgumentParser(
        prog='pydylogger',
        usage=usage,
    )  #formatter_class=RawTextHelpFormatter)

    tw = TextWrapper()
    mywrap = lambda x: "\n".join(tw.wrap(x))
    tw.width = 80 - 25

    #
    parser.add_argument('arglist', nargs='*', default=list(), help='N/A')
    parser.add_argument('-t', '--translate', action="store_true", \
            dest="translate", default=False, help="Print human readable " \
            "packets. Default: %(default)s")
    parser.add_argument('-a', '--all', action="store_true", \
            dest="saveall", default=False, help="Optionally save all bytes/" \
            "packets, including malformed packets. Default: %(default)s")
    parser.add_argument('-o', '--output', action="store", \
            dest="output", default="logging_output.txt", help="Specify output " \
            "file for log of packets. Default: %(default)s")
    #

    options = parser.parse_args()
    args = options.arglist

    # TODO get rid of these globals...
    global saveall
    global translate
    saveall = options.saveall
    translate = options.translate

    global port
    global baud
    global timing
    global id_dict
    cfg = PyDyConfigParser()
    cfg.read()
    port, baud, __, timing, __ = cfg.get_params()
    id_dict = cfg.get_id_to_device_dict()

    if saveall:
        print "All packets (including bad packets) will be saved in " \
                "{0}".format(options.output)
    if translate:
        print "Packets will be translated for listing in this window."

    logger_method(outputfile=options.output)

    return
Exemplo n.º 2
0
def main():
    """Parse command line options
    
    """
    
    usage = "usage: %(prog)s [options]"
    parser = ArgumentParser(prog='pydylogger', usage=usage,
                            )#formatter_class=RawTextHelpFormatter)
    
    tw = TextWrapper()
    mywrap = lambda x: "\n".join(tw.wrap(x))
    tw.width = 80 - 25
    
    #
    parser.add_argument('arglist', nargs='*', default=list(), help='N/A')
    parser.add_argument('-t', '--translate', action="store_true", \
            dest="translate", default=False, help="Print human readable " \
            "packets. Default: %(default)s")
    parser.add_argument('-a', '--all', action="store_true", \
            dest="saveall", default=False, help="Optionally save all bytes/" \
            "packets, including malformed packets. Default: %(default)s")
    parser.add_argument('-o', '--output', action="store", \
            dest="output", default="logging_output.txt", help="Specify output " \
            "file for log of packets. Default: %(default)s")
    #
    
    options = parser.parse_args()
    args = options.arglist
    
    # TODO get rid of these globals...
    global saveall
    global translate
    saveall = options.saveall
    translate = options.translate
    
    global port
    global baud
    global timing
    global id_dict
    cfg = PyDyConfigParser()
    cfg.read()
    port, baud, __, timing, __ = cfg.get_params()
    id_dict = cfg.get_id_to_device_dict()

    if saveall:
        print "All packets (including bad packets) will be saved in " \
                "{0}".format(options.output)
    if translate:
        print "Packets will be translated for listing in this window."
    
    logger_method(outputfile=options.output)
    
    return
Exemplo n.º 3
0
from pydypackets import PyDyPackets
from pydypackets.PyDyConfig import PyDyConfigParser

cfg = PyDyConfigParser()
cfg.read()
port, baud, __, timing, __ = cfg.get_params()
id_dict = cfg.get_id_to_device_dict()


def test_vals_split_and_translate_runs():
    # Ping packet for servo id 0C.
    testpacket = [0xff, 0xff, 0x0c, 0x02, 0x01, 0xec]
    PyDyPackets.vals_split_and_translate(testpacket, 2, id_dict)


def test_vals_split_and_translate_keyerr():
    # should fail due to lack of a servo with ID FC.
    testpacket = [0xff, 0xff, 0xfc, 0x02, 0x01, 0xec]
    PyDyPackets.vals_split_and_translate(testpacket, 2, id_dict)
Exemplo n.º 4
0
def main():
    """Parse command line options
    
    """
    usage = "usage: %(prog)s [raw-input-file [options] ]\n" \
            "Program will filter and/or translate supplied raw packets.\n"
    parser = ArgumentParser(prog='pydyparser', usage=usage,
                            formatter_class=RawTextHelpFormatter)

    tw = TextWrapper()
    mywrap = lambda x: "\n".join(tw.wrap(x))
    tw.width = 80 - 25
    quicktext = "\n".join(["\n".join(tw.wrap(_)) for _ in (
            "Arg(s) will be concatenated and treated as "
            "a single packet and then parsed. Input should be space-delimited "
            "bytes. 0xff, 255, and \\xff styles are all supported. "
            "If using the latter, space delimiting is optional, but you must "
            "wrap the sequence of bytes in quotes "
            "(or escape the backslashes).\n\n"
            "Example usage:"
            "\n$ pydyparser -q 255 255 12 7 3 30 0 2 0 2 195"
            "\n$ pydyparser -q 0xFF 0xFF 0x0C 0x07 0x03 0x1E 0x00 0x02 0x00 0x02 0xC3"
            "\n$ pydyparser -q \"\\xFF\\xFF\\x0C\\x07\\x03\\x1E\\x00\\x02\\x00\\x02\\xC3\""
            "\n$ pydyparser -q \"\\xFF \\xFF \\x0C \\x07 \\x03 \\x1E \\x00 \\x02 \\x00 \\x02 \\xC3\""
            "\n\nThese all produce output:\n"
            " ['ID: 12', 'write data', 'GOAL_POSITION_L       ', 'Val:     512', "
            "'GOAL_SPEED_L', 'Val:     512', 'invalid checksum c3 (actual c9)']").splitlines()])

    #
    parser.add_argument('arglist', nargs='*', default=list(),
            help=mywrap("Path to a file to parse/translate, or list of bytes "
            "to parse/translate if using -q flag."))
    parser.add_argument('-q', '--quick', action="store_true", dest="quick",
            default=False, help=quicktext)
    parser.add_argument('-s', '--servos', action="store",
            dest="my_f_id", default=None,
            help=mywrap("A single integer "
            "or set of comma separated integers for servo IDs to keep "
            "when filtering; e.g. '-s 1,2,3'.\nDefault: %(default)s"))
    parser.add_argument('-i', '--instructions', action="store",
            dest="my_f_instr", default=None, help=mywrap("A single integer "
            "or set of comma separated integers for instructions to keep "
            "when filtering; e.g. '-i 1,2,3'.\nDefault: %(default)s"))
    parser.add_argument('-c', '--commands', action="store",
            dest="my_f_cmd", default=None, help=mywrap("A single integer "
            "or set of comma separated integers for commands to keep "
            "when filtering; e.g. '-c 1,2,3'.\nDefault: %(default)s"))
    parser.add_argument('-o', '--output', action="store",
            dest="output", default="filtered_out.txt", help=mywrap("Specify "
	    "output file for filtered list of packets. (do `-o ''` to prevent "
	    "output creation.) Default: %(default)s"))
    parser.add_argument('-t', '--translate', action="store_true",
            dest="translate", default=False, help=mywrap("Write filtered "
	    "packets in human-readable form.\nDefault: %(default)s"))
    parser.add_argument('--time', action="store_true",
            dest="timestamp", default=None, help=mywrap("Appends timestamps "
	    "to end of each translated packet (if timestamps exist). "
	    "Default: %(default)s"))
    parser.add_argument('-T', '--Tally', action="store",
            dest="my_tally_by", default=None, help=mywrap("Tally filtered "
	    "packets by command (cmd), instruction (instr) or servo ID (id). "
	    "E.g.: '-T id'. Default: %(default)s"))
    parser.add_argument('-S', '--SyncWrite', action="store_true",
            dest="sync_split", default=None, help=mywrap("Split up sync-write "
	    "packets when filtering to look for contents satisfying other "
	    "criteria. Can also be used just to create individual packets. "
            "Default: %(default)s"))
    #
    
    options = parser.parse_args()
    args = options.arglist
    
    if len(args) == 0:
        print "Command line use requires the name of a file with a packet " \
                "log. (Or a string of bytes if using --quick option.)\n" \
                "Use the -h option for more help."
        return

    cfg = PyDyConfigParser()
    cfg.read()
    __, __, __, __, itit = cfg.get_params()
    id_dict = cfg.get_id_to_device_dict()

    if options.timestamp is None:
        options.timestamp = itit

    do_filtering(options, args, id_dict)
    return
Exemplo n.º 5
0
from pydypackets import PyDyPackets
from pydypackets.PyDyConfig import PyDyConfigParser


cfg = PyDyConfigParser()
cfg.read()
port, baud, __, timing, __ = cfg.get_params()
id_dict = cfg.get_id_to_device_dict()

def test_vals_split_and_translate_runs():
    # Ping packet for servo id 0C.
    testpacket = [0xff, 0xff, 0x0c, 0x02, 0x01, 0xec]
    PyDyPackets.vals_split_and_translate(testpacket, 2, id_dict)

def test_vals_split_and_translate_keyerr():
    # should fail due to lack of a servo with ID FC.
    testpacket = [0xff, 0xff, 0xfc, 0x02, 0x01, 0xec]
    PyDyPackets.vals_split_and_translate(testpacket, 2, id_dict)
Exemplo n.º 6
0
# This should be invoked from the top level of the package directory as "python testing/Test.py"
# Currently a bunch of the checksums are wrong.
# In lieu of doing actual testing, this [was] is my current test-suite. I'm a bad man.
    
from pydypackets import PyDyPackets as PDP
from pydypackets import PyDyParser as PDPar
from pydypackets.PyDyConfig import PyDyConfigParser


cfg = PyDyConfigParser()
cfg.read()
port, baud, __, timing, __ = cfg.get_params()
z = cfg.get_id_to_device_dict() # z = id_dict

print '\t'.join(PDP.translate_packet([ \
        0XFF,0XFF,0XFE,0X18,0X83,0X1E,0X04,0X00,0X10,0X00,0X50,
        0X01,0X01,0X20,0X02,0X60,0X03,0X02,0X30,0X00,0X70,0X01,
        0X03,0X20,0X02,0X80,0X03,0X12], z) )
print "\nnon-sync-write tests:"
print '\t'.join(PDP.translate_packet([0xff,0xff,0x00,0x02,0x00,0xfd], z))

print '\t'.join(PDP.translate_packet([255,255,52,5,3,8,255,3,185], z)) #write
print '\t'.join(PDP.translate_packet([255,255,61,5,3,30,235,1,176], z)) #write
print '\t'.join(PDP.translate_packet([0xff,0xff,0x00,0x04,0x03,0x04,0x01,0xf3], z)) # write
print '\t'.join(PDP.translate_packet([0xff,0xff,0x00,0x03,0x03,0x04,0xf2], z)) # write
print '\t'.join(PDP.translate_packet([0xff,0xff,0x01,0x04,0x02,0x00,0x03,0xf5], z)) #read data
print '\t'.join(PDP.translate_packet([0xff,0xff,0x00,0x02,0x06,0xf7], z)) #reset
print '\t'.join(PDP.translate_packet([0xff,0xff,0x01,0x02,0x01,0xfb], z)) #ping
print '\t'.join(PDP.translate_packet([0xff,0xff,0x01,0x02,0x05,0xf7], z)) #action
print '\t'.join(PDP.translate_packet([0xff,0xff,0x00,0x07,0x03,0x1e,0x00,0x02,0x00,0x02,0xd3], z)) #example 18
Exemplo n.º 7
0
def plot_trends(fr, id=None, instr=3, cmd=30, make_plot=True):
    """A file stream of packets is filtered and plotted
    
    The packet stream is stored as a dictionary of lists, with each list
    corresponding to a servo ID.
    
    Parameters
    ----------
    fr : file stream
        e.g. via ``open('file', 'r')``
    (optional)
    id : list/tuple of integers
        ID #s to keep
    instr : list/tuple of integers
        instruction values to keep
    cmd : integer
        command value to keep
    make_plot : boolean
        If False, plotting is skipped.
    
    Returns
    ----------
    plotting_dict : dictionary
        the dictionary storing the lists that would be plotted
    """
    # Get values from PyDyConfigParser:
    cfg = PyDyConfigParser()
    cfg.read()
    subplot_dict = cfg.get_plot_config()
    id_dict = cfg.get_id_to_device_dict()

    # get list of packets, each packet is a list of integers
    packets = PyDyParser.filtering_method(fr, f_id=id, f_instr=instr, \
            f_cmd=cmd, sync_split=True)

    plotting_dict = {}
    for packet in packets:
        if packet[0] != "255":
            xval = [float(packet[0])]
            packet = packet[1:]
        else:
            xval = []
        
        # packet_id = str(packet[_id])
        packet_id = packet[_id]
        
        if packet_id in plotting_dict.keys():
            plotting_dict[packet_id].append(sum_single_cmd_val(packet, cmd, id_dict))
        else:
            plotting_dict[packet_id] = [ sum_single_cmd_val(packet, cmd, id_dict) ]

    # plotkeys is the list (and order) of plots to make
    if id is None:
        plotkeys = plotting_dict.keys()
    else:
        plotkeys = PyDyParser._is_list(id)
        # This preserves the user-specified order, but tosses IDs that weren't
        # in the filtered packets.
        plotkeys = [x for x in plotkeys if x in plotting_dict.keys()]

    # Generate the plots
    fignum = 1
    plt.figure(1)
    cnt = 0;
    for key in plotkeys:
        # If reached the limit of 'plots per window,' then next plots
        # are in a new window.
        if cnt == len(subplot_dict.keys()):
            fignum += 1
            plt.figure(fignum)
            cnt = 0
        
        plt.subplot(subplot_dict[cnt])
        plt.plot(plotting_dict[key])
        plt.title("{1} ID: {0}".format(key, id_dict[key]))
        
        cnt += 1

    if make_plot:
        plt.show()

    # script pauses until plot window is closed.

    return plotting_dict
Exemplo n.º 8
0
def plot_trends(fr, id=None, instr=3, cmd=30, make_plot=True):
    """A file stream of packets is filtered and plotted
    
    The packet stream is stored as a dictionary of lists, with each list
    corresponding to a servo ID.
    
    Parameters
    ----------
    fr : file stream
        e.g. via ``open('file', 'r')``
    (optional)
    id : list/tuple of integers
        ID #s to keep
    instr : list/tuple of integers
        instruction values to keep
    cmd : integer
        command value to keep
    make_plot : boolean
        If False, plotting is skipped.
    
    Returns
    ----------
    plotting_dict : dictionary
        the dictionary storing the lists that would be plotted
    """
    # Get values from PyDyConfigParser:
    cfg = PyDyConfigParser()
    cfg.read()
    subplot_dict = cfg.get_plot_config()
    id_dict = cfg.get_id_to_device_dict()

    # get list of packets, each packet is a list of integers
    packets = PyDyParser.filtering_method(fr, f_id=id, f_instr=instr, \
            f_cmd=cmd, sync_split=True)

    plotting_dict = {}
    for packet in packets:
        if packet[0] != "255":
            xval = [float(packet[0])]
            packet = packet[1:]
        else:
            xval = []

        # packet_id = str(packet[_id])
        packet_id = packet[_id]

        if packet_id in plotting_dict.keys():
            plotting_dict[packet_id].append(
                sum_single_cmd_val(packet, cmd, id_dict))
        else:
            plotting_dict[packet_id] = [
                sum_single_cmd_val(packet, cmd, id_dict)
            ]

    # plotkeys is the list (and order) of plots to make
    if id is None:
        plotkeys = plotting_dict.keys()
    else:
        plotkeys = PyDyParser._is_list(id)
        # This preserves the user-specified order, but tosses IDs that weren't
        # in the filtered packets.
        plotkeys = [x for x in plotkeys if x in plotting_dict.keys()]

    # Generate the plots
    fignum = 1
    plt.figure(1)
    cnt = 0
    for key in plotkeys:
        # If reached the limit of 'plots per window,' then next plots
        # are in a new window.
        if cnt == len(subplot_dict.keys()):
            fignum += 1
            plt.figure(fignum)
            cnt = 0

        plt.subplot(subplot_dict[cnt])
        plt.plot(plotting_dict[key])
        plt.title("{1} ID: {0}".format(key, id_dict[key]))

        cnt += 1

    if make_plot:
        plt.show()

    # script pauses until plot window is closed.

    return plotting_dict