Monday, April 23, 2012

Reading the Cam Angle - Part 1 (Signal Conversion)

If you understand the basics of a 4 stroke engine you'll know that there are 4 states that a cylinder can be in: intake, compression, power, exhaust. Of course there's always the informal: suck, squish, bang, blow if you prefer. If we read the sensors attached to the cam, then we can know when each event happens since the cam position is what determines the state of each cylinder.

On my car the cam sensors are built into the distributor. The first thing I needed was a way to easily intercept the signals and determine their type. I chose to replace part of the wiring with DB9 connectors. This allowed me to easily connect the distributor as standard into the car electrical system or break the connection and attach each DB9 plug to my own circuitry and intercept the signals.

There are two signals which I'll refer to as "inner loop" and "outer loop". The outer loop will pulse once per cam revolution. The inner loop will pulse 4 times per cam revolution. When the inner and outer loop signal offset(phase) is changed, the result is a change in the spark timing. So, when the inner loop pulses occur later in time compared to the outer loop pulses, the spark timing will occur at a later point in each engine revolution.

The Outer Loop
From what I can figure out, it looks like the outer loop signal is generated from a hall effect sensor. The sensor is switched on/off by a small magnet that moves past it on each rotation.

The result is a 0-12v square wave signal that looks like this. The signal is a bit noisy, but I'm not too fussed at the moment.

The microcontroller works with TTL logic levels (0v-0.8v for low/0 and 2.6v-5v for high/1). At first it looks like a simple voltage divider will do what I need. The only problem with that is the assumption that you're always working with 12v, but the voltage output could change anywhere between 8v and 14v mostly depending on how much power the battery and alternator can provide vs demand on the car's electrical system (fans, wipers, radio). The solution is to make your voltage divider out of a diode and resistor. I chose a 4.7v zenner diode because that voltage is within the TTL spec.. I've included a circuit diagram here. I've never used circuit design software before, so it's a bit crap. I didn't get exactly what I wanted in 15 minutes of playing around, but for now it should provide you with an understanding of what I'm doing.
Conversion to TTL voltages
VS1 represents the original signal, and TPdv1 is the TTL output. The end result is a 0v-5v square wave that can switch a microcontroller input pin!

The inner loop
This is generated from a reluctor sensor.

Reading the inner loop signal is a bit more involved. It's an AC signal ranging from +11v to -7v P-P. The wave form isn't a nice sine wave, but that shouldn't cause us any problems. Each wave pulse is generated every quarter turn. Obviously the faster it turns, the higher the signal frequency. The only difference is the signal amplitude will also change with engine speed. The +11v to -7v was recorded while the engine was at it's max RPM. At lower engine speeds the signal could range just -2v to 3.5v.

I started out with the same circuit that I used for the outer loop. When the current is flowing forwards (during the first half of the signal), the peak voltage across the zenner diode can't be any more than 4.7v. When current is flowing in reverse (second half) the voltage across the diode will peak at -0.5v. Ideally it should be 0v, but the diode must have a breakdown voltage of just 0.5v. Below you can see the original signal in red, and the converted signal in blue.
Red: Original, Blue: Zenner

The inner loop - digital
While this new signal is now within TTL voltage ranges it will cross into undefined voltages that are neither recognised as 0 or 1. This can cause really weird problems, so the final step should change this into a square wave. I didn't include a circuit diagram here because it would take me far too long to do. I haven't had the time to learn how to properly use 5Spice yet!

The first thing I did was feed the signal into an optocoupler. I chose a 4N25. The inner loop signal is provided across 2 wires. If I were to connect it directly to a TTL IC, then one wire would have to be connected to a ground point common with IC. This would alter the voltage range of the signal. Because the standard ECU is still using this signal, I chose to leave it intact. The optocoupler will isolate the signal from the rest of my circuitry, giving me a mirror version to work with. The first thing I learned was not to feed the signal in directly. Excess current flow caused 2 spectacular  failures. *POP* and the IC shot straight off the breadboard. I never saw it again! Placing a current limiting resistor before the input worked, but the output waveform was changed. At first it just looked inverted, but flipping it over didn't exactly match the input.
Optocoupler output
Optocoupler output inverted

The signal is still usable, but I thought I was doing something wrong and decided to look into it. I played around with measuring input an outputs while feeding constant voltages and saw that the relation between input and output wasn't linear. Googling reveals that it's skewed according to the optocoupler's CTR(current transfer ratio). I found some more info here

Now that I have a signal I can work with, the last step is to convert it to a square wave. My first choice for doing this was to use a voltage comparator. If I were to use a 2.5v reference then any portion of the signal above that will set the comparator output to a solid 5v, and anything below the reference will set the output to a solid 0v (or as close to those voltages as the comparator can output). Digital 1 and 0. I tried using both an LM393 and LM311 and unfortunately I got some really confusing results. The comparator output would either not change at all or oscillate at a very high speed. Of course I measured the signals going in, and tested the IC's by feeding in various test voltages while measuring the output. I can only assume that I've overlooked something or electrical noise was making them go haywire. In the end I succeeded with an OPA2134 op-amp. 
Woohoo! Digital square wave!

To get this signal I had to make one final change, which was to make my reference 4.9v instead of 2.5v. No, 5v wouldn't work and neither would 4.8v. This I don't understand, but it shows that experimentation pays off. At this point I'm assuming that it has something to with how much current the input signals (reference voltage and optocoupler output) can source/sink, but didn't see either being pulled high or low. I'll need to find out more before I deal with this problem again.

End result: Success! & Strange results to look into.

Edit on 2013-10-01: Fixed mislabeled reluctor

Tuesday, April 10, 2012

Stock engine management system

My car is old school and so the ECU doesn't have any standard way to be reflashed, there is no OBD connectivity, and it actually doesn't have an internal ignition timing map. That's done mechanically with springs and a vacuum actuator in the distributor (which PIT will not use). It's a weird mix of electronic goodness, and mechanical cleverness.

The stock engine management system is made up of two modules, an ECU (engine control unit) and a knock sensor unit. I wanted to get a better understanding of how they work, but couldn't find much information to start with. Perhaps my google-fu is not powerful enough, but there doesn't appear to be any publicly available information on the modules themselves. All i can figure is that the knock sensor was created by Mitsubishi, and the ECU by Denso.

Lets crack them open!
I started with the knock controller. Engine knock is an event that occurs in an engine cylinder where ignition occurs too early and/or in multiple locations causing turbulence which can knock the piston into the side of the cylinder wall instead of pushing it smoothly straight down.
Wikipedia: Sensing engine knock is a complex process which starts with taking audio input from a microphone or piezo sensor mounted on the engine and then isolating a specific sound frequency.

I was surprised by what I found, which was.. not much
The datasheet for the 2 ICs show that they're quad comparators. I expected to see a microcontroller or DSP! Underneath I found another PCB connected by a ribbon cable.
Unfortunately i couldn't remove it without damaging it. No way I could find a replacement and I still need my car to work in the mean time!

Not very interesting, so I decided to move onto the ECU.

I'm most interested in these two ICs. The one on the left looks like some sort of microcontroller, and the one on the right appears to be an EEPROM chip. Surprise, there is no data available for them. I can't even find a pinout. After much googling I found that yes, the one on the left is a microcontroller, but it's a proprietary Denso one. It was probably used in many cars during the 80's. Here I found that at least a few people have successfully hacked them.

"I hacked the Denso MCU a long time ago.
The above board runs the original factory code that is inside the original ECU.
If I leave the code stock (apart from a couple of essential byte changes to swap the mode and ensure the ROM checksum is OK) then the ECU runs exactly as stock.
It's possible to alter anything in the original program or the mapping."

So it seems that some clever people are able to do some interesting things with it, but that's beyond my capabilities. I at least got to learn a couple of things, which was really the whole point.

Fast cars go *whoosh*!

A post for the fellow car enthusiasts. I've had  my car since 2005, it's an '88 Ford Laser TX3. For those of you outside of Australia, you might know it as a Mazda 323/Familia GTX. It's basically the same car, built in the same factory with some cosmetic changes and the ECU tuned for Australian fuel. It has 1.6L injected twin cam turbo engine and a 4WD drivetrain (with centre diff lock!). Stock motor makes 100kw and i'm estimating (based on past dyno run and some later changes) my car is putting out 120kw, which i think decent for a 1150Kg car.

My car with stupid heavy wheels, oops :P

I always enjoy driving it, traction + power = fun. I've focused on keeping the braking, steering, suspension and engine in good running order. I plan on completing an advanced driver course with my wife, and taking it out on a track day.

The future plan: I'm a fan of light, agile cars. I can keep upgrading this car to get where i want.... or i can get my hands on a nice MX5. I won't be able to keep both cars and plan to replace the standard MX5 engine with the one from the TX3. I'll miss my 4WD, but there's a few reasons i want to do this. Easy conversion, the engines are actually very similar, parts are interchangeable and with the help of people who know more than me i should be able to install my more powerful TX3 motor without any expensive customisation. It's lighter which means faster cornering (on tarmac!), braking and acceleration. RWD means less drivetrain power loss. The TX3 should be able to provide heaps of goodies (brakes, big radiator, fuel pump).

Because it's the same motor i expect the PIT project wont really be affected. I might even get some bonuses like the crank angle sensor from the MX5 motor!

Monday, April 9, 2012

PIT project scope

The project started before the blog so there is a bit of a backlog. I'll be playing catchup for a bit!

Background: In order to properly understand the project scope, you'll first need to understand:
- how a car engine works
- the role of an engine ECU
I recommend reading and if you don't know much on this topic.

Scope: The replacement ECU will monitor sensor outputs from the car engine and control the engine components to ensure a smooth running motor while matching or surpassing the current fuel efficiency and power figures. Of course, I'll be happy if it runs at all.
- Cam angle
- Engine air flow
- Intake air temperature (depending on air flow sensor type used)
- Knock sensor
- Engine temperature
- Throttle position
- Oxygen sensor
- Ignition coils
- Fuel injectors

Project Phases:
1. Monitor the signals sent to and from the current ECU to gain an understanding of how it works.
2. Build the PIT system to run in-line as a sort of "man in the middle" configuration between the cam angle sensor and current ecu input. With this setup i can only set a static ignition timing offset. A dynamically set offset is ideal.
3. Finished system. Some sensors replaced with different types. Monitor all sensors and electronically control all possible components.