-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsim.py
165 lines (134 loc) · 5.81 KB
/
sim.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
#!/bin/python
# -*- coding: utf-8 -*-
import pybullet
import pybullet_data
import time
import numpy as np
import backup.Kinematics as K
import quad_ctrl as qc
import quad_ctrl_cycloid as qcc
# 四足仿真环境
class QuadrupedSim:
def __init__(self, control, config) -> None:
self.client = pybullet
self.client_data = pybullet_data
self.client.connect(self.client.GUI)
self.client.setAdditionalSearchPath(self.client_data.getDataPath())
# 设置为 0 以禁用隐式圆锥摩擦并使用金字塔近似值(默认为圆锥体)
self.client.setPhysicsEngineParameter(enableConeFriction=1)
self.client.setGravity(0, 0, -9.8)
#self.client.setGravity(0, 0, 0)
# 设置初始摄像机视角
#self.client.resetDebugVisualizerCamera(2, 135, -15, [0, 0, 1])
self.client.resetDebugVisualizerCamera(2.0, 0, 0, [0, 0, 0.4])
# 设置仿真步长
self.time_step = 1. / 240.
self.client.setTimeStep(self.time_step)
self.control = control
self.config = config
self.t = 0
# 状态复位
self.reset()
def exit(self):
quadrupedPos, quadrupedOrn = self.client.getBasePositionAndOrientation(
self.quadruped)
print(quadrupedPos, quadrupedOrn)
self.client.disconnect()
def reset(self):
self.plane = self.client.loadURDF('plane.urdf')
# 加载模型
quadrupedStartPos = [0, 0, .4]
# p.getQuaternionFromEuler([0,0,0])
quadrupedStartOrientation = [0, 0.5, 0.5, 0]
urdfFlags = self.client.URDF_USE_SELF_COLLISION
self.quadruped = self.client.loadURDF("mini_cheetah/mini_cheetah.urdf",
quadrupedStartPos,
# quadrupedStartOrientation,
# flags=urdfFlags,
useFixedBase=False)
# lower_legs = [2, 5, 8, 11]
# for l0 in lower_legs:
# for l1 in lower_legs:
# if (l1 > l0):
# enableCollision = 1
# print("collision for pair", l0, l1,
# self.client.getJointInfo(self.quadruped, l0)[12],
# self.client.getJointInfo(self.quadruped, l1)[12], "enabled=", enableCollision)
# self.client.setCollisionFilterPair(self.quadruped, self.quadruped, l0, l1, enableCollision)
# 记录joints 和 四足位置
self.joints_var = [[], [], [], []]
self.foot_pos = [[], [], [], []]
self.leg_sign = [-1, -1, 1, 1]
# 设置各部分颜色,便于区分
self.client.changeVisualShape(
self.quadruped, -1, rgbaColor=[1, 1, 1, 1])
color = [[1, 0, 0, 1], [1, 1, 0, 1], [0, 0, 1, 1], [1, 1, 0, 1],
[1, 0, 0, 1], [1, 1, 0, 1], [0, 0, 1, 1], [1, 1, 0, 1],
[1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1], [1, 1, 0, 1],
[1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1], [1, 1, 0, 1]]
# 打印关节信息
print("[INFO] !!!Joints info!!!")
for j in range(self.client.getNumJoints(self.quadruped)):
self.client.changeVisualShape(
self.quadruped, j, rgbaColor=color[j])
#self.client.changeDynamics(self.quadruped, j, linearDamping=0, angularDamping=0)
#print(self.client.getJointInfo(self.quadruped, j))
# joint的旋转轴方向
self.joint_directions = [[1, -1, -1], # LF
[1, -1, -1], # RF
[1, -1, -1], # RH
[1, -1, -1]] # LH
# 给joint编号
self.joint_ids = [[4, 5, 6], [0, 1, 2], [8, 9, 10], [12, 13, 14]]
self.joint_names = []
for j in range(self.client.getNumJoints(self.quadruped)):
self.joint_names.append(
self.client.getJointInfo(self.quadruped, j)[1])
# 初始化位姿
for i in range(4):
self.set_pos(i, self.control.param.init_angles[i*3],
self.control.param.init_angles[i*3+1],
self.control.param.init_angles[i*3+2])
def set_pos(self, leg_id, hip_angle, upper_angle, lower_angle):
pos = [hip_angle, upper_angle, lower_angle]
final_pos = np.array(self.joint_directions[leg_id]) * np.array(pos)
force = [20.] * len(self.joint_directions[leg_id])
self.client.setJointMotorControlArray(self.quadruped, self.joint_ids[leg_id],
self.client.POSITION_CONTROL,
final_pos, forces=force)
def step(self):
init_t = 0
# self.client.setRealTimeSimulation(1)
while init_t <= 120 * 1:
init_t += 1
self.client.stepSimulation()
time.sleep(self.time_step)
while not self.needExit():
self.run(self.time_step)
self.client.stepSimulation()
time.sleep(self.time_step)
def step_once(self, func):
t = 0
func(t)
while not self.needExit():
self.client.stepSimulation()
time.sleep(self.time_step)
def needExit(self):
qKey = ord('q')
keys = self.client.getKeyboardEvents()
if qKey in keys and keys[qKey] & self.client.KEY_IS_DOWN:
return True
else:
return False
def run(self, step):
pos = self.control.step_ctrl(self.t, self.config)
for i in range(4):
self.set_pos(i, pos[i*3], pos[i*3+1], pos[i*3+2])
self.t += step
if __name__ == "__main__":
#ctrl = qc.Quadfly()
param = K.QuadParam()
ctrl = qcc.QuadflyCycloid(param)
dog = QuadrupedSim(ctrl, ctrl.param.pace_config)
dog.step()
dog.exit()