<commit_before>--- 
layout: post
title:  "Differential kinematics and inverse kinematics"
date:   2015-12-11
---

This learning module covers the relationship between movement in the 
generalized coordinates
and corresponding movement at a designated point on the robot. You may find
it helpful to consider an industrial robot, in which case you can substitute
the term "joint positions" or "joint angles" for "generalized coordinates and
the term "joint velocities" for "generalized velocities", 
rather than more general robots (like humanoids). 

### Analytical inverse kinematics

Recall that the forward kinematics equation is:

\begin{equation}
\mathbf{x} = \mathbf{f}^p(\mathbf{q})
\end{equation}

where \\(p\\) is a frame attached to a rigid link of the robot,
\\(\mathbf{q}\\) are the \\(n\\) generalized coordinates of the robot, and \\(\mathbf{x}\\) is the pose of \\(p\\) in another frame (typically the world frame). If \\(\mathbf{x}\\) is represented by \\(m\\) real numbers, then \\(\mathbf{f} : \mathbb{R}^n \to \mathbb{R}^m \\).

**We will use \\(p\\) throughout this learning module to refer to
a pose attached to a rigid link on the robot.** 

{% include image.html url="../../assets/img/frame-p-on-robot.png" description="Depiction of Frame p, which is defined with respect to one of the robot's links. While forward kinematics seeks to determine how p is positioned and oriented with respect to the global frame, differential kinematics seek to determine how quickly p changes as a function of the robot's current configuration." %} 

#### Inverting \\(\mathbf{f}(.)\\)

Inverting \\(\mathbf{f}(.)\\) means determining \\(\mathbf{f}^{-1}(\mathbf{x}) = \mathbf{q}\\), where \\(\mathbf{x}\\) is one of the possible outputs of the forward kinematics function (SO(2), SO(3), \\(\mathbb{R}^2, \mathbb{R}^3\\), SE(2), SE(3)). This inversion is generally challenging because \\(\mathbf{f}(.)\\) is a nonlinear function. Additional challenges are that:

* \\(\mathbf{f}^{-1}(\mathbf{x})\\) may have no solutions
* \\(\mathbf{f}^{-1}(\mathbf{x}\\)) may have multiple solutions
* Computation of \\(\mathbf{f}^{-1}(.)\\) generally recommends the use of a symbolic mathematics package (like [Macsyma](http://maxima.sourceforge.net)), which means that changing the kinematics of the robot means re-derivation of the inverse kinematics function

Analytical inverse kinematics requires that the number of constraints be
equal to the robot's number of generalized coordinates. Artificial constraints
may be introduced to satisfy this requirement. For example, a "typical"
anthropomorphic arm provides seven degrees of freedom, while controlling
position and orientation in 3D presents six constraints. Analytical inverse
kinematics solutions for such arms typically leave the elbow position as
an open parameter for the user to adjust, reducing the controllable degrees
of freedom of the arm to six. 

{% include image.html url="../../assets/img/ik.png" description="An example of analytical inverse kinematics, using a two link planar arm. Image cribbed from Stefan Schaal's course notes." %}
 
### Differential kinematics

Another way to do inverse kinematics is using _differential kinematics_. Differential kinematics examines the change in \\(\mathbf{x}\\) given small
changes in \\(\mathbf{q}\\). [Recall that \\(\mathbf{x}\\) can represent position and/or orientation in 2D or 3D](../forward-kinematics), so the aforementioned "small changes" refer to small changes in position and/or orientation.

\begin{equation}
\dot{\mathbf{x}} = \begin{bmatrix} 
\frac{\partial f\_1}{\partial q\_1} & \ldots & \frac{\partial f\_1}{\partial q\_n} \\\\
& \vdots & \\\\
\frac{\partial f\_m}{\partial q\_1} & \ldots & \frac{\partial f\_m}{\partial q\_n}
\end{bmatrix}
\begin{bmatrix}
\dot{q}\_1 \\\\
\vdots \\\\
\dot{q}\_n
\end{bmatrix} \label{eqn:Jacobians-full}
\end{equation}

The first matrix is the _Jacobian of pose \\(\mathbf{p}\\) with respect to
the generalized configuration_. Refer back to the [material on linear algebra](../linear-algebra/) if you need a refresher on Jacobian matrices. The second matrix is just the generalized
velocity, \\(\dot{\mathbf{q}}\\). So, we can write Equation \ref{eqn:Jacobians-full} as:
\begin{equation}
\dot{\mathbf{x}} = \mathbf{J}\dot{\mathbf{q}} \label{eqn:Jacobians}
\end{equation}
_You will frequently see this equation in robotic manipulation_.

#### An example: analytically computing the Jacobian of a double pendulum

Consider the [double pendulum from the learning module on forward kinematics](../forward-kinematics). At this point in time, assume that we wish only to
compute the Jacobian matrix of the double pendulum's endpoint. Stated another way, \\(\mathbf{f}(\mathbf{q}) \to \mathbb{R}^2\\) and is defined as:
\begin{equation}
\begin{bmatrix}
l\_1 c\_1 + l\_2 c\_{1+2} \\\\
l\_1 s\_1 + l\_2 s\_{1+2}
\end{bmatrix}
\label{eqn:fkin-example}
\end{equation}

**Before reading further, make sure you know (a) the dimension of \\(\mathbf{q}\\), (b) the dimension of \\(\mathbf{f}(.)\\), and the dimension of the Jacobian matrix that will be produced.** 

The Jacobian matrix for Equation \ref{eqn:fkin-example} will be:
\begin{equation}
\begin{bmatrix}<commit_msg>Fixed newlines in differential kinematics<commit_after>--- 
layout: post
title:  "Differential kinematics and inverse kinematics"
date:   2015-12-11
---

This learning module covers the relationship between movement in the 
generalized coordinates
and corresponding movement at a designated point on the robot. You may find
it helpful to consider an industrial robot, in which case you can substitute
the term "joint positions" or "joint angles" for "generalized coordinates and
the term "joint velocities" for "generalized velocities", 
rather than more general robots (like humanoids). 

### Analytical inverse kinematics

Recall that the forward kinematics equation is:

\begin{equation}
\mathbf{x} = \mathbf{f}^p(\mathbf{q})
\end{equation}

where \\(p\\) is a frame attached to a rigid link of the robot,
\\(\mathbf{q}\\) are the \\(n\\) generalized coordinates of the robot, and \\(\mathbf{x}\\) is the pose of \\(p\\) in another frame (typically the world frame). If \\(\mathbf{x}\\) is represented by \\(m\\) real numbers, then \\(\mathbf{f} : \mathbb{R}^n \to \mathbb{R}^m \\).

**We will use \\(p\\) throughout this learning module to refer to
a pose attached to a rigid link on the robot.** 

{% include image.html url="../../assets/img/frame-p-on-robot.png" description="Depiction of Frame p, which is defined with respect to one of the robot's links. While forward kinematics seeks to determine how p is positioned and oriented with respect to the global frame, differential kinematics seek to determine how quickly p changes as a function of the robot's current configuration." %} 

#### Inverting \\(\mathbf{f}(.)\\)

Inverting \\(\mathbf{f}(.)\\) means determining \\(\mathbf{f}^{-1}(\mathbf{x}) = \mathbf{q}\\), where \\(\mathbf{x}\\) is one of the possible outputs of the forward kinematics function (SO(2), SO(3), \\(\mathbb{R}^2, \mathbb{R}^3\\), SE(2), SE(3)). This inversion is generally challenging because \\(\mathbf{f}(.)\\) is a nonlinear function. Additional challenges are that:

* \\(\mathbf{f}^{-1}(\mathbf{x})\\) may have no solutions
* \\(\mathbf{f}^{-1}(\mathbf{x}\\)) may have multiple solutions
* Computation of \\(\mathbf{f}^{-1}(.)\\) generally recommends the use of a symbolic mathematics package (like [Macsyma](http://maxima.sourceforge.net)), which means that changing the kinematics of the robot means re-derivation of the inverse kinematics function

Analytical inverse kinematics requires that the number of constraints be
equal to the robot's number of generalized coordinates. Artificial constraints
may be introduced to satisfy this requirement. For example, a "typical"
anthropomorphic arm provides seven degrees of freedom, while controlling
position and orientation in 3D presents six constraints. Analytical inverse
kinematics solutions for such arms typically leave the elbow position as
an open parameter for the user to adjust, reducing the controllable degrees
of freedom of the arm to six. 

{% include image.html url="../../assets/img/ik.png" description="An example of analytical inverse kinematics, using a two link planar arm. Image cribbed from Stefan Schaal's course notes." %}
 
### Differential kinematics

Another way to do inverse kinematics is using _differential kinematics_. Differential kinematics examines the change in \\(\mathbf{x}\\) given small
changes in \\(\mathbf{q}\\). [Recall that \\(\mathbf{x}\\) can represent position and/or orientation in 2D or 3D](../forward-kinematics), so the aforementioned "small changes" refer to small changes in position and/or orientation.

\begin{equation}
\dot{\mathbf{x}} = \begin{bmatrix} 
\frac{\partial f\_1}{\partial q\_1} & \ldots & \frac{\partial f\_1}{\partial q\_n} \\\
& \vdots & \\\
\frac{\partial f\_m}{\partial q\_1} & \ldots & \frac{\partial f\_m}{\partial q\_n}
\end{bmatrix}
\begin{bmatrix}
\dot{q}\_1 \\\
\vdots \\\
\dot{q}\_n
\end{bmatrix} \label{eqn:Jacobians-full}
\end{equation}

The first matrix is the _Jacobian of pose \\(\mathbf{p}\\) with respect to
the generalized configuration_. Refer back to the [material on linear algebra](../linear-algebra/) if you need a refresher on Jacobian matrices. The second matrix is just the generalized
velocity, \\(\dot{\mathbf{q}}\\). So, we can write Equation \ref{eqn:Jacobians-full} as:
\begin{equation}
\dot{\mathbf{x}} = \mathbf{J}\dot{\mathbf{q}} \label{eqn:Jacobians}
\end{equation}
_You will frequently see this equation in robotic manipulation_.

#### An example: analytically computing the Jacobian of a double pendulum

Consider the [double pendulum from the learning module on forward kinematics](../forward-kinematics). At this point in time, assume that we wish only to
compute the Jacobian matrix of the double pendulum's endpoint. Stated another way, \\(\mathbf{f}(\mathbf{q}) \to \mathbb{R}^2\\) and is defined as:
\begin{equation}
\begin{bmatrix}
l\_1 c\_1 + l\_2 c\_{1+2} \\\\
l\_1 s\_1 + l\_2 s\_{1+2}
\end{bmatrix}
\label{eqn:fkin-example}
\end{equation}

**Before reading further, make sure you know (a) the dimension of \\(\mathbf{q}\\), (b) the dimension of \\(\mathbf{f}(.)\\), and the dimension of the Jacobian matrix that will be produced.** 

The Jacobian matrix for Equation \ref{eqn:fkin-example} will be:
\begin{equation}
\begin{bmatrix}
{'fraction_non_alphanumeric': 0.1355640535372849, 'fraction_numerical': 0.007074569789674952, 'mean_word_length': 4.181277860326895, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3012131', 'n_tokens_mistral': 3096, 'n_tokens_neox': 2852, 'n_words': 1269}