testies.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # License: BSD
4 # https://raw.github.com/robotics-in-concert/rocon_multimaster/master/rocon_gateway/LICENSE
5 #
6 
7 ##############################################################################
8 # Imports
9 ##############################################################################
10 
11 import math
12 import rospy
13 import rocon_utilities.console as console
14 import visualization_msgs.msg as visualization_msgs
15 from std_msgs.msg import Float32
16 
17 # Can't turn it much - different error params?
18 # Sometimes jumps x-values near the edges -> due to bad z calculations on the fringe (out by 50cm!)
19  # should be able to easily filter these out
20 # a and b disappear near the edges, but I still get readings
21 
22 ##############################################################################
23 # Functions
24 ##############################################################################
25 
26 class Marker:
27  def __init__(self, id, x, y, z):
28  self.id = id
29  self.update(x, y, z)
30 
31  def update(self, x, y, z):
32  self.x = x
33  self.y = y
34  self.z = z
35  self.distance = math.sqrt(x*x + z*z)
36 
37 class Jagi(object):
38  def __init__(self):
39  self.baseline = 0.26
40  self.left_marker = Marker(id=3, x=0, y=0, z=0)
41  self.right_marker = Marker(id=0, x=0, y=0, z=0)
42  self.left_last_timestamp = rospy.get_rostime()
43  self.right_last_timestamp = rospy.get_rostime()
44  self.x_publisher = rospy.Publisher('x', Float32)
45  self.z_publisher = rospy.Publisher('z', Float32)
46  self.heading_publisher = rospy.Publisher('heading', Float32)
47  self.subscriber = rospy.Subscriber('/visualization_marker', visualization_msgs.Marker, self.callback)
48 
49 
51  b = self.baseline/2 + (self.left_marker.distance*self.left_marker.distance - self.right_marker.distance*self.right_marker.distance)/(2*self.baseline)
52  try:
53  a = math.sqrt(self.left_marker.distance*self.left_marker.distance - b*b)
54  except ValueError:
55  print("Value error: measured_distance=%s, b=%s" % (self.left_marker.distance, b))
56  return False # self.left_marker not initialised fully yet, so negative sqrt
57  print(console.cyan + " a" + console.reset + " : " + console.yellow + "%s" % a + console.reset)
58  print(console.cyan + " b" + console.reset + " : " + console.yellow + "%s" % b + console.reset)
59  return True
60 
61  def print_pose_results(self):
62  # angle between the robot and the first marker
63  alpha = math.atan2(self.left_marker.x, self.left_marker.z)
64  alpha_degrees = alpha*180.0/math.pi
65  # alpha + beta is angle between the robot and the second marker
66  beta = math.atan2(self.right_marker.x, self.right_marker.z)
67  beta_degrees = beta*180.0/math.pi
68  # theta is the angle between the wall and the perpendicular in front of the robot
69  theta = math.atan2((self.left_marker.z - self.right_marker.z), (self.right_marker.x - self.left_marker.x))
70  theta_degrees = theta*180.0/math.pi
71 
72  print(console.cyan + " alpha" + console.reset + " : " + console.yellow + "%s" % alpha_degrees + " degrees" + console.reset)
73  print(console.cyan + " beta" + console.reset + " : " + console.yellow + "%s" % beta_degrees + " degrees" + console.reset)
74  print(console.cyan + " theta" + console.reset + " : " + console.yellow + "%s" % theta_degrees + " degrees" + console.reset)
75 
76  # M1 = (self.left_marker.x, self.left_marker.z)
77  # M2 = (self.right_marker.x, self.right_marker.z)
78  # M3 = M1 + (M2 - M1)/2 # midpoint of M1, M2
79  # Target stop position relative to marker mid point (40cm perpendicularly out)
80  # M4 = (-0.4*sin(theta), -0.4*cos(theta))
81  # Target stop position
82  # M5 = M3 + M4
83  target_x = self.left_marker.x + (self.right_marker.x - self.left_marker.x)/2 - 0.4*math.sin(theta)
84  target_z = self.left_marker.z + (self.right_marker.z - self.left_marker.z)/2 - 0.4*math.cos(theta)
85  target_heading = math.atan2(target_x, target_z)
86  target_heading_degrees = target_heading*180.0/math.pi
87 
88  print(console.cyan + " target" + console.reset + " : " + console.yellow + "(x=%s, z=%s, heading=%s)" % (target_x, target_z, target_heading_degrees) + console.reset)
89  self.x_publisher.publish(Float32(target_x))
90  self.z_publisher.publish(Float32(target_z))
91  self.heading_publisher.publish(Float32(target_heading_degrees))
92 
93  def callback(self, data):
94  if data.id == 0:
95  self.right_marker.update(data.pose.position.x, data.pose.position.y, data.pose.position.z)
96  self.right_last_timestamp = data.header.stamp
97  print(console.cyan + "r: [x, y, z]" + console.reset + " : " + console.yellow + "[%s, %s, %s]" % (self.right_marker.x, self.right_marker.y, self.right_marker.z) + console.reset)
98  print(console.cyan + "r: d" + console.reset + " : " + console.yellow + "%s" % self.right_marker.distance + console.reset)
99  else:
100  self.left_last_timestamp = data.header.stamp
101  self.left_marker.update(data.pose.position.x, data.pose.position.y, data.pose.position.z)
102  print(console.cyan + "l: [x, y, z]" + console.reset + " : " + console.yellow + "[%s, %s, %s]" % (self.left_marker.x, self.left_marker.y, self.left_marker.z) + console.reset)
103  print(console.cyan + "l: d" + console.reset + " : " + console.yellow + "%s" % self.left_marker.distance + console.reset)
104  #if math.fabs(self.left_last_timestamp - self.right_last_timestamp) < 0.1:
106  if self.print_pythags_results():
107  self.print_pose_results()
108 
109 ##############################################################################
110 # Main
111 ##############################################################################
112 
113 if __name__ == '__main__':
114  #visualization_msgs/Marker
115  rospy.init_node('roles_and_apps')
116  jagi = Jagi()
117  rospy.spin()
def update(self, x, y, z)
Definition: testies.py:31
heading_publisher
Definition: testies.py:46
def __init__(self)
Definition: testies.py:38
Functions.
Definition: testies.py:26
def print_pythags_results(self)
Definition: testies.py:50
def __init__(self, id, x, y, z)
Definition: testies.py:27
def callback(self, data)
Definition: testies.py:93
right_last_timestamp
Definition: testies.py:43
left_last_timestamp
Definition: testies.py:42
def print_pose_results(self)
Definition: testies.py:61


yocs_ar_marker_tracking
Author(s): Daniel Stonier, Jorge Santos
autogenerated on Mon Jun 10 2019 15:53:43