# Logic Gate Linkage Calculations

In my previous post, I simulated some mechanical logic gates using a novel 3-bar linkage.
The dimensions of the linkage are critical to achieving correct operation;

Inputs $$\{ false\ false \}$$, $$\{ false\ true \}$$, $$\{ true\ false \}$$ must all produce the same output position.

For the simulations, an empirical approach was used. (fiddle with the values until it looks right)

This time, I’m going to attempt to find the exact values to use for a given stroke, separation distance and output position.

Here, you can see the linkage layout for $$\{ false\ false \}$$ and $$\{ true\ false \}$$

# Mathematics

Ahead lurks tough mathematics (which may be unsuitable for liberal-arts majors)
If it’s hard to read, then sorry! It was hard to write!

## Parameters

• $$2 sep$$ Separation between inputs
• $$2 str$$ Stroke length $$false \to true$$
• $$d_i$$ Input link length
• $$d_o$$ Output link length

## Geometry

• $$A(x_a, y_a) = A(0, sep)$$ is the input position when $$A_{state}=false$$
• $$A'(x_a’, y_a’) = A'(2*str, sep)$$ is the input position when $$A_{state}=true$$
• $$B(x_b, y_b) = B(0,-sep)$$ is the input position (The system is symmetric, $$B’$$ does not need to be tested.)
• $$M(x_m, y_m)$$ is the middle position when $$A_{state}=false$$
• $$M'(x_m’, y_m’)$$ is the middle position when $$A_{state}=true$$
• $$O(x_o, y_o)$$ is the output position. ($$O \equiv O’$$ for the gate to function correctly.

## Solving

### Finding $$M$$:

$$M(x_m, y_m) = M( \sqrt{d_i^2 – sep^2}, 0)\\$$

### Finding $$O$$:

\begin{align} O(x_o, y_o) &= O( x_m + d_o, 0 )\\ & = O( \sqrt{d_i^2 – sep^2} + d_o, 0) \end{align}

### Finding $$M’$$:

• $$C'(x_c’, y_c’)$$ is the midpoint of $$BA’$$
• $$b$$ is the length of the line $$BC’$$
• $$h$$ is the length of the line $$C’M’$$
\begin{align} x_c’ &= (x_a’ + x_b)/2 \\ &= str\\ y_c’ &= (y_a’ + y_b)/2 \\ &= 0\\ b &= \sqrt{ sep^2 + str^2 }\\ h &= \sqrt{ d_i^2 – b^2 } && Because BC’ \bot C’M’\\ &= \sqrt{ d_i^2 – sep^2 – str^2 }\\ \end{align}\\

Using this, we can calculate M’:
\begin{align} \frac{h}{b} &= \frac{\sqrt{ d_i^2 – sep^2 – str^2 }}{\sqrt{ sep^2 + str^2 }} \\ &= \sqrt{\frac{ d_i^2 – sep^2 – str^2 }{ sep^2 + str^2 }} \\ &= \sqrt{\frac{d_i^2}{sep^2 + str^2} – 1}\\ \\ x_m’ &= x_c’\ – ( y_c’ – y_b)\frac{h}{b}\\ &= str – \sqrt{\frac{d_i^2}{sep^2 + str^2} – 1} \\ y_m’ &= y_c’ + ( x_c’ – x_b)\frac{h}{b}\\ &= str \sqrt{\frac{d_i^2}{sep^2 + str^2} – 1} \end{align}\\

### Finding $$O’$$:

\begin{align} x_o’ &= x_m’ + \sqrt{ d_o^2 – y_m’^2 }\\ &= ( str – \sqrt{\frac{ d_i^2 }{sep^2 + str^2} – 1}) + \sqrt{ d_o^2 – ( str \sqrt{\frac{d_i^2}{sep^2 + str^2} – 1} )^2 }\\ &= str – \sqrt{\frac{ d_i^2 }{sep^2 + str^2} – 1} + \sqrt{ d_o^2 – str^2 (\frac{d_i^2}{sep^2 + str^2} – 1) }\\ y_o’ &= 0 \end{align} \\

## And then:

Now that $$O$$ and $$O’$$ are found, and we know $$O \equiv O’$$…

### Finding the $$x_o’$$ equation in terms of $$d_o^2$$:

\begin{align} x_o’ &= x_m’ + \sqrt{ d_o^2 – y_m’^2 }\\ d_o^2 – y_m’^2 &= (x_o’ – x_m’)^2 \\ d_o^2 – str^2 (\frac{d_i^2}{sep^2 + str^2} – 1) &= (x_o’ – str + \sqrt{\frac{d_i^2}{sep^2 + str^2} – 1})^2 \\ d_o^2 &= \left( x_o’ – str + \sqrt{\frac{d_i^2}{sep^2 + str^2} – 1} \right)^2 + str^2 (\frac{d_i^2}{sep^2 + str^2} – 1) \\ \end{align}

### Substituting back into the $$x_o$$ equation:

\begin{align} x_o &= \sqrt{d_i^2 – sep^2} + d_o \\ x_o – \sqrt{d_i^2 – sep^2} &= d_o \\ ( x_o – \sqrt{d_i^2 – sep^2} )^2 &= d_o^2 \\ ( x_o – \sqrt{d_i^2 – sep^2} )^2 &= \left( x_o’ – str + \sqrt{\frac{d_i^2}{sep^2 + str^2} – 1} \right)^2 + str^2 (\frac{d_i^2}{sep^2 + str^2} – 1) \end{align}
Ugly, but workable?

# Example:

• $$sep = 50$$ Separation between inputs $$100 mm$$
• $$str = 20$$ Stroke length $$false \to true: 40 mm$$
• $$d_i = ?$$ Input link length
• $$d_o = ?$$ Output link length
• $$x_o = x_o’ = 70$$ Desired output position

\begin{align} ( x_o – \sqrt{d_i^2 – sep^2} )^2 &= \left( x_o’ – str + \sqrt{\frac{d_i^2}{sep^2 + str^2} – 1} \right)^2 + str^2 (\frac{d_i^2}{sep^2 + str^2} – 1) \\ ( 70 – \sqrt{d_i^2 – 50^2} )^2 &= \left( 70 – 20 + \sqrt{\frac{d_i^2}{50^2 + 20^2} – 1} \right)^2 + 20^2 (\frac{d_i^2}{50^2 + 20^2} – 1) \\ \end{align} \\
Pushed through Wolfram Alpha…
\begin{align} \\ d_i &= \sqrt{2900} \\ &= 53.852\\ \text{and}\\ d_o &= x_o – \sqrt{d_i^2 – sep^2} \\ &= 70 – \sqrt{ 2900 – 2500 } \\ &= 50 \end{align}

# Summary:

Phew!

Thusly, one can calculate the input and output linkage lengths for any logic gate.
These dimensions will work for an OR gate too, as the linkages are just reversed.

It’s irksome that I couldn’t work out a symbolic solution for the last equations, especially since $$d_i$$ in the example came out to such a neat value.

I might revisit this further in the future, and it’ll definitely come in handy when building something with these gates.

# Springless Reversible Mechanical Logic

Logic gates! The building blocks of computing as we know it. What if we ditch the electrons?

I want some mechanical logic for an upcoming project, so I did some research…

# Research

There are several examples of mechanical logic out there on the internet:

Keshav Saharia’s Lego Logic Push-pull, needs a spring return.
Randomwraith’s Lego Logic Push-pull with internal rotating elements, reversible.
Mechalogic’s Logic Elements Push-pull, AND & NOT reversible, OR needs a spring return.
Xiaoji Chen’s Linkage Computer Rotating logic, reversible, some mechanical issues with parallelogram weakness.
Spillerrec’s Lego Logic Gates Push-pull, very compact, needs a spring return.
He identifies the problem with needing springs, but doesn’t have a fix. Mentions how AND/OR gates can be reversed to become OR/AND gates.
Zeroumus’s AND Gate Push-pull, needs a spring return.
KNEX XOR Gate Push-pull, needs a spring return.
Carolin Liebl and Lisa Hopf’s NAND Gate Push-pull, reversible, complex.

There are many more ideas out there, even water pipe logic, and of course the early mechanical computers Digi-Comp I and Digi-Comp II.

For my project, I have the following requirements;

• Push-pull logic
• Output movement range must equal input movement range
• No springs/elastic bands, and no forces for inputs to overcome
• No gears!
• Easily chain-able, and simple enough to implement a score of them.

This disqualifies most of the designs I found, particularly the ‘no springs’ requirement.

# AND Gate

Mechalogic’s AND gate design uses an elastic band, but doesn’t appear to need it, so it serves as a good starting point:

It’s time consuming to tweak parameters in the physical world, so I wrote a small simulation: (click to see in action)

Success! Meets all the requirements, and only needs three parts.

The geometry is important – the ratio will determine the output’s position when only one of the inputs is asserted, according to;

• distance between the inputs (red),
• length of the input links (green),
• length of the output link (teal)
• input displacement

Now, can an OR gate be designed with the same success?

# OR Gate

Most OR gates look like this; the output (on the left of this picture) has a flat plate, and either input will push the flat forward. Unfortunately the design needs that elastic band to return the output to zero.

Inspired by some earlier attempts to couple an AND and a backwards OR gate together, I tried flipping the AND gate around; (click to see in action)

Success! Oddly enough, an OR gate is just an AND gate connected up backwards.
Even the linkage lengths are the same!
Best of all, the OR gate is reversible like the AND gate – no springs and no force required.

# AND Then?

As the graphs show, output displacement equals input displacement, so these designs should be chainable. Let’s have a look; (click to see in action)

Success!

Now I just have to build something complex with them…

All the simulations are viewable here.
The code they’re written with is available on GitHub;
https://github.com/MrTrick/logic_gates

# Javascript: Self-replacing Promises

My application has several resources that need to be loaded just once, and used often afterwards.

Here is a pattern that I’m finding quite useful lately:

/**
* Retrieve the resource once only.
* If the resource hasn't been loaded yet,
*   return a deferred that resolves to the resource.
* If the resource has been loaded,
*   return the resource directly.
*/
App.getResource = function() {
//Return the resource or the deferred if it's set already
if (App._resource)
return App._resource;
//Otherwise build, set and return a deferred that
else
return App._resource = $.getJSON("resource/url").then( function(data) { console.debug("Loaded resource"); return App._resource = data; } ); }; • The first time the function is called, it fetches the resource and returns a deferred object. • When the promise resolves, it passes the resource to handlers. • If called again before the promise resolves, it returns the same promise. (won’t fetch twice) • After the promise has resolved, the function instead returns the resource directly. • If the resource can’t be loaded, the promise fails instead, and is not replaced. To use directly; $.when(App.getResource()).done(function(resource) {
//Use the parameter from the promise
resource.doStuff();
});

The $.when call transparently converts a non-promise into a resolved promise with that value. The snippet behaves the same whether App.getResource() has been previously loaded or not. So why replace the promise at all? Sometimes you need to be able to treat the resource like a synchronous value, such as within a library or a function that is called synchronously. //Here's a class with a synchronously-called function var Model = Backbone.Model.extend({ ... validate: function(attrs) { if (App.getResource().exists(attrs[id])) { return "Model with that id already exists"; } else { return null; } } }); var model = new Model(); //Wait for everything necessary to load first.$.when(App.getResource(), model.fetch(), ...).done(
function() {
if (model.isValid()) doFoo();
else doBar();
});

This code waits for App.getResource() to resolve, as well as for any other needed promises. Afterwards, App.getResource() can just be treated like a synchronous function.

This pattern enables flexible and transparent use of remote resources, in both synchronous and asynchronous ways.

The App.getResource() function is also quite similar to a Lazy Promise, as discussed by James Coglan.

# Cherry Ripe Cheesecake

I’m having trouble getting good photos of my desserts… they keep being eaten! Here’s another hard-to-photograph success; Cherry Ripe Cheesecake!

# Cherry Ripe Cheesecake

Makes a 25cm dia cheesecake

## Base

A: 375g good-quality chocolate biscuits
B: 125g butter, melted
C: 3tbsp cocoa (for chocoholics)

Process biscuits in a food processor until very fine. Mix in cocoa.
Add melted butter and stir together.
Line a spring-form tin with paper and press the mixture into the base and sides of the tin.
TIP: Pour about 2/3rds of the mixture in, and make the sides first! Then pour the rest in and flatten out a base.
Place in the fridge to set.

## Filling

A: 200g dark chocolate
B: 4 large cherry ripe bars
C: 1 tin black cherries, drained
D: 750g cream cheese, at room temperature
E: 100g caster sugar
F: 3 eggs

Chop up chocolate and cherry ripe bars, place in a double boiler to melt.
Beat cream cheese and sugar together until smooth. Beat in eggs, one at a time.
Once chocolate has melted take it off the heat, and ensure it’s not too hot.
Beat in the chocolate mixture and cherries.
Pour the filling into the springform tin, and smooth out the top.
Bake at 180°C for 50 minutes.
Switch off the heat and prop the door very slightly ajar, let cool for 45 minutes.
Refrigerate for at least 3 hours.

## Topping

A: 300g cherry jam
B: Cherry ripe bars, to decorate

Microwave jam in a heatproof bowl until liquid.
Pour evenly over cheesecake.
Chop or slice cherry ripe bars as desired, decorate top.
Place back in fridge for at least 5 minutes before serving.

Almost ready to eat!

And almost gone!

# Eagle: Text Silkscreen Width

Thanks to Hackvana, I’m making teeny tiny circuit boards!
No matter how good a board house’s process, there are some fundamental limits to how small things a board and the design on it can be.

A PCB is much easier to understand with a silkscreen! However, you can’t draw silkscreen lines narrower than a certain width, often about 5 mils. (Or 4 mils if you’re really lucky) So you’d better make sure none of your silkscreen is smaller, or lines will be spotty and text will be unreadable.

(Huh? What’s a mil? / One thousandth of an inch. / Okay. What’s an inch? / …)

With Eagle able to display line widths in mils, it’s not too difficult to check your lines.
(How do I get mils? / Click on the Grid button in the upper left, and change the grid units to mils. You can change it back later)

Text Settings: Size: 20, Ratio: 5%. NO.

Here is the text “ISP”, and the text attributes dialog.

• Note that font has been set to vector. Keep your text in vector mode, it will ensure that the Gerber files you output are likely to match what you see inside Eagle.
• The grid size in these examples has been set to 10 mils
• There are two important settings – Size and Ratio.

Size is the height of one row of text.
Ratio is the text’s line width, as a percentage of size.

• For the above image, the line width is 5% of 20mils – only 1mil! That definitely won’t work!
• Remember; SIZE x RATIO > 5 mils

## But I Want Small Text!

That’s fine. You’ll just need to make it thicker.

Size: 20, Ratio: 10% : 2 mil thickness. NO.

Size: 20, Ratio: 20% : 4 mil thickness. NO.

These are still a bit small…

Size: 20, Ratio: 25% : 5 mil thickness. JUST…

This one is probably juuust thick enough. But the text is only half a millimetre high!

# Okay, I’ll be sensible…

Good. 50 mils (1.27mm) or larger text is probably more sensible. If size is 50, and we need at least 5 mil silkscreen, then the ratio should be higher than 10%. Easy!

Size: 50, Ratio: 5% : 2.5 mil thickness. NO.

Size: 50, Ratio: 10% : 5 mil thickness. JUST.

Size: 50, Ratio: 20% : 10 mil thickness. GREAT! And you still need reading glasses!

# One more time, graphically

WIDTH = SIZE x RATIO

# In Conclusion

A board house will often tell you their dimension limits, for traces, isolation, and silkscreen.
For best results, avoid the limits.

If you can make your text 10 mil thick, and the board house can manage down to 5 mil, you should have no problems.

# Salted Caramel Sauce

Left-over cream, what can I do with this? Cribbed a few recipes together, and improved the technique.

Salted Caramel Sauce

A. 500g caster sugar
B. 150mL water
C. 300ml double cream
D. 250g butter
E. 1 heaped tsp salt

• Heat cream, butter, and salt in a small saucepan, at low heat.
• Stir occasionally, continuing on with the recipe, and switch heat off and cover once the butter is dissolved.
• In a heavy-based medium saucepan, heat sugar & water together until the sugar fully dissolves.
• Increase the sugar mix’s heat to high, and bring to the boil.
• When boiling cover the pan for 2 minutes, to allow the buildup of steam to dissolve any grains of sugar on the sides.
• Uncover the pan, boil without stirring.
• Watch pot carefully! When mixture reaches >170ºC and becomes golden, remove from heat. (IR thermometers are great)
• With a long-handled spoon stir the mixture, while very slowly pouring in the cream/butter mix. The mixture will foam up a lot!
• When all combined, put back onto medium heat, still stirring.
• Bring mixture back to the boil for a few minutes, lifting the pan off the heat if it threatens to boil over.
• Let cool for 10 minutes, then taste test. You might need to sample several times.
• Pour into jars, over ice cream, cakes, whatever you like.

# Salted Caramel Peanut Chocolate Tarts

Here’s my new very dangerous tart recipe.
Not for diabetics or those with heart conditions!

## Pastry

A. 250g plain flour, sifted
B. 3tbs cocoa powder, sifted
C. 3tbs pure icing sugar, sifted
D. 165g chilled unsalted butter, chopped
E. 30ml (2 tbsp) chilled water

• Process all ingredients except the water in a food processor until fine powder.
• Add the water, process until combined.
• Turn out onto floured board, knead for 1 minute, roll into a ball, cover with glad-wrap and refrigerate for 15 minutes.
• Divide the dough into 8 portions, then for each portion;
• Roll into a 12cm-dia circle, place into an 8cm-dia flan case, trim excess with a knife
• Place on a tray in the freezer for 30 minutes
• Preheat oven to 200ºC.
• Cover the cases with baking paper and pastry weights, bake for 10 minutes.
• Remove and bake for 5 minutes longer – should be dry to the touch.
• Let cool completely.

## Salted Peanut Caramel

A. 300g caster sugar
B. 180ml water
C. 250ml double cream
D. 120g salted peanuts, finely chopped

• In a heavy-based saucepan, dissolve sugar & water at low heat until the sugar dissolves.
• Set heat to high, and boil without stirring. Occasionally remove sugar crystals from the side of the pan with a wet pastry brush.
• Watch pot carefully! When mixture reaches 160ºC and becomes deep golden, remove from heat. (IR thermometers are great)
• With a long-handled spoon stir cream quickly into the mixture – it will foam up!
• Put back onto medium heat. Stir for at least 2 minutes, until mixture is smooth and bubbles up again.
• Let cool for 30 minutes.
• Stir in peanuts.

## Ganache

A. 150ml double cream
B. 150g dark chocolate, coarsely chopped

• Chop, process or grate the chocolate into small pieces, put in a jug.
• Heat the cream (but do not boil), then tip into the jug.
• Stir with a spoon until the chocolate is combined, then mix with a stick blender until smooth.

## Putting it all together

• Pour caramel into each case, leaving room for the chocolate layer, tilt cases to spread caramel
• Put back in freezer for 5-10 minutes
• Pour ganache into each case, tilting to ensure coverage.
• Place a few flecks of sea salt in the centre of each tart.
• Chill until served.

The tarts will be very rich. Serve with cream or ice-cream.

# Atmel Studio: Clearing debugWIRE

debugWIRE is a fantastic bit of technology in many recent AVR chips, allowing you to set breakpoints, step through your code, and a bunch of other useful features. All you need is a programmer that supports it – the AVR Dragon for instance.

In the Atmel Studio project, simply set the Debugger/Programmer Interface to debugWIRE.

The only fly in the ointment is that for extremely low-power applications, having the DWEN fuse and thus debugWIRE enabled will prevent the chip from fully sleeping, meaning your device is much more thirsty than expected.

So you set the Debugger/Programmer Interface back to ISP, and try to program your device again; Failure! In fact; CRYPTIC ERROR MESSAGE!

TO CLEAR debugWIRE FROM A CHIP WITHIN Atmel Studio 6:
Start a debug session on the chip, then
Go to the “Debug” menu and select “Disable debugWIRE and close”.

Now you can access the chip using ordinary ISP, and low-power devices will be more frugal.

This may seem an obvious thing, but there are a number of “solutions” floating around, including people advocating hooking HVPP programmers into devices simply to clear DWEN!