Makelangelo Tutorials

Makelangelo Spirograph Art

Making spirograph art is easy on the Makelangelo. Here’s a few examples of how you can generate beautiful geometric patterns and spirograph art. Post your favorites to the forums!

Makelangelo Spirograph Art with Scratch

Export the output from Scratch, load the SB file in Makelangelo Software, and proceed as normal.

Makelangelo Spirograph Art with Processing

void setup() {
  float r=225;
  float a = 0;
  println("G0 Z90");
  driveto(r,a);
  println("G0 Z30");
  while(r>15) {
    r-=0.5;
    a+=121;
    driveto(r,a);
  }
  println("G0 Z90");
  println("G28");
}

void driveto(float r,float a) {
  float x = sin(radians(a)) * r;
  float y = cos(radians(a)) * r;
  
  println("G0 X"+x+" Y"+y);
}

Copy/paste the output into a file called “test.ngc”, open that file in Makelangelo-Software, and proceed and normal.

Robot Arm Tutorials

Calculating Jacobian matrixes for a 6 axis Robot Arm

What are Jacobian matrixes (good for)?

I want to know how fast the Sixi robot has to move each joint (how much work in each muscle) to move the end effector (finger tip) at my desired velocity (direction * speed). For any given pose, the Jacobian matrix describes the relationship between the joint velocities and the end effector velocity. The inverse jacobian matrix does the reverse, and that’s what I want.

The Jacobian matrix could be a matrix of equations, solved for any pose of the robot. That is a phenomenal amount of math and, frankly, I’m not that smart. I’m going to use a method to calculate the instantaneous approximate Jacobian at any given robot pose, and then recalculate it as often as I need. It may be off in the 5th or 6th decimal place, but it’s still good enough for my needs.

Special thanks to Queensland University of Technology. Their online explanation taught me this method. I strongly recommend you watch their series, which will help this post (a kind of cheat sheet) make more sense.

What tools do I have to find the Jacobian matrix?

  • I have the D-H parameters for my robot;
  • I have my Forward Kinematics (FK) calculations; and
  • I have my Inverse Kinematics (IK) calculations.

I have a convenience method that takes 6 joint angles and the robot description and returns the matrix of the end effector.

/**
 * @param jointAngles 6 joint angles
 * @param robot the D-H description and the FK/IK solver
 * @return the matrix of the end effector
 */
private Matrix4d computeMatrix(double [] jointAngles,Sixi2 robot) {
     robot.setRobotPose(jointAngles);  // recursively calculates all the matrixes down to the finger tip.
     return new Matrix4d(robot.getLiveMatrix());
}

The method for approximating the Jacobian

Essentially I’m writing a method that returns the 6×6 Jacobian matrix for a given robot pose.

/**
 * Use Forward Kinematics to approximate the Jacobian matrix for Sixi.
 * See also https://robotacademy.net.au/masterclass/velocity-kinematics-in-3d/?lesson=346
 */ 
public double [][] approximateJacobian(Sixi robot,double [] jointAnglesA) { 
     double [][] jacobian = new double[6][6]; 
     //....
     return jacobian;
}

The keyframe is a description of the current joint angles, and the robot contains the D-H parameters and the FK/IK calculators.

Each column of the Jacobian has 6 parameters: 0-2 describe the translation of the hand and 3-5 describe the rotation of the hand. Each column describes the change for a single joint: the first column is the change in the end effector isolated to only a movement in J0.

So I have my current robot pose T and one at a time I will change each joint a very small change (0.5 degrees) and calculate the new pose Tnew. (Tnew-T)/change gives me a matrix dT showing the amount of change. The translation component of the Jacobian can be directly extracted from here.

     double ANGLE_STEP_SIZE_DEGREES=0.5;  // degrees
     double [] jointAnglesB = new double[6]; 

     // use anglesA to get the hand matrix 
     Matrix4d T = computeMatrix(jointAnglesA,robot);

     int i,j;
     for(i=0;i<6;++i) {  // for each axis
         for(j=0;j<6;++j) {
             jointAnglesB[j]=jointAnglesA[j];
         }
         // use anglesB to get the hand matrix after a tiiiiny adjustment on one axis. 
         jointAnglesB[i] += ANGLE_STEP_SIZE_DEGREES; 
         Matrix4d Tnew = computeMatrix(jointAnglesB,robot);

         // use the finite difference in the two matrixes
         // aka the approximate the rate of change (aka the integral, aka the velocity) 
         // in one column of the jacobian matrix at this position.
         Matrix4d dT = new Matrix4d();
         dT.sub(Tnew,T);
         dT.mul(1.0/Math.toRadians(ANGLE_STEP_SIZE_DEGREES));
         jacobian[i][0]=dT.m03;
         jacobian[i][1]=dT.m13;
         jacobian[i][2]=dT.m23;

We’re halfway there! Now the rotation part is more complex. We need to look at just the rotation part of each matrix.

        Matrix3d T3 = new Matrix3d(
                 T.m00,T.m01,T.m02,
                 T.m10,T.m11,T.m12,
                 T.m20,T.m21,T.m22);
        Matrix3d dT3 = new Matrix3d(
                 dT.m00,dT.m01,dT.m02,
                 dT.m10,dT.m11,dT.m12,
                 dT.m20,dT.m21,dT.m22);
        T3.transpose();  // inverse of a rotation matrix is its transpose
        Matrix3d skewSymmetric = new Matrix3d();
        skewSymmetric.mul(dT3,T3);
        //[  0 -Wz  Wy]
         //[ Wz   0 -Wx]
         //[-Wy  Wx   0]
         jacobian[i][3]=skewSymmetric.m12;  // Wx
         jacobian[i][4]=skewSymmetric.m20;  // Wy
         jacobian[i][5]=skewSymmetric.m01;  // Wz
    }
    return jacobian;
}

Testing the Jacobian (finding Joint Velocity over Time)

So remember the whole point is to be able to say “I want to move the end effector with Force F, how fast do the joints move?” I could apply this iteratively over some period of time and watch how the end effector moves.

public void angularVelocityOverTime() {
    System.out.println("angularVelocityOverTime()");
    Sixi2 robot = new Sixi2();

    BufferedWriter out=null;
    try {
        out = new BufferedWriter(new FileWriter(new File("c:/Users/Admin/Desktop/avot.csv")));
        out.write("Px\tPy\tPz\tJ0\tJ1\tJ2\tJ3\tJ4\tJ5\n");
        
        DHKeyframe keyframe = (DHKeyframe)robot.createKeyframe();
        DHIKSolver solver = robot.getSolverIK();
        double [] force = {0,3,0,0,0,0};  // force along +Y direction

        // move the hand to some position...
        Matrix4d m = robot.getLiveMatrix();
        m.m13=-20;
        m.m23-=5;
        // get the hand position
        solver.solve(robot, m, keyframe);
        robot.setRobotPose(keyframe);
        
        float TIME_STEP=0.030f;
        float t;
        int j, safety=0;
        // until hand moves far enough along Y or something has gone wrong
        while(m.m13<20 && safety<10000) {
            safety++;
            m = robot.getLiveMatrix();
            solver.solve(robot, m, keyframe);  // get angles
            // if this pose is in range and does not pass exactly through a singularity
            if(solver.solutionFlag == DHIKSolver.ONE_SOLUTION) {
                double [][] jacobian = approximateJacobian(robot,keyframe);
                // Java does not come by default with a 6x6 matrix class.
                double [][] inverseJacobian = MatrixHelper.invert(jacobian);
                
                out.write(m.m03+"\t"+m.m13+"\t"+m.m23+"\t");  // position now
                double [] jvot = new double[6];
                for(j=0;j<6;++j) {
                    for(int k=0;k<6;++k) {
                        jvot[j]+=inverseJacobian[k][j]*force[k];
                    }
                    // each jvot is now a force in radians/s
                    out.write(Math.toDegrees(jvot[j])+"\t");
                    // rotate each joint aka P+= V*T
                    keyframe.fkValues[j] += Math.toDegrees(jvot[j])*TIME_STEP;
                }
                out.write("\n");
                robot.setRobotPose(keyframe);
            } else {
                // Maybe we're exactly in a singularity.  Cheat a little.
                m.m03+=force[0]*TIME_STEP;
                m.m13+=force[1]*TIME_STEP;
                m.m23+=force[2]*TIME_STEP;
            }
        }
        
    }
    catch(Exception e) {
        e.printStackTrace();
    }
    finally {
        try {
            if(out!=null) out.flush();
            if(out!=null) out.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

Viewing the results

The output of this method is conveniently formatted to work with common spreadsheet programs, and then graphed.

Position and Joint velocity over Time

I assume the small drift in Z is due to numerical error over many iterations.

Now what?

Since velocity is a function of acceleration (v=a*t) and acceleration is a force I should be able to teach the arm all about forces:

  • Please push this way (squeeze my lemon)
  • Please stop if you meet a big opposite force. (aka compliant robotics aka safe working around humans)
  • You are being pushed. Please move that way. (push to teach)
  • Are any of the joints turning too fast? Warn me, please.

Final thoughts

All the code in this post is in the open source Robot Overlord app on Github. The graph above is saved to the Sixi 2 Github repository.

Please let me know this tutorial helps. I really appreciate the motivation! If you want to support my work, there’s always my Patreon or the many fine robots on this site. If you have follow up questions or want me to explain more parts, contact me.

Tutorials

Send Instagram Videos to Youtube with Raspberry Pi and Bash

Making Instagram videos is quick and easy.  Making Youtube videos is a laborious editing process that sucks the sunlight out of my soul.  People kept asking me if I have a Youtube channel and I was embarrassed to admit that yes, sort of, but no, not really.  I needed a way to send my Instagram videos to Youtube… a way to make the rock that kills the first bird bounce and kill the second bird!  With a bit of Bash scripting I was able to get my Raspberry Pi to automatically do this for me.  Read on for all the deets.

I used to use Zapier but they got dumb.  First they wanted to charge me to add extra steps to my one and only zap, then they said they couldn’t work ANYWAYS because Instagram tightened the data access rules.  I’m a big fan of IFTT but they can’t do it, either.  So!  Hand-rolled it is.

The pieces someone else made

I already had a Raspberry Pi 3 (?) set up to take timelapse pictures of my Prusa 3D prints.  I figure it’s idle most of the time, why not make it work a little harder?  Firstly I logged in and updated all the underlying framework.

sudo apt-get update
sudo apt-get upgrade
mkdir ig2yt
cd ig2yt

Then I installed https://github.com/rarcega/instagram-scraper to grab my instagram content.

sudo apt-get install python3
sudo apt-get install python3-venv
python3 -m venv env
/env/bin/python -m pip install instagram-scraper

and tested the instagram-scraper

/env/bin/instagram-scraper yourTargetChannel -u yourIGName -p yourIGPassword -t video

Next I installed https://github.com/tokland/youtube-upload by following the instructions in their readme.

wget https://github.com/tokland/youtube-upload/archive/master.zip
unzip master.zip
cd youtube-upload-master
sudo python setup.py install
cd ..
rm -rf youtube-upload-master

There were also a number of steps (in their readme) to install the .youtube-upload-credentials.json file to /home/pi/.  The file will contain your specific Youtube upload credentials and should remain secret.

That which binds them in darkness

Finally I was ready to test a bash script that would glue these other pieces together.  What you see here is the end result of an afternoon’s work.  It doesn’t show the testing steps along the way, most of which were later removed for brevity.  Test first!  Don’t blow up your following with a crazy upload machine.

#!/bin/bash

# the folder where mp4s will be stored
DESTINATION=imakerobots
PASSWORD=yourPasswordHere
MYACCOUNT=yourAccountNameHere
TARGETACCOUNT=nameItHere

# instagram-scraper looks in $DESTINATION to see what videos you've got
# and only grabs newer videos. youtube-upload uploads indiscriminately.
# we need the list of old videos so we don't upload old stuff again.
# get the list of files already in $DESTINATION
shopt -s nullglob
fileList=($DESTINATION/*.mp4)
#echo $fileList

# grab the 10 newest instagram videos
env/bin/instagram_scraper $TARGETACCOUNT -u $MYACCOUNT -p $PASSWORD -t video --maximum 10 --template {datetime} --latest

# upload each new video to youtube
for filename in $DESTINATION/*.mp4; do
if [[ ! "${fileList[@]}" =~ "${filename}" ]]; then
#echo $filename is new
filename2="${filename##*/}"
filename3="${filename2%.*}"
youtube-upload --title "$filename3" "$filename" --privacy="private"
fi;
done

# delete all but the 3 newest files so we don't fill the drive.
ls $DESTINATION -1tr | head -n -3 | xargs -d '\n' rm -f --

Final Thoughts

  • I didn’t setup youtube-upload in env because I’m a python newb.  I can barely ask “where it the toilet” in parseltongue.
  • I did setup this script to run as a cron job once an hour, every hour.
  • videos will be private so they don’t smash your subscriber’s data plan.  I had them originally set to unlisted but youtube still told everyone!  Many ugly emojis were received.
  • videos will be titled in youtube with the date and time of their original publication.
  • if you make the mistake of publishing a few of them and then pulling older instagrams and publishing those in a second wave your videos will be all out of sequence.  “Ha, ha,” says Youtube, “No fix for you!”
  • hese scripts don’t email me if the process worked, failed, or what have you.  They could be way more robust.
  • Special thanks to the helpful people in IRC freenode channel #bash and #python for helping with the many (many) different versions of python.

As always please let me know if I missed a step, if this was helpful to you, and so on.

3D Printing Tutorials

Gcode Reference Guide

Here is a comprehensive list of all the gcode understood by the Makelangelo firmware.  Please use this guide to create your own gcode generator, to understand the gcode created by Makelangelo software, or to build machines that comply with the format.

Gcode commands should be common to most CNC machines and follow the same format as 3D printers, mills, lathes, and others.  For a more complete reference, please visit https://en.wikipedia.org/wiki/G-code.  Wherever possible we have tried to match the format.

Command structure

All commands are on separate lines (one command per line).  All lines end with a newline (\n). A return (\r) is not required.  Blank lines are ignored.
Any text after a ; is ignored. This gives you room to add comments inside a list of gcode commands.  Commands can be preceeded by a line number:

N[n] command;[checksum]

where [n] is some number >= 0 and checksum is a non-printable character (the XOR sum of all previous characters in the command, not including line number and semi-colon ‘;’ ).  Every correctly parsed command with a line number will increment the expected line number.

M Commands

M6 T[n]
Change to tool number [n].

M17
Engage (turn on) all motors. They will actively resist being moved and will obey commands.

M18
Disengage (turn off) all motors. They will not resist being moved and will ignore commands.

M20
List files on SD card (if any) to the serial connection.

M42 P[n1] S[n2]
Change digital pin number [n1] to state [n2] (0 for low voltage, 1 for high voltage)

M100
Print help message to the serial connection.

M101 A[n1] T[n2] B[n3]
Change software limits for axis [n1] to max/top [n2] and minimum/bottom [n3].  These limits are in millimeters.

M102
Print machine axis limits to the serial connection.

M110 N[n]
Change the next expected line number to number [n]. This means that the N[n] can appear twice on a single line with different values. The firmware should treat the first N as the current line number and the second N as the desired line number.

M114
Report the current state of the machine to the serial connection. This includes where it believes it is positioned, the feed rate, the acceleration, and possibly other values.

M117 [string]
Display messsage [string] on the LCD panel.

M226 P[n1] S[n2]
Wait for digital pin number [n1] to be in state [n2] (0 for off, 1 for on).
If P is not included, the default is the LCD button pin.
If S is not included, the default is off (0).

M300 P[n1] S[n2]
Play frequency [n2] for [n1] milliseconds through the LCD speaker.
If P is not included, the default is 250 (1/4s).
If S is not included, the default is 60 (a C note).
Some buzzers cannot play different frequencies.

G commands

G0 X[n1] Y[n2] Z[n3] U[n4] V[n5] W[n6] A[n7] F[n8]
G1 X[n1] Y[n2] Z[n3] U[n4] V[n5] W[n6] A[n7] F[n8]
Move the machine in a straight line to (X,Y,Z,U,V,W) at feedrate F and acceleration A.  All values are millimetres, except F and A which are in motor steps per second.  If any of these parameters are not included, the current value is used.

G2 X[n1] Y[n2] I[n3] J[n4] A[n5] F[n6]
G3 X[n1] Y[n2] I[n3] J[n4] A[n5] F[n6]
Move the machine in an an arc around a circle in the XY plane. The arc begins at the current position. The arc ends at (n1,n2).  The center of the circle of the arc is (n3,n4).  G2 is for clockwise arcs.  G3 is for counter-clockwise arcs.  If any of these values are not included, the value is unchanged.  I and J default to X and Y, respectively.

G4 P[n1] S[n2]
Dwell (wait) for n1 seconds and n2 milliseconds.

G28
Find home. This is different for each machine design. In general this means that the machine moves until it activates sensors to confirm its position. Many machines will refuse other G commands until G28 is completed. G28 is not run automatically when the robot turns for human safety.

G54 X[n1] Y[n2] Z[n3] U[n4] V[n5] W[n6]
Adjust tool offset for tool 0. This way the position the machine will report is the same as the position at the tip of the tool.

G55-G59
Adjust tool offset for tools 1-5. See G54 for more information.

G90
Absolute movement mode. All movement position values are relative to the machine’s frame of reference.

G91
Relative movement mode. All movement position values are relative to the current position.

G92 X[n1] Y[n2] Z[n3] U[n4] V[n5] W[n6]
Adjust the machine’s internal position value.

Non-standard Gcode Commands

The following commands are unique to Makelangelo firmware.  They will not appear in other machines.

UID [n]
Set the robot’s unique id number to [n].

D0 L[n1] R[n2] U[n3] V[n4] W[n5] T[n6]
Jog motors. Each letter represents a different motor. each [n] is a number of steps to move. the number of steps can be negative for a reverse jog. Not all machines have one motor per axis.

D4 [string]
Begin processing the file on the SD card called [string].

D5
Report firmware version number.

D6 X[n1] Y[n2] Z[n3] U[n4] V[n5] W[n6]
Set home position for each axis. When G28 completes the machine will teleport (G92) to this position.

D7 [Lnnn] [Rnnn]
Adjust Makelangelo calibration values for the length of left and right belts. Default is 1011mm.

D8
Report Makelangelo calibration values for left and right belts

D9
Save Makelangelo calibration values for left and right belts.

D10
Print hardware version number to the serial connection.

D11
Set up all default values for the Makelangelo 5.

D12
Assume the robot is as machine reference (absolute) position 0 on each axis. Move towards home switches and measure the distance. Then adjust D6 based on the measured distance.

D13
Adjust the pen angle on the Makelangelo. This is the same as a jog command. it is always absolute position in degrees.

D14
Report the machine style (kinematic model) to the serial connection.