<commit_before>---
layout: post
comments: true
title:  "Deriving equations of motion for a 2-R manipulator using MATLAB"
excerpt: "Lagrange methods to derive equations of motion of robotic systems under no contraints is presented. MATLAB scripts are provided that implement that perform same calculations using symbolic toolbox."
date:   2016-08-30 11:00:00
mathjax: true
---


#### <NAME> 

#### Motivation
This document presents Lagrangian techniques to derive equations of motion using symbolic toolbox in MATLAB. Mathematical models are developed to approximate what the actual system may be doing. All mathematical models involve some simplifying assumptions, and typically relaxing these assumptions results in more complex representation of the dynamics of the system. As a result, traditional pen and paper techniques are not sufficient to derive equations representing dynamics of complex systems. Screen shot below presents 2 terms of inertia matrix of 3-D humanoid robot. 

<div class='fig figcenter fighighlight'>
  <img src='/images/biped_model.gif'>
  <figcaption>Fig2. - 7-link biped with curved feet</figcaption>
</div>

Thankfully MATLAB, python, and other programming languages offer support for symbolic calculations that can be utilized to automate deriving these equations. In this document, we will derive equations of motion for a 2-link robotic arm (or double pendulum) using MATLAB. 

### two-link robotic arm model

Consider the model of a simple manipulator shown below. This configuration is also referred to as double pendulum. We assume that the massless links of length \\( l_1 \\) and \\( l_2\\) connect masses \\( m_1 \\) and \\(m_2\\), and the corresponding segment angles are given by \\( \theta_1 \\) and  \\( \theta_2 \\) . We assume that there is one motor each at each of the joints.


<div class='fig figcenter fighighlight'>
  <img src='/images/manip_2R.png'>
  <figcaption>Fig1. - Simple 2-link manipulator or double pendulum</figcaption>
</div>

*** We chose angles \\( \theta_1 \\) and \\( \theta_2 \\) to describe the system because this represent a representation that requires least number of independent variables to completely describe the system. It is possible to use \\( x \\) and \\( y \\) locations of the masses to derive the equations of motion too, however in such cases additional constraints must be imposed on the system to ensure the lengths of links remain constant. Such contraints can be imposed using Pfaffin constraints. However, this method is beyond the scope of this course. For details please refer to chapter 6 in the [robotics book by Murray and Sastry](http://www.cds.caltech.edu/~murray/books/MLS/pdf/mls94-complete.pdf). For complex robotic systems or biomechanics simulations, the position vectors are obtained using screw theory or Denavit–Hartenberg parameters. Using appropriate coordinate systems can greatly simplify the equations of motion, however this may not always be possible. For example, human walking model with curved feet cannot be modeled only by ignoring the position and velocity constraint between feet and ground. ***

<div class='fig figcenter fighighlight'>
  <img src='/images/EOM_ss.png'>
  <figcaption>Fig3. - 7-link biped with curved feet</figcaption>
</div>


We will derive the equations of motions using Lagrange method in the following steps,

#### 1. Compute position and velcoties 

The first step is to compute position of all the masses in the systems. In this case, we will compute positions of the masses with respect to the origin attached at the base of the manipulator in terms of angles \\( \theta_1 \\) and \\( \theta_2 \\).*

$$ \left[ \begin{array}{c} P_{1,x} \\ P_{1,y}   \end{array} \right] = \left[\begin{array}{c} l_1 cos(\theta_1) \\  l_1 sin(\theta_1)   \end{array}  \right], $$

and 

$$ \left[ \begin{array}{c} P_{2,x} \\ P_{2,y}   \end{array} \right] = \left[\begin{array}{c} l_1 cos(\theta_1) + l_2 cos(\theta_1 + \theta_2) \\  l_1 sin(\theta_1)  + l_2 sin(\theta_1 + \theta_2)   \end{array}  \right]. $$

We next compute velocities above using, <commit_msg>fixed fig orders<commit_after>---
layout: post
comments: true
title:  "Deriving equations of motion for a 2-R manipulator using MATLAB"
excerpt: "Lagrange methods to derive equations of motion of robotic systems under no contraints is presented. MATLAB scripts are provided that implement that perform same calculations using symbolic toolbox."
date:   2016-08-30 11:00:00
mathjax: true
---


#### <NAME> 

#### Motivation
This document presents Lagrangian techniques to derive equations of motion using symbolic toolbox in MATLAB. Mathematical models are developed to approximate what the actual system may be doing. All mathematical models involve some simplifying assumptions, and typically relaxing these assumptions results in more complex representation of the dynamics of the system. As a result, traditional pen and paper techniques are not sufficient to derive equations representing dynamics of complex systems. Screen shot below presents 2 terms of inertia matrix of 3-D humanoid robot. 

<div class='fig figcenter fighighlight'>
  <img src='/images/EOM_ss.png'>
  <figcaption>Fig1. - One term in the equation of 3_D humanoid robot. </figcaption>
</div>


Thankfully MATLAB, python, and other programming languages offer support for symbolic calculations that can be utilized to automate deriving these equations. In this document, we will derive equations of motion for a 2-link robotic arm (or double pendulum) using MATLAB. 

### two-link robotic arm model

Consider the model of a simple manipulator shown below. This configuration is also referred to as double pendulum. We assume that the massless links of length \\( l_1 \\) and \\( l_2\\) connect masses \\( m_1 \\) and \\(m_2\\), and the corresponding segment angles are given by \\( \theta_1 \\) and  \\( \theta_2 \\) . We assume that there is one motor each at each of the joints.


<div class='fig figcenter fighighlight'>
  <img src='/images/manip_2R.png'>
  <figcaption>Fig 2. - Simple 2-link manipulator or double pendulum</figcaption>
</div>

*** We chose angles \\( \theta_1 \\) and \\( \theta_2 \\) to describe the system because this represent a representation that requires least number of independent variables to completely describe the system. It is possible to use \\( x \\) and \\( y \\) locations of the masses to derive the equations of motion too, however in such cases additional constraints must be imposed on the system to ensure the lengths of links remain constant. Such contraints can be imposed using Pfaffin constraints. However, this method is beyond the scope of this course. For details please refer to chapter 6 in the [robotics book by Murray and Sastry](http://www.cds.caltech.edu/~murray/books/MLS/pdf/mls94-complete.pdf). For complex robotic systems or biomechanics simulations, the position vectors are obtained using screw theory or Denavit–Hartenberg parameters. Using appropriate coordinate systems can greatly simplify the equations of motion, however this may not always be possible. For example, human walking model with curved feet cannot be modeled only by ignoring the position and velocity constraint between feet and ground. ***


<div class='fig figcenter fighighlight'>
  <img src='/images/biped_model.gif'>
  <figcaption>Fig 3. - 7-link biped with curved feet</figcaption>
</div>




We will derive the equations of motions using Lagrange method in the following steps,

#### 1. Compute position and velcoties 

The first step is to compute position of all the masses in the systems. In this case, we will compute positions of the masses with respect to the origin attached at the base of the manipulator in terms of angles \\( \theta_1 \\) and \\( \theta_2 \\).*

$$ \left[ \begin{array}{c} P_{1,x} \\ P_{1,y}   \end{array} \right] = \left[\begin{array}{c} l_1 cos(\theta_1) \\  l_1 sin(\theta_1)   \end{array}  \right], $$

and 

$$ \left[ \begin{array}{c} P_{2,x} \\ P_{2,y}   \end{array} \right] = \left[\begin{array}{c} l_1 cos(\theta_1) + l_2 cos(\theta_1 + \theta_2) \\  l_1 sin(\theta_1)  + l_2 sin(\theta_1 + \theta_2)   \end{array}  \right]. $$

We next compute velocities above using, 
{'fraction_non_alphanumeric': 0.08873720136518772, 'fraction_numerical': 0.013895660653339834, 'mean_word_length': 4.121722846441948, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3742958', 'n_tokens_mistral': 2307, 'n_tokens_neox': 2162, 'n_words': 1083}