Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import numpy as np
|
| 2 |
-
import
|
| 3 |
import gradio as gr
|
| 4 |
from scipy.spatial.distance import euclidean
|
| 5 |
from scipy.optimize import minimize
|
|
@@ -10,13 +10,17 @@ 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 |
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])
|
| 16 |
y = L[0] * np.sin(theta[0]) + L[1] * np.sin(theta[0] + theta[1]) + L[2] * np.sin(theta[0] + theta[1] + theta[2]) + \
|
| 17 |
L[3] * np.sin(theta[0] + theta[1] + theta[2] + theta[3]) + L[4] * np.sin(theta[0] + theta[1] + theta[2] + theta[3] + theta[4]) + \
|
| 18 |
L[5] * np.sin(theta[0] + theta[1] + theta[2] + theta[3] + theta[4] + theta[5])
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Objective function: Minimize the distance between the end-effector and the target
|
| 22 |
def objective_function(theta, target):
|
|
@@ -29,39 +33,16 @@ def find_shortest_path(initial_angles, target):
|
|
| 29 |
return result.x
|
| 30 |
|
| 31 |
# Gradio interface
|
| 32 |
-
def robot_pathfinder(target_x, target_y):
|
| 33 |
-
target = np.array([target_x, target_y])
|
| 34 |
initial_angles = np.array([0, 0, 0, 0, 0, 0]) # Initial joint angles
|
| 35 |
optimal_angles = find_shortest_path(initial_angles, target)
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
ax.set_ylim(-5, 5)
|
| 41 |
-
ax.set_aspect('equal')
|
| 42 |
-
|
| 43 |
-
# Draw the robot arm
|
| 44 |
-
x = [0]
|
| 45 |
-
y = [0]
|
| 46 |
for i in range(len(L)):
|
| 47 |
-
x
|
| 48 |
-
y
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# Mark the target
|
| 52 |
-
ax.plot(target[0], target[1], 'rx', markersize=10)
|
| 53 |
-
|
| 54 |
-
plt.title("6-DOF Robot Arm Pathfinding")
|
| 55 |
-
return fig
|
| 56 |
-
|
| 57 |
-
# Gradio app
|
| 58 |
-
iface = gr.Interface(
|
| 59 |
-
fn=robot_pathfinder,
|
| 60 |
-
inputs=["number", "number"],
|
| 61 |
-
outputs="plot",
|
| 62 |
-
live=True,
|
| 63 |
-
title="6-DOF Robot Shortest Path Finder",
|
| 64 |
-
description="Enter target coordinates (x, y) to find the shortest path for a 6-DOF robot arm."
|
| 65 |
-
)
|
| 66 |
-
|
| 67 |
-
iface.launch()
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
+
import plotly.graph_objects as go
|
| 3 |
import gradio as gr
|
| 4 |
from scipy.spatial.distance import euclidean
|
| 5 |
from scipy.optimize import minimize
|
|
|
|
| 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])
|
| 17 |
y = L[0] * np.sin(theta[0]) + L[1] * np.sin(theta[0] + theta[1]) + L[2] * np.sin(theta[0] + theta[1] + theta[2]) + \
|
| 18 |
L[3] * np.sin(theta[0] + theta[1] + theta[2] + theta[3]) + L[4] * np.sin(theta[0] + theta[1] + theta[2] + theta[3] + theta[4]) + \
|
| 19 |
L[5] * np.sin(theta[0] + theta[1] + theta[2] + theta[3] + theta[4] + theta[5])
|
| 20 |
+
z = L[0] * np.sin(theta[0]) + L[1] * np.sin(theta[0] + theta[1]) + L[2] * np.sin(theta[0] + theta[1] + theta[2]) + \
|
| 21 |
+
L[3] * np.sin(theta[0] + theta[1] + theta[2] + theta[3]) + L[4] * np.sin(theta[0] + theta[1] + theta[2] + theta[3] + theta[4]) + \
|
| 22 |
+
L[5] * np.sin(theta[0] + theta[1] + theta[2] + theta[3] + theta[4] + theta[5])
|
| 23 |
+
return np.array([x, y, z])
|
| 24 |
|
| 25 |
# Objective function: Minimize the distance between the end-effector and the target
|
| 26 |
def objective_function(theta, target):
|
|
|
|
| 33 |
return result.x
|
| 34 |
|
| 35 |
# Gradio interface
|
| 36 |
+
def robot_pathfinder(target_x, target_y, target_z):
|
| 37 |
+
target = np.array([target_x, target_y, target_z])
|
| 38 |
initial_angles = np.array([0, 0, 0, 0, 0, 0]) # Initial joint angles
|
| 39 |
optimal_angles = find_shortest_path(initial_angles, target)
|
| 40 |
|
| 41 |
+
# Compute joint positions for visualization
|
| 42 |
+
joint_positions = []
|
| 43 |
+
joint_positions.append([0, 0, 0]) # Base position
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
for i in range(len(L)):
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|