Wishful Coding

Didn't you ever wish your computer understood you?

Fast PyCAM on Mac using Homebrew

Ever since I started working on my CNC mill, I have been looking for decent software to generate G-code for it.

I wrote a G-code interpreter for my machine, after I found out LinuxCNC doesn’t support my type of LPT stepper driver, but finding good software to turn a 3D model into G-code turned out to be hard.

I went down this list, but most of them where hard to install or impossible to configure. In 2D I had some luck with the Inkscape plugin, but I was lost for 3D.

With the mill in a working state, and nothing to mill, I decided to push forward with PyCAM. I skipped PyCAM previously, because of its dependencies, which weren’t all available in Homebrew.

Some experimentation revealed a few important things:

  • It runs okay on my Ubuntu netbook.
  • Toolpath calculations take a looong time.
  • Its CLI runs without OpenGL and GTK+
  • It supports multiple and distributed processors.

After I cancelled the Ubuntu calculations, I ran it headless on my Mac. While somewhat faster, it still took a long time to generate G-code.

The real speedup came later, when I found PyCAM supports Psyco. But rather than messing with Psyco, I followed the advice on the Psyco homepage, and found the headless PyCAM runs great, and very fast, on PyPy.

Psyco is unmaintained and dead. Please look at PyPy for the state-of-the-art in JIT compilers for Python.

After successfully running PyCAM headless, I finally put my teeth in the dependencies. Installation steps using Homebrew follow:

  1. Download PyCAM.
  2. Apply my patch to make sure it finds pygtk.
  3. brew install pygtk
  4. sudo easy_install PyOpenGL
  5. Wait for my pygtkglext Formula to be merged, or get it from my branch.
  6. brew install pygtkglext or brew install https://raw.github.com/pepijndevos/homebrew/master/Library/Formula/pygtkglext.rb

Now you should be able to just run ./pycam to see the GUI pop up. However, I use the following 2 commands to run a server on PyPy, for extra speed.

pypy pycam --start-server-only --server-auth-key=fietspomp --number-of-processes=4

python pycam --enable-server --remote-server=localhost --server-auth-key=fietspomp --number-of-processes=0

I’ll blog about the mill itself later. This morning I tried to mill a small sample project, but the mill is very inaccurate and jammed after a few layers.

Pepijn de Vos

Crazy Seq

crazy-seq, the solution to all your head-holding induced memory problems.

(defn crazy-seq [head f]
  (reify
    clojure.lang.ISeq
    (first [_] head)
    (more [_] (crazy-seq (f head) f))
    (next [this] (rest this))
    (seq [this] this)
    (equiv [_ _] false)))

(def s (crazy-seq 0 inc)) ; hold head
(dorun (take 100000000 s)) ; low fat
(nth s 10) ; recomputes
;=> 10

A normal lazy seq is made up of a car and a function that returns the cdr, which is then cached.

This means that if you store the first item and walk to the millionth, it will keep all these million items in memory.

What crazy-seq does, is that it simply does not cache the value. So you can walk to the millionth item, and it will only store the first and the current, the others are GC’d.

This means that walking twice will cost more CPU cycles, and it means that you get in trouble if the fn is not side-effect free.

Have fun.

Pepijn de Vos

Sciento CS-113

After my success with the Cyber arm, I had to try my luck at the Sciento one.

This time I was out of luck with Dutch or English documentation, but my brother found a Spanish PDF, from which I was able to get the needed information using Google Translate.

Unlike the Cyber, the Sciento is controlled by sending actual ASCII commands, rather than stepping the motors directly.

It is a lot smarter than the other arm, as it can remember its position. A list of commands:

  • ”Z” Sets the reference point
  • ”N” Go to the reference position
  • ”C”/”O” Close/open the claw
  • “M100,0,-250,0,0,0” Move the motors for the specified steps.
  • “H7” Save the current position in the specified register(1-100)
  • “P5,500,200,-5,0,0,0” Save the specified position in the specified register(first number, 1-100)
  • “G1” Move to the location in the specified register
  • “S5” Set the motor speed(1-5)
  • “D1” Wait for x seconds. Useless?
  • “L1” Check the limits of the motors. Recommended for testing, off by default.

Note that numbers are represented as ASCII text and that commands are upper-case. All commands are terminated by a carriage return.

A downside to this arm is that in comparison to the one-byte commands for the Cyber, it takes ages to send an ASCII command across, making this arm unsuitable for smooth control with a gamepad.

I programmed a coarse and ugly controller, with buttons for storing positions and moving to them. In the video I’m just stepping through the motions I stored earlier.

The controller is a real mess, based on the Cyber code, and not very interesting.

The code for the arm itself is nice though. Note the use of the actual strobe line, as opposed to the strobe bit in the Cyber code.

import parallel
from time import sleep

p = parallel.Parallel()

def write(s):
    bs = map(ord, s)
    bs.append(13)
    for b in bs:
        p.setData(b)
        p.setDataStrobe(1)
        p.setDataStrobe(0)
        sleep(0.001)

def command(cmd, *params):
    write(cmd+",".join(map(str, params)))

def move(m1=0, m2=0, m3=0, m4=0, m5=0, m6=0):
    "Move a number of steps"
    command("M", m1, m2, m3, m4, m5, m6)

def here(n):
    "Save current position in slot n 1-100"
    command("H", n)

def position(n, m1, m2, m3, m4, m5, m6):
    "Save the specified position in slot n 1-100"
    command("P", n, m1, m2, m3, m4, m5, m6)

def zero():
    "Set the reference point"
    command("Z")

def goto(n):
    "Go to position in slot n 1-100"
    command("G", n)

def home():
    "Return to the reference"
    command("N")

def open():
    "open the claw"
    command("O")

def close():
    "Close the claw"
    command("C")

def speed(n):
    "Set the speed 1-5"
    command("S", n)

def delay(n):
    "Wait for n seconds"
    command("D", n)

def limit(b):
    "enable/disable limits"
    commands("L", b)
Pepijn de Vos