Filter for ID and data

can anyone help me understand filtering on the carloop? I’m trying to filter for a specific ID and data. but its not working

these are the examples but I’m not seeing how to filter for ID and Data
// EXAMPLES
CANChannel can(CAN_D1_D2);
// Match only message ID 0x100
can.addFilter(0x100, 0x7FF);
// Match any message with the highest ID bit set
can.addFilter(0x400, 0x400);
// Match any message with the higest ID bit cleared
can.addFilter(0x0, 0x400);
// Match only messages with extended IDs
can.addFilter(0, 0, CAN_FILTER_EXTENDED);

@dubb45,

As I understand it, the CAN filters do the filtering on ID only.
Once you have a message with a matching ID, then your firmware could then process the data. In your own processing, you could have some code that does filtering.

The general assumption is that the data format is specific for a given ID, so you would probably have to design a separate filtering function for each ID you are interested in.

When it comes to filtering the data, look into using bitwise operators to make your filtering efficient. For example, a bitwise OR goes through two input variables bit-by-bit and ORs each bit individually and produces an output variable. The bitwise OR (|) behaves differently than the logical OR (||). You will probably also need the bitwise AND (&) operator.

Hope that gets you started. Let us know if you need more detail or help.

appreciate it time to do a bit of research and hopefully I can post the working code soon.

I had someone share this bit of code with me, hopefully this will help others too. I’m still trying ti figure out how to implement it in the carloop code.
else if (can_MsgRx.id == 0x002)
{
status._rpm = (can_MsgRx.data[0] << 8) + can_MsgRx.data[1];
status._speed = ((can_MsgRx.data[2] << 8) + can_MsgRx.data[3]) >> 7;

  // what are the other 4 bytes?

}
else if (can_MsgRx.id == 0x003)
{
status._brake = can_MsgRx.data[3] & 0x01;
status._gear = can_MsgRx.data[4];
}
else if (can_MsgRx.id == 0x0d0)
{
if (can_MsgRx.data[0] == 0x80)
{
status._parkingBrake = true;
}
else
{
status._parkingBrake = false;
}
}

this is what I tried out

// This #include statement was automatically added by the Particle IDE.

#include <carloop.h>

#include “application.h”

CANChannel can(CAN_D1_D2);

void setup() {
can.begin(125000); // pick the baud rate for your network
// accept one message. If no filter added by user then accept all messages
can.addFilter(0x318, 0x7FF);
}

void loop() {
CANMessage Message;

Message.id = 0x318;
can.transmit(Message);

delay(10);

if can.receive(Message) {
    // message received
}

else if (can.receive == 0x002)

{
status._rpm = (can.receive.data[0] << 8) + can.receive.data[1];
status._speed = ((can.receive.data[2] << 8) + can.receive.data[3]) >> 7;

  // what are the other 4 bytes?

}
else if (can.receive.id == 0x003)
{
status._brake = can.receive.data[3] & 0x01;
status._gear = can.receive.data[4];
}
else if (can.receive.id == 0x0d0)
{
if (can.receive.data[0] == 0x80)
{
status._parkingBrake = true;
}
else
{
status._parkingBrake = false;
}
}

}

I get a bunch of errors in the particle IDE lol ill keep at it.

Should that line be the following?

else if (can.receive.id == 0x002)

Also, you have too many end braces:

}

Make sure every opening brace has a closing brace and that they are in the right place. If they are unmatched, you will get errors. If they are in the wrong place, the firmware will behave in unexpected ways.