Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 import rospy
00022 from sensor_msgs.msg import Imu
00023
00024
00025 class ImuPlay(object):
00026 def __init__(self):
00027 self.last_msg = None
00028 self.imu_sub = rospy.Subscriber('/base_imu',
00029 Imu,
00030 self.imu_cb,
00031 queue_size=1)
00032 rospy.loginfo(
00033 "Subscribed to: '" + str(self.imu_sub.resolved_name) + "' topic.")
00034 self.run()
00035
00036 def imu_cb(self, msg):
00037 """
00038 :type msg: Imu
00039 """
00040 self.last_msg = msg
00041
00042 def run(self):
00043 """Show information on what was found in the imu"""
00044 rospy.loginfo("Waiting for first Imu message...")
00045 while not rospy.is_shutdown() and self.last_msg is None:
00046 rospy.sleep(0.2)
00047
00048
00049 r = rospy.Rate(5)
00050 while not rospy.is_shutdown():
00051 self.do_stuff_with_last_msg()
00052 r.sleep()
00053
00054 def do_stuff_with_last_msg(self):
00055 """Print funny sentences about what we can guess of our status thanks to the imu"""
00056 acc = self.last_msg.linear_acceleration
00057
00058 total_acc = abs(acc.x) + abs(acc.y)
00059 if total_acc > 1.0:
00060 rospy.loginfo("Oh no, stop shaking me!")
00061 else:
00062 rospy.loginfo("No one is shaking me, luckily!")
00063
00064
00065 if __name__ == '__main__':
00066 rospy.init_node('imu_play')
00067 lp = ImuPlay()
00068 rospy.spin()