-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene_code.py
More file actions
181 lines (140 loc) · 5.5 KB
/
scene_code.py
File metadata and controls
181 lines (140 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# python
import random, os
import numpy as np
from geometry_msgs.msg import PoseStamped, Pose
NUM_TABLES = 2
NUM_CONTAINERS = 3
NUM_OBJECTS = 3
NUM_OBSTACLES = 4
def sysCall_init():
place_scene_objects()
global fake_subscriber
publish_scene_objects()
fake_subscriber = simROS.subscribe(
"/move_group/fake_controller_joint_states",
"sensor_msgs/JointState",
"joint_state_callback",
)
def sendTfMessage(objHandle, object_name):
base_frame = sim.getObject("/reference_frame")
orientation = sim.getObjectQuaternion(objHandle,base_frame)
position = sim.getObjectPosition(objHandle,base_frame)
msg = {
'header': {
'stamp': simROS.getTime(),
'frame_id': '/reference_frame'
},
'child_frame_id': object_name,
'transform': {
'rotation': {'x': orientation[0], 'y': orientation[1], 'z': orientation[2], 'w': orientation[3]},
'translation': {'x': position[0], 'y': position[1], 'z': position[2]}
}
}
simROS.sendTransform(msg)
def publish_scene_objects():
# get tf of tables
for i in range(1, NUM_TABLES+1):
name = f"Table_{i}"
sendTfMessage(scene_objects['tables'][name], name)
# get tf of obstacles
for i in range(1, NUM_OBSTACLES+1):
name = f"Obstacle_{i}"
sendTfMessage(scene_objects['obstacles'][name], name)
# get tf of containers (only the upper part)
for i in range(1, NUM_CONTAINERS+1):
name = f"Container_{i}"
upperpart_container= f"/{name}/upperPart"
sendTfMessage(sim.getObjectHandle(upperpart_container), name)
# get tf of objects
for i in range(1, NUM_OBJECTS+1):
name = f"Object_{i}"
sendTfMessage(scene_objects['objects'][name], name)
def joint_state_callback(object):
for i in range(len(object['name'])):
handle = sim.getObjectHandle(object['name'][i])
sim.setJointPosition(handle, object['position'][i])
# adjust finger joints
if object['name'][i] == "panda_finger_joint1":
handle = sim.getObjectHandle("panda_finger_joint2")
sim.setJointPosition(handle, object['position'][i])
# for adding a noise when placing objects on the table
def get_noisy_position(position, noise=0.05):
x, y, z = position
x = x + random.uniform(-noise, +noise)
y = y + random.uniform(-noise, +noise)
return [x, y, z]
def place_scene_objects():
global scene_objects
scene_objects = {
"objects": {},
"containers": {},
"obstacles": {},
"tables": {}
}
# Add tables to scene_objects
table0 = sim.getObject(f"/Table[0]")
table1 = sim.getObject(f"/Table[1]")
scene_objects["tables"]["Table_1"] = table0
scene_objects["tables"]["Table_2"] = table1
# Create raster for possible object locations
raster_size = 0.2
# Possible positions for table 0
possible_positions_t0 = []
for x in np.arange(-0.3, 0.5, raster_size+0.1):
for y in np.arange(-0.1, 0.3, raster_size):
possible_positions_t0.append([x, y, 0.1])
actual_positions_t0 = random.sample(
possible_positions_t0, NUM_OBJECTS+int(NUM_OBSTACLES/2))
sample_number_t0 = 0
# Possible positions for table 1
possible_positions_t1 = []
for x in np.arange(-0.3, 0.5, raster_size+0.1):
for y in np.arange(-0.15, 0.15, raster_size):
possible_positions_t1.append([x, y, 0.1])
actual_positions_t1 = random.sample(
possible_positions_t1, NUM_CONTAINERS+int(NUM_OBSTACLES/2))
sample_number_t1 = 0
# Randomly place objects
for i in range(1, NUM_OBJECTS+1):
object_name = f"Object_{i}"
object = sim.getObject(f"/{object_name}")
scene_objects["objects"][object_name] = object
position = get_noisy_position(actual_positions_t0[sample_number_t0])
sim.setObjectPosition(object, table0, position)
#randomize orientation for the cubes
rand_orientation = random.uniform(0,2*np.pi)
sim.setObjectOrientation(object, table0, [0,0,rand_orientation])
print(f"Placed object {i} at {position} w.r.t. Table[0]")
sample_number_t0 += 1
# Randomly place containers
for i in range(1, NUM_CONTAINERS+1):
container_name = f"Container_{i}"
container = sim.getObject(f"/{container_name}")
scene_objects["containers"][container_name] = container
position = get_noisy_position(actual_positions_t1[sample_number_t1], 0.02)
sim.setObjectPosition(container, table1, position)
print(f"Placed container {i} at {position} w.r.t. Table[1]")
sample_number_t1 += 1
# Randomly place obstacles
for i in range(1, NUM_OBSTACLES+1):
obstacle_name = f"Obstacle_{i}"
obstacle = sim.getObject(f"/{obstacle_name}")
table_name = "Table[0]" if (i % 2 == 0) else "Table[1]"
table = table0 if (i % 2 == 0) else table1
sample_number = sample_number_t0 if (i % 2 == 0) else sample_number_t1
actual_positions = actual_positions_t0 if (
i % 2 == 0) else actual_positions_t1
scene_objects["obstacles"][obstacle_name] = obstacle
position = get_noisy_position(actual_positions[sample_number])
sim.setObjectPosition(obstacle, table, position)
print(f"Placed obstacle {i} at {position} w.r.t. {table_name}")
if (i % 2 == 0):
sample_number_t0 += 1
else:
sample_number_t1 += 1
def sysCall_actuation():
pass
def sysCall_sensing():
pass
def sysCall_cleanup():
pass