|
1 | 1 |
|
2 | 2 | import pybullet as p
|
| 3 | +from pdControllerExplicit import PDControllerExplicit |
| 4 | +from pdControllerExplicit import PDControllerStable |
| 5 | + |
3 | 6 | import time
|
4 | 7 |
|
| 8 | + |
5 | 9 | useMaximalCoordinates=False
|
6 | 10 | p.connect(p.GUI)
|
7 |
| -pole = p.loadURDF("cartpole.urdf", useMaximalCoordinates=useMaximalCoordinates) |
8 |
| -for i in range (p.getNumJoints(pole)): |
| 11 | +pole = p.loadURDF("cartpole.urdf", [0,0,0], useMaximalCoordinates=useMaximalCoordinates) |
| 12 | +pole2 = p.loadURDF("cartpole.urdf", [0,1,0], useMaximalCoordinates=useMaximalCoordinates) |
| 13 | +pole3 = p.loadURDF("cartpole.urdf", [0,2,0], useMaximalCoordinates=useMaximalCoordinates) |
| 14 | +pole4 = p.loadURDF("cartpole.urdf", [0,3,0], useMaximalCoordinates=useMaximalCoordinates) |
| 15 | + |
| 16 | +exPD = PDControllerExplicit(p) |
| 17 | +sPD = PDControllerStable(p) |
| 18 | + |
| 19 | + |
| 20 | +for i in range (p.getNumJoints(pole2)): |
9 | 21 | #disable default constraint-based motors
|
10 | 22 | p.setJointMotorControl2(pole,i,p.POSITION_CONTROL,targetPosition=0,force=0)
|
11 |
| - print("joint",i,"=",p.getJointInfo(pole,i)) |
| 23 | + p.setJointMotorControl2(pole2,i,p.POSITION_CONTROL,targetPosition=0,force=0) |
| 24 | + p.setJointMotorControl2(pole3,i,p.POSITION_CONTROL,targetPosition=0,force=0) |
| 25 | + p.setJointMotorControl2(pole4,i,p.POSITION_CONTROL,targetPosition=0,force=0) |
| 26 | + |
| 27 | + #print("joint",i,"=",p.getJointInfo(pole2,i)) |
12 | 28 |
|
13 | 29 |
|
| 30 | +timeStepId = p.addUserDebugParameter("timeStep",0.001,0.1,0.01) |
14 | 31 | desiredPosCartId = p.addUserDebugParameter("desiredPosCart",-10,10,2)
|
15 | 32 | desiredVelCartId = p.addUserDebugParameter("desiredVelCart",-10,10,0)
|
16 |
| -kpCartId = p.addUserDebugParameter("kpCart",0,500,300) |
| 33 | +kpCartId = p.addUserDebugParameter("kpCart",0,500,1300) |
17 | 34 | kdCartId = p.addUserDebugParameter("kdCart",0,300,150)
|
18 | 35 | maxForceCartId = p.addUserDebugParameter("maxForceCart",0,5000,1000)
|
19 | 36 |
|
| 37 | +textColor = [1,1,1] |
| 38 | +shift = 0.05 |
| 39 | +p.addUserDebugText("explicit PD", [shift,0,.1],textColor,parentObjectUniqueId=pole,parentLinkIndex=1) |
| 40 | +p.addUserDebugText("explicit PD plugin", [shift,0,-.1],textColor,parentObjectUniqueId=pole2,parentLinkIndex=1) |
| 41 | +p.addUserDebugText("stablePD", [shift,0,.1],textColor,parentObjectUniqueId=pole4,parentLinkIndex=1) |
| 42 | +p.addUserDebugText("position constraint", [shift,0,-.1],textColor,parentObjectUniqueId=pole3,parentLinkIndex=1) |
20 | 43 |
|
21 | 44 | desiredPosPoleId = p.addUserDebugParameter("desiredPosPole",-10,10,0)
|
22 | 45 | desiredVelPoleId = p.addUserDebugParameter("desiredVelPole",-10,10,0)
|
23 |
| -kpPoleId = p.addUserDebugParameter("kpPole",0,500,200) |
| 46 | +kpPoleId = p.addUserDebugParameter("kpPole",0,500,1200) |
24 | 47 | kdPoleId = p.addUserDebugParameter("kdPole",0,300,100)
|
25 | 48 | maxForcePoleId = p.addUserDebugParameter("maxForcePole",0,5000,1000)
|
26 | 49 |
|
|
29 | 52 |
|
30 | 53 | p.setGravity(0,0,-10)
|
31 | 54 |
|
32 |
| -useRealTimeSim = True |
| 55 | +useRealTimeSim = False |
33 | 56 |
|
34 | 57 | p.setRealTimeSimulation(useRealTimeSim)
|
35 | 58 |
|
36 |
| -p.setTimeStep(0.001) |
| 59 | +timeStep = 0.001 |
37 | 60 |
|
38 | 61 |
|
39 | 62 | while p.isConnected():
|
| 63 | + p.getCameraImage(320,200) |
| 64 | + timeStep = p.readUserDebugParameter(timeStepId) |
| 65 | + p.setTimeStep(timeStep) |
| 66 | + |
| 67 | + desiredPosCart = p.readUserDebugParameter(desiredPosCartId) |
| 68 | + desiredVelCart = p.readUserDebugParameter(desiredVelCartId) |
| 69 | + kpCart = p.readUserDebugParameter(kpCartId) |
| 70 | + kdCart = p.readUserDebugParameter(kdCartId) |
| 71 | + maxForceCart = p.readUserDebugParameter(maxForceCartId) |
| 72 | + |
| 73 | + desiredPosPole = p.readUserDebugParameter(desiredPosPoleId) |
| 74 | + desiredVelPole = p.readUserDebugParameter(desiredVelPoleId) |
| 75 | + kpPole = p.readUserDebugParameter(kpPoleId) |
| 76 | + kdPole = p.readUserDebugParameter(kdPoleId) |
| 77 | + maxForcePole = p.readUserDebugParameter(maxForcePoleId) |
| 78 | + |
| 79 | + taus = exPD.computePD(pole, [0,1], [desiredPosCart,desiredPosPole],[desiredVelCart,desiredVelPole], [kpCart,kpPole], [kdCart,kdPole],[maxForceCart,maxForcePole], timeStep) |
| 80 | + p.setJointMotorControlArray(pole, [0,1], controlMode=p.TORQUE_CONTROL, forces=taus) |
| 81 | + |
40 | 82 | if (pd>=0):
|
41 |
| - desiredPosCart = p.readUserDebugParameter(desiredPosCartId) |
42 |
| - desiredVelCart = p.readUserDebugParameter(desiredVelCartId) |
43 |
| - kpCart = p.readUserDebugParameter(kpCartId) |
44 |
| - kdCart = p.readUserDebugParameter(kdCartId) |
45 |
| - maxForceCart = p.readUserDebugParameter(maxForceCartId) |
46 | 83 | link = 0
|
47 |
| - p.setJointMotorControl2(bodyUniqueId=pole,jointIndex=link,controlMode=p.PD_CONTROL,targetPosition=desiredPosCart,targetVelocity=desiredVelCart,force=maxForceCart, positionGain=kpCart, velocityGain=kdCart) |
48 |
| - |
49 |
| - desiredPosPole = p.readUserDebugParameter(desiredPosPoleId) |
50 |
| - desiredVelPole = p.readUserDebugParameter(desiredVelPoleId) |
51 |
| - kpPole = p.readUserDebugParameter(kpPoleId) |
52 |
| - kdPole = p.readUserDebugParameter(kdPoleId) |
53 |
| - maxForcePole = p.readUserDebugParameter(maxForcePoleId) |
| 84 | + p.setJointMotorControl2(bodyUniqueId=pole2,jointIndex=link,controlMode=p.PD_CONTROL,targetPosition=desiredPosCart,targetVelocity=desiredVelCart,force=maxForceCart, positionGain=kpCart, velocityGain=kdCart) |
54 | 85 | link = 1
|
55 |
| - p.setJointMotorControl2(bodyUniqueId=pole,jointIndex=link,controlMode=p.PD_CONTROL,targetPosition=desiredPosPole,targetVelocity=desiredVelPole,force=maxForcePole, positionGain=kpPole, velocityGain=kdPole) |
| 86 | + p.setJointMotorControl2(bodyUniqueId=pole2,jointIndex=link,controlMode=p.PD_CONTROL,targetPosition=desiredPosPole,targetVelocity=desiredVelPole,force=maxForcePole, positionGain=kpPole, velocityGain=kdPole) |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + taus = sPD.computePD(pole4, [0,1], [desiredPosCart,desiredPosPole],[desiredVelCart,desiredVelPole], [kpCart,kpPole], [kdCart,kdPole],[maxForceCart,maxForcePole], timeStep) |
| 92 | + p.setJointMotorControlArray(pole4, [0,1], controlMode=p.TORQUE_CONTROL, forces=taus) |
56 | 93 |
|
57 |
| - |
| 94 | + p.setJointMotorControl2(pole3,0, p.POSITION_CONTROL, targetPosition=desiredPosCart, targetVelocity=desiredVelCart, positionGain=timeStep*(kpCart/150.), velocityGain=0.5, force=maxForceCart) |
| 95 | + p.setJointMotorControl2(pole3,1, p.POSITION_CONTROL, targetPosition=desiredPosPole, targetVelocity=desiredVelPole, positionGain=timeStep*(kpPole/150.), velocityGain=0.5, force=maxForcePole) |
| 96 | + |
58 | 97 | if (not useRealTimeSim):
|
59 | 98 | p.stepSimulation()
|
60 |
| - time.sleep(1./240.) |
| 99 | + time.sleep(timeStep) |
61 | 100 |
|
62 | 101 |
|
0 commit comments