Go Down

Topic: MCP2515 buffer problem? (Read 167 times) previous topic - next topic

silviocross

Hi all,

I'm working on a small dashboard to be connected on motorcycles CAN data bus.
The hardware is based on the Sparkfun shield (MCP2251/MCP2515), but shrunk down to a single SMD concept.





It should work in a passive way, reading the cyclic messages on the bus and writing some values on a 128x64 display.
CAN messages are sent from the motorcycle ECU every 100ms.
I am at the moment only interested on two messages IDs (there are ~50 IDs in the bus).



The initial test code is working correctly when I simulate the bus with only the messages I'm interested on, but when I connect it to the real bus, data is updated randomly with delay varying from 20 seconds to 10 minutes.
It looks like a buffer congestion, I tried applying filters but the problem is still there...

Can you maybe give me some suggestions on how to solve the problem??
Here's part of the code:

Code: [Select]


#include <SPI.h>
#include "mcp_can.h"   // sparkfun library


// include the library header
// no font headers have to be included
#include <openGLCD.h>

#define SPI_CS_PIN 10

unsigned int canId = 0x00;

unsigned char len = 0;
unsigned char buf[8];
unsigned char temp;

int data1 = 0;  // initializing values
int voltage = 0;
float voltagemin = 0;
float voltagemax = 0;
int temperaturemin = 0;
int temperaturemax = 0;
int pof = 0;

MCP_CAN CAN(SPI_CS_PIN);                                    // Set CS pin

void setup()
{
  // Initialize the GLCD
  GLCD.Init();

  Serial.begin(115200);

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

  // splash screen
  splash();

    CAN.init_Mask(0, 0, 0xFFC0000);
    CAN.init_Mask(1, 0, 0xFFC0000);
   
    CAN.init_Filt(0, 0, 0x8000000);   //0x200
    CAN.init_Filt(1, 0, 0x8140000);   //0x205

}

void loop()
{

if(CAN_MSGAVAIL == CAN.checkReceive()) {           // check if data coming

    CAN.readMsgBuf(&len, buf);    // read data,  len: data length, buf: data buf
    canId = CAN.getCanId();       // get CAN ID


      if(canId == 0x200){
        Serial.print("get data from ID: 0x");
        Serial.println(canId,HEX);
        temp = buf[4];
        data1 = (int)temp;
        temp = buf[3];
        pof = (int)temp;
      }
      else if(canId == 0x205){
        Serial.print("get data from ID: 0x");
        Serial.println(canId,HEX);
        temp = buf[1];
        voltage = ((float)temp)*25.6;
        temp = buf[0];
        voltage = voltage + (float)temp/10;
        temp = buf[3];
        temperaturemin = (int)temp;
        temp = buf[4];
        temperaturemax = (int)temp;
        temp = buf[5];
        voltagemin = ((float)temp)/50;
        temp = buf[6];
        voltagemax = ((float)temp)/50;
     }
  }

 
GLCD.SelectFont(CalBlk36);
GLCD.GotoXY(32,10);
GLCD.print(data1);

}


musskopf

Hello... you might ne having troubles because of the volume of messages. You need to speedup your main loop.

First thing just make a timer to print how long your loop is taking. Print it every 10s example.

Now remove everything and just make a counter every time a CAN msg is available and print this counter every 10s as well.

My suspicion is that you're spending all time updating the LCD... so make a function the will only update if new data and define a max update rate, like every second.
http://talk2.wisen.com.au

silviocross

Hello Musskopf,

you are right, I'm now updating the LCD values each 2s and everything works absolutely fine!

thank you very much!
Silvio

musskopf

Great! By the way, which bike are you connecting to? I have on my ToDo list to connect a Talk2 RPi Hat to a F800R for some fun projects, especially to try to produce to visual alert (high bean and hazard lights) when I use the horn. Hopefully will find some time soon.
http://talk2.wisen.com.au

silviocross

Great! By the way, which bike are you connecting to? I have on my ToDo list to connect a Talk2 RPi Hat to a F800R for some fun projects, especially to try to produce to visual alert (high bean and hazard lights) when I use the horn. Hopefully will find some time soon.
I'm not sure the F800R has a CAN connected headlight... I'm using my dashboard on a KTM Freeride-E (electric motocross bike) but I'm only visualizing data without sending anything new on the bus.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy