Wideband O2 to CAN communicator

A forum for discussing the MegaSquirt related (but non-B&G) board development, assembly, installation, and testing.

Moderators: jsmcortina, muythaibxr

humjaba
Helpful MS/Extra'er
Posts: 35
Joined: Fri Sep 26, 2014 11:17 am

Wideband O2 to CAN communicator

Post by humjaba »

So like the current top set of threads, I am getting started on my own project to make a CAN gauge. While I'm talking over CAN, I figured, why not send the digital O2 reading from my Innovate LC2 over the CANBUS and get some sweet smooth closed loop action as well? So far, not so good.

I am using a Teensy 3.1 micro controller (think arduino on steroids, has a built in CAN controller), and I have a max3232 serial to UART bridge. However I cannot seem to read any messages coming from the LC2 on the Teensy, through the max3232. Does anyone know if it follows normal RS232 protocol? I can read LC2 messages on my laptop in realterm, and I can send messages at the same baud rate to my teensy from realterm. But I can't read messages on the teensy from the LC2.

In summary, this works:
realterm (set to 19200,8n1) --> usb/rs232 adapter -> max3232 -> teensy --> serial monitor

and this works:
serial monitor --> teensy --> max3232 --> usb/rs232 adapter --> realterm (set to 19200, 8n1)

and this works:
wideband lc2 --> usb/rs232 adapter -> realterm (set to 19200, 8n1)

but this doesn't:
wideband lc2 --> max3232 --> teensy --> serial monitor
I'm not getting any messages whatsoever on the Teensy, not even some garbled mess suggesting that it's a translation issue. The only thing I can think of is that the LC2 doesn't actually use RS232. But I don't have a scope to check the TX voltage (and I don't want to fry my microcontroller if I'm wrong)

Code is simple:

Code: Select all

#define HWSERIAL Serial1


void setup() {
Serial.begin(9600);
HWSERIAL.begin(19200, SERIAL_8N1);
}

void loop() {
int incomingByte;

if (Serial.available() > 0) {
incomingByte = Serial.read();
Serial.print("USB received: ");
Serial.println(incomingByte, DEC);
//HWSERIAL.print("USB received:");
//HWSERIAL.println(incomingByte, DEC);
}
if (HWSERIAL.available() > 0) {
incomingByte = HWSERIAL.read();
Serial.print("UART received: ");
Serial.println(incomingByte, DEC);
//HWSERIAL.print("UART received:");
//HWSERIAL.println(incomingByte, DEC);
}
}
I know there are others of you who have done this (and some folks here who sell boxes which do the same thing..), but here's hoping someone who's embarked on something similar can shed some light. Thanks!
jacky4566
Helpful MS/Extra'er
Posts: 112
Joined: Mon Aug 31, 2015 10:05 am

Re: Wideband O2 to CAN communicator

Post by jacky4566 »

My first question is why not feed the o2 directly into the Megasquirt? It has the capability and you are reducing any lag factor introduced into the Teensy. Then you can read the o2 from the MS via CAN as other have done.

Secondly, Teensy is not an automotive hardened device so i would be REALLY careful about sending anything from the teensy that would affect fueling.
humjaba
Helpful MS/Extra'er
Posts: 35
Joined: Fri Sep 26, 2014 11:17 am

Re: Wideband O2 to CAN communicator

Post by humjaba »

It is currently but the only way to do that is with an 0-5v analog signal which will always have some kind of offset. By taking the digital signal from the serial output of the lc2 and feeding it over can, I remove two digital/analog conversion steps and the issue of the analog voltage never being quite right. Less noise too.

And it's a fun project. I'm not worried about it breaking because it's simple, and second because I have ego limits and ego only runs under light-medium load anyway. And there's no lag - actually less than trying to change the analog voltage. The Teensy clock speed is way higher than the lc2 updates.
slow_hemi6
Super MS/Extra'er
Posts: 4122
Joined: Fri May 07, 2004 3:33 am
Location: Australia

Re: Wideband O2 to CAN communicator

Post by slow_hemi6 »

Any chance you have Tx and Rx from the LC2 crossed?
Find the Manuals up top under Quick links: Manuals. :RTFM:
Cheers Luke
humjaba
Helpful MS/Extra'er
Posts: 35
Joined: Fri Sep 26, 2014 11:17 am

Re: Wideband O2 to CAN communicator

Post by humjaba »

slow_hemi6 wrote:Any chance you have Tx and Rx from the LC2 crossed?
It's a d-sub connector out of the LC2, and if that were the case, I wouldn't see anything in the terminal on my laptop right? In any case, I did try to switch tx/rx on the teensy, and (predictably) that did nothing.
slow_hemi6
Super MS/Extra'er
Posts: 4122
Joined: Fri May 07, 2004 3:33 am
Location: Australia

Re: Wideband O2 to CAN communicator

Post by slow_hemi6 »

Max chips don't work like that. They are directional level shifters. Transmit is Txin to Txout and Recieve is Rxin to Rxout. If you try to put Tx on Rx, on the 232 side of the device it will be blocked and if you try to put Rx on Tx, on the 232 of the device side it will be blocked. If it is blocked it never gets to the logic side so swapping the logic side does not fix the problem.
If ever I have a serial problem swapping Tx and Rx on the 232 side is always my first effort at sorting. From there I usually check that there is ground continuity between the devices and stages. Been bitten by that before.
Find the Manuals up top under Quick links: Manuals. :RTFM:
Cheers Luke
slow_hemi6
Super MS/Extra'er
Posts: 4122
Joined: Fri May 07, 2004 3:33 am
Location: Australia

Re: Wideband O2 to CAN communicator

Post by slow_hemi6 »

See the directional gates.
Image

Some devices have the internal cross over so Tx can talk to Rx and Rx can hear Tx these need a null modem connection. Others need the cross over to be in the cable. Not locking in a standard has made things a little complicated but it usually works one way or the other depending on your combination of devices.
Find the Manuals up top under Quick links: Manuals. :RTFM:
Cheers Luke
humjaba
Helpful MS/Extra'er
Posts: 35
Joined: Fri Sep 26, 2014 11:17 am

Re: Wideband O2 to CAN communicator

Post by humjaba »

slow_hemi6 wrote:See the directional gates.
Image

Some devices have the internal cross over so Tx can talk to Rx and Rx can hear Tx these need a null modem connection. Others need the cross over to be in the cable. Not locking in a standard has made things a little complicated but it usually works one way or the other depending on your combination of devices.
Yeah that's what I figured. But I wasn't getting any messages at all, so I switched to try and narrow down which wide of the 3232 the issue was on. Since I'm able to communicate with it through my laptop, it's on the rs232 side.

I'll see if I can rig up a way to switch the rx and TX lines.

Sent from my A0001 using Tapatalk
grom_e30
Super MS/Extra'er
Posts: 4461
Joined: Thu Mar 08, 2012 12:44 pm
Location: UK

Re: Wideband O2 to CAN communicator

Post by grom_e30 »

have you got the rx from the teensy to the tx on the max
1990 bmw 320i daily driver with m20b25 ms3 sequential fuel, 380cc injectors, d585 coil near plug, home made cam sync, launch control, fan control, vss, homebrew egt logging what's next????
racingmini_mtl
Super MS/Extra'er
Posts: 9130
Joined: Sun May 02, 2004 6:51 am
Location: Quebec, Canada
Contact:

Re: Wideband O2 to CAN communicator

Post by racingmini_mtl »

If you can try to use a MAX232 instead of the 3232, that could tell you if that's the issue. The Microsquirt uses the MAX3232 while the other models use the MAX232 and it is more picky about which devices can communicate with it. If the input signal is not a real RS232 signal, it doesn't seem to like it and nothing goes through the MAX3232.

There has been some posts about it on the forum.

Jean
jbperf.com Main site . . . . . . . . . . . . . . . . . . . . . . jbperf.com Forum
Image
dontz125
Super MS/Extra'er
Posts: 4228
Joined: Mon May 11, 2009 7:14 pm
Location: York, ON
Contact:

Re: Wideband O2 to CAN communicator

Post by dontz125 »

Curious - what do you mean by a "real" RS-232 signal? Since none of the chips mentioned are throwing +/-12v signals around, wouldn't the 3232 would just sit in its corner and sulk?
Temporarily shut down - back soon!
QuadraMAP Sensor Module -- PWM-to-Stepper Controller -- Dual Coil Driver
Coming soon: OctoMAP Sensor Module
TTR Ignition Systems
racingmini_mtl
Super MS/Extra'er
Posts: 9130
Joined: Sun May 02, 2004 6:51 am
Location: Quebec, Canada
Contact:

Re: Wideband O2 to CAN communicator

Post by racingmini_mtl »

dontz125 wrote:Curious - what do you mean by a "real" RS-232 signal? Since none of the chips mentioned are throwing +/-12v signals around, wouldn't the 3232 would just sit in its corner and sulk?
Something like a -V to +V signal where V=3 to 12V. There are many setups where the signal is 0-V where V=3.3V or 5V.

The MAX3232 doesn't seem to like a signal that doesn't go negative while the MAX232 does fine with it. At least that's what seems to be the case with the Microsquirt implementation vs the V3.0/V3.57 implementation.

Jean
jbperf.com Main site . . . . . . . . . . . . . . . . . . . . . . jbperf.com Forum
Image
humjaba
Helpful MS/Extra'er
Posts: 35
Joined: Fri Sep 26, 2014 11:17 am

Re: Wideband O2 to CAN communicator

Post by humjaba »

So the LC-2 out line has 4 wires but I'm unable to find any literature indicating what they are. I'm able to measure a steady 0.12V between what should be the gnd and tx lines on the db9 port (using the serial cable included with the lc-2). That doesn't even seem to be TTL level - what gives? :?
slow_hemi6
Super MS/Extra'er
Posts: 4122
Joined: Fri May 07, 2004 3:33 am
Location: Australia

Re: Wideband O2 to CAN communicator

Post by slow_hemi6 »

Red and White should be the signal lines. Will be hard to see data without a scope. Does this help?
Image
The flash scan bit is device specific and has no relevance.
Find the Manuals up top under Quick links: Manuals. :RTFM:
Cheers Luke
humjaba
Helpful MS/Extra'er
Posts: 35
Joined: Fri Sep 26, 2014 11:17 am

Re: Wideband O2 to CAN communicator

Post by humjaba »

Success! :D

My male to male dsub9 adapter either mirrored when it shouldn't, or didn't mirror, or whatever... in any case, I hooked in ground and TX as shown below and got it working. The code I found / modified to decode the signal coming in works as well - it reports when the LC-2 is warming up correctly, and then correctly spits out the AFR using the AFR multiplier as defined by the innovate protocol. Happy to share that if anyone's interested.

Image

Next step is to see if I can get it communicating with the MS3 over CAN. Will keep this posted I suppose.

Thanks for the help!
grom_e30
Super MS/Extra'er
Posts: 4461
Joined: Thu Mar 08, 2012 12:44 pm
Location: UK

Re: Wideband O2 to CAN communicator

Post by grom_e30 »

yeah post it up if you could
1990 bmw 320i daily driver with m20b25 ms3 sequential fuel, 380cc injectors, d585 coil near plug, home made cam sync, launch control, fan control, vss, homebrew egt logging what's next????
humjaba
Helpful MS/Extra'er
Posts: 35
Joined: Fri Sep 26, 2014 11:17 am

Re: Wideband O2 to CAN communicator

Post by humjaba »

I stole the majority of the translation code from kckr's MSCan_Gauge project:
https://github.com/kckr/MSCan_Gauge

I added comments and a line at the end because LC1_AFR wasn't being calculated properly

Code: Select all


void readO2Sensor() {
	byte incoming_byte;
	byte index;
	byte LC1_status;
	byte LC1_AFR;
	byte LC1_AFRs;
	int LC1_lambda;
	byte packet[7];     

	while (O2Serial.available() > 0){
		incoming_byte=O2Serial.read(); 
		if ( (incoming_byte & 0xA2) == 0xA2 ) { // synchronization byte
			index=0;
			packet[0]=incoming_byte;
		} 
		else if ((index == 0) && ((incoming_byte & 0x80) == 0x80)) { // 2nd syncro byte
			index=1;
			packet[1]=incoming_byte;
		} 
		else {
			index++;
			packet[index]=incoming_byte;
		}
		if (index >= 5 ) {
			LC1_status = (packet[2] & B00011100) >> 2;
			
			// Get status report from controller
			LC1_AFRs = (packet[2] & B00000001) << 7;
			LC1_AFRs += packet[3] & B01111111;
			
			// Combine lambda values from 2 
			LC1_lambda = packet[4] & B00111111;
			LC1_lambda = LC1_lambda << 7;
			LC1_lambda += (packet[5] & B01111111);
			
			// Display status
			switch (LC1_status) {
			case 0: LC1_AFR = LC1_lambda; break; // reporting lambda
			case 1: LC1_AFR = ((LC1_lambda + 500) * LC1_AFRs ) / 1000; break;
			case 2: Serial.println("Free Air Calibration in progress..."); LC1_AFR = 14.7; break;
			case 3: Serial.println("Need Calibration"); LC1_AFR = 14.7; break;
			case 4: Serial.println("Heater Warming"); LC1_AFR = 14.7; break;
			case 5: Serial.println("Heater Calibration"); LC1_AFR = 14.7; break;
			case 6: Serial.println("Error Code"); LC1_AFR = 14.7; break;
			case 7: Serial.println("Reserved"); LC1_AFR = 14.7; break;
			}
			
			// If no error, reset packet buffer and index
			if (LC1_status == 0 ) {
				LC1_AFR=((LC1_lambda + 500) * LC1_AFRs ) / 1000;
				Serial.print("lambda ");
				Serial.println(LC1_lambda);
				Serial.print("AFR ");
				Serial.println(LC1_AFR);
				index=0;
				for (byte i; i <=6 ; i++) packet[i]=0;
			}
		}
	}
}

humjaba
Helpful MS/Extra'er
Posts: 35
Joined: Fri Sep 26, 2014 11:17 am

Re: Wideband O2 to CAN communicator

Post by humjaba »

Got the CAN stuff wired up last night and I'd just like to make the comment that soldering into a DB37 connector is a massive pain in the ass, especially when your solder tip won't fit between the pins.
Once I got it hooked up to the connector and my can transceiver, I flipped it on and swiped some of xattiracer's code to read and interpret the CAN messages. Of course it didn't work right away (nothing does)... It was apparently receiving messages, but the CAN ID's were a all over the place, and none were 4 digit numbers around 1520. So I'm not sure quite what to do with that just yet.

Is there anything I need to do in Tunerstudio apart from enabling CAN Broadcasting?
grom_e30
Super MS/Extra'er
Posts: 4461
Joined: Thu Mar 08, 2012 12:44 pm
Location: UK

Re: Wideband O2 to CAN communicator

Post by grom_e30 »

nope just turn it on. were you trying to view the can id's in decimal numbers or hex?
1990 bmw 320i daily driver with m20b25 ms3 sequential fuel, 380cc injectors, d585 coil near plug, home made cam sync, launch control, fan control, vss, homebrew egt logging what's next????
humjaba
Helpful MS/Extra'er
Posts: 35
Joined: Fri Sep 26, 2014 11:17 am

Re: Wideband O2 to CAN communicator

Post by humjaba »

Code: Select all

			Serial.write("ID: ");
			Serial.println(rxmsg.id);
They were all numbers, so I'm guessing decimal. I didn't get a copy of the serial monitor before I went to bed for the night but I'll try to grab the ID's tonight. There were 2 or 3 different ID's that repeated. I didn't have time to write a function to dump what the messages were.
Post Reply