Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
from mpl_toolkits.mplot3d import Axes3D
|
4 |
+
import gradio as gr
|
5 |
+
from scipy.optimize import minimize
|
6 |
+
|
7 |
+
# Define forward kinematics (simplified example)
|
8 |
+
def forward_kinematics(theta):
|
9 |
+
# Replace with actual FK for your robot
|
10 |
+
# Example: Simple 3D position calculation
|
11 |
+
x = np.cos(theta[0]) + np.cos(theta[1]) + np.cos(theta[2])
|
12 |
+
y = np.sin(theta[0]) + np.sin(theta[1]) + np.sin(theta[2])
|
13 |
+
z = theta[3] + theta[4] + theta[5]
|
14 |
+
return np.array([x, y, z])
|
15 |
+
|
16 |
+
# Define inverse kinematics using scipy.optimize
|
17 |
+
def inverse_kinematics(target_position, initial_angles):
|
18 |
+
def error_function(theta):
|
19 |
+
current_position = forward_kinematics(theta)
|
20 |
+
return np.linalg.norm(target_position - current_position)
|
21 |
+
|
22 |
+
# Use scipy.optimize to minimize the error
|
23 |
+
result = minimize(error_function, initial_angles, method='BFGS')
|
24 |
+
return result.x
|
25 |
+
|
26 |
+
# Generate trajectory (from initial to target position)
|
27 |
+
def generate_trajectory(target_position):
|
28 |
+
initial_angles = [0, 0, 0, 0, 0, 0] # Initial joint angles
|
29 |
+
target_angles = inverse_kinematics(target_position, initial_angles)
|
30 |
+
# Interpolate between initial and target angles
|
31 |
+
trajectory = np.linspace(initial_angles, target_angles, 100)
|
32 |
+
return trajectory
|
33 |
+
|
34 |
+
# Plot the trajectory in 3D
|
35 |
+
def plot_trajectory(trajectory):
|
36 |
+
x, y, z = [], [], []
|
37 |
+
for theta in trajectory:
|
38 |
+
position = forward_kinematics(theta)
|
39 |
+
x.append(position[0])
|
40 |
+
y.append(position[1])
|
41 |
+
z.append(position[2])
|
42 |
+
|
43 |
+
fig = plt.figure()
|
44 |
+
ax = fig.add_subplot(111, projection='3d')
|
45 |
+
ax.plot(x, y, z, label="Robot Trajectory")
|
46 |
+
ax.scatter(x[0], y[0], z[0], color='green', label="Start")
|
47 |
+
ax.scatter(x[-1], y[-1], z[-1], color='red', label="Target")
|
48 |
+
ax.set_xlabel("X")
|
49 |
+
ax.set_ylabel("Y")
|
50 |
+
ax.set_zlabel("Z")
|
51 |
+
ax.legend()
|
52 |
+
return fig
|
53 |
+
|
54 |
+
# Gradio Interface
|
55 |
+
def gradio_interface(x, y, z):
|
56 |
+
target_position = np.array([x, y, z])
|
57 |
+
|
58 |
+
# Generate trajectory
|
59 |
+
trajectory = generate_trajectory(target_position)
|
60 |
+
|
61 |
+
# Plot trajectory
|
62 |
+
fig = plot_trajectory(trajectory)
|
63 |
+
return fig
|
64 |
+
|
65 |
+
# Launch Gradio App
|
66 |
+
iface = gr.Interface(
|
67 |
+
fn=gradio_interface,
|
68 |
+
inputs=[
|
69 |
+
gr.Number(label="Target X"),
|
70 |
+
gr.Number(label="Target Y"),
|
71 |
+
gr.Number(label="Target Z")
|
72 |
+
],
|
73 |
+
outputs="plot",
|
74 |
+
live=False, # Disable live updates to show Submit button
|
75 |
+
title="6-DOF Robot Path Planning with Inverse Kinematics",
|
76 |
+
description="Enter the target (x, y, z) position and click Submit to generate the optimized trajectory."
|
77 |
+
)
|
78 |
+
|
79 |
+
iface.launch()
|