example_client.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 from __future__ import print_function
00003 
00004 import os
00005 import signal
00006 import sys
00007 import threading
00008 import time
00009 
00010 import actionlib
00011 import actionlib_msgs.msg as actionlib_msgs
00012 import rospy
00013 
00014 import gcloud_speech_msgs.msg as gcloud_speech_msgs
00015 
00016 GoalStatus = actionlib_msgs.GoalStatus
00017 
00018 def SpeechToTextSimpleExampleClient():
00019   def DoneCallback(state, result):
00020     print("\n\nDone, state {}, result:\n{}\n".format(state,result))
00021 
00022 
00023   def ActiveCallback():
00024     print("The goal is now active.\n")
00025 
00026 
00027   def FeedbackCallback(feedback):
00028     print("{}\n".format(feedback))
00029 
00030 
00031   global sigint_received
00032   sigint_received = False
00033 
00034   def SignalIntHandler(signal, frame):
00035     print("Received SIGINT.")
00036     global sigint_received
00037     sigint_received = True
00038 
00039 
00040   def DelayedExit(wait_time):
00041     time.sleep(wait_time)
00042     os._exit(0)
00043 
00044 
00045   signal.signal(signal.SIGINT, SignalIntHandler)
00046 
00047   client = actionlib.SimpleActionClient(
00048     "/cogrob/speech_to_text", gcloud_speech_msgs.SpeechToTextAction)
00049   client.wait_for_server()
00050 
00051   goal = gcloud_speech_msgs.SpeechToTextGoal()
00052   client.send_goal(goal, done_cb=DoneCallback, active_cb=ActiveCallback,
00053                    feedback_cb=FeedbackCallback)
00054 
00055   while client.get_state() in [GoalStatus.PENDING, GoalStatus.ACTIVE]:
00056     time.sleep(1)
00057     if sigint_received:
00058       break
00059 
00060   if sigint_received:
00061     # Waits for 1 seconds and force kill the program, in case the action server
00062     # is dead already (which is the case in our example launch file).
00063     exit_thread = threading.Thread(target=DelayedExit, args=(1, ))
00064     exit_thread.daemon = True
00065     exit_thread.start()
00066 
00067     client.cancel_goal()
00068 
00069   client.wait_for_result()
00070 
00071 
00072 if __name__ == '__main__':
00073   try:
00074     rospy.init_node("gcloud_speech_example_client", anonymous=True)
00075     SpeechToTextSimpleExampleClient()
00076   except rospy.ROSInterruptException:
00077     print("Program interrupted before completion.", file=sys.stderr)


gcloud_speech_utils
Author(s):
autogenerated on Thu Jun 6 2019 17:58:05