Ruptor
Posts: 72
Joined: Mon Jan 27, 2014 12:26 pm
Location: London

RPi as car ECU?

Fri Jan 23, 2015 11:46 pm

I searched and couldn't find any ECU projects that had been done but plenty of people say they are going to have a go.
WIthout worrying about interfaces between the car components and the RPi is there any fundamental reason why the RPi couldn't do it? I would like to have a crack at it using Lazarus and FPC.

otani
Posts: 53
Joined: Fri Dec 13, 2013 4:18 am

Re: RPi as car ECU?

Sat Jan 24, 2015 7:44 am

Here is some information about OBD2 and lazarus.

Search Keyword "ELM327"
The original ELM327 is implemented on the PIC18F2480 microcontroller.
ELM327 works as protcall converter between CAN-BUS and RS232,USB,Brootooth.
Its price is about $10 to $30 at amazon.com, price depends on the interface.
Of couse you can use PIC18F2480 , the source code is open.

Lazarus project of a diagnostic tool for car.
http://sourceforge.net/projects/diagkwp/
wiki KWP2000
http://en.wikipedia.org/wiki/Keyword_Protocol_2000
Adafruit OBD-Pi
http://www.adafruit.com/blog/2014/06/27 ... pberry_pi/
Try to search on google, with keyword of ( ELM327 or OBD and Raspberry pi) or (ELM327 and Lazarus).

Ruptor
Posts: 72
Joined: Mon Jan 27, 2014 12:26 pm
Location: London

Re: RPi as car ECU?

Sat Jan 24, 2015 2:08 pm

Hi Otani
Yes I can get some ideas about real time software from those examples but they are all about monitoring the ECU I want to know if anybody has made an ECU using a RPi to control a car.
Just had the thought that the security device could be either the removal of the boot simcard or a usb key that matches a code in the boot code. To beat it someone would have to know that the car ECU was based on the RPi then they would need to change the boot simcard for their own to match their usb key. It is no different to a thief arriving with a key and matching ECU but RPi wouldn't be standard. Security is on my mind because my house was broken in to and my car keys stolen hence the idea to make an ECU.

otani
Posts: 53
Joined: Fri Dec 13, 2013 4:18 am

Re: RPi as car ECU?

Sat Jan 24, 2015 5:18 pm

OBD systems give the vehicle owner or repair technician access to the status of the various vehicle subsystems. This system is designed for monitoring only .
http://en.wikipedia.org/wiki/On-board_diagnostics

Recently, automatic driving car by a robot has been developed.
In this case, unique ECU with external control must be used.
However, there is no international standard for such ECU in car industry yet.

Actual ECU is not designed to be externally controlled.
This is why only monitoring applications are made.

aalhaimi
Posts: 1
Joined: Sat Mar 28, 2015 8:50 pm

Re: RPi as car ECU?

Sat Mar 28, 2015 11:48 pm

Hi Rupter,

I'm interested to know if you have come across any potential solutions.

From a programming point of view, it's possible. But, it all depends on whether the original ECU code is made available so you can figure out how operates.

I'm looking for an answer to this as well. I'm completely new to RP. I'll keep you posted if I find something.

rotwang
Posts: 243
Joined: Sat Dec 07, 2013 1:12 pm

Re: RPi as car ECU?

Sun Mar 29, 2015 10:56 am

Ruptor wrote:I searched and couldn't find any ECU projects that had been done but plenty of people say they are going to have a go.
WIthout worrying about interfaces between the car components and the RPi is there any fundamental reason why the RPi couldn't do it? I would like to have a crack at it using Lazarus and FPC.
1) It isn't fast enough for hard real-time work
2) None of the available operating systems can do hard real-time, you'd have to write your own to run on bare metal
3) It isn't designed or intended for use in safety critical systems (this is the real idea killer)
4) Pascal may not be a good choice for writing code to run on bare metal

Ruptor
Posts: 72
Joined: Mon Jan 27, 2014 12:26 pm
Location: London

Re: RPi as car ECU?

Tue Jun 23, 2015 7:06 pm

I have finally returned to this project part time while there is a lull in my cure for all diseases project. Why is it that people always have a can't be done attitude rather than a can do one. My answer is they are dragged down by pathogens but that is another story. The RPi is easily fast enough to control a car and make a cup of tea since I can do it on a 40 MHz PIC easily. To get around the unknown aspect of real time control on an RPi I was contemplating using a PIC to interface the real world to the RPi but came to the conclusion there was no point in having the RPi.
My intention now is to write code using the FPC that I can test in Lazarus. I hope I can get some pointers from experts here on the RPi and chip concerning the I/O and peripherals although there does not seem to be many people using FPC for stuff on the RPi let alone real time stuff.
First step is to get some code that links the main ECU I/O together. The simplest I/O set is based on a traditional car system with no electronic control.
Inputs
Throttle position, Engine speed, Ignition timing.
Outputs
Fuel injectors, Ignition coils.
For this minimum system it is just a matter of tying the throttle range to the injectors and the ignition timing to the engine speed. I see the biggest problems as the hardware interfaces and the conditioning of any analogue inputs rather than any software issues but time will tell.

asandford
Posts: 1997
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: RPi as car ECU?

Tue Jun 23, 2015 7:27 pm

Ruptor wrote:I have finally returned to this project part time while there is a lull in my cure for all diseases project. Why is it that people always have a can't be done attitude rather than a can do one. My answer is they are dragged down by pathogens but that is another story. The RPi is easily fast enough to control a car and make a cup of tea since I can do it on a 40 MHz PIC easily. To get around the unknown aspect of real time control on an RPi I was contemplating using a PIC to interface the real world to the RPi but came to the conclusion there was no point in having the RPi.
My intention now is to write code using the FPC that I can test in Lazarus. I hope I can get some pointers from experts here on the RPi and chip concerning the I/O and peripherals although there does not seem to be many people using FPC for stuff on the RPi let alone real time stuff.
First step is to get some code that links the main ECU I/O together. The simplest I/O set is based on a traditional car system with no electronic control.
Inputs
Throttle position, Engine speed, Ignition timing.
Outputs
Fuel injectors, Ignition coils.
For this minimum system it is just a matter of tying the throttle range to the injectors and the ignition timing to the engine speed. I see the biggest problems as the hardware interfaces and the conditioning of any analogue inputs rather than any software issues but time will tell.
Once you tell your insurance company that you *may* modify your car to be controlled by a RPi, I think the quote you get back will stop any further research.

The Pi isn't fit 'mission critical' systems. I doubt that your setup would be legal to run on the road in the UK.

Ruptor
Posts: 72
Joined: Mon Jan 27, 2014 12:26 pm
Location: London

Re: RPi as car ECU?

Tue Jun 23, 2015 8:21 pm

In Oddballs words "what's with the negative vibs man". Scoobees run custom ECUs all the time that double the power and the RPi is just a chip on a board that can be protected electrically and environmentally.
Anyway back to the code where I don't have a clue how to do anything other than GUIs in Lazarus. :?

asandford
Posts: 1997
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: RPi as car ECU?

Tue Jun 23, 2015 8:33 pm

Ruptor wrote:In Oddballs words "what's with the negative vibs man".
When the Pi crashes and disables your brakes and enables the accelerator.

User avatar
MattHawkinsUK
Posts: 538
Joined: Tue Jan 10, 2012 8:48 pm
Location: UK
Contact: Website

Re: RPi as car ECU?

Tue Jun 23, 2015 8:48 pm

Ruptor wrote:Why is it that people always have a can't be done attitude rather than a can do one.
It's not really people being deliberately negative it's just the reality that Linux isn't a real time operating system. If you can cope with unpredictable delays then it may appear real time. Is an ECU going to work properly if there is a 10-100ms delay between inputs and outputs? A real time system isn't related to processor speed. My desktop PC is far more powerful than a Pi but still isn't real time.

The other main issue as mentioned by others is that at some point you will make a mistake in your code and your system will fail. When you are dealing with 2 tons of metal that isn't going to end well.
Ruptor wrote:Scoobees run custom ECUs all the time
An ECU is a real time safety critical processing system. The Pi isn't.
My Raspberry Pi blog and home of the BerryClip Add-on board : http://www.raspberrypi-spy.co.uk/
Follow me on Google+, Facebook, Pinterest and Twitter (@RPiSpy)

Ruptor
Posts: 72
Joined: Mon Jan 27, 2014 12:26 pm
Location: London

Re: RPi as car ECU?

Tue Jun 23, 2015 8:51 pm

The brakes are completely independent and dual circuit for obvious reasons and the throttle is a mechanical butterfly controlled by your foot. All my ECU will do is set fuel and ignition to run the engine so the worse case is no engine.
Any constructive help will be appreciated.

Ruptor
Posts: 72
Joined: Mon Jan 27, 2014 12:26 pm
Location: London

Re: RPi as car ECU?

Wed Jun 24, 2015 10:09 am

This is what I need but it all looks a bit like it is in its infancy.
http://wiki.freepascal.org/TARGET_Embedded
I have developed many embedded control systems on many platforms but never done anything on an ARM or in FPC. It is going to be a learning curve to get the compiler to make target files for starters but once that is done it should be straight forward and just down to me.

asandford
Posts: 1997
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: RPi as car ECU?

Sat Jun 27, 2015 11:09 pm

Ruptor wrote: so the worse case is no engine.
LOL, and that's obviously not a problem in the fast lane of a motorway!

Ruptor
Posts: 72
Joined: Mon Jan 27, 2014 12:26 pm
Location: London

Re: RPi as car ECU?

Wed Jul 01, 2015 9:34 am

Nothing useful to say then since any car can break down. To give your pointless comments perspective you are trying to advise a Chartered Electronics Design Engineer that has been designing safety critical medical equipment for a quarter of a century.
I hear the concerns that the RPi isn't very robust but it can be cocooned and the only way to find out if it can be made to work is to do it. Looking at all the interfaces required to control the auxiliary components that the car manufacturer has attached to the ECU is a bit daunting and might be the stumbling block in terms of available development time.

scotty101
Posts: 3749
Joined: Fri Jun 08, 2012 6:03 pm

Re: RPi as car ECU?

Wed Jul 01, 2015 10:28 am

Crack on then. Do it.

Given the responses here there is nobody on the Raspberry Pi forums with any knowledge on the topic to help you. Use your 25 years experience and try asking on some ECU hacking forums for advice.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

PiGraham
Posts: 3669
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: RPi as car ECU?

Wed Jul 01, 2015 10:46 am

This might be interesting:
For his PhD at the University of Michigan, [Adam] designed a Raspberry Pi-based system that controls an HCCI engine, a type of engine which combines the merits of both diesel and gasoline engines. These engines exhibit near-chaotic behavior and are very challenging to model, so he developed a machine learning algorithm on a Raspberry Pi that adaptively learns how to control the engine.

[Adam]’s algorithm needs real-time readings of cylinder pressures and the crankshaft angle to run. To measure this data on a Raspberry Pi, [Adam] designed a daughterboard that takes readings from pressure sensors in each cylinder and measures the crankshaft angle with an encoder. The Pi is also equipped with a CAN transceiver that communicates with a low-level engine control unit.
http://hackaday.com/2015/01/28/raspberr ... on-engine/

P_Monty
Posts: 57
Joined: Sat Dec 27, 2014 2:45 pm
Location: Wiltshire, UK

Re: RPi as car ECU?

Thu Jul 09, 2015 8:40 pm

If you want to develop your own ecu, rather than a pi, why not play with one of these?
http://www.megasquirtuk.co.uk

User avatar
sommersoft
Posts: 35
Joined: Sat Jul 11, 2015 1:43 pm
Location: Delaware, US

Re: RPi as car ECU?

Tue Jul 14, 2015 3:29 am

In an effort to not be negative, this is about all I can offer...

1. Run your AFRs "pig rich" to keep detonation at bay. Hell, I'd stay as close to 15 as I could. Basically, stay on the safe side of Speed Density calculations.

2. Run only in closed loop; don't allow learning based upon environment (open loop). Makes #1 very important, but much more attainable than doing SD calculations on the fly.

3. If its based on a Drive By Wire throttle, make the throttle position curve shallow and plateau early. If Drive By Cable, 1 & 2 are your only options.

4. Forget emissions control. Hippies will hate you. They're going to hate you anyway, since you'll be floating hydrocarbons into the air by the gallon with running as rich as you need to.

I get the "can I" sentiment, and hell, I applaud it. Its what we all do here. Just please don't hurt yourself, or anyone else. And keep us updated.

P.S. This guy has some interesting and helpful stuff. It's motorcycle based, but the principles and procedures are the same.
http://www.randomexploits.com/home.htm
"We mock what we don't understand." - Dr. Trowbridge

ghellquist
Posts: 68
Joined: Thu Aug 02, 2012 8:47 am
Location: Stockholm Sweden

Re: RPi as car ECU?

Wed Jul 15, 2015 8:28 pm

Oops. What negative talk. Let us keep things positive, I believe the OP knows or will find out about the problems in due time.

It should be possible to do an ECU using a Pi. Not easy of course but why tackle easy thing. I believe you would need a few added pieces of hardware, such as AD and DA converters. I believe you will need to find a way to work around the Linux kernel, possibly programming directly on the hardware. There is a special subforum for that here.

One alternative might be to use specific hardware for the detailed timing, say spark timing. Back of the envelope says: 4000 rpm, max error allowed 3 degrees, spark should be within a window of 7,5 milliseconds. This is possible to achieve with the Pi, but probably not from inside standard Linux.

All in all, it should be possible.

Personally I would be most concerned about physical "hardening" of the system. I believe the memory card with program code is one problem spot. Vibrations, temperature variations, humidity -- takes a better case than the standard. Might be alternative hardware out there making it easier, but why not.

Ruptor, please go ahead and keep us informed.

// Gunnar

tufty
Posts: 1456
Joined: Sun Sep 11, 2011 2:32 pm

Re: RPi as car ECU?

Thu Jul 16, 2015 6:22 am

The "negative vibes" you're seeing are because the Pi is not the right tool for the job. And, in the same way you can bang a screw in with a lump hammer, you can probably make an ECU from a Pi - it's easily fast enough to do the job. However, there are a few things that spring to mind.

1 - ECUs and other safety critical hard-real-time systems are usually based on microcontrollers rather than the microprocessor on the Pi. Just because it doesn't have a case doesn't make it an embedded controller.
2 - The Pi doesn't have much in the way of I/O. In all probability, you'll need additional IO expanders.
3 - You don't get much in the way of timers and interrupts, particularly with regards to triggering counters on falling and rising edges.

1 is the killer. For an ECU, you want something that's deterministic. It's a hard real-time system, so you'll probably need to junk Linux and work on the metal. That's not particularly hard in itself (indeed, ironically enough, it makes some aspects of what you want to do easier), but it's the PC-nature of the Pi thats gonna hurt you. Caches in particular can screw up the deterministic nature of code, an instruction might take 2 or 3 cycles one time, and then hundreds the next time if the cache has been clobbered. If you want an idea of how complex this can be, the impact cache can have, and how much work it is to get best performance, there's a thread on the Bare Metal forum about making a high performance memcpy (see below). Writing deterministic code is hard, especially when you have to work around / against the hardware.

Trivial memcpy for non-overlapping regions looks like this:

Code: Select all

void * memcpy(void * target, void * src, unsigned count) {
  for (int i = 0; i < count; i++)
    *target[i] = *src[i];
  return target;
}
My suggestion would be to use one of the Core-M0 or Core-M3 based boards out there (PJRC's Teensy 3.1, for example). Cheaper than a Pi, with more I/O, and an architecture and hardware design intended for embedded use. Like using a screwdriver to drive in your screws, it's easier to do, you get a better finish, and it's cheaper.

sezyal
Posts: 1
Joined: Wed Nov 18, 2015 7:40 pm
Location: Istanbul, Turkey

Re: RPi as car ECU?

Wed Nov 18, 2015 8:41 pm

Sorry Ruptor,

If you are young, by now you learned the difference between losers and winners in life. Losers will only think about how they lost and how they would lose anyways. Winners just do things. Do not let other people make you a loser. There will always be too many losers around and I couldn't yet find a way to fix them. Maybe you will.

So much nonsense BS and jargon split. There is no such a thing as realtime computer BS or non-realtime ,delayed maybe sleeping CPU! It doesn't matter at all. There is CPU, there is faster CPU etc. You need also fast I/O which may or may not be sufficient but you can always add integrated controllers like Arduinos to get easy tasks off CPU. ECU does many things, checking sensors, monitoring, checking oxygen sensors(I did that with arduino) and firing injectors and spark plugs(I did control both with an Arduino) I am pretty sure RPI2 handle can handle I/O speed but if it can't you can offload it to something else like Arduino. Arduino has very fast hardware interrupts. You will probably need ADC/DAC modules anyways. Important thing is to control all these connected to an RPI. Car is a mechanical thing, it doesn't need so fast processing anyways. Data you get from an oxygen sensor won't change in 1ms. Sensors are even not that fast. So 1Ghz CPU is > DSP, microcontroller, realtime cpu(whoever invented that marketing term)

Right before I saw your post, I was looking at my car's ecu to see if I can hack into it. I have a Ford and I understand Ford engineers are lacking basic knowledge to fix my speed limiter issue.
So it runs on Infineon TC1797. It's a 180Mhz microcontroller(let's say) and has only 4MB program flash! Compare that to RPI2, this latest ECU processor is very primitive.

So I am sure RPI2 and some components can be used as ECU and it would be a very good one. Important thing is to make it open source so that knowledge is accumulated.

My RPIs crashed twice because of bad SD card. Otherwise running forever. Surely you will need to clean up OS and just run REQUIRED stuff. You can even disable most kernel modules. So no doubt that it will be as reliable. ARM is used by billions of people, tested and approved. Who knows Infineon? And we already have very stable older Linux kernels that can run this. So no problem at all even if you write ABS algorithm in RPI. (Yes you can make ABS controller with RPI as well using sensors on wheels and some other motion sensors. Ignore losers)

So things to do to can be:
-Find a test car, check and target for a specific ECU, find pinouts and understand all.
-Experiment RPI with relays, turn on gas pump etc. Also you will need Solid State relays for various things like injector timing.
-Experiment with oxygen sensors. I wrote a code for a 5-pin Bosch oxygen sensor once I found the technical paper about it. They usually work with voltage small drop and you can use analog input for that.
-Experiment with ADC/DAC modules
Ebay and Aliexpress are amazing to find stuff.
Biggest problem will be lack of documentation. Same as RPI2's GPU issues :shock: These companies make people believe they do something amazing(according to thread, they are successful) but if you are advanced with RPI, they don't do too much more.
If you can find all technical documents and pinouts, that will make things very easy.

I wish I could help but I am already working on an electric car. And controllers will be CPU.

Good luck!

JeffWilliamsUSA
Posts: 1
Joined: Tue Jan 19, 2016 12:00 am

Re: RPi as car ECU?

Tue Jan 19, 2016 12:07 am

would focusing on OBD-1 help the situation ?
also .. at CRXcommunity.com we have any pinout for any honda ecu we use .. :) and even some that we don't use .. lol
http://crxcommunity.com/

and we have a section of our forum dedicated to Gadgets and Electronics
Car Audio, Gadgets and Security
http://crxcommunity.com/viewforum.php?f=9
Also, there is an arduino project to control turbo boost
google : arduino turbo boost controller
for youtube video etc ..

and i'd also like to see an OBD-1 version of OBDpi ..

hope this helps ..
cheers .. jeff

Ruptor
Posts: 72
Joined: Mon Jan 27, 2014 12:26 pm
Location: London

Re: RPi as car ECU?

Wed Mar 16, 2016 1:21 pm

Thanks Sezyal and Jeff. Having looked at using Lazarus and the embedded code system of development I found it too complicated to get off the ground for now given the time I have available to spend on this project so I decided to use a PIC 18. I wasn't sure if it would have enough inputs for the sensors but I have managed to come up with a solution that fits. Of course once the PIC version is working it can be ported to any processor if more fancy calculation or complex processes are required and the PIC can't do but when all is said and done there isn't a lot required to control a car anyway. Having collected all the information about my Impreza Subaru signals, designed the hardware and 70% of the software to collect and distribute the I/O it is now just a question of finishing the first stage of software then testing by simulation to make sure all the signals come out at the right time before I go anywhere near the car so I make sure I don't fire a piston on an up stroke and blow it up. :lol:
I have always thought the existing car ECU were not much good and now having designed one myself I think they are even worse than I thought. I would compare existing systems to a car with a fixed axal and no differential. They work but they could have been designed a lot better. It begs the question if I can implement an ECU on a PIC 18 running at 40 MHz with a prototype board measuring 2"x3" what are they doing with their 100 MHz + processors and massive realestate? :roll:

a_q
Posts: 2
Joined: Tue Apr 19, 2016 5:38 pm

Re: RPi as car ECU?

Tue Apr 19, 2016 6:40 pm

Hi Ruptor,

shame you gave up on the Pi, looks like I might take on the mantle.

yes the Pi looks eminently suitable for real-time. As back in 2014 someone brewed a linux-rt kernel for it:
http://www.emlid.com/raspberry-pi-real-time-kernel/

I currently use an arm 9 running pthreads on linux (without rt) and that's quite capable of turning around 2.3ms GSM half-frames, and shoving 20ms RTP in and out, and I know rt linux reduces the latency even further.

So that's the CPU and real-time sorted.

As for GPIO's and the like well there's I2C and SPI both available for GPIO expanders and A-D's, D-A's etc.

There are some good points about environmentals, most will be sorted out with a suitable box, probably the old box that the original ECU was in (which also has a plug for the loom), but vibration might be a problem especially in my car with fairly stiff suspension. Possibly solder down the SD card...

So my idea is to make a pi extender board with similar drivers etc as per megasquirt, which plugs into the 40-way expansion plug.

My ECU only has to control fuelling, on banked injectors, but I could look at ignition and timing later.

Bring it on!

Return to “General programming discussion”