run_motion_python_node.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 # System imports
00022 import sys
00023 import time
00024 # ROS imports
00025 import rospy
00026 from actionlib import SimpleActionClient, GoalStatus
00027 from play_motion_msgs.msg import PlayMotionAction, PlayMotionGoal
00028 
00029 
00030 def show_usage():
00031     """Show usage information giving the possible motions to use."""
00032     # Get the available motion names from param server
00033     param_names = rospy.get_param_names()
00034     motion_names = []
00035     for param_name in param_names:
00036         # If the parameter is like '/play_motion/motions/MOTION_NAME/joints'
00037         if "/play_motion/motions" in param_name and '/joints' in param_name:
00038             motion_name = param_name.replace('/play_motion/motions/', '')
00039             motion_name = motion_name.replace('/joints', '')
00040             motion_names.append(motion_name)
00041 
00042     rospy.loginfo("""Usage:
00043 
00044 \trosrun run_motion run_motion_python_node.py MOTION_NAME"
00045 
00046 \twhere MOTION_NAME must be one of the motions listed in: """ + str(motion_names))
00047 
00048 
00049 def wait_for_valid_time(timeout):
00050     """Wait for a valid time (non-zero), this is important
00051     when using a simulated clock"""
00052     # Loop until:
00053     # * ros master shutdowns
00054     # * control+C is pressed (handled in is_shutdown())
00055     # * timeout is achieved
00056     # * time is valid
00057     start_time = time.time()
00058     while not rospy.is_shutdown():
00059         if not rospy.Time.now().is_zero():
00060             return
00061         if time.time() - start_time > timeout:
00062             rospy.logerr("Timed-out waiting for valid time.")
00063             exit(0)
00064         time.sleep(0.1)
00065     # If control+C is pressed the loop breaks, we can exit
00066     exit(0)
00067 
00068 
00069 def get_status_string(status_code):
00070     return GoalStatus.to_string(status_code)
00071 
00072 if __name__ == '__main__':
00073     rospy.init_node('run_motion_python')
00074     if len(sys.argv) < 2:
00075         show_usage()
00076         exit(0)
00077 
00078     rospy.loginfo("Starting run_motion_python application...")
00079     wait_for_valid_time(10.0)
00080 
00081     client = SimpleActionClient('/play_motion', PlayMotionAction)
00082 
00083     rospy.loginfo("Waiting for Action Server...")
00084     client.wait_for_server()
00085 
00086     goal = PlayMotionGoal()
00087     goal.motion_name = sys.argv[1]
00088     goal.skip_planning = False
00089     goal.priority = 0  # Optional
00090 
00091     rospy.loginfo("Sending goal with motion: " + sys.argv[1])
00092     client.send_goal(goal)
00093 
00094     rospy.loginfo("Waiting for result...")
00095     action_ok = client.wait_for_result(rospy.Duration(30.0))
00096 
00097     state = client.get_state()
00098 
00099     if action_ok:
00100         rospy.loginfo("Action finished succesfully with state: " + str(get_status_string(state)))
00101     else:
00102         rospy.logwarn("Action failed with state: " + str(get_status_string(state)))


play_motion
Author(s): Paul Mathieu , Adolfo Rodriguez Tsouroukdissian
autogenerated on Sat Jun 8 2019 20:26:22