CAN integration with a DIY device

This forum is for the discussion of other projects on Megasquirt/Microsquirt hardware that don't fit into any of the other forums

Moderators: jsmcortina, muythaibxr

Post Reply
jsdevel
Experienced MS/Extra'er
Posts: 318
Joined: Mon May 23, 2016 3:41 pm
Location: Phoenix, AZ

CAN integration with a DIY device

Post by jsdevel »

Anyone have a forum link or documentation on integrating a custom CAN device with MS? Say I wanted to read what the CLT level was in MS, how would I go about doing that in general? Does MS broadcast a message with all variables available? Or would my CAN device need to query MS for it?
jsmcortina
Site Admin
Posts: 39615
Joined: Mon May 03, 2004 1:34 am
Location: Birmingham, UK
Contact:

Re: CAN integration with a DIY device

Post by jsmcortina »

Enabling the "Dash broadcast" on the Megasquirt and then having your device listen for that broadcast is strongly recommended.

The CAN protocol documents are available from the "Manuals" link.

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".
jsdevel
Experienced MS/Extra'er
Posts: 318
Joined: Mon May 23, 2016 3:41 pm
Location: Phoenix, AZ

Re: CAN integration with a DIY device

Post by jsdevel »

Awesome! Thanks James! I'll post back here if I have more questions.
jsdevel
Experienced MS/Extra'er
Posts: 318
Joined: Mon May 23, 2016 3:41 pm
Location: Phoenix, AZ

Re: CAN integration with a DIY device

Post by jsdevel »

Well, 2 years later! :lol:

I finally got around to having the time to working with the CAN bus more, and got an arduino uno communicating with my MS2.

Here's the message of interest:

Code: Select all

5E8 8 3 C6 19 9C 3 CD 2 7F
That equates to this message:
field-list.png
field-list.png (28.6 KiB) Viewed 3001 times
So if I'm interested in the coolant field, I concatenate data 4 and 5 to construct the hex number 3cd, which in decimal is 973, which when divided by 10 is 97.3! The coolant level I see in msdroid! success!

Now I just need some code snippets in C to concat 2 hex numbers. Anyone have any?
jsdevel
Experienced MS/Extra'er
Posts: 318
Joined: Mon May 23, 2016 3:41 pm
Location: Phoenix, AZ

Re: CAN integration with a DIY device

Post by jsdevel »

Sorry, I think I've answered my own question. If anyone has a better way, please let me know:

Code: Select all

Serial.print(((canMsg.data[4]<<8) + canMsg.data[5])/10);
jsmcortina
Site Admin
Posts: 39615
Joined: Mon May 03, 2004 1:34 am
Location: Birmingham, UK
Contact:

Re: CAN integration with a DIY device

Post by jsmcortina »

jsdevel wrote:Sorry, I think I've answered my own question. If anyone has a better way, please let me know:

Code: Select all

Serial.print(((canMsg.data[4]<<8) + canMsg.data[5])/10);
You might want to use a bitwise OR instead of addition and perhaps cast to 16bit. In C language, I would be doing this:

Code: Select all

uin16_t value;
value = (((uint16_t)canMsg.data[4] << 8) | (uint16_t)canMsg.data[5]) / 10;
The casts may look messy, but otherwise you are relying on the compiler to make an implicit type conversion which does not always work as expected!

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".
Manu
Master MS/Extra'er
Posts: 723
Joined: Mon Feb 15, 2010 4:57 am
Location: Alès - France
Contact:

Re: CAN integration with a DIY device

Post by Manu »

With arduino you also can use « word », which use cast too

Code: Select all

uint16_t value = int (word(canMsg.data[4],canMsg.data[5]))
https://www.arduino.cc/reference/en/lan ... /wordcast/
I can supply, repair or upgrade Megasquirts in FRANCE.

Image
https://www.megasquirt.fr
racingmini_mtl
Super MS/Extra'er
Posts: 9130
Joined: Sun May 02, 2004 6:51 am
Location: Quebec, Canada
Contact:

Re: CAN integration with a DIY device

Post by racingmini_mtl »

Manu wrote:With arduino you also can use « word », which use cast too

Code: Select all

uint16_t value = int (word(canMsg.data[4],canMsg.data[5]))
https://www.arduino.cc/reference/en/lan ... /wordcast/
Is 'word' always 16-bit regardless of the CPU used? (The link seems to imply this is the case) Using uint16_t removes any ambiguity about the number of bits in the cast.

Using 'word' as a 16-bit value on a 32-bit CPU (or an 8-bit CPU) is not what I would call a good practice. That may be obvious for most Arduino developer but to me that's a very platform-dependent term. But it may be that I'm a too ancient developer (started programming more than 40 years ago).

Jean
jbperf.com Main site . . . . . . . . . . . . . . . . . . . . . . jbperf.com Forum
Image
Manu
Master MS/Extra'er
Posts: 723
Joined: Mon Feb 15, 2010 4:57 am
Location: Alès - France
Contact:

Re: CAN integration with a DIY device

Post by Manu »

I think that « word » is an arduino command (only)...
I can supply, repair or upgrade Megasquirts in FRANCE.

Image
https://www.megasquirt.fr
jsdevel
Experienced MS/Extra'er
Posts: 318
Joined: Mon May 23, 2016 3:41 pm
Location: Phoenix, AZ

Re: CAN integration with a DIY device

Post by jsdevel »

jsmcortina wrote: The casts may look messy, but otherwise you are relying on the compiler to make an implicit type conversion which does not always work as expected!
Thanks James! How would you go about separating out the decimal portion? I've noticed that it's dropped after division.
Manu wrote:I think that « word » is an arduino command (only)...
Thanks Manu! I'll definitely check that out.

Right now I'm working on displaying the data on some OLED displays :D
Manu
Master MS/Extra'er
Posts: 723
Joined: Mon Feb 15, 2010 4:57 am
Location: Alès - France
Contact:

Re: CAN integration with a DIY device

Post by Manu »

Manu wrote:

Code: Select all

uint16_t value = int (word(canMsg.data[4],canMsg.data[5]))
I've made a mistake in the command. This is the right one, for example it get RPM from MS CAN protocol :

Code: Select all

REG = (int)(word(frame.buf[6], frame.buf[7]));
I can supply, repair or upgrade Megasquirts in FRANCE.

Image
https://www.megasquirt.fr
jsmcortina
Site Admin
Posts: 39615
Joined: Mon May 03, 2004 1:34 am
Location: Birmingham, UK
Contact:

Re: CAN integration with a DIY device

Post by jsmcortina »

jsdevel wrote:
jsmcortina wrote: The casts may look messy, but otherwise you are relying on the compiler to make an implicit type conversion which does not always work as expected!
Thanks James! How would you go about separating out the decimal portion? I've noticed that it's dropped after division.

It depends what you want to do with it. Inside the MS2/3 firmware we use the raw number and the code "knows" that it is *10.
You _could_ use floating point, but depending on the capabilities of your microcontroller that might consume a ton of cycles. Don't use floating point unless the MCU has a floating point module.
The MS2/MS3 MCUs do not have hardware floating point capability so we use solely integer maths.

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".
jsdevel
Experienced MS/Extra'er
Posts: 318
Joined: Mon May 23, 2016 3:41 pm
Location: Phoenix, AZ

Re: CAN integration with a DIY device

Post by jsdevel »

I finally got it working!!! :yeah!:

Created a video for proof: https://www.youtube.com/watch?v=BliEJyVSeB4

Currently using 2 OLED displays. I ordered a multiplexer on aliexpress and plan to have a total of 4 - 6 displays :D

Thank you everyone for your help and support over the years!
Post Reply