Skip to content

An interactive web application with a user-friendly (I hope) interface for visualizing the optimization procedure while calculating the inverse kinematics of a robot arm on a 2D plane using gradient descent.

License

Notifications You must be signed in to change notification settings

mrodiduger/inverse-kinematics-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

21 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Inverse Kinematics via Gradien Descent Optimization

INVERSE-KINEMATICS-APP

license last-commit repo-top-language repo-language-count

Developed with the software and tools below.

JavaScript Pydantic HTML5 Jinja Bootstrap Vite
Vue.js SymPy Python NumPy JSON Flask


πŸ”— Quick Links


Some Thoughts

Well, this repository is the kinda-final form of my hobby project. I first got the idea from a visualization by Matt Henderson a while ago, and recently decided to implement it to expose myself to web development a bit. I also deployed the application to Heroku but don't want to pay for more resources, so I hope no more than a few users try to use it at the same time :D You can use it locally by following the installation steps, though :) I think it might be a good educational resource for some instructors to use in lectures.

πŸ“ Overview

This web application aims to visualize inverse kinematics via gradient descent optimization.

Inverse Kinematics

Inverse kinematics (IK) deals with finding the joint parameters (angles) that provide a desired position of the robot's end effector. For a robot arm in a 2D plane, this involves calculating the angles of its joints such that the end of the arm reaches a specific point $(x, y)$ in the plane.

Kinematic Equations

For a simple 2-joint (2-link) robot arm in a 2D plane, the forward kinematics can be expressed as:

$$x = l_1 cos(ΞΈ_1) + l_2 cos(ΞΈ_1 + ΞΈ_2)$$

$$y = l_1 sin(ΞΈ_1) + l_2 sin(ΞΈ_1 + ΞΈ_2)$$

Where:

$l_1$ and $l_2$ are the lengths of the robot arm's links. $ΞΈ_1$ and $ΞΈ_2$ are the angles of the joints. The goal of inverse kinematics is to find the angles $ΞΈ_1$ and $ΞΈ_2$ given the desired $(x, y)$ position.

Optimization via Gradient Descent

Gradient descent is an optimization algorithm used to minimize a function by iteratively moving towards the steepest descent, i.e., the negative gradient of the function. In the context of inverse kinematics, the function we want to minimize is the error between the current position of the end effector and the target position.

Error Function

The error function E can be defined as the squared Euclidean distance between the current end effector position $(x_current, y_current)$ and the target position $(x_target, y_target)$:

$$E(ΞΈ_1, ΞΈ_2) = 1/2 [(x_current - x_target)^2 + (y_current - y_target)^2]$$

In the application, you can select various error functions and observe the effect of different error functions on the optimization process

Gradient Calculation

To minimize the error E, we need to compute its gradients with respect to the joint angles $ΞΈ_1$ and $ΞΈ_2$:

$$βˆ‚E/βˆ‚ΞΈ_1$$

$$βˆ‚E/βˆ‚ΞΈ_2$$

Using the chain rule and the kinematic equations, these gradients can be derived.

We compute the gradients by defining joint angles as parameters and calculating the end effector position as a function of joint angles to leverage automatic differentiation of PyTorch.

Iterative Update

With the gradients computed, the joint angles are updated iteratively using the gradient descent rule:

$$ΞΈ_1 ← ΞΈ_1 - Ξ± βˆ‚E/βˆ‚ΞΈ_1$$

$$ΞΈ_2 ← ΞΈ_2 - Ξ± βˆ‚E/βˆ‚ΞΈ_2$$

Here, $Ξ±$ is the learning rate, a small positive value that controls the step size of the update.

Convergence

The iterative process continues until the error E is minimized to an acceptable level, meaning the end effector is sufficiently close to the target position.

Summary

Inverse kinematics via gradient descent optimization involves:

Defining the kinematic equations for the robot arm. Setting up an error function representing the distance between the current and target positions. Calculating the gradients of the error function with respect to the joint angles. Iteratively updating the joint angles using gradient descent to minimize the error.


πŸ“‚ Repository Structure

└── inverse-kinematics-app/
    β”œβ”€β”€ Procfile
    β”œβ”€β”€ README.md
    β”œβ”€β”€ app.py #main logic for optimization
    β”œβ”€β”€ client
    β”‚   β”œβ”€β”€ .gitignore
    β”‚   β”œβ”€β”€ .vscode
    β”‚   β”‚   └── extensions.json
    β”‚   β”œβ”€β”€ README.md
    β”‚   β”œβ”€β”€ __init__.py
    β”‚   β”œβ”€β”€ dist
    β”‚   β”‚   β”œβ”€β”€ assets
    β”‚   β”‚   β”‚   β”œβ”€β”€ index-Bw-pKMAr.js
    β”‚   β”‚   β”‚   └── index-CV_YbN5t.css
    β”‚   β”‚   β”œβ”€β”€ favicon.ico
    β”‚   β”‚   └── index.html
    β”‚   β”œβ”€β”€ index.html
    β”‚   β”œβ”€β”€ jsconfig.json
    β”‚   β”œβ”€β”€ package-lock.json
    β”‚   β”œβ”€β”€ package.json
    β”‚   β”œβ”€β”€ public
    β”‚   β”‚   └── favicon.ico
    β”‚   β”œβ”€β”€ src
    β”‚   β”‚   β”œβ”€β”€ App.vue
    β”‚   β”‚   β”œβ”€β”€ assets
    β”‚   β”‚   β”‚   β”œβ”€β”€ base.css
    β”‚   β”‚   β”‚   β”œβ”€β”€ logo.svg
    β”‚   β”‚   β”‚   └── main.css
    β”‚   β”‚   β”œβ”€β”€ components
    β”‚   β”‚   β”‚   β”œβ”€β”€ Config.vue
    β”‚   β”‚   β”‚   └── Visualizer.vue #main logic for visualization
    β”‚   β”‚   β”œβ”€β”€ main.js
    β”‚   β”‚   └── router
    β”‚   β”‚       └── index.js
    β”‚   └── vite.config.js
    β”œβ”€β”€ requirements.txt
    └── runtime.txt

πŸš€ Getting Started

βš™οΈ Installation

  1. Clone the inverse-kinematics-app repository:
git clone https://github.com/mrodiduger/inverse-kinematics-app
  1. Change to the project directory:
cd inverse-kinematics-app
  1. Create a virtual environment and install dependecies:
python3 -m venv -venv
source .venv/bin/activate
python3 -m pip install --upgrade pip
python3 -m pip install -r requirements.txt

πŸ€– Running inverse-kinematics-app

Use the following command to run inverse-kinematics-app:

flask run

πŸ“„ License

This project is protected under the MIT License.


About

An interactive web application with a user-friendly (I hope) interface for visualizing the optimization procedure while calculating the inverse kinematics of a robot arm on a 2D plane using gradient descent.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published