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.


Friday, July 22, 2011

Lebanese electronics shops: the TSOP experience.

As everybody working with electronics in Beirut would agree. We don't have a great accessibility for components and IC's. If I am dealing with non common components, I don't even bother looking around in the local electronics hardware shops that we have (Katranji, Incotel, Boujikian, Narinco, etc.) I wait till I gather a big list and I order my components from abroad (that is an extra 50-100$ added to the bill).

I will not talk about shipping in this post, however I will be talking about the wonderful experience one has to deal with in our lovely local electronics shops.

Starting with Boujikian, all I can provide you is a simple advice: if you have an Armenian friend, tag him along with you.

Now I would like to share a nice story. A few weeks ago I was building an IR remote control emitter/receiver circuit (I will provide info for the circuit in another post). So I needed to buy an IR receiver chip TSOP1738 that only works with carrier frequency of 38KHz. Perfect.

First thing: Check Katranji's website if they have the component. (+3points to katranji for having a website) However the website search engine sucks, so you have to look manually for the part (-1pt). After not finding the part on their website. You would grab your component name/number and head to Jneh in the middle of the traffic jam and heat. Once you get there, don't expect to be treated well. Sometimes don't expect to be treated at all. (-1pt). I was lucky enough to find a decent help, lets call him Nabil (hypothetical name for a hypothetical person)
-Bassam: Can I have the following component please.
Nabil stares at his monitor for 5 minutes only to tell me that they don't have it.
-Bassam: Do you have anything equivalent. TSOP18...21.. wlek 28... anything at all...
Nabil stares at his monitor again 5 minutes only to tell me that they don't have.
-Bassam: But sometimes my friends come to get some phototransistors, and you either end up giving them photodiodes or TSOP chips (true story), so I am sure you have something equivalent.
Nabil goes backstage for 5 min and comes back with a TSOP looking chip (not TSOP thou).
-Bassam: Ok great so what is the name/number of this chip? 
Nabil stares at his monitor for 5 minutes only to tell me that they don't know.
-Bassam: Tayeb, what is the carrier frequency for this chip.. anything.. just give me any info.
Needless to say, Nabil didn't know.

I ended up going back to my workshop and spending 2 hrs trying to figure out pointlessly how to make the chip work. So this leaves Katranji with 1 point in my improvised grading scheme.
Don't get me wrong, i have a complicated love/hate relationship with Katranji & Nabil that only the people knowledgeable in electronics in Lebanon would understand, but you have to admit Katranji does have a wide and good range of components available. And most of the time, if you were instructive and knew what you want, you will end up finding at Katranji's.

Getting frustrated. I called Incotel. The guy was really friendly. and as soon as I told him  I need a TSOP1738, he told me that they don't have it but they have something equivalent. I couldn't believe it. His voice was like a divine melody in my ear. I went to Incotel, got the needed chip. The guy even suggested a IR led that works on the same wavelength as my chip. Perfect +10pts for Incotel for their great customer support. 

So I bought 4 TSOPs and 4 LEDs and was about to leave when I asked for the bill. Ok. -8pts for Incotel. I won't tell you the price so that you won't get depressed. But I can say that it was the expected price x4!!
This leaves Incotel with 2pts on my improvised grading scheme.
Incotel 2pts vs.  Katranji 1pt
Well if you don't care about price. Go to Incotel... but honestly you'd be better off gathering a big component list and ordering from abroad. 

N.B. I will be criticizing other electronics shops as I write in my blog. And I will be elaborating more on my love/hate relationship with Katranji and Nabil.

Tuesday, July 19, 2011

Mr. Warhol here is my chance...15 minutes of fame can also be life changing.

Back in 2008, I attended a presentation about a freshly starting pan arab competition: Stars of Science. It immediately got my attention. The concept of the competition is simple. You have an idea for a product or an innovation of some sort, you present it, if it gets accepted you go to Qatar and develop it with the help of experts. 

It all sounds perfect and nice.. oh wait... there is something missing... it is a reality TV show!! You actually have to work all day, all week, for months in a workshop full of cameras, microphones, and its going to be broadcasted all over the Arab world. Everybody will be observing every step you take, every mistake you make. Euh.. scary? but this must be something new in my life. Once in a lifetime opportunity to try something new. why not? let's apply.

Mr. Warhol here is my chance.

So what about Stars of Science? a brief description:
Stars of Science, initiated by Qatar Foundation in 2009, is the first Pan-Arab reality-TV program dedicated to innovation, aiming to shine a spotlight on the next generation of young Arab innovators. Following an international recruitment campaign among more than 5000 people, 16 young women and men of Arab origin, from different academic and professional backgrounds, are selected by a Pan Arab jury and given the chance to develop their own innovation project. The selected candidates are invited to enter a specially-designed workshop, located in Doha, where they have access to an array of state-of-the art resources as well as the support of top professionals. The 16 take part of a competition in successive rounds, each of them corresponding to a key stage of the innovation process (proof-of-concept, engineering, design, business and marketing). 
In 2009, I applied and I was selected for my product- an automated tuner device for all string instruments that I called Dozan (stay tuned as later I will have a post dedicated for Dozan from early stage till now). After successfully passing all the stages I won the 1st prize in the competition.
Mr. Warhol, here comes the 15 minutes.
The media buzz was intense here are a few articles: dailystar, maktoob, (if you google you will find plenty of international magazines and blogs also). I was even announced one of the 30-under-30 of the Arab world’s best and brightest entrepreneurs by ArabianBusiness.com.

Wow, ok, great. now what? Honestly, a part of me enjoyed the 15 minutes but a bigger part wanted them to be over because i knew that this is when the real work starts. Reality hits. WORK work WORK! I want to finish my masters, my research and I want to develop Dozan and make it a success story and share my experience with fellow local entrepreneurs and innovators going on the same path. Oh and I would like to have a hand in changing the arab world from a consumer to a producer of technology. too much to ask for? step by step.

Mr. Warhol, I had my chance and I would like to tell you that even though the 15 minutes can be over, but they can also be life changing.

p.s. to all the young innovators in the arab world reading this, go to Stars of Science and apply for the coming seasons!