コード例 #1
0
def main():

    if len(sys.argv) < 2:
        usage()
        print 'ERROR: not enough paramters'
        exit(1)

    port_name = sys.argv[1]

    #
    #   Initialize serial connection
    #
    print 'Initializing serial connection on', port_name
    ser = CommsSerial(port_name, 115200)

    #
    #   Initialize PyGame
    #
    pygame.init()

    screen = pygame.display.set_mode((640,480))

    is_up = False
    is_down = False
    is_left = False
    is_right = False

    running = True
    while(running):

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    is_up = True
                if event.key == pygame.K_DOWN:
                    is_down = True
                if event.key == pygame.K_LEFT:
                    is_left = True
                if event.key == pygame.K_RIGHT:
                    is_right = True
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP:
                    is_up = False
                if event.key == pygame.K_DOWN:
                    is_down = False
                if event.key == pygame.K_LEFT:
                    is_left = False
                if event.key == pygame.K_RIGHT:
                    is_right = False

        print 'is_up: ', is_up
        print 'is_down: ', is_down

        left_x = 0
        left_y = 0
        right_x = 0
        right_y = 0
        throttle = 0

        if is_up: left_y -= 1.0
        if is_down: left_y += 1.0
        if is_left: left_x -= 1.0
        if is_right: left_x += 1.0

        button_0 = False
        button_1 = False
        button_2 = False
        button_3 = False
        button_4 = False
        button_5 = False
        button_6 = False
        button_7 = False
        button_8 = False
        button_9 = False


        print 'type: ', type(button_0)

        msb = struct.pack('fffff??????????', left_x, left_y, 
                          right_x, right_y, throttle,
                          button_0, button_1, button_2,
                          button_3, button_4, button_5,
                          button_6, button_7, button_8,
                          button_9 )

        print 'Sending: '
        for m in msb:
            print hex(ord(m)),
        print ""
        ser.send( 0x0002, msb )

        screen.fill( (0, 0, 0) )
        pygame.draw.circle( screen, (255,0,0), (50,50), 10 )
        pygame.display.flip()

        time.sleep(0.25)
コード例 #2
0
def main():

    if len(sys.argv) < 2:
        usage()
        print "ERROR: not enough paramters"
        exit(1)

    port_name = sys.argv[1]

    #
    #   Initialize serial connection
    #
    print "Initializing serial connection on", port_name
    ser = CommsSerial(port_name, 115200)

    hex = Hex(color.white, 0.8)
    hex.set_raw(
        1500,
        1500,
        1500,  # leg rear left
        1500,
        1500,
        1500,  # leg middle left
        2000,
        2000,
        2000,  # leg front left
        2000,
        2000,
        2000,  # leg front right
        1500,
        1500,
        1500,  # leg middle right
        1500,
        1500,
        1500,
    )  # leg rear right
    hex.frame.pos = (0, 120, 0)

    draw_grid()

    while True:
        rate(100)
        hex.set_raw(
            1500,
            1500,
            1500,  # leg rear left
            1500,
            1500,
            1500,  # leg middle left
            2000,
            2000,
            2000,  # leg front left
            2000,
            2000,
            2000,  # leg front right
            1500,
            1500,
            1500,  # leg middle right
            1500,
            1500,
            1500,
        )  # leg rear right

    b = time.time()

    print "Sengin msg"
    ser.ser.flushInput()
    ser.send(0x0002, None)
    message = ser.recv_timeout(0.5)

    for m in message:
        print hex(ord(m)), " ",
    print ""
コード例 #3
0
def main():

    if len(sys.argv) < 2:
        usage()
        print 'ERROR: not enough paramters'
        exit(1)

    port_name = sys.argv[1]

    #
    #   Initialize serial connection
    #
    print 'Initializing serial connection on', port_name
    ser = CommsSerial(port_name, 115200)

    #
    #   Initialize joystick
    #
    joy = initialize_pygame_joystick()
    if joy == None:
        print 'ERROR: No joystick found'
        exit(1)


    running = True
    while(running):

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
                
        left_x = joy.get_axis(0)
        left_y = -joy.get_axis(1)
        right_x = joy.get_axis(3)
        right_y = -joy.get_axis(2)
        throttle = joy.get_axis(4)

        button_0 = joy.get_button(0)
        button_1 = joy.get_button(1)
        button_2 = joy.get_button(2)
        button_3 = joy.get_button(3)
        button_4 = joy.get_button(4)
        button_5 = joy.get_button(5)
        button_6 = joy.get_button(6)
        button_7 = joy.get_button(7)
        button_8 = joy.get_button(8)
        button_9 = joy.get_button(9)


        print 'type: ', type(button_0)

        msb = struct.pack('fffff??????????', left_x, left_y, 
                          right_x, right_y, throttle,
                          button_0, button_1, button_2,
                          button_3, button_4, button_5,
                          button_6, button_7, button_8,
                          button_9 )

        print 'Sending: '
        for m in msb:
            print hex(ord(m)),
        print ""
        ser.send( 0x0002, msb )

        time.sleep(0.25)