I am building some software to send and receive CAN messages. Therefore I have created 2 libraries myself and I am using the MCP_CAN library (GitHub - coryjfowler/MCP_CAN_lib: MCP_CAN Library). I will give a short explanation about my own written libraries.
I have a class called CAN_Message which keeps the basic function of a CAN message frame. Then I have a publisher and a subscriber class. The publisher has properties latency and callback. If i.e. the latency is 1000, the callback function should be called every 1000 ms in which you can program yourself what the publisher should do (probably to send a CAN message). The subscriber has as properties can_id and callback. The idea is that when a message with id can_id has been received, the callback function of the subscriber is called.
Then I have a class called CAN_Node_Handler. The node handler maintains a list of publishers and a list of subscribers. In the loop it should check for every publisher if it is time to call the publisher callback function. The node handler also should be notified about a new CAN message, so it can check for all of the subscribers if the callback function should be called.
The list of publishers is a simplified implementation of c++'s std::vector with just the stuff I need. As you might notify I use publisher pointers and subscriber pointers in the node handler. This is because the pass by reference didn’t seem to work properly. Everytime I got one publisher out of the list of publishers it seemed like it has been copied. I fixed it using pointers.
Now the problem. In the loop I first spin the publishers, so I check for each publisher if it is time to call the callback function and if it is, I call the callback function. Everything goes alright, messages are sent correctly with use of the MCP_CAN_lib. Then I try to receive a message. If there is a message (digitalRead(2) == false) I receive it. This also goes alright, I can receive messages with MCP_CAN_lib. But from the moment I have tried to receive a message with MCP_CAN_lib (the first time digitalRead(2) == false) the values of my publisher’s properties have changed (see the serial prints). I have no clue why this is. I suspected maybe I used too much space for the variables, so I reduced the size of the publisher and subscriber lists, but this didn’t help.
If you don’t have an MCP can module, you can still reproduce this. You just have to connect the ground to D2.
Can anyone help me with this? I have attached all of the code I have used.
can_container.h (783 Bytes)
can_message.h (2.93 KB)
can_node_handler.h (2.18 KB)
can_publisher.h (1.04 KB)
can_subscriber.h (807 Bytes)
rtb_can.h (419 Bytes)
rtb_can_database.h (873 Bytes)
rtb_can_dfs.h (131 Bytes)
rtb_lib_test.ino (2 KB)
rtb_functions.h (1.35 KB)