Friday, August 10, 2012

Cloning Bootable Linux Flash Drives

I'm forever forgetting the Linux command to clone Linux flash drives. It comes in handy because it's really easy to accidentally trash the file system if your PC crashes or you accidentally perform and un-graceful shut-down. Keep many and make frequent back-up of your work in progress if you're doing on a flash drive!

Here's the command:

    dd if=/dev/sdc of=/dev/sdd

This is a very powerful command! You will need to invoke the power of sudo! Make sure that if the PC has a hard drive that you don't point the "of" (output file) at it our you'll wipe out what every operating system was on it. Remember that you also need to run this from a drive that isn't your input or output flash drive. For this reason you may need as many as three USB devices to complete this operation. 

Do not run this command verbatim! First do an ls to determine what your current drives are, and make sure to keep them out of this command. Then plug in the source drive, and use ls to find its name (i.e. sdc). Next plug in the destination drive, and again determine its name. Only then run the command. You will find that a 4GB flash drive may take quite a while to copy.

Wednesday, July 4, 2012

Getting the time date stamp into the file name

We just returned from a two week trip to China on which we took 3000 photos with four different cameras. Now we need select a hundred or so to put on our web page. The problem is that most image previewers sort by file name. This means the photos are each grouped by camera.


require 'rubygems'
require 'exifr'

mypix = Dir.glob(ARGV[0] +"/*.{jpg,JPG}")
    somepix = mypix.sort_by \
        { |a| EXIFR::JPEG.new(a).exif.to_hash[:date_time_original] }
    somepix.each do |pic_file| 
 regex = /[\/\.]/
 system("mv " + pic_file + " " + 
            pic_file.to_s.split(regex)[0] + "/" +
            pic_file.to_s.split(regex)[0] + "_" + 
            EXIFR::JPEG.new(pic_file).exif.to_hash[:date_time_original].strftime("%Y-%m-%d_%H%M%S") + 
            ".jpg")

end

The run the app

$ruby rename_pix.rb trip12a


And now you can sort your pix.

Fixing Mini-blinds - Fishing Supplies to the Rescue

Many of us have mini-blinds or honeycomb shades at home. These are usually special-ordered and custom cut to fit your windows. For the most part you get the same style for the whole house.

When part of the mechanism on one of our honeycomb shades broke, the left side hung lamely when I pulled the cord to let some light into the bedroom. Our shades are old, and I'm pretty sure that I wouldn't be able to find an exact match if I wanted to replace it. I thought to myself, "this is going to be a real mess to fix." Then it occurred to me that someone had to assemble these shades in the first place, and I'm at least as handy as that person.



I found the there were end caps that easily snapped off, and a plastic cover that slid away to reveal a drawstring. the drawstring had become severed after years of sliding over the bale that held the shades in the open position. The drawstring was a tough, finely braided, but small diameter cord. I took a sample of the drawstring to a craft store that sold DIY drapery supplies. They had something similar, but it was far to big to fit through the bale mechanism. Then I went to a big-box home improvement store - no dice. Where have I seen this stuff before? Fishing!

I went to a fishing supply store and showed them the cord and they said, "We've got something like that. It's a little thiner and if you don't mind the white and green color, it's 6 cents per foot".

"Yeah? Gimme a hundred feet then"!

I believe the stuff is called braided fishing line. I got a lot of it because:

a) it was cheap.
b) who knows when another shade will break?
c) it looked like really useful stuff to have at the workbench.


I pulled all the old string out of the shade. Holding the shade closed, I was easily able to thread the line through the honeycomb fabric using a rather large needle. I replaced all three lines, threaded them through the locking bale and re-hung the shade. I pulled the cord and the shade lifted smoothly and evenly. I snapped the cords to the left to latch the shade in the up position, released the cords, and the shade fell on windowsill with a bang. The cords were too thin for the locking bale! As a quick and dirty fix, I cut a strip of metal from a can and wrapped it through the bale to make it just a little thicker. I had to be careful that none of the rough edges would rub and abrade the strings. If I ever re-do this I'm going to cut some thin pieces of plastic and crazy-glue them inside the bale instead.

Now our honeycomb shades let the sun shine through again. I've used the fishing line on mini-blinds too, and even though the mechanism is quite different, I found it works quite well.

Saturday, February 25, 2012

Improving Battery Tester Accuracy

In earlier experiments I found that when powering the Arduino from USB, the battery voltage measurements were all over the place. This is because when the Arduino uses its power supply as the reference voltage for analog conversion, and the power supply isn't precise, neither is the analog measurement precise. It turns out USB power can vary +/-10% and still be in spec.

Accuracy can be improved by adding a reference voltage. I used an LM431Adjustable Precision Zener Shunt Regulator. By the way, when looking for spec sheets it's best to go to the manufacturer's site or to a vendor site, otherwise you may end up in the land of pop-up hell or you may run the risk opening malevolent PDFs.

Normally one would connect the reference output to Vref, but since my battery voltages are over over 2.5 volts, I connected it to one of the analog inputs such as A0.

I then applied the measured reference voltage as a correction factor to each measurement.

batt01 = (float)a1 * 2.5 / (float)a0;

This resulted in much more stable measurements, but unfortunately it didn't improve the repeatably. I repeated charged and discharged the batteries and although some generalizations about each battery can be made, I doesn't seem like it will be possible to compare a battery from one set to a battery in another set. I want to be able to do this so I can regroup the batteries into better performing sets.

Better, but still not good enough. To be able to compare batteries from different sets I would need to see the discharge curve for each battery overlay itself in these test runs. Perhaps the problem is related to the charger. I'll try a different charger and repeat the experiment.

One final improvement I want to make is to the resolution of the graphed data. I'm rounding off the results to to two decimal places. The A-D converter has twice that resolution, so in the future I'll increase the output to three decimal places.

The source code for this project can be found on GitHub as usual.

Saturday, November 5, 2011

Yogurt Maker Part 1

We started building this yogurt maker.

We've completed the TTL controlled power switch and that seems like a pretty useful project by itself. Of course we had to hack the hack and redesign Chris Reilly's implementation to suit our needs and the parts we had on hand. We roughed out the design on paper. As great as CAD is, nothing beats a sheet of quad paper, a mechanical pencil, and a big eraser for your first few design iterations.

Next I went to a big box home supply store - there are no more hardware stores in our neighborhood - and found parts such as a junction box, a power socket and a cable clamp. Since it wasn't exactly what I needed, yet another design iteration was called for. The result was this:

And on the outside it looks like this. The cable plugs into the wall and that powers up the outlet on top. We'll plug the power supply for our microcontroller into that outlet. The microcontroller's ground, 5V power, and signal lines will plug into the connector on the left. The amber LED on top indicates 5V power. The red LED indicates that the signal line is high, and that the outlet on the bottom should be energized.
In the next installment I'll write about the microcontroller and sensors.

Tuesday, August 2, 2011

Taiko Synth - Phase 9 - Waveforms

Until now, the taiko drum pad was a simple piece of masonite on a stack of packing foam with some mouse mats on top. A single strike resulted in multiple signals and I suspected it was because of either the masonite resonating, or all the pieces bouncing up and down. I needed some instrumentation.

The first step was to tie the drum pad together in an attempt to hold all the pieces together. I cut a circular base from plywood, then used a hacksaw to cut the foam to match. The foam looks terribly jagged - I think it needed some kind of hot-wire cutter for that. I got some 1/4" shock-cord (at REI, this is what they call this bungee cord) and cord-stoppers (clips used to close up back-packs, etc), then I cut holes in the drum head, foam, and base through which to thread the shock-cord. The hardest part was punching holes in the foam and threading the cord through it. The drill won't cut; the foam just got all wrapped around the bit! Forcing a 1/4" rod through the foam worked pretty well, although it was sill nearly impossible to thread the shock cord through the foam. Once the shock cord was threaded and cinched up, I glued some 2x2s to the base so it didn't rock back and forth on the shock-cord and cord-stoppers underneath. I used Gorilla Glue, which is really strange stuff, attach the 2x2s. As it hardens it foams up like polyurethane and can be a big mess if you use too much. The last step was to attach the mouse mats to the top surface of the masonite with silicone rubber glue.

After all that, it was time to hook it up. I connected a mini stereo plug to the drum and plugged that into the input jack of a laptop running XOscope. This is a pretty neat program. The controls are pretty intuitive, but I'm not going to be able to get any useful amplitude data until I figure out some kind of calibration work-flow. The other thing I didn't do which I should have done was to put some protection on the inputs - I could have damaged the PC's audio circuit - or worse. Note to self: next time be more careful!


Here's a trace of a hard DON strike. Right click and open the graph a new tab to enbiggen it sufficiently. The green trace it the drum head and the blue trace is the rim. You can see that the green trace is clipped, but what I was looking for was how long it resonated for. This way I could know when to start listening for the next strike. The graph shows that the signal took about 75 mS to settle down.

The other thing we can determine is that the resonant frequency is 4/0.02125, or 188 Hz

What I see there is that the drum head needs more damping. I want a single bounce - like when you push down on the fender of your car to see if the shocks are shot.


Below is a plot of a softer strike; one that 's not clipped. From it we can obtain the damping coefficient Tau, or the time it takes for the sine wave component to decay to 0.27 times its origin amplitude. It looks like Tau is about 0.017 Seconds.

The other thing I see in this plot is cross-talk. Striking the center of the drum results in a signal on the rim sensors. Cross talk's roughly (0.25/3.5)*100% or 7% and that's not too bad.


Tapping on the rim shows a quite different crosstalk story. Here the cross talk is (0.5/1.2)*100% or 42%. Something will need to be done to improve this. I think I many need to cut a slot in the foam to separate the rim from the drum pad center, but I'm concerned that the remaining foam will be too thin to support the rim. Maybe the foam will need to be replaced by a stiffer elastomer.


From this base-line data, I'll be able to see if changes I make to the construction of the drum pad improve or worsen its performance.

Sunday, July 24, 2011

Ardunino-based AA Battery Tester

I've been a big believer in buying products that use AA rather than proprietary batteries. One advantage of AA batteries is that if you use up all your rechargeable AAs when traveling, you can alway find alkaline batteries in a convenience store. Also, if all you devices use AAs, you only need one charger.

On the other hand, a disadvantage of AAs is that because of NiMH technology, and the shape of the cells, their total energy density is less than Li-ion batteries.

When using NiMH cells, a problem with I've encountered occurs when one of the four cells in the pack is bad. With a fully charged pack in my camera, I've been out of power after only five pictures because of one bad cell. Even if all all cells are good, it's best to match cells with similar characteristics. This was the purpose of this project. Yes, one can buy a battery tester, but its so much more fun to do it yourself!

My goal was not just to test the batteries as a set of four, but to get discharge data from each cell. I've seen some projects in which the battery pack is tested by connecting a power resistor and a clock to the terminals. The idea is that the when the battery is discharged, the clock stops, and you can make a calculation of battery capacity from that. There are two problems with this. One is that the battery isn't discharging at constant current, so you can't accurately compare your results to the battery's capacity rating. The other problem is that fully discharging an NiMH battery below 1 volt per cell will damage it. If you test in this manner, each subsequent test will result in worse performance.

The first step in the project was to do a little research on NiMH Batteries. I found a good reference in Sanyo's Twicell battery manual. Here are some of the more useful facts about NiMH batteries.

If you discharge the battery below 1V/cell, the cell can undergo a polarity reversal (!). This would cause the battery (a battery is a group of cells) voltage to drop suddenly. To give the cells a margin of safety, I limited discharge to 1.1V/cell.

The typical test discharge rate (It) is defined as the current at which the battery would be drained in one hour. Thus, a 2800 mAh (milliAmp-hours) battery can be drained at 2.8 amps. That would dissipate if a lot of power! I decided limit battery drain to 1 amp - a nice even number.

Here's the circuit:

The schematic is a little hard to read in the blog. You can right click it and open it in a new tab to enbiggen it.

Here are the circuit details.

Current is drained through an LM317 three-terminal voltage regulator. It's configured for constant current mode with eight 10 Ohm 1/2 Watt resistors. Since full discharge current passes through these resistors, they must be big enough to dissipate that power. Increasingly higher rates of discharge can be selected as each of the switches are closed.
S1           => 1.2 Volts / (10 Ohms / 2) = 0.24 Amps
S1 + S2 => 1.2 Volts / (10 Ohms / 4) = 0.48 Amps
S1 + S2 + S3 => 1.2 Volts / (10 Ohms / 8) = 0.96 Amps

I only used the maximum discharge rate because any less results in a long test time. At the lowest setting, a full discharge can take ten hours.

One curious thing you may notice about the circuit is 1 ohm, 10 watt resistor. That's there because of the possibility of the maximum battery voltage exceeding 5 volts. Rather than attempting to measure the voltage of all 4 cells directly, the voltage is dropped 1 volt when draining the cells at 1 amp. In this way, the Arduino's maximum analog input is not exceeded. It does make calculation of the cell four's voltage a little strange-looking, though.
VB1 = A0
VB2 = A1 - A0
VB3 = A2 - A1
VB4 = A3 - A2 + (It * 1)

To convert these values to voltages and perform the calculations I imported a floating point library. In this way I was able perform the calculations and format the output directly to so that could be used by a plotting program such as GnuPlot. To use the tester I charged the batteries using the same charger that I travel with. I transferred the batteries from the charger to the tester. The tester was plugged into a laptop running a Putty terminal which logged all the output to a file. When "g" was typed at the keyboard, D7 went high, closing the relay and discharging the cells. If any cell reached less than 4.1 Volts, D7 would go low, stopping the test. From the saved data, a discharge profile could be plotted:

Several problems were encountered with the first version of the battery tester. The first was heat. I didn't expect that I'd need a very large heat sink on the LM317 since it was only dissipating four watts. I was wrong. My rule of thumb is that if a component is so hot that you can't hold your thumb on it, the component needs more cooling. I first tried adding a small 5 volt fan to the heat sink, powering it off the laptop's USB port. This cooled the LM317 sufficiently but made an annoying sound I suspected it was causing electrical noise that was making the battery readings erratic. I clamped ferrites on USB cable, but that didn't improved the accuracy of the measurements, so I removed the small heatsink, and installed the big heatsink you see in the photo. That took care of the temperature, but the measurements were still unstable. It turns out the source of the problem was the instability of the 5 volts supplied from the laptop via USB. A check of the Arduino's Vref pin showed that the voltage was varying all over the place. The solution was to use an external power supply to power up the Arduino, so the Arduino's regulator could provide a stable Vref.

I've not done anything with this project recently, so I wanted to document everything. When I'm ready to start again, I won't have to start from scratch!