Fighting with computers

Computers are not always friendly.

Tuesday, September 19, 2017

Raspberry Pi is teaching me new tricks

For an upcoming Art project, I needed to make a few things using a Raspberry Pi 3. And while I have not yet figured out a neat way of setting the wifi configuration wirelessly (as they did for ESPlink), I have made some progress on other fronts needed and configuration-related.

First thing on the list was to make the RPi3 work with a 3.5" color LCD with touchscreen.  It was simple once I followed the right set of instructions.  Not sure if the display can be kept on while using the HDMI output but guessing no I removed the HDMI output for all my testing. This LCD display is 480x320 resolution and it can work as a minimum display for both text console and X11. You just do not want to browse the web with that.

While the display worked nicely, I wanted it to be kept working all the time but the power saving settings disabled it after being idle for a while. A bit of googling led me to a nice solution: to add the line xserver-command=X -s 0 dpms to /etc/lightdm/lightdm.conf. And once reboot later the display is on all the time. Good.

I also wanted to show a fixed image on the display once the system was on without windows decorations of a mouse pointer. So I created a simple Java program that handles the first part and used this solution for taking care of the second one.

Another part of the job is to figure out the IPv4 addresses of the RPi3 once it has successfully connected to a wired or wireless network (so I can connect to it as I have open the ssh service). For that I used a simple python program I wrote. The code will broadcast (using UDP) the network configuration of Ethernet and Wifi interfaces on the RPi3. If you have a laptop sharing the same network you can receive it using the conmmand nc -w 1 -ul 55555  



Monday, August 21, 2017

Reading local files on HTML5

I wanted to perform some calculations over STL files. I thought it would be nice to be able to do that within a modern browser so it would work on any computer without the need to install any binary. But I have never dealt before with reading local files using Javascript within an HTML page.

I was sure it had to be a way as sites like gcode.ws  or chilipeppr.com allow you to either select a local file to be analyzed or to just drag and drop it to be sent to a CNC machine. But my experience with Javascript is quite limited and I have never found myself at ease with that language (though it is mostly the mess of Javascript + HTML + server extensions + server database what makes usually programmer's life a living hell).

But given it was a long time without venturing myself into the intricacies of the File API I decided I would learn a new trick. Most of the trouble is the asynchronous behavior of the system that took me a while to understand.

In many languages, you open a file and then you read from it and then you close it. Using the new FileReader() class of HTML5 you can do it all at once, even if your file is several megabytes long. However, reading your file takes time, so they use an asynchronous design here to prevent long blocking calls that would make your browser unresponsive. So instead of your code waiting for a potentially long-ish call, a certain callback function will be called once the read operation is over.

This mode of operation means that whatever you want to do with the file contents cannot be placed just next to the file read operation, as it may not have finished at all when that line is processed.  Associated with FileReader class there are certain events that signal different moments of the read operation. The FileReader.onLoad event handler is what we are interested in for signaling our file has been entirely read.

A second event handler is needed so we can pass the filename selected to the FileReader to perform the actual reading. For security reasons, we cannot hardcode a filename to read.  The code above can be found here and it will allow you to select a text file that will later be shown below the button.

Files can be read in different ways, as Text, as DataURL or as a byteArray depending on your needs and the type of file.  For my needs, I used the byteArray option that made me more or less easy to parse the STL. I use the base of the parseBinary function from Three.js and in the process, I discovered an error that reported to the project's git. However, the solution I suggested breaks something too.

Sunday, July 30, 2017

Spanish Stick Font for your project

I have a project where I need to draw some text on a plotter and while I could just use a vectorized Truetype font, that usually comes with the undesired effect of doubling the printing time plus leaving a white background, as the font strokes are made of two different lines.


A stick font consists of a single line. Just googling around I found this program. That happens to include a set of font files that were easy to parse (as they were text files).  This is the font I selected for my purposes:


Originally it did not include the full set of Spanish characters but that was easy to fix, as you can already see them on the picture above. Plus I realized some of the existing characters could be optimized so drawing time could be reduced. 

Maybe there is a better way as I have a very limited knowledge about font standards. But this approach makes sense to me. Now I just need to create a simple script to go from text to SVG or G-CODE. 

Update: I just put my code where my mouth was.

Saturday, July 08, 2017

Measuring weight with Arduino

While there are different solutions to achieve weight measuring, it seems the cheapest and neatest way is to use the HX711 amplifier and A/D converter.


Of course, a test weight is needed for you to be able to set the scale right. In my case that is going to be done by that idler that totals 11.30 grams. My sample load cell is a 100 grams unit so it is not that bad, but for a larger end scale, you want to have a test weight at least 10% of end of scale value.

What HX711 does is two things, on one hand, it is a precision low-noise amplifier for the load cell signal and on the other hand, it performs a 24-bit analog to digital conversion (much higher resolution than the 10-bit resolution of the Arduino built-in analog inputs).
Once data is converted to digital format, Arduino can read it from the HX711 using a two-wire serial interface. It basically streams the 24-bit value on one pin while the other provides the clock signal for the transfer. There are several Arduino libraries for that.
You want to wire the HX711 board (I used one I bought off eBay) to be close to the load cell. Example code is very simple and just provide you a numerical value that represents the digital number provided by the HX711 representing the current measurement.

My load cell is measuring around 106088 units (after I have divided by 100 the measured value to filter out most of the noise). And once I place the test weight on the load cell I get the measurement of 104635 units. That means the difference, that is 1.453 units, represents the weight of my test weight. Given that I know its real weight is 11.30 grams, that means there are 128.58 units per gram.

So, to measure in grams any other weight what I do is
grams = ( 106088 - measurement )/ 128.58;

Given my setup, each weight I measure is giving me a smaller number than the one I get when the scale is empty. If yours is the opposite, just invert the two terms (the zero-value and the measurement) or just consider the magnitude and not the sign of the value. 



Please note that even though I have divided the measured values by one hundred, there is still some jitter on the measured value. For better accuracy, you may want to average several measurements together.

Wednesday, June 28, 2017

Of cars and goats

Some days ago, while listening to an exam exercise, I learned about an old story that happened in the early nighties. It seems it stirred quite a controversy at the time. It was about a TV show where contestant was offered to chose one door out of three to win a car. The setup was that only behind one of the doors a car was present while the two others contained a goat. Apparently, most people value the car as the good prize but could not care less about the goat, that was the losing item.


The host would open a door different than the one the contestant chose to reveal, invariably a goat. And later the host will offer the contestant to switch to the other remaining door. The controversy was about what was the best choice the contestant could do: to switch or to keep the original choice.

We all agree that, initially, the contestant have one of out of three chances to win the car. However, when the host opens one of the remaining (non chosen) doors showing a goat it is not so clear what the chances are. Many people would think that given the fact that only two doors remain closed and one of them contains, necesarily, a car, then contestant has a 50/50 chance to win now. If that were true, swiching or staying put would make no difference.

However, the recomendation by Marilyn vos Savant in the article linked above was to "always swicth" as a more beneficial policy, which many people failed to understand.

So after my exam, I decided to read the above column and to figure that by myself. The way I shown to myself how the logic of this problem works as follows: Let us assume contestant has chosen the left door.

This selection creates a divide between the chosen door and the other two remaining doors. It is easy to see than the chances of the car to be in the chosen door is 1/3 and the chances of the car to be on the other remaining two doors is 2/3 (the picture shows one of these latter cases).

And what are the implications of the contestant keeping his original choice? Well, he will win in 1/3 of the cases, most people will easily agree on that too.

So the last question is, what are the implications of always switching once the host has shown us a door with a goat? Well, we are then selection the box in the right of the image, the one that has a 2/3 choice of having the car, that we know from a fact the car is not on the open door, so it will be on the closed door with a choice of 2/3.

Therefore, as odd as it might seem, changing to the other remaining door when the host shows us one door containing a goat is the best course of action to maximize our chances to win. That said, we might lose the game if our initial door choice was right, but that will only happen 1/3 of the times. So always switching will grant us a 2/3 chance of winning.

Saturday, June 10, 2017

Hello Clipper, bye JTS

Polygon clipping and polygon offsetting are operations that due to the multitud of cases possible are quite a difficult beast to tame. That is why I have used an external library when I have needed such a feature in a program.

A few days ago, researching for a student's assignment, I learned that "clipper" library had been migrated to Java by Tobias Mahlmann.  I had a minor trouble with one file not using UTF-8 encoding but other than that it compiled and run beautifully (not in my large display though as I am using an scale factor larger than one that messes the Java GUI layout).


Another interesting detail is that the code will need Java 1.8 to compile as it uses a Lambda function in one of the Comparators used. However, I wanted to use it with some older code that was using 1.6 and the compiler was not happy with my version request, so I needed to do a small change to get rid of the Lamba function code in favor of an inner class. 

Once the code was working, I could use it with some code developed with Processing 2.21 and it would work ok.  Now I have to do some tests but my first impression is that Clipper is way faster than Java Topology Suite.

Update: After some tests it seems Clipper is not as robust as JTS, so I am not going to make any change to my software after all.

Thursday, June 08, 2017

OpenSCAD kept crashing on Ubuntu 16.04 with AMD drivers

After the upgrade of my computer's graphics card for a new AMD RX 460 I noticed OpenSCAD program was crashing all the time with a segmentation fault (every time I pressed F5 or F6). But the problem would only happen if I was using AMD native driver (amdgpu-pro). I saw no references online to this specific problem which was weird.

I decided to buy the RX 460 as it apparently had good Linux support, so it was odd to have this kind of problem but no matter what version of the driver I was using or what version of OpenSCAD nightly build I could not use the application. I managed to get an AppImage version that worked without a problem in my system and that is what I have been using for a while.

Today, I have decided to check if somehow problem could he related to my locale, so I invoked the application like this: LC_ALL=C openscad-nightly

And lo and behold, OpenSCAD is working as it should, no problem whatsoever. So it is clear that my Spanish locale was what caused the problem. I wish I have thought sooner about making this simple test.

Update: Oops, there is still something else, as csg example still crashes the program (time to try a stable release now).

Update2: Well, stable release is a compelte no-go.

Update3: When you discover that the crashes happening from command line are gone if program is run from gdb.  WTF? It turns out it is a "Heisenbug" (time-sensitive bug)