electronics

An Arduino-Based Computer For The Jeep

In my early 20s, I made what, at that point, was the biggest purchase of my life: my little, green Volkswagen. In both a literal and figurative way, it carried me through some formative years. After 11 years, when repair bills were starting to exceed the value of the car, it finally became time to retire The Green Machine. (It wasn’t an easy decision, but I got a very large estimate to weld out a bad catalytic converter, and weld in a new one that would be necessary to get the car to pass its emissions inspection.) In its place, I wanted something that would handle snowy mountains a bit better — that meant four-wheel drive — and something that I could pay for in cash without totally draining the kitty. I ended up with an 11 year old Jeep Wrangler.

The Jeep has several drawbacks over the Volkswagen. It’s loud. It’s a rough ride. Despite seeming to be quite a bit larger, it doesn’t have much carry capacity at all — packing the car for any long trip becomes an exercise in applied geometry and spacial relations. However, it does have one big advantage: it is user serviceable. It’s a great car for modification and personalization. All cars should be so modular. When a side mirror recently broke, my only problem was deciding what kind of mirror I wanted to replace it with. I browsed through the options on Quadratec, picked a set, they arrived two days later, and it took me 20 minutes to replace both mirrors. When I decided that the stock headlights were too dim, I bought new enclosures for a different bulb type, and swapped the old for the new. Busted radiator? Got a new one at the local auto parts store, and swapped it out. Want seat heaters? Add a fuse block, and wire them to switches on the dashboard. (I’m sure I’ll post more about that one later.)

Since I’ve been driving the Jeep, one feature of the VW I have longed for is the little computer that would tell me the temperature outside, how long I have been driving, and my average speed. Maybe I care too much for stats, but I liked to know, eg, This trip was 19 miles, it took 26 minutes, and it is 68° F outside. The Jeep doesn’t even have a clock on the dashboard. No, to be able to see the time, I need to leave the radio (also an aftermarket replacement) on the clock display.

I finally took up the challenge to put together something that would give me the information I so yearned for. I decided to use an Arduino as the microcontroller for the project. I went to my other favorite site, Adafruit, and ordered most of the parts I’d need, such as a digital temperature sensor, a GPS shield, and a touchscreen display. When the parts arrived, it was time to get soldering.

Parts to solder

I had to solder the header pins to the GPS shield, and a set of wires to both the GPS shield and the touchscreen in order to connect them to each other. I didn’t want to connect them directly. The plan was to set it up so the microcontroller/GPS unit would be hidden under the dashboard. The display would be affixed to the top of the dashboard. The temperature sensor would be mounted to the front of a front wheel well, and wires would be run back through the engine compartment, and through a small hole I had drilled in the firewall. But before I could do all of that, I wanted to make sure the basic unit worked. Once I figured out how to interface with the temperature sensor, the touchscreen, and the GPS, it wasn’t particularly challenging to write code to pull data from the sensors, and spit the information to the display. I connected the display to the microcontroller/GPS unit through a breadboard, and plugged a 9V battery into it. It was a self-contained unit that I could plop on top of the dashboard.

Computer on dash

I’m holding the display, which is connected (through the breadboard) to the GPS shield which is plugged in to the Arduino, which is powered by a 9V battery. The display shows the time of day at the top. On the left is the outside temperature (“??? F” since it is not currently connected to the temp sensor), current speed, and altitude above sea level. On the right is current trip information: time, distance, and average speed (“0.0 MPH” because of a bug in my code at that time). At the bottom is the latitude and longitude, and a green dot, indicating that the GPS has a fix.

Once I was generally satisfied with how the device was working, I needed to replace the breadboard with a more convenient connection. I needed to connect nine wires, so I started digging around for a standard RS232 serial cable with a DB9 connector on both ends. Fortunately, I keep around all sorts of old cable that will probably never be useful again. After digging around the box, I found a cable with DB9 connectors. I tested all the pins to make sure that I had a serial cable — where there is a one-to-one connection between pins on each end — and not a null modem cable — where a pin on one end might be connected to multiple pins on the other end. I was in luck; the cable would work. I picked up some solderable DB9 connectors from Radio Shack, and put one end on the display, and the other end on the microcontroller.

For the temperature sensor, I needed to be able to plug in three wires. I like the simplicity of a standard 1/8″ stereo jack, so I soldered a female end to the wires on the microcontroller, and a male end to the wires from the temperature sensor. So that’s an easy-peasy-parcheesi connection! The only trouble I had with the temperature sensor was joining the wires that ran through the engine compartment. The sensor itself is wrapped in a waterproof casing, and is connected to a 1 meter wire. But that wasn’t long enough to make it to the cabin. Joining longer wires to complete the run was not a problem, but securing them from the elements was a concern. So I picked up some dual-layer heat-shrink. Standard heat-shrink will shrink tightly around a join, but it’s still possible that moisture could get in. Dual-layer heat-shrink has an internal layer of adhesive that melts at a temperature lower than the application temperature. The adhesive fills in the gaps, and creates a waterproof layer — it’s preferable to standard heat-shrink for outdoor applications. So I made the joins, heat-shrunk each of the three individual wires, and tested it. Everything worked fine. I added one more layer of heat-shrink around all three wires, and tested again. The sensor no longer worked. I suspect that I over-heated the wires. Rather than using a proper heat gun, I was just waving a lighter under the heat-shrink. I think I was over zealous in my attempts at shrinkage, and I somehow ruined the connections or wires with too much heat. I had to cut out the connection, pull the remaining wires closer together, and start over. Eventually, I got it right.

Temperature Sensor

Clockwise from top left: 1) Under the front, drive-side wheel well. The white wire is connected to the temperature sensor. The sensor itself is on the left, wrapped in black Gorilla Tape, to add extra insulation against the elements. 2) Under the hood, the white temperature sensor wire comes from the bottom left up to the point where it is joined (with a length of black heat-shrink) to red, black, and green wires that run to the back of the engine compartment and through the firewall. 3) The wires from the temperature sensor terminate at a 1/8″ stereo plug.

For the GPS, I needed an external antenna, as the GPS chip would eventually be placed under the dash, where it would have a hard time finding a signal. I got an external, active antenna that I could place someplace where it would have good line-of-sight to the satellites. I found that the best location was on top of the dashboard. That made it easy to run the wire behind the dashboard, down to where the microcontroller would eventually be living. The antenna wire ends in an SMA connection (which is a small coax-type connection), so I us an SMA to uFL adapter to connect it to the uFL connector on the GPS shield.

The antenna is mounted on the dashboard. The wire runs behind the dashboard, and terminates in an SMA connector, which is connected to a uFL adapter.

The antenna is mounted on the dashboard. The wire runs behind the dashboard, and terminates in an SMA connector, which is connected to a uFL adapter.

The final piece of the puzzle was power. Fortunately, for other purposes, I had previously installed an extra fuse box in the Jeep, and I had a few spare switched (ie, on when the car is on; off when the car is off) leads in the cabin. I connected one to a 2.1mm power connector, and that was all I needed to do to power the microcontroller. However, when I took the whole contraption for a ride, I noticed that the Arduino ran very hot. The car provides a fairly dirty 12-14 volts, with possible spikes that could get to 18 or 20 volts. Based on the Arduino specs, the on-board voltage regulator should be able to handle that. But aside from my own observation that the device became worryingly hot, research seemed to indicate that it was only a matter of time before I fried the device. So I decided that I’d try to feed it a clean 7 or 9 volts. (It needed to be at least 7V because the GPS shield required 5V, and the on-board regulator would cause a drop of at least 1.5V from whatever I supplied.) I considered cheaping out, and just adding a standard, 7807 or 7809 voltage regulator. But in case I end up packing everything together in a tight space, I didn’t want to have the heat dissipation issues of a voltage regulator, so I went with a much-more-expensive buck converter. Whereas a voltage regulator reduces voltage by dissipating excess energy as heat, a buck converter is a highly efficient electronic device that will step down voltage with minimal waste. I hunted down a Traco TSR 1-2490 and installed it inline with the power connector.

A Traco TSR 1-2490 buck convert is connected inline with the power connector to supply a clean 9 volts.

A Traco TSR 1-2490 buck convert is connected inline with the power connector to supply a clean 9 volts.

Since I haven’t finalized the installation, the cockpit of the car is a mess of wires that I can connect to the microcontroller when I put it into the car.

mess_of_wires

And the controller itself is sort of a mess (but somewhat less delicate that you’d think — yay for solid solder joints!).

The Whole Kielbasa

But hey, it works! So the next step is to make some custom enclosures, and tidy everything up. I have some ideas, but I might not get around to it for a few months.

Display

bike

A New Old Bike

In the late 90s, my parents gave me a Specialized Crossroads A1 Expert hybrid bike. At the time, it was the nicest bike I had ever owned — aluminum frame and 7-speed with a triple (or “21-speed” depending on your preferred nomenclature). I would tool around on the bike paths for a few miles at a time, and eventually, I trained for my first triathlon on that bike (though I did the race on a borrowed road bike).

In the years since then, I’ve had an array of bikes — triathlon, road, fixie, mountain bike — each serving a different purpose. Several years ago, my father asked if I was using the Crossroads, and if I wasn’t, he was interested in taking it so he could run short errands on it. He used it for a while, but for the past few years, the bike has been sitting in my parents’ garage, unused and unloved.

The last triathlon I did was an iron-distance race in 2008. In 2009 and 2010, I was focused on running, and since then, I had been so exhausted with Lyme disease that I could never muster the energy to do much swimming or biking. But a few changes have gotten me back on the bike:

  1. Since addressing the whole Lyme thing, I’m returning to my old energy levels.
  2. Spending a week crewing for my friend, Kiwi Chris, as she completed a quintuple iron-distance triathlon (12 mile swim, 560 mile bike, 131 mile run) made me nostalgic for all the good times and adventures she and I had on the bike over the years, and I realize how much I missed riding.
  3. I continue to have severe bursitis in both of my heels. I’m fairly certain that the inflammation was originally due to the Lyme, but at this point, it is somewhat chronic. My heels are better than they were a year ago, but it is entirely possible that it could be years — if ever — before I can run entirely pain-free. So my weekly running mileage is dictated by what my heels can handle. But biking doesn’t seem to cause pain or problems.
  4. Martha has been struggling with a few running-related injuries that have similarly limited her weekly mileage. So I’ve been introducing her to the joys (though she might use a different word) of swimming and biking.

To get in a few miles on the bike, I found a reasonable route to the office, and I started bike commuting occasionally. Living in Washington, DC, and commuting on a route that takes me over bumpy, pot-holed roads, craggy sidewalks, and a little bit of dirt, I’d use my mountain bike for the ride. It was a bit of overkill, but I decided that it would be better for my wee, little bum than trying to ride my road bike. Since I wasn’t doing much road riding, I left the road bike in storage, and I’d keep the mountain bike in the bike storage room of the co-op  where I live.

One day, when I went to get my mountain bike, I found an empty space in the bike storage room when my bike had been. The door leading outside from the bike storage room had been having trouble with the lock, so I assume that someone had come in from outside and picked the nicest bike in the room to steal. At the same time, Martha was getting more interested in riding (or at least, becoming a more willing participant). So I had pulled my road bike out of storage, and taken indefinite loan of a friend’s old bike for Martha to ride. Operation Ride More Bikes was in full swing, but we had hit several hitches:

  1. I was lacking a bike suitable for commuting to work.
  2. Aside from my non-existent commuter bike, we had two bike that could not be left in the bike room (since the bike room was clearly not secure), yet we live in an apartment that is barely big enough for two adults and a cat.

Before I could address point #1, I had to do something about point #2. Fortunately, we have high ceilings and solid walls. My first attempts to drill into the studs failed. But a new carbide drill bit made quick work of it.

 

Bikes on a wall

A nuisance becomes an objet d’art! The cat approves.

Once I had gotten the road bikes out of the way, I could focus on my commuter. The old Crossroads was available.

Bike before

The frame was perfectly good, but the 7-speed drivetrain was due for an overhaul. Further, I don’t like riding in tight spaces with straight handlebars. I started spec’ing out parts to turn it into a cyclocross-style bike. I didn’t want to pour thousands of dollars into it, but I wanted to build a bike that could take a beating and would last. I considered going with Campy or SRAM, but I’ve always used Shimano, so I stuck with what I know. I like the 105 line as a good compromise between price and quality. So I ordered a bunch of parts — a new bottom bracket, cranks and chainrings, a cog set, deraillures, levers — and started to strip down the bike.

Stripped bike

Getting out the bottom bracket was the hardest part. It was seized with 16 years of dirt and grime. Fortunately, some WD-40 and a 24″ breaker bar did the trick.

My strategy was to replace only as much as necessary to convert the bike to a Shimano 10-speed drive train (with a double) and drop handle bars. I had to hunt around a bit for an inexpensive rear wheel that would fit the 135mm dropouts, but once I had that, I was ready to go. The build-up was pretty easy.

My new bike

As much as it sucked for my mountain bike to be stolen, I’m really, really happy with my new old bike. It brings together the best qualities of my mountain bike (softer ride) and my road bike (faster ride) when cruising around the city.

There are a few to-do items remaining for the bike, but none of them is stopping me from riding the steed.

  1. New pedals: I currently have platform pedals with single-sided SPD attachments. I’d prefer double-sided SPD pedals with no platform.
  2. New brakes: I considered picking up some new brakes, but I decided that it wasn’t absolutely necessary. The current brakes are old, but still functional. If the bike works out, next summer, I’ll upgrade. For now, I just put in new pads.
  3. New stem: This was my biggest miscalculation. I didn’t realize that modern drop bars have a different diameter at the center than old drop bars. None of the old quill stems I had laying around would work with the new bars. So I had to order a quill stem adapter, and a new stem. The stem I got is 100mm, but it turns out that that’s too long. To avoid being overly stretched out, I have to jam the seat way forward. It’s not an ideal fit, but it works for now. Again, if the bike works out in the long term, I’ll pick up a 50mm stem and fix the fit.
  4. New headset and fork: The headset is in terrible condition. It’s still smooth, but externally, there’s quite a bit of rust. If I had a headset press handy, I’d probably take care of this sooner rather than later. However, I’ll either need to borrow the tools from someone, but build my own DIY headset press. (I do few enough headsets that I don’t want to pay an extra $100-$150 just for the tools.) Whenever I do the headset, I’d like to convert to threadless, and replace the fork as well.
  5. New seat post: This is purely aesthetic, but I think I’d be happier with a clean, matte black seat post. Maybe I’ll just wrap the current post with electrical tape and call it a day.
  6. New deraillure hanger: The deraillure hanger is slightly bent, which means that if I were to shift into my biggest cog (easiest gear), the spokes on the rear wheel would rip off the chain tensioner. For now, I have the limiters set so I don’t accidentally cause that to happen.

I wanted to take some pictures of the bike all shiney and new, but actually riding it took precedence. So these pictures are after a few rides (and one commute to work). Please excuse the shmutz.

Drivetrain

Levers

Breaks

Stem

Saddle

nonfiction

Let It Marinate

In my first year of college, my dorm was configured as a suite, with five rooms around a common living area. One of my suite mates was a fellow named John whose primary motivation in all activities was to bring ladies to his room to “rub”.

“I’m goin’ out with this lady tonight, then we gonna’ come back to my room an’ rub,” he’d say. Except he never said “rub” as a normal person would say “rub” (fittingly enough). He would always elongate the “u”. Ruuuuuuuuuuuub. As in, We gonna’ ruuuuuuuuub.

John was always a great source of amusement to me. He could easily have crossed the line of creepiness, but he seemed to be able to stay on the right side of that divide (barely) by becoming a sort of caricature of himself.

His term of choice—ruuuuuuuuub—was part of his charm. It’s meaning in the given context was clear. Yet, by itself—rub, without the elongated vowel—it’s a fairly innocuous word. I decided one day that I was interested in learning more about what was involved in John’s favorite pastime.

“I’m goin’ out with this lady tonight, then we gonna’ come back to my room an’ ruuuuuuuuub.”

“John, man,” I said, “how does one ‘ruuuuuuuuub’?”

“C’mere, lemme show you somethin’.” He gestured for me to come to his room. Aside from the incandescent lamp draped with a sheer piece of fabric to add a slight red tint to the light of the fluorescent bulbs above, nothing about the room struck me as particularly conducive to what I had interpreted ruuuuuubbing to involve. I looked around the room, then stared at him blankly. He smiled at me. “Hold on,” he said as he walked to the light switch, and flipped off the lights above. We were now staring at each other in the dim, red light of the incandescent lamp. “Check it,” and he pointed at the ceiling.

His ceiling was covered with the sort of adhesive, glow-in-the-dark stars that a fifth-grader might use to decorate his room. But that wasn’t what he was pointing to. He was pointing to the spot directly above the head of his bed. He had taken a sheet of the same material used for the stars, and with the skill and accuracy of a young child, cut out letters to form the word RELAX. I can’t say for certain, but I would imagine that if I were a female, and some fellow had flipped off the lights to reveal a sloppily cut, glow in the dark RELAX on the ceiling, I would do anything but.

Now, like John, I was smiling—but for a different reason. “So this is part of ruuuuuuubbing?”

“First, you set the mood.” He paused and smiled even more broadly. “Then you let it marinate.”

In my entire college experience, I attended many lectures. In each of those lectures, a professor hoped to teach some lesson that would, at least in some small way, shape the minds of the students in the class. Yet I would be hard pressed to provide an example of a lesson that made more of an impact on me than what John taught me that day. First, you set the mood. Then you let it marinate. True, dat.

knitting

Faux Cable Hat

Download the pattern: Faux Cable Hat

Ravelry Pattern Page

The standard technique for moving a column of stitches across a pattern is cabling, in which one or more stitches are passed over one or more other stitches while knitting a row. However, a column of stitches can also be moved across a pattern by balancing increases on one side of the column with decreases on the other side of the column. As an exercise in this technique, I started drawing up a chart with two such columns, criss-crossing is opposite directions.

I ended up with what I call my Faux Cable Hat. It’s a subtle pattern — without the bulk of real, honest-to-goodness cables — that looks best in a single color yarn.

Also, it can be knit with either a foldable brim, or a short, simple brim.

One note about the increases: All increases have either a “left” or “right” lean, specified as M1L and M1R, respectively. M1L is made by lifting the bar between the current two stitches from the front, and knitting through the back loop. M1R is made by lifting the bar between the current two stitches from the back, and knitting through the front loop. Further information about these two techniques can be found at,

http://www.twistcollective.com/collection/component/content/article/92-how-to/1046-make-1-left-or-right-m1-m1l-m1r

I occasionally become bleary-eyed, and forget which type on increase requires lifting the bar from which side. Eventually, I came up with the mnemonic that M1L lifts the bar from the Front, because L and F are similar (both letters are constructed with only horizontal and vertical lines). Likewise, M1R lifts the bar from the Back, because R and B are similar (both letters are constructed with a rounded shapes).

Download the pattern: Faux Cable Hat

programming

Race Progress Visualization Using D3

[The project referred to in this post can be found at http://vestigial.org/MMT/ ]

I’ve been looking for some better tools to produce interactive, data driven, visually appealing web content. In the past couple of years, I’ve become enamored with R for analysis and visualization, but the graphic results are static. (Sure, there are tricks to create animations, but I’m not looking for workarounds.) I occasionally use Google Charts when I need to put together a quick visualization, but they don’t provide quite the level of flexibility I’d like. I started looking at either working directly with SVG or Canvas DOM elements, or using a Javascript SVG library that would allow me to avoid the low-level details.

The most interesting possibility was the D3 framework. D3 — for Data-Driven Documents — is an entire framework for DOM manipulation in data-driven sites. Browsing through the examples on the D3 site, I recognized several memorable visualizations that have appeared on one of my favorite blogs through the years, Flowing Data. It is possible to use D3 for SVG construction and manipulation while non-data-driven portions of the site are handled by, eg, jQuery or standard Javascript. But as long as you’re already using the bandwidth to load the framework, you might as well drop other frameworks, and use the tools that D3 provides.

I was keen to get some experience with D3. When learning a new technology, I prefer to dive straight in — come up with a short, but non-trivial project that I can build. In this case, I came up with a project that melds technology, data visualization, and ultrarunning. The Massanutten Mountain 100 Mile Trail Run (or MMT) is in a few weeks. In such a long race, runners and crews like to have some idea when they’ll arrive at intermediate points along the course if they’re aiming for some given finish time. Conversely, knowing when they’ve arrived at points along the course can help to predict what sort of finish time to expect. While I’m not the first person to provide a visualization, or some tool to correlate aid station splits with finish times, it’s fun to put together something that’s visually appealing and useful.

Showing data from 2011 and 2013 for finishers who finished between 20:59 and 25:55, race time. The horizontal axis is time and the vertical axis is distance, labeled on the left with mileage at each aid station, and on the right with the aid station name. Each diagonal line represents a single racer. Intermediate times on the graph show first and last racer times of arrival at each aid station (for racers in the result set).

Showing data from 2011 and 2013 for finishers who finished between 20:59 and 25:55, race time. The horizontal axis is time and the vertical axis is distance, labeled on the left with mileage at each aid station, and on the right with the aid station name. Each diagonal line, or “track”, represents a single racer. Intermediate times on the graph show first and last racer times of arrival at each aid station (for racers in the result set). Tufte would be proud.

 

There are several interactive components that I think are noteworthy. First, I provide on-demand data loading. When the page loads, none of the race results is loaded. When a year is selected, the page checks whether the data have been downloaded. If not, it fires an AJAX request, and saves the data so the results can be turned on and off.

The page also provides sliders to limit the result set based on finish time. Each limiter consists of three components: a triangular slider widget (represented by an SVG path element), a time display (represented by an SVG text element), and a vertical guide line (represented by an SVG line element). When the widget is slid, all three elements should move in unison, and the time display should update with the time value at the current point. As a bonus, the vertical guide gets brighter. So I needed to be able to address each element individually, but move them in unison. To build that, first I needed to define the shape for my widget (note that in SVG coordinates, the top left is [0,0]):

var limpolygon = [{x: 0, y: 0}, {x: 10, y: 0}, {x: 5, y: 10}, {x: 0, y: 0}];

I also need to define a function to tell D3 how to interpret the data above. I can use d3.svg.line() to return a function for this purpose. Since I’ve built the object with straight-forward X and Y coordinates, I just need to build a simple function based on those values:

var limline = d3.svg.line()
  .interpolate("linear")
  .x(function(d) { return d.x; })
  .y(function(d) { return d.y; });

Finally, I put the group together. I define a group element (“g”), and append the widget, which I construct in place. I then use the D3 selector to reselect the group, and add the line, then the text:

svg.append("g")  // Create the group, append it to the svg object
  .attr("id", "lim1")
  .attr("transform", "translate("+lim1x+","+limy+")")  // Put it into position
  .append("path")  // Create "path" element for widget, and append it to group
    .attr("id", "lim1_point")
    .attr("d", limline(limpolygon))  // A path has a "d" attribute which gives
                                     // instructions for drawing. Our limline()
                                     // translates raw data into path data
    .attr("fill", "white")
    .on("mousedown", function() {
      capt = "lim1";
      d3.select("#lim1_line").style("stroke-opacity", "1");
    });

d3.select("#lim1").append("svg:line")   // Create line element, append to group
  .attr("x1", limhalfw)
  .attr("y1", ex_pad.top)
  .attr("x2", limhalfw)
  .attr("y2", height - ex_pad.bottom)
  .attr("id", "lim1_line");

d3.select("#lim1").append("svg:text")   // Create text element, append to group
  .attr("id", "lim1_time")
  .text("00:00")
  .style("text-anchor", "end")
  .attr("transform", "translate(-2)");  // Push it 2px to left, for a nice gap

In my view, the coolest trick is making the data respond to the sliders. Whereas showing or hiding the individual years relies on a small number (3) of discrete values, I need to show or hide individual race results based on what is essentially a continuous scale. This involves several steps. First, when adding each track to the graph, I need to attach the finish time to it. Fortunately, HTML5 provides the ability to specify arbitrary data attributes with the data-* construct.

lineset.enter()
  .append("path")
  .attr("data-finish", function(d) {  // Add the data-finish attribute
    return d.finish;
   })
  .style("stroke-opacity", function(d) {
    if (d.finish > finScale(lim2x) || d.finish < finScale(lim1x)) return "0";
    else return ".3";
   })
  .datum(function(d) { return d.splits; })
  .attr("class", "rtrack line " + iden)  // Classes to use later in selectors
  .attr("d", line);

Above is the code to add the tracks. While it might not make much sense if you are not familiar with D3, the key point is the third line. The object has a data object, d, applied to it, and on that line, we set the data-finish attribute to the value of d.finish. (Directly below that, we set the opacity of the line to 0 (making it invisible) if it falls outside of our specified range, or .3 if it is inside the range. But we’re getting ahead of ourselves.)

The next thing we need to a way to translate the location of a slider into a finish time. D3 provides “scales” for just such a purpose. Usually, D3 scales are used to translate some real world value to a pixel position. In this case, we want to do the reverse. I want to build a function that will translate an input domain of a pixel position into the output range of a race time, which in this case is between 0 and 36 hours.

var finScale = d3.scale.linear()
  .domain([lim1x, lim2x])
  .range([0, 36]);

(An astute reader who is familiar with D3 might note that somewhere else, I must have defined a scale to translate from times to pixel values. In that case, someone might wonder why I don’t just use linear.invert() to translate a range value into its corresponding domain value. The answer is that the scale that translates from time to position uses a domain defined by the time of day as a date object, whereas in this case, I want to translate between position and a floating point number representing the finish time in hours (with minutes represented in the fractional portion of the number). Hence the need to define a new scale.)

In this case, lim1x is the initial pixel position of the lower limit slider, and lim2x is the pixel position of the upper limit slider. That produces a function that can be called as finScale(px_pos) to return a corresponding race time. I can then use that in the function that is called when a slider is released.

function updateRange() {
  var fin1 = finScale(lim1x);  // Translate pixel positions to finish times
  var fin2 = finScale(lim2x);
  d3.selectAll(".rtrack").transition(500).style("stroke-opacity", function(d) {
    if (this.getAttribute("data-finish") > fin2 ||
        this.getAttribute("data-finish") < fin1) return "0";
    else return ".3";
  });

  updateAidStationTimes();
}

That function translates the current pixel positions of the sliders into race times (fin1 and fin2). Then it uses d3.selectAll to get every item with the class “rtrack” (which is every race line displayed on the graph), applies a 500ms transition time to the following step, then sets the stroke-opacity style based on a function that checks whether the custom attribute data-finish is in the range defined by the limiters. Finally, it calls updateAidStationTimes(), which I won’t explain in detail here, but it uses d3.extent() with a custom accessor function to find the first and last arrival time of racers in the result set at each aid station. (If you’re particularly interested, you can always dig it out of the source.) It then updates the times displayed on the graph, and moves them into the proper positions.

I started the project on Saturday morning with no experience in D3 (or with SVG graphics), and I finished Sunday evening. I even had time to get out for a bike ride, a run, and a trip to the library to get a movie (which I also watched over the weekend). In the course of this project, I came to appreciate just how massive D3 is. I’m starting to get a feel for it, but this project just scratched the tip of the D3 iceberg (though I’m not sure one would really scratch an iceberg, the tip or otherwise).

[The project referred to in this post can be found at http://vestigial.org/MMT/ ]

music

Traumerei

When I was very young — maybe around second grade — I started taking piano lessons from Mrs. Bickley, who also happened to be my school’s librarian. But I had little patience for practicing. Working through a new song, note by note, making the same mistakes again and again created such visceral frustration that there came a point when I refused to practice at all. Sometime during fourth or fifth grade, my parents allowed me to quit. I felt some shame whenever I would venture into the library and see Mrs. Bickley, but that couldn’t nearly outweigh how much I disliked practicing.

Some years later, when my brother was taking guitar lessons, I tried again. My parents could take us both to the music store with the sound-proof practice rooms in the back, and we could take our lessons. That time around, I lasted perhaps a year before deciding that it wasn’t for me.

In middle school and high school, I was involved in theater (some, of the musical variety) and choral groups. I wasn’t much of a singer, but it turns out that at certain times and places, if you are a male, it’s sufficient just to be willing to show up. I was the beneficiary of just such a situation.

Many, many years later, I found myself regretting that I didn’t stick with music. I’m envious of people who can sit down with an instrument, and play a song. As time went on, my regret grew more and more palpable. I still had a mini Casio keyboard that I received as a gift as a child. I plugged it in, and started to pick out songs on its miniature keys. After several weeks of the tinny, synthesized keyboard sound, I decided that it was time to commit. I bought a full-sized keyboard, and I started to play again. Whereas I had been an impatient child, I have become an adult who is quite content to spend an hour or two working through a troublesome section of a song.

I can’t claim that I’ve become a very good piano player. But I can pick through a song, and repeat it enough that it eventually starts to sound coherent. One of my favorites is Traumerei by Schumann. It’s short and melancholy and open to oh-so-much expression. Here, I play it too fast, and I’m sort of distracted by the concerns of self-recording. I hammer the keys too hard and unevenly in many places, and I continually turn even eighth notes into dotted eighth notes and sixteenth notes.

But it brings me joy to be able to sit down at an instrument, and play a song.

 

 

(I should note that the C above middle C on my keyboard is busted. The key itself is loose and most of the time, it doesn’t play when struck. So if you are paying close attention, you’ll notice that that note is almost entirely absent from the song. Also, I recorded the sound on Garageband on my iPad. For some reason, that app has recently decided that my songs really need occasional, randomly placed gong sounds. So again, if you are paying close attention, you’ll hear a couple of Garageband gongs.)

life

Grandma

Grandma passed away four years ago. The following is the eulogy I gave at her memorial service.


Grandma moved in with us when I was very young. She was a part of the family, and she helped my mother and my father raise me and my brother. I suppose that the prime directive of raising two boys is to get them through adolescence without allowing them to cause excessive harm to themselves or others. And as a testament to their success in that endeavor, my brother, Mark, and I are here today.

But if I were to claim that grandma only kept me out of harm’s way, I would be failing to recognize the deep impact she had on my life—on making me the person I am today. And while she influenced me in many ways, there was one lesson she taught me that stands above all others. It’s a lesson that she repeated throughout her life—to me and to everyone who came in contact with her. She never forced it on anyone, and most of the time you might not have recognized that she was offering a lesson at all. I didn’t. I didn’t realize it until many years after she started teaching it to me.

When I was young, I realized that grandma had a unique way of seeing the world. While other adults would talk about difficulties, she would talk about joys. When other adults would talk about how cold and rainy it was outside, she would talk about how warm and cozy it was inside. I thought that she just didn’t see somethings that other people saw—that she had some sort of blind spot for those things that most people dwell on.

Years passed, and I grew older. Eventually, I could talk to her as an adult. And as an adult, I realized something: I had been naive. She wasn’t missing anything. She could see the difficulties and travails of life just as well as anyone else. The difference between her and everyone else was that she had an amazing ability to put the bad aside, and focus on the good. She wasn’t an optimist because she couldn’t see the down side of a situation. She was an optimist because she chose to embrace the up side of all situations. Her optimism was intentional.

Grandma never tried to convince me that I should practice her brand of intentional optimism. No, she was content to let me become my own person, to find my own way. All the while—through my childhood and beyond—she was a presence, offering herself as an example of one way to see the world.

None of us can go through life without affecting, and being affected by, the people around us. Grandma lived for 96 years. Many people loved her, and she loved many people. In one way or another, she touched every one of us. When someone like Grandma passes on, those of us who loved her struggle to find a way to honor her and everything that she gave to us. I’m glad that we could all come here today to remember and honor her. But after we leave here, and return home, she will still be with us. Each of us has the opportunity to continue honoring her by embracing that most important lesson that she taught all of us: that we can choose to see the joy, the up side, the good in any situation. I know that every time I practice intentional optimism, a part of her carries on. And in that way, she will always be with me.

knitting

A Sweater

While in Iceland for the Laugavegur Ultra Marathon, Martha and I came across the storefront for the Handknitting Association of Iceland. I took the opportunity to stock up on yarn. The Álafoss Lopi was ridiculously inexpensive, so I got a sweater’s worth of it.

sweater_detail

After a bit of consideration, and consultation with Martha, we decided on a forest green main color, with some yellow and white for contrasting colors. I didn’t really know what I was going to do with it.

sweater_front

I ended up working the standard seamless design from Elizabeth Zimmerman’s Knitting Without Tears.  I started with a provisional cast on, and reversed it with a perl round after a few inches to give me a straight, hemmed edge. I did the same around the sleeves. The touch of color on the inside makes me happy.

sweater_hem

I made up the yoke as I went along. You put in so much work for something like that, and you have no idea whether it’ll fit until it’s pretty much done. So you need to enjoy the process, and accept that in the end you might walk away with something that sucks. If it comes out right, well, that’s just a bonus.

sweater_seated

 

music

Music From Long Ago

I recently came across some old tapes from high school, when I sang with a madrigal group. Twenty years ago? Really?

I dug up an old Walkman™, put in some fresh batteries, plugged it into a computer, pulled the songs into an audio editing program to clean them up (most noticeably by getting rid of a loud hiss at around 8kHz), and put them on the interwebs so that the digital archeologists of the future might someday take note.

 

electronics

Blinking LED Circuit

As part of a larger project, I needed a circuit to blink an LED. It’s a simple task, and there are plenty of existing designs. But having almost no experience with circuit design, I wanted to make my own. Further, I wanted to make it with basic components — no ICs. An integrated circuit, like a 555, would take the fun out of it!

As is the case with most oscillators of this sort, the charge/discharge cycle of a capacitor acts as a switch to turn the LED on and off. I sketched out a few ideas, and eventually arrived at this:

Blinking LED Circuit

The blinking happens because the circuit oscillates between several states:

  1. Current goes through PNP transistor Q2 to the anode side of capacitor C1. As C1 charges, the cathode side drains through resistor R1 to ground. In doing so, it also puts “pressure” on the base of PNP transistor Q1, preventing current from flowing through it.
  2. After C1 is fully charged, its cathode side will no longer be draining, so there will be nothing preventing flow from the base of Q1 through R1 to ground. At that point, Q1 will start to conduct to the big loop, which will cause three things to happen.
    1. Current will flow to the base of NPN transistor Q3, which will allow C1 to slowly discharge through resistor R3. Also, once Q3 is conducting, the drain on the base of Q1 will increase as current flows to the cathode side of C1.
    2. Current will flow through diode D1, to put “pressure” on the base of Q2, thereby preventing further charging of C1 from the voltage source.
    3. Current will flow through light emitting diode D2, causing it to light up.
  3. Once C1 is fully drained, the base of Q1 will only drain through R1. If R1 has a high enough value, the output of the collector of Q1 will fall below the threshold to block the base of Q2. When that happens, current will once again flow through Q2, C1 will start charging again, Q1 will stop conducting, the LED will turn off, and we return to step 1.

So that was my theory. My next step was to build it, and figure out the right values for all the components. I breadboarded it like this:

Blinking LED Circuit

Voltage: +5
Q1: (PNP) 9015
Q2: (PNP) 9015
Q3: (NPN) PN2222
R1: 10KΩ
R2: 10KΩ
R3: 680Ω
R4: 68Ω
C1: 470µF
D1: 1N4148
D2: Blue, 3.7V, 20mA

That resulted in a flash rate of 1.3Hz. The blinking speed can be adjusted by changing the capacitor and/or changing the values of R1 and R2. I swapped C1 for a 22µF cap, and the flash rate increased dramatically, perhaps to something between 20 and 30Hz. So I swapped R1 and R2 for 100KΩ, and the flash rate returned to something around 1.6Hz. Ideally, the capacitor should be very small, since it is essentially charging, then dumping its charge in every cycle. I’d like to try to use a much smaller capacitor (with larger R1 and R2 values) to see if I can maintain enough current in that part of the circuit to control the functioning of the transistors. I used a blue, 3.7V LED on a 5V circuit. The LED can be changed, as long as R4 is changed as well to ensure the proper current for the LED’s voltage.

It’s not the simplest circuit of this sort, and I’m sure that I’ve screwed up at least part of the analysis. But as someone who only knows as much about circuit design as he could find on the internet, and a couple of books (namely, Getting Started in Electronics by Forrest Mims, and Starting Electronics by Keith Brindley), I was fairly pleased with myself for making this work.