torso_cmder.py
Go to the documentation of this file.
00001 #!/usr/bin/env python 
00002 # Copyright (c) 2008, Willow Garage, Inc.  
00003 # All rights reserved. 
00004 # 
00005 # Redistribution and use in source and binary forms, with or without 
00006 # modification, are permitted provided that the following conditions are met:
00007 #
00008 #     * Redistributions of source code must retain the above copyright
00009 #       notice, this list of conditions and the following disclaimer.
00010 #     * Redistributions in binary form must reproduce the above copyright
00011 #       notice, this list of conditions and the following disclaimer in the 
00012 #       documentation and/or other materials provided with the distribution.
00013 #     * Neither the name of the Willow Garage, Inc. nor the names of its 
00014 #       contributors may be used to endorse or promote products derived from 
00015 #       this software without specific prior written permission. 
00016 #
00017 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00019 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR     
00022 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF    
00023 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00025 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
00026 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027 # POSSIBILITY OF SUCH DAMAGE.  
00028 
00029 ##\author Kevin Watts
00030 ##\brief Commands torso to go up and down repeatedly
00031 
00032 PKG = "life_test"
00033 import roslib; roslib.load_manifest(PKG)
00034 
00035 import sys
00036 import rospy
00037 
00038 from time import sleep
00039 from std_msgs.msg import Float64
00040 from sensor_msgs.msg import JointState
00041 
00042 from optparse import OptionParser
00043 
00044 class LastMessage():
00045     def __init__(self, topic, msg_type):
00046         self.msg = None
00047         rospy.Subscriber(topic, msg_type, self.callback)
00048 
00049     def last(self):
00050         return self.msg
00051 
00052     def callback(self, msg):
00053         self.msg = msg
00054  
00055 def main():
00056     parser = OptionParser()
00057     parser.add_option("-l", "--low", dest="low_only", 
00058                       help = "Keep in lower end only", action="store_true", 
00059                       default = False)
00060     parser.add_option("-s", "--slow", dest="slow",
00061                       help = "Move only every 5 minutes",
00062                       action="store_true", default=False)
00063                      
00064     
00065     (options, args) = parser.parse_args()
00066 
00067     rospy.init_node('torso_life_test')
00068 
00069     pub = rospy.Publisher("torso_lift_velocity_controller/command", Float64, latch = True)
00070     last_state = LastMessage('joint_states', JointState)
00071     turn_count = 0
00072     vel = 10.0
00073     pub.publish(Float64(vel))
00074     print 'Published', vel
00075 
00076     try:
00077         while not last_state.msg and not rospy.is_shutdown(): sleep(0.1)
00078         while not rospy.is_shutdown():
00079             sleep(0.01)
00080             jnt_states = last_state.last()
00081             torso_idx = -1
00082             for index, name in enumerate(jnt_states.name):
00083                 if name == 'torso_lift_joint':
00084                     torso_idx = index
00085                     break
00086             if torso_idx < 0:
00087                 rospy.logwarn("The joint %s was not found in the joints states" % 'torso_lift_joint')
00088 
00089             if abs(jnt_states.velocity[torso_idx]) < 0.002:
00090                 turn_count += 1
00091             else:
00092                 turn_count = 0
00093 
00094             if turn_count > 25:
00095                 vel = -1 * vel
00096                 turn_count = 0
00097                 pub.publish(Float64(vel))
00098 
00099             if options.slow:
00100                 sleep(5)
00101 
00102     except  KeyboardInterrupt, e:
00103         pass
00104     except:
00105         import traceback
00106         rospy.logerr(traceback.format_exc())
00107         traceback.print_exc()
00108 
00109 
00110 if __name__ == '__main__':
00111     main()


life_test
Author(s): Kevin Watts
autogenerated on Sat Dec 28 2013 17:56:37