Back on the Vec(tra)

Some time ago I made a first OBD interface circuit for a Opel Vectra, and I even got some data back that I could at least interpret and check against the data displayed on the car’s dashboard. Now I quickly found out that there is more than meets the eye. There are a lot of different standards and I was not quite sure which one exactly is used in the Opel Vectra C. However, I did found out about the ELM327 and STN1110 IC who allow interchanging data over OBD in a more controlled way. Under the hood of both these devices is a PIC microcontroller so in fact there is not really anything special about the circuitry, there is just the firmware doing its magic abstracting things away for who over uses these IC’s. You can order the IC or you can pick up end-products for as low as € 20, and because it hides some of the more complex OBD protocol features it is really a good toy to get you going on OBD reading. There is also a datasheet that explains how developers should interact with the ELM327, really handy! I picked up one of these (which is obviously a Chinese clone of the real ELM327 interface):

The ELM327 based devices can be had in different types: USB, WiFi, BT, RS232… And once we have this off course we go back to our previous code and adopt it to our new interface, and after some experimenting I have a basic command line mode way of exchanging data running. Until now the tool allows for auto detecting the interface, connect to it and then ask the user which commands he would like to execute. The console then replies which whatever data is received.

Currently I’m also working on logging what ECU’s we have been talking with, creating a more user friendly form based user interface, make the software auto config the ELM327 for correct operation, save some user data on disk. Other stuff I would like to add is a monitor mode, some debug logging, set up the software to allow translations of texts.

Here is some of the console output I was able to create today:

run:
Not connected
Scanning for serial ports
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
Scanning serial ports completed
=========================================
Auto detecting ELM327 device
Connecting to serial port
Connected to /dev/ttyUSB0 @ 38400 baud/s
=========================================
READY
Enter command: ATH1
ATH1
OK

Enter command: AT@2
AT@2
?

Enter command: AT@1
AT@1
OBDII to RS232 Interpreter

Enter command: ATH1
ATH1
OK

Enter command: 0101
0101
7E8 06 41 01 81 06 80 00

Enter command: 0105
0105
7E8 03 41 05 3C

Enter command: 0104
0104
7E8 03 41 04 00

Enter command: ATD
ATD
OK

Enter command: ATDP
ATDP
AUTO, ISO 15765-4 (CAN 11/500)

Enter command: 0151
0151
7F 01 12

Enter command: 0902
0902
7F 09 78
014
0: 49 02 01 57 30 4C
1: 30 5A 43 46 33 35 34
2: 31 90 90 90 90 90 31

Enter command: STOP
Not connected
Experimental: JNI_OnLoad called.
BUILD SUCCESSFUL (total time: 5 minutes 40 seconds)

The ELM327 allows AT commands to configure it, if a commands is received that does not begin with “AT” than the command is considered to be an OBD command in which the replies differ in layout as you may have noticed in the output above. Command ATH1 sets the ELM to display OBD packet headers from which we can retrieve address info. A bit later, the OBD command 0105 replies with “7E8 03 41 05 3C“. Because we’re talking about CAN replies here (11bit CAN, 500kbaud), 7E8 is the address of the replying ECU. 03 tell us that we’re receiving 3 byte’s of data. The first byte of data, 41, tell us it is a response to a mode 01 request (01 + 40, 40 is a constant number to differentiate replies from requests). The next byte 05 is a copy of the 05 OBD PID that is also found in our request. The final byte is where the actual data sits. 3C is a hex number (as all the others are), if we convert it to a decimal number we get 60. The formula for OBD mode 01 PID 05 states that we should take 40 from this decimal number in order to get the correct result, and so 60 – 40 equals 20, a possible temperature for a cold engine. In the next post I’ll hopefully be able to show a little more user interface and less OBD details, a presto!

Java 8 benchmarking on ARM and x86

In a previous test I saw already that Java 8 might bring some big improvements for ARM devices compared with previous Java versions. Now with newer versions available for both Java 7 and 8 I found it useful to redo these test and check if the same performance gain applies for x86 machines. Here are the results:

Afbeelding

x86

On ARM devices we can see that the newer builds for both Java 7 (OpenJDK) and 8 (Oracle) do not bring more performance compared to previous Java builds. On the other hand it confirms once again that Java 8 has some nice performance tweaks on board and lets hope these tweaks also find their way in the release build.

For x86 (32-bit) however there is no difference in performance when using Java 8. However, code-wise, Java 8 off course brings some new features, but that’s not the focus of these tests.

“Call by value” vs “Call by reference” in C# (bis)

The previous example is good to get you going, there are however some side notes here to make. When a reference type is called by value, it is actually taking a shallow copy of the variable you pass into the method. Almost like you’re doing this:
variable inside the method  = variable outside the method
So you have 2 objects pointing to the same data, and so it is normal that when you’re adjusting the data inside the method it also applies to the data outside of the method, because it is the same data in memory. However, if inside of your method you reassign the method variable to a different object, a new object or null, the changes you then make after reassigning will no longer affect to variable outside of the method, because we are simply no longer referring to it.

And that is where the difference relies for calling by reference for reference types. The variable inside the method is not a copy of the variable passed into the method, it is the same variable which implies that if you assign it to null, a new object or another object inside the method these changes are also applied for the outer method variable. And to illustrate this: a code example.

In the previous example, add these functions:

private static void SetInitialData(List<int> numbers)
{

numbers = new List<int>();
numbers.Add(10);
numbers.Add(11);
numbers.Add(12);

}

private static void SetInitialData(ref List<int> numbers)
{

numbers = new List<int>();
numbers.Add(10);
numbers.Add(11);
numbers.Add(12);

}

And in the Main function, add these statements:

Console.WriteLine (“\nReset called by value…”);
SetInitialData(numbers);
for (int i=0; i<numbers.Count; i++) Console.WriteLine (“numbers[“ + i + “] = “ + numbers[i]);

Console.WriteLine (“\nReset called by reference…”);
SetInitialData(ref numbers);
for (int i=0; i<numbers.Count; i++) Console.WriteLine (“numbers[“ + i + “] = “ + numbers[i]);

We now get the following output:

k = 1
numbers[0] = 10
numbers[1] = 11
numbers[2] = 12

Increment called by value…
k = 1
numbers[0] = 11
numbers[1] = 12
numbers[2] = 13

Increment called by reference…
k = 2
numbers[0] = 12
numbers[1] = 13
numbers[2] = 14

Add called by value…
numbers[0] = 12
numbers[1] = 13
numbers[2] = 14
numbers[3] = 20

Add called by reference…
numbers[0] = 12
numbers[1] = 13
numbers[2] = 14
numbers[3] = 20
numbers[4] = 25

Reset called by value…
numbers[0] = 12
numbers[1] = 13
numbers[2] = 14
numbers[3] = 20
numbers[4] = 25

Reset called by reference…
numbers[0] = 10
numbers[1] = 11
numbers[2] = 12

As you can see, in the call by value method we reassigned the method variable and thus changes to this variable no longer applied to the variable passed into the method. In the call by reference method changes do apply because we’re basically working with the same variable.

Another thing to add here: variables of the type struct may look like a stripped down version of a class, it is however different in a way that a struct is a value type  while a class is a reference type. In coding a struct may look like a reference object of a class, however in behavior it  acts like primitive types like int, bool, … So if you pass a struct into a method and call it by value, the variable in the method is a copy of the struct passed into the method, and so if you change the struct variable in the method the changes will not apply to the struct outside of the method (unless you return the changes). When calling by reference, you’re working with the same variable as outside the method and so changes inside the method do also apply outside the method.

“Call by value” vs “Call by reference” in C#

In this small example I hope to show you the differences in passing reference and value types in C# and what happens if you make the parameters either call by value or call by reference. All combinations are possible.

Reference vs Value variable types
In C#, as you know there are different variable types, for example int’s are value types and List<in> and other objects are reference types. It means that a variable of type int contains a value that you can adjust directly in memory, while for reference variables the variable itself is more or less just a referring to a spot in memory where all the object data is located and so the reference variable itself actually only contains a memory address.

“Call by reference” vs “Call by value”
As you may or may not know there are also different ways of passing parameters in C#. You can either call them by value, which is the default way of doing so, but you can also call them by reference. To do this one needs to explicitly add ‘ref’ both in the method signature and when calling this method.
What is the difference? When you pass the parameters by value, a copy is being taken of the data. When using referenced parameters only the reference to the data is passed.
But what does this imply? Well.. when ‘calling by value’  you get a copy of the variable and whatever you do inside the method does not affect the variable outside of this method (unless you use a return statement and reassign the variable with the returning data). Yet, if you ‘call by reference’ you have a reference to the place in memory where the variable relies, and so when you’re changing the variable inside the method it will also affect the variable outside of the scope of the method.

There are however side affects that you need to know off… because reference type variables actually contain just a reference to a space in memory, the variable itself is always passed in a call by reference way, and so when passing reference types you do not explicitly have to do a call by reference, but it is happening implicitly.

Here is an example that shows the difference:

class MainClass
{

public static void Main (string[] args)
{

int k = 1; //value type
List<int> numbers = new List<int>(); //reference type
numbers.Add(10);
numbers.Add(11);
numbers.Add(12);

//print default values
Console.WriteLine (“k = “ + k);
for (int i=0; i<numbers.Count; i++) Console.WriteLine (“numbers[“ + i + “] = “ + numbers[i]);

Console.WriteLine (“\nIncrement called by value…”);
Increment(k); //value type, call by value
Increment(numbers); //reference type, call by value
Console.WriteLine (“k = “ + k);
for (int i=0; i<numbers.Count; i++) Console.WriteLine (“numbers[“ + i + “] = “ + numbers[i]);

Console.WriteLine (“\nIncrement called by reference…”);
Increment(ref k); //value type, call by reference
Increment(ref numbers); //reference type, call by reference
Console.WriteLine (“k = “ + k);
for (int i=0; i<numbers.Count; i++) Console.WriteLine (“numbers[“ + i + “] = “ + numbers[i]);

Console.WriteLine (“\nAdd called by value…”);
Add(numbers);
for (int i=0; i<numbers.Count; i++) Console.WriteLine (“numbers[“ + i + “] = “ + numbers[i]);

Console.WriteLine (“\nAdd called by reference…”);
Add(ref numbers);
for (int i=0; i<numbers.Count; i++) Console.WriteLine (“numbers[“ + i + “] = + numbers[i]);

}

#region called by value
private static void Increment (int k)
{

k++;

}

private static void Increment (List<int> numbers)
{

for (int i=0; i<numbers.Count; i++) numbers[i]++;

}

private static void Add (List<int> numbers)
{

numbers.Add(20);

}
#endregion

#region called by reference
private static void Increment (ref int k)
{

k++;

}

private static void Increment (ref List<int> numbers)
{

for (int i=0; i<numbers.Count; i++) numbers[i]++;

}

private static void Add (ref List<int> numbers)
{

numbers.Add(20);

}
#endregion

}//end of class

Output:

k = 1
numbers[0] = 10
numbers[1] = 11
numbers[2] = 12

Increment called by value…
k = 1
numbers[0] = 11
numbers[1] = 12
numbers[2] = 13

Increment called by reference…
k = 2
numbers[0] = 12
numbers[1] = 13
numbers[2] = 14

Add called by value…
numbers[0] = 12
numbers[1] = 13
numbers[2] = 14
numbers[3] = 20

Add called by reference…
numbers[0] = 12
numbers[1] = 13
numbers[2] = 14
numbers[3] = 20
numbers[4] = 25

As you can see, variable types called by value don’t get adjusted out of the method, only when you call them by reference. For reference types, there is no difference on first sight in between calling by value or calling by reference because the variable itself is already a reference.

Note: I’m testing this C# code on a Linux machine using MonoDevelop. On a Windows system using .Net you will however get the same results.

Using OpenCV in Eclipse under Linux

For OpenCV 2.4.6.1 …
Installing and using Eclipse under Linux is pretty much straight forward when using the windows tutorial for Eclipse and OpenCV build tutorial for Linux. Here is a fast run through both combined which should get you going in half an hour, depending on you internet connection and cpu power.

– First make sure you have Eclipse installed. This can be done easily from Ubuntu’s Software Center or through the Eclipse website.

– Install all required libraries to download and build OpenCV:
apt-get install cmake -f
apt-get install libgtk2.0-dev -f
apt-get install libdc1394-22 -f
apt-get install libjpeg-dev -f
apt-get install libpng-dev -f
apt-get install libtiff-dev -f
apt-get install libjasper-dev -f
apt-get install libavcodec-dev -f
apt-get install libavformat-dev -f
apt-get install libswscale-dev -f
apt-get install python-dev -f
apt-get install python-numpy -f
apt-get install git -f

– next, clone the OpenCV 2.4.6.1 files from the online repository:
mkdir opencv
cd opencv
git clone https://github.com/Itseez/opencv.git

– then, build OpenCV from source:
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ../opencv/

– after building has been completed, install:
make
sudo make install

– now lets configure Eclipse:
Now, open Eclipse and select Window -> Preferences from the menu.
Next, navigate to Java  -> Build Path -> User Libraries and click New …
Enter the library name: OpenCV-2.4.6.1
Then click Add External JARs…
and browse to the bin folder under the opencv release directory created above (for example /home/homeUser/opencv/release/bin)
Next extend the opencv-300.jar dropdown menu, navigate to Native library location and press Edit…
Now navigate to the lib folder under the opencv release directory created above (ex: /home/homeUser/opencv/release/lib)

– with Eclipse configured we can now test the library:
Create a new Java program.
During creation, when the wizard asks for Java Settings, choose the Libraries tab and select Add Library…
Select OpenCV-2.4.6.1 and finish the wizard.
Now create a new package and inside this package a new class called Hello
Enter following code:

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;

public class Hello
{
   public static void main( String[] args )
   {
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      Mat mat = Mat.eye( 3, 3, CvType.CV_8UC1 );
      System.out.println( "mat = " + mat.dump() );
   }
}

And run your code. You should see a 3 by 3 matrix output, more or less looking like this:

mat = [ 1, 0, 0;
0, 1, 0;
0, 0, 1]

Well done, enjoy the coding!
references:
http://docs.opencv.org/trunk/doc/tutorials/introduction/java_eclipse/java_eclipse.html#java-eclipse
http://docs.opencv.org/doc/tutorials/introduction/linux_install/linux_install.html#required-packages

Opel/Vauxhall Vectra CAN interfacing

Since I got this car second hand it has been through a series of faults, failures and repairs (which is actually hard to avoid at a mileage of 150k+ km), but the cost of taking it to the Opel dealer every time pushed me in to the direction of doing some of the car repairs myself lately. The down side, the car being made in 2004, is full of sensors and so whenever a problem arises it is quite hard to make good  diagnosis without proper tools to read out this sensor data. Many times, engine failures are due to malfunctioning sensors for example. And so I started to think about interfacing the cars CAN bus and see what data I can get, unfortunately this is not like opening a serial terminal and watch data appear…

First of all there is this CAN bus which is a two wire (CAN-H, CAN-L) physical layer and on top of that a kind of networking layer which makes CAN look more like a networking protocol than the common RS232 serial link. As I’m not the only one to think of such a project I found quite some info online and also some libraries for Arduino to get me started. I also looked at some of the info I could get out of the Chinese Op-com clone I have around here, but unfortunately the unit had died.

Some Arduino CAN bus projects:

Canduino: https://code.google.com/p/canduino/

Seeed studio CAN-BUS shield: http://www.seeedstudio.com/wiki/CAN-BUS_Shield

Sparkfun CAN-BUS shield: https://www.sparkfun.com/products/10039

SK Pang CAN-BUS shield: http://www.skpang.co.uk/catalog/arduino-canbus-shield-with-usd-card-holder-p-706.html

Furthermore there is also some info regarding making a Raspberry-pi based CAN interface:

http://elinux.org/RPi_CANBus

http://lnxpps.de/rpie/

Another good link is this one with some basic CAN bus functionality explained and a Arduino schematic:

http://modelrail.otenko.com/arduino/arduino-controller-area-network-can

And the “Hacking You Car” article by Marco Guardigli:

http://marco.guardigli.it/2010/10/hacking-your-car.html

All together, most projects use a MCP2551 CAN transceiver and MCP2515 CAN controller so most schematics looks more or less the same and so one can use one schematic and try the different software libraries that are available for Arduino. And so I took the ODB connecter from the broken Op-com unit and used it in my own schematic which looks more or less like this:

schematic

DSC05983 DSC05981

After trying some of these libraries I noticed that the SK Pang library is the easiest one to quickly get some data out of your car. Although the default Arduino code allows to get out some basic engine info easily and in a human readable way, I found not very impressive and so I’m now exploring on how I can modify the code the get more info out of the car. In the latest SAE J1979 OBD standard there are 10 modes of functioning described, where SK Pang uses mode 1 ($01. Show current data) to request and show the engine data.  Requests depend on the PID (packet indentifier) one sends, whenever a ECU responsible for this PID sees the request it will get the data and respond. Standard PIDs can be found online here: http://en.wikipedia.org/wiki/OBD-II_PIDs. And  so I started trying every combination in between 0 and 255 to see which ones I can get a reply to, here is some of the info:

#Reply: 0*152#
#Reply: 1*129#
#Reply: 5*65#
#Reply: 11*103#
#Reply: 12*24#
#Reply: 15*62#
#Reply: 16*11#
#Reply: 32*160#
#Reply: 35*20#

the first number is the PID, the second one is its value, both in decimal form. To get human readable output one has to look up the code and use the appropriate method to convert the data into a correct format. For example, the frame with PID 5 (0x05 in HEX value) contains the engine coolant temperature, the formula is as following: A-40. And so, our engine coolant temperature is 65-40=15°C.

DC motor control (reverse engineering that old toy pt.2)

Since I had the electronics worked out the last time it was now time take this thing one step further and add some control to it. I first made sure the software in the Arduino controlling the DC motors was able to listen to its serial port for incomming commands and afterwards control the motors according to the command that it received. I made sure I had a second Arduino board available and by using XBee modules I could now send commands from my pc wireless over the ZigBee network to my remote Arduino controlling the DC motors.

Next I found myself a XBOX 360 controller and because it has dual joysticks it can control both DC motors, one joystick for each motor. This one I hooked up to my laptop and next I wrote a small interfacing program in Java that translates the XBOX360 code into commands that are being accepted by my Arduino program controlling the motors.

The result:

 

DSC05979Sketch:

sketch_final

DC motor control (reverse engineering that old toy pt.1)

Since my return home I’ve been doing some paperwork and other ridiculous stuff, catching up the last 6 months and off course going to the 10 day festival that is yearly organised in my Belgian home town. But then I stumbled upon one of my first electronic kid toys ever, a Raccoon RC car that was in really bad condition. It has 6 tires/wheels of which only 4 are left over and off course the remote control is missing too. So no fun, unless… And so I decided to revive this toy, not because I like RC cars that much, just because off having something to do during the day-hours of the festival (and to recover from parties the day before).

Because of the remote control missing I had to reverse engineer the current circuit board inside the car. Unfortunately, the toy is like 30 years old and so off course the toy is based upon analogic components instead of digital ones, and off course with the datasheets of some components are missing. Great! Though it did not hold me back to start measuring around and understanding what kind of voltage and current levels are involved in driving the motors, because when you get that there is not much more holding you back from throwing in an Arduino or Raspberry Pi for example to do the rest of the task.

The car architecture is as following: the car has/had 6 wheels, 3 on each side and each side of wheels is driven by one DC motor, so there are 2 motors in total. The car has no wheel that can turn so the only way of making corners is by having different speed on the wheels for each side. If the left wheels are turning slower than those on the right, the car will turn left. Off course!

DSC05950

From my measurements I found the motor control circuit to look something like this:

h-bridge_schematics_old
(click to enlarge)

The two horizontal resistors central in the picture are the motors M1 and M2. From my first drawing things looked way too complicated, but after reading up a bit about DC motor control through H-bridge NPN and PNP transistor configurations I noticed they did exactly the same thing in this circuit. If you’re not common with H-bridge configurations read up a bit:
http://www.instructables.com/id/H-Bridge-on-a-Breadboard/step2/H-Bridge-Theory/

What is so different here to most schematics is that 2 motors are driven from the same circuit. For one motor you need 4 NPN transistors (or 2 NPN + 2 PNP transisotors: http://letsmakerobots.com/node/9450) and probable you would repeat the same configuration for adding a second motor. But the above on is also working, but you must keep in mind two things:
1) both of your motors should always go into the same direction. You can’t make M1 turn your left wheels to the front and make M2 drive your right wheels in the opposite direction because then both transistor in the middle of the schematic (Q7 and Q10) will be made conductive and so you will create a short between Vcc and ground resulting in white smoke and popped transistors!
2) The transistors in the middle need to conduct both the current from M1 and M2 when both motors are used, make sure that the transistors are rated for the expected amount of current. So far example, if you want to drive forward one should make Q6 and Q8 conducting and also Q10 in the middle. While Q6 and Q8 handle the current for their motor only, the current comes together at returns leads from each motor and is than flowing into Q10 which should so be able to handle both currents together.

As far as 1) goes, that is something I can handle in software (Arduino). For 2) I made sure to reuse the original design in some way but adapt it to my own needs. The transistors were rated good enough for the old circuit so they should be good enough now too. Also note in the above schematic the potentiometer on top which allows one to correct the differences in motors speed as one motor will never be exactly the same as the other one.

h-bridge_schematics_new
(click to enlarge)

The main setup didn’t change much, overall the NPN transistor H-bridge remained but now the transistor are controlled by Arduino pins. For each pin I first had 22k resistors connected to the transistor base but later I swapped these to 1k resistors as the transistors that I have never came to conduct. A breadboard sketch would look like this:

sketch

I however soldered mine because my breadboard is still in use for some other project. Note that in the image above the transistor has the base as the center pin. This is however different to the C3279 transistors I’m using where pin 2 is the collector. Also note that the Fritzing drawing tool does not support a socket for 3 batteries so that’s why there are 4 here. In real life 4 batteries would however result in 5,33~6 Volts depending on the batteries you choose resulting in more power from the motors (and more current through the transistors).

Some movement

I won’t quit the blog, it’s a nice and easy to use portal and it might someday contain usefull information (working on that!!). However as I’m no longer student in Porto (which was tremendously fun!!) I see no longer the need keep the Erasmusitis in Porto blog name and so that’s why I decided to change the blog into the current on, No Fab Lab, which is exactly what I am: not a fab lab. I will however continue to mess around with small electronics, embedded devices and programming, so stay tuned for more.

Oh, and life is not so bad back at home, I love Belgium too, not only Portugal 😉

Java benchmarking

Raspberry-Pi is compact and has aside of that other benefits which makes it a really cool tool to play around with. Unfortunately the processing power is limited compared to current day computers and so I hit the performance limit pretty early. But it seems now that Oracle has released a pre-release test version of Java 8 which comes with ARM performance tweaks. And so I was very curious to check out the differences between all Java versions I’ve been using past few months. I used Hwbot Prime to test the difference in computational power between OpenJDK6, OpenJDK7 and Oracle’s JDK8 (pre-release). Here are the results:

java benchmarking

As you can see from the above chart, there is nearly no difference in using OpenJDK 6 or 7, but because of the ARM tweaks found in JDK 8 from Oracle we see a huge performance boost there, roughly 2,5 times higher performance. Off course these results should be taken with a grain of salt. This is a pure computational test, this will not say anything about process threading, MySQL performance or IO performance.. But it’s a nice indication that things are getting better.