Libelium is offering new technical support channels, to contact with de Technical Support team to solve any issue our doubt please contact with your sales agent or visit our contact site



Post a new topicPost a reply Page 1 of 1   [ 4 posts ]
Author Message
 Post subject: Timing issue between two CAN message
PostPosted: Wed Jun 27, 2018 5:31 pm 

Joined: Wed Jun 27, 2018 5:29 pm
Posts: 2
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:
#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);


Top
 Profile  
 
 Post subject: Re: Timing issue between two CAN message
PostPosted: Thu Jun 28, 2018 8:59 am 

Joined: Mon Sep 28, 2009 12:06 pm
Posts: 11366
Hi,

You can try to include two flags that will be set to 1 when a packets is received and when the two flags will be 1, compare the packets received.

Code:
if (received1){
  flag1=1;
}
if (received2){
  flag2=1;
}
if(flag1 && flag2){
  comparePackets();
}


Regards


Top
 Profile  
 
 Post subject: Re: Timing issue between two CAN message
PostPosted: Thu Jun 28, 2018 3:08 pm 

Joined: Wed Jun 27, 2018 5:29 pm
Posts: 2
Thanks


Top
 Profile  
 
 Post subject: Re: Timing issue between two CAN message
PostPosted: Wed Jul 11, 2018 8:00 am 

Joined: Mon Sep 28, 2009 12:06 pm
Posts: 11366
You're welcome!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post a new topicPost a reply Page 1 of 1   [ 4 posts ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Libelium theme based on 610nm Style by Daniel St. Jules of http://www.gamexe.net


© Libelium Comunicaciones Distribuidas S.L. | Terms of use.