-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpendulum_manipulator.py
201 lines (167 loc) · 7.31 KB
/
pendulum_manipulator.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import argparse
import json
import math
import time
number_of_links_urdf = 1
# 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 scrip[t using python 3.7.2
# import the os mudule (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
import argparse
# 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 timestep 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
initial_position = 15
initial_velocity = 1
# 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.5
)
p.changeDynamics(
bodyUniqueId=pendulum_uniqueId_z3,
linkIndex=0,
jointDamping=0.5
)
j = 0
p.resetJointState(
bodyUniqueId=pendulum_uniqueId_pybullet,
jointIndex=0,
targetValue=initial_position,
targetVelocity=initial_velocity
)
p.resetJointState(
bodyUniqueId=pendulum_uniqueId_z3,
jointIndex=0,
targetValue=initial_position,
targetVelocity=initial_velocity
)
time_step = 0
previous_theta = []
previous_theta_d = 0
# check equality
data = {}
config_file = open("config.json")
config = json.load(config_file)
"""
take the values from Z3, and call resetJointState,
{"0": {"position": 1.0, "velocity": 0.0}, "23": {"position": 1.0141867807385068,
"velocity": 0.40087002089676194}, "196": {"position": 1.015961884242391,
"velocity": 0.4260248409322054}, "247": {"position": 1.0587030949993685,
"velocity": 0.8321196745638638}, "431":
"""
def simulate_pendulums(init_position=None, init_velocity=None):
global initial_velocity, initial_position, number_of_links_urdf, data, p
if init_velocity and init_velocity:
initial_velocity = init_velocity
initial_position = init_position
p.resetJointState(
bodyUniqueId=pendulum_uniqueId_pybullet,
jointIndex=0,
targetValue=initial_position,
targetVelocity=initial_velocity
)
p.resetJointState(
bodyUniqueId=pendulum_uniqueId_z3,
jointIndex=0,
targetValue=initial_position,
targetVelocity=initial_velocity
)
f = open("position_velocity_pybullet_data.txt", "w")
i = 0
while i < config["time_steps"]:
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)))
data[i] = {
"position": a[0][0],
"velocity": a[0][1]
}
i += 1
p.stepSimulation()
#time.sleep(1)
f.write(json.dumps(data))
f.close()
if __name__ == "__main__":
simulate_pendulums()