Hello,
Full code at the bottom of post.
I have read most if not all of the CANBUS filtering posts but I'm still struggling to get it to work,
Quick summary,
I'm trying to filter on ID's 0x280 & 0x288, so only these are presented to the Arduino buffer. I've tried the following in turn but none of them appear to work;
CAN0.init_Mask( 0, 0, 0b001010001000 ); // Init first mask...
CAN0.init_Filt( 0, 0, 0b001010000000 ); // Init 1st filter...
CAN0.init_Filt( 1, 0, 0b001010001000 ); // Init 2nd filter...
CAN0.init_Mask( 0, 0, 0b111111111111 ); // Init first mask...
CAN0.init_Filt( 0, 0, 0b001010000000 ); // Init 1st filter...
CAN0.init_Filt( 1, 0, 0b001010001000 ); // Init 2nd filter...
CAN0.init_Mask( 0, 0, 640 | 648 ); // Init first mask...
CAN0.init_Filt( 0, 0, 640 ); // Init 1st filter...
CAN0.init_Filt( 1, 0, 648 ); // Init 2nd filter...
CAN0.init_Mask( 0, 0, 0x280 | 0x288 ); // Init first mask...
CAN0.init_Filt( 0, 0, 0x280 ); // Init 1st filter...
CAN0.init_Filt( 1, 0, 0x288 ); // Init 2nd filter...
The filters are being set,
Entering Configuration Mode Successful!
Setting Baudrate Successful!
MCP2515 Initialized Successfully!
Entering Configuration Mode Successful!
Setting Baudrate Successful!
MCP2515 Initialized Successfully!
Starting to Set Mask!
Setting Mask Successful!
Starting to Set Filter!
Setting Filter Successful!
Starting to Set Filter!
Setting Filter Successful!
This is what I'm seeing after trying to apply the filters, as you can see all the ID's still appear,
ID: 2C0 Data: 00 83
ID: 488 Data: 00 23 23 7E A6 00 00 00
ID: 2A0 Data: 00 00 00
ID: 2C0 Data: 00 83
ID: 1A0 Data: 00 40 00 00 FE FE 00 15
ID: 280 Data: 09 23 2C 13 22 00 20 22
RPM: 1227
Max_RPM: 1727
ID: C2 Data: 00 80 00 80 04 B2 00 4D
ID: 320 Data: 04 02 12 00 00 00 00 00
ID: 38A Data: A8 00 A8 00
ID: 2C0 Data: 00 83
ID: 2A0 Data: 00 00 00
ID: 280 Data: 09 22 3C 13 22 00 20 22
RPM: 1231
Max_RPM: 1727
ID: 5A0 Data: 80 00 00 31 A3 01 00 AB
ID: 1A0 Data: 00 40 00 00 FE FE 00 19
ID: 38A Data: B8 00 B8 00
ID: 1A0 Data: 00 40 00 00 FE FE 00 1A
ID: 2C0 Data: 00 83
ID: 288 Data: 63 51 10 00 00 7B B4 1E
ID: 4A8 Data: 90 80 01 00 00 00 A0 B1
ID: C2 Data: 00 80 00 80 04 F2 00 0D
ID: 4A0 Data: 00 00 00 00 00 00 00 00
ID: 38A Data: C8 00 C8 00
ID: 1A0 Data: 00 40 00 00 FE FE 00 1D
ID: 280 Data: 09 21 49 13 22 00 20 22
RPM: 1234
Max_RPM: 1727
ID: 480 Data: F0 00 AA 7C 00 A5 00 00
ID: 2C0 Data: 00 83
ID: 280 Data: 09 21 49 13 22 00 20 22
I'm sure it must be something simple I'm missing but for the life of me I can't find what it is, any help would be most appreciated.
Thanks Ian
#include <mcp_can.h>
#include <SPI.h>
#include <SoftwareSerial.h>
//DWIN
const unsigned long eventInterval = 333;
unsigned long previousTimeRPM = 0;
unsigned long previousTimeSpeed = 0;
unsigned long previousTimeWM = 0;
unsigned long previousTimeIAT = 0;
unsigned long previousTimePSI = 0;
SoftwareSerial DWINSerial(3, 2); // RX, TX
unsigned char Buffer[8];
unsigned char RPMDWIN[8] = {0x5a, 0xa5, 0x05, 0x82, 0x12, 0x01, 0x00, 0x00};
unsigned char IAT_CDWIN[8] = {0x5a, 0xa5, 0x05, 0x82, 0x15, 0x01, 0x00, 0x00};
unsigned char PSIDWIN[8] = {0x5a, 0xa5, 0x05, 0x82, 0x20, 0x01, 0x00, 0x00};
unsigned char Max_RPMDWIN[8] = {0x5a, 0xa5, 0x05, 0x82, 0x16, 0x01, 0x00, 0x00};
unsigned char Max_IATDWIN[8] = {0x5a, 0xa5, 0x05, 0x82, 0x19, 0x01, 0x00, 0x00};
unsigned char Max_PSIDWIN[8] = {0x5a, 0xa5, 0x05, 0x82, 0x21, 0x01, 0x00, 0x00};
//CanBus
#define CAN0_INT 9 // Set INT to pin 9
MCP_CAN CAN0(10); // Set CS to pin 10
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
bool init_state = true;
//Change lights
int numLEDSLit = 1;
int latchPin = 8; // (8) ST_CP [RCK] on 74HC595
int clockPin = 7; // (5) SH_CP [SCK] on 74HC595
int dataPin = 6; // (3) DS [S1] on 74HC595
int leds = 0;
// Boost Pressure and IAT
// int offset = 125; // low pressure calibration
// int span = 895; // high pressure calibration
int offset = 111; // low pressure calibration
int span = 912; // high pressure calibration
float Boost_pressure_Pa;
float Boost_pressure_kPa;
float Boost_pressure_PSI;
int boost_pressure_pin = A3;
float Max_PSI = 0;
int IAT_voltage = 0;
float IAT_Vin = 4.6; // 4.6 volts - USB powered
float IAT_Vout = 0;
float R1 = 2000; // Temperature resistor value
float IAT_buffer = 0;
float logR2;
float R2;
float IAT;
int IAT_pin = A1;
float IAT_C = 0;
float Max_IAT = 0;
// float A = 1.297373711e-03, B = 2.587931666e-04, C = 1.639343214e-07; // Steinhart-Hart and Hart Coefficients Max
// float A = 1.306496435e-03, B = 2.580557547e-04, C = 1.752303942e-07; // Steinhart-Hart and Hart Coefficients Nominal
float A = 1.316012871e-03, B = 2.572431200e-04, C = 1.877359037e-07; // Steinhart-Hart and Hart Coefficients Min
//RPM
int RPM = 0;
int Max_RPM = 0;
int ARPM = 0;
int BRPM = 0;
//Speed
int Speed = 0;
int Max_Speed = 0;
//W-M
int Max_WM = 0;
int WM_value_percent = 1;
int WM_pin = 6;
int WM_value_T;
int WM_value_P;
int WM_value;
//int WM_Level_pin = A2;
int WM_Level = 101;
void setup() {
Serial.begin(115200);
DWINSerial.begin(115200);
pinMode(WM_pin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
// Initialize MCP2515 running at 8MHz with a baudrate of 500kb/s and the masks and filters disabled.
if (CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!");
else Serial.println("Error Initializing MCP2515...");
if (CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK)
Serial.println("MCP2515 Initialized Successfully!");
else
Serial.println("Error Initializing MCP2515...");
CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data.
pinMode(9, INPUT); // Setting pin 9 for /INT input
pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input
// CAN0.init_Mask( 0, 0, 0b001010001000 ); // Init first mask...
// CAN0.init_Filt( 0, 0, 0b001010000000 ); // Init 1st filter...
// CAN0.init_Filt( 1, 0, 0b001010001000 ); // Init 2nd filter...
CAN0.init_Mask( 0, 0, 0b111111111111 ); // Init first mask...
CAN0.init_Filt( 0, 0, 0b001010000000 ); // Init 1st filter...
CAN0.init_Filt( 1, 0, 0b001010001000 ); // Init 2nd filter...
// CAN0.init_Mask( 0, 0, 640 | 648 ); // Init first mask...
// CAN0.init_Filt( 0, 0, 640 ); // Init 1st filter...
// CAN0.init_Filt( 1, 0, 648 ); // Init 2nd filter...
// CAN0.init_Mask( 0, 0, 0x280 | 0x288 ); // Init first mask...
// CAN0.init_Filt( 0, 0, 0x280 ); // Init 1st filter...
// CAN0.init_Filt( 1, 0, 0x288 ); // Init 2nd filter...
}
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}
void sendRPMDWIN() {
unsigned long currentTimeRPM = millis();
if (currentTimeRPM - previousTimeRPM >= eventInterval) {
/* Event code */
RPMDWIN[6] = highByte(RPM);
RPMDWIN[7] = lowByte(RPM);
DWINSerial.write(RPMDWIN, 8);
/* Update the timing for the next time around */
previousTimeRPM = currentTimeRPM;
}
}
void sendSpeedDWIN() {
unsigned long currentTimeSpeed = millis();
if (currentTimeSpeed - previousTimeSpeed >= eventInterval) {
byte SpeedDWIN = Speed;
byte open[] = {0x5A, 0xA5, 0x05, 0x82, 0x13, 0x01, 0x00, SpeedDWIN};
DWINSerial.write(open, sizeof(open));
previousTimeSpeed = currentTimeSpeed;
}
}
void sendWM_value_percentDWIN() {
unsigned long currentTimeWM = millis();
if (currentTimeWM - previousTimeWM >= eventInterval) {
byte WM_value_percentDWIN = WM_value_percent;
byte open[] = {0x5A, 0xA5, 0x05, 0x82, 0x14, 0x01, 0x00, WM_value_percentDWIN};
DWINSerial.write(open, sizeof(open));
previousTimeWM = currentTimeWM;
}
}
void sendIAT_CDWIN() {
unsigned long currentTimeIAT = millis();
if (currentTimeIAT - previousTimeIAT >= eventInterval) {
int DWINIAT_C = IAT_C * 100;
IAT_CDWIN[6] = highByte(DWINIAT_C);
IAT_CDWIN[7] = lowByte(DWINIAT_C);
DWINSerial.write(IAT_CDWIN, 8);
previousTimeIAT = currentTimeIAT;
}
}
void sendPSI_DWIN() {
unsigned long currentTimePSI = millis();
if (currentTimePSI - previousTimePSI >= eventInterval) {
int DWINPSI = Boost_pressure_PSI * 100;
PSIDWIN[6] = highByte(DWINPSI);
PSIDWIN[7] = lowByte(DWINPSI);
DWINSerial.write(PSIDWIN, 8);
previousTimePSI = currentTimePSI;
}
}
void sendMax_RPMDWIN() {
if (RPM > Max_RPM) {
Max_RPM = RPM;
Max_RPMDWIN[6] = highByte(Max_RPM);
Max_RPMDWIN[7] = lowByte(Max_RPM);
DWINSerial.write(Max_RPMDWIN, 8);
Serial.print ("sending Max_RPM: ");
Serial.println (Max_RPM);
}
}
void sendMax_SpeedDWIN() {
if (Speed > Max_Speed) {
Max_Speed = Speed;
byte Max_SpeedDWIN = Max_Speed;
byte open[] = {0x5A, 0xA5, 0x05, 0x82, 0x17, 0x01, 0x00, Max_SpeedDWIN};
DWINSerial.write(open, sizeof(open));
}
}
void sendMax_WMDWIN() {
if (WM_value_percent > Max_WM) {
Max_WM = WM_value_percent;
byte Max_WMDWIN = Max_WM;
byte open[] = {0x5A, 0xA5, 0x05, 0x82, 0x18, 0x01, 0x00, Max_WMDWIN};
DWINSerial.write(open, sizeof(open));
}
}
void sendMax_IATDWIN() {
if (IAT_C > Max_IAT) {
Max_IAT = IAT_C;
int Max_DWINIAT_C = Max_IAT * 100;
Max_IATDWIN[6] = highByte(Max_DWINIAT_C);
Max_IATDWIN[7] = lowByte(Max_DWINIAT_C);
DWINSerial.write(Max_IATDWIN, 8);
}
}
void sendMax_PSIDWIN() {
if (Boost_pressure_PSI > Max_PSI) {
Max_PSI = Boost_pressure_PSI;
int Max_DWINPSI = Max_PSI * 100;
Max_PSIDWIN[6] = highByte(Max_DWINPSI);
Max_PSIDWIN[7] = lowByte(Max_DWINPSI);
DWINSerial.write(Max_PSIDWIN, 8);
}
}
void MAXRESET () {
if (DWINSerial.available())
{
for (int i = 0; i <= 8; i++) //this loop will store whole frame in buffer array.
{
Buffer[i] = DWINSerial.read();
// Serial.print(Buffer[0], HEX);
// Serial.print(Buffer[2], HEX);
// Serial.print(Buffer[2], HEX);
// Serial.print(Buffer[3], HEX);
// Serial.print(Buffer[4], HEX);
// Serial.print(Buffer[5], HEX);
// Serial.print(Buffer[6], HEX);
// Serial.print(Buffer[7], HEX);
// Serial.println(Buffer[8], HEX);
}
if (Buffer[0] == 0X5A)
{
switch (Buffer[4])
{
case 0x11:
if (Buffer[8] == 1)
{
ResetRPM();
ResetSpeed();
ResetIAT();
ResetWM();
ResetPSI();
}
}
}
}
}
void ResetRPM () {
Max_RPM = 0;
byte open[] = {0x5a, 0xa5, 0x05, 0x82, 0x16, 0x01, 0x00, 0x00};
DWINSerial.write(open, sizeof(open));
}
void ResetSpeed () {
Max_Speed = 0;
byte open[] = {0x5A, 0xA5, 0x05, 0x82, 0x17, 0x01, 0x00, 0x00};
DWINSerial.write(open, sizeof(open));
}
void ResetIAT () {
Max_IAT = 0;
byte open[] = {0x5a, 0xa5, 0x05, 0x82, 0x19, 0x01, 0x00, 0x00};
DWINSerial.write(open, sizeof(open));
}
void ResetWM () {
Max_WM = 0;
byte open[] = {0x5A, 0xA5, 0x05, 0x82, 0x18, 0x01, 0x00, 0x00};
DWINSerial.write(open, sizeof(open));
}
void ResetPSI () {
Max_PSI = 0;
byte open[] = {0x5A, 0xA5, 0x05, 0x82, 0x21, 0x01, 0x00, 0x00};
DWINSerial.write(open, sizeof(open));
}
void loop() {
// Serial.println("loop start");
if (!digitalRead(9)) // If pin 9 is low, read receive buffer
{
// Serial.println("before readMsgBuf");
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
Serial.print("ID: ");
Serial.print(rxId, HEX);
Serial.print(" Data: ");
for (int i = 0; i < len; i++) // Print each byte of the data
{
if (rxBuf[i] < 0x10) // If data byte is less than 0x10, add a leading zero
{
Serial.print("0");
}
Serial.print(rxBuf[i], HEX);
Serial.print(" ");
}
Serial.println();
// unsigned long msTime = millis();
// Serial.println(msTime);
if (rxId == 0x280) {
// throttle = rxBuf[5] * 0.4;
// Serial.print("Throttle (%): ");
// Serial.print(throttle, HEX);
// Serial.print(" ");
// Serial.println(throttle, DEC);
// if (throttle > throttlemax) throttlemax = throttle;
// Serial.print("Throttlemax: ");
// Serial.println(throttlemax, DEC);
//
//
// load = rxBuf[1] * 0.39;
// Serial.print("Engine Load (%): ");
// Serial.print(load, HEX);
// Serial.print(" ");
// Serial.println(load, DEC);
// if (load > loadmax) loadmax = load;
// Serial.print("loadmax: ");
// Serial.println(loadmax, DEC);
// RPM = ((rxBuf[3] * 256) + rxBuf[2]) * 0.25;
ARPM = rxBuf[3] * 256;
BRPM = ARPM + rxBuf[2];
RPM = BRPM * 0.25;
Serial.print("RPM: ");
Serial.println(RPM, DEC);
Serial.print("Max_RPM: ");
Serial.println(Max_RPM, DEC);
}
// if(rxId == 0x288){
//
// Speed = (rxBuf[3]*0.759);
// Serial.print("vehicle Speed (MPH): ");
// Serial.print(Speed);
// Serial.print(" ");
// Serial.println(Speed, DEC);
// if (Speed > Max_Speed) Max_Speed = Speed;
// Serial.print("Max_Speed: ");
// Serial.println(Max_Speed, DEC);
// }
}
// WM level code
// int WM_Level = analogRead(WM_Level_pin); // get adc value
//
// if (((HistoryValue >= WM_Level) && ((HistoryValue - WM_Level) > 10)) || ((HistoryValue < WM_Level) && ((WM_Level - HistoryValue) > 10)))
// {
// //sprintf(printBuffer, "ADC%d level is %d\n", WM_Level_pin, WM_Level);
// //Serial.print(printBuffer);
// HistoryValue = WM_Level;
// }
// Boost Pressure
Boost_pressure_Pa = map(analogRead(boost_pressure_pin), offset, span, 500, 4000);
Boost_pressure_kPa = Boost_pressure_Pa / 10, 1;
if (Boost_pressure_kPa < 20) Boost_pressure_kPa = 20;
Boost_pressure_PSI = Boost_pressure_kPa * 0.145038;
Boost_pressure_PSI = Boost_pressure_PSI - 14;
if (Boost_pressure_PSI < 1) Boost_pressure_PSI = 0;
// if (Boost_pressure_PSI > Max_PSI) Max_PSI = Boost_pressure_PSI;
//IAT
IAT_voltage = analogRead(IAT_pin);
if (IAT_voltage) {
IAT_buffer = IAT_voltage * IAT_Vin;
IAT_Vout = (IAT_buffer) / 1024.0;
IAT_buffer = (IAT_Vin / IAT_Vout) - 1;
R2 = R1 / IAT_buffer;
}
logR2 = log(R2);
IAT = (1.0 / (A + B * logR2 + C * logR2 * logR2 * logR2)); // Steinhart and Hart Equation. T = 1 / {A + B[ln(R)] + C[ln(R)]^3}
IAT_C = IAT - 273.15;
// WM delivery
if (IAT_C >= 27) {
WM_value_T = map (IAT_C, 27, 40, 0, 255);
WM_value_P = map(Boost_pressure_PSI, 8, 25, 0, 255);
if (WM_value_T >= WM_value_P) WM_value = WM_value_T;
if (WM_value_P >= WM_value_T) WM_value = WM_value_P;
if (WM_value <= 0) WM_value = 0;
WM_value_percent = WM_value / 2.5;
if (Boost_pressure_PSI >= 31) WM_value_percent = 100;
if (WM_value_percent >= 100) WM_value_percent = 100;
WM_value = WM_value_percent * 2.55;
analogWrite(WM_pin, WM_value);
}
else
{
WM_value_P = map(Boost_pressure_PSI, 8, 25, 0, 255);
WM_value_T = map (IAT_C, 28, 40, 0, 255);
if (WM_value_P >= WM_value_T) WM_value = WM_value_P;
if (WM_value_T >= WM_value_P) WM_value = WM_value_T;
if (WM_value <= 0) WM_value = 0;
WM_value_percent = WM_value / 2.5;
if (Boost_pressure_PSI >= 25) WM_value_percent = 100;
if (WM_value_percent >= 100) WM_value_percent = 100;
WM_value = WM_value_percent * 2.55;
analogWrite(WM_pin, WM_value);
}
// Shift lights not sure how it works
int numLEDSLit = RPM / 1000;
if (numLEDSLit >= 6) numLEDSLit = 8;
if (numLEDSLit < 1) numLEDSLit = 1;
leds = 0; // no LEDs lit to start
for (int i = 0; i < numLEDSLit; i++)
{
leds = leds + (1 << i); // sets the i'th bit
}
updateShiftRegister();
sendRPMDWIN();
sendSpeedDWIN();
sendWM_value_percentDWIN();
sendIAT_CDWIN();
sendPSI_DWIN();
sendMax_RPMDWIN();
sendMax_SpeedDWIN();
sendMax_WMDWIN();
sendMax_IATDWIN();
sendMax_PSIDWIN();
MAXRESET ();
}