data_collect.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 import sys
5 import os
6 from time import sleep
7 from sensor_msgs.msg import JointState
8 import csv
9 from std_msgs.msg import String
10 
11 
12 POSITIONS_LIST = []
13 LABEL = None
14 COUNT = 0
15 PREV = None
16 
17 
18 '''
19  Callback function when a joint_states message is published.
20  Adds positions to global list to be processed right afterwards.
21 '''
22 def control_command(val):
23  global POSITIONS_LIST
24  global LABEL
25  global PREV
26  global TASK_COUNT
27 
28  if list(val.position) == [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]:
29  return
30 
31  if LABEL:
32  if PREV != list(val.position):
33  POSITIONS_LIST.append(list(val.position) + [LABEL])
34  PREV = list(val.position)
35  else:
36  if PREV != list(val.position):
37  POSITIONS_LIST.append(list(val.position))
38  PREV = list(val.position)
39 
40 
41 '''
42  Helper for writing positions to csv
43 '''
44 def collect(out):
45 
46  while POSITIONS_LIST:
47  out.writerow(POSITIONS_LIST[0])
48  POSITIONS_LIST.pop(0)
49 
50 
51 if __name__ == '__main__':
52 
53  print 'collect - ', sys.argv
54 
55  rospy.init_node('data_collector', anonymous=True)
56  rospy.Subscriber(sys.argv[1],
57  JointState,
58  control_command)
59 
60 
61  # Argv 1 - path + file name is expected
62  cwd = sys.argv[2]
63 
64  # NOTE: The label is an option
65  if len(sys.argv) > 3:
66  LABEL = sys.argv[3]
67 
68  sleep(3)
69  with open(cwd, "w+") as out_file:
70 
71  # print out_file
72  out = csv.writer(out_file, delimiter=',')
73  print 'Writing to', cwd
74  while not rospy.is_shutdown():
75  collect(out)
def control_command(val)
Definition: data_collect.py:22
def collect(out)
Definition: data_collect.py:44


mh5_anomaly_detector
Author(s): Vedanth Narayanan
autogenerated on Mon Jun 10 2019 13:49:20