Fouzanjaved commited on
Commit
803f98a
·
verified ·
1 Parent(s): 2dd2cad

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -0
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Install required libraries
2
+ !pip install numpy matplotlib gradio scipy
3
+
4
+ import numpy as np
5
+ import matplotlib.pyplot as plt
6
+ from mpl_toolkits.mplot3d import Axes3D
7
+ import gradio as gr
8
+ from scipy.optimize import minimize
9
+
10
+ # Define forward kinematics (simplified example)
11
+ def forward_kinematics(theta):
12
+ # Replace with actual FK for your robot
13
+ # Example: Simple 3D position calculation
14
+ x = np.cos(theta[0]) + np.cos(theta[1]) + np.cos(theta[2])
15
+ y = np.sin(theta[0]) + np.sin(theta[1]) + np.sin(theta[2])
16
+ z = theta[3] + theta[4] + theta[5]
17
+ return np.array([x, y, z])
18
+
19
+ # Define obstacle positions (example)
20
+ obstacles = [
21
+ {"position": np.array([0.5, 0.5, 0.5]), "radius": 0.2},
22
+ {"position": np.array([1.0, 1.0, 1.0]), "radius": 0.3}
23
+ ]
24
+
25
+ # Check for collisions with obstacles
26
+ def is_collision(position):
27
+ for obstacle in obstacles:
28
+ obstacle_position = obstacle["position"]
29
+ obstacle_radius = obstacle["radius"]
30
+ distance = np.linalg.norm(position - obstacle_position)
31
+ if distance < obstacle_radius:
32
+ return True # Collision detected
33
+ return False # No collision
34
+
35
+ # Define inverse kinematics with obstacle avoidance
36
+ def inverse_kinematics(target_position, initial_angles):
37
+ def error_function(theta):
38
+ current_position = forward_kinematics(theta)
39
+ # Attractive force to target
40
+ error = np.linalg.norm(target_position - current_position)
41
+ # Add penalty for collisions
42
+ if is_collision(current_position):
43
+ error += 1000 # Large penalty for collisions
44
+ return error
45
+
46
+ # Use scipy.optimize to minimize the error
47
+ result = minimize(error_function, initial_angles, method='BFGS')
48
+ return result.x
49
+
50
+ # Generate trajectory (from initial to target position)
51
+ def generate_trajectory(target_position):
52
+ initial_angles = [0, 0, 0, 0, 0, 0] # Initial joint angles
53
+ target_angles = inverse_kinematics(target_position, initial_angles)
54
+ # Interpolate between initial and target angles (fewer points for faster processing)
55
+ trajectory = np.linspace(initial_angles, target_angles, 50) # Reduced to 50 points
56
+ return trajectory
57
+
58
+ # Plot the trajectory in 3D with obstacles
59
+ def plot_trajectory(trajectory):
60
+ x, y, z = [], [], []
61
+ for theta in trajectory:
62
+ position = forward_kinematics(theta)
63
+ x.append(position[0])
64
+ y.append(position[1])
65
+ z.append(position[2])
66
+
67
+ fig = plt.figure()
68
+ ax = fig.add_subplot(111, projection='3d')
69
+ ax.plot(x, y, z, label="Robot Trajectory")
70
+ ax.scatter(x[0], y[0], z[0], color='green', label="Start")
71
+ ax.scatter(x[-1], y[-1], z[-1], color='red', label="Target")
72
+
73
+ # Plot obstacles
74
+ for obstacle in obstacles:
75
+ obstacle_position = obstacle["position"]
76
+ obstacle_radius = obstacle["radius"]
77
+ u, v = np.mgrid[0:2*np.pi:20j, 0:np.pi:10j]
78
+ x_obs = obstacle_position[0] + obstacle_radius * np.cos(u) * np.sin(v)
79
+ y_obs = obstacle_position[1] + obstacle_radius * np.sin(u) * np.sin(v)
80
+ z_obs = obstacle_position[2] + obstacle_radius * np.cos(v)
81
+ ax.plot_wireframe(x_obs, y_obs, z_obs, color='orange', alpha=0.5, label="Obstacle" if obstacle == obstacles[0] else "")
82
+
83
+ ax.set_xlabel("X")
84
+ ax.set_ylabel("Y")
85
+ ax.set_zlabel("Z")
86
+ ax.legend()
87
+ return fig
88
+
89
+ # Gradio Interface
90
+ def gradio_interface(x, y, z):
91
+ target_position = np.array([x, y, z])
92
+
93
+ # Generate trajectory
94
+ trajectory = generate_trajectory(target_position)
95
+
96
+ # Plot trajectory
97
+ fig = plot_trajectory(trajectory)
98
+ return fig
99
+
100
+ # Launch Gradio App
101
+ iface = gr.Interface(
102
+ fn=gradio_interface,
103
+ inputs=[
104
+ gr.Number(label="Target X"),
105
+ gr.Number(label="Target Y"),
106
+ gr.Number(label="Target Z")
107
+ ],
108
+ outputs="plot",
109
+ live=False, # Disable live updates to show Submit button
110
+ title="6-DOF Robot Path Planning with Obstacle Avoidance",
111
+ description="Enter the target (x, y, z) position and click Submit to generate the optimized trajectory."
112
+ )
113
+
114
+ iface.launch()