Hi everyone,
I am a first year Student in an engeneer school in france, I am completly new on this forum and i would like to trade a bit of your wisdom and patience with my foolishness and poor english...
I am currently trying to get informations from a calculator PE3, which use the SAE J1939 protocol to transmit it's canbus, in order to get speed, rpm and other informations on the engine and the car I am working on. I have managed to get the canbus but my problems come from the fact that those informations don't change over time, even when the car is speeding up or slowing down.
I am certain the calculator does work because we have a software that can read the data correctly by plugging the calculator to a PC.
Here is my code:
#include <SPI.h>
#include <mcp_can.h>
//prototypes
int cmp_tab(float tab1,float tab2,int taille); //pour comparer 2 tableaux
//variables
unsigned char Flag_Recv = 0;
unsigned char len = 0;
unsigned char buf[8];
char str[20];
int w = 0;
unsigned char rpm [8];
unsigned char BvlteCT [8];
unsigned char vitesse[8];
float RPM;
void setup()
{
Serial.begin(115200);
START_INIT:
if(CAN_OK == CAN.begin(CAN_250KBPS)) // init can bus : baudrate = 250k
{
CAN.init_Filt(0,1,1); //actually i don't really know why i'm doing this but it doesn't work without this line
Serial.println("\nCAN BUS Shield init ok");
}
else
{
Serial.println("\nCAN BUS Shield init fail");
Serial.println("\nInit CAN BUS Shield again");
delay(100);
goto START_INIT;
}
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
}
void MCP2515_ISR()
{
Flag_Recv = 1;
}
void loop()
{
if(Flag_Recv) // check if get data
{
Flag_Recv = 0;
INT32U a=CAN.getCanId(); //get the bus ID in order to see what data i'm receiving
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
switch(a)
{
case 218099784: //rpm et tps
Serial.println("\nrpm et tps: ");
for(int i = 0; i<len; i++) // print the data
{
rpm[i]=buf[i];
Serial.print(rpm[i]);Serial.print("\t"); //getting print and recorded the data i've received, depending on the ID
}
RPM=rpm[4]*256+rpm[5];
if (RPM>=32767)
{
RPM=(RPM-65536);
}
Serial.println("\nles rpm:");
Serial.println(RPM);
break;
case 218100808:
Serial.println("\nvitesse: ");
for(int i = 0; i<len; i++)
{
vitesse[i]=buf[i];
Serial.print(vitesse[i]);Serial.print("\t ");
}
break;
case 218101064:
Serial.println("\nBttry cool temp: ");
for(int i = 0; i<len; i++)
{
BvlteCT[i]=buf[i];
Serial.print(BvlteCT[i]);Serial.print("\t");
}
break;
}
}
}
Basically what it does is simple:
-check if some data has arrived
-get the ID of the bus
-check which ID it is
-store the data regarding the ID
-print the canbus
here is the canbus protocol of the calculator:
http://www.pe-ltd.com/joomla/images/downloads/AN400_CAN_Protocol_B.pdf
My skills in programing and english are a bit limited so I have to ask you to have simple answers please.
I am in a bit of a hurry though because I have to finish this before next thursday and i really don't know how to do it....
I have search in the deep and dark parts of internet and I haven't found the answer to my problem, even on forums.
I thank you already for your usefull answers