CAN integration with a DIY device
Moderators: jsmcortina, muythaibxr
CAN integration with a DIY device
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?
-
- Site Admin
- Posts: 39619
- Joined: Mon May 03, 2004 1:34 am
- Location: Birmingham, UK
- Contact:
Re: CAN integration with a DIY device
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
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".
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".
Re: CAN integration with a DIY device
Awesome! Thanks James! I'll post back here if I have more questions.
Re: CAN integration with a DIY device
Well, 2 years later!
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:
That equates to this message:
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?
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
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?
Re: CAN integration with a DIY device
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);
-
- Site Admin
- Posts: 39619
- Joined: Mon May 03, 2004 1:34 am
- Location: Birmingham, UK
- Contact:
Re: CAN integration with a DIY device
You might want to use a bitwise OR instead of addition and perhaps cast to 16bit. In C language, I would be doing this: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);
Code: Select all
uin16_t value;
value = (((uint16_t)canMsg.data[4] << 8) | (uint16_t)canMsg.data[5]) / 10;
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".
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".
-
- 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
With arduino you also can use « word », which use cast too
https://www.arduino.cc/reference/en/lan ... /wordcast/
Code: Select all
uint16_t value = int (word(canMsg.data[4],canMsg.data[5]))
-
- 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
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.Manu wrote:With arduino you also can use « word », which use cast too
https://www.arduino.cc/reference/en/lan ... /wordcast/Code: Select all
uint16_t value = int (word(canMsg.data[4],canMsg.data[5]))
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
-
- 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
I think that « word » is an arduino command (only)...
Re: CAN integration with a DIY device
Thanks James! How would you go about separating out the decimal portion? I've noticed that it's dropped after division.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 Manu! I'll definitely check that out.Manu wrote:I think that « word » is an arduino command (only)...
Right now I'm working on displaying the data on some OLED displays
-
- 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
I've made a mistake in the command. This is the right one, for example it get RPM from MS CAN protocol :Manu wrote:Code: Select all
uint16_t value = int (word(canMsg.data[4],canMsg.data[5]))
Code: Select all
REG = (int)(word(frame.buf[6], frame.buf[7]));
-
- Site Admin
- Posts: 39619
- Joined: Mon May 03, 2004 1:34 am
- Location: Birmingham, UK
- Contact:
Re: CAN integration with a DIY device
Thanks James! How would you go about separating out the decimal portion? I've noticed that it's dropped after division.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!
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".
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".
Re: CAN integration with a DIY device
I finally got it working!!!
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
Thank you everyone for your help and support over the years!
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
Thank you everyone for your help and support over the years!