Bringing software to the physical world

Leslie Wittig Quintanilla
Klarna Engineering
Published in
5 min readJun 29, 2017

--

Last year I bought a 3D printer and it gave me a whole new world to explore. New ideas just came flooding into my mind!

I started by downloading some models ready to print, then I played around with different software to customise the models. Soon I was creating my own models and discovering that writing functions for the models is even more fun.

In March I accepted an invitation to be a coach at the ClojureBridge meetup Stockholm that would be hosted by Klarna. I didn’t know Clojure yet but after some classes, I thought: can I design 3D models with Clojure?

After some research I found a blog post about the scad-clj library.. In a nutshell, scad-clj is a library that translates Clojure code into the OpenSCAD scripting language, which allows you to use the OpenSCAD software to view the model generated from the script. The concept is simple enough to start playing around and still be able to see the final results before printing.

I started out with basic shapes and learned how to manipulate their properties to generate different forms. I was very excited and shared my early experiences with Mariana and Cristina, two other engineers at Klarna who knew Clojure. They suggested getting together and doing a mob programming session to explore 3D modelling together. We grabbed a room the next day after work and started coding!

We wanted to work on something fun and instantly recognisable, so we chose the head of the world’s most famous mouse, Mickey! Roughly three spheres later, we had produced a fairly good model of Mr. Mouse.

For something a bit more challenging, we decided to try to combine many spheres to make a heart shape. This required us to search our brains for bits and pieces of almost forgotten trigonometry, then search Google for the requisite equations.

Source: http://mathworld.wolfram.com/HeartCurve.html

Once we understood the equations, it was fairly straightforward to translate them into Clojure code, which we then executed to produce models that could be viewed in OpenSCAD.

Let me take a moment to explain this code a little bit for those who aren’t too familiar with Clojure.

We’re defining a function here named heart-coords which takes no arguments and returns a list of spheres that will describe our heart shape once translated into the OpenSCAD language.

We construct this list by using Clojure’s for macro (list comprehension), which iterates through the numbers between negative pi and positive pi, 0.01 apart (e.g. -3.14, -3.13, -3.12, … 3.12, 3.13), binding each value to the symbol t within the body of the for. The first thing inside our for is a let form, which creates a lexical scope by taking a vector of pairs where the first element is a symbol and the second element is a value which will be bound to that symbol in the body of the let. Think of these as local variables.

Our first order of business is to define the x, y, and z coordinates of a three-dimensional point which will be the centre of our sphere, using the formula from Wolfram Alpha:

  • x = 16 sin3 t (remember, t is one of the numbers between -π and π)
  • y = 13 cos t — 5 cos (2 t) — 2 cos (3 t) — cos (4 t)
  • z = 0

We then generate a red, green, and blue value for our colour:

  • r = sin ((t + 3) / 6.1 * π)
  • g = 0
  • b = 0

From the above, maybe you can start to see why Lisp programmers love prefix notation so much!

Finally, we use the scad-clj union function to create an OpenSCAD union of a sphere with a radius of 2, our colour, and our 3D point. And then we move on to the next value in the series from -π and π.

Running the output of this function through scad-clj’s translation generates an OpenSCAD script that produces this beautiful model:

The next step was to bring our code all the way into the physical world. I took the models home and fed them to my PLA printer.

My printer uses the Fused Filament Fabrication (FFF) concept, which means that it lays melted plastic layers. Each layer is printed upon another so, if the model has a section to print that has nothing below, the plastic will be melted into the air and it is going to be a mess or a modern piece of art. To solve that, some printer software have a tool to help you draw support structures, like some bars to sustain the layers, before sending the file to the printer. Also those supports need to be strong enough to resist until the end of the process. If one uses supports, they have to be removed after the printing process and the model will not look as good.

Having that said, before sending Mickey’s head to print, I had to split the model in half and print each part with the largest surface down. Luckily, the heart didn’t need any special preparation.

“But wait!” you might be asking yourself. “Do I need to have a 3D printer in order to bring my software to life?”

Luckily, the answer is “no”. Here in Stockholm, you can take your models to Kulturhuset in Sergels Torg or the Stockholm Makerspace, and use their printers! If you are not in Stockholm, no problem. Check if there’s a Hacker Space or a Maker Space in your area, or try your local library.

And what’s so great about bringing code into the physical world? Well, if you’re anything like me, the whole process from the beginnings of an idea, to writing it in code, to visualising the model, and then finally printing it makes you feel proud. You can build something as simple as a toy or as complicated as a custom keyboard. The possibilities are endless. Having the power to make your ideas a physical reality is exciting and fun! Why not try it out for yourself?

The results of this project were presented as a lightning talk at ClojureBridge meetup Stockholm to show that coding is fun and it can even bring your imagination to the physical world.

Thank you Mariana, Cristina, Josh and Fabricio, my husband, for your help, review and support.

--

--