tracker.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 import math
4 
5 from easy_markers.generator import Marker, MarkerGenerator
6 
7 from geometry_msgs.msg import Point, Vector3
8 
9 from kalman_filter import Kalman
10 
11 from people_msgs.msg import People, Person, PositionMeasurementArray
12 
13 import rospy
14 
15 
16 def distance(leg1, leg2):
17  return math.sqrt(math.pow(leg1.x - leg2.x, 2) +
18  math.pow(leg1.y - leg2.y, 2) +
19  math.pow(leg1.z - leg2.z, 2))
20 
21 
22 def average(leg1, leg2):
23  return Point((leg1.x + leg2.x) / 2,
24  (leg1.y + leg2.y) / 2,
25  (leg1.z + leg2.z) / 2)
26 
27 
28 def add(v1, v2):
29  return Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z)
30 
31 
32 def subtract(v1, v2):
33  return Vector3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z)
34 
35 
36 def scale(v, s):
37  v.x *= s
38  v.y *= s
39  v.z *= s
40 
41 
42 def printv(v):
43  print('%.2f %.2f %.2f' % (v.x, v.y, v.z),)
44 
45 
47 gen.type = Marker.ARROW
48 gen.ns = 'velocities'
49 gen.lifetime = .5
50 
51 
52 class PersonEstimate(object):
53  def __init__(self, msg):
54  self.pos = msg
55  self.reliability = 0.1
56  self.k = Kalman()
57 
58  def update(self, msg):
59  last = self.pos
60  self.pos = msg
61  self.reliability = max(self.reliability, msg.reliability)
62 
63  ivel = subtract(self.pos.pos, last.pos)
64  time = (self.pos.header.stamp - last.header.stamp).to_sec()
65  scale(ivel, 1.0 / time)
66 
67  self.k.update([ivel.x, ivel.y, ivel.z])
68 
69  def age(self):
70  return self.pos.header.stamp
71 
72  def get_id(self):
73  return self.pos.object_id
74 
75  def velocity(self):
76  k = self.k.values()
77  if k is None:
78  return Vector3()
79  v = Vector3(k[0], k[1], k[2])
80  return v
81 
82  def publish_markers(self, pub):
83  gen.scale = [.1, .3, 0]
84  gen.color = [1, 1, 1, 1]
85  vel = self.velocity()
86  m = gen.marker(points=[self.pos.pos, add(self.pos.pos, vel)])
87  m.header = self.pos.header
88  pub.publish(m)
89 
90  def get_person(self):
91  p = Person()
92  p.name = self.get_id()
93  p.position = self.pos.pos
94  p.velocity = self.velocity()
95  p.reliability = self.reliability
96  return self.pos.header.frame_id, p
97 
98 
99 class VelocityTracker(object):
100  def __init__(self):
101  self.people = {}
102  self.TIMEOUT = rospy.Duration(rospy.get_param('~timeout', 1.0))
103  self.sub = rospy.Subscriber('/people_tracker_measurements',
104  PositionMeasurementArray,
105  self.pm_cb)
106  self.mpub = rospy.Publisher('/visualization_marker',
107  Marker,
108  queue_size=10)
109  self.ppub = rospy.Publisher('/people',
110  People,
111  queue_size=10)
112 
113  def pm_cb(self, msg):
114  for pm in msg.people:
115  if pm.object_id in self.people:
116  self.people[pm.object_id].update(pm)
117  else:
118  p = PersonEstimate(pm)
119  self.people[pm.object_id] = p
120 
121  def spin(self):
122  rate = rospy.Rate(10)
123  while not rospy.is_shutdown():
124  # Remove People Older Than timeout param
125  now = rospy.Time.now()
126  for p in self.people.values():
127  if now - p.age() > self.TIMEOUT:
128  del self.people[p.id()]
129  self.publish()
130  rate.sleep()
131 
132  def publish(self):
133  gen.counter = 0
134  pl = People()
135  pl.header.frame_id = None
136 
137  for p in self.people.values():
138  p.publish_markers(self.mpub)
139  frame, person = p.get_person()
140  pl.header.frame_id = frame
141  pl.people.append(person)
142 
143  self.ppub.publish(pl)
144 
145 
146 if __name__ == '__main__':
147  rospy.init_node('people_velocity_tracker')
149  vt.spin()
def __init__(self, msg)
Definition: tracker.py:53
def get_id(self)
Definition: tracker.py:72
def update(self, msg)
Definition: tracker.py:58
def get_person(self)
Definition: tracker.py:90
def average(leg1, leg2)
Definition: tracker.py:22
def distance(leg1, leg2)
Definition: tracker.py:16
def add(v1, v2)
Definition: tracker.py:28
def subtract(v1, v2)
Definition: tracker.py:32
def scale(v, s)
Definition: tracker.py:36
def publish_markers(self, pub)
Definition: tracker.py:82
def printv(v)
Definition: tracker.py:42
def pm_cb(self, msg)
Definition: tracker.py:113
def velocity(self)
Definition: tracker.py:75


people_velocity_tracker
Author(s): David V. Lu!!
autogenerated on Sun Feb 21 2021 03:56:52