-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpybullet01.py
49 lines (40 loc) · 1.65 KB
/
pybullet01.py
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
import pybullet as p
import time
import pybullet_data
class globals:
pass
# load URDF file in python and pybullet, store initial conditions
def init_sim():
if not p.isConnected():
globals.physicsClient = p.connect(p.GUI) #or p.DIRECT for non-graphical version
p.setAdditionalSearchPath(pybullet_data.getDataPath()) #optionally
else:
p.resetSimulation(globals.physicsClient)
p.setGravity(0, 0, -10)
planeId = p.loadURDF("plane.urdf")
cubeStartOrientation = p.getQuaternionFromEuler([0, 0, 0])
globals.sph1 = p.loadURDF("robots/capsule01.urdf", [0, 0, 3], cubeStartOrientation)
globals.sph2 = p.loadURDF("robots/sphere01_man.urdf", [0, 0, 5], cubeStartOrientation)
globals.t_step = p.getPhysicsEngineParameters()['fixedTimeStep']
globals.curr_step = 0
p.setJointMotorControl2(globals.sph1, 0, controlMode=p.VELOCITY_CONTROL, force=0)
def distance(obj1, obj2):
"""Calculate the scalar Euclidean distance between two objects."""
pos1 = p.getBasePositionAndOrientation(obj1)[0]
pos2 = p.getBasePositionAndOrientation(obj2)[0]
return np.sqrt(np.sum(np.subtract(pos1, pos2)**2))
def up_force(obj, mag):
"""Upward force on obj of a given magnitude mag."""
p.applyExternalForce(objectUniqueId=obj, linkIndex=-1, forceObj=[0, 0, mag], posObj=[0, 0, 0], flags=p.LINK_FRAME)
def sim(n_steps, animate=True):
for i in range(n_steps):
up_force(globals.sph2, 9)
p.stepSimulation()
if animate:
time.sleep(globals.t_step)
globals.curr_step += n_steps
if __name__ == "__main__":
init_sim()
sim(500)
time.sleep(15)
p.disconnect(globals.physicsCilent)