Categories
Experimentation! Project Logs

LFS Dashboard Pt1

Well where to start on this one… I suppose my initial inspiration came from this post on HackADay, This lead me to the discovery of Live For Speed and my new gaming addiction! It wasnt long before I had a C# program connecting to the LFS out-gauge API and displaying tachometer info on a small LED display I hacked together on the LPT port… (I will detail this in another post some time 🙂 ) But it wasn’t long before I decided to move onto, ah, bigger ideas 😛

The plan is to have a C# app connecting to LFS and sending data to a PIC micro through a USB-RS232 converter, and then have the PIC control a real instrument cluster! Initially I connected a pic up and wrote a lil C# app to test the theory and mannaged to get the pic to output a variable duty PWM signal dependent on the car’s RPM!! 😀 So onto step 2!

Yesterday I ordered a Logitech G27 to really get some realism going! This should be arriving in a week or two so that’s my time-frame to try get the instrument cluster finished. After ordering the wheel I set off into the depths of the Verulam scrap yards to try find a cheap workable gauge cluster, Initially everywhere was too expensive wanting R500-R700 for the cheapest 2ND HAND!! cluster… But just as I thought all hope was lost I pulled in to a place called “Mandos used spares” where I found this:

BMW Instrument Cluster…

At first they wanted R750 for item, but after some bargaining I managed to get it for R400! Unfortunately I didn’t know if or how easy it would be to use, but I kinda guessed Alex Rosiu was using a similar BMW cluster so how hard could it be!?

Immediately after getting home I located the part number and started a 3 hour long Google quest!:

Part Number: 6932894…

Unfortunately I only found small scattered amounts of info on this cluster, which I now believe to be a cluster off of a 2002 BMW E46. Eventually, after some creative Googling, I tracked down this pin-out which seems to match. However I didn’t see any simple inputs for speed/tachometer etc that I could just stick a PWM signal into and get the needles to move, by the look of things I figured I may have to talk to to the on-board cluster computer somehow and send it the right info serially, something I half expected  I would need to do this being typically BMW… But time to open her up and take a look:

The cluster open revealing lotsa LED’s and even a small LCD!! 😀

Yes I had all ready de-soldered the black connector and hacked in some power and ground pins to test with…

Things were WAY more complicated on the inside than I had originally expected.. I connected it up to 12v from the bench supply and things came to life! but this didn’t help much as I had no way of getting data into it yet… Back to Google and I eventually discovered that MOST of the data was sent to the cluster, not over some simple serial protocol as I had originally hoped but over the vehicles CAN serial interface from the ECU… BIG complications. At this point after a bit of thinking I figured I was stuck with two options:

1) Get my PIC to talk to the cluster over the CAN interface. The problem with this idea is I dont know what ‘Language’ the cluster talks over the CAN interface, AND I dont have any experience making PICs communicate anything other than with the UART over RS232… As far as I can tell, unless I could find a very well documented example of talking to the cluster over the CAN on the net, which doesn’t seem very likely but if you have any ideas PLEASE let me know! my only other option would be to get a Bus Pirate and an E46 BMW and sniff the traffic.. and then inject it into my cluster. All very possible but a huge amount of work and a bit out of my league for the moment and for a relatively simple project… OR

2) Figure out how to control the mechanics that move the needles and implement my own controller from scratch with a PIC. Possibly easier than option 1 although still alot more work than I originally anticipated.. and it would mean basically destroying my R400 cluster!

To make a decision I decided I needed to see what mechanics drove the needles.. I quickly de-soldered one of the ‘Motors’ to have a look hoping it would be similar to an analog Volt or mA meter that could be driven directly from PWM..

No such luck.. Turns out they are miniature Bipolar Stepper motors! It just keeps getting more complicated! Time for some more thinking…

At first I had NO idea how I would drive this thing, I’m sure I could make It move with a PIC but how would I tell what position It was at? Then I realized its a bipolar STEPPER motor, It moves in steps! It has a small tab internally that stops it at a maximum and minimum position and I’d say its range is about 300′. Theoretically I could just set it to the minimum position and move it forward and backwards with the PIC while keeping a counter for how far away it is from the ‘Zero’ position and thus know ‘where’ it is. EXCITING!

Each coil measured 275ohm so at 5v they would pass about 18.5mA of current and according to the data-sheet a PIC 16f88 can sink or source up to 25mA on each I/O pin, so I could drive it directly at-least. After reading up on how to control Bipolar Steppers I connected it to the PIC and through together some rough code.

From the 12 o’clock position going counter-clockwise PIN A,B,C,D

Basically the control scheme for 1 ‘step’ is to connect the pins in the following order AB,CD,BA,DC (where BA is the reverse polarity of AB) some code:

#include <stdio.h>
#include <htc.h>
#include <delay.h>
#include <delay.c>
#include <usart.c>
#include <usart.h>
#include <stdlib.h>

__CONFIG(0x3F30);
__CONFIG(0X3FFC);

//define
#define _XTAL_FREQ=8MHZ
#define A        RA1
#define B        RA0
#define C        RA7
#define D        RA6
#define dly     3
#define TRUE    1
#define FALSE   0

volatile char speed_current, count;
char data_in

void init(void)
{
IRCF2=1; //
IRCF1=1; // These set the internal osc @ 8Mhz. Prob overkill 😀
IRCF0=1; //

TRISA=0b00000000;
TRISB=0b01001111; // make RB6/3/2/1/0 input

GIE=1;
PEIE=1;
init_comms();    // set up the USART – settings defined in usart.h

// Output a message
printf(“\rHello There 🙂 I’m online! :D\n”);

}

void interrupt my_isr(void)
{
if((RCIE)&&(RCIF)) // Data has arrived via usart
{
data_in = getch();
//No clearing RCIF, must clear RCREG
RB4 = !RB4;
RCREG = 0;
}
}

void fwd1stp(void);
void rev1stp(void);
void startup(void);

//main function
//================================================================
void main(void)
{

//device peripheral configuration
init();
startup();
speed_current = 0;
ei();
RCIE = 1;
RB4 = !RB4;

//infinite loop
while(1)
{
if(data_in > speed_current)
{
fwd1stp();
speed_current++;

}

if(data_in < speed_current)
{
rev1stp();
speed_current–;
}

}
}

void fwd1stp(void)
{
D=1;
DelayMs(dly);
D=0;
B=1;
DelayMs(dly);
B=0;
C=1;
DelayMs(dly);
C=0;
A=1;
DelayMs(dly);
A=0;
}

void rev1stp(void)
{
A=1;
DelayMs(dly);
A=0;
C=1;
DelayMs(dly);
C=0;
B=1;
DelayMs(dly);
B=0;
D=1;
DelayMs(dly);
D=0;
}

void startup(void)
{
for(int c =0; c<120; c++)
{
fwd1stp();
}
for(int c =0; c<120; c++)
{
rev1stp();
}

}

I found a delay of about 2ms between coil transitions worked best however I think I didn’t quite get the wiring of the coils right and the stepper code can certainly use some improvement like finer delay sequences and half-stepping to make it A LOT smoother and increase the resolution… I’ll get to this eventually! but for the moment IT WORKS! here is a quick vid I made of the stepper being controlled by LFS: (I apologize for holding the camera the wrong way and the shitty vid xD in my defense it was about 6AM and I hadn’t had my coffee yet! :P)

Well for now thats that! when I get home this evening I’m going to do some more work on this project and see if I can get it all up and running! feel free to comment and keep a look out for part 2!

~Rob.

Categories
Experimentation!

Learning Serial Communication

This morning I decided its about time I learnt how to use the UART hardware in most PIC microcontrollers. I started off with a USB-RS-232 converter that I got for US$3 from DealExtreme. There are a few reasons why I used a converter, firstly being because my main PC no longer has an RS-232 port since its old tech, but also because I thought it’d be alot neater for my projects to have a USB connection and it means I can interface with TTL signal levels (0-5v) instead of the RS-232 standards -12v – +12v which would have added complications, but we’ll get back to that just now…

DealExtreme USB-Serial connectors

The USB-Serial adapter from Dealextreme, the Bottom one has been modded to remove the DB9 connector and the MAX211 IC as well as adding connections to the TTL TX, RX and Gnd

(sorry about the pics again, using my phone, camera isn’t back yet 😛 )

I Started by removing the MAX211 IC which takes the PL-2303 (the usb-serial IC) TTL level signals and converts them to -12v – 12v and vice versa. Since we will be interfacing with a TTL level part, the 16F88, this is no longer needed. I also removed the DB9 connector because it was getting in the way and I’m not going to use it anyway 😛 Also added wires to the TXD, RXD and Gnd pads so I can connect it to my breadboard 🙂

Next I connected TX and RX together for a loop-back test to make sure everything is still working:

USB-Serial in PC

Loopback test

Everything seems to be working well! So onto the microcontroller side of things..

I’m currently using MPLAB and the Hi-Tech C compiler after recently teaching myself C for microcontrollers (this is another complicated topic without many easy beginner tutorials, you just sorta have to jump and and google your way to success! perhaps one-day I’ll write a post about it… 😉 ) and an ICD2 to program. Luckily in the Hi-Tech samples folder there is a USART example! this made life alot easier… here it is:

usart example

A nice simple sample program to echo the users keypress’s

I quickly burnt it onto the 16F88, connected the usb-serial dongle and fired up the Termite, pressed some keys and couldn’t believe my luck! 😀

USART test

Bi-directional serial communication with my PIC 16F88!!! I thought this would be really difficult but it turned out to be easier than expected 🙂

What are the next steps? We’ll I recently completed a simple automated irrigation system for my vegitable patch, which I’ll document soon in another post, and I would like to have it communicating with a small C# app on my server PC that will have a web interface such that I can set watering times and dates 🙂 I have some other project ideas I’d like to try out aswell like servo controlled blinds and an automated coffee machine 😀 😛

Hope you enjoyed my ramblings! until next time 😉

~Rob.