Pages

Tuesday, October 3, 2017

OpenSCAD Essentials: Lists and list manipulation

Lists

This is the second part in a series on OpenSCAD (here is a link to the first). This series will not cover the very basics of OpenSCAD, there is plenty of good material on that, but it's for those who want to advance beyond these basics. This part is about Lists, sequences of OpenSCAD values. These are very important in OpenSCAD because they are the equivalent of arrays. They're also referred to as vectors. Lists can contain values numbers, booleans and strings. For most examples below version 2015.03 is required.

Quicksort example from the OpenSCAD User Manual demonstrates how to manipulate a list. The red row of cylinders represents the unsorted list while the green row represents the sorted list.

First let's create some lists by typing the following in the OpenSCAD editor.

lst = [1,2,3,4,5,6,7,8,9]; //a list of numbers
lst2 = ["red","yellow","blue"]; //a list of strings
lst3 = [false,true,undef]; //a list of booleans
echo(lst);
echo(lst2);
echo(lst3);

In OpenSCAD the user is able to automatically generate lists with for, if and let. A couple of examples

lst4 = [for (i = [0:9]) i*i];

This generates a list of [0, 1, 4, 9, 16, 25, 36, 49, 64, 81], so for every i the results is i multiplied by i. OpenSCAD even allows nested loops

lst5 = [for (i = [0:2]) for (j =[0:2]) for (n=[0:2]) [i,j,n]];

This generates a list of a long list of vectors ECHO: [[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], [0, 1, 1], [0, 1, 2], [0, 2, 0], [0, 2, 1], [0, 2, 2], [1, 0, 0], [1, 0, 1], [1, 0, 2], [1, 1, 0], [1, 1, 1], [1, 1, 2], [1, 2, 0], [1, 2, 1], [1, 2, 2], [2, 0, 0], [2, 0, 1], [2, 0, 2], [2, 1, 0], [2, 1, 1], [2, 1, 2], [2, 2, 0], [2, 2, 1], [2, 2, 2]]

Manipulate a list

A list in OpenSCAD can be manipulated with a function. Manipulation is needed in order to make changes to the list. Since OpenSCAD is a Functional Language we can't just for example change value in the list. We need a function to do this. Lets assume we want to create a partial list from an existing list. We then create the following function:

function partial(list,start,end) = [for (i = [start:end]) list[i]];
echo(partial(lst,1,8));

In the Console of OpenSCAD the following is displayed ECHO: [2, 3, 9, 5, 6, 7, 8, 4]. The function has three parameters: a list, a start number and an end number. The function iterates through the list and then creates a new list with the items with indices 1 to 8. Note that the first item in the list with index 0 has been excluded from the new list. Another example.

echo(partial(lst,4,7));

This prompts ECHO: [5,6,7,8], a list with the 4th to the 7th item of lst. Remember that lst itself hasn't changed (you can check this yourself). Next we write a function to remove a number in a list.

function remove_item(list,position) = [for (i = [0:len(list)-1]) if (i != position) list[i]];
echo(remove_value(lst,2));

This prompt ECHO: [1,2,4,5,6,7,8,9], the third item in the list has been removed. The function, that has the parameters list and position, loops through the items of the list and if the index i is not equal to the parameter position it is added to the new list. In this example it means that the item with the index 2 is not part of the new list. The following function inserts a value instead of removing it.

function insert(list,value,position) = let (l1 = partial(list,0,position-1), l2 = partial(list,position,len(list)-1)) concat(add_value(l1,value),l2);
echo(insert(lst,10,4));

It cuts the list on the position 4 in two new lists, inserts a number of 10 between the two lists, using the function partial that was created above. The result in the console is: ECHO: [1, 2, 3, 4, 10, 5, 6, 7, 8, 9]. BTW: It's entirely possible that there's an easier way to do this.

The last and most complicated example is taken from the OpenSCAD User Manual and implements the quicksort algorithm. The quicksort function uses an unsorted list as input and produces a sorted list with a recursive method.

lst6 = [1,4,6,7,3,5,7,2]; //a list of numbers
function quicksort(list) = !(len(list)>0) ? [] : let(
    pivot   = list[floor(len(list)/2)],
    lesser  = [ for (i = list) if (i  < pivot) i ],
    equal   = [ for (i = list) if (i == pivot) i ],
    greater = [ for (i = list) if (i  > pivot) i ]
) concat(
    quicksort(lesser), equal, quicksort(greater)
);

echo(lst6);
echo(quicksort(lst6));

It produces the following output ECHO: [1,2,3,4,5,6,7,7]. The function starts with a base case !(len(list)>0) ? [] meaning if a the length of list is 0 return an empty list (or the boolean value false) else continue. Next a pivot is determined being the number in the middle of the list, in this case the number 3. Next all numbers in the list smaller than 3 are placed in a list lesser, all numbers equal to 3 are placed in a list equal and all numbers larger than 3 are placed in a list greater. These three list are then concatenated to a new list however, and now we get to the recursive parts, the lesser and greater lists are the new parameters for quicksort. This iteration continues until the base case, the length of the list is equal to zero or less, is reached.

Understanding lists and list manipulation is essential to advance in OpenSCAD. Without it would for instance be near impossible to work with the all important vectors in both 2D and 3D. But also in other areas, such as iterating through a row, lists are essential.

Friday, September 15, 2017

OpenSCAD Essentials: Functional Programming

Functional programming

OpenSCAD is a 3D CAD program where the 3D model is created with a script. Although the basics are very simple there is a point on the learning curve where the OpenSCAD programming language can become confusing. OpenSCAD is a Functional Programming Language and behaves differently than most people, used to the Imperative Programming Paradigm, expect. With Imperative Programming a value can be assigned to a variable and later another value can be assigned to same variable later in the program. This changes the state of the program. This approach will not work in OpenSCAD.

Sierpinski triangle in OpenSCAD. A program I made earlier that demonstrates recursion in OpenSCAD.

An example

The easiest way to explain this is with a simple example. In this example we want the program to add the numbers 1 to 10 (1+2+3+4+5+6+7+8+9+10).

total = 0;

module count(number) {
    for (i = [1:number]) {
        total=total+i;
    }
    echo(total);
}

count(10);

You might expect that the answer is 55 however to your horror you'll find in OpenSCAD that it actually is 0. What happened? Instead of assigning a new value to the variable total for every interation, total keeps it's initial value. The program written in OpenSCAD is a mathematical expression that is evaluated by pressing F5. OpenSCAD supports powerful functional constructs to support these expressions. In the case of the example above we write.

function sum(number) = (number == 0 ? 0 : number + sum(number-1));

echo(sum(10));

The result of this program is 55 as intended but how does it work. We define a function (hence the name Functional Language) sum with a parameter number. On the right side of the function there is an evaluation, in pseudocode: if n == 0 then 0 else number + sum(number-1). Notice that the function is recursive so for example for number is 3 the function evaluates as follows

sum(3) = 3 + sum(2) = 3 + 2 + sum(1) = 3 + 2 + 1 = 6

Iteration in OpenSCAD is usually accomplished via recursion. If you are not used to recursion it's takes effort and practice to understand but it's often simple and elegant. Now let's do a somewhat more complicated example, the greatest common devisor (gcd). When we look at gcd algorithms on wikipedia we find the recursive Euclidian algorithm which is described as follows:

gcd(a,0) = a
gcd(a,b) = gcd(b, a mod b)

Even if we would not understand the algorithm it's easy to translate it to OpenSCAD

function gcd(a,b) = b == 0 ? a : gcd(b,a%b);

Where a%b means a modulo b (BTW: the syntax used in the functions above is similar to the programming language C). It's worthwhile as a training to find recursive algorithms and try to implement them in OpenSCAD.

Further reading

If you want to advance in OpenSCAD it is necessary to read the manual at some point. This discloses valuable information that is hard to find by just using the Cheatsheet or the watching YouTube videos. The user manual can be found here: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Language

Tuesday, August 22, 2017

Creating a Raspberry Pi Squeezebox server

Frustration with Spotify

In my last blog post I wrote how Spotify remotely deleted their app from my Squeezeboxes. This pissed me off and I wondered who was in control of the Squeezebox devices, me or some third party. It forced me to choose between the service mysqueezebox.com, that streams music from the internet, or set up the Squeezebox server on my network. The Squeezebox server software being free and open source software guarantees that I take control of my Squeezeboxes and the music that I'm streaming.

The're many hardware options for the Squeezebox server software (or Logitech Media Server) but I decided to use the Raspberry Pi 2 that I had lying around because of it's small footprint and massive support for the Pi. Within a couple of hours I had the Logitech Squeezebox Server up an running with my music playing.

Below I'll show the steps that need to be taken to set up the Logitech Squeezebox Server on the Raspberry Pi. I got some important steps from the Variax Firmation website but I tested everything.

Raspberry Pi 2 with a USB-harddrive connected to my network with an ethernet cable.


Step 1: Ripping CD's and writing them on an HDD (choosing an audio format)

Before setting up a server rip the CD's and write them to a HDD. For this project I used a small usb-harddrive that was also lying around. I formatted the HDD as FAT32. Next I collected all my music CD's and ripped them. As ripping software I used the Asunder software on my Tahrpup Linux system (6.0.5) but I guess that the're good alternatives. As audio format I choose mp3 for now. If I buy a larger drive I'll probably move to flac.

Step 2: Installing Raspbian Stretch Lite

Next I installed the just released Raspbian Strech Lite on the Raspberry Pi 2 by first downloading the image and writing the image to the micro SD card using these instructions.  I chose the lite version of Raspbian Stretch to make the server as lightweight as possible. I had no problems booting the Raspberry Pi and connecting it to my network using ethernet. To avoid potential problems connect the Pi to a monitor with an HDMI cable and make sure that it boots properly.

Step 3: Mount the HDD on the Raspberry Pi

Now with Raspbian Lite installed I mounted the HDD that we prepared in step 1 we need to login remotely and retrieve the name of the drive. To login remotely we need another computer (Windows, Linux or OSX). Open a terminal (or Putty in case of Windows) and type
sudo ssh pi@Raspberry_Pi_ip_address (in my case 192.168.178.69)
The password is raspberry. Then type.
sudo fdisk -l
Then look for the HDD that is just connected. This was /dev/sda1 in my case (but this may differ in yours). To make the files on the HDD accesible to the Raspberry Pi we need mount the drive. But before that we need to make a mounting point.
sudo mkdir /media/usb-drive
and mount
sudo mount /dev/sda1 /media/usb-drive
You should hear some HDD activity. Now type
cd /media/usb-drive ls -l
and a list of your music should be visible.
To unmount the HDD do
sudo umount /media/usb-drive

The list of folders with albums that I ripped. In this stage I only ripped a few albums to test the system.

Step 4: Installing Logitech Media Center on the Raspberry Pi

Before we need to install a library to play audio files.  Since you want mp3 only do:
sudo apt-get install -y libsox-fmt-all.
This installes SoX, a command line utility that, among other things, plays various types of audio files. If you want flac you'll probably need to do this
sudo apt-get install -y libsox-fmt-all
Now with the audio libraries in place the Logitech Media Software can be downloaded. We want the latest version which is 7.9.0.
wget -O logitechmediaserver_all.deb $(wget -q -O - "http://www.mysqueezebox.com/update/?version=7.9.0&revision=1&geturl=1&os=deb")
Now install the server software with:
sudo dpkg -i logitechmediaserver_all.deb
With all the software installed we're ready to go.

Step 5: Working with Logitech Media Center

To enter the web interface of the Logitech Media Center on the Pi we need a computer and a browser. In the browser url we type:
<your_Raspberry_Pi_ip_address:9000> (in my case 192.168.178.69:9000) 

The Logitech Media Center is started for the first time and a script is started to set it up. Important is that the mounting point of the HDD (in my case /media/usb-drive) is entered. This way the Media Center is able to retrieve all the music files.
sudo mkdir /media/usb-drive
The Logitech Media Server is now ready and every time the Raspberry Pi boots the Logitech Media Server is automatically started however the HDD is not automatically mounted so we need to change that. This is done in the fstab system configuration file that can be found at /etc/fstab
sudo nano fstab
and add the following line
/dev/sda1    /media/usb-drive     vfat&    defaults     0
Save the file and exit nano. Reboot the Raspberry Pi. Now the installation is complete. The music can be played from the web interface (see above), with the remote controls that come with the Squeezebox devices or with a free app for Android. The Squeezebox Radio also has controls on the device to play the music. So, plenty of options.

Webinterface of the Logitech Media Server.

What's next

To give the media server a permanent place a case is needed. Luckily I have a 3D printer and some 3D CAD skills so I'll create my own. I'll show the finished product in a future blog post. I also wonder if I could make this work on the C.H.I.P which would make the project even cheaper.
 

Wednesday, August 16, 2017

Spotify silently and remotely deleted app from Squeezebox devices

Spotify removed

Spotify silently and remotely deleted the Spotify app from the Squeezebox devices I found out after returning from vacation (Yes, they did post an article on their help pages I found out but I wasn't notified). I own a Squeezebox Radio and a Squeezebox Touch that I both use heavily to listen to music with the Spotify app. I even have a Spotify Premium Family account that enables me to play Spotify on multiple devices. I understand that support for hardware, such as the Squeezebox, is limited and will end some day. However what outrages me is that Spotify remotely removes software on a device that I own. Spotify makes it very clear that they control the software that they provide for the device and not the user. With that they control the functionality of my devices.

The Logitech Squeezebox Radio, one of several types of Squeezebox devices, now without Spotify (photo from https://en.wikipedia.org/wiki/Squeezebox_(network_music_player)).

The Squeezebox music player

The Squeezebox device is a network music player from Logitech. The devices are versatile and have excellent hardware. The Squeezebox can be used in two different ways; as a player that works with an online service, mysqueezebox.com, or with a private server called Logitech Media Server. I preferred to use my Squeezebox devices with the online service mainly for its ease of use and because some streaming services such as Spotify and Deezer are available as an app. The apps are very convenient to stream music just with a remote control. The Squeezebox hardware was discontinued in 2012 but  but, to it's credit, Logitech maintains the online service to this very day.

I didn't mention the best thing about Squeezebox, both the server and player software are non-proprietary and can be installed on several different operating systems. This enables all users to make build their own Squeezebox clients and servers.

Choices

With the removal of the Spotify app I have a choice to make. Will I continue to be using the my Squeezebox devices with the online server using Deezer instead of Spotify or will I set up my own private server? The latter is of course more of a hassle since it involves installing and maintaining the Logitech Media Server, ripping CD's to mp3's and storing them on the server. Nevertheless I may choose to create my own private server. I recently decided to move the software on my iMac from proprietary to non-proprietary so creating the private server for my Squeezebox would also make sense. I happen to have a Raspberry Pi 2 lying around and in the coming weeks I'll try to set it up as a Logitech Media Server.


Thursday, August 10, 2017

Replacing Contacts, Calender and Mail with Thunderbird on OSX

Introduction

I prevously wrote about the reason why I prefer open source software above proprietary software for my DIY projects. This time I'd like to take it a step further by replacing all proprietary software on my iMac with open source or non-proprietary software. This is of course not possible because the OSX operating system itself is proprietary but I'm replacing as much programs on it as I can. This will ultimately make it easier to move to the Linux operating system.

Thunderbird the replacement for Contacts, Calender and Mail on OSX.

 

Thunderbird

Today I replaced the proprietary Contacts, Calender and Mail, that come with OSX, with Mozilla's Thunderbird (v52.2.1). Most important are my mails. I have three email accounts from which two migrated without program. Not suprisingly I was unable to migrate my iCloud account. Not a huge problem since I want to abandon this account anyway.

All contacts from OSX Contacts were imported during the installation of Thunderbird. Next I exported my Calender data as an .ics file. Thunderbird imported this file without problem (Events and Tasks/Import).

After just a couple of hours of usage I can't provide a opinion of the Thunderbird software but that is not relevant. Even if Thunderbird would lack certain features or the user interface is not as pretty as the other programs the more important point is that I gained more freedom today. I unchained myself from three proprietary OSX programs and I'm slowly freeing myself from the Apple ecosystem.

Further down the road

Next on the agenda are Quicktime and iMovie which I use extensively for my video tutorials. Possible candidates are OpenShot and Kdenlive. I've heard that both programs can have stability issues so that may be a challenge also the Kdenlive version of OSX is outdated according the Kdenlive website.

Even further down the road is OSX Photos. I have thousands of photo's and as you can imagine they're precious to me. Finding an non-proprietary image organizer for OSX is not easy. My son uses Shotwell on his Ubuntu system but I haven't found an OSX version of it. Another alternative is digiKam. This program appears to be maintained very well so I might want to give it a try.

Any comments on migrating from proprietary to non-proprietary programs are welcome as well as any comments on proprietary versus non-proprietary in general. In the future I'll regularly write an update on how I'm faring with the transition.

 

Tuesday, June 20, 2017

Line and Polyline modules in OpenSCAD

Polygon in OpenSCAD

The polygon function of OpenSCAD is very cool but for some applications it just doesn’t cut it. For instance when polygon is used to draw a Starpolygon, a self intersecting polygon, the space between the polygon lines is filled, an undesired effect that cannot be negated. In case of the Starpolygon we just want to see the lines.

Line and Polyline

Luckily we can create our own polygon module in OpenSCAD that meets our needs. However before we can create a polygon we need to be able to create a single line. Also a single line can be created in OpenSCAD, actually in several ways. In the YouTube video below I'll show one such approach.

A Vase created with line, polyline and rotate_extrude in OpenSCAD.

Furthermore I’ll show how to create a line module and a polygon module that fit our needs but it doesn’t stop there. With the line and polyline a complete 2D-library in OpenSCAD can be created. Also, in combination with linear_extrude and rotate_extrude 3D objects can be created that are impossible to create with CSG alone.



In a next blogpost I’ll show you how to create a Bezier Spline with line and polyline. This Bezier Spline can then be used to create a smooth curve needed in a 3d printable vase for example.

I want to mention Justin Lin here. He has a great website www.openhome.cc where he, not only shares his OpenSCAD work, but also explains it in great detail. I think it’s a very useful resource for OpenSCAD.

The file of this video can be downloaded with the following link: https://drive.google.com/open?id=0Bwg0RBbuN0fMVkJISXdDQV9sekE

OpenSCAD is open source (GPLv2 license) and is well maintained by Marius Kintel et al. Besides the stable releases for Windows, OSX and Linux, development snapshots are available. I recommend using these development snapshots since they have all the latest features. 

Friday, June 2, 2017

Cubic Bezier Spline tool in Solvespace

Cubic Bezier Spline tool 

The Cubic Bezier Spline tool in Solvespace can be used to create shapes that are hard or impossible to accomplish with other tools such as straight lines. A Cubic Bezier Spline is a line segment that can be controlled with four points. By changing the position of these points the curve can be manipulated or smoothened.

By clicking with the left mouse button in the graphics window the new control points can be added to the Cubic Bezier Spline tool to a maximum of 12. This is essentially a Composite Bezier Curve or polybezier. The problem with these curves is that it is hard to keep the curve smooth.

Polybezier with eight control points fully constrained approximating a circle


a classic example of shapes that benefit from Cubic Bezier Splines are Vases. Vases also appear to be popular with 3d printers. In the video below I'll demonstrate how to create a Vase in Solvespace using the Cubic Bezier Spline tool.

At the end of the Vase video I’ll demonstrate two often overlooked functions in Solvespace, the Length Ratio constraint and the Length Difference constraint I’ll also show how to apply the Length Ratio constraint to two circles.


The Solvespace Vase file from this tutorial can be downloaded here.

For this tutorial I used Solvespace 2.3 on OSX.

Solvespace is open source (GPLv3 license) and is available for Window, OSX and Linux. It is developed by Jonathan Westhues and maintained by Whitequark and others. It can be downloaded here: http://solvespace.com/download.pl