test_tools
hmi_logger.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # import pyaudio
3 # import wave
4 import json
5 import os
6 from datetime import datetime
7 
8 import rospy
9 from hmi_msgs.msg import QueryActionGoal, QueryActionResult
10 
11 # CHUNK = 1024
12 # FORMAT = pyaudio.paInt16
13 # CHANNELS = 2
14 # RATE = 44100
15 
16 # P = pyaudio.PyAudio()
17 
18 RECORDING = False
19 RESULT = None
20 # STREAM = P.open(format=FORMAT,
21 # channels=CHANNELS,
22 # rate=RATE,
23 # input=True,
24 # frames_per_buffer=CHUNK)
25 
26 
27 def start(msg):
28  global RECORDING
29 
30  if RECORDING:
31  return
32 
33  # frames = []
34 
35  rospy.loginfo("Starting HMI recording")
36  RECORDING = True
37 
38  # last_print = rospy.Time.now()
39  while RECORDING:
40  rospy.sleep(0.1)
41  # data = STREAM.read(CHUNK)
42  # frames.append(data)
43  # if rospy.Time.now() - last_print > rospy.Duration(1):
44  # rospy.loginfo(".. recording")
45  # last_print = rospy.Time.now()
46 
47  now = datetime.now()
48  # wav_filename = "%s/%s.wav" % (STORAGE_FOLDER, now.strftime("%Y-%m-%d-%H-%M-%d-%f"))
49  # wf = wave.open(wav_filename, 'wb')
50  # wf.setnchannels(CHANNELS)
51  # wf.setsampwidth(P.get_sample_size(FORMAT))
52  # wf.setframerate(RATE)
53  # wf.writeframes(b''.join(frames))
54  # wf.close()
55 
56  data = {
57  # "wav_filename": wav_filename,
58  "request": msg.__str__(),
59  "result": RESULT.__str__()
60  }
61 
62  if RESULT.status.status == 3:
63  json_filename = "%s/%s.json" % (STORAGE_FOLDER, now.strftime("%Y-%m-%d-%H-%M-%d-%f"))
64  with open(json_filename, 'w') as outfile:
65  json.dump(data, outfile)
66 
67  rospy.loginfo("Writing HMI log to %s" % json_filename)
68 
69 
70 def end(msg):
71  global RECORDING, RESULT
72  RESULT = msg
73  rospy.loginfo("End callback")
74  RECORDING = False
75 
76 
77 if __name__ == '__main__':
78  rospy.init_node("hmi_logger")
79 
80  start_sub = rospy.Subscriber("goal", QueryActionGoal, start)
81  end_sub = rospy.Subscriber("result", QueryActionResult, end)
82  rospy.loginfo("Start topic: %s", start_sub.name)
83  rospy.loginfo("End topic: %s", end_sub.name)
84  STORAGE_FOLDER = rospy.get_param('~storage_folder', os.path.expanduser('/tmp/hmi'))
85 
86  if not os.path.exists(STORAGE_FOLDER):
87  os.makedirs(STORAGE_FOLDER)
88 
89  rospy.spin()
90 
91  # STREAM.stop_stream()
92  # STREAM.close()
93  # P.terminate()
hmi_logger.end
def end(msg)
Definition: hmi_logger.py:70
hmi_logger.start
def start(msg)
Definition: hmi_logger.py:27