Category: Programming

Microcontroller fun

The Arduino has a huge hobbyist-level codebase and lots of libraries for talking to various devices.

The 8051 is a venerable old processor that still gets used in lots of stuff because it’s cheap, and has a low gate count.

It’s probably possible to port a lot of the Arduino stuff (everything that doesn’t use specific on-chip features) to the 8051, thus allowing people to use the software environment they are comfortable with on a new chip. The same is likely true of PICs, and other chips.

The general case, then, is to create a translation system that automates, as much as possible, the process of porting the Arduino libraries and environment from one chip to another. This is, at a high level, possible because anything a computer with a turing-complete instruction set can do, any other computer with a turing-complete instruction set can also do. The hang-up would be on limitations of real hardware (there’s a lot of cool stuff in there, but no infinite data/instruction tape).

Pebbles and antlers

I have a bunch of ideas for little applications.

One is a game-like interactive entertainment where you contemplate a pebble. You can turn it around, zoom into its surface, etc. That’s it, just examining a pebble. The trick is, this is actually technically somewhat complex. Pebbles have structure ranging from overall shape to near-microscopic scratches and dents. To faithfully render the pebble, all of that has to show up as you look around. To save processor time and storage space, I plan to generate all of that on the fly. I’ve been advised that I should either use the Unity game engine, or write my own engine to do this.

Pebble is a proof of concept, really. The original idea was to write a game where you examine a tree. You can fly all around it, zoom into the cracks in the bark until they tower over you like canyons, zoom out until the tree is a green spot below you. There is no way to sit down and hand-make all of that data, so it pretty much has to be generated. Pebble seems like something I can do to determine that the concept is possible.

(I’m not sure if my interest in generative content is an indication that I’m into elegant, intelligent solutions, or just that I’m profoundly lazy.)

Another application is a piece of software that generates antlers. You run it, and it spits out an STL file that you can use to get the antlers 3D printed. You can modify things like how the antlers branch and curve, to get things like ibex horns or deer antlers.

(Oddly, thinking about these programs, I realize that I don’t know what trees, pebbles, or antlers look like. I mean, I can identify them, and even sort of describe them, but not with anything like the sort of description required to produce them. This is, I think, the difference between looking at things like an artist and looking at them like someone who just doesn’t want to bump into stuff.)

Closure(s)

About 10 years ago, 10.58 if you want to get really precise, I took the class CS2135 Programming Language Concepts. I remember being really impressed with a lot of the power and expressiveness of functional programming.

This evening, I was working on a little goofy project, and it turned out that doing it with a closure was the clean and useful way to go. I find it amusing that when I finally got around to using these techniques, it was just kind of a natural thing.

The (half done) code is all over here.

Drinking with Robots

I am building a drink-dispensing robot. It has 5 pumps internally, so I want to find the set of five liquids that will produce the largest variety of mixed drinks. To do this, I’m going to need a huge set of drink recipes. I got a bunch from a cocktail database that esquire maintains, but the biggest list I’m aware of is The Webtender. Unfortunately, that database isn’t in a form that permits me to make queries to find out what is the maximal set of drinks that I can make, given the constraint of 5 liquid ingredients. So instead of the web interface, I want the raw data.

This means I want to download every single drink recipe from The Webtender. The URLs there are of the form http://www.webtender.com/db/drink/6217, so the obvious thing to do would be to write up a little bash oneliner that just wgets each of the files in turn. It would probably look something like this:

for (( i=1; i <= 6217; i++ )); do wget http://www.webtender.com/db/drink/$i; done 

But it may be that The Webtender issues a 403 Forbidden error if you use wget, probably to prevent just this sort of hijinks. Unfortunately for them, wget can be configured to claim to be something else. These instructions provide the config file to cause wget to claim to be Mozilla on Windows NT, which The Webtender should have no problem with.

For my next trick, I'll use BeautifulSoup to turn the HTML files from the webtender and Esquire into a SQL database, and probably perform some form of normalization on the data, such as making all the measurements be in the same units.

Coloring cells based on cell value in OpenOffice

OpenOffice (probably LibreOffice too) has limited functionality for setting the background color of a cell. I wrote a little macro that colors each cell blue, with saturation depending on the range of the value. This is in OOBasic, but it demonstrates the general idea, and the proper way of setting cell background colors.

Sub Main

End Sub

sub ColorCells

dim ii
dim jj
dim r
dim g
dim b
dim doc as object
dim sheet as object
dim cell as object

doc = ThisComponent
sheet = doc.Sheets.getByName("Sheet1")

for ii = 0 to 5
	for jj = 0 to 626
		cell = sheet.getCellByPosition(ii, jj)
		r = 255 - (cell.Value * 255)65536
		g = 255 - (cell.Value * 255)65536
		b = 255
		cell.CellBackColor = RGB(r,g,b)
	next jj
next ii

end sub

CSS: Turing complete?

“The simple selector matches if all of its components match. ” according to the W3.org page on selectors. That sounds to me like an AND gate, in that something is selected if the logical AND of its components match.

Combine that with logical inversion, provided by the not selector, and it seems to me that you get NAND, which is a universal gate. Any boolean function can be composed of NAND gates, so it is in theory possible to compose an entire CPU out of NAND gates.

This makes it seem to me like you could write a processor simulation in CSS.

The main thing that I wonder about is how browsers evaluate CSS, because if it is not re-evaluated until nothing happens (e.g. nothing gets its attributes changed) then the CPU wouldn’t work because the “output” of a selection would not be able to be used as input into more selections (e.g. by changing their classes). Heck, I’m not sure you can even have CSS change the classes of an element on the fly.

But if you can…

Eclipse Unresolved Imports with ROS on Ubuntu

I use Eclipse as an IDE. I do this to get code completion and syntax highlighting, as it does those pretty well for C/C++. Recently, I was trying to write a ROS plugin, and Eclipse couldn’t find the ros.h header file, which I had included with the line #include "ros/ros.h". Because of this, it marked every line where there was any use of anything from that header as an error, and gave a warning on the include line. They were not errors, and the code still built fine, but the IDE displayed it wrong. To fix this, I tried the normal solution, which is:

Click Project -> Properties -> C/C++ General -> Paths and Symbols -> Includes -> GNU C++ and then use the Add button to add the path to the include file. After that, reindex the project by right-clicking on the project, clicking Index -> Rebuild, and the include should be resolved.

Unfortunately, it wasn’t. Restarting Eclipse didn’t pick it up.

These instructions suggested that it might be something wrong with my language settings, but that doesn’t work either.

This is the proper way of doing it, and it actually works.

Now my tools are out of the way and I can start work. At 5:00PM on Friday.

My new favorite way of commenting out code blocks

/* Debugging, print out the array
for (int ii = 0; ii < sizeof(data); ii++) {
  if ((ii % 8) == 0) {
    cout << endl;
  }
  printf("%02X ", ((unsigned char *) data)[ii]);
}
cout << endl;
//*/

That last line is a single line comment that closes a multi-line comment. By adding or removing "*/" to the comment that describes the block, I can comment out the entire block. No more hunting for the proper point to close it at, just adding or removing the comment closure to the top line.

Of course, this doesn't work if there's a multi-line comment inside the code block.

Touch and pressure sensing with the Arduino

A friend of mine is working on a device that requires multiple soft fabric pressure sensors over a volume approximately equivalent to a human arm. I have more Arduino experience than her, so I’m helping out with the electronic implementation details.

Our first attempt was to examine some commercial force-sensing resistors. These consist of a plastic layer with a pattern of interleaved contacts printed on it, and a layer of carbon-impregnated rubbery material over the contacts. The more pressure there is on the rubber, the more it touches the contacts and the lower its resistance becomes. These devices are very stable, yielding repeatable resistance measurements with repeated contacts. Unfortunately, they are of a fixed size, and cannot be cut or reshaped. They also are flexible, but not exactly soft.

Back of a set of force sensitive resistors, showing contacts

After discarding that approach, we tried making our own force sensitive resistors out of the conductive foam from IC packaging. This works, but has a couple of problems. The first is that the resulting device doesn’t have a simple response to force. Its resistance goes down when pressed, and goes back up when released, but it doesn’t always return to the same values, and the resulting sensor data is noisy. We also don’t have a good source for a lot of IC packaging foam.

Most recently, we’ve tried making a pressure sensor based on a capacitor. The Arduino CapSense library provides a simple way to turn two pins of an Arduino into a capacitance sensor. One plate of the capacitor is a sheet of conductive material, the other plate is the user, and is effectively connected to ground (or at least “away” as charges can leave the circuit that way). The page notes that you can use the capacitive sensing pad, covered with an insulator, as a pressure sensor with an approximately logarithmic response.

However, it also notes that putting a ground plane under the touch sensor makes the results more stable. Instead of doing that, I put down the sensing plate, made of copper-coated nylon, two layers of soft interfacing (A sewing material kind of like a sheet of stuffed toy stuffing) and a ground plate made of silver-coated spandex over the interfacing. Pressing on the ground pad compresses the interfacing and brings the ground plate closer to the sensing plate, increasing the capacitance, and registering as pressure to the sensor. Because the upper/interactive surface is the ground plate, it shields the sensing plate, so the capacitive pressure sensor does not also act as a proximity sensor and trigger before it is touched.

I taped the bottom layer down to my desk, and hooked a clip lead to it. This lead goes to pin 2 of my Arduino.
Bottom layer of fabric pressure sensor
Then I put two layers of interfacing on top.
Middle layer of fabric pressure sensor
Then I put the ground layer on top. This is connected to a clip lead that goes to a ground connection on the Arduino.
Top layer of fabric pressure sensor

In the finished device, there will probably be a stuffed fabric tube with rings of conductive fabric around it as sensors, surrounded by a layer of interfacing, and then by a conductive grounded layer. There will also likely be an outer layer of fabric to protect and decorate the whole thing. In order to determine if this is a good way to build the thing, I intend to use the technique to make a stuffed toy that can detect squeezing.

This is the code I used to read the values from the sensor:

#include 

/*
 * CapitiveSense Library Demo Sketch
 * Paul Badger 2008
 * Modified by Abe Shultz 2012
 * Uses a high value resistor e.g. 10 megohm between send pin and receive pin
 * Resistor effects sensitivity, experiment with values, 50 kilohm - 50 megohm.
 * Larger resistor values yield larger sensor values.
 * Receive pin is the sensor pin - try different amounts of foil/metal on this pin
 * Best results are obtained if sensor foil and wire is covered with an insulator
 * such as paper or plastic sheet
 */


CapSense cs_4_2= CapSense(4,2);        // 1 megohm resistor between pins 4 & 2, pin 2 is sensor pin

void setup()
{
   Serial.begin(9600);
   cs_4_2.reset_CS_AutoCal();
}

void loop()
{
    long start = millis();
    long total1 =  cs_4_2.capSense(30);

    Serial.print(millis() - start);        // check on performance in milliseconds
    Serial.print("t");                    // tab character for debug window spacing

    Serial.println(total1);                // print sensor output 1

    delay(10);                             // arbitrary delay to limit data to serial port
}

Counting Instances of Things

This is something I seem to do a lot. Most recently, I was trying to see how many nodes of each degree there were in a graph, but it could also be used for word counts and such.

#Run through a list and count occurrences of each item,
#store them in a dictionary of items to counts
def bin(list):
    bins = {}
    for item in list:
        if item in bins.keys():
            bins[item] += 1
        else:
            bins[item] = 1
    return bins

The key thing here is not really the little bit of python glue code to do the counting. What is key is to learn to recognize things that you do more than once, and automate them. A folkloric axiom of computer programming is that you need to do something either exactly once, or an unknown number of times. If you have to do it once, then the program should do it once. If it’s the other case, then the program should determine how often to do it, and then do it.

For example, if the program trims the whitespace off the end of a text file, it should either do it once (which allows the user to call it in a shell script loop if they want to process any number of files), or do it to any number of files. The alternative, having it process some arbitrary number of files and then fail, has two problems. First, it adds complexity. The program now has to keep a counter, check it, and fail when it hits a certain value. Second, the program, which previously worked in an infinite number of cases (that is, it would process N files for all N such that N is a positive integer), now has an infinite number of failure cases (fail for all integers greater than M), and has gone from complete success to complete failure, via the most difficult route.