47 uint16_t cob_id = message.
cob_id;
49 std::vector<uint8_t> data;
51 DEBUG_LOG(
"Received transmit PDO with cob_id 0x"<<std::hex<<cob_id<<
" (usually from node 0x"<<node_id<<
") length = "<<message.
len);
53 for (
unsigned i=0; i<message.
len; ++i) {
54 data.push_back(message.
data[i]);
58 bool found_callback =
false;
60 std::lock_guard<std::mutex> scoped_lock(m_receive_callbacks_mutex);
62 if (callback.cob_id == cob_id) {
63 found_callback =
true;
66 callback.callback(data);
72 if (!found_callback) {
73 PRINT(
"PDO is unassigned. Here is the data (LSB):");
74 for (
unsigned i=0; i<data.size(); ++i) {
75 std::cout << std::hex << (unsigned)data[i] <<
" ";
77 std::cout << std::endl;
83 void PDO::send(uint16_t cob_id,
const std::vector<uint8_t>& data) {
85 assert(data.size()<=8 &&
"[PDO::send] A PDO message can have at most 8 data bytes.");
90 message.len = data.size();
91 for (uint8_t i=0; i<data.size(); ++i) {
92 message.data[i] = data[i];
95 DEBUG_LOG_EXHAUSTIVE(
"Sending the following PDO:");
96 DEBUG_EXHAUSTIVE(message.print();)
103 std::lock_guard<std::mutex> scoped_lock(m_receive_callbacks_mutex);
104 m_receive_callbacks.push_back({cob_id,callback});
uint16_t cob_id
Message ID aka COB-ID.
uint8_t len
Message's length (0 to 8)
void send(const Message &message)
Sends a message.
void send(uint16_t cob_id, const std::vector< uint8_t > &data)
Sends a PDO message.
std::function< void(std::vector< uint8_t >) > Callback
Type of the callback.
This class implements the Core of KaCanOpen It communicates with the CAN driver, sends CAN messages a...
void process_incoming_message(const Message &message) const
Handler for an incoming PDO message.
void add_pdo_received_callback(uint16_t cob_id, PDOReceivedCallback::Callback callback)
Adds a callback which will be called when a PDO has been received with the given COB-ID.
uint8_t get_node_id() const
Extracts the node id from the COB-ID.
PDO(Core &core)
Constructor.
A PDO message receiver function together with it's COB-ID Important: Never call add_pdo_received_call...
This struct represents a CANOpen message.
uint8_t data[8]
Data bytes.