-
Notifications
You must be signed in to change notification settings - Fork 185
机械臂的模型 #2604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
机械臂的模型 #2604
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
4b69818
update
xiaodai236 a336919
update
xiaodai236 7193c82
Merge branch 'OpenHUTB:main' into main
xiaodai236 9808e08
Merge branch 'OpenHUTB:main' into main
xiaodai236 abea7f9
update
xiaodai236 64a6a85
update
xiaodai236 551206e
Merge branch 'main' of https://github.com/xiaodai236/csx_nn
xiaodai236 b2c75d7
update
xiaodai236 243e95c
update
xiaodai236 d717db4
Merge branch 'OpenHUTB:main' into main
xiaodai236 aadade5
Merge branch 'OpenHUTB:main' into main
xiaodai236 c3f8881
update
xiaodai236 5fefdb9
Merge branch 'main' of https://github.com/xiaodai236/csx_nn
xiaodai236 27ee457
update
xiaodai236 9300624
update
xiaodai236 dbf6309
update
xiaodai236 edc016c
update
xiaodai236 4df5ab1
Merge branch 'OpenHUTB:main' into main
xiaodai236 39737e2
update
xiaodai236 f34a366
Merge branch 'main' of https://github.com/xiaodai236/csx_nn
xiaodai236 a7aef6a
update
xiaodai236 c150bbf
update
xiaodai236 a9f68af
update
xiaodai236 6448181
update
xiaodai236 aec50d9
update
xiaodai236 e085701
Merge branch 'OpenHUTB:main' into main
xiaodai236 c0417c9
update
xiaodai236 67c01d4
Merge branch 'OpenHUTB:main' into main
xiaodai236 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
人形机器人 | ||
|
||
直接运行main.py或main2.py | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import mujoco.viewer | ||
import ikpy.chain | ||
import transforms3d as tf | ||
import numpy as np | ||
from collections import deque | ||
import matplotlib.pyplot as plt | ||
|
||
|
||
def viewer_init(viewer): | ||
"""渲染器的摄像头视角初始化""" | ||
viewer.cam.type = mujoco.mjtCamera.mjCAMERA_FREE | ||
viewer.cam.lookat[:] = [0, 0.5, 0.5] | ||
viewer.cam.distance = 2.5 | ||
viewer.cam.azimuth = 180 | ||
viewer.cam.elevation = -30 | ||
|
||
|
||
class ForcePlotter: | ||
"""实时可视化接触力""" | ||
|
||
def __init__(self, update_interval=20): | ||
plt.ion() | ||
self.fig = plt.figure() | ||
self.ax = self.fig.add_subplot(111, projection='3d') | ||
self.update_interval = update_interval # 更新间隔帧数 | ||
self.frame_count = 0 # 帧计数器 | ||
|
||
def plot_force_vector(self, force_vector): | ||
self.frame_count += 1 | ||
if self.frame_count % self.update_interval != 0: | ||
return # 跳过本次渲染 | ||
|
||
self.ax.clear() | ||
|
||
origin = np.array([0, 0, 0]) | ||
force_magnitude = np.linalg.norm(force_vector) | ||
force_direction = force_vector / force_magnitude if force_magnitude > 1e-6 else np.zeros(3) | ||
|
||
# 主箭头 | ||
arrow_tip = force_direction * 1.5 | ||
self.ax.quiver(*origin, *arrow_tip, color='r', arrow_length_ratio=0) | ||
|
||
# 蓝色箭头 | ||
self.ax.quiver(*arrow_tip, *(0.5 * force_direction), color='b', arrow_length_ratio=0.5) | ||
|
||
# XY平面投影 | ||
self.ax.plot([0, arrow_tip[0]], [0, arrow_tip[1]], [-2, -2], 'g--') | ||
|
||
# XZ平面投影 | ||
self.ax.plot([0, 0], [2, 2], [0, arrow_tip[2]], 'm--') | ||
|
||
# 力大小指示条 | ||
scaled_force = min(max(force_magnitude / 50, 0), 2) | ||
self.ax.plot([-2, -2], [2, 2], [0, scaled_force], 'c-') | ||
self.ax.text(-2, 2, scaled_force, f'Force: {force_magnitude:.1f}', color='c') | ||
|
||
# 坐标系设置 | ||
self.ax.scatter(0, 0, 0, color='k', s=10) | ||
self.ax.set_xlim([-2, 2]) | ||
self.ax.set_ylim([-2, 2]) | ||
self.ax.set_zlim([-2, 2]) | ||
self.ax.set_title(f'Force Direction') | ||
|
||
plt.draw() | ||
plt.pause(0.001) | ||
self.frame_count = 0 # 重置计数器 | ||
|
||
|
||
class ForceSensor: | ||
def __init__(self, model, data, window_size=100): | ||
self.model = model | ||
self.data = data | ||
self.window_size = window_size | ||
self.force_history = deque(maxlen=window_size) | ||
|
||
def filter(self): | ||
"""获取并滑动平均滤波力传感器数据(传感器坐标系下)""" | ||
# 获取MjData中的传感器数据 | ||
force_local_raw = self.data.sensordata[:3].copy() * -1 | ||
|
||
# 添加新数据到滑动窗口 | ||
self.force_history.append(force_local_raw) | ||
|
||
# 计算滑动平均 | ||
filtered_force = np.mean(self.force_history, axis=0) | ||
|
||
return filtered_force | ||
|
||
|
||
class JointSpaceTrajectory: | ||
"""关节空间坐标系下的线性插值轨迹""" | ||
|
||
def __init__(self, start_joints, end_joints, steps): | ||
self.start_joints = np.array(start_joints) | ||
self.end_joints = np.array(end_joints) | ||
self.steps = steps | ||
self.step = (self.end_joints - self.start_joints) / self.steps | ||
self.trajectory = self._generate_trajectory() | ||
self.waypoint = self.start_joints | ||
|
||
def _generate_trajectory(self): | ||
for i in range(self.steps + 1): | ||
yield self.start_joints + self.step * i | ||
# 确保最后精确到达目标关节值 | ||
yield self.end_joints | ||
|
||
def get_next_waypoint(self, qpos): | ||
# 检查当前的关节值是否已经接近目标路径点。若是,则更新下一个目标路径点;若否,则保持当前目标路径点不变。 | ||
if np.allclose(qpos, self.waypoint, atol=0.02): | ||
try: | ||
self.waypoint = next(self.trajectory) | ||
return self.waypoint | ||
except StopIteration: | ||
pass | ||
return self.waypoint | ||
|
||
|
||
def main(): | ||
model = mujoco.MjModel.from_xml_path('model/universal_robots_ur5e/scene.xml') | ||
data = mujoco.MjData(model) | ||
my_chain = ikpy.chain.Chain.from_urdf_file("model/ur5e.urdf", | ||
active_links_mask=[False, False] + [True] * 6 + [False]) | ||
|
||
start_joints = np.array([-1.57, -1.34, 2.65, -1.3, 1.55, 0]) # 对应机械臂初始位姿[-0.13, 0.3, 0.1, 3.14, 0, 1.57] | ||
data.qpos[:6] = start_joints # 确保渲染一开始机械臂便处于起始位置,而非MJCF中的默认位置 | ||
|
||
# 设置目标点 | ||
ee_pos = [-0.13, 0.6, 0.1] | ||
ee_euler = [3.14, 0, 1.57] | ||
ref_pos = [0, 0, -1.57, -1.34, 2.65, -1.3, 1.55, 0, 0] | ||
ee_orientation = tf.euler.euler2mat(*ee_euler) | ||
|
||
joint_angles = my_chain.inverse_kinematics(ee_pos, ee_orientation, "all", initial_position=ref_pos) | ||
end_joints = joint_angles[2:-1] | ||
|
||
joint_trajectory = JointSpaceTrajectory(start_joints, end_joints, steps=100) | ||
|
||
force_sensor = ForceSensor(model, data) | ||
force_plotter = ForcePlotter() | ||
|
||
with mujoco.viewer.launch_passive(model, data) as viewer: | ||
viewer_init(viewer) | ||
while viewer.is_running(): | ||
waypoint = joint_trajectory.get_next_waypoint(data.qpos[:6]) | ||
data.ctrl[:6] = waypoint | ||
|
||
filtered_force = force_sensor.filter() | ||
force_plotter.plot_force_vector(filtered_force) | ||
|
||
mujoco.mj_step(model, data) | ||
viewer.sync() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import mujoco.viewer | ||
|
||
def main(): | ||
model = mujoco.MjModel.from_xml_path('model/universal_robots_ur5e/scene.xml') | ||
data = mujoco.MjData(model) | ||
|
||
with mujoco.viewer.launch_passive(model, data) as viewer: | ||
while viewer.is_running(): | ||
mujoco.mj_step(model, data) | ||
viewer.sync() | ||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
一般只有一个入口主函数就行(实在没办法缩也要说明分别是做什么的)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
增加了机器人模拟搬砖路径的算法