I love to learn about new things and challenge myself, but crossword puzzles are so old school!  I have found something far more interesting, legal sites aimed at training people on essential hacking skills! – Hack this site and hack this are firm favourites.

This blog post contains the perl script I used to solve the basic programming level 1 challenge on Hack this site!  The mission is simple, descramble a selection of words within the allotted time.  To solve this problem I implemented a perl script, this is given below, read the comments to see how I approached the problem.

#!/usr/bin/perl -w

# Written by Craig Barry 24/2/13
#
# A program designed to descramble words
# Call the perl script and '>' to point the output to a txt file for ease of use in
# programming basic 1 on www.hackthissite.org/missions/prog/1/
# eg. bash$: perl HTS_sortwordlist.pl > descrambled_words.txt

# The script first parses the dictionary file (wordlist.txt) and hashes the sorted characters
# against a primary key, (the dictionary word).
# It then accepts the scrambled words that need to be identified.
# These are stored as sorted characters, eg. handle --> adehln
# We then iterate through all of the sorted dictionary words,
# When we find a match for a hash value (sorted dictionary word) we look up its corresponding
# key, which is the actual dictionary word, this is then piped to our output file, '>'

use strict;
my ($word,$sorted_word,@chars, %list_hash,$sorted_dict);
my $dictionary_file = "wordlist.txt"; # sub with <> for user input!
my $scrambled_file = "scrambled.txt"; # sub with <> for user input!

open(my $dict_FH, '<', $dictionary_file); #$dict_FH is my file handle for dictionary file
open(my $desc_FH, '<', $scrambled_file);  #$desc_FH is my file handle for descramble words

while(<$dict_FH>)
{
print STDERR "$_";
@chars = split //,$_;
@chars = sort @chars;
$sorted_word = join '',@chars;
$sorted_word =~ s/\n//g; #replace all new lines
$sorted_dict = $_; # this is the value of the key for the hash
$list_hash{$_} = $sorted_word; #the key is the actual word, sorted is the hash value
}

print STDERR "\nHash table in RAM, ready to start finding scrambled words...\n\n";

@chars       = undef;
$sorted_word = undef;
$word        = undef; #empty the old bits...
print STDERR "The scrambled words were...\n";
while(<$desc_FH>)
{
@chars = split //,$_;
@chars = sort @chars;
$sorted_word = join '',@chars; #this takes the scrambled word, arranges in alpha order

while( ($word, $sorted_dict) = each %list_hash ) #the key of the list hash is a real word!
{
$word =~ s/\W//g; #Remove all the other invisible formatting
$sorted_dict =~ s/\W//g;
$sorted_word =~ s/\W//g;

if ($sorted_word eq $sorted_dict)
{
print "$word, "; # We matched a scrambled word with a dictionary word
}
}
print STDERR "$_";
}

Economical driving techniques

Posted: 17th November, 2011 in Cars

This is a topic that is perhaps quite dull and boring, but if it saves you money, why not have a read…

First off, I think I should keep it simple and perhaps append some more detailed reasoning on the end of this as and when I feel like it.  So without directly worrying about BSFC maps I will outline some best practices to save fuel and reduce CO2 emissions.

  • Avoid long idles, lots of manufactures have implemented ‘stop start’ strategies, which turn off the engine after a very short period of idling.
  • Buy a manual, automatics generally use more fuel.  Traditional automatics use torque convertors which are horribly inefficent compared with a clutch.  However an exception to this rule, new automatics, such as VW DSG boxes use a wet clutch and 7 gears, arguably improving economy over the manual.
  • Change gear as soon as possible.  ‘Labouring’ the engine is actually a good thing!  Internal combustion engine’s are most efficient at high load, so don’t let the revs go any higher than they need to.  This is particularly true of petrol engines, where throttling losses play a big part in fuel economy.
  • Accelerate fairly swiftly, this serves two purposes, you are likely to place the engine in a high load regime and you can minimise the time accelerating and enjoy longer steady state low fuel consumption cruising time ;)  As a rule of thumb, I try to keep engine revs around 1750 by changing gear as and when is needed whilst keeping the acclerator pedal about 75% until I reach my desired speed.
  • Avoid use of the brakes wherever possible, braking is a complete waste of energy, you should approach bends at a speed to safely negotiate them without using the brakes.
  • If you are approaching a junction that you need to stop at, stay in gear to activate a fuel shut off.
  • If you are looking to maintain a very gradual loss of speed, for example when approaching a red traffic light that is likely to change, you may not want to engine brake, as you will lose too much speed, instead you may want to preserve as much speed as possible by selecting neutral.  This technique is often a judgement call between choosing to burn fuel whilst coasting in neutral,  versus choosing to shed more speed with a fuel shut off.  Think of it as the car’s speed is used to keep the engine from stalling, (instead of it’s fuel).
  • Drive a diesel, they have better brake specific fuel consumption due to favorable pumping and thermo dynamic efficiency characteristics.
  • Don’t carry extra weight, this increases the fuel needed to accelerate and climb hills.
  • The extreme true eco warrior should be looking to ‘pulse and glide ‘ for answers.  By pulsing the engine at full load and then switching it off, it is only ever used under the most efficient operating regime.
  • Look out for cars with a low Cd number.  Drag is wasted energy

Thanks for reading, any questions or discussions are welcomed.

Homage to my Citroën

Posted: 16th October, 2011 in Cars
Tags: , , , ,

It is horrendously slow and shares the same levels of complexity and longevity as cutlery, but my Citroen ZX really is quite the work horse!

I drive approximately 120 miles per day to get to work and back and the ZX has returned an honest 51mpg throughout.  I service it myself and have to say that parts are so cheap and easy to come by.  Here are the sorts of views I enjoy from the slightly ripped driving seat each day.

The car is dropping a rather inspiring 68 bhp down to the front wheels here is a 5th gear pull I did the other day as I filmed it covering a respectable 200,000 miles…

From Citroen

Like all ‘petrol heads’ it is essential to have compiled a list of your most favourite and desired automobiles!  I think I will take the concept sideways by starting a series of blogs discussing a weird car (that I like) a classically great car of that era (that I also like) finishing with something I hate!

WEIRD

I will start with my current weird favourite, don’t laugh – it’s a Citroën.

Maserati engined Citroen

I love Citroën of old, they were inventive and whacky.  The Citroën SM has everything I want and is missing some things you might think you would need, like a brake pedal.  Here’s why everyone needs to spend a spare £15,000 on a decent example of an SM: A 2.7 litre Maserati engine nestled under the bonnet.  Self levelling headlights that track the steering.  Hydro neumatic self levelling suspension.  The steering is self-centering and fully powered (as opposed to hydraulically assisted).  The brake pedal is what can only be described as a rubber button!  A rare classic, reminiscent of Citroën’s innovative past.

HOT

The 70s was really not a good decade for car makers. The price of fuel took a huge hike and so I come to a great former British manufacturer, based in Blackpool up North or course.

During 1973-1974 TVR were ‘busily’ pushing a limited number of 3000Ms out of the door. It looks good, was quick and was a TVR, which means that it is raw, manly and not for girls!

NOT

A truly horrible offering. The 1970’s spelled diseaster for the fledgling British Leyland marque. During the oil crisis and ‘3 day week’ BL did manage to offer the world with the crappy 1800 or Princess.

I think it looks rubbish and in no way conjurers up associations of a princess, reliability wasn’t well renowned either!

Some more evidence to support my outrageous claim that diesel cars days are numbered. The comedic Richard Porter of www.sniffpetrol.com produced this accurate assessment of Fiats recent achievements.

A cheap to manufacture petrol engine, offering significant reduction in harmful emissions, a cheaper fuel than diesel and the same sort of fuel economy! Yes why would anyone bother with diesel in this case?

BMW are such a big force in the auto industry, they can help swing government rules on emissions legislation. If they can’t achieve the goals set out by EU law, chances are the goals must be changed.

Alas, not just the Italiens have been working on diesel beaters.  The Germans at BMW have been doing what all good German engineers should do – lots of research and development. They have been busy downsizing their petrol engines whilst ensuring that vehicle performance does not suffer. Apparently, these will be found in all of their forthcoming models. So what does that mean?

By downsizing an engine, as the name suggests the engine gets smaller. Most people know that generally the bigger the engine, the bigger the fuel bill. Driving along a flat motorway at 70 mph will only require about 45hp. Accelerating the 1.5 ton car to that speed in an acceptable time will require 130 hp, this is the reason we require such big engines in our cars. For those times when we need to accelerate to a chosen speed.

It is a lesser known fact that when an engine is at full load, (delivering as much power as possible) during wide open throttle (foot flat to the floor) it is actually most efficient. So a downsized engine is operating closer to full load more of the time, giving increased efficiency.

In order for a car to be economical they downsize. In order to retain acceptable performance it is necessary to turbo charge. The turbo forces air into the engine increasing its effective volume and delivering the same performance of a much larger engine that is not being turbo charged.

BMWs new range uses a twin turbo charged 1.6 engines delivering the same kind of performance as a 2 litre with the same kind of fuel economy as a similarly specified diesel.:

BMW 116i: Four-cylinder in-line petrol engine with BMW TwinPower Turbo technology package, consisting of twin-scroll turbocharger, High Precision Injection, VALVETRONIC and Double-VANOS variable valve control,
capacity: 1598 cc, output: 100 kW/136 PS at 4400 rpm,
maximum torque: 220 Nm at 1350-4300 rpm,
acceleration (0-100 km/h): 8.5 seconds,
maximum speed: 210 km/h (131 mph),
combined-cycle fuel consumption: 5.5-5.7 l/100km (50mpg)

The twin air, mentioned at the start of this article is an extreme example, it is an inline 2 cylinder twin turbocharged engine:

capacity: 875cc, ouput: 62.6kW/85.2PS at 5500rpm,
maximum torque: 145Nm at 1900 rpm,
acceleration (0-100 km/h): 11.0 seconds,
maximum speed: 173 km/h (108 mph),
combined-cycle fuel consumption: 5.5-5.7 l/100km (50mpg)

Finally the local town of Colchester announces that harmful NOx emissions are exceeding european recommended guidlines, this would not be an issue if everyone entered the town using a petrol powered vehicle.

The Colchester Gazette reports: “The air in three busy Colchester town centre streets may be seriously polluted, tests suggest.  The High Street, Head Street and Queen Street have all been shown to have nitrogen dioxide levels higher than target levels set by the European Union.”

It’s stories like this, that convince me that really there is no viable future for diesel to be the prominent fuel to power cars going forward into the future.

I wonder what will be the popular fuel of choice in 20 years time?

In Europe, 2011, it’s safe to say that diesel is currently most popular.  The ACEA winter report 2008 claims 70% of all new cars purchased in France, Italy and Belgium were diesel.  This trend is continuing to further favour diesel.  So what will be top in 2031 then?

Twenty years ago, 1991, diesels were not nearly as popular as they are now.  They were slow, smelly and uncouth!  The tides did start changing around this period however.  The diesel engine was now turbo charged, relatively high revving, refined, cheap to run and best of all it offered respectable performance.   Any petrol head would still be repulsed by this engine, but for the masses, this was just the thing.

Of course, low fuel consumption equates to low CO2 emissions.  Now the European governments are keen to encourage low consumption of foreign oil, this is achieved by heavily taxing fuel, justified by claims on environmental grounds.  Great, we are saving the environment, less fuel burnt means less CO2 released which means less global warming, (that’s what they want you to think anyway)!

Europe is no doubt a leader in diesel tech, but why?

In the late 80’s through to the mid 90’s when Europe was developing and manufacturing diesels, the rest of the world still loved petrol.  There are some good reasons why America didn’t share the same enthusiasm for reducing CO2 as us Europeans back in the 90s, ultimately it comes down to cost!

North America's most popular automobile of all time

It would appear that the American domestic market is driven by lazy advertisers.  Fashion trends dictate that the bigger the car the better.  The consumer is told they want low cost powerful engines, so thats what is made and thats what they buy, a chicken and egg situation.  The only thing to change that attitude will be fuel cost.

Well in days gone by, North America have had little interest in saving fuel, as they are not completely reliant upon foreign oil- they have their own domestic production.  Another big player in the auto industry, the Japanese, appear to have focussed alot of their attention on American requirements.  The Japs always tend to play it safe, not noted for innovation, they could have implemented Rudolph Diesel’s invention on a mass scale but opted out, it seems they still regard diesel as a dirty, noisy fuel and shy away from using it in their heavily crowded cities.  This leaves the  European market left hankering after frugle little cars that sip as little of the heavily taxed black gold as possible.

Lowering fuel consumption requires expensive research & development as well as increased material and manufacturing costs.  This is what holds back the rest of the world from investing.  In Europe fuel is so expensive the consumer is willing to pay more for their vehicle than an American.  However, recently opinons in the US are shifting, $4 per gallon fuel prices have been hitting the headlines for example.  Take a look at what the likes of GM and Toyota are offering.  GM has a brand image built on the American dream of large trucks, so they can’t go building small town cars, instead they are now offering hybrids as part of their lineup.  Toyota have small eco cars as their foothold and again offer hybrids.

Why do Americans generally not have diesel cars then?  The answer is simple, California!  The state of California has the strictest emissions laws in the world.  That’s right, stricter than ‘green’ Europe.

They require that the following emissions are within a strict defined limit:

  • Carbon monoxide, the highly toxic gas.
  • Hydrocarbons, a large contributor to smog air pollution and disease.
  • NOx, the nastiest of all, acid rain, smog illness and ultimately death.
  • Particulate matter, cause of asthma, cancer and found chucked out of London busses at about the height of a baby in a pram.

Now the state of California is leading the way in emissions regulations.  Diesels are essentially outlawed in California, where limits are set out in three simple classes, low emissions vehicle (LEV), ultra low emissions (ULEV) and super ultra low emissions!… (SULEV).  The majority of diesels are incapable of even meeting LEV, (note that all petrols are SULEV or better).  So in Europe we demand SULEV for our petrol vehicles and then it’s a case of one rule for petrol, another for diesel!  The European governments are essentially making special allowances for dirty diesels.  The only emissions that diesel emit less of, is the harmless gas that plants use to respire, CO2.

So, will Europe eventually come to it’s senses and outlaw diesel like California and the rest of North America?  The alternative is to ignore the problem and keep fooling people into thinking that diesel is a fine alternative to petrol.  In the background Euro 6 standards are being agreed, this will force diesels to be less efficient as they are engineered to battle against all of the emmisions equipment required to clean up their exhaust.  In times gone by, standards have been made and manufactors have responded by failing to achieve the goals, so governments simply move the goal posts!

Ultimately it is down to money, in 20 years time oil will still be lying around and it will be sourced from the same places it is found today, hence, I don’t see any shifts, unless Europe decides to actually be green and then petrol really will be the only sensible solution.

Personally I would hope to see lots more use of electricity and innovative hybridisation and down sizing for the forthcoming 20 years.

Replacing MX-5 Differential side seal

Posted: 2nd May, 2011 in Cars

Having put a reconditioned 1.8 torsen diff into my MX-5 last year,  it has subsequently sprung a leak from the passenger side.  This is evident from all the oil on that side of the diff.

I set to work with replacing and it didn’t take too long, however I would recommend that if you do get a second hand diff, fit new side seals before using in your car!

I ordered new diff oil from eurocarparts – API GL 5 and a new seal from autolink, part number – MA02-27-238A. (Torsen diff; not viscous, as was originally fitted, so don’t use registration if you have changed your diff!)

  • So slacken the road wheel nuts before jacking up.  (then you are not swinging on it with the car in the air)
  • Raise the car up and support with axle stands.  Do not go anywhere near the underneath until you have thouroughly shaken the car, better it falls off now than when you are underneath!
  • Remove the wheel and undo the four nuts attaching the drive shaft to the diff flange (14mm)
  • Don’t bother removing the axle hub nut as suggested in some how-tos, instead undo the top nut and bolt attaching the hub to the upper wishbone.  I suggest jacking the lower wishbone up to a sensible height to relieve pressure and faciliate removal of this bolt.
  • Lower the wishbone and swing the hub out towards you, this will give enough room to pull the driveshaft off the diff flange.
  • The flange is retained by a circular c-clip.  To remove simply apply a large leverage on the flange.  I ended up with a breaker bar wedged between the diff housing and the top section of flange.  With enough force the flange will pop out.  Check out the c-clip, and dont lose it.  You could replace it but chances are it will be fine.
  • Withdraw the flange ensuring not to allow dirt into the diff.
  • I used a claw hammer to ease the old seal out, take care here not to lose the old spring from the seal you are removing into the diff.  Make sure not to scratch or mark the faces with your hammer!
  • With the seal removed, run a bit of diff oil around the edges of the new seal and locate it squarely.
  • I then used the old seal to press the new one in by tapping with a hammer until it was flush with the diff housing.
  • Locate the diff flange on the splines and push in, holding it in as far as it will go use a block of wood to drift it back in with a sharp tap from a hammer.
  • Reassemble.
  • Drain and replace with fresh diff oil.

All done, enjoy.

I got in my Citroen ZX typed the code into the immobiliser keypad, cranked and cranked, but no start.  Panicked slightly, at this point it was 18.30 and I was 60 miles from home.  Gave it another go and voila fired up with the most almighty plume of blue smoke.  The car then drove flawlessly at 70 mph all the way home.

Next day I came to drive it and this time it just wouldn’t start.  Due to my flawless drive home only a day earlier, I knew it wasn’t injector issues.  If it was, the failure would have left me down on power and dropping clouds of black diesel smoke.  Due to running fine once started, it can’t be the last two reasons for not starting listed below:

  •  The glow plugs are not being powered – (won’t start, runs fine once going.)
  •  The stop solenoid is not lifting. (won’t start, runs fine once going.)
  • Air in the fuel (might start and cut out or idle/run erratically)
  • Low compression, ring or valve wear. (driveability and fuel consumption both suffer if you do get it started)

Don’t worry, their is no lift pump to fail, fuel is brought to the high pressure pump via the hand primer found on top of the engine mount on cam belt side, the only failure is for it to allow air into the sytem.

First things first, these engines will very rarely start from cold without red hot glow plugs – cranking simply does not generate enough in cylinder temperature.  They are really easy to check, all you need is:

  • 5 minutes
  • An 8mm spanner / socket
  • A multimeter

1) Check you have a decent battery.  I set my multimeter up to read in the 20v range and dropped my meter probes onto the battery terminal.  Expect to see well in excess of 12volts for a healthy battery.  With the engine off mine reads 13.14v.

healthy battery

2) Now you need to measure the resistance to ground for each plug.  To get an accurate reading I suggest removing the power supply screwed to the top of each plug.  Look closely in the image below, I have removed the power from the left most glow plug.

Disconnect the power rails to all glow plugs

3) Set your multimeter up to read low resistance, I set mine up for 200 ohm or less.  Now connect the meter’s ground lead to a suitable ground, negative battery terminal is fine.  The positive should go on the top where the power rail plugs, expect a resistance of less than 1 ohm.  My number 1 glow plug measured a corrected 0.7 ohm.  The correction is -0.1 ohm as this is the resistance measured when I touch the two probes together.  If  the glow plug has failed it will read open circuit, giving the resistance of thin air (very very high!!)

glow plug resistance

4) Assuming all plugs are in good shape, you MUST check that they are being powered when you turn on the ignition.  Do not skip this step because it is most likely that a sudden no start condition is caused by a bad earth or failed relay box, pictured below.

Relay and timers for powering the glow plugs

5)  So if I think back to school a couple of formulas can be used to theorise what voltage should be read from the power rail feeding all 4 glow plugs.  Assume all plugs measure a corrected 0.7 ohms.

~ Rtot =1/( (1/0.7) x 4)

~Rtot = 0.175 ohms

We can meaure the voltage across one plug, this is essentially acting as a potential divider :

- Vin is the supply voltage acting across the whole circuit, this is battery voltage = 13.14volts in this instance.

- Rtotal is the equivalent of Rtop = 0.175 ohms.

- Rbottom is the resistance measured earlier across one of our glow plugs.  Now we want to measure Vout across that glow plug.

Vout = (0.7 / 0.175 + 0.7) x 13.14

Vout = 10.51 volts

Furthermore we can apply kirchoffs 2nd law: “The sum of the emfs in any closed loop is equivalent to the sum of the potential drops in that loop.”

Applied below, where x = potential drop.

13.14 – x = 4x

13.14 = 5x

x = 2.63

one potential drop is observed at 13.14 -2.63 = 10.51 volts.

In actual fact I measured a mere 9.43 volts across my glow plug when I turned on the igniton on during glow plug warming period:

The crucial step, power up the plugs and measure voltage on the power rail

The crucial step, power up the plugs and measure voltage on the power rail

So how much heat are these babies pumping out when they get switched on?

V = IR

I = 9.43 / 0.7

I = 13.5A per plug, giving a power of P = IV, P = 127 Watts.

Anyway I seem to have digressed.  Assuming you have power going to your rail and after approx 20 secs you see the voltage across a charged glow plug drop back to ground listen to the relay box as it clicks back.  If something is amiss, try bashing the relay box or simply unplug, clean up the contacts and plug back in.

If you absolutely must get home, I recommend spraying 1-2 seconds worth of easy start parrafin or perhaps even try some WD-40 into the intake!  This has a low flash point and will combust from cold without glow plugs, allowing diesel combustion to initiate.  Beware combustion occurs rather uncontrolled, it causes nasty knocking- potentially damaging big end bearings.  It can also cause the engine to over rev as it burns uncontrollably, that being said, use sparingly better too little than too much.

If those checks confirm glow plugs to be working, it is likely that the failure to start is caused by the stop solenoid. On my car it is used in conjunction with the immobiliser.  When the immobiliser is satisfied you have typed the correct code it sends a message to a unit hidden behind a tamper shield.  This unit, upon receiving the message, drops 12volts across the solenoid and lifts a plunger up.  This plunger is designed to stop fuel from being drawn from the fuel filter into the pump when down.  Check out the suction cup on the bottom, the action of the pump drawing fuel in, actually pulls the plunger on tighter, when in the ‘off’ position.  Here’s what it looks like when removed:

Stop plunger

The offending article

So with this stopping fuel flow, my engine was not ever gonna start!  To get to it is not elementary.  The stop solenoid is hidden under a hardened steel shroud.  There are 3 sheer bolts plus a nut and bolt at the bottom stopping any ‘would be’ thief from doing exactly what I needed to do – remove the plunger and therefore allow the engine to start.

I borrowed the services of a local handyman for the afternoon.  He got the shroud off in a couple of hours using a hammer chisel and centre punch to turn the sheer bolts.  And here it is off…

tamper shield

You can make out where the chisel went in to those bloody awkward bolts.  Don’t be afraid to disconnect the high pressure fuel lines and intake manifold to improve access for that chisel to work the bolts round ‘slowly but surely’.  I am told the most awkard of the three is the one at the bottom (blurred in this image, directly below the other two).  He used a mirror to get a visual on the bolt and hit it round with a centre punch.

With the tamper shield off, we pulled the stop solenoid out and watched to see if it popped in and out with the ignition and code.  It did but I reckon that was due to all the disturbance it had had.  I insisted we reassemble the pump with the plunger completely removed from the solenoid to see if the old girl would run.

We had to bleed the high pressure fuel lines as they had been taken out to improve access.  This was achieved, by loosening one at a time, the unions into the injector body and cranking the engine until fuel started coming out at the injector, repeat for all four.  Now I cranked for a further 3 – 4 secs whilst my assistant held the throttle open.  It fired and ran for the first time in a week!  I quickly removed the key from the ignition and of course it kept going.  Beware of doing this, I believe you remove the battery circuit from the alternator whilst it is still generating current, this is not advised and can damage stuff!

I cannot switch the car off any more with the key, I need to pull on the fuel cut off under the bonnet or simply stall the engine in gear.  I could wire a new solenoid to operate off ignition feed but where’s the fun in that?  Instead I simply tug on this rip cord when I’m finished with the car. (see pictures)

Manual engine shut off

FIXED!

So a while back I was designing my carputer.  Now the MX-5 is a sweet handling car, it is not particuarly fast, but it is so much fun on all those twisty country roads found all across England!  Here is an account of a complete electronics novice experience with getting to grips with the basics of integrating an accelerometer into a car pc!

I wanted to get an idea of cornering forces, and although completely pointless, I thought it would be fun to add an accelerometer to my carputer.  I quickly realised that this was not going to be a case of simply plug and play.  Most accelerometers are designed for embedded systems, this means that they don’t work with USB.  A common theme was I2C, pronounced I squared C.  This is a protocol proposed by Phillips a while ago.  It is a two wire interface, a clock line going low and high, and a data line carrying data bits in sync with the clock.

So of course I set about finding an I2C to USB convertor, these don’t really exist and are generally quite expensive.  I continued researching and stumbled across ‘Arduino’, it can do Input/Output (I am also hoping to be able to switch things on/off like my headlights etc. using my computer).  The Arduino could also talk I2C, so I bought one.  I then bought a soldering iron, solder and multimeter!

I was really quite fortunate and acquired an accelerometer from work, but something like a butchered wii remote is adequate, I suppose it depends on your required accuracy of data.

Power

The first hurdle was powering the damn thing, my Arduino runs at slightly less than 3.3V or an equally ‘not quite’ 5v.  I needed a nice stable 1.8V.  I am new to electronics and had just learnt about potential dividers.  Incidentally I put that knowledge to good use, creating a VERY simple circuit + code to drive pin 13’s LED high when ambient light in the room drops low.

Anyway, I initially planned to create a potential divider to step down my Arduino’s voltage.  This is not the way to go, I tried and it just does not provide a suitable solution.  A loaded circuit behaves differently to one with no current draw.

I then learnt about zener diodes, they allow current to flow once a certain voltage is achieved, ‘knee point’ or something similar.  This could work but at such low voltage it is best to use a linear regulator. I got hold of a 1.8v LDO from Farnell.  A three pin affair; input voltage, ground and regualted output.  Get a nice chunky one, then when you wire it up wrong it will shut down before it dies!

An LDO needs a capacitor for stability.  The data sheet often specifies what you need as a minimum capacitance, this is for high volume low cost circuit designers.  However, I am lead to believe that if you have a bigger one lying around, there is no harm in using that.  I have a big cap (100 microfarad) on the input side aswell as a little one (10 micro farad) and then I have another big (100 micro farad) cap on the output.  The big caps are there to keep a constant flow of current as and when the LDO requires it.  The smaller cap is not strictly neccessary, but it should ensure that supply remains ripple free, (its small size facilitates good transient response).

Now I have my 1.8v I am ready to wire up the accelerometer.  I am using a freescale MMA8450Q.  The data sheet found on freescale’s site shows you where each of it’s 16 pins need to go, following this sheet can be tiresome but I bought some stripboard and a few 8 row dual inline (DIL) plugs and set to work.

Wiring it up

Something I have failed to mention so far, the I2C bus supports multiple slaves. That means you can hook loads of devices up and read from each one seperately using only two wires connected to the slave (the Arduino of course!).  So I bought a temperature sensor , now I can pretend I drive one of those cars that has a sensor for the outside temperature.

The final piece of hardware clearly visible in my completed circuit pictured below, is a logic level convertor.  It consists of a mosfet and enables me to run logic at two different voltages.  Basically my Arduino will be running at 5v supply and hence it sees a logic level of ‘1/on’ as anything in excess of 3v for example.  My 1.8v digital devices on the other hand, need only 1.5v for example, to denote a logical 1.  The logic level convertor is the elegant solution to this nasty issue!

Before producing the circuit you see before you,  I sketched a rough layout to ensure I could fit everything I wanted in the available space –  I needed it to fit neatly inside a disguarded Ferrero Rocher case of course!  I didn’t keep to the plan it kind of evolved as I was making it.

A tip that may or may not be common knowledge.  I found that a good way to mount a flat, off the shelf  integrated circuit (IC ) breakout board like the one I am using (pictured).

Chop the legs off a resistor and then run it through the breakout board’s hole, then solder the leg to that and then onto the strip board.  Look closely and you will see what I mean, I have left the odd leg poking out!

My stripboard circuit was designed in such a way that the I2C lines were available as an expandable bus.  By this I mean that the two wires (clock and serial) ran the full width of the board.  You can make out two resistors in the finished picture, these are the pullups for each line and the wires are colour coded as white= SCL and red = SDA.  Now in theory I can add further sensors to the bus as and when.  The pullup resistors are present because the I2C lines are designed to remain in a logic high state, these ensure that the line is pulled high.  The sensors in my circuit are only able to pull things to ground, they cannot pull the lines high.

Following the data sheet I cut and soldered each of the 16 pins to their appropriate destinations and tried it out.

Firing it up

First off it didn’t work, this was a result of sloppy workmanship.  Using a multimeter it was apparent that something wasn’t quite right.  Further digging revealed the first error- I had failed to snip the excess wire off one of my components after soldering, this meant that the data line was shorted out to ground.  Having solved that issue I tried again.  Voltages were now as expected.  I read a few tutorials online and used the wire library to perform some read/writes to my accelerometer.  This failed.  After many frustrated hours I READ THE DATASHEET very carefully.  Basically the wire library was unable to support my hardware.  The accelerometer uses ‘repeated start’ this is something unsupported by the wire library so after some searching I discovered another I2C library which quite frankly is far far better than wire.  It is provided free by Peter Fleury available on his website here.  Using the data sheet and this excellent library I was able to interface with the accelerometer and the temperature sensor.

The code

And finally, the last piece in the puzzle- the exciting bit.  We write some code, compile and upload onto the Arduino’s Atmega 328 chip.  So first off I downloaded my new I2C library and added it to the libraries folder in my Arduino directory.  I think I might have changed the c file extension to a .cpp in order to be compatible with the arduino’s compiler.

Be sure to include the relevant c++ library.  Read your datasheet to establish the address of the device you wish to talk to.  The accel chip is addressed as 0x0D and temp chip addressed as 0x48.  This is #defined at the top of my sketch, a method of letting the compiler know to assign a constant value to an identifier.  Here is a code snippet with most of the variables I #defined…

#include <i2cmaster.h>

//accelerometer
#define ACCEL_ADDRS (0x1D << 1) //This is the MMA8450Q device address when pin 7 is high.
#define ACCEL_CTRL_REG1 (0x38) //This is the system control register
#define standby (0x00) //stops measuring, used to configure device.
#define RESOLUTION (0x01) //Sets to 2g mode 0 1
#define ODR (0x03 << 2) //Set the output data rate 3 = 50Hz
#define WHO_AM_I (0x0F)
#define XYZ_DATA_CFG (0x16) //Configure the ‘data ready’ register
#define REG_STATUS (0x04) //This gets set when data is available and reset when data is read

//temperature sensor
#define TEMP_ADDRS (0x48 << 1) //This is the temp device address
#define TEMP_REG (0x00) //This is the temperature register

Now avoid blindly copying my code, you will be better reading how to use the library and applying that to your application.  Check your data sheet and try to understand what is going on.  That being said I will proceed with what I got up to in this project…

/*
The Standby and Active modes are controlled by the last two bits of the System
Control 1 Register (at 0x38), FS1 and FS0.
[0 0 = Standby], [0,1 = Active 2g mode], [1,0 = Active 4g], [1,1 = 8g]
*/
#define OUT_X_LSB (0x05) //X Register
#define OUT_X_MSB (0x06) //Y Register
#define OUT_Y_LSB (0x07) //Z Register
#define OUT_Y_MSB (0x08)
#define OUT_Z_LSB (0x09)
#define OUT_Z_MSB (0x0A)

int MSB_x = 0;
int MSB_y = 0;
int MSB_z = 0;
int LSB_x = 0;
int LSB_y = 0;
int LSB_z = 0;
int X_data = 0;
int Y_data = 0;
int Z_data = 0;
int xx_dat = 0;
int yy_dat = 0;
int zz_dat = 0;
int wire_debug = 1;
int running2g = 0;
int data_ready = 0;
int looopcount = 0;
int xyz_event = 80;
bool temp_sensor_read = 0;
double ambient_temp = 0;
unsigned long tot_accel = 0;
unsigned long avg_accel = 0;
int array_pos = 0;
const int avg_size = 10;
unsigned int accel_now[avg_size]; //set size of array for storing readings
bool movement_detected = 0; //false means vehicle is stationary
unsigned int roll_reset = 0;
unsigned short time_stamp;

void setup()
{

Serial.begin(38400);
//  Serial.println(“Serial running”);
i2c_init(); //Sets up HW pins for I2C
delay(100);

/*Initialise the sensor, put all configs here before we activate */

i2c_start_wait(ACCEL_ADDRS + I2C_WRITE); //Accelerometer device address
i2c_write(ACCEL_CTRL_REG1);
i2c_write(standby);
i2c_stop();

//Setup for polling mode
i2c_start_wait(ACCEL_ADDRS + I2C_WRITE);
i2c_write(XYZ_DATA_CFG);
i2c_write(REG_STATUS);
i2c_stop();
delay(500);

/* All configs should be above, as we are going online now. */
i2c_start_wait(ACCEL_ADDRS + I2C_WRITE); //Accelerometer device address
wire_debug = i2c_write(ACCEL_CTRL_REG1); //This addresses the appropriate register
if (wire_debug == 0) //Success
{
i2c_write(ODR + RESOLUTION);
i2c_stop();
if (RESOLUTION == 1)
{
running2g = 1;
}
}
else
{
Serial.println(“failed to wake the ol’ girl up”); //Error trap
delay(500);
}

for (int thisReading = 0; thisReading < avg_size; thisReading++)
accel_now[thisReading] = 0; //ensures the array is full of 0s when we start
} // end setup()

I chose to go for a fairly quick serial baud rate, anything quicker and my frontend gui was overwhelmed.  The HW pins 4&5 on the Arduino are lost as an I2C interface when we send i2c_init(); Next I address the sensor and tell it to expect a write.  Then I send the register address.  Then I send the value intended to be written to the defined register on the defined slave address.  Various configurations are then set – the data rate for example.  Now onto the main programme which runs indefinately.

So we head into the loop.  I am still developing bits and pieces, this code has a timestamp which is to be passed across the serial for my GUI, in future I expect that will go and I will keep time on the pc side.  My data ready flag is initialised as 0 and is set high by the accelerometer everytime the accel reports it has data on the XYZ registers.  A while loop is perfect for polling.  It will continually keep checking if data is ready until this flag is not 0.  The commented section was used during debugging for establishing how many times I could run the while loop before data was ready.  A significant number of iterations of the while loop was possible, so I decided to go and read from my temperature sensor.  To avoid doing this too often I set a boolean high once temperature has been obtained, this flag gets set once outside of the main while loop.

void loop()
{
time_stamp = millis();
while (data_ready == 0)
{
i2c_start_wait(ACCEL_ADDRS + I2C_WRITE); //Accelerometer device address
i2c_write(REG_STATUS); //Register with data ready flag
i2c_rep_start(ACCEL_ADDRS + I2C_READ);
data_ready = i2c_readNak();
i2c_stop();
//looopcount = looopcount + 1;
// Expect a min of 1422 iterations in 2g mode at 1.5 Hz
// N.B. 1422 even after reading from temp sensor!
// This gives us a great window of opportunity for temp readings!!
if (temp_sensor_read == 0)
{
ambient_temp = get_temperature();
temp_sensor_read = 1;
}
}
// Serial.println(looopcount); //for debug
//looopcount = 0;
data_ready = 0; //Resets flag for next loop iteration.
temp_sensor_read = 0;  //Set this to 0 to tell function to read temp sensor

i2c_start_wait(ACCEL_ADDRS + I2C_WRITE);
i2c_write(OUT_X_LSB);
i2c_rep_start(ACCEL_ADDRS + I2C_READ);

Binary hex and dec

Onto some bit shifting.  Computers can’t strictly count, they can recognise if voltage levels are high or not, (on or off).  This is why for a computer all numbers must be made up of only ones and zeroes, for example, 5 = 101 in binary.  Basically we are working with a number system to base 2.

An unsigned 8 bit number has the possibility of representing 255 integers (as well as 0), that is, 2^8.  So for 5 we have 1.(2^2) + 0.(2^1) + 1.( 2^0), giving 101.  Unsigned means the number can’t go negative.  If you want negative you have to use a signed integer.  An 8 bit signed number ranges between 127 to -127, we have 7 bits available for the value with the final left most (most significant bit) available for the sign.  Check out two’s complement for a more comprehensive description.

Bit shifting is very handy.  Hopefully you understand that numbers are represented as binary at the very basic level on the Arduino chip.  Programmers often use hexadecimal, hex for short, this is purely for simplicity when writing code.  It has ended up, for whatever reason that bits are often packaged as bytes, 8 bits are in a byte.  One byte occupies only 2 characters in hex.  Theoretically we could write 1111 1111 (this is what it will look like on the chip) or we could say 255 in decimal (base 10 is generally used in the real world), but FF in hex is the least cumbersome of the three, despite all three having the same value.

In my code I receive 12 bits or 1.5 bytes for each direction (1 byte & 1 nibble).  In order to arrange it meaningfully into one number of the correct magnitude I must do some bit shifting.  It’s like if you shift the decimal number 10 left, we add a zero to the end and it becomes 100.  The same happens in binary.  So if we shift b101 left: 101<<1 it becomes 1010, so we now have decimal 10.

That being said, the data is read using a multibyte read, this reduces the number of physical writes required as the accelerometer chip will auto increment through its registers, (rather than wasting time and resource using the Arduino to request data sequentially for each register).

//Let’s start a multi byte read… (use the datasheet for the order!)
LSB_x = i2c_readAck() << 4;
MSB_x = i2c_readAck() << 8;
LSB_y = i2c_readAck() << 4;
MSB_y = i2c_readAck() << 8;
LSB_z = i2c_readAck() << 4;
MSB_z = i2c_readNak() << 8;
i2c_stop();

X_data = get_accel_data(MSB_x,LSB_x);
xx_dat = X_data / 10;
Y_data = get_accel_data(MSB_y,LSB_y);
yy_dat = Y_data / 10;
Z_data = get_accel_data(MSB_z,LSB_z);
zz_dat = Z_data / 10;

/*I want to keep a rolling average of sum of squares for say 10 ticks at 200 Hz */

tot_accel = tot_accel – accel_now[array_pos]; //allows us to keep rolling!
accel_now[array_pos] = (xx_dat*xx_dat + yy_dat*yy_dat + zz_dat*zz_dat);
tot_accel = tot_accel + accel_now[array_pos];
array_pos++;
if (array_pos >= avg_size)
{
array_pos = 0;
avg_accel = tot_accel/avg_size;
roll_reset++;
}

/*Now we have an average, lets check if we think we are stationary! */

if(  ((accel_now[array_pos] + avg_accel) / 2) > (avg_accel)* 1.05 || ((accel_now[array_pos] + avg_accel) / 2) < (avg_accel)* 0.95  )
{
movement_detected = 1;
}
else
{
movement_detected = 0;
}

Serial.print(“|”);
Serial.print(X_data);
Serial.print(“|”);
Serial.print(Y_data);
Serial.print(“|”);
Serial.print(Z_data);
Serial.print(“|”);
Serial.print(ambient_temp);
Serial.print(“|”);
Serial.print(movement_detected);
Serial.print(“|”);
Serial.print(time_stamp);
Serial.println(“|”);
}

This pretty much completes it.  I am developing a quick and easy movement detected algorithm using the sum of squares to identify if acceleration is abnormal compared with the stationary average.

I push each value out on the serial port.  It is seperated using a ‘|’ this makes it easy to ID where each piece of data resides when using it in the GUI.  The raw data is gathered and arranged using a function, you can see it being called in the code above, ‘get_accel_data’ for example.  This and the temperature data is obtained using the functions written after the void loop() as follows:

double get_temperature()
{
double TEMP_DATA = 0; // Contains the temperature intended to be displayed as a decimal
int TEMP_DATA_L = 0; //Low byte of the form xxxx 0000 where 0s are not populated
int TEMP_DATA_H = 0; //High byte containing complete data set MSB contains sign info 0 = +ve

i2c_start(TEMP_ADDRS + I2C_WRITE); //Accelerometer device address
i2c_write(TEMP_REG); //Setup for a read from device’s appropriate register
i2c_stop();
i2c_start(TEMP_ADDRS + I2C_READ);
TEMP_DATA_H = i2c_readAck() << 4;
TEMP_DATA_L = i2c_readNak() >> 4;
i2c_stop();
TEMP_DATA = (TEMP_DATA_H) | (TEMP_DATA_L);
TEMP_DATA = TEMP_DATA * 0.0625;
return TEMP_DATA;
}

int get_accel_data(int MSB, int LSB)
{
int raw_data = MSB | LSB; //This is a 16 bit number with last 4 bits redundant

// The arduino is already performing 2’s complement maths on a 2 byte word.
// So once the data is arranged as a word, we can shift if back to 12 bits.

//Get rid of the first 4 bits which are always zero.
raw_data = raw_data / (B1000);
return raw_data; //2024 counts for 1 g.
}


There we have it.  Hopefully I will post up how I get on with the GUI and how I get on with it in my car very soon.  Questions and comments welcomed, have fun if you try something similar!

This is a nice project to try out.  It is quite simple, especially for those of you using an OBD 2 compliant petrol car.

If you have a car computer, and want to make a power meter here is how to do it:

Step 1 Figure out how much fuel you are burning. (MAF reporting gasoline vehicles)

  • Using an elm tool or whatever you have to read OBD data, request MAF – PID 0x10 (this is mass airflow) [g/s]
  • Now divide this MAF value by 14.7, this is the magic stoichiometric ratio that fuel needs to burn in air.  For the sake of an engine power calculation we don’t need to worry if the engine is running rich, it is only able to physically burn at this ratio, so any additional fuel is not turned into useful work at the crank.
  • The result of this equation gives fuel mass [g/s]

Step 1 Figure out how much fuel you are burning. (Non-MAF reporting gasoline vehicles)

  • If you don’t have a MAF sensor reported on your OBD you will have to do a speed density calculation, this is less error prone if your car is NA (non turbo).
  • Note engine size in [cc]
  • Gas constanst R = 0.082057 [L atm mol^-1 K^-1]
  • Molar mass of air = 28.75 [g/mol]
  • Estimate Volumetric Efficiency  (this is a definate source of error, browse the web for expected VE figures) for now assume  a vtec engine should be getting around VE = 0.9 at 6500 rpm and at idle assume VE = 0.4, do interpolation to find VE for all other engine speeds, or make a lookup table.
  • Prepare to request the following PIDs:
  • Engine speed (PID 0x0C) [RPM] (raw/4)
  • MAP – Manifold air pressure (PID 0x0B) [kpa] (raw = value)
  • MAT – Manifold air temp (PID 0x0F) [degC] (raw + 40)
  • Work out airflow with the following formula (adding 273 to MAT converts to degrees Kelvin)
  • Airflow [g/s] = (MAP/100) * (Eng_speed/120) * (EngSize/1000) * (1/(MAT+273)) * (1/R) * 28.75 * VE
  • Fuel flow [g/s]  = Airflow / 14.7

Step 2 Estimate engine power

The net calorific value of petrol in the UK is about ~ 43 MJ/kg depending on what fuel station you go to!  The rather poor efficiency of our beloved internal engine is ICE_eff ~ 0.33 (33% is thermal loss to the cooling system and a further 33% goes to bearing and pumping losses).

Power [kW] = 43 * fuel flow [g/s] * ICE_eff

Power [bhp] = power [kw] * 1.34 (look at my previous post to calculate torque)

There we have it.  Have a fiddle with the engine efficiency value, it should not really exceed 0.36, let me know how you get on.