In the previous exciting post in this series I outlined the project, which is in the title, and we worked through getting access to the arm through Python. The next step was deriving the Jacobian, and that’s what we’re going to be talking about in this post!
This was a time I was very glad to have a previous post talking about generating transformation matrices, because deriving the Jacobian for a 6DOF arm in 3D space comes off as a little daunting when you’re used to 3DOF in 2D space, and I needed a reminder of the derivation process. The first step here was finding out which motors were what, so I went through and found out how each motor moved with something like the following code:
for ii in range(7): target_angles = np.zeros(7, dtype='float32') target_angles[ii] = np.pi / 4.0 rob.move(target_angles) time.sleep(1)
and I found that the robot is setup in the figures below
this is me trying my hand at making things clearer using Inkscape, hopefully it’s worked. Displayed are the first 6 joints and their angles of rotation, through . The 7th joint, , opens and closes the gripper, so we’re safe to ignore it in deriving our Jacobian. The arm segment lengths and are named based on the nearest joint angles (makes easier reading in the Jacobian derivation).
Find the transformation matrix from end-effector to origin
So first thing’s first, let’s find the transformation matrices. Our first joint, , rotates around the axis, so the rotational part of our transformation matrix is
and and our origin frame of reference are on top of each other so we don’t need to account for translation, so our translation component of is
Stacking these together to form our first transformation matrix we have
So now we are able to convert a position in 3D space from to the reference frame of joint back to our origin frame of reference. Let’s keep going.
Joint rotates around the axis, and there is a translation along the arm segment . Our transformation matrix looks like
Joint also rotates around the axis, but there is no translation from to . So our transformation matrix looks like
The next transformation matrix is a little tricky, because you might be tempted to say that it’s rotating around the axis, but actually it’s rotating around the axis. This is determined by where is mounted relative to . If it was mounted at 90 degrees from then it would be rotating around the axis, but it’s not. For translation, there’s a translation along the axis up to the next joint, so all in all the transformation matrix looks like:
And then the transformation matrices for coming from to and to are the same as the previous set, so we have
Alright! Now that we have all of the transformation matrices, we can put them together to get the transformation from end-effector coordinates to our reference frame coordinates!
At this point I went and tested this with some sample points to make sure that everything seemed to be being transformed properly, but we won’t go through that here.
Calculate the derivative of the transform with respect to each joint
The next step in calculating the Jacobian is getting the derivative of . This could be a big ol’ headache to do it by hand, OR we could use SymPy, the symbolic computation package for Python. Which is exactly what we’ll do. So after a quick
sudo pip install sympy
I wrote up the following script to perform the derivation for us
import sympy as sp def calc_transform(): # set up our joint angle symbols (6th angle doesn't affect any kinematics) q = [sp.Symbol('q0'), sp.Symbol('q1'), sp.Symbol('q2'), sp.Symbol('q3'), sp.Symbol('q4'), sp.Symbol('q5')] # set up our arm segment length symbols l1 = sp.Symbol('l1') l3 = sp.Symbol('l3') l5 = sp.Symbol('l5') Torg0 = sp.Matrix([[sp.cos(q), -sp.sin(q), 0, 0,], [sp.sin(q), sp.cos(q), 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) T01 = sp.Matrix([[1, 0, 0, 0], [0, sp.cos(q), -sp.sin(q), l1*sp.cos(q)], [0, sp.sin(q), sp.cos(q), l1*sp.sin(q)], [0, 0, 0, 1]]) T12 = sp.Matrix([[1, 0, 0, 0], [0, sp.cos(q), -sp.sin(q), 0], [0, sp.sin(q), sp.cos(q), 0], [0, 0, 0, 1]]) T23 = sp.Matrix([[sp.cos(q), 0, sp.sin(q), 0], [0, 1, 0, l3], [-sp.sin(q), 0, sp.cos(q), 0], [0, 0, 0, 1]]) T34 = sp.Matrix([[1, 0, 0, 0], [0, sp.cos(q), -sp.sin(q), 0], [0, sp.sin(q), sp.cos(q), 0], [0, 0, 0, 1]]) T45 = sp.Matrix([[sp.cos(q), 0, sp.sin(q), 0], [0, 1, 0, l5], [-sp.sin(q), 0, sp.cos(q), 0], [0, 0, 0, 1]]) T = Torg0 * T01 * T12 * T23 * T34 * T45 # position of the end-effector relative to joint axes 6 (right at the origin) x = sp.Matrix([0,0,0,1]) Tx = T * x for ii in range(6): print q[ii] print sp.simplify(Tx.diff(q[ii])) print sp.simplify(Tx.diff(q[ii])) print sp.simplify(Tx.diff(q[ii]))
And then consolidated the output using some variable shorthand to write a function that accepts in joint angles and generates the Jacobian:
def calc_jacobian(q): J = np.zeros((3, 7)) c0 = np.cos(q) s0 = np.sin(q) c1 = np.cos(q) s1 = np.sin(q) c3 = np.cos(q) s3 = np.sin(q) c4 = np.cos(q) s4 = np.sin(q) c12 = np.cos(q + q) s12 = np.sin(q + q) l1 = self.l1 l3 = self.l3 l5 = self.l5 J[0,0] = -l1*c0*c1 - l3*c0*c12 - l5*((s0*s3 - s12*c0*c3)*s4 + c0*c4*c12) J[1,0] = -l1*s0*c1 - l3*s0*c12 + l5*((s0*s12*c3 + s3*c0)*s4 - s0*c4*c12) J[2,0] = 0 J[0,1] = (l1*s1 + l3*s12 + l5*(s4*c3*c12 + s12*c4))*s0 J[1,1] = -(l1*s1 + l3*s12 + l5*s4*c3*c12 + l5*s12*c4)*c0 J[2,1] = l1*c1 + l3*c12 - l5*(s4*s12*c3 - c4*c12) J[0,2] = (l3*s12 + l5*(s4*c3*c12 + s12*c4))*s0 J[1,2] = -(l3*s12 + l5*s4*c3*c12 + l5*s12*c4)*c0 J[2,2] = l3*c12 - l5*(s4*s12*c3 - c4*c12) J[0,3] = -l5*(s0*s3*s12 - c0*c3)*s4 J[1,3] = l5*(s0*c3 + s3*s12*c0)*s4 J[2,3] = -l5*s3*s4*c12 J[0,4] = l5*((s0*s12*c3 + s3*c0)*c4 + s0*s4*c12) J[1,4] = l5*((s0*s3 - s12*c0*c3)*c4 - s4*c0*c12) J[2,4] = -l5*(s4*s12 - c3*c4*c12) return J
Alright! Now we have our Jacobian! Really the only time consuming part here was calculating our end-effector to origin transformation matrix, generating the Jacobian was super easy using SymPy once we had that.
Hack position control using the Jacobian
Great! So now that we have our Jacobian we’ll be able to translate forces that we want to apply to the end-effector into joint torques that we want to apply to the arm motors. Since we can’t control applied force to the motors though, and have to pass in desired angle positions, we’re going to do a hack approximation. Let’s first transform our forces from end-effector space into a set of joint angle torques:
To approximate the control then we’re simply going to take the current set of joint angles (which we know because it’s whatever angles we last told the system to move to) and add a scaled down version of to approximate applying torque that affects acceleration and then velocity.
where is the gain term, I used .001 here because it was nice and slow, so no crazy commands that could break the servos would be sent out before I could react and hit the cancel button.
What we want to do then to implement operational space control here then is find the current position of the end-effector, calculate the difference between it and the target end-effector position, use that to generate the end-effector control signal , get the Jacobian for the current state of the arm using the function above, find the set of joint torques to apply, approximate this control by generating a set of target joint angles to move to, and then repeat this whole loop until we’re within some threshold of the target position. Whew.
So, a lot of steps, but pretty straight forward to implement. The method I wrote to do it looks something like:
def move_to_xyz(self, xyz_d): """ np.array xyz_d: 3D target (x_d, y_d, z_d) """ count = 0 while (1): count += 1 # get control signal in 3D space xyz = self.calc_xyz() delta_xyz = xyz_d - xyz ux = self.kp * delta_xyz # transform to joint space J = self.calc_jacobian() u = np.dot(J.T, ux) # target joint angles are current + uq (scaled) self.q[...] += u * .001 self.robot.move(np.asarray(self.q.copy(), 'float32')) if np.sqrt(np.sum(delta_xyz**2)) < .1 or count > 1e4: break
And that is it! We have successfully hacked together a system that can perform operational space control of a 6DOF robot arm. Here is a very choppy video of it moving around to some target points in a grid on a cube.
So, granted I had to drop a lot of frames from the video to bring it’s size down to something close to reasonable, but still you can see that it moves to target locations super fast!
Alright this is sweet, but we’re not done yet. We don’t want to have to tell the arm where to move ourselves. Instead we’d like the robot to perform target tracking for some target LED we’re moving around, because that’s way more fun and interactive. To do this, we’re going to use spiking cameras! So stay tuned, we’ll talk about what the hell spiking cameras are and how to use them for a super quick-to-setup and foolproof target tracking system in the next exciting post!