WUT Velma robot API
create_state_snapshot.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 
6 
7 # Copyright (c) 2021, Robot Control and Pattern Recognition Group,
8 # Institute of Control and Computation Engineering
9 # Warsaw University of Technology
10 #
11 # All rights reserved.
12 #
13 # Redistribution and use in source and binary forms, with or without
14 # modification, are permitted provided that the following conditions are met:
15 # * Redistributions of source code must retain the above copyright
16 # notice, this list of conditions and the following disclaimer.
17 # * Redistributions in binary form must reproduce the above copyright
18 # notice, this list of conditions and the following disclaimer in the
19 # documentation and/or other materials provided with the distribution.
20 # * Neither the name of the Warsaw University of Technology nor the
21 # names of its contributors may be used to endorse or promote products
22 # derived from this software without specific prior written permission.
23 #
24 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 # DISCLAIMED. IN NO EVENT SHALL <COPYright HOLDER> BE LIABLE FOR ANY
28 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #
35 # Author: Dawid Seredynski
36 #
37 
38 import roslib; roslib.load_manifest('rcprg_ros_utils')
39 
40 import rospy
41 import math
42 import numpy as np
43 import PyKDL
44 import std_srvs.srv
45 from gazebo_msgs.msg import *
46 from gazebo_msgs.srv import *
47 
48 if __name__ == "__main__":
49  rospy.init_node('create_state_snapshot', anonymous=False)
50 
51  rospy.sleep(0.5)
52 
53  world_prop = None
54  print 'Waiting for ROS services...'
55  rospy.wait_for_service('/gazebo/get_world_properties')
56  rospy.wait_for_service('/gazebo/get_model_properties')
57  rospy.wait_for_service('/gazebo/get_joint_properties')
58  rospy.wait_for_service('/gazebo/pause_physics')
59  rospy.wait_for_service('/gazebo/unpause_physics')
60  try:
61  get_world_properties = rospy.ServiceProxy('/gazebo/get_world_properties', GetWorldProperties)
62  get_model_properties = rospy.ServiceProxy('/gazebo/get_model_properties', GetModelProperties)
63  get_joint_properties = rospy.ServiceProxy('/gazebo/get_joint_properties', GetJointProperties)
64  pause_physics = rospy.ServiceProxy('/gazebo/pause_physics', std_srvs.srv.Empty)
65  unpause_physics = rospy.ServiceProxy('/gazebo/unpause_physics', std_srvs.srv.Empty)
66  except rospy.ServiceException, e:
67  print "Service call failed: %s"%e
68 
69 # pause_physics()
70 # print 'Simulation is paused.'
71 
72  world_prop = get_world_properties()
73 
74  file_lines = []
75  for model_name in world_prop.model_names:
76  print 'model_name:', model_name
77  model_prop = get_model_properties(model_name)
78  print 'joint_names:', model_prop.joint_names
79  for joint_name in model_prop.joint_names:
80  joint_prop = get_joint_properties(model_name + '::' + joint_name)
81  if math.isnan(joint_prop.position[0]):
82  continue
83  if joint_prop.type == GetJointPropertiesResponse.REVOLUTE or \
84  joint_prop.type == GetJointPropertiesResponse.CONTINUOUS or \
85  joint_prop.type == GetJointPropertiesResponse.PRISMATIC:
86  print ' ' + joint_name + ' ' + str(joint_prop.position) + 'type:' + str(joint_prop.type)
87  file_lines.append( model_name + ' ' + joint_name + ' ' + str(joint_prop.position[0]) )
88  else:
89  print ' ' + joint_name + ' of type ' + str(joint_prop.type) + ' is not supported'
90  print 'You can now save the Gazebo world using Gazebo client (File->Save World As).'
91  with open('state_snapshot.txt', 'w') as f:
92  for line in file_lines:
93  f.write(line + '\n')