odom_tf.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 """
4 connect to Oculus Prime Server Application
5 poll server for odometry data
6 broadcast tranform between base_link and odom frames
7 """
8 
9 from math import radians, sin, cos
10 import rospy, tf
11 from nav_msgs.msg import Odometry
12 import oculusprimesocket
13 
14 
15 # lastupdate = 0
16 # updateinterval = 0.25
17 pos = [0.0, 0.0, 0.0]
18 before = 0
19 now = 0
20 default_lag = 0.035 # 0.075 = xtion (using device time) 0.035 = astra (using system time)
21 
22 
23 def broadcast(s):
24  global before, pos, now
25  now = rospy.Time.now() - rospy.Duration(rospy.get_param('~odom_lag', default_lag))
26  # 0.05 subtract socket + serial + fifo read lag 0.075 = tested with rviz odom only
27  dt = (now-before).to_sec()
28  before = now
29 
30  distance = float(s[2])/1000
31  delta_x = distance * cos(pos[2])
32  delta_y = distance * sin(pos[2])
33  delta_th = radians(float(s[3]))
34  pos[0] += delta_x
35  pos[1] += delta_y
36  pos[2] += delta_th
37 
38  # tf
39  odom_quat = tf.transformations.quaternion_from_euler(0, 0, pos[2])
40  br.sendTransform((pos[0], pos[1], 0), odom_quat, now, "base_link","odom")
41  # future
42  # quat = tf.transformations.quaternion_from_euler(0, 0, 0)
43  # br.sendTransform((-0.054, 0.048, 0.29), quat, now, "camera_depth_frame", "base_link")
44  # br.sendTransform((0, 0, 0), quat, now, "odom", "map")
45 
46  # odom
47  odom = Odometry()
48  odom.header.stamp = now
49  odom.header.frame_id = "odom"
50 
51  #set the position
52  odom.pose.pose.position.x = pos[0]
53  odom.pose.pose.position.y = pos[1]
54  odom.pose.pose.position.z = 0.0
55  odom.pose.pose.orientation.x = odom_quat[0]
56  odom.pose.pose.orientation.y = odom_quat[1]
57  odom.pose.pose.orientation.z = odom_quat[2]
58  odom.pose.pose.orientation.w = odom_quat[3]
59  odom.pose.covariance= [0.000001, 0, 0, 0, 0, 0,
60  0, 0.000001, 0, 0, 0, 0,
61  0, 0, 0.000001, 0, 0, 0,
62  0, 0, 0, 0.000001, 0, 0,
63  0, 0, 0, 0, 0.000001, 0,
64  0, 0, 0, 0, 0, 0.000001]
65 
66  #set the velocity
67  odom.child_frame_id = "base_link"
68  odom.twist.twist.linear.x = distance / dt
69  odom.twist.twist.linear.y = 0
70  odom.twist.twist.linear.z = 0
71  odom.twist.twist.angular.x = 0
72  odom.twist.twist.angular.y = 0
73  odom.twist.twist.angular.z = delta_th / dt
74 
75  #publish
76  odom_pub.publish(odom)
77 
78 def cleanup():
79  oculusprimesocket.sendString("state delete odometrybroadcast")
80  oculusprimesocket.sendString("odometrystop")
81  # oculusprimesocket.sendString("state delete navigationenabled")
82  oculusprimesocket.sendString("log odom_tf.py disconnecting") # goodbye
83 
84 
85 
86 # MAIN
87 
88 rospy.init_node('odom_tf', anonymous=False)
89 before = rospy.Time.now()
91 odom_pub = rospy.Publisher('odom', Odometry, queue_size=10)
92 rospy.on_shutdown(cleanup)
94 oculusprimesocket.sendString("log odom_tf.py connected")
95 oculusprimesocket.sendString("state odometrybroadcast 250") # ms, needs to be set prior to odometrystart
96 oculusprimesocket.sendString("odometrystart")
97 broadcast("* * 0 0".split()) # broadcast zero odometry baseline
98 
99 while not rospy.is_shutdown():
100 
101  # t = rospy.get_time()
102  # if t-lastupdate > updateinterval: # request odometry update
103  # oculusprimesocket.sendString("odometryreport")
104 
105  s = oculusprimesocket.replyBufferSearch("<state> distanceangle ")
106  if not s=="":
107  broadcast(s.split())
108  # lastupdate = now.to_sec()
109 
110  rospy.sleep(0.005) # was 0.01
def cleanup()
Definition: odom_tf.py:78
def replyBufferSearch(pattern)
def broadcast(s)
Definition: odom_tf.py:23


oculusprime
Author(s): Colin Adamson
autogenerated on Wed Mar 10 2021 03:14:59