Numerical Methods Lab 4
In this lab you will explore the use of non-linear optimization on a problem that occurs in robotics/animation. In particular, you will evaluate Newton's and Broyden's method for non-linear systems of equations in inverse kinematic applications.
Given a kinematic chain (a set of joints that are joined by links), forward kinematics is the problem of determining the position of the joints (or only the end-effector) given the joint angles. This problem is a straighforward application of Euclidean transformations.
Inverse kinematics is the inverse problem: determine the joint orientations that satisfy a given a set of constraints (e.g., find the joint orientations that put the end-effector at a specific location). This problem is harder than forward kinematics; the analytic solution is often complicated or doesn't exist so numerical solutions are required. Furthermore, it is possible that the solution is not unique. There may be no solutions or several solutions.
Matlab Path
presentation.pdf
Please addpath /cshome/vis/data so that you get access to the different resources
required for this lab. If you are using your own computer you might have to test the last part at
the labs to make sure everything is working properly.
In the first part of this lab you will focus on a 2D planar manipulator with two links and two rotational degrees of freedom (d.o.f.) (see Fig.
). We will parameterize our manipulator with a vector of two link lengths (
) and a vector of angles
.
The non-linear equations for positioning the end effector at a point
are
- Write a function [pos,J]=evalRobot2D(l,theta) that returns the position,
(
vector), of the end-effector and the Jacobian,
, given the rotation angles theta and the fixed link lengths l.
- Write a function J=fdJacob2D(l,theta,alpha) that uses central differences to compute the Jacobian using step-size alpha. Evaluate several different values for alpha by comparing the finite difference Jacobian to the analytic Jacobian for a variety of values for
.
- As an example, the central difference formula for the first parameter of the Jacobian would be (evalRobot2D(l,theta+[alpha;0])-evalRobot2D(l,theta-[alpha;0]))/(2*alpha)
Note that you'll have to figure out the formula for the second parameter of the Jacobian.
- Are the results close enough to be useful in the optimization?
- Why would you use this finite-difference approximation instead of the analytic derivative?
- Write a function theta=invKin2D(l,theta0,pos,n,mode) that returns the rotation angles in a
vector
that put the end-effector at
. Your method should terminate when the constraint is satisfied (use a threshold on the norm of the residual) or when the
iterations have occured. When mode=0 you should use Newton's method, and mode=1 you should use Broyden's method
- A note to help you understanding Newton's method note-newton
- For Broyden's method start with the Jacobian from your evalRobot2D function.
-
Use a script similar to the eval2D.m
to evaluate the effect of mode. You can use our plotRobot2D.m
function to plot.
- How does Broyden's compare to Newton?
The system of nonlinear equations for the position of end effector (Forward Kinematics)
is given by the equations below.
Unlike the planar manipulator, we also have rotations in the z direction given by
.
- Firstly, try to plot the robot using the routine plotRobot3D(l,theta).
The given routine [pos,J] = evalRobot3D(l, theta) returns the position pos of the end effector
and the Jacobian J of the nonlinear system of equations given above, given the rotation angles theta
and the link lengths l. Use the above method to display the Jacobian for
the following configurations and explain what happens:
a. Elbow at pi/2
b. Arm fully stretched in a line
c. Arm at the center
- Design a routine theta=invKin3D(l,theta0,desired,n,mode) as in Exercise 1
to obtain the joint angles for a ‘desired’ end effector position.
When mode=0 you should use Newton's method, and mode=1 you should use Broyden's method.
Test your algorithm on random points near the end effector and check the following conditions:
a. Start moving the target point farther from the end effector and check where the Newton’s method stops converging.
Explain why it stops converging.
b. How would you tackle the problem above where the target is far from the end effector?
Hint: Think about a straight line or a curve connecting the end effector and target point.
Keep in mind the singularity conditions of the Jacobian as experimented in Exc.1.
c. Choose target points in the diametrically opposite quadrant to the end effector
and try to reach the point using your algorithm above.
What could be the issue here? Plan a path to the target and show how that would solve this issue.
Hint: Try breaking down the path into separate segments so that you can avoid singularities.
Optional: Think about how we can make the motions smoother using curves (splines/bezier).
- Implement the pick-up and place task in the eval3D.m file using the methods discussed in Exc.2.
Watch the demo video to get an idea of the required result.
Move all the cans from the table to the conveyor belt.
Note: The target points at the conveyor belt and the location of cans
at the table are provided in can_positions.mat .
In the second half of this lab you will extend your 2D implementation to 3D.
We will be animating two legs (independently) for a human.
Assume that we can parameterize each leg with four degrees of freedom. The
location of the end-effector is then the homogeneous zero transformed by the
a sequence of transformations.
![$\displaystyle f({\bf\theta}) = {\bf M}_{hip}{\bf R}_z(\theta_3){\bf R}_y(\theta...
...\bf R}_x(\theta_1){\bf M}_{knee}{\bf R}_{x}(\theta_4){\bf M}_{foot} [0,0,0,1]^T$](img15.png) |
(3) |
Where
is a rigid transformation encoding the rest position and orientation
of a joint in the space of its parent, and may be different for each leg.
As input, all your functions should take
(a cell
array) as its first parameter, followed by the joint angles
.
- Write a function [pos,J]=evalRobot3D(M,theta) that returns the position
and Jacobian for a set of rotation angles. Note: using the homogeneous representation (Equation 3) your function should discard the homogeneous component of the vector, i.e., pos should be
The homogeneous component should always be 1 as M contain only Euclidean transformations.
- Write a function theta=invKin3D(M,theta,pos) that returns
the inverse kinematic solution for pos.
- Should be straighforward to modify your Newton implementation from part 1.
- Test your function on the data sequences provided in the data directory
(walk1.mat,walk2.mat,walk3.mat,...,jump1.mat). Each .mat file contains a L and R variables that are
matrices containing a time sequence of positional constraints for the left and right legs respectively.
Note:
We only provide support for the lab computer environment due to the compatibility issue of openGL.
The following steps assume that you are using the lab computer either locally or remotely with X11 forwarding.
(a). Type addpath /cshome/vis/data in matlab first.
(b). Use the humanInterp(drad,angles)
function to display the results. The function assumes you have stacked the left and right
vectors of angles on top of each other into one
vector (left joint angles as the first four elements). Note that
drad is defined in human_data.mat.
(c). Use robot3D('new') to recreate
the OpenGL window.
(d). Pass the output of "humanInterp.m" to "robot3D" to show the result.
(e). In case you have multiple frames, i.e., multiple thetas, write a for loop to display frame by frame.
How to enable X11 forwarding through ssh?
First, you need to install the X Server.
For Windows: you need to install xming server from https://sourceforge.net/projects/xming/
For Mac: you need to install XQuartz server from https://www.xquartz.org/
For Ubuntu: you need to install xauth package: $ sudo apt-get install xauth
Second, when you start an ssh connection from your ssh client, you need to enable X11 either by "ssh -X your_csid@innisfree.cs.ualberta.ca" or google it for your ssh client, e.g., PuTTY.
In case the 3D rendering engine (robot3D function) does not work for you for some reason, we provide you with an alternative API for rendering a stick figure in 3D.
Note that this is only for animation purpose. In other words, the functions that you implement in step 1 and 2 will be the same regardless of the rendering API.
(a) Type addpath /cshome/vis/data in matlab first.
(b) Just call stick3D(angle) for the animation, where angle is the same as the one described in the humanInterp(drad,angles) function: 8x1 vector that contains the joint angles for the left and
right legs.
(c) In case you have multiple frames, i.e., multiple thetas, write a for loop to display frame by frame.
Here are two videos that show the expected output of the animation of walk1 sequence:
human rendering , stick figure rendering.
Add /cshome/vis/data to your path or copy to a local directory if you want to modify. The first part of the lab:
- eval2D.m: script for you to test your inverse kinematic function in 2D.
- plotRobot2D.m: plotRobot2D(ls,ts) plots the robot with link lengths ls and thetas ts
The second part:
- The data directory contains animation sequences (walk1,walk2,...).
- human_data.mat
Contains the matrix definitions for the human and variables used for animation.
- robot3D.mexglx: The mex function will work on 32bit computers,
there is no need to recompile.
- robot3D.mexa64: The mex function will work on the lab
computers, there is no need to recompile.
- humanInterp.m: humanInterp(drad,angles) returns a full animation vector that can be passed to robot3D to draw an interpolated pose for the upper body. drad is defined in human_data.mat.
- stick3D.m: stick3D(angles) create a stick figure for the legs in 3D using the angles that the user computed.
Homogeneous matrix transforms:
The regular rules of calculus apply to matrix products as long as you are aware that
you cannot commute elements. The product rule for matrices:
Notice that when
is not a function of
you get:
An example of how to compute the Jacobian : Jacobian_example