#include <mcp_can.h>
#include <SPI.h>
MCP_CAN CAN0(53); // Set CS to pin 53
//MCP_CAN CAN0(2); //for my new prototype shield
unsigned char stmp[8] = {0, 0xA0, 0x41, 0, 0, 0, 0, 0};
unsigned char extpgn[26] = {0, 0xA0, 0x41, 0, 0, 0, 0, 0};
void setup(){
Serial.begin(115200);
// init can bus, baudrate: 250k
if(CAN0.begin(CAN_250KBPS) == CAN_OK) Serial.print("can init ok!!\r\n");
else Serial.print("Can init fail!!\r\n");
//testPGN127489();
//transmitPGN127489(0,55.5,164,171,13.54,15.8,221,11.2,32.1,38,44,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
testRPM();
}
void loop(){
//PGN 127488 binary 011111001000000000
//for id try 110 + above + 00000000 = 11001111100100000000000000000 = hex 19F20000
//highest priority = 000 try 00001111100100000000000000000 = hex 1F20000 = dec 32636928
//PGN 127489 binary 011111001000000001
//for id try 00001111100100000000100000000 = hex 1F20100 = dec 32637184
// send data: id = 0x00, extended frame, data len = 8, stmp: data buf
CAN0.sendMsgBuf(32636928L, 1, 8, stmp);
//testPGN127489();
//transmitPGN127489(0,55.5,164,171,13.54,15.8,221,11.2,32.1,38,44,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); //with checkEngine flag set to true
transmitPGN127489(0,55.5,164,171,13.54,15.8,221,11.2,32.1,38,44,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); //with checkEngine flag set to false
delay(1000);
}
void testPGN127489(){
//if an engine parameter is not available, set it to 255
unsigned char extpgn[26] = {0, 208, 14, 170, 13, 151, 136, 236, 4, 37, 2, 112, 207, 11, 0, 255, 255, 255, 255, 1, 0, 0, 0, 0, 255, 255}; //decimal values
unsigned char temp[8] = {0,0,0,0,0,0,0,0};
//
int cur=0;
for(int i = 0; i<=3; i++){
temp[0] = i; //frame counter
if (i==0){
temp[1] = 26; //total bytes in fast packet
//send the first 6 bytes
for(int j = 2; j<8; j++){
temp[j]=extpgn[cur];
cur++;
}
} else{
for(int j = 1; j<8; j++){
temp[j]=255;
}
//send the next 7 data bytes
for(int j = 1; j<8; j++){
temp[j]=extpgn[cur];
cur++;
}
}
CAN0.sendMsgBuf(32637184L, 1, 8, temp);
delay(1);
}
}
void testRPM(){
unsigned char stmp[8] = {0, 0xA0, 0x41, 0, 0, 0, 0, 0};
int rpm;
for(int i = 0; i<=4200; i=i+100){
rpm=(i*4);
stmp[2]=rpm/256;
stmp[1]=rpm-(stmp[2]*256);
CAN0.sendMsgBuf(32636928L, 1, 8, stmp);
delay(100);
}
}
void transmitPGN127489(int engineInstance, float engineOilPress, float engineOilTemp, float engineCoolantTemp, float battery,
float fuelRate, float engineHours, float engineCoolantPress, float engineFuelPress, float engineLoad, float engineTorque, bool flagCheckEngine,
bool flagOverTemp, bool flagLowOilPress, bool flagLowOilLevel, bool flagLowFuelPress, bool flagLowSystemVoltage, bool flagLowCoolantLevel,
bool flagWaterFlow, bool flagWaterInFuel, bool flagChargeIndicator, bool flagPreheatIndicator, bool flagHighBoostPress, bool flagRevLimitExceeded,
bool flagEgrSystem, bool flagTPS, bool flagEmergencyStopMode, bool flagWarning1, bool flagWarning2, bool flagPowerReduction,
bool flagMaintenanceNeeded, bool flagEngineCommError, bool flagSubThrottle, bool flagNeutralStartProtect, bool flagEngineShuttingDown){
//if an engine parameter is not available, set it to 255
unsigned int extpgn[26]; // {0, 208, 14, 170, 13, 151, 136, 236, 4, 37, 2, 112, 207, 11, 0, 255, 255, 255, 255, 1, 0, 0, 0, 0, 255, 255}; //decimal values
unsigned char temp[8]; // {0,0,0,0,0,0,0,0};
float v;
extpgn[0]=engineInstance;
v=engineOilPress;
v=v*68.94757;
//extpgn[2]=v/256;
//extpgn[1]=v-(extpgn[2]*256);
extpgn[2]=highByte((int) v);
extpgn[1]=lowByte((int) v); //this works well. I should change the code below to use this format.
v=engineOilTemp;
v=(((v-32)*5/9)+273)*100;
extpgn[4]=v/256;
extpgn[3]=v-(extpgn[4]*256);
v=engineCoolantTemp;
v=(((v-32)*5/9)+273)*100;
extpgn[6]=v/256;
extpgn[5]=v-(extpgn[6]*256);
v=battery;
v=v*100;
extpgn[8]=v/256;
extpgn[7]=v-(extpgn[8]*256);
v=fuelRate;
v=v*37.8541;
extpgn[10]=v/256;
extpgn[9]=v-(extpgn[10]*256);
v=engineHours;
v=v*3600;
//extpgn[14]=v/16777216;
//extpgn[13]=(v-(extpgn[14]*16777216))/65536;
//extpgn[12]=(v-((extpgn[14]*16777216)+(extpgn[13]*65536)))/256;
//extpgn[11]=v-((extpgn[14]*16777216)+(extpgn[13]*65536)+(extpgn[12]*256));
int tempVar = (long) v >> 16;
extpgn[14]=highByte(tempVar);
extpgn[13]=lowByte(tempVar);
tempVar = (long) v & 0xFFFF;
extpgn[12]=highByte(tempVar);
extpgn[11]=lowByte(tempVar);
v=engineCoolantPress;
v=v*68.94757;
extpgn[16]=v/256;
extpgn[15]=v-(extpgn[16]*256);
v=engineFuelPress;
v=v*689.4757;
extpgn[18]=v/256;
extpgn[17]=v-(extpgn[18]*256);
extpgn[19] = 255; //nmea2000 reserved byte
int engineStatus1P1 = B00000000;
int engineStatus1P2 = B00000000;
int engineStatus2 = B00000000;
if (flagCheckEngine) engineStatus1P1 |= B00000001;
if (flagOverTemp) engineStatus1P1 |= B00000010;
if (flagLowOilPress) engineStatus1P1 |= B00000100;
if (flagLowOilLevel) engineStatus1P1 |= B00001000;
if (flagLowFuelPress) engineStatus1P1 |= B00010000;
if (flagLowSystemVoltage) engineStatus1P1 |= B00100000;
if (flagLowCoolantLevel) engineStatus1P1 |= B01000000;
if (flagWaterFlow) engineStatus1P1 |= B10000000;
if (flagWaterInFuel) engineStatus1P2 |= B00000001;
if (flagChargeIndicator) engineStatus1P2 |= B00000010;
if (flagPreheatIndicator) engineStatus1P2 |= B00000100;
if (flagHighBoostPress) engineStatus1P2 |= B00001000;
if (flagRevLimitExceeded) engineStatus1P2 |= B00010000;
if (flagEgrSystem) engineStatus1P2 |= B00100000;
if (flagTPS) engineStatus1P2 |= B01000000;
if (flagEmergencyStopMode) engineStatus1P2 |= B10000000;
if (flagWarning1) engineStatus2 |= B00000001;
if (flagWarning2) engineStatus2 |= B00000010;
if (flagPowerReduction) engineStatus2 |= B00000100;
if (flagMaintenanceNeeded) engineStatus2 |= B00001000;
if (flagEngineCommError) engineStatus2 |= B00010000;
if (flagSubThrottle) engineStatus2 |= B00100000;
if (flagNeutralStartProtect) engineStatus2 |= B01000000;
if (flagEngineShuttingDown) engineStatus2 |= B10000000;
extpgn[20]=engineStatus1P1;
extpgn[21]=engineStatus1P2;
extpgn[22]=engineStatus2;
extpgn[23]=0;
extpgn[24]=engineLoad;
extpgn[25]=engineTorque;
int cur=0;
for(int i = 0; i<=3; i++){
temp[0] = i; //frame counter
if (i==0){
temp[1] = 26; //total bytes in fast packet
//send the first 6 bytes
for(int j = 2; j<8; j++){
temp[j]=extpgn[cur];
cur++;
}
} else{
for(int j = 1; j<8; j++){
temp[j]=255;
}
//send the next 7 data bytes
for(int j = 1; j<8; j++){
temp[j]=extpgn[cur];
cur++;
}
}
CAN0.sendMsgBuf(32637184L, 1, 8, temp);
delay(1);
}
//for debugging
/*
Serial.println();
Serial.print("Data = ");
for(int j = 0; j<26; j++){
if (j<25){
Serial.print(extpgn[j]);
Serial.print(", ");
} else {
Serial.println(extpgn[j]);
}
}
Serial.print("HexData = ");
for(int j = 0; j<26; j++){
if (j<25){
Serial.print(extpgn[j],HEX);
Serial.print(", ");
} else {
Serial.println(extpgn[j],HEX);
}
}
*/
}