I am looking for help regarding a project, the can bus communication has the multimaster principle. Can anyone advise me with the programming code for the nodes to send and receive data.
The general guidelines only handle communication between 2 nodes, but how is the correct way to communicate more than two? There must be a bidirectional communication, I have developed several codes to establish this but through the analysis with the CAN BUS Analyzer and the oscilloscope fluctuates.
That depends on the hardware you are using. While the application layer can be the same the low level stuff depends on the CAN controller. If you let us know what Arduino and CAN controller (built-in or external) you use you may get some library recommendation.
There is no other way to communicate in CAN. One node sends a message and any other node can read the message and decide whether it needs to use the data.
That depends on how you look at it. At the bit level the communication is bi-directional. Every message needs to be read by any other node and acknowledged.
At the message level there is no bi-directional communication. A message goes from one master to any other master. You may have some application layer on top of it that exchanges data but on the message level it is one message at a time.
You are overthinking the problem. In simplistic terms: remember each node has its own ID and will receive all messages on the bus. Each node determines what to do with each message, process something, send some data, ignore it etc... When a node has a response it simply sends it, any node interested in it can read it and do what it wants with it. Each message can have from 0 to 8 bytes of data, the number of bytes is encoded in the header. It is up to your software to determine what those bytes mean, and if they are bytes, integers, float, etc.
Note CAN is unidirectional but can switch directions for every message, it is transparent to you, the hardware does the work. Each node also has a priority encoded in its ID, the lower the number the higher the priority.
See if this helps: Assume you are with several people and you are all talking then the one of the people calls your name, you immediately pay attention to what that person said ignoring all the other stuff going around. There node names are called IDs.
thank you for replying. I am using 4 mega arduinos and 4 Sunflower can bus shield V3 boards.
I had the approach that being multimaster, one node sends and the others become receivers, then another one sends and the others become receivers, like a cyclic communication that starts with the module with the lowest ID, since they are configured with similar periods.
That is by default how the CAN bus operates, each node comes on when it wants to, there is no waiting for another node to come first. The ID can be a bit confusing, it only comes into play when more then one node wants to access the bus, used for arbitration, otherwise any order is ok and nothing gets delayed.
I have implemented a timer in each node, this for the sending period, the first two with 10ms and the other two with 20ms. There will come a point where everyone needs to access the bus and I don't understand why in the analyzer the order varies after some time.
the image shows when the time delta times do not fluctuate so much
You will have clock drift different in each Arduino as is the clock speed. To keep synced you need a common time base or a periodic message to sink the world. Simply sink from the main transmission by have the slaves respond x amount of time after they receive a transmission.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.

