06-27-2008, 04:54 PM
|
#21 (permalink)
|
|
EcoModding Lurker
Join Date: Jun 2008
Location: USA
Posts: 32
|
Well, this is what I've done so far, ignore the serial stuff, I was doing that out of naivety, (actually, it might work if you have a rs232>OBDII adapter), feel free to take whatever you want out of it. Basically all I was interested in was instant mpg, speed, and rpm at the moment, so all the calculations and such should work. The display code definitely works as tested, haven't hooked it into a vehicle yet though. I'm waiting on a serial>usb adapter for my laptop to continue work on it.
Code:
/*
OpenScanGauge
*/
#include <LCD4Bit.h>
#include <stdio.h>
LCD4Bit lcd = LCD4Bit(4);
double vss;
double maf;
double mpg;
int rpm;
//pins to use for data
int busneg=0;
int buspos=1;
//arrays to store values returned from ECU
char vss_byte[10];
char maf_byte[10];
char rpm_byte[10];
//hex commands for speed, MAF, and rpm
char* vss_cmd[6] = {"61", "6A", "F1", "01", "0D", "8B"};
char* maf_cmd[6] = {"61", "6A", "F1", "01", "10", "C7"};
char* rpm_cmd[6] = {"61", "6A", "F1", "01", "0C", "96"};
//array to look up binary values for hex chars
char* hexconvert[15][2] = {{"0","0000"},{"1","0001"},{"2","0010"},{"3","0011"},{"4","0100"},{"5","0101"},{"6","0110"},{"7","0111"},{"8","1000"},{"9","1001"},{"A","1010"},{"B","1011"},{"C","1100"},{"D","1101"},{"E","1110"},{"F","1111"}}
char vss_str[5];
char mpg_str[4];
char rpm_str[5];
void setup() {
lcd.init();
}
void loop() {
getValues();
printOutput();
}
void printOutput() {
lcd.clear();
lcd.cursorTo(1,0);
lcd.printIn("MPG: ");
lcd.cursorTo(1,5);
mpg = (710.7 * vss) / maf;
sprintf(mpg_str, "%f", mpg);
lcd.printIn(mpg_str);
lcd.cursorTo(1,9);
lcd.printIn("Speed: ");
lcd.cursorTo(1,16);
vss = vss * 0.6214;
sprintf(vss_str, "%f", vss);
lcd.printIn(vss_str);
lcd.cursorTo(3,0);
lcd.printIn("RPM: ");
lcd.cursorTo(3,5);
sprintf(rpm_str, "%d", rpm);
lcd.printIn(rpm_str);
delay (1000);
}
void getValues() {
pwm_send(vss_cmd);
vss_byte = pwm_read();
vss = vss_byte[5];
pwm_send(rpm_cmd);
rpm_byte = pwm_read();
rpm = .25 * (rpm_byte[5] * 256 + rpm_byte[6]);
pwm_send(maf_cmd);
maf_byte = pwm_read();
maf = .01 * ((256 * maf_byte[5]) + maf_byte[6]);
}
void pwm_send(char* cmd[]) {
char bincmd[] = hex2bin(cmd[]);
pinmode(busneg, OUTPUT);
//send SOF and drop back to neutral
AnalogWrite(buspos, 255);
digitalWrite(busneg, LOW);
delayMicroseconds(32);
AnalogWrite(buspos, 0);
digitalWrite(busneg, LOW);
//loop through array of bits
for (int i=0; i < 48; i++) {
//send 0
if bincmd[i] = '0' {
analogwrite(buspos, 255);
digitalwrite(busneg, LOW);
delaymicroseconds(16);
AnalogWrite(buspos, 0);
digitalWrite(busneg, LOW);
}
//send 1
else if (bincmd[i] = '1') {
analogwrite(buspos, 255);
digitalwrite(busneg, LOW);
delaymicroseconds(8);
AnalogWrite(buspos, 0);
digitalWrite(busneg, LOW);
}
}
//send EOF to be here
}
char[] pwm_read() {
pinmode(busneg, INPUT);
//code for reading values here (complex methinks)
}
//convert hex command to binary
char[] hex2bin(char* cmd[]) {
char bincmd[];
int binloc = 0;
for(int i=0; i < 6; i++) {
for(int j=0; j < 15; j++) {
if (cmd[i][0] = hexconvert[j][0]) {
for (int k=; k < 4; k++) {
bincmd[binloc] = hexconvert[j][1][k];
binloc++;
}
}
}
for(int j=0; j < 15; j++) {
if (cmd[i][1] = hexconvert[j][0]) {
for (int k=; k < 4; k++) {
bincmd[binloc] = hexconvert[j][1][k];
binloc++;
}
}
}
}
return bincmd;
}
Last edited by n8thegr8; 06-30-2008 at 04:23 PM.
|
|
|
|
06-27-2008, 04:55 PM
|
#22 (permalink)
|
|
OBDuino coder
Join Date: Jun 2008
Location: Montréal, QC
Posts: 95
|
I made changes in my previous code, check message
OBD MPGuino gauge
As always, untested as I have no hardware!!
__________________
|
|
|
|
06-27-2008, 04:58 PM
|
#23 (permalink)
|
|
OBDuino coder
Join Date: Jun 2008
Location: Montréal, QC
Posts: 95
|
LOL we posted at the same time 
__________________
|
|
|
|
06-28-2008, 01:58 PM
|
#24 (permalink)
|
|
EcoModding Lurker
Join Date: Jun 2008
Location: ohio
Posts: 27
|
I have been working on my own obd II MPG Gauge for a while now using the elm323 and the pic16f877a but have run into quite a few snags. I think im going to give up and start working with you guys. Hopefully I can be of some help. I have ordered all of the parts and went with the 16x2 LCD. I have not ordered an obd cable yet but I am thinking of ordering the standard male 10 pin for $5.
|
|
|
|
06-28-2008, 06:08 PM
|
#25 (permalink)
|
|
EcoModding Lurker
Join Date: Jun 2008
Location: USA
Posts: 32
|
yeah, I built my cable with the connector from mouser for my usb>obdII reader I built (also a pic btw, pic18f2455) I mainly built it for diagnostics and for checking my arduino up against. SO...I'll only be working on PWM, as that's the only car I have access to, BUT the code should work on VPW as well with slight modifications (vpw uses only the 1 data line instead of 2, but same logically and physically otherwise).
I have found a few more resources about it, and also, I now have a noob knowledge of how differential bus works :P. apparently, you send the signal on the bus+ line, and send the complement of that signal on the bus- line, so from what I understand, when high on bus+, you need to pull bus- low, and vice versa. If I am wrong, PLEASE correct me. Anywho, so I don't think I need any extra hardware to bit bang it, so I'll be working on functions for sending the triggers and converting the hex code to the appropriate high's and low's. Here's a little more about pwm (and vpw for that matter) for those interested:
SAE J1850 Description, Electrical Interface Bus
|
|
|
|
06-28-2008, 06:19 PM
|
#26 (permalink)
|
|
EcoModding Lurker
Join Date: Jun 2008
Location: ohio
Posts: 27
|
I have never worked with the quino board before. Do I need anything special to connect my lcd and my MCZ33290EF or is it just plug and play?
|
|
|
|
06-28-2008, 07:30 PM
|
#27 (permalink)
|
|
EcoModding Lurker
Join Date: Jun 2008
Location: USA
Posts: 32
|
the arduino platform is generally pretty flexible. Nearly all of it's pins can be reassigned to do certain tasks. typically, all you need to do to hook it up to a peripheral is wire it to whichever pins you want to use to drive it. Check out the playground on Arduino - HomePage. they have a lot of really good examples and code to get you started. I would also suggest looking into the 4bit LCD library, so it will cut down on how many wires you have to connect and free up some pins to use for other things (cuts it from 8 wires to 4), that's what I use with my lcd.
|
|
|
|
06-29-2008, 12:33 PM
|
#29 (permalink)
|
|
EcoModding Lurker
Join Date: Jun 2008
Location: USA
Posts: 32
|
sweet, that looks like a pretty neat little project. I've turned up some more on VPW and PWM, they both use "bit symbols", so it's not the fact that the bus is high or low that matters, it's for how long the bus is high or low (in microseconds). This is going to be interesting, I got started on the code to generate the necessary pulses. Here's some more reading material for those interested in the timings for bits:
Third draft of Eval Eng article
The one of interest for PWM is the very last table at the bottom of the page.
|
|
|
|
06-29-2008, 02:39 PM
|
#30 (permalink)
|
|
EcoModding Lurker
Join Date: Jun 2008
Location: ohio
Posts: 27
|
In the mpguino forum they used the pulse width but last I read it was innacurate in terms of MPG. Wouldnt it be more accurate to use your liters x 14.7% to get your fuel use per revolution and multiply that by your rpm. With a little conversion you get gallons per hour. Divide MPH by GPH and get MPG. I dont know if that would be easier or not but as long as your getting MPG I guess it doesnt matter.
|
|
|
|
06-29-2008, 03:50 PM
|
#31 (permalink)
|
|
Master EcoModder
Join Date: Feb 2008
Location: 3rd rock
Posts: 1,308
|
Man I love the energy you guys are putting into this
Quote:
Originally Posted by tom43571
In the mpguino forum they used the pulse width but last I read it was innacurate in terms of MPG.
|
FYI, Yoshi has been reporting mpg results within 1% on a prius by monitoring pulse width.
|
|
|
|
06-29-2008, 06:30 PM
|
#32 (permalink)
|
|
EcoModding Lurker
Join Date: Jun 2008
Location: USA
Posts: 32
|
If I'm not mistaken, I think you guys are confusing measuring pulse width off the injectors (MPGuino wasn't obdii was it?) with what I'm talking about. The "Pulse width" I'm carrying on about is the J1850-PWM OBDII protocol. The J1850 protocol uses VPW (a single wire) or PWM (a balanced bus 2-wire) at the physical level to send the bits. My car (1998 mustang) uses the PWM flavor of the J1850 protocol.
It should be every bit as accurate as a scanguage, because it's pulling the same information and running the same formula against it (calculating based on speed and MAF reading from the OBDII port rather than reading off the injectors).
|
|
|
|
06-29-2008, 07:04 PM
|
#33 (permalink)
|
|
Master EcoModder
Join Date: Feb 2008
Location: 3rd rock
Posts: 1,308
|
Correct, mpguino hooks up directly to the fuel injector, no obd involved. Same technique as Yoshi's supermid basically.
I would think an obduino could possibly improve on the scangauge accuracy if it is a concern, by cranking up the polling rate and/or taking more variables into account as available on certain cars.
Keep up the good work guys 
|
|
|
|
06-29-2008, 08:44 PM
|
#34 (permalink)
|
|
Liberti
Join Date: Feb 2008
Location: California
Posts: 504
|
Like Nate posted, the only way to universally determine MPG via OBDII is to use the MAF sensor (or convert to MAF via the MAP & IAT sensors). The equations are posted earlier in this thread.
Fuel injector pulsewidth is not a standard OBD-II PID...
Great work guys. I wish I could contribute code as this is a project I really want to see hit fruition.
- LostCause
|
|
|
|
06-29-2008, 09:06 PM
|
#35 (permalink)
|
|
EcoModding Lurker
Join Date: Jun 2008
Location: ohio
Posts: 27
|
Sweet. I think I get it now. Please correct me if im wrong, VPW uses k-line and 12v while PWM uses k and L-line plus 12volt. I wish I had some hardware here to get going on this. Does this mean that the VPW will be using only 2 pins of the OBD(Pins 7 and 16)?
|
|
|
|
06-29-2008, 09:58 PM
|
#36 (permalink)
|
|
EcoModding Lurker
Join Date: Jun 2008
Location: USA
Posts: 32
|
actually, the K and L lines are for ISO only I believe, pwm uses J1850 bus+ and bus-, which are 2 seperate lines (pins 2 and 10), and VPW just uses the the J1850 bus+ (pin 2). K and L lines are on pins 7 and 15 for ISO, and I dunno much about CAN.
I also now know how you can have a -5v, lol. That was confusing the hell out of me. it's measured in reference to each other, so an active drive high is 5v on bus+ and 0v on bus-, and a drive low just swaps the polarity. This is getting intense, lol. So far the only code I've driven is the SOF, but I've set up the arrays for converting hex to binary and such.
My code will only support J1850, since I don't have anything else to test with, but if someone wants to add my code to theirs to make it more comprehensive once I get something working, that's cool by me.
|
|
|
|
06-30-2008, 12:58 AM
|
#37 (permalink)
|
|
EcoModding Lurker
Join Date: Jun 2008
Location: ohio
Posts: 27
|
N8THEGR8 are you using the freeduino and the MCZ33290EF chip or are you using your previous OBDII project for all of this? Im just trying to get on the same page as you, I am going to be using the freeduino setup and was thinking of hooking it up based on the schematic on post 9 when the hardware arrives.
|
|
|
|
06-30-2008, 10:10 AM
|
#38 (permalink)
|
|
EcoModding Lurker
Join Date: Jun 2008
Location: USA
Posts: 32
|
yeah, I guess I shoulda explained myself, lol. I'm doing this on an arduino single sided serial that I built from scratch ( MasterNater: Arduino Finished!!! and MasterNater: SO MUCH DONE!!!). Just your basic, plain jane arduino.
Since the arduino can drive +/-5v with the digital and analog pins, I'm *attempting* to do this without any additional hardware (no mcz chip, just connecting the arduino 100% directly to the OBDII port), that's why I was stating that it'll only work on pwm and vpw systems, but my code should be easy to add to someone elses project that does run ISO to get it running multiple protocols.
Do you know which protocol your vehicle uses? because you might want to consider that in order to direct which way you go. You can look up the model and year here: ScanTool.net - Resources - Compatible Vehicles
I've finished the method for converting hex to binary, now I should just be able to loop through the array of bits to drive the data, hopefully. I still don't know how successful I'll be, as I still kinda feel like I'm fumbling in the dark, lol.
|
|
|
|
06-30-2008, 10:44 AM
|
#39 (permalink)
|
|
OBDuino coder
Join Date: Jun 2008
Location: Montréal, QC
Posts: 95
|
The MCZ33290EF is for ISO K line only, I am working on it, n8thegr8 works on the PWM/VPW stuff that I don't have a clue about
I received my OBD-II plug, yeah  waiting for the remaining...
BTW n8thegr8, in my code I posted on page 1, I use some function like iso_write_data() or iso_read_byte() whatever, maybe it would be good to have only one generic program that does the calculation/display/etc so we will not work on the same thing? And you can work on pwm_read_data() or pwm_write_byte() and so on.
EDIT: I updated the code on first page to my latest version. It should display instant gasoline consumption, in L/100 or US MPG. Too bad I can not test it LOL.
__________________
Last edited by Magister; 06-30-2008 at 03:23 PM.
|
|
|
|
06-30-2008, 04:28 PM
|
#40 (permalink)
|
|
EcoModding Lurker
Join Date: Jun 2008
Location: USA
Posts: 32
|
Interesting, that sounds like a good idea. I updated my code on page 2 as well with what I have so far if you want to take anything from it. I haven't even compiled it yet, and I'm the kind of coder that throws crap together and lets the debugger sort it out, lol, but it's lookin good to me so far. It's pretty rough, but I commented it so you can get an idea of what i'm *trying* to do, lol. Maybe we could create a header file containing our common data/functions? or maybe start a google code project page, lol. hrmmmm....damn, I'd really like to start testing this.
|
|
|
|
BMW Car Insurance Getting car insurance for a BMW can be an expensive business. Use our detailed comparison service to find a competitive deal!
|