|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import rospy |
| 4 | +from gazebo_ros_link_attacher.msg import Attach |
| 5 | +from gazebo_msgs.srv import SpawnModel, SpawnModelRequest, SpawnModelResponse |
| 6 | +from copy import deepcopy |
| 7 | +from tf.transformations import quaternion_from_euler |
| 8 | + |
| 9 | +sdf_cube = """<?xml version="1.0" ?> |
| 10 | +<sdf version="1.4"> |
| 11 | + <model name="MODELNAME"> |
| 12 | + <static>0</static> |
| 13 | + <link name="link"> |
| 14 | + <inertial> |
| 15 | + <mass>1.0</mass> |
| 16 | + <inertia> |
| 17 | + <ixx>0.01</ixx> |
| 18 | + <ixy>0.0</ixy> |
| 19 | + <ixz>0.0</ixz> |
| 20 | + <iyy>0.01</iyy> |
| 21 | + <iyz>0.0</iyz> |
| 22 | + <izz>0.01</izz> |
| 23 | + </inertia> |
| 24 | + </inertial> |
| 25 | + <collision name="stairs_collision0"> |
| 26 | + <pose>0 0 0 0 0 0</pose> |
| 27 | + <geometry> |
| 28 | + <box> |
| 29 | + <size>SIZEXYZ</size> |
| 30 | + </box> |
| 31 | + </geometry> |
| 32 | + <surface> |
| 33 | + <bounce /> |
| 34 | + <friction> |
| 35 | + <ode> |
| 36 | + <mu>1.0</mu> |
| 37 | + <mu2>1.0</mu2> |
| 38 | + </ode> |
| 39 | + </friction> |
| 40 | + <contact> |
| 41 | + <ode> |
| 42 | + <kp>10000000.0</kp> |
| 43 | + <kd>1.0</kd> |
| 44 | + <min_depth>0.0</min_depth> |
| 45 | + <max_vel>0.0</max_vel> |
| 46 | + </ode> |
| 47 | + </contact> |
| 48 | + </surface> |
| 49 | + </collision> |
| 50 | + <visual name="stairs_visual0"> |
| 51 | + <pose>0 0 0 0 0 0</pose> |
| 52 | + <geometry> |
| 53 | + <box> |
| 54 | + <size>SIZEXYZ</size> |
| 55 | + </box> |
| 56 | + </geometry> |
| 57 | + <material> |
| 58 | + <script> |
| 59 | + <uri>file://media/materials/scripts/gazebo.material</uri> |
| 60 | + <name>Gazebo/Wood</name> |
| 61 | + </script> |
| 62 | + </material> |
| 63 | + </visual> |
| 64 | + <velocity_decay> |
| 65 | + <linear>0.000000</linear> |
| 66 | + <angular>0.000000</angular> |
| 67 | + </velocity_decay> |
| 68 | + <self_collide>0</self_collide> |
| 69 | + <kinematic>0</kinematic> |
| 70 | + <gravity>1</gravity> |
| 71 | + </link> |
| 72 | + </model> |
| 73 | +</sdf> |
| 74 | +""" |
| 75 | + |
| 76 | + |
| 77 | +def create_cube_request(modelname, px, py, pz, rr, rp, ry, sx, sy, sz): |
| 78 | + """Create a SpawnModelRequest with the parameters of the cube given. |
| 79 | + modelname: name of the model for gazebo |
| 80 | + px py pz: position of the cube (and it's collision cube) |
| 81 | + rr rp ry: rotation (roll, pitch, yaw) of the model |
| 82 | + sx sy sz: size of the cube""" |
| 83 | + cube = deepcopy(sdf_cube) |
| 84 | + # Replace size of model |
| 85 | + size_str = str(round(sx, 3)) + " " + \ |
| 86 | + str(round(sy, 3)) + " " + str(round(sz, 3)) |
| 87 | + cube = cube.replace('SIZEXYZ', size_str) |
| 88 | + # Replace modelname |
| 89 | + cube = cube.replace('MODELNAME', str(modelname)) |
| 90 | + |
| 91 | + req = SpawnModelRequest() |
| 92 | + req.model_name = modelname |
| 93 | + req.model_xml = cube |
| 94 | + req.initial_pose.position.x = px |
| 95 | + req.initial_pose.position.y = py |
| 96 | + req.initial_pose.position.z = pz |
| 97 | + |
| 98 | + q = quaternion_from_euler(rr, rp, ry) |
| 99 | + req.initial_pose.orientation.x = q[0] |
| 100 | + req.initial_pose.orientation.y = q[1] |
| 101 | + req.initial_pose.orientation.z = q[2] |
| 102 | + req.initial_pose.orientation.w = q[3] |
| 103 | + |
| 104 | + return req |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == '__main__': |
| 108 | + rospy.init_node('demo_attach_links') |
| 109 | + attach_pub = rospy.Publisher('/link_attacher_node/attach_models', |
| 110 | + Attach, queue_size=1) |
| 111 | + rospy.loginfo("Created publisher to /link_attacher_node/attach_models") |
| 112 | + spawn_srv = rospy.ServiceProxy('/gazebo/spawn_sdf_model', SpawnModel) |
| 113 | + rospy.loginfo("Waiting for /gazebo/spawn_sdf_model service...") |
| 114 | + spawn_srv.wait_for_service() |
| 115 | + rospy.loginfo("Connected to service!") |
| 116 | + |
| 117 | + # Spawn object 1 |
| 118 | + rospy.loginfo("Spawning cube1") |
| 119 | + req1 = create_cube_request("cube1", |
| 120 | + 0.0, 0.0, 0.51, # position |
| 121 | + 0.0, 0.0, 0.0, # rotation |
| 122 | + 1.0, 1.0, 1.0) # size |
| 123 | + spawn_srv.call(req1) |
| 124 | + rospy.sleep(1.0) |
| 125 | + |
| 126 | + # Spawn object 2 |
| 127 | + rospy.loginfo("Spawning cube2") |
| 128 | + req2 = create_cube_request("cube2", |
| 129 | + 0.0, 1.1, 0.41, # position |
| 130 | + 0.0, 0.0, 0.0, # rotation |
| 131 | + 0.8, 0.8, 0.8) # size |
| 132 | + spawn_srv.call(req2) |
| 133 | + rospy.sleep(1.0) |
| 134 | + |
| 135 | + # Spawn object 3 |
| 136 | + rospy.loginfo("Spawning cube3") |
| 137 | + req3 = create_cube_request("cube3", |
| 138 | + 0.0, 2.1, 0.41, # position |
| 139 | + 0.0, 0.0, 0.0, # rotation |
| 140 | + 0.4, 0.4, 0.4) # size |
| 141 | + spawn_srv.call(req3) |
| 142 | + rospy.sleep(1.0) |
| 143 | + |
| 144 | + # Link them |
| 145 | + rospy.loginfo("Attaching cube1 and cube2") |
| 146 | + amsg = Attach() |
| 147 | + amsg.model_name_1 = "cube1" |
| 148 | + amsg.link_name_1 = "link" |
| 149 | + amsg.model_name_2 = "cube2" |
| 150 | + amsg.link_name_2 = "link" |
| 151 | + |
| 152 | + attach_pub.publish(amsg) |
| 153 | + rospy.sleep(1.0) |
| 154 | + # From the shell: |
| 155 | + """ |
| 156 | +rostopic pub /link_attacher_node/attach_models gazebo_ros_link_attacher/Attach "model_name_1: 'cube1' |
| 157 | +link_name_1: 'link' |
| 158 | +model_name_2: 'cube2' |
| 159 | +link_name_2: 'link'" |
| 160 | + """ |
| 161 | + rospy.loginfo("Published into linking service!") |
| 162 | + |
| 163 | + |
| 164 | + rospy.loginfo("Attaching cube2 and cube3") |
| 165 | + amsg = Attach() |
| 166 | + amsg.model_name_1 = "cube2" |
| 167 | + amsg.link_name_1 = "link" |
| 168 | + amsg.model_name_2 = "cube3" |
| 169 | + amsg.link_name_2 = "link" |
| 170 | + |
| 171 | + attach_pub.publish(amsg) |
| 172 | + rospy.sleep(1.0) |
| 173 | + |
| 174 | + |
| 175 | + rospy.loginfo("Attaching cube3 and cube1") |
| 176 | + amsg = Attach() |
| 177 | + amsg.model_name_1 = "cube3" |
| 178 | + amsg.link_name_1 = "link" |
| 179 | + amsg.model_name_2 = "cube1" |
| 180 | + amsg.link_name_2 = "link" |
| 181 | + |
| 182 | + attach_pub.publish(amsg) |
| 183 | + rospy.sleep(2.0) |
0 commit comments