example_client.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 from __future__ import print_function
3 
4 import os
5 import signal
6 import sys
7 import threading
8 import time
9 
10 import actionlib
11 import actionlib_msgs.msg as actionlib_msgs
12 import rospy
13 
14 import gcloud_speech_msgs.msg as gcloud_speech_msgs
15 
16 GoalStatus = actionlib_msgs.GoalStatus
17 
19  def DoneCallback(state, result):
20  print("\n\nDone, state {}, result:\n{}\n".format(state,result))
21 
22 
23  def ActiveCallback():
24  print("The goal is now active.\n")
25 
26 
27  def FeedbackCallback(feedback):
28  print("{}\n".format(feedback))
29 
30 
31  global sigint_received
32  sigint_received = False
33 
34  def SignalIntHandler(signal, frame):
35  print("Received SIGINT.")
36  global sigint_received
37  sigint_received = True
38 
39 
40  def DelayedExit(wait_time):
41  time.sleep(wait_time)
42  os._exit(0)
43 
44 
45  signal.signal(signal.SIGINT, SignalIntHandler)
46 
48  "/cogrob/speech_to_text", gcloud_speech_msgs.SpeechToTextAction)
49  client.wait_for_server()
50 
51  goal = gcloud_speech_msgs.SpeechToTextGoal()
52  client.send_goal(goal, done_cb=DoneCallback, active_cb=ActiveCallback,
53  feedback_cb=FeedbackCallback)
54 
55  while client.get_state() in [GoalStatus.PENDING, GoalStatus.ACTIVE]:
56  time.sleep(1)
57  if sigint_received:
58  break
59 
60  if sigint_received:
61  # Waits for 1 seconds and force kill the program, in case the action server
62  # is dead already (which is the case in our example launch file).
63  exit_thread = threading.Thread(target=DelayedExit, args=(1, ))
64  exit_thread.daemon = True
65  exit_thread.start()
66 
67  client.cancel_goal()
68 
69  client.wait_for_result()
70 
71 
72 if __name__ == '__main__':
73  try:
74  rospy.init_node("gcloud_speech_example_client", anonymous=True)
76  except rospy.ROSInterruptException:
77  print("Program interrupted before completion.", file=sys.stderr)
def SpeechToTextSimpleExampleClient()


gcloud_speech_utils
Author(s):
autogenerated on Wed Jun 5 2019 21:24:09