-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpendulum_compare.py
More file actions
172 lines (147 loc) · 6.69 KB
/
pendulum_compare.py
File metadata and controls
172 lines (147 loc) · 6.69 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
import argparse
import json
import time
# state the number of links in your urdf (edit this as necessary)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='example script to demonstrate pybullet simulator and control of a 2D N-link robot') # add capability of user input
parser.add_argument('-num_links', '--number_of_links_urdf', help='number of links in urdf', type=int,
default=1) # set default input to 1 if none is given
arguments = parser.parse_args()
print('type: ', type(arguments.number_of_links_urdf))
number_of_links_urdf = int(arguments.number_of_links_urdf) # define the number of links
# I ran this script using python 3.7.2
# import the os module (needed when p.loadURDF() method is invoked)
import os
# imports the pybullet simulator API
import pybullet as p
# imports the pybullet_data module (used to figure out where the robot model needs to be loaded from)
import pybullet_data
# allows python script to be called with inputs
# loads the pybullet physics simulator (with no robot yet)
p.connect(p.GUI)
# this prints out where the dataPath base directory is. The DataPath base directory is the default directory where pybullet looks in order to load a robot model (when the p.loadURDF() method is invoked)
print(pybullet_data.getDataPath())
# state the directory extension for pybullet to find your urdf (edit this as necessary)
# model_urdf_directory_extension = "romans_urdf_files/octopus_files/python_scripts_edit_urdf/octopus_generated_"+str(number_of_links_urdf)+"_links.urdf"
# if you do not want to create an extension simply uncomment this line
model_urdf_directory_extension = "cs292c_robot_models/octopus_generated_" + str(number_of_links_urdf) + "_links.urdf"
# this line loads a URDF (3D robot model) into pybullet physics simulator, and it returns a unique ID. That unique ID is needed when querying information about the robot. It is also used when controlling the robot
# octopusBodyUniqueId = p.loadURDF( fileName=os.path.join(pybullet_data.getDataPath(), model_urdf_directory_extension), flags=p.URDF_USE_SELF_COLLISION | p.URDF_USE_SELF_COLLISION_EXCLUDE_ALL_PARENTS)
# print(os.getcwd())
# p.setAdditionalSearchPath(os.getcwd())
pendulum_uniqueId_pybullet = p.loadURDF(
fileName=os.path.join(pybullet_data.getDataPath(), model_urdf_directory_extension),
flags=p.URDF_USE_SELF_COLLISION | p.URDF_USE_SELF_COLLISION_EXCLUDE_ALL_PARENTS)
pendulum_uniqueId_z3 = p.loadURDF(fileName=os.path.join(pybullet_data.getDataPath(), model_urdf_directory_extension),
flags=p.URDF_USE_SELF_COLLISION | p.URDF_USE_SELF_COLLISION_EXCLUDE_ALL_PARENTS)
p.setCollisionFilterPair(bodyUniqueIdA=pendulum_uniqueId_pybullet,
bodyUniqueIdB=pendulum_uniqueId_z3,
linkIndexA=0,
linkIndexB=0,
enableCollision=0
)
p.setCollisionFilterPair(bodyUniqueIdA=pendulum_uniqueId_pybullet,
bodyUniqueIdB=pendulum_uniqueId_z3,
linkIndexA=0,
linkIndexB=-1,
enableCollision=0
)
p.setCollisionFilterPair(bodyUniqueIdA=pendulum_uniqueId_pybullet,
bodyUniqueIdB=pendulum_uniqueId_z3,
linkIndexA=-1,
linkIndexB=0,
enableCollision=0
)
# enables gravity
p.setGravity(0, 0, -9.8)
# sets real time simulation
p.setRealTimeSimulation(
enableRealTimeSimulation=0) # now we dont have to call p.stepSimulation() in order to advance the time step of the simulation environment
# turn off all motors so that joints are not stiffened for the rest of the simulations
p.setJointMotorControlArray(
bodyUniqueId=pendulum_uniqueId_pybullet,
jointIndices=list(range(number_of_links_urdf)),
controlMode=p.TORQUE_CONTROL,
positionGains=[0.1] * number_of_links_urdf,
velocityGains=[0.1] * number_of_links_urdf,
forces=[0] * number_of_links_urdf
)
# load the block thatwe are trying to avoid
targetPositions = [0] * (number_of_links_urdf)
targetPositions[0] = 3.14 * (36 / 360) # set desired joint angles in radians
targetVelocities = [0] * (number_of_links_urdf) # set desired joint velocities
# move the robot according to desited joint angles and desired joint velocities using PD control
p.setJointMotorControlArray(
bodyUniqueId=pendulum_uniqueId_pybullet,
jointIndices=list(range(number_of_links_urdf)),
controlMode=p.POSITION_CONTROL,
# targetPositions=targetPositions,
velocityGains=[0] * number_of_links_urdf,
positionGains=[0] * number_of_links_urdf,
forces=[0] * number_of_links_urdf
)
p.setJointMotorControlArray(
bodyUniqueId=pendulum_uniqueId_z3,
jointIndices=list(range(number_of_links_urdf)),
controlMode=p.POSITION_CONTROL,
# targetPositions=targetPositions,
velocityGains=[0] * number_of_links_urdf,
positionGains=[0] * number_of_links_urdf,
forces=[0] * number_of_links_urdf
)
p.changeDynamics(
bodyUniqueId=pendulum_uniqueId_pybullet,
linkIndex=0,
jointDamping=0.1
)
p.changeDynamics(
bodyUniqueId=pendulum_uniqueId_z3,
linkIndex=0,
jointDamping=0.1
)
j = 0
p.resetJointState(
bodyUniqueId=pendulum_uniqueId_pybullet,
jointIndex=0,
targetValue=1,
targetVelocity=0
)
p.resetJointState(
bodyUniqueId=pendulum_uniqueId_z3,
jointIndex=0,
targetValue=1,
targetVelocity=0
)
f_z3 = open("position_velocity_z3_data.txt")
lines = f_z3.read()
f_z3.close()
z3_data_blob = json.loads(lines)
z3_sorted_ts = sorted(map(lambda x: int(x), z3_data_blob.keys()))
f_pybullet = open("position_velocity_pybullet_data.txt")
lines = f_pybullet.read()
f_pybullet.close()
pybullet_data_blob = json.loads(lines)
pybullet_sorted_ts = sorted(map(lambda x: int(x), pybullet_data_blob.keys()))
timesteps = json.load(open("config.json"))["time_steps"]
i = 0
while i < timesteps:
if i not in z3_sorted_ts:
i += 1
continue
p.resetJointState(
bodyUniqueId=pendulum_uniqueId_pybullet,
jointIndex=0,
targetValue=z3_data_blob[str(i)]["position"],
targetVelocity=z3_data_blob[str(i)]["velocity"]
)
p.resetJointState(
bodyUniqueId=pendulum_uniqueId_z3,
jointIndex=0,
targetValue=pybullet_data_blob[str(i)]["position"],
targetVelocity=pybullet_data_blob[str(i)]["velocity"]
)
a = p.getJointStates(bodyUniqueId=pendulum_uniqueId_pybullet, jointIndices=list(range(number_of_links_urdf)))
b = p.getJointStates(bodyUniqueId=pendulum_uniqueId_z3, jointIndices=list(range(number_of_links_urdf)))
p.stepSimulation()
i += 1