Skip to content

Interactive 2-DOF robotic arm simulator with real-time inverse kinematics, visualization, and math explanations—built with Python and Gradio.

Notifications You must be signed in to change notification settings

gokul6350/Interactive-2DOF-Arm-Sim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

title emoji colorFrom colorTo sdk sdk_version app_file tags thumbnail license pinned short_description
Interactive Arm Simulator
🚀
blue
green
gradio
5.36.2
app.py
robotics
inverse-kinematics
gradio
python
simulation
education
visualization
arm-simulator
interactive
STEM
mit
true
Interactive 2-DOF robotic arm simulator with real-time inver

Interactive Arm Simulator

Preview of Controls

A modern, interactive Gradio app for simulating the inverse kinematics of a 2-DOF (two-degree-of-freedom) robotic arm. Visualize, experiment, and learn the math behind robotic arm movement in real-time!


🚀 Features

  • Live Simulation: Adjust target coordinates (X, Y) and arm lengths (L1, L2) with sliders and see the arm move instantly.
  • Visual Feedback: Clear visualization of the arm, joints, and unreachable targets.
  • Angle Display: See calculated joint angles (shoulder and elbow) in both radians and degrees.
  • Math Explanation: Built-in accordion explains the inverse kinematics formulas.
  • Copyable Python Code: Easily grab the core function to use in your own projects.

🕹️ Controls Preview

Controls Screenshot Use the sliders to set the arm lengths and target position. The plot updates in real-time.


📦 Usage

  1. Install requirements:
    pip install gradio matplotlib numpy
  2. Run the app:
    python app.py
  3. Interact:
    • Move the sliders for X, Y, L1, and L2.
    • Watch the arm and joint angles update.
    • Use the "Copy the Core Python Function" dropdown for your own code.

🧮 How It Works: Inverse Kinematics

This app calculates the joint angles needed for a 2-link arm to reach a target point (x, y) using geometry:

1. Elbow Angle ($q_2$): Uses the Law of Cosines: $$ \cos(q_2) = \frac{x^2 + y^2 - L_1^2 - L_2^2}{2L_1L_2} $$ 2. Shoulder Angle ($q_1$): Combines the angle to the target and the triangle's internal angle: $$ q_1 = \alpha - \beta $$ Where:

  • $\alpha = \text{atan2}(y, x)$ (angle to target)
  • $\beta = \text{atan2}(L_2 \sin(q_2), L_1 + L_2 \cos(q_2))$ If the target is unreachable, the app shows a warning and marks it in red.

📝 Copy the Core Python Function

The app includes a dropdown with the following code for your use:

def inver_k(l1, l2, x, y):
    """
    Calculates the joint angles (q1, q2) for a 2-DOF robotic arm.
    Args:
        l1, l2: Lengths of the arm segments
        x, y: Target coordinates
    Returns:
        (success, q1, q2): Whether the point is reachable and the joint angles in radians
    """
    import math
    import numpy as np
    dist_sq = x**2 + y**2
    if dist_sq > (l1 + l2)**2 or dist_sq < (l1 - l2)**2:
        return (False, 0, 0)
    cos_q2 = (dist_sq - l1**2 - l2**2) / (2 * l1 * l2)
    cos_q2 = np.clip(cos_q2, -1.0, 1.0)
    q2 = math.acos(cos_q2)
    alpha = math.atan2(y, x)
    beta = math.atan2(l2 * math.sin(q2), l1 + l2 * math.cos(q2))
    q1 = alpha - beta
    return (True, q1, q2)

📚 Credits

  • Original inverse kinematics logic from ARMv6 by gokul6350
  • Adapted and extended as an interactive Gradio app for educational purposes.

License

MIT

About

Interactive 2-DOF robotic arm simulator with real-time inverse kinematics, visualization, and math explanations—built with Python and Gradio.

Topics

Resources

Stars

Watchers

Forks

Languages