Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,6 @@ L = np.array([1, 1, 1, 1, 1, 1]) # Lengths of each arm segment
|
|
10 |
# Forward kinematics: Compute the end-effector position given joint angles
|
11 |
def forward_kinematics(theta):
|
12 |
theta = np.array(theta)
|
13 |
-
# Compute x, y, z coordinates for each joint
|
14 |
x = L[0] * np.cos(theta[0]) + L[1] * np.cos(theta[0] + theta[1]) + L[2] * np.cos(theta[0] + theta[1] + theta[2]) + \
|
15 |
L[3] * np.cos(theta[0] + theta[1] + theta[2] + theta[3]) + L[4] * np.cos(theta[0] + theta[1] + theta[2] + theta[3] + theta[4]) + \
|
16 |
L[5] * np.cos(theta[0] + theta[1] + theta[2] + theta[3] + theta[4] + theta[5])
|
@@ -45,4 +44,58 @@ def robot_pathfinder(target_x, target_y, target_z):
|
|
45 |
x = joint_positions[-1][0] + L[i] * np.cos(np.sum(optimal_angles[:i+1]))
|
46 |
y = joint_positions[-1][1] + L[i] * np.sin(np.sum(optimal_angles[:i+1]))
|
47 |
z = joint_positions[-1][2] + L[i] * np.sin(np.sum(optimal_angles[:i+1]))
|
48 |
-
joint_positions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
# Forward kinematics: Compute the end-effector position given joint angles
|
11 |
def forward_kinematics(theta):
|
12 |
theta = np.array(theta)
|
|
|
13 |
x = L[0] * np.cos(theta[0]) + L[1] * np.cos(theta[0] + theta[1]) + L[2] * np.cos(theta[0] + theta[1] + theta[2]) + \
|
14 |
L[3] * np.cos(theta[0] + theta[1] + theta[2] + theta[3]) + L[4] * np.cos(theta[0] + theta[1] + theta[2] + theta[3] + theta[4]) + \
|
15 |
L[5] * np.cos(theta[0] + theta[1] + theta[2] + theta[3] + theta[4] + theta[5])
|
|
|
44 |
x = joint_positions[-1][0] + L[i] * np.cos(np.sum(optimal_angles[:i+1]))
|
45 |
y = joint_positions[-1][1] + L[i] * np.sin(np.sum(optimal_angles[:i+1]))
|
46 |
z = joint_positions[-1][2] + L[i] * np.sin(np.sum(optimal_angles[:i+1]))
|
47 |
+
joint_positions.append([x, y, z])
|
48 |
+
|
49 |
+
# Create 3D plot using Plotly
|
50 |
+
fig = go.Figure()
|
51 |
+
|
52 |
+
# Add robot arm segments
|
53 |
+
for i in range(len(joint_positions) - 1):
|
54 |
+
fig.add_trace(go.Scatter3d(
|
55 |
+
x=[joint_positions[i][0], joint_positions[i+1][0]],
|
56 |
+
y=[joint_positions[i][1], joint_positions[i+1][1]],
|
57 |
+
z=[joint_positions[i][2], joint_positions[i+1][2]],
|
58 |
+
mode='lines+markers',
|
59 |
+
line=dict(color='blue', width=5),
|
60 |
+
marker=dict(size=5)
|
61 |
+
))
|
62 |
+
|
63 |
+
# Add target point
|
64 |
+
fig.add_trace(go.Scatter3d(
|
65 |
+
x=[target[0]],
|
66 |
+
y=[target[1]],
|
67 |
+
z=[target[2]],
|
68 |
+
mode='markers',
|
69 |
+
marker=dict(color='red', size=10)
|
70 |
+
))
|
71 |
+
|
72 |
+
# Set layout
|
73 |
+
fig.update_layout(
|
74 |
+
scene=dict(
|
75 |
+
xaxis=dict(range=[-5, 5]),
|
76 |
+
yaxis=dict(range=[-5, 5]),
|
77 |
+
zaxis=dict(range=[-5, 5]),
|
78 |
+
aspectmode='cube'
|
79 |
+
),
|
80 |
+
title="6-DOF Robot Arm Pathfinding in 3D"
|
81 |
+
)
|
82 |
+
|
83 |
+
return fig
|
84 |
+
|
85 |
+
# Gradio app
|
86 |
+
iface = gr.Interface(
|
87 |
+
fn=robot_pathfinder,
|
88 |
+
inputs=[
|
89 |
+
gr.Number(label="Target X-coordinate"),
|
90 |
+
gr.Number(label="Target Y-coordinate"),
|
91 |
+
gr.Number(label="Target Z-coordinate")
|
92 |
+
],
|
93 |
+
outputs=gr.Plot(), # Use Plotly for 3D visualization
|
94 |
+
live=True,
|
95 |
+
title="6-DOF Robot Shortest Path Finder in 3D",
|
96 |
+
description="Enter target coordinates (x, y, z) to find the shortest path for a 6-DOF robot arm in 3D space."
|
97 |
+
)
|
98 |
+
|
99 |
+
# Launch the app
|
100 |
+
if __name__ == "__main__":
|
101 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|