6 from sensor_msgs.msg
import JointState
7 import matplotlib.pyplot
as plt
8 from sklearn.externals
import joblib
10 from mpl_toolkits.mplot3d
import Axes3D
11 from mpl_toolkits.mplot3d.art3d
import Poly3DCollection
31 This function NEEDS to get called first, so that global variables can 33 Once set, they can be used to classify incoming data. 34 Sets global variables, so they can be used later. 45 SCALER = joblib.load(
"../data/scale.pkl")
46 REDUCE = joblib.load(
"../data/reduce.pkl")
48 load_file =
'../data/'+load_file
49 MODEL = joblib.load(load_file)
65 print 'Modules loaded. Over.' 71 Callback function for when specified message is published. 72 Adds data to global list to be processed later. 75 poses = list(val.position)
78 POSITIONS_LIST.append(poses)
98 data = np.array([POSITIONS_LIST[0]])
101 scaled = SCALER.transform(data)
104 pca_ed = REDUCE.transform(scaled)
105 POINTS.append(pca_ed.tolist()[0])
108 prediction = MODEL.predict(pca_ed)
109 PREDS.append(prediction[0])
114 print 'Good: ', GOOD/float(GOOD+BAD),
'Bad: ', BAD/float(GOOD+BAD),
'Count: ', GOOD+BAD
117 print 'Good: ', GOOD/float(GOOD+BAD),
'Bad: ', BAD/float(GOOD+BAD),
'Count: ', GOOD+BAD
118 rospy.logwarn(
"WARNING: Robot state classified as an \ 119 anomaly.\n" + str(data))
124 if (BAD/float(BAD+GOOD) > float(THRESHOLD))
and (BAD+GOOD > 60):
125 rospy.logerr(
"ERR: Robot state classified as an \ 126 anomaly.\n" + str(data))
136 percentgood = GOOD/float(GOOD+BAD)
137 percentbad = BAD/float(GOOD+BAD)
138 print GOOD, BAD, percentgood, percentbad
141 pts0 = [row[0]
for row
in POINTS]
142 pts1 = [row[1]
for row
in POINTS]
143 pts2 = [row[2]
for row
in POINTS]
146 ax = fig.gca(projection=
'3d')
147 ax.scatter(pts0, pts1, pts2, c=PREDS, cmap=
'bwr', s=10)
152 if __name__ ==
'__main__':
154 rospy.init_node(
'monitor', anonymous=
True)
159 rospy.Subscriber(sys.argv[1],
163 rospy.on_shutdown(graph_3d)
165 while not rospy.is_shutdown():
def load_modules(load_file, th)