MS2 Can output to Arduino

This is a forum for discussing the development and testing of alpha MS2/Extra code. Documentation
(Runs on MS2 and Microsquirt)

Moderators: jsmcortina, muythaibxr

Post Reply
kevin.debosscher
MS/Extra Newbie
Posts: 8
Joined: Thu Aug 21, 2014 3:16 pm

MS2 Can output to Arduino

Post by kevin.debosscher »

Hey Dudes!

Been searching for weeks to establish a simple communication between an arduino and MS2.But can't get it to work :/
Anybody that has a basic sketch?

All i want is to read simple data like rpm,clt, etc basic stuff

I want to stay on the 325 firmware sow can request messages comes in iirc?
Or are there other,easier solutions?

Thanks!!

Code: Select all

#include <SPI.h>
#include "mcp_can.h"



//raw readings
int seconds = 0;
int pw1 = 0;
int pw2 = 0;
int rpm = 0;

int adv = 0;
float afrtgt1 = 0.0;

float baro = 0;
float MAP = 0;
float mat = 0;
float clt = 0;

int tps = 0;
float bat = 1;
float afr1 = 1;
int cel_status = 0;

unsigned long previousMillis = 0;


unsigned char Flag_Recv = 0;
unsigned char len = 0;
unsigned char buf[8];
INT32U canId = 0x000;
char str[20];


MCP_CAN CAN(10);                                            // Set CS to pin 10

void setup()
{
    Serial.begin(115200);
    
   ;
    CAN.begin(CAN_500KBPS);                       // init can bus : baudrate = 500k
    CAN.init_Mask(0,0,0x7ff);                      
    CAN.init_Mask(1,0,0x7ff);                     
    //CAN.init_Filt(0,0,0x5f0);                      
    //CAN.init_Filt(1,0,0x5f2);                      
    //CAN.init_Filt(2,0,0x5f3);                      
    //CAN.init_Filt(3,0,0x0);                      
    //CAN.init_Filt(4,0,0x0);                      
    //CAN.init_Filt(5,0,0x0);                     

}


void loop() {
  canRead();
  //rpmSmoothing();
  canDisplay();
}   

void canRead() {
  if(CAN_MSGAVAIL == CAN.checkReceive()) {         // check if data coming
    CAN.readMsgBuf(&len, buf);    // read data,  len: data length, buf: data buf
    switch (CAN.getCanId()) {
    case 0x5f0:
      seconds = word(buf[0] , buf[1]);
      pw1 = word(buf[2] , buf[3]);
      pw2 = word(buf[4] , buf[5]);
      rpm = word(buf[6] , buf[7]);
      break; 
    
    case 0x5f1:  // Group 1 
      adv = word(buf[0] , buf[1]);
      afrtgt1 = buf[4];
      //mat = word(buf[4] , buf[5]);
      //clt = word(buf[6] , buf[7]);
      break;
      
    case 0x5f2:  // Group 2 
      baro = word(buf[0] , buf[1]);
      MAP = word(buf[2] , buf[3]);
      mat = word(buf[4] , buf[5]);
      clt = word(buf[6] , buf[7]);
      break;
    
    case 0x5f3:   // Group 3 
      tps = ((buf[0] * 256) + buf[1]);
      bat = ((buf[2] * 256) + buf[3]);
      afr1 = ((buf[4] * 256) + buf[5]);
      //afr2 = (((buf[6] *256) + buf[7]); 
      break;
    }    
  }        
}


void canDisplay() {

  const int interval = 50;
  unsigned long currentMillis = millis();
  //refresh rate
  if (currentMillis - previousMillis >= interval) { //if time elapsed is greater than the signal interval
    previousMillis = currentMillis; //then reset time


  

    Serial.print(" Snds ");
    Serial.print(seconds);
    Serial.print(" RPM ");
    Serial.print(rpm);
    Serial.print(" PW1 ");
    Serial.print(pw1/1000, 2);
    Serial.print(" ADV ");
    Serial.print(adv/10, 1);
    Serial.print(" AFRtgt ");
    Serial.print(afrtgt1/10, 1);
    Serial.print(" AFR ");
    Serial.print(afr1/10, 1);
    Serial.print(" MAP ");
    Serial.print(MAP/10, 1);
    Serial.print(" CLT ");
    Serial.print(clt/10, 1);
    Serial.print(" TPS ");
    Serial.print(tps/10);
    Serial.println(" ");
  
  }
jsmcortina
Site Admin
Posts: 39618
Joined: Mon May 03, 2004 1:34 am
Location: Birmingham, UK
Contact:

Re: MS2 Can output to Arduino

Post by jsmcortina »

I didn't read your code in detail.
Two options.
A. Use the current firmware and enable CAN broadcasting which is simple to receive on third party devices.
B. Implement Megasquirt CAN.

Specifications for both are available on the Manuals page.

James
I can repair or upgrade Megasquirts in UK. http://www.jamesmurrayengineering.co.uk

My Success story: http://www.msextra.com/forums/viewtopic ... 04&t=34277
MSEXTRA documentation at: http://www.msextra.com/doc/index.html
New users, please read the "Forum Help Page".
winstonusmc
Experienced MS/Extra'er
Posts: 204
Joined: Sun Jul 31, 2011 7:45 am

Re: MS2 Can output to Arduino

Post by winstonusmc »

Thought this looked familiar when I read your post. Post up some info on your hardware setup, like what CAN shield you are using and how you are plugged into the arduino. I used a cheap chinese CAN transceiver that I had to modify to get it to work.

You need to start off with a simple can receive sketch that allows you to see everything without any type of filter. This will allow you to see the raw traffic without trying to tie it to any variables. Use the examples from the library for the Sparkfun CAN shield, I am pretty sure that is what I used. The receive example is pretty simple.

The sketch you posted up was an evolution of that process. Unfortunately with Ardiuno, there is too many different setups you can have to simply copy someone else's sketch. You need to understand the process within the sketch to adapt it to you own setup. Its kinda like Megasquirt itself in that aspect. Not that I am upset in any way about copying, thats why I post it up, to help people learn.
Nissan Skyline R34 RB26DETT ran MS3/MS3X w/ factory Hitachi CAS (sold)
Nissan Silvia S14 RB25DE ITB/NA ran MS3/MS3X w/ factory Mitsubishi CAS (disassembled)
Datsun 240z RB25DE ITB/NA with MS3/MS3X
kevin.debosscher
MS/Extra Newbie
Posts: 8
Joined: Thu Aug 21, 2014 3:16 pm

Re: MS2 Can output to Arduino

Post by kevin.debosscher »

Arduino Uno with a MCP2515 shield

Image

MS2 with 325firm
winstonusmc
Experienced MS/Extra'er
Posts: 204
Joined: Sun Jul 31, 2011 7:45 am

Re: MS2 Can output to Arduino

Post by winstonusmc »

If you are using an Uno or similar, you may have to change the crystal to 16mhz crystal to get it to work. Just check the crystal on the arduino. The sketch should work just fine then as its the same CAN device I use. It works with my UNO, Mega, and pro mini, all at 16mhz. Just start with the "receive check" example to make sure everything works first.
Nissan Skyline R34 RB26DETT ran MS3/MS3X w/ factory Hitachi CAS (sold)
Nissan Silvia S14 RB25DE ITB/NA ran MS3/MS3X w/ factory Mitsubishi CAS (disassembled)
Datsun 240z RB25DE ITB/NA with MS3/MS3X
Alfagta
Experienced MS/Extra'er
Posts: 239
Joined: Wed Aug 21, 2013 2:54 am

Re: MS2 Can output to Arduino

Post by Alfagta »

I have tested the com to an MS 2 and 3 with this shield and arduino with 20Mhz.

No problem...
Alfa 156 GTA
Alfa 75 Race Car MS III -- alway latest FW
Alfa 164 V6 Turbo
Alfa 155 2,5TD RIP
Test MS II --always latest FW
Tunerstudio V2.6.11 Registered
MegaLog Viewer V3.4.04 Registered
grom_e30
Super MS/Extra'er
Posts: 4461
Joined: Thu Mar 08, 2012 12:44 pm
Location: UK

Re: MS2 Can output to Arduino

Post by grom_e30 »

i have the exact same with 8mhz crystal i had to set can bus speed to 1000kbps to get it to work.
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????
Alfagta
Experienced MS/Extra'er
Posts: 239
Joined: Wed Aug 21, 2013 2:54 am

Re: MS2 Can output to Arduino

Post by Alfagta »

I think the crystal on the shield has nothiung to do with the speed on the arduino.
The crystal on the shield is for the canbus chip.
The arduino talk with spi with the canbus chip. And you can setup a speed for the spi....

I have used µc and the mcp canbus chip with different crystal without any problem.

Some of the arduino lib's for the mcp are not 100% working. I tried some of them. But then i create one myself..
Alfa 156 GTA
Alfa 75 Race Car MS III -- alway latest FW
Alfa 164 V6 Turbo
Alfa 155 2,5TD RIP
Test MS II --always latest FW
Tunerstudio V2.6.11 Registered
MegaLog Viewer V3.4.04 Registered
grom_e30
Super MS/Extra'er
Posts: 4461
Joined: Thu Mar 08, 2012 12:44 pm
Location: UK

Re: MS2 Can output to Arduino

Post by grom_e30 »

if i put a 16mhz crystal in the can bus it connects and works if set to 500kbps, with the 8mhz crystal the can speed has to be set to 1000kbps for it to work the exact same ebay china can bus module.
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????
Alfagta
Experienced MS/Extra'er
Posts: 239
Joined: Wed Aug 21, 2013 2:54 am

Re: MS2 Can output to Arduino

Post by Alfagta »

Maybe the canbus timing registers setup where not correct.
Alfa 156 GTA
Alfa 75 Race Car MS III -- alway latest FW
Alfa 164 V6 Turbo
Alfa 155 2,5TD RIP
Test MS II --always latest FW
Tunerstudio V2.6.11 Registered
MegaLog Viewer V3.4.04 Registered
kevin.debosscher
MS/Extra Newbie
Posts: 8
Joined: Thu Aug 21, 2014 3:16 pm

Re: MS2 Can output to Arduino

Post by kevin.debosscher »

Just updated to the newer broadcasting firm.
I finaly receive some can data but they are in this format:

Code: Select all

CAN ID: 243
data len = 8
4	119	0	137	0	74	0	74	
Now I'm stuck in how to convert to logical values...
jsmcortina
Site Admin
Posts: 39618
Joined: Mon May 03, 2004 1:34 am
Location: Birmingham, UK
Contact:

Re: MS2 Can output to Arduino

Post by jsmcortina »

What data were you trying to send?

James
I can repair or upgrade Megasquirts in UK. http://www.jamesmurrayengineering.co.uk

My Success story: http://www.msextra.com/forums/viewtopic ... 04&t=34277
MSEXTRA documentation at: http://www.msextra.com/doc/index.html
New users, please read the "Forum Help Page".
kevin.debosscher
MS/Extra Newbie
Posts: 8
Joined: Thu Aug 21, 2014 3:16 pm

Re: MS2 Can output to Arduino

Post by kevin.debosscher »

Hello James,

Under CAN realtime data broadcasting:
02:baro,map,mat,clt

These are 11bit?
jsmcortina
Site Admin
Posts: 39618
Joined: Mon May 03, 2004 1:34 am
Location: Birmingham, UK
Contact:

Re: MS2 Can output to Arduino

Post by jsmcortina »

You need to read the upper byte of the CANid too. (11 bits don't fit into 8 bits !)

Did you read the broadcasting PDF where it explains how the data is laid out?

James
I can repair or upgrade Megasquirts in UK. http://www.jamesmurrayengineering.co.uk

My Success story: http://www.msextra.com/forums/viewtopic ... 04&t=34277
MSEXTRA documentation at: http://www.msextra.com/doc/index.html
New users, please read the "Forum Help Page".
kevin.debosscher
MS/Extra Newbie
Posts: 8
Joined: Thu Aug 21, 2014 3:16 pm

Re: MS2 Can output to Arduino

Post by kevin.debosscher »

I've been reading so many things... :RTFM:
Can you post a link with the correct info James?

Really a noob in canbus stuff
jsmcortina
Site Admin
Posts: 39618
Joined: Mon May 03, 2004 1:34 am
Location: Birmingham, UK
Contact:

Re: MS2 Can output to Arduino

Post by jsmcortina »

Follow the "Manuals" link above and scroll down to the end.

James
I can repair or upgrade Megasquirts in UK. http://www.jamesmurrayengineering.co.uk

My Success story: http://www.msextra.com/forums/viewtopic ... 04&t=34277
MSEXTRA documentation at: http://www.msextra.com/doc/index.html
New users, please read the "Forum Help Page".
kevin.debosscher
MS/Extra Newbie
Posts: 8
Joined: Thu Aug 21, 2014 3:16 pm

Re: MS2 Can output to Arduino

Post by kevin.debosscher »

I really don't see it...
Need to sleep!

If somebody spot the error in the codes,:

Code: Select all

#include <SPI.h>
#include "mcp_can.h"

// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
unsigned char len = 0;
unsigned char buf[8];
char str[20];
INT8U Flag_Recv = 0;

MCP_CAN CAN(SPI_CS_PIN);                                    // Set CS pin

void setup()
{
    Serial.begin(115200);

START_INIT:

    if(CAN_OK == CAN.begin(CAN_500KBPS))                   // init can bus : baudrate = 500k
    {
        Serial.println("CAN BUS Shield init ok!");
    }
    else
    {
        Serial.println("CAN BUS Shield init fail");
        Serial.println("Init CAN BUS Shield again");
        delay(100);
        goto START_INIT;
    }
   
}

unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};

void loop()
{
  // send data:  id = 0x00, stock frame, data len = 8, stmp: data buf
    CAN.sendMsgBuf(0x0c, 0, 8, stmp);
    delay(10);                       // send data per 100ms

      CAN.readMsgBuf(&len, buf);    // read data,  len: data length, buf: data buf
      unsigned char canId = CAN.getCanId();
      //Print data to the serial console 
      //and the LCD display
      Serial.println("CAN_BUS DATA!");
      Serial.print("CAN ID: ");
      Serial.println(canId);
      Serial.print("data len = ");
      Serial.println(len);
      //This loops through each byte of data and prints it
      
      for(int i = 0; i<len; i++)    // print the data
      {
          Serial.print(buf[i]);
          Serial.print("\t"); 
      }
          Serial.println();
          delay(200);
}
Alfagta
Experienced MS/Extra'er
Posts: 239
Joined: Wed Aug 21, 2013 2:54 am

Re: MS2 Can output to Arduino

Post by Alfagta »

What are you trying with this code.

You send a Message out on the bus, wait 10ms and then you read a message??
You try to read the message you have sendet?

Are you connected to the MS Canbus?

From which source do you have the mcp lib?

BR
Alfa 156 GTA
Alfa 75 Race Car MS III -- alway latest FW
Alfa 164 V6 Turbo
Alfa 155 2,5TD RIP
Test MS II --always latest FW
Tunerstudio V2.6.11 Registered
MegaLog Viewer V3.4.04 Registered
jsmcortina
Site Admin
Posts: 39618
Joined: Mon May 03, 2004 1:34 am
Location: Birmingham, UK
Contact:

Re: MS2 Can output to Arduino

Post by jsmcortina »

kevin.debosscher wrote:I really don't see it...
What don't you see?
- the PDF that describes the CAN protocol?
- the byte packing explanation?

James
I can repair or upgrade Megasquirts in UK. http://www.jamesmurrayengineering.co.uk

My Success story: http://www.msextra.com/forums/viewtopic ... 04&t=34277
MSEXTRA documentation at: http://www.msextra.com/doc/index.html
New users, please read the "Forum Help Page".
Post Reply