Skip to content

Commit 4db9468

Browse files
Zackory EricksonZackory Erickson
Zackory Erickson
authored and
Zackory Erickson
committed
Lecture 2 code examples
1 parent 4324fce commit 4db9468

File tree

11 files changed

+156
-22
lines changed

11 files changed

+156
-22
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ __pycache__/
88
*.out
99
trained_models*
1010
*.json
11+
*.swp
1112

1213
# C extensions
1314
*.so

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ python3 -m pip install mengine@git+https://github.com/Zackory/mengine.git
1919
## Example
2020
```bash
2121
cd examples
22-
python3 example_push.py
22+
python3 lecture01_pushing.py
2323
```
2424

2525
![Manipulation Engine](images/mengine.jpg "Manipulation Engine")

examples/lecture02_robots.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import os
2+
import numpy as np
3+
import mengine as m
4+
5+
# Create environment and ground plane
6+
env = m.Env()
7+
ground = m.Ground()
8+
9+
env.set_gui_camera(distance=2.5)
10+
11+
# Create robots
12+
robot = m.Robot.Baxter(position=[-2, 0, 0.92], orientation=[0, 0, -np.pi/2])
13+
14+
robot2 = m.Robot.PR2(position=[-0.5, 0, 0], orientation=[0, 0, -np.pi/2])
15+
16+
robot3 = m.Robot.Stretch(position=[0.5, 0, 0], orientation=[0, 0, -np.pi/2])
17+
18+
robot4 = m.Robot.Panda(position=[1.5, 0, 0])
19+
20+
robot5 = m.Robot.Jaco(position=[2, 0, 0])
21+
22+
m.step_simulation(steps=1000, realtime=True)
23+
24+

examples/lecture02_sensors.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import os
2+
import numpy as np
3+
import mengine as m
4+
import matplotlib.pyplot as plt
5+
np.set_printoptions(precision=3, suppress=True)
6+
7+
# Create environment and ground plane
8+
env = m.Env()
9+
ground = m.Ground()
10+
11+
env.set_gui_camera(distance=2, yaw=30)
12+
13+
# Create table
14+
table = m.URDF(filename=os.path.join(m.directory, 'table', 'table.urdf'), static=True, position=[0, 0, 0], orientation=[0, 0, 0, 1])
15+
16+
# Create robots
17+
panda = m.Robot.Panda(position=[0.5, 0, 0.75])
18+
panda.enable_force_torque_sensor(8) # Create a F/T sensor at the wrist
19+
# panda.print_joint_info()
20+
21+
stretch = m.Robot.Stretch(position=[0.25, -1, 0], orientation=[0, 0, np.pi/2])
22+
# stretch.print_joint_info()
23+
# Rotate the camera
24+
stretch.set_joint_angles(angles=[-np.pi/8], joints=[21])
25+
# stretch.set_joint_angles(angles=[-np.pi/4], joints=[20])
26+
27+
# Create a cube to make contact with
28+
cube = m.Shape(m.Box(half_extents=[0.1]*3), static=False, mass=1, position=[-0.25, 0, 0.85], orientation=[0, 0, 0, 1], rgba=[0, 1, 0, 1])
29+
30+
for i in range(50):
31+
# Move the end effector into a cube and print sensor values
32+
position, orientation = panda.get_link_pos_orient(panda.end_effector)
33+
target_joint_angles = panda.ik(panda.end_effector, target_pos=position+[-0.08, 0, 0], target_orient=orientation, use_current_joint_angles=True)
34+
panda.control(target_joint_angles)
35+
36+
m.step_simulation(realtime=True)
37+
38+
if i % 20 == 0:
39+
# Print joint position, velocities, and torques
40+
motor_indices, motor_positions, motor_velocities, motor_torques = panda.get_motor_joint_states()
41+
print('Joint angles:', np.array(motor_positions))
42+
print('Joint velocities:', np.array(motor_velocities))
43+
print('Joint torques:', np.array(motor_torques))
44+
print('Force-torque:', panda.get_force_torque_sensor(8))
45+
linkA, linkB, posA, posB, contact_distance = panda.get_closest_points(bodyB=cube, linkA=8)
46+
print('Proximity (shortest distance from end effector to cube):', contact_distance[0])
47+
print('')
48+
49+
# Get the position and orientation for the camera_color_optical_joint
50+
head_pos, head_orient = stretch.get_link_pos_orient(30)
51+
# Move link slightly out of the mesh
52+
head_pos, head_orient = m.multiply_transforms(head_pos, head_orient, [0.01, 0, 0], [0, 0, 0])
53+
# m.visualize_coordinate_frame(head_pos, head_orient)
54+
55+
# Create a virtual camera for the Stretch's RealSense
56+
camera = m.Camera(fov=60, camera_width=1920//4, camera_height=1080//4)
57+
rpy = m.get_euler(head_orient)
58+
# Align axes between head and camera
59+
rpy = [rpy[0]-np.pi/2, rpy[1], rpy[2]+np.pi/2]
60+
camera.set_camera_rpy(look_at_pos=head_pos, distance=-0.001, rpy=np.degrees(rpy))
61+
62+
img, depth, segmentation_mask = camera.get_rgba_depth()
63+
64+
plt.figure(); plt.title('RGB'); plt.imshow(img)
65+
plt.figure(); plt.title('Depth'); plt.imshow(depth)
66+
plt.figure(); plt.title('Segmentation Mask'); plt.imshow(segmentation_mask)
67+
plt.show()
68+
69+
# Capture a point cloud from the camera
70+
pc, rgba = camera.get_point_cloud(body=panda)
71+
# pc, rgba = camera.get_point_cloud()
72+
# Visualize the point cloud
73+
m.DebugPoints(pc, points_rgb=rgba[:, :3], size=10)
74+
# Hide the panda to see the visualized point cloud
75+
panda.set_base_pos_orient([0.5, 0, 100.75])
76+
77+
m.step_simulation(steps=1000, realtime=True)
78+

mengine/bodies/baxter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ def __init__(self, env, position=[0, 0, 0], orientation=[0, 0, 0, 1], controllab
1414
# right_gripper_indices = [27, 29] # Gripper actuated joints
1515
# left_gripper_indices = [49, 51] # Gripper actuated joints
1616

17-
body = p.loadURDF(os.path.join(directory, 'baxter', 'baxter_custom.urdf'), useFixedBase=fixed_base, basePosition=position, baseOrientation=orientation, physicsClientId=env.id)
17+
body = p.loadURDF(os.path.join(env.directory, 'baxter', 'baxter_custom.urdf'), useFixedBase=fixed_base, basePosition=position, baseOrientation=orientation, physicsClientId=env.id)
1818
super().__init__(body, env, controllable_joints, end_effector, gripper_joints)
1919

2020
# Recolor robot
2121
for i in [20, 21, 23, 31, 32, 42, 43, 45, 53, 54]:
22-
p.changeVisualShape(self.body, i, rgbaColor=[1.0, 1.0, 1.0, 0.0], physicsClientId=id)
22+
p.changeVisualShape(self.body, i, rgbaColor=[1.0, 1.0, 1.0, 0.0], physicsClientId=env.id)
2323
# Close gripper
2424
# self.set_gripper_position([0]*2, set_instantly=True)
2525

mengine/bodies/jaco.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ def __init__(self, env, position=[0, 0, 0], orientation=[0, 0, 0, 1], controllab
77
controllable_joints = [1, 2, 3, 4, 5, 6, 7] if controllable_joints is None else controllable_joints
88
end_effector = 8 # Used to get the pose of the end effector
99
gripper_joints = [9, 11, 13] # Gripper actuated joints
10-
body = p.loadURDF(os.path.join(directory, 'jaco', 'j2s7s300_gym.urdf'), useFixedBase=fixed_base, basePosition=position, baseOrientation=orientation, physicsClientId=env.id)
10+
body = p.loadURDF(os.path.join(env.directory, 'jaco', 'j2s7s300_gym.urdf'), useFixedBase=fixed_base, basePosition=position, baseOrientation=orientation, physicsClientId=env.id)
1111
super().__init__(body, env, controllable_joints, end_effector, gripper_joints)
1212

mengine/bodies/kinova_gen3.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ def __init__(self, env, position=[0, 0, 0], orientation=[0, 0, 0, 1], controllab
77
controllable_joints = [0, 1, 2, 3, 4, 5, 6] if controllable_joints is None else controllable_joints
88
end_effector = 7 # Used to get the pose of the end effector
99
gripper_joints = [] # Gripper actuated joints
10-
body = p.loadURDF(os.path.join(directory, 'kinova_gen3', 'GEN3_URDF_V12.urdf'), useFixedBase=fixed_base, basePosition=position, baseOrientation=orientation, physicsClientId=env.id)
10+
body = p.loadURDF(os.path.join(env.directory, 'kinova_gen3', 'GEN3_URDF_V12.urdf'), useFixedBase=fixed_base, basePosition=position, baseOrientation=orientation, physicsClientId=env.id)
1111
super().__init__(body, env, controllable_joints, end_effector, gripper_joints)
1212

mengine/bodies/pr2.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ def __init__(self, env, position=[0, 0, 0], orientation=[0, 0, 0, 1], controllab
1515
# right_gripper_indices = [57, 58, 59, 60] # Gripper actuated joints
1616
# left_gripper_indices = [79, 80, 81, 82] # Gripper actuated joints
1717

18-
body = p.loadURDF(os.path.join(directory, 'PR2', 'pr2_no_torso_lift_tall.urdf'), useFixedBase=fixed_base, basePosition=position, baseOrientation=orientation, flags=p.URDF_USE_INERTIA_FROM_FILE, physicsClientId=env.id)
18+
body = p.loadURDF(os.path.join(env.directory, 'PR2', 'pr2_no_torso_lift_tall.urdf'), useFixedBase=fixed_base, basePosition=position, baseOrientation=orientation, flags=p.URDF_USE_INERTIA_FROM_FILE, physicsClientId=env.id)
1919
super().__init__(body, env, controllable_joints, end_effector, gripper_joints)
2020

2121
# Recolor robot
2222
for i in [19, 42, 64]:
23-
p.changeVisualShape(self.body, i, rgbaColor=[1.0, 1.0, 1.0, 1.0], physicsClientId=id)
23+
p.changeVisualShape(self.body, i, rgbaColor=[1.0, 1.0, 1.0, 1.0], physicsClientId=env.id)
2424
for i in [43, 46, 49, 58, 60, 65, 68, 71, 80, 82]:
25-
p.changeVisualShape(self.body, i, rgbaColor=[0.4, 0.4, 0.4, 1.0], physicsClientId=id)
25+
p.changeVisualShape(self.body, i, rgbaColor=[0.4, 0.4, 0.4, 1.0], physicsClientId=env.id)
2626
for i in [45, 51, 67, 73]:
27-
p.changeVisualShape(self.body, i, rgbaColor=[0.7, 0.7, 0.7, 1.0], physicsClientId=id)
28-
p.changeVisualShape(self.body, 20, rgbaColor=[0.8, 0.8, 0.8, 1.0], physicsClientId=id)
29-
p.changeVisualShape(self.body, 40, rgbaColor=[0.6, 0.6, 0.6, 1.0], physicsClientId=id)
27+
p.changeVisualShape(self.body, i, rgbaColor=[0.7, 0.7, 0.7, 1.0], physicsClientId=env.id)
28+
p.changeVisualShape(self.body, 20, rgbaColor=[0.8, 0.8, 0.8, 1.0], physicsClientId=env.id)
29+
p.changeVisualShape(self.body, 40, rgbaColor=[0.6, 0.6, 0.6, 1.0], physicsClientId=env.id)
3030
# Close gripper
3131
# self.set_gripper_position([0]*2, set_instantly=True)
3232

mengine/bodies/stretch.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def __init__(self, env, position=[0, 0, 0], orientation=[0, 0, 0, 1], controllab
77
controllable_joints = [3, 5, 6, 7, 8, 9] if controllable_joints is None else controllable_joints
88
end_effector = 15 # Used to get the pose of the end effector
99
gripper_joints = [11, 13] # Gripper actuated joints
10-
body = p.loadURDF(os.path.join(directory, 'stretch', 'stretch_uncalibrated.urdf'), useFixedBase=fixed_base, basePosition=position, baseOrientation=orientation, physicsClientId=env.id)
10+
body = p.loadURDF(os.path.join(env.directory, 'stretch', 'stretch_uncalibrated.urdf'), useFixedBase=fixed_base, basePosition=position, baseOrientation=orientation, physicsClientId=env.id)
1111
super().__init__(body, env, controllable_joints, end_effector, gripper_joints)
1212

1313
# Recolor robot
@@ -16,11 +16,11 @@ def __init__(self, env, position=[0, 0, 0], orientation=[0, 0, 0, 1], controllab
1616
dark_gray = [0.4, 0.4, 0.4, 1]
1717
black = [0.251, 0.251, 0.251, 1]
1818
for i in [0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 12, 14, 19, 20, 23, 35]:
19-
p.changeVisualShape(self.body, i, rgbaColor=black, physicsClientId=id)
19+
p.changeVisualShape(self.body, i, rgbaColor=black, physicsClientId=env.id)
2020
for i in [-1, 11, 13, 21, 16, 17, 18, 33, 34]:
21-
p.changeVisualShape(self.body, i, rgbaColor=gray, physicsClientId=id)
21+
p.changeVisualShape(self.body, i, rgbaColor=gray, physicsClientId=env.id)
2222
# for i in [16, 17, 18, 33, 34]:
2323
# p.changeVisualShape(self.body, i, rgbaColor=white, physicsClientId=id)
2424
for i in [3, 32]:
25-
p.changeVisualShape(self.body, i, rgbaColor=dark_gray, physicsClientId=id)
25+
p.changeVisualShape(self.body, i, rgbaColor=dark_gray, physicsClientId=env.id)
2626

mengine/env.py

+37-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
from .util import Util
88
from .bodies.body import Body
99
from .bodies.panda import Panda
10+
from .bodies.jaco import Jaco
11+
from .bodies.baxter import Baxter
12+
from .bodies.pr2 import PR2
13+
from .bodies.stretch import Stretch
14+
from .bodies.kinova_gen3 import KinovaGen3
1015

1116
envir = None
1217
directory = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'assets')
@@ -100,6 +105,31 @@ def __init__(self, position=[0, 0, 0], orientation=[0, 0, 0, 1], controllable_jo
100105
env = env if env is not None else envir
101106
super().__init__(env, position, get_quaternion(orientation), controllable_joints, fixed_base)
102107

108+
class Jaco(Jaco):
109+
def __init__(self, position=[0, 0, 0], orientation=[0, 0, 0, 1], controllable_joints=None, fixed_base=True, env=None):
110+
env = env if env is not None else envir
111+
super().__init__(env, position, get_quaternion(orientation), controllable_joints, fixed_base)
112+
113+
class Baxter(Baxter):
114+
def __init__(self, position=[0, 0, 0], orientation=[0, 0, 0, 1], controllable_joints=None, fixed_base=True, env=None):
115+
env = env if env is not None else envir
116+
super().__init__(env, position, get_quaternion(orientation), controllable_joints, fixed_base)
117+
118+
class PR2(PR2):
119+
def __init__(self, position=[0, 0, 0], orientation=[0, 0, 0, 1], controllable_joints=None, fixed_base=True, env=None):
120+
env = env if env is not None else envir
121+
super().__init__(env, position, get_quaternion(orientation), controllable_joints, fixed_base)
122+
123+
class Stretch(Stretch):
124+
def __init__(self, position=[0, 0, 0], orientation=[0, 0, 0, 1], controllable_joints=None, fixed_base=True, env=None):
125+
env = env if env is not None else envir
126+
super().__init__(env, position, get_quaternion(orientation), controllable_joints, fixed_base)
127+
128+
class KinovaGen3(KinovaGen3):
129+
def __init__(self, position=[0, 0, 0], orientation=[0, 0, 0, 1], controllable_joints=None, fixed_base=True, env=None):
130+
env = env if env is not None else envir
131+
super().__init__(env, position, get_quaternion(orientation), controllable_joints, fixed_base)
132+
103133
class Camera:
104134
def __init__(self, camera_pos=[0.5, -0.5, 1.5], look_at_pos=[0, 0, 0.75], fov=60, camera_width=1920//4, camera_height=1080//4, env=None):
105135
self.env = env if env is not None else envir
@@ -141,13 +171,14 @@ def get_point_cloud(self, body=None):
141171
pixels = np.stack([x, y, z, h], axis=1)
142172

143173
# Filter point cloud to only include points on the target body
144-
pixels = pixels[segmentation_mask == body.body]
145-
z = z[segmentation_mask == body.body]
146-
rgba = rgba[segmentation_mask == body.body]
174+
if body is not None:
175+
pixels = pixels[segmentation_mask == body.body]
176+
z = z[segmentation_mask == body.body]
177+
rgba = rgba[segmentation_mask == body.body]
147178

148179
# filter out "infinite" depths
149-
pixels = pixels[z < 0.99]
150-
rgba = rgba[z < 0.99]
180+
pixels = pixels[z < 20]
181+
rgba = rgba[z < 20]
151182
pixels[:, 2] = 2 * pixels[:, 2] - 1
152183

153184
# turn pixels to world coordinates
@@ -182,7 +213,7 @@ def get_difference_quaternion(q1, q2, env=None):
182213
def quaternion_product(q1, q2, env=None):
183214
env = env if env is not None else envir
184215
# Return Hamilton product of 2 quaternions
185-
return p.multiplyTransforms([0, 0, 0], get_quaternion(q1), [0, 0, 0], q2, physicsClientId=env.id)[1]
216+
return p.multiplyTransforms([0, 0, 0], get_quaternion(q1), [0, 0, 0], get_quaternion(q2), physicsClientId=env.id)[1]
186217

187218
def multiply_transforms(p1, q1, p2, q2, env=None):
188219
env = env if env is not None else envir

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
packages=find_packages(),
1919
python_requires='>=3',
2020
# install_requires=['gym>=0.2.3', 'pybullet @ git+https://github.com/Zackory/bullet3.git@pybullet_3_0_9#egg=pybullet', 'numpy', 'keras==2.3.0', 'tensorflow==1.14.0', 'h5py==2.10.0', 'smplx', 'trimesh', 'ray[rllib]', 'numpngw', 'tensorflow-probability==0.7.0', 'matplotlib'] + ['screeninfo==0.6.1' if sys.version_info >= (3, 6) else 'screeninfo==0.2'],
21-
install_requires=['pybullet', 'numpy', 'scipy', 'numpngw'] + ['screeninfo==0.6.1' if sys.version_info >= (3, 6) else 'screeninfo==0.2'],
21+
install_requires=['pybullet', 'numpy', 'scipy', 'numpngw', 'matplotlib'] + ['screeninfo==0.6.1' if sys.version_info >= (3, 6) else 'screeninfo==0.2'],
2222
# description='Physics simulation for assistive robotics and human-robot interaction.',
2323
# long_description=long_description,
2424
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)