Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 sklearn.neural_network import MLPRegressor
|
| 6 |
+
from sklearn.model_selection import train_test_split
|
| 7 |
+
from sklearn.metrics import mean_squared_error
|
| 8 |
+
|
| 9 |
+
# -------------------------------
|
| 10 |
+
# Step 1: Generate synthetic data
|
| 11 |
+
# -------------------------------
|
| 12 |
+
def forward_kinematics(theta):
|
| 13 |
+
x = np.cos(theta[0]) + np.cos(theta[1]) + np.cos(theta[2])
|
| 14 |
+
y = np.sin(theta[0]) + np.sin(theta[1]) + np.sin(theta[2])
|
| 15 |
+
z = theta[3] + theta[4] + theta[5]
|
| 16 |
+
return np.array([x, y, z])
|
| 17 |
+
|
| 18 |
+
# Generate dataset
|
| 19 |
+
def create_dataset(num_samples=5000):
|
| 20 |
+
X = []
|
| 21 |
+
y = []
|
| 22 |
+
for _ in range(num_samples):
|
| 23 |
+
angles = np.random.uniform(low=-np.pi, high=np.pi, size=6)
|
| 24 |
+
position = forward_kinematics(angles)
|
| 25 |
+
X.append(position)
|
| 26 |
+
y.append(angles)
|
| 27 |
+
return np.array(X), np.array(y)
|
| 28 |
+
|
| 29 |
+
# Generate and train
|
| 30 |
+
X, y = create_dataset()
|
| 31 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
|
| 32 |
+
|
| 33 |
+
model = MLPRegressor(hidden_layer_sizes=(64, 64), max_iter=1000)
|
| 34 |
+
model.fit(X_train, y_train)
|
| 35 |
+
|
| 36 |
+
# Optional: Evaluate model
|
| 37 |
+
mse = mean_squared_error(y_test, model.predict(X_test))
|
| 38 |
+
print(f"Test MSE: {mse:.4f}")
|
| 39 |
+
|
| 40 |
+
# -------------------------------
|
| 41 |
+
# Step 2: Use ML to predict angles
|
| 42 |
+
# -------------------------------
|
| 43 |
+
def predict_angles_with_ml(target_position):
|
| 44 |
+
return model.predict([target_position])[0]
|
| 45 |
+
|
| 46 |
+
def generate_trajectory(target_position):
|
| 47 |
+
initial_angles = [0, 0, 0, 0, 0, 0]
|
| 48 |
+
target_angles = predict_angles_with_ml(target_position)
|
| 49 |
+
trajectory = np.linspace(initial_angles, target_angles, 100)
|
| 50 |
+
return trajectory
|
| 51 |
+
|
| 52 |
+
def plot_trajectory(trajectory):
|
| 53 |
+
x, y, z = [], [], []
|
| 54 |
+
for theta in trajectory:
|
| 55 |
+
pos = forward_kinematics(theta)
|
| 56 |
+
x.append(pos[0])
|
| 57 |
+
y.append(pos[1])
|
| 58 |
+
z.append(pos[2])
|
| 59 |
+
|
| 60 |
+
fig = plt.figure()
|
| 61 |
+
ax = fig.add_subplot(111, projection='3d')
|
| 62 |
+
ax.plot(x, y, z, label="Robot Trajectory")
|
| 63 |
+
ax.scatter(x[0], y[0], z[0], color='green', label="Start")
|
| 64 |
+
ax.scatter(x[-1], y[-1], z[-1], color='red', label="Target")
|
| 65 |
+
ax.set_xlabel("X")
|
| 66 |
+
ax.set_ylabel("Y")
|
| 67 |
+
ax.set_zlabel("Z")
|
| 68 |
+
ax.legend()
|
| 69 |
+
return fig
|
| 70 |
+
|
| 71 |
+
# -------------------------------
|
| 72 |
+
# Step 3: Gradio Interface
|
| 73 |
+
# -------------------------------
|
| 74 |
+
def gradio_interface(x, y, z):
|
| 75 |
+
target_position = np.array([x, y, z])
|
| 76 |
+
trajectory = generate_trajectory(target_position)
|
| 77 |
+
fig = plot_trajectory(trajectory)
|
| 78 |
+
return fig
|
| 79 |
+
|
| 80 |
+
iface = gr.Interface(
|
| 81 |
+
fn=gradio_interface,
|
| 82 |
+
inputs=[gr.Number(label="Target X"), gr.Number(label="Target Y"), gr.Number(label="Target Z")],
|
| 83 |
+
outputs="plot",
|
| 84 |
+
title="ML-Based Inverse Kinematics for 6-DOF Robot",
|
| 85 |
+
description="Neural network predicts joint angles for a given (x, y, z) target using learned inverse kinematics."
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
iface.launch()
|