0

Timing issue between two CAN message
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Timing issue between two CAN message

by dutta_gaurav on Wed Jun 27, 2018 11:56 am

So i am using two CAN shield stacked on one another. i am trying to receive the same message with the same identifier but on different CAN networks. I want to receive both of them together, compare and print the difference.
The problem i am facing is both the can messages are received at different times so at one point of time it shows the difference while other time it receives a 0 from the other can message and shows a difference of negative the first value (Voltage1-voltage2).
there is a timing issue. how do i solve this so i i keep the previous value and only compare when both the messages have been received?
Thank you again.

Code: Select all | TOGGLE FULL SIZE
#include <mcp_can.h>
#include <SPI.h>
static int Voltage1;
static int Voltage2;


const int SPI_CS_PIN1 = 9; // select pin 9
const int SPI_CS_PIN2 = 10 ; // select pin 10


MCP_CAN CAN1 (SPI_CS_PIN1);
MCP_CAN CAN2 (SPI_CS_PIN2);                                     // Set CS pin

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

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


unsigned char data3[8] = {04,00,00,00,00,00,00,00}; // Wake up can

void loop()
{
 
    unsigned char len1 = 0;// clear flag  unsigned char len1 = 0;
    unsigned char buf1[8];
    unsigned char len2 = 0;
    unsigned char buf2[8];
   
    CAN1.sendMsgBuf(0x700,0,8,data3); // Send pack 1 to communicate
    CAN2.sendMsgBuf(0x700,0,8,data3); // Send pack 2 to communicate
   
   
   
    if(CAN_MSGAVAIL == CAN1.checkReceive())         // check if data coming
    {
       CAN1.readMsgBuf(&len1, buf1);
       unsigned int canId1 = CAN1.getCanId();

      if (canId1==768)
          {
           
                    unsigned int x1= buf1[2]; //Select bit
                    unsigned int y1= buf1[3];
                    unsigned int z1= x1*256 + y1; //concatenate
                    Voltage1 = z1*0.002; //Convert obtained decimal value to voltage 
                    Serial.println(Voltage1);     
          }
         
     }
      if(CAN_MSGAVAIL == CAN2.checkReceive())
      {
          CAN2.readMsgBuf(&len2, buf2); 
          unsigned int canId2 = CAN2.getCanId();
         
      if (canId2==768)
      {
                    unsigned int x2= buf2[2]; //Select bit
                    unsigned int y2= buf2[3];
                    unsigned int z2= x2*256 + y2; //concatenate
                    Voltage2 = z2*0.002; //Convert obtained decimal value to voltage 
                    Serial.println(Voltage2);       
          }
         
      }
       int difference=Voltage1-Voltage2;
      Serial.println(difference);

dutta_gaurav
 
Posts: 7
Joined: Thu Jun 21, 2018 11:11 am

Re: Timing issue between two CAN message

by adafruit_support_bill on Wed Jun 27, 2018 12:29 pm

We do not make a CAN bus shield. You should contact the manufacturer of your shields for assistance.

adafruit_support_bill
 
Posts: 69809
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.