Optimum CANBUS message structure for Megasquirt MS3

I have a MS3 bench tester aftermarket ECU with working CANBUS comms via a Mega2560/MCP2515 module.

My question is about the Arduino CANBUS messaging structure in this code, and if this the optimum coding for this type of project. I did not code this, but reused/tweaked what I found from another source. I am no coder, and I would like to get some opinions from folks who are.

This code works, and I can see the ADC output from both temp sensors and the pot in TunerStudio (ECU/tuning software for Megasquirt). Eventually, I'd like to get PWM for relay control and triggers for warning lights set up.

The library used is GitHub - autowp/arduino-mcp2515: Arduino MCP2515 CAN interface library

Thanks in advance..


// this feeds data into MS3 Mgasquirt over CAN bus
// pot with +5, GND, signal to A2
// temp sensors on A0/1


#include <SPI.h>          //Library for using SPI Communication 
#include <mcp2515.h>      //Library for using CAN Communication

struct can_frame canMsg1;
struct can_frame canMsg2;
struct can_frame canMsg3;

MCP2515 mcp2515(53);

int sensor01=A0;  //temp sensor
int sensor02=A1; // temp sensor
int sensor03=A2; //Pot
int s1, a, b;
int s2, c, d;
int s3, e, f;

void setup() 
{
  //while (!Serial);
  Serial.begin(9600);
  
  SPI.begin();               //Begins SPI communication
  
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS,MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
  mcp2515.setNormalMode();

}

void loop() 
{
  s1=analogRead(sensor01);  
  s2=analogRead(sensor02);
  s3=analogRead(sensor03);  

  canMsg1.data[0]=s1/256;
  canMsg2.data[0]=s2/256;
  canMsg3.data[0]=s3/256;
  b=s1%256;  
  d=s2%256; 
  f=s3; //pot pin
  //a=highByte(s1); alternate way of doing it
  //b=lowByte(s1);
  
  canMsg1.can_id  = 0x001;           //CAN id as 0x001 (leading zeros matter on this field)
  canMsg1.can_dlc = 8;               //CAN data length as 8
  //canMsg.data[0] = a;               
  canMsg1.data[1] = b;               
  canMsg1.data[2] = 0x00;            
  canMsg1.data[3] = 0x00;
  canMsg1.data[4] = 0x00;
  canMsg1.data[5] = 0x00;
  canMsg1.data[6] = 0x00;
  canMsg1.data[7] = 0x00;
  
  canMsg2.can_id  = 0x002;           //CAN id as 0x002 (leading zeros matter on this field)
  canMsg2.can_dlc = 8;               //CAN data length as 8
  //canMsg.data[0] = c;               
  canMsg2.data[1] = d;               
  canMsg2.data[2] = 0x00;            
  canMsg2.data[3] = 0x00;
  canMsg2.data[4] = 0x00;
  canMsg2.data[5] = 0x00;
  canMsg2.data[6] = 0x00;
  canMsg2.data[7] = 0x00;

canMsg3.can_id  = 0x003;           //CAN id as 0x003 (leading zeros matter on this field)
  canMsg3.can_dlc = 8;               //CAN data length as 8
  //canMsg.data[0] = e;               
  canMsg3.data[1] = f;               
  canMsg3.data[2] = 0x00;            
  canMsg3.data[3] = 0x00;
  canMsg3.data[4] = 0x00;
  canMsg3.data[5] = 0x00;
  canMsg3.data[6] = 0x00;
  canMsg3.data[7] = 0x00;
   
  
  mcp2515.sendMessage(&canMsg1);     //Sends the CAN message
  mcp2515.sendMessage(&canMsg2); 
  mcp2515.sendMessage(&canMsg3); 
  
  delay(100);
}



This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.