Friday, 12 November 2010

Electronics for Dogs

Okay so I've not posted in over a year. I've had nothing to say, so I've not said it.
Anyway I remembered this morning a project I started years ago and never quite finished. So I thought I'd have another go at it on here.

The challenge came from the idea that I think engineering and computers are actually really simple, people just think they're complex because what you have is a great number of very simple concepts working together and you have to understand most of then to understand any of them.
For me it came as a revelation about 3/4 of the way into my electronics BTEC that ever since then the concepts have been easy even if the maths and execution is difficult. Therefore I wanted to explain the easy bit to help people understand this fundamental part of the modern world even if you do leave the complexity up to those who have studied.
The project aim was expressed at the time as "To explain electronics in such a way that my girlfriend could understand it". Which goes to show how much I have learned over the past few years because I should have known how bad an idea it was to express the concept in such an insulting form. To then go on and tell her that I was naming said project "Electronics for Dogs" in a tribute to Wallace and Gromit goes on to show just how badly I could get things wrong and explains why I spent almost all of University single.

So here we go, I present the first chapter, if this proves successful to me then I'll post more later.

Computers are complex and a pillar of modern society. They're clearly designed by really clever people. Rubbish. I design them and that should be enough justification for you that they're not that hard.
To start geeky as I mean to go on Tom Barker in his first Doctor Who story expressed it best "A computer is a very fast idiot. They do exactly what you tell them to at amazing speed. Which becomes a bit of a problem if you've told them to do something you don't want them to."
I don't think the average person would believe just how simple a computer actually is at its heart but it'll be a while before we can get to that, let's start off with a simple problem. A key code door lock. It's a simple device really, everyone can understand how to use one, but how hard is it to make one? Very simple actually, provided you break the problem down. This is going to be the key to everything we do if you'll pardon the pun. It's crucial to say that at each stage you may ask, "but how do you do that?" to which I could explain and hopefully will later on in further chapters but the most vitally important part to getting your head around all this stuff is the ability to say to yourself "I won't worry about that now, that's someone else's problem" Up until now, how a computer works or how to build an electronic door lock might have been someone else's problem, well let's try and peel the onion a bit, but don't worry yet about what lies at the heart of the onion we'll get there, but it may take a while. All I will say is that if you can follow this first article you too can understand the depths of your computer because this one covers what I believe is the hardest concept.
However just so you know here's the plan I assume you know nothing about the internals of computers or electronics or anything above basic physics, so you'll have to bear with me otherwise it will go:
Student: How does a computer work?
Me: Erm I need to explain programming languages [explanation follows]
Student: well how does that run on a computer
Me: Well I have to explain what a processor is [explanation follows]
Student: Well what is a logic gate?
Me: Well I have to explain what a transistor is [explanation follows]
Student: But I don't understand quantum mechanics
Me: Neither do I, just trust me on this. (In fact there is the quote that "If you are not completely confused by quantum mechanics, you do not understand it")

So for our first discussion we talk about programming languages and only programming languages. Trust me that next time we'll talk about how you could implement a programming language in a way that it would run on a machine. It doesn't matter yet that you understand how that machine would look, just that you follow the basic concepts in the programming language bit.

So let's take that door lock, lets break the problem down into a few steps.

1) If the user presses a button, is it the clear button? If it is then clear everything otherwise:
2) Does the button match the first digit in the code? If it is then wait for the second button.
3) Does the button match the second digit in the code? If it is then wait for the third button
etc until
4) have all buttons been pressed in the correct order? If yes then unlock the door.

That does help us break the problem down and so is helpful, but it's a bit confused, but often your first attempts are a bit confused.
Let's introduce some pseudo-code, that is a way of writing some of these things in a way that while still ambiguous is more precise than the English language. Over time we'll develop this pseudo code into a more precise language.

What I'll say is "if {something or other} then {something we want to do} otherwise {something we'll do instead}.

So let's try that door lock again shall we, hopefully this time a little bit more precise.
if {someone has pressed a button} then {does it match the first digit of the code? } otherwise {it's the incorrect code so do nothing until someone presses clear}

Hang on though before we go any further and finish that off notice that our first thing to do is another question. so let's take that and break it down again, we could re-phrase that as:
if {button pressed is equal to first code digit} then {first digit is pressed so we need to test for next code digit}

which means the thing to do could again be written as
if {button pressed is equal to second code digit} then {second digit is pressed so we need to test for next code digit} otherwise {it's the incorrect code so do nothing until someone presses clear}

This is all getting quite confusing now, having these statements inside statements means things get awefully complex very quickly. What we need is a way to remember what button press we're up to. So what about having seen what I've written above can we produce something more clean? Well have this thing, this variable, I'll call it anything I want, let's say last_correct_code_pressed that I can set to a value and when i reference it in other parts of the program the pseudocode will know that I'm referencing this thing I set earlier. It doesn't matter what it actually is, or how the code does it, as long as the reader (and therefore later the machine) knows that I'm referencing this thing.
Let's try restating the problem as

set last_correct_code_pressed to 0
if {a button is pressed} then
{
if {button pressed is equal to the next code in the sequence} then {
set last_correct_code_pressed to last_correct_code_pressed +1
if {last_correct_code_pressed is the last code in the sequence} then {open the door }
}
otherwise
{
set last_correct_code_pressed to 0
}
}
Okay it's quite a different way of stating the problem but hopefully you were able to follow what we're trying to say, by breaking the code across multiple lines and using the brackets to keep the code together we can break the problem down. Now the above isn't a full solution to the problem neither will it run on any computer in existence. That is not the point of this exercise. There are lots of books on how to write software code that will take you from the concept to the implementation. The point I'm trying to start with is that you have the concepts.

To push the concept further now you might ask, "well how does it know a button is pressed?" Well, to give you a quick answer, assume there are the numbers 0 to 9 on a keypad. in a typical implementation you would have a bit of code that every few milliseconds (that is thousands of times a second) does the following:

button_to_check = 0
while {button to check is less than or equal to 9} then {
if {the button to be check is telling me that it is pressed} then {tell who needs to be told} otherwise {button_to_check = button_to_check +1}
}

The important part here being this while statement will keep going round and round within itself until button_to_check is equal to 10. if it detects a button pressed it will go and "call" whoever needs to do something about it. Given that this is happening in our example 1000 times a second you can understand it's probably for the best that nothing much happens a lot of the time.

"Ah, but" you say "You still haven't answered how a button can tell me it is pressed"
To which I will say, "good point". Now this is the really weird bit, or the really trivial bit depending on your point of view. Assuming you followed the above with the button_to_check then you already know the answer. In the same way there was a variable called button_to_check that you could store a number in there can be a variable called (for example) button_pressed that tells you what button is pressed. How that piece of magic happens will for the moment remain magic, but trust me it will become clear later.

Now I'm not saying that's all there is to programming, far far far from it, you wouldn't spend 3 years at university learning computer science if that was all there was to it. That is however the start and almost enough. Let's jump a few stages from key locks to something much more complex, let's try a web browser. In our pseudocode parts of a web browser's code might look something like:

if {user presses back button} then {switch address to previous page}
if {user presses refresh button} then {reload this page}
if {user presses home button} then {set address to recorded home page}

Hang on we've yet to actually draw the web browser on screen yet, okay so lets take a step back; after firefox loads, code equivalent to this might run:

Draw a generic window this_wide by this_high at this_location on the screen
put the firefox logo at this_location
Set the window title text to Blogger: Double you - Create Post - Mozilla Firefox
Add a file menu
Add an edit menu
Add a view menu
etc

Hang on though, you ask how do you draw a window?
Simple, you ask the operating system to do it for you. Most people don't care because it is someone else's problem but let's assume you're curious; as always break the problem down:

Draw a grey box this_wide by this_high at this_location on the screen
Draw the minimise, maximise and close icons
Draw the title bar from here to here in this colour.
etc

Fine you ask, how do you draw a grey box on the screen, (answer: you ask the graphics card driver to do it for you), then knowing that you ask how does the graphics card make those pixels light up etc.

Hopefully you can see that this is all about taking what appears to be a very complex problem and breaking it down. As soon as you can break it down into logical steps you can then break each of those steps down into more and more steps until you end up with something so simple even an idiot like a computer could do it. The only thing a computer does is these very very simple things very very very fast.
What those simple things a computer does is a topic for next chapter.

Wednesday, 28 October 2009

How to make everything right wrong again

No post for ages - work has been busy, not that anyone follows this so no worries there. Also I had nothing interesting to say and the last thing the internet needed was more drivel.
An interesting idea was given to me today after reading a bbc article about Northern Rock being split into a "good bank" and a "bad bank".
Now as I read this all the good/reliable mortgages and business will be packaged together and sold off to make some of the bailout money back. This is a good thing.
As for the bad bank then as I read this it will exist solely to hold onto the bad mortgages. Now it was pointed out to me that these mortgages will eventually come to the end of their term. The question is what happens next? Assuming that this bad bank doesn't issue new mortgages then the people facing this will have to go to the open market to look for a new deal. So the good profitable parts of the business will eventually dwindle and leave, leaving only mortgages that foreclose or people who are unable to go elsewhere.

With my cynical hat on this seems like a very efficient way to make sure that the taxpayer is left with all the bad debt and the free market only has the reliable debt. This may be a good thing for the financials of the banking iundustry, but it does undermine the concept that the banking industry would eventually pay back the bailouts.
Now I'm not that naive, but it does seem that those of us currently without mortgages are being charged for those who drew a straight line on growth curves; which is a little unfair, but hey we already have an idiot tax in the form of the national lottery so this feels like a "not being an idiot" tax. Ah! Smug self righteousness, better than any drug I know!

Tuesday, 24 March 2009

Mad Salt Lake Idea

I think this idea must be mad, because no-one seems to have done it yet; however I don't understand what the problem would be, so I explain it here to be deconstructed.

There exist in the world a large number of salt flats that are to my knowledge to all intents and purposes barren wastelands with no useful purpose to the ecology or humans. I propose to change that.
There is no shortage on this planet of seawater so we pump the seawater back to these salt flats and create new salt lakes. This would give nice shallow water, unfortunately it'd almost certainly be too salty for anything but the most hardy algae to live in. So what would creating a new dead sea give us.
  • Increase precipitation
  • Cooling effects of bodies of water on surrounding area
  • You could use it as cooling water runoff area for power plants.
  • Salt production
Not major benefits it might seem except that people are suggesting building boats spraying sea water to create more clouds to combat global warming. Also there's always talk about the lack of fresh water, so here's a way to increase it.

The only problems I can see with this would be the energy cost of pumping the water (which might effectively be free if the power plant cooling is available) and the potential corruption of the water table. If that was a problem though then presumably every time it rained on one of these locations you'd get irreversible salt water corruption of the water table.

Precognescence In sportsmen

This is an old thought of mine that I thought I'd share; it's a thought experiment so:

Suppose precognescence is real.
Suppose this talent is particularly useful in sports such as motorracing where knowing ahead of time for example exactly how much grip you have available would be an advantage. Similarly knowing that someone is going to try and overtake you by dodging to the left before they do it would be a huge advantage.
Now suppose that because the offspring of sportsmen/women are likely to interbreed and produce offspring likely to go into that sport themselves then you have set up perfect evolutionary conditions for enhancing this effect.

To summarise - if the ability to see into the future is possible, and this is a genetically carried feature, expect to see it in sportsmen.

Now the existence of this sort of this goes against what you would term my belief system (i.e. all currently observed scientific data) however that's never a reason to fully discount something so: how could we test for it?

Easy - change the penalty for jumping the start of a race. At the moment (taking F1 as an example) there is a random delay from all lights going on, to the lights going off and the race starting. If you are judged to have moved before all lights go off you are judged to have jumped the start. Change this so that no penalty is given if you are sufficiently close to the lights going out. If you have no precognescence, then jumping the start will then most of the time give you a penalty, however if you can truly anticipate when it is going to happen then it'll soon become clear which these drivers are.

I know what results I'd expect to see (i.e. that nothing really changes) but the universe could yet surprise us.

Monday, 9 March 2009

New Speed Limit?

According to this there's plans for a 50 limit on single carrageways.

Despite the fact that quoting the governments own statistics:
  • A 40% reduction in the number of people killed or seriously injured (KSI) in road accidents;
  • A 50% reduction in the number of children killed or seriously injured (children are defined as being those aged under 16);
  • A 10% reduction in the slight casualty rate, expressed as the number of people slightly injured per 100 million vehicle kilometres

Compared with the 1994-98 average baseline, in 2007

  • The number of people killed or seriously injured was under 31 thousand, 36 per cent below the baseline.
  • The number of children killed or seriously injured was 55 per cent below the baseline.
  • The slight casualty rate was 32 per cent below the baseline.
  • In this period the traffic has risen by an estimated 16 per cent.
So things are in fact getting better.
We have to fight this! Time to get onto writetothem

Monday, 23 February 2009

Sparticus Invented the internet

Very busy at work so another post I wrote a long time ago and never got around to posting as a reply to someone who annoyed me...

Look I'm fed up with this "we invented the internet" attitude. Lots of countries had their own nationwide computer networks, some educational, some military; it just so happens that Arpanet became the successful one.
Does it mean it was the better network? Maybe, but if Arpanet had never existed, then any of the other networks in existence in any other country could have become dominant and we'd still be surfing the net, just the underlying protocol may have been based on some ISO/OSI model instead of the IP model (for example). or on anything else any of those prototype networks used.

Maybe people feel that the US is vital to the internet? Fine unplug yourselves and see who suffers more. I'll truly miss google, wikipedia, archive.org - but life will carry on, I'll still keep sending emails to all my friends, I'll still make VoIP calls to my parents and I'll still keep getting new versions of the latest Linux distros.

I see the fact that Arpanet/TCPIP "won" as no more of a validation of the technology than VHS over betamax. Please remember that most trunk traffic still travels over ATM, and that most broadband connections run as ATM too; the link to both my place of work and my home are both IP over ATM.
An old rule always applies that given two competing products the American one will normally win - the Transputer, IBM PC, VHS etc.
On that note I'm waiting for some American company to rediscover the Transputer in the next decade and 30 years later I'll be here still online with people saying that Americans invented parallel computing like they claim they invented computers. ENIAC may have been the 1st by some definition but it was neither the first electronic computing device (Colossus) or the first stored program computer(Manchester Mark 1). It's a half way house and computers evolved - you might as well try and find when a chicken first evolved.

Not that I'm complaining of course - I'm employed by an American company, but I still find it fascinating how these things pan out.

Wednesday, 11 February 2009

Embryo/Abortion Rant

I wrote this a while ago as a rant after an online discussion and i never got the chance to post it. So here it is:

A human embryo has the potential to become a human being. An embryo has no claim to humanity other than the DNA/other gubbins it contains. Change the DNA for another chunk of DNA and you no longer have a human.
So does the information in the DNA count as humanity? I hope not, if we have sequenced the DNA of a human, then each copy of this I delete puts me as a murderer.

You cannot make judgement based on something's potential. The obvious example is I have the potential to be a murderer, but you don't arrest me. Any woman is equipped to be a prostitute, but you don't send her to jail.

When the day comes that we can take a computer representation of a cell and hook it up to a machine which will spin a cell from that, then every computer in the world will have the potential to be a father/mother.

Is it intelligence then? I'd argue most dogs show more intelligence than a new born baby, so why do they have different rights? How about disabled adults who either mentally or physically can't perform functions we'd expect of a human.

No, there is no scientific answer to when a human becomes a human; you might as well ask where a rainbow starts.