imu_play.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Copyright (c) 2016 PAL Robotics SL. All Rights Reserved
00004 #
00005 # Permission to use, copy, modify, and/or distribute this software for
00006 # any purpose with or without fee is hereby granted, provided that the
00007 # above copyright notice and this permission notice appear in all
00008 # copies.
00009 #
00010 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00011 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00012 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00013 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00014 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00015 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00016 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00017 #
00018 # Author:
00019 #   * Sammy Pfeiffer
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         # Check at a 5Hz rate to not spam
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         # Ignore acc.z as it will be ~ -9.8 from gravity
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()


play_with_sensors
Author(s):
autogenerated on Fri Aug 26 2016 13:20:19