def send_cmds_to_ev3(ev3): """ This function reads the global command queue cmd_q and sends each of the command to the EV3 with an appropriate delay between them. The param ev3 is the pointer to the EV3. This is meant to be run as a thread """ inter_cmd_wait = 2 # Sec. Min delay between successive commands print('ENTERED send_cmds_to_ev3 function/thread, cmd_q length is {}', format(cmd_q.qsize())) while True: try: cmd = cmd_q.get( ) # This should block the thread if the queue is empty print("-> Command from file is {}".format(cmd)) if len(cmd) == 0 or cmd.isspace( ): # Skip commands that are blank or only whitespace continue 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 if cmd == "STOPEV3": ev3.close() return #Thread terminates else: time.sleep( inter_cmd_wait ) # wait some time until the next commad can be sent except KeyboardInterrupt: break
def process_event(assistant, event): # Function returns a Boolean. True means user asked the conversation to end; false otherwise global EV3 # THis is the pointer to the LEGO EV3 print('*** PROCESS EVENT: Point 0--Begin Process Event') status_ui = aiy.voicehat.get_status_ui() if event.type == EventType.ON_START_FINISHED: print('*** PROCESS EVENT: Point 1--ON_START_FINISHED') status_ui.status('ready') if sys.stdout.isatty(): print('Say "OK, Google" then speak, or press Ctrl+C to quit...') elif event.type == EventType.ON_CONVERSATION_TURN_STARTED: print('*** PROCESS EVENT: Point 2--ON CONVERSATION TURN STARTED') status_ui.status('listening') elif event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED and event.args: print('You said:', event.args['text']) print('*** PROCESS EVENT: Point 3--ON RECOGNIZING_SPEECH_FINISHED') text = event.args['text'].lower() if text == 'power off': print('*** PROCESS EVENT: Point 4--Local Power Off') assistant.stop_conversation() power_off_pi() elif text == 'reboot': print('*** PROCESS EVENT: Point 5--Local Reboot') assistant.stop_conversation() reboot_pi() elif text == 'ip address': print('*** PROCESS EVENT: Point 6--Local IP Address') assistant.stop_conversation() say_ip() elif text == 'ev3 forward' or text == 'lego forward': print('*** PROCESS EVENT: Point 7--Local EV3 Forward') assistant.stop_conversation() ev3Msg = "FWD" print('*** ENTERING messageGuin') ## m = ev3_rpi_ctrl_pkg.messageGuin("EV3-CMD", 10.1,"number") # SPG: DELETE THIS cFOR TEST ONLY m = ev3_rpi_ctrl_pkg.messageGuin( "EV3-CMD", ev3Msg, "text" ) # SPG: UNCOMMENT THIS convert message; select EV3-CMD block to send to ## print('*** EV3 is {}; EV3.isOpen() is {}'.format(EV3)) ## print('*** EV3.isOpen() is {}'.format(EV3.isOpen())) print('*** After messageGuin()') print('-> EV3 Settings', EV3.get_settings()) ## ev3_rpi_ctrl_pkg.messageSendX(EV3) # DELETE FOR DEBUGGING ## print('*** Enter messageSendY') ## ev3_rpi_ctrl_pkg.messageSendY(m) # DELETE FOR DEBUGGING ## ev3_rpi_ctrl_pkg.messageSend(EV3, m) # send converted message # DUPLICXATE OF BELOW if EV3 is not None and EV3.isOpen() is True: print('\n -> Sending to EV3 msg: {} \n'.format(ev3Msg)) print('\n -> Encoded message to EV3: {} \n'.format(m)) print('*** Message is of type {}'.format(type(m))) ## print('*** Is instance of unicode {}'.format(isinstance(m, unicode))) print('*** ENTERING messageSend') ev3_rpi_ctrl_pkg.messageSend(EV3, m) # send converted message ## time.sleep(5) # Wait 5 sec; MAY BE NOT NEEDED else: print("\n Can't send--EV3 is not open \n") elif text == 'ev3 open' or text == 'open ev3' or text == 'open lego' or text == 'lego open': print('*** PROCESS EVENT: Point 8--Local EV3 Open') assistant.stop_conversation() EV3, ev3Port = open_connected_ev3() # Try to open the EV3 ## print('**** Returned from open_connected_ev3()') if EV3 is not None: aiy.audio.say('EV3 has been opened') ## print('**** EV3 type',type(EV3)) ## print('**** EV3 name',EV3.name) print('-> EV3 Settings', EV3.get_settings()) else: aiy.audio.say('EV3 has not been opened') elif text == 'exit': print('*** PROCESS EVENT: Point 9--LocaL eXIT') assistant.stop_conversation() exit_pi() return True # Indicate that user asked that the conversation end elif event.type == EventType.ON_END_OF_UTTERANCE: print('*** PROCESS EVENT: Point 10--ON_END_OF_UTTERANCE') status_ui.status('thinking') elif (event.type == EventType.ON_CONVERSATION_TURN_FINISHED or event.type == EventType.ON_CONVERSATION_TURN_TIMEOUT or event.type == EventType.ON_NO_RESPONSE): print('*** PROCESS EVENT: Point 11') status_ui.status('ready') elif event.type == EventType.ON_ASSISTANT_ERROR and event.args and event.args[ 'is_fatal']: print('*** PROCESS EVENT: Point 12--FATAL') sys.exit(1) print('*** PROCESS EVENT: Point 13--EXIT PROCESS EVENT') return False # User DID NOT ask for conversation to end
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 process_event(assistant, event): status_ui = aiy.voicehat.get_status_ui() if event.type == EventType.ON_START_FINISHED: status_ui.status('ready') if sys.stdout.isatty(): print('Say "OK, Google" then speak, or press Ctrl+C to quit...') elif event.type == EventType.ON_CONVERSATION_TURN_STARTED: status_ui.status('listening') elif event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED and event.args: print('You said:', event.args['text']) text = event.args['text'].lower() if text == 'power off': assistant.stop_conversation() power_off_pi() elif text == 'reboot': assistant.stop_conversation() reboot_pi() elif text == 'ip address': assistant.stop_conversation() say_ip() elif text == 'ev3 forward' or text == 'lego forward': ev3Msg = "FWD" print('*** ENTERING messageGuin') m = ev3_rpi_ctrl_pkg.messageGuin( "EV3-CMD", ev3Msg, "text") # convert message; select EV3-CMD block to send to ## print('*** EV3 is {}; EV3.isOpen() is {}'.format(EV3)) ## print('*** EV3.isOpen() is {}'.format(EV3.isOpen())) print('*** After messageGuin() \n *** Enter messageSendX') print('-> EV3 Settings', EV3.get_settings()) ## ev3_rpi_ctrl_pkg.messageSendX(EV3) # DELETE FOR DEBUGGING ## print('*** Enter messageSendY') ## ev3_rpi_ctrl_pkg.messageSendY(m) # DELETE FOR DEBUGGING ## ev3_rpi_ctrl_pkg.messageSend(EV3, m) # send converted message # DUPLICXATE OF BELOW if EV3 is not None and EV3.isOpen() is True: print('\n -> Sending to EV3 msg: {} \n'.format(ev3Msg)) print('*** ENTERING messageSend') ev3_rpi_ctrl_pkg.messageSend(EV3, m) # send converted message else: print("\n Can't send--EV3 is not open \n") elif text == 'ev3 open' or text == 'open ev3' or text == 'open lego' or text == 'lego open': EV3, ev3Port = open_connected_ev3() # Try to open the EV3 ## print('**** Returned from open_connected_ev3()') if EV3 is not None: aiy.audio.say('EV3 has been opened') ## print('**** EV3 type',type(EV3)) ## print('**** EV3 name',EV3.name) print('-> EV3 Settings', EV3.get_settings()) else: aiy.audio.say('EV3 has not been opened') elif text == 'exit': assistant.stop_conversation() exit_pi() return True # Indicate that user asked that the conversation end elif event.type == EventType.ON_END_OF_UTTERANCE: status_ui.status('thinking') elif (event.type == EventType.ON_CONVERSATION_TURN_FINISHED or event.type == EventType.ON_CONVERSATION_TURN_TIMEOUT or event.type == EventType.ON_NO_RESPONSE): status_ui.status('ready') elif event.type == EventType.ON_ASSISTANT_ERROR and event.args and event.args[ 'is_fatal']: sys.exit(1)
def process_event(assistant, led, event): # Function returns a Boolean. True means user asked the conversation to end; false otherwise global EV3 # THis is the pointer to the LEGO EV3 logging.info(event) if event.type == EventType.ON_START_FINISHED: led.state = Led.BEACON_DARK # Ready. print('Say "OK, Google" then speak, or press Ctrl+C to quit...') elif event.type == EventType.ON_CONVERSATION_TURN_STARTED: led.state = Led.ON # Listening. elif event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED and event.args: print('You said:', event.args['text']) text = event.args['text'].lower() if text == 'power off': assistant.stop_conversation() power_off_pi() elif text == 'reboot': assistant.stop_conversation() reboot_pi() elif text == 'ip address': assistant.stop_conversation() say_ip() elif text == 'ev3 forward' or text == 'lego forward': ev3Msg = "FWD" print('*** ENTERING messageGuin') m = ev3_rpi_ctrl_pkg.messageGuin( "EV3-CMD", ev3Msg, "text") # convert message; select EV3-CMD block to send to ## print('*** EV3 is {}; EV3.isOpen() is {}'.format(EV3)) ## print('*** EV3.isOpen() is {}'.format(EV3.isOpen())) print('*** After messageGuin() \n *** Enter messageSendX') print('-> EV3 Settings', EV3.get_settings()) ## ev3_rpi_ctrl_pkg.messageSendX(EV3) # DELETE FOR DEBUGGING ## print('*** Enter messageSendY') ## ev3_rpi_ctrl_pkg.messageSendY(m) # DELETE FOR DEBUGGING ## ev3_rpi_ctrl_pkg.messageSend(EV3, m) # send converted message # DUPLICXATE OF BELOW if EV3 is not None and EV3.isOpen() is True: print('\n -> Sending to EV3 msg: {} \n'.format(ev3Msg)) print('*** ENTERING messageSend') ev3_rpi_ctrl_pkg.messageSend(EV3, m) # send converted message else: print("\n Can't send--EV3 is not open \n") elif text == 'ev3 open' or text == 'open ev3' or text == 'open lego' or text == 'lego open': EV3, ev3Port = open_connected_ev3() # Try to open the EV3 print('**** Returned from open_connected_ev3()') if EV3 is not None: tts.say('EV3 has been opened') print('**** EV3 type', type(EV3)) print('**** EV3 name', EV3.name) print('-> EV3 Settings', EV3.get_settings()) else: tts.say('EV3 has not been opened') elif text == 'exit': assistant.stop_conversation() exit_pi() return True # Indicate that user asked that the conversation end elif event.type == EventType.ON_END_OF_UTTERANCE: led.state = Led.PULSE_QUICK # Thinking. elif (event.type == EventType.ON_CONVERSATION_TURN_FINISHED or event.type == EventType.ON_CONVERSATION_TURN_TIMEOUT or event.type == EventType.ON_NO_RESPONSE): led.state = Led.BEACON_DARK # Ready. elif event.type == EventType.ON_ASSISTANT_ERROR and event.args and event.args[ 'is_fatal']: sys.exit(1) return False # User DID NOT ask for conversation to end
def send_cmds_to_ev3(ev3, ex_event): """ This function reads the global command queue cmd_q and sends each of the command to the EV3 with an appropriate delay between them. The param ev3 is the pointer to the EV3. This is meant to be run as a thread. The ex_event parameter is an event() object in the threading package that allows this thread to shut down the other threads when it processes the EXIT command. """ # global thread_stop inter_cmd_wait = 2 # Sec. Min delay between successive commands print('ENTERED send_cmds_to_ev3 function/thread, cmd_q length is {}', format(cmd_q.qsize())) while not ex_event.is_set(): try: cmd = cmd_q.get( block=True, timeout=inter_cmd_wait ) # This should block the thread if the queue is empty print("-> Command from file is {}".format(cmd)) if len(cmd) == 0 or cmd.isspace( ): # Skip commands that are blank or only whitespace continue if cmd == "EXIT": # thread_stop = True # Stop all threads and allow the program to exit ex_event.set( ) #Sets the ex_event to signal other threads to shut themselves down print( "-> FROM send_cmds_to_ev3, processing EXIT, thread_stop = {}" .format(ex_event.is_set())) break # Close the open file and end the thread 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 # if cmd=="STOPEV3": # break # thread terminates gracefully ## ev3.close() ## return #Thread terminates # else: # time.sleep(inter_cmd_wait) # wait some time until the next commad can be sent # The command STOPEV3 should not terminate the thread time.sleep(inter_cmd_wait ) # wait some time until the next commad can be sent except KeyboardInterrupt: break except Queue.Empty as e: # print('FROM send_cmds_to_ev3, EXCEPTION IS {}'.format(e)) continue ev3.close() print('EXITED send_cmds_to_ev3') return
def send_cmds_to_ev3(ev3, ex_event): """ This function reads the global command queue cmd_q and sends each of the command to the EV3 with an appropriate delay between them. The param ev3 is the pointer to the EV3. This is meant to be run as a thread. The ex_event parameter is an event() object in the threading package that allows this thread to shut down the other threads when it processes the EXIT command. The var 'cmd' is now sent/specified as 'msg_type' should be 'text', 'number' or 'logic'. It controls the type of encoding that get sent to the EV3. The parameter dest_ev3_block is the message block in the EV3 program that will receive this message. The name of the block (destination) has to be encoded into the message. """ # global thread_stop inter_cmd_wait = 2 # Sec. Min delay between successive commands print('ENTERED send_cmds_to_ev3 function/thread, cmd_q length is {}', format(cmd_q.qsize())) while not ex_event.is_set(): try: cmd_line = cmd_q.get( block=True, timeout=inter_cmd_wait ) # This should block the thread if the queue is empty print("-> Command from file is {}".format(cmd_line)) if len(cmd_line) == 0 or cmd_line.isspace( ): # Skip commands that are blank or only whitespace continue cmd_line_tokens = cmd_line.split() if len(cmd_line_tokens) < 2: print('** Bad command: {}'.format(cmd_line)) continue elif len(cmd_line_tokens) >= 2 and cmd_line_tokens[1] == "EXIT": ex_event.set( ) #Sets the ex_event to signal other threads to shut themselves down print( "-> FROM send_cmds_to_ev3, processing EXIT, thread_stop = {}" .format(ex_event.is_set())) break # Close the open file and end the thread dest_ev3_block = cmd_line_tokens[0] cmd = cmd_line_tokens[ 1] #This can either be a command (like FWD) or the number from a parameter (like PARAM_TIME) # if cmd == "EXIT": # # thread_stop = True # Stop all threads and allow the program to exit # ex_event.set() #Sets the ex_event to signal other threads to shut themselves down # print("-> FROM send_cmds_to_ev3, processing EXIT, thread_stop = {}".format(ex_event.is_set())) # break # Close the open file and end the thread # Figure out if the command/parameter is all 'numeric' or not. If it is, send as numberic; otherwise, send as 'text' try: cmd_num = float(cmd) cmd = cmd_num msg_type = 'number' except ValueError: if cmd.lower() == 'true': cmd = True msg_type = 'logic' elif cmd.lower() == 'false': msg_type = 'logic' cmd = False else: msg_type = 'text' if msg_type == 'number': m = ev3_rpi_ctrl_pkg.messageGuin( dest_ev3_block, cmd, msg_type ) # convert message; select EV3-CMD block to send to # m = ev3_rpi_ctrl_pkg.encodeMessage(ev3_rpi_ctrl_pkg.MessageType.Numeric, 'EV3-PARAM', cmd) elif msg_type == 'text': m = ev3_rpi_ctrl_pkg.messageGuin( dest_ev3_block, cmd, msg_type ) # convert message; select EV3-CMD block to send to # m = ev3_rpi_ctrl_pkg.encodeMessage(ev3_rpi_ctrl_pkg.MessageType.Text, 'EV3-CMD', cmd) print('** Sending to EV3 msg: {} msg_type: {} type: {}'.format( cmd, msg_type, type(cmd))) ev3_rpi_ctrl_pkg.messageSend(ev3, m) # send converted message # if cmd=="STOPEV3": # break ' # thread terminates gracefully ## ev3.close() ## return #Thread terminates # else: # time.sleep(inter_cmd_wait) # wait some time until the next commad can be sent # The command STOPEV3 should not terminate the thread time.sleep(inter_cmd_wait ) # wait some time until the next commad can be sent except KeyboardInterrupt: break except Queue.Empty as e: # print('FROM send_cmds_to_ev3, EXCEPTION IS {}'.format(e)) continue ev3.close() print('EXITED send_cmds_to_ev3') return