Saturday, October 22, 2011

Template metaprogramming

Template metaprogramming is a fairly useless feature of C++, but we can have some fun with it. I've been trying to improve my functional programming skill set, so a friend proposed a problem:

Write a routine that can count bits in an integer at compile time using templates.

Here's the basic recursive solution:

int count_bits(unsigned int x)
if(x == 0)
return 0;
return (x & 0x1) + count_bits(x >> 1);

This isn't what I had in mind though. I want to do this using templates. That gets us there:

#include <stdio.h>

template<unsigned long long input>struct numBits
static const unsigned long long value = numBits<(input >> 1)>::value+(input&1);

template <> struct numBits<0>
static const unsigned long long value=0;

#define output(num) printf("0x%llX: %u\n", (unsigned long long)num, numBits<(unsigned long long)num>::value)

int main(int argc, const char **argv)

return 0;

Ha! The biggest problem with this is it only works on constants (it IS compile time). It's an academic exercise, as there's not much good reason to use this over the other options. The output looks like:

0x0: 0
0x1: 1
0x2: 1
0x3: 2
0x4: 1
0x11: 2
0x111: 3
0x1000: 1
0x1010: 2
0x1001001: 3

Sunday, October 9, 2011

Near space attempt #1

I have been reading about a bunch of the high altitude weather balloon experiments, and I wanted to give it a try. I started with a bunch of websites sort of explaining what they did, and initially decided to copy them. I got myself a Canon A470 and a Motorola i290 prepaid cell phone from Amazon, a sturdy styrofoam box and some kite string from my parents, a 600g weather balloon off Ebay, a rocket parachute from a sketchy online hobby shop and two 60 cubic foot helium tanks from Taylor Rental. The goal was 100K feet (3 times higher than those really small jets up there).

This was collected over a 2.5 month period. The project was "officially" started when I found a different camera and the Motorola cell phone on sale on Amazon on July 24th. The first camera I got couldn't support larger than a 2GB memory card, so I returned it and bough a used Canon A470 instead. I'll put up more info about each of the parts used in separate posts.

We've had a lot of rainy weekends lately, and the weekend of October 8th was the first nice one in the past 3 or 4 weeks. It turns out the the high winds we favorably blowing from north to south, allowing us to launch the balloon from Nashua and recovering it somewhere in the Massachusetts Metro-West area. I wanted it to land in a civilized area, so it we had a chance of getting to it without a huge overland hike and somewhere where there should be cell phone coverage.

Using the predicted flight path put it somewhere south of Framingham. This was the weekend.

Fill 'er up

I played phone tag all week with various FAA offices until I at least got someone to at a least acknowledge that I was going to send a balloon up somewhere near Boire Field. I arrived at 8 and scoped out a place to launch it and decided that behind the tennis court would work. A co-worker helped inflate the balloon. The 600g balloon was supposed to be 6ft in diameter fully inflated (113 cubic ft). I used a piece of tubing duct taped inside the balloon nozzle and stuck over the balloon filler. Emptying a single 60 cubic foot helium tank took about 8 or 9 minutes. This is a picture of the balloon after the first tank:

Switching to the second tank was pretty easy. Packing the payload was pretty straightforward:

The green piece is a mylar balloon crumpled up so it (possibly) would show up on radar. I don't know if that worked, but it didn't add much weight. The black box plugged into the phone is a battery extender that I had. I don't think that did anything either, but it did add weight. The phone would typically run for around 10 hours with the GPS tracking software enabled, so the extra battery pack was overkill. The camera is pointing through a hole in the side of the box. I filled the box up the rest of the was with packing peanuts and taped a note onto the top:

Hopefully that will keep the Massachusetts police organizations from over-react-ing.

The Launch

Liftoff was at 9:13 EDT. We toddled down to Framingham to wait for a signal of the parachuting payload coming back to Earth. While eating lunch, we got our tracking signal:

Awesome! It landed in North Easton!?! We were still 40 miles away in Framingham. Touchdown was at 12:13 EDT. We drove over and found the payload intact:

The balloon was shredded and really twisted up in the parachute. I think it would be better to put the ballon farther from the parachute next time, but I'll detail that later.

The raw result:

Flying a balloon to 100K feet

The pictures weren't outstanding, but I'll call it a success. I got a picture of the blackness of space, so I know it got a long way up there, and we were able to successfully do it once.
Some of the highlights: