Exemple #1
0
def main():

    ev3_cmd_filename = '/home/pi/Lego_ev3/ev3_cmds.txt'  # Command destined for the EV3 should be written here by voice_assist_ev3_ctrlx.py
    wait_period = 0.25  # SEC.  This is the amount of time to wait for the voice_assist_ev3_ctrlx to creat a file

    ev3, ev3PortOpen = ev3_rpi_ctrl_pkg.openEv3(
    )  #Port poitner abd ID if successful, None otherwise

    if ev3PortOpen is not None:
        print('\nOpened EV3 Brick on {}'.format(ev3PortOpen))
        # Get the pointer to the open BT interface
    else:  # If no port are found

        print('EV3 does not appear to be open on any /dev/rfcomm port')
        sys.exit()


##    poll_cmd_file(ev3, ev3_cmd_filename, wait_period)           # Look for commands in command file and send to EV3

    poll_cmd_thr = threading.Thread(target=poll_cmd_file,
                                    name='Command_Poll',
                                    args=(ev3, ev3_cmd_filename, wait_period))
    print('STARTING Command_Poll thread')
    poll_cmd_thr.start()
    print('JOINING Command_Poll thread')
    poll_cmd_thr.join()

    print('ENDED Command_Poll thread')

    print('ENDED Main thread')

    sys.exit()
Exemple #2
0
def main():
    global cmd_q  # global command queue.  THis is the command queue
    # tga the Command_Poll threads will read command into

    ev3_cmd_filename = '/home/pi/Lego_ev3/ev3_cmds.txt'  # Command destined for the EV3 should be written here by voice_assist_ev3_ctrlx.py
    wait_period = 0.25  # SEC.  This is the amount of time to wait for the voice_assist_ev3_ctrlx to creat a file

    ev3, ev3PortOpen = ev3_rpi_ctrl_pkg.openEv3(
    )  #Port poitner abd ID if successful, None otherwise

    if ev3PortOpen is not None:
        print('\nOpened EV3 Brick on {}'.format(ev3PortOpen))
        # Get the pointer to the open BT interface
    else:  # If no port are found

        print('EV3 does not appear to be open on any /dev/rfcomm port')
        sys.exit()


##    poll_cmd_file(ev3, ev3_cmd_filename, wait_period)           # Look for commands in command file and send to EV3

    cmd_q = Queue.Queue(
    )  # THis is the command queue tga the Command_Poll threads will read command into

    ## THIS IS TEMP FOR DEBUGGING.  MAKE THIS A CONCURRENT THREAD
    ##    enter_user_cmds()

    poll_cmd_thr = threading.Thread(target=poll_cmd_file,
                                    name='Command_Poll',
                                    args=(ev3_cmd_filename, wait_period))
    print('STARTING Command_Poll thread')
    poll_cmd_thr.start()
    ##    print('JOINING Command_Poll thread')
    ##    poll_cmd_thr.join()

    ##    print('ENDED Command_Poll thread')

    send_cmd_thr = threading.Thread(
        target=send_cmds_to_ev3, name='Send_Cmds_to_EV3',
        args=(ev3, ))  # Thread waits for command in cmd_q and sends to EV3
    print('STARTING Send_Cmds_to_EV3 thread')
    send_cmd_thr.start()

    user_cmd_thr = threading.Thread(
        target=enter_user_cmds,
        name="User_Cmd_Thread")  # Thead allows user to enter commands
    user_cmd_thr.start()

    print('JOINING Send_Cmds_to_EV3 thread')
    send_cmd_thr.join()

    print('COMMAND QUEUE HAS {} ITEMS.  ITEMS ARE:'.format(cmd_q.qsize()))
    for i in range(cmd_q.qsize()):
        c = cmd_q.get()
        print("ITEM {}: {}".format(i, c))

    print('ENDED Main thread')

    sys.exit()
Exemple #3
0
def main():

    ev3_cmd_filename = '/home/pi/Lego_ev3/ev3_cmds.txt'  # Command destined for the EV3 should be written here by voice_assist_ev3_ctrlx.py
    wait_period = 0.25  # SEC.  This is the amount of time to wait for the voice_assist_ev3_ctrlx to creat a file

    ev3, ev3PortOpen = ev3_rpi_ctrl_pkg.openEv3(
    )  #Port poitner abd ID if successful, None otherwise

    if ev3PortOpen is not None:
        print('\nOpened EV3 Brick on {}'.format(ev3PortOpen))
        # Get the pointer to the open BT interface
    else:  # If no port are found

        print('EV3 does not appear to be open on any /dev/rfcomm port')
        sys.exit()

    while True:

        try:
            ev3_file = open(ev3_cmd_filename, "r")
        except IOError:
            time.sleep(wait_period)  # Wait for the file to be created
            continue  # and try again
        except KeyboardInterrupt:
            break

        cmds = ev3_file.read().splitlines(
        )  # get all commands and remove the line terminatorsd  (\n)

        try:

            for cmd in cmds:
                print("-> Command from file is {}".format(cmd))
                if cmd is not None:
                    m = ev3_rpi_ctrl_pkg.messageGuin(
                        "EV3-CMD", cmd, "text"
                    )  #  convert message; select EV3-CMD block to send to
                    print('Sending to EV3 msg: {}'.format(cmd))
                    ev3_rpi_ctrl_pkg.messageSend(ev3,
                                                 m)  # send converted message
                    time.sleep(3)  # DELETE--FOR DEBUGGING ONLY

        except KeyboardInterrupt:
            break

        if ev3_file is not None:
            os.remove(
                ev3_cmd_filename
            )  # Delete processed commands so that voice_assist_ev3_ctrl can make more

    ev3.close()
    sys.exit()
def open_connected_ev3():
    # This opens the LEGO EV3 on the bluetooth (BT) interface.  Note that the BT interface must be on and
    # paired with the LEGO EV3 for this to work.  Function returns the pointer to the serial BT interface
    # (if successful) or None if not successful
    # print('** ENTERED open_connected_ev3')
    EV3, ev3PortOpen = ev3_rpi_ctrl_pkg.openEv3()               # Port ID if successful, False otherwise
    # print('** PERFORMED ev3_rpi_ctrl_pkg.openEv3')
    # print('\n-> Ev3PortOpen {}\n'.format(ev3PortOpen))

    if ev3PortOpen is not None:
        print('\n-> Opened EV3 Brick on {}'.format(ev3PortOpen))   # Get the pointer to the open BT interface
##        print('*** EV3 type',type(EV3))
        print('\n-> EV3 Settings: name', EV3.name, EV3.get_settings())
##        print('*** Returning from open_connected_ev3()')
        return EV3, ev3PortOpen
    else:       # If no port are found
        print('\n** EV3 does not appear to be open on any /dev/rfcomm port\n')
        aiy.audio.say('EV3 does not appear to be open on any /dev/rfcomm port')
        print('*** Returning from open_connected_ev3()')
        return None, None
def main():
##    EV3 = serial.Serial('/dev/rfcomm5',timeout=1)   
##    printSerIntInfo(EV3)
    
    global secSinceLastRx, RxToPeriod
    
##    global EV3

    timeStart = datetime.datetime.now()
    EV3, ev3PortOpen = ev3_rpi_ctrl_pkg.openEv3()               #Port poitner abd ID if successful, None otherwise
##    ev3PortOpen = ev3Info[0]
    
    print('# Ev3PortOpen {}'.format(ev3PortOpen))

##    ev3PortOpen, EV3 = ev3_rpi_ctrl_pkg.openEv3()               #Port ID if successful, False otherwise
##    ev3PortOpen = ev3Info[0]

    if ev3PortOpen is not None:
        print('\nOpened EV3 Brick on {}'.format(ev3PortOpen))
                 # Get the pointer to the open BT interface
    else:       # If no port are found
        
        print('EV3 does not appear to be open on any /dev/rfcomm port')
        sys.exit()
 

    #Debugging
##    print('End device name = ',EV3.name)
##    print('Baud rate = ', EV3.baudrate)

    timeLastRx = datetime.datetime.now()
    print("Local Time Now {}\n".format(timeLastRx))

    RxToPeriod = 5      #Seconds.  This is the amount of time that can pass before doing a buffer reset
    maxRxWait = 4       #Max time in sec to wait for message from EV3
    
    try:
    
        while True:
            userCmd = input('Command? ')                     # REALLY NOT SURE WHAT THE PROBLEM HERE IS
            userCmdUpper = userCmd.upper().strip()               #Convert to upper ans strip all spaces
            
##            userCmdUpper = getUserInput()
##            print('** COMMAND IS {}'.format(userCmdUpper))
            
            
            if "FORWARD" == userCmdUpper or "FWD" == userCmdUpper:
                ev3Msg = "FWD"
            elif "BACK" == userCmdUpper or "BACKWARDS" == userCmdUpper or "RVS" == userCmdUpper or "REVERSE" == userCmdUpper:
                ev3Msg = "RVS"
            elif "STOP" == userCmdUpper:
                ev3Msg = "STOP"
            elif "JOSHISCOOL" == userCmdUpper:
                ev3Msg = "JOSHISCOOL"
            elif "STOPEV3" == userCmdUpper or "STOPPROGRAM" == userCmdUpper:
                ev3Msg = "STOPEV3"
            elif "EXIT" == userCmdUpper or "END" == userCmdUpper:
                break
            else:
                ev3Msg = None
                
            if ev3Msg is not None:
##                err = ev3_rpi_ctrl_pkg.msg_fmt_send(EV3, "EV3-CMD",ev3Msg,"text")
##                if err:
##                    print('*** Error sending {}, err no {}'.format(ev3Msg, err))                             
                                                    
                m = ev3_rpi_ctrl_pkg.messageGuin("EV3-CMD",ev3Msg,"text")  #  convert message; select EV3-CMD block to send to
                print('Sending to EV3 msg: {}'.format(ev3Msg))
                ev3_rpi_ctrl_pkg.messageSend(EV3, m) # send converted message
            else:
                print('** Error with {}'.format(userCmd))
                
            time.sleep(2)                                # Just slow the loop a little
                
    except KeyboardInterrupt:
        # Send a stop message and stop the robot
        ev3Msg = "STOP"
        m = ev3_rpi_ctrl_pkg.messageGuin("EV3-CMD",ev3Msg,"text")  #  convert message; select EV3-CMD block to send to
        print('Sending to EV3 msg {}'.format(ev3Msg))
        ev3_rpi_ctrl_pkg.messageSend(EV3, m) # send converted message

    
    EV3.close()
    sys.exit()
def main():
    global cmd_q  # global command queue.  THis is the command queue
    # tga the Command_Poll threads will read command in
    global thread_stop  # This is a signal to the threads to stop themselves.  This is a bad way of
    # sending this signal because it forces the threads to use common memory.  The
    # better way is to convert the threads to real classes and send in a semafore
    # requesting a stop

    thread_stop = False  # Initially, allow all subordinate threads to run

    ev3_cmd_filename = '/home/pi/Lego_ev3/ev3_cmds.txt'  # Command destined for the EV3 should be written here by voice_assist_ev3_ctrlx.py
    wait_period = 0.25  # SEC.  This is the amount of time to wait for the voice_assist_ev3_ctrlx to creat a file

    ev3, ev3PortOpen = ev3_rpi_ctrl_pkg.openEv3(
    )  #Port poitner abd ID if successful, None otherwise

    if ev3PortOpen is not None:
        print('\nOpened EV3 Brick on {}'.format(ev3PortOpen))
        # Get the pointer to the open BT interface
    else:  # If no port are found

        print('EV3 does not appear to be open on any /dev/rfcomm port')
        sys.exit()


##    poll_cmd_file(ev3, ev3_cmd_filename, wait_period)           # Look for commands in command file and send to EV3

    cmd_q = Queue.Queue(
    )  # THis is the command queue tga the Command_Poll threads will read command into

    ## THIS IS TEMP FOR DEBUGGING.  MAKE THIS A CONCURRENT THREAD
    ##    enter_user_cmds()

    poll_cmd_thr = threading.Thread(target=poll_cmd_file,
                                    name='Command_Poll',
                                    args=(ev3_cmd_filename, wait_period))
    print('STARTING Command_Poll thread')
    poll_cmd_thr.start()
    ##    print('JOINING Command_Poll thread')
    #     poll_cmd_thr.join()

    ##    print('ENDED Command_Poll thread')

    send_cmd_thr = threading.Thread(
        target=send_cmds_to_ev3, name='Send_Cmds_to_EV3',
        args=(ev3, ))  # Thread waits for command in cmd_q and sends to EV3
    print('STARTING Send_Cmds_to_EV3 thread')
    send_cmd_thr.start()
    send_cmd_thr.join()

    #     user_cmd_thr = threading.Thread(target=enter_user_cmds, name='Enter_User_Cmds')                 # Thread allows the user to enter commands throuhg the console
    #     user_cmd_thr.start()
    #     user_cmd_thr.join()                                                                             # Dont start these user_cmd_thr threads.  This will be done by a separate Python program
    #     enter_user_cmds()                       # Allow user to enter commands
    #     thread_stop = True                      # stop all other thread so that main thread can exit

    # When the user exits the command session, terminate the threads
    ##    poll_cmd_thr._stop()
    ##    send_cmd_thr._stop()

    ##    user_cmd_thr = threading.Thread(target=enter_user_cmds, name="User_Cmd_Thread")                 # Thead allows user to enter commands
    ##    user_cmd_thr.start()

    ##    print('JOINING Send_Cmds_to_EV3 thread')
    ##    send_cmd_thr.join()
    ##
    ##    print('COMMAND QUEUE HAS {} ITEMS.  ITEMS ARE:'.format(cmd_q.qsize()))
    ##    for i in range(cmd_q.qsize()):
    ##          c = cmd_q.get()
    ##          print("ITEM {}: {}".format(i,c))

    print('ENDED Main thread')

    sys.exit()

    return