Tuesday, August 23, 2011

Chaos Theory: Simulating a double pendulum to understand how I owe my existence to Gaddafi's apple

"Does God Play Dice? The New Mathematics of Chaos" is a beautiful book by Ian Stewart. It provided me with a valuable introduction into the concept of chaos and chaos theory. Motivated by the book I decided to experiment with Chaos and write a dynamics simulator on MATLAB to simulate a 2 degrees of freedom pendulum (double pendulum).

In chaos theory 'Chaos' does not necessarily mean random (or a state of disorder). "Chaos occurs when a deterministic system behaves in an apparently random manner"Chaos is actually all around us. Chaos is the rule rather than the exception.

It is only when you think that everything is under 'control' that you wake up one morning to see that yesterday's weather forecast was all wrong and there is a huge storm outside, you check the stock market only to find out that you have lost thousands of dollars. HELL! you turn on the news only to realize that  suddenly the Libyan people rose against a crazy tyrant who ruled them for 42 years. Chaos.

An important characteristic of Chaotic systems is their high sensitivity to initial conditions. Even for a deterministic system (with no random elements), this high sensitivity to initial conditions is what causes the impossibility of long term prediction of the system's behavior. A slight change in the initial conditions would yield completely different and diverging end results.

To experiment with Chaos, I wrote a dynamic simulation of a double pendulum (2DOF pendulum) on MATLAB. The double pendulum is a classical example of Chaos theory.   I used the "Simple Forward Kinematics library for Robotic Chains" that I developed back in 2010 (click here to download), and I developed a new library to compute the joint-space dynamic equations and to perform the simulation for any kinematic chain (click here to download). So back to our chaos theory. To experiment, I ran two simulations:
- The first (on the left in the video below) is a simulation with a base joint initial position of 130 degrees
- The second (on the right in the video below) is a simulation with a base joint initial position of 129 degrees

Notice that although the initial conditions are almost the same, a difference in the trajectory begins to be noticeable only after 7 seconds. This difference increases as time passes. And after 40 seconds we notice that each pendulum is located on a totally different position in space.
We've all heard of the vivid and beautiful term "the butterfly effect". Here it is. The impossibility of predicting the long term behavior of the pendulum.

In 1972, Muammar Al-Gaddafi, while having diner in his tent and surrounded by his beautiful amazonian guards, decided to eat an apple rather than a banana. Imagine he ate the banana instead and it turned out the banana was contaminated with a deadly bacteria. He would have died after a month of suffering. Libya would have been 'inherited' by a crazier person (as is always the case). This person would have changed the entire political scene in the region, maybe causing the Lebanese civil war to be more intense. My father would have decided to flee the war and immigrate to Canada and he wouldn't have met my mother.  I wouldn't have existed. I owe my existence to Gaddafi's apple.

- Simple Forward Kinematics library for Robotic Chains
- Dynamics Simulator for Kinematic Chains

Thursday, August 4, 2011

Sound Triggered Flash: High speed photography

capturing scene invisible to the naked eyes: water balloon popping

I was always amazed with Discovery Channel's Time wrap video clips. So I decided to do some experimenting on my own. A high frame rate video camera costs thousands of dollars... that was out of the question. A compromise needed to be made... instead of shooting videos, let's go for high speed photography. Sound Triggered Flash.  As the name indicates, it is a flash triggered by a sound impulse. Using my SLR camera with the shutter open in a dark room, I would be able to capture photos of sound generating events.

Simple but inspiring. Enough to get me started on the project.

After doing some online search, I was able to find some links for DIY sound triggered flash. They were pretty helpful to get me started, however I decided to drop the research and just go with my own circuit design (more fun).

1. Component list:

- Disposable camera  with built-in flash x1
- Electret Microphone x1
- 9V battery x1
- LM386- Low voltage audio power amplifier x1
- LM339- Comparator x1
- NE555- 555 timer x1
- TIP120- BJT darlington transistor x1
- 10Kohm potentiometer x2
- Resistances: 10K x7 - 5K x 1 - 510 x 2
- Capacitors:   10nF x3 - 10uF x2 -  220 uF x1
- LED x2
- On/Off dip switch x 2

2.   Hacking the disposable camera:

You must have realized by now that we only need the flash circuit from the disposable camera (of course any professional flash would do the job, but for $10 the disposable camera flash is good enough). After cracking open the Kodak disposable camera that I got, and analyzing the amazing ingenuity behind the trigger/shutter/flash mechanisms, i focused on the flash circuit.

Now notice how I carefully hold the flash circuit PCB. Notice how there is a 185uF - 330V capacitor in the circuit. I was actually electrocuted by the 330V capacitor -twice. Not fun. Not deadly, but not fun. (What doesn't kill you makes you stronger doesn't really apply in this case..)
So I have to say this..
Courtesy of "How camera flash work" article
Be careful in handling the flash PCB. It is preferable to discharge the capacitor by triggering the flash before holding the pcb.

As you can see, the flash circuit is powered with a 1.5V AA battery. It consists of impulsing a high voltage into a xenon tube. So the circuit boosts up the battery's low voltage into a high voltage to charge the 330V capacitor. More theoretical info can be found in this nice "How camera flash work" article.

After understanding the circuit, I realized that connecting the two metallic leads M1 and M2 in the above picture causes the flash to trigger. PERFECT! (The circuit differs between different brands, however you will always find the similar two metallic leads). Again DON'T CONNECT THE LEADS WITH YOUR BARE HANDS! I soldered two wires to M1 and M2 as can be seen in the picture below.

3. The electronics:

First step in designing my circuit was to decide on the user interface. I would like the user to:
-Tweak the sound sensitivity using a potentiometer
-Tweak the time delay in the order of milliseconds between the sound detection and flash triggering using a potentiometer. (I realized during experimentation the need for this feature, else the pictures were captured a bit too early)
-Test the sound detection using a LED.
-Press a push button to switch the circuit from triggering a LED into triggering the flash.

I prefer not using a microcontroller for such a simple circuit, it would be an overkill. Let's do it the old challenging way: build my own A/D conversion and triggering.

The idea behind the circuit is simple: 
  1. Pick up the signal using an Electret microphone.
  2. Amplify the signal using LM386.
  3. Compare the amplified signal to a constant threshold (which can be modified using a pot to change sensitivity) using the LM339 comparator.
  4. The comparator's falling edge would trigger a pulse using the 555 timer in Monostable mode.
  5. The pulse would be delayed by charging an RC circuit connected to the LM339 comparator. (The resistance of the RC is actually a pot so that that we could vary the value of the time delay)
  6. The delayed pulse coming out of the comparator would go into the base of the TIP120 transistor switch that would trigger the flash.

M1 and M2 wires are connected to the H1-camera header in the schematic.
Audio: C1 - removes the DC component in the signal
C2 - removes the high frequency noise
LM386- amplifies the signal. C3 is connected between terminals 1 and 8 so that the gain is 200 (46db)
Comparator: Pot1-varies the voltage between 4.5V and 9V at the positive terminal of the LM339 comparator (because the output of the amplifier is biased at 4.5V)
When the signal voltage goes above the voltage specified by Pot1, a falling edge occurs that would trigger the 555 timer to generate a pulse.
555 timer is in Monostable mode: the pulse width is t=R4*C4*ln(3)=2.4 sec.
Time delay: The pulse generated by the 555 timer will charge the RC circuit composed of pot2 and C6.
The RC is a first order system:

which is compared using LM339 to a voltage divider circuit providing 10k/15k*Vin. As soon as this voltage is reached, the flash would trigger. The time delay equation is:

So the maximum time delay achieved with the 10K pot is t =110ms.

Trigger: Finally, the signal switches the TIP120 transistor. If SW2 is pressed, LED2 would light up (for testing purposes), otherwise the flash would trigger.

4. Prototype and PCB

One of the earliest models I made was based on the NE5532 audio amplifier. Here are a few images of the early prototype built with a perforated board.

The reason I switched to LM386 amplifier is for its simplicity, however NE5532 provided the option of having a pot in order to modify the gain.
I designed a single sided Printed Circuit Board of the new circuit

Click here to download Gerber files of the PCB.

5. Experimentation:

Here are a some of early pictures I took:
More experimentation pictures will be uploaded soon.. stay tuned.

6. Purchase

Contact us at DepotBeirut if you'd like to purchase a PCB or even a fully assembled sound triggered flash.