1
+ #!/usr/bin/env python
2
+
3
+ import rospy
4
+ import numpy as np
5
+ from geometry_msgs .msg import PoseStamped , Point , Pose , PointStamped , PoseArray
6
+ import tf_conversions
7
+
8
+ class Reference (object ):
9
+
10
+ def __init__ (self ):
11
+ self .p = rospy .Publisher ('/reference_pose' , PointStamped , queue_size = 6 )
12
+ rospy .Subscriber ('/vrpn_client_node/robot_name/pose' , PoseStamped , self .get_reference )
13
+ rospy .wait_for_message ('/vrpn_client_node/robot_name/pose' , PoseStamped )
14
+
15
+
16
+ def get_reference (self , msg ):
17
+ self .reference = np .empty (2 )
18
+ reference_point = msg
19
+
20
+ self .reference [0 ] = reference_point .pose .position .x
21
+ self .reference [1 ] = reference_point .pose .position .y
22
+
23
+ point = PointStamped ()
24
+ point .header .frame_id = "map"
25
+ point .point .x = self .reference [0 ]
26
+ point .point .y = self .reference [1 ]
27
+ point .point .z = 0
28
+ self .p .publish (point )
29
+
30
+ class Particles (object ):
31
+
32
+ def __init__ (self ):
33
+ rospy .Subscriber ('/particlecloud' , PoseArray , self .particlecloud )
34
+ rospy .wait_for_message ('/particlecloud' , PoseArray )
35
+
36
+ def particlecloud (self ,msg ): # callback function from topic /particlecloud
37
+ self .particles = np .empty ((len (msg .poses ),3 ))
38
+ for i in range (len (msg .poses )):
39
+ self .particles [i ,0 ] = msg .poses [i ].position .x
40
+ self .particles [i ,1 ] = msg .poses [i ].position .y
41
+ angle = (
42
+ msg .poses [i ].orientation .x ,
43
+ msg .poses [i ].orientation .y ,
44
+ msg .poses [i ].orientation .z ,
45
+ msg .poses [i ].orientation .w )
46
+ self .particles [i ,2 ] = tf_conversions .transformations .euler_from_quaternion (angle )[2 ]
47
+
48
+ def main ():
49
+ r = Reference ()
50
+ p = Particles ()
51
+
52
+ while not rospy .is_shutdown ():
53
+
54
+ reference_point = r .reference
55
+ E = np .mean (p .particles , axis = 0 )
56
+
57
+ Error = np .linalg .norm (np .abs (E [0 :2 ]- reference_point ))
58
+
59
+ E_pub = rospy .Publisher ('/Expection' , PointStamped , queue_size = 6 )
60
+ point = PointStamped ()
61
+ point .header .frame_id = "map"
62
+ point .point .x = E [0 ]
63
+ point .point .y = E [1 ]
64
+ point .point .z = 0
65
+ E_pub .publish (point )
66
+
67
+ if __name__ == "__main__" :
68
+ rospy .init_node ('mse_calc' )
69
+ main ()
70
+
71
+ rospy .spin ()
0 commit comments