imu_monitor.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 import PyKDL
5 from sensor_msgs.msg import Imu
6 from pr2_mechanism_controllers.msg import Odometer
7 from threading import Lock
8 from math import fabs, pi
9 from diagnostic_msgs.msg import DiagnosticArray, DiagnosticStatus, KeyValue
10 
11 EPS = 0.0001
12 
13 class ImuMonitor:
14  def __init__(self):
15  self.lock = Lock()
16 
17  # reset state
18  self.dist = 0.0
19  self.drift = -1.0
20  self.last_angle = 0.0
21  self.start_angle = 0.0
22  self.start_time = rospy.Time.now()
23 
24  # subscribe to topics
25  self.imu_sub = rospy.Subscriber('torso_lift_imu/data', Imu, self.imu_cb)
26  self.odom_sub = rospy.Subscriber('base_odometry/odometer', Odometer, self.odom_cb)
27 
28  # diagnostics
29  self.pub_diag = rospy.Publisher('/diagnostics', DiagnosticArray)
30 
31 
32  def imu_cb(self, msg):
33  with self.lock:
34  rot = PyKDL.Rotation.Quaternion(msg.orientation.x, msg.orientation.y,
35  msg.orientation.z, msg.orientation.w)
36  (r, p, self.last_angle) = rot.GetRPY()
37 
38  def odom_cb(self, msg):
39  with self.lock:
40  dist = msg.distance + (msg.angle * 0.25)
41 
42  # check if base moved
43  if dist > self.dist + EPS:
44  self.start_time = rospy.Time.now()
45  self.start_angle = self.last_angle
46  self.dist = dist
47 
48  # do imu test if possible
49  if rospy.Time.now() > self.start_time + rospy.Duration(10.0):
50  self.drift = fabs(self.start_angle - self.last_angle)*180/(pi*10)
51  self.start_time = rospy.Time.now()
52  self.start_angle = self.last_angle
53  self.last_measured = rospy.Time.now()
54 
55  # publish diagnostics
56  d = DiagnosticArray()
57  d.header.stamp = rospy.Time.now()
58  ds = DiagnosticStatus()
59  ds.name = "imu_node: Imu Drift Monitor"
60  if self.drift < 0.5:
61  ds.level = DiagnosticStatus.OK
62  ds.message = 'OK'
63  elif self.drift < 1.0:
64  ds.level = DiagnosticStatus.WARN
65  ds.message = 'Drifting'
66  else:
67  ds.level = DiagnosticStatus.ERROR
68  ds.message = 'Drifting'
69  drift = self.drift
70  if self.drift < 0:
71  last_measured = 'No measurements yet, waiting for base to stop moving before measuring'
72  drift = 'N/A'
73  else:
74  age = (rospy.Time.now() - self.last_measured).to_sec()
75  if age < 60:
76  last_measured = '%f seconds ago'%age
77  elif age < 3600:
78  last_measured = '%f minutes ago'%(age/60)
79  else:
80  last_measured = '%f hours ago'%(age/3600)
81  ds.values = [
82  KeyValue('Last measured', last_measured),
83  KeyValue('Drift (deg/sec)', str(drift)) ]
84  d.status = [ds]
85  self.pub_diag.publish(d)
86 
87 def main():
88  rospy.init_node('imu_monitor')
89  rospy.sleep(0.001) # init time
90 
91  monitor = ImuMonitor()
92 
93  rospy.spin()
94 
95 
96 if __name__ == '__main__':
97  main()
def odom_cb(self, msg)
Definition: imu_monitor.py:38
def imu_cb(self, msg)
Definition: imu_monitor.py:32
def main()
Definition: imu_monitor.py:87


imu_monitor
Author(s): Wim Meeussen
autogenerated on Tue Jun 1 2021 02:50:48