Hi, I am developing a multiplexed network project based on Arduino microcontrollers, using the mcp2515.can.h and SPI.h library. I have a problem to synchronize the sending and receiving of messages according to the priority of the message. Can someone help me ?
with CanBus the identifier sets the priority of the CAN message
if you have a number of nodes exchanging messages you set the message IDs so high priority messages get thru first
Hello, thank you for replying. I have set in each code a different identifier and a different data field. But at the time of communication the nodes send messages out of sync. That is, node 1 is expected to send a message, then node 2, 3 and finally node 4 to start again the sending cycle.
#include <SPI.h>
#include <mcp2515_can.h>
const int SPI_CS_PIN = 10;
const int CAN_INT_PIN = 2;
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
unsigned char flagRecv = 0;
unsigned char len = 0;
unsigned char buf[8];
char str[20];
void setup() {
SERIAL_PORT_MONITOR.begin(115200);
attachInterrupt(digitalPinToInterrupt(CAN_INT_PIN), MCP2515_ISR, FALLING); // start interrupt
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
SERIAL_PORT_MONITOR.println("CAN init fail, retry...");
delay(100);
}
SERIAL_PORT_MONITOR.println("CAN init ok!");
}
void MCP2515_ISR() {
flagRecv = 1;
}
void loop() {
if (flagRecv) {
flagRecv = 0; // clear flag
while (CAN_MSGAVAIL == CAN.checkReceive()) {
// read data, len: data length, buf: data buf
long unsigned int rxId;
CAN.readMsgBuf(&len, buf);
Serial.print("Mensaje check: ");
// print the data
for (int i = 0; i < len; i++) {
SERIAL_PORT_MONITOR.print(buf[i]); SERIAL_PORT_MONITOR.print("\t");
}
SERIAL_PORT_MONITOR.println();
}
}
else {
unsigned char data[8] = {3, 3, 3, 3, 3, 3, 3, 3};
CAN.MCP_CAN::sendMsgBuf(0x30, 0, 8, data);
Serial.print("Mensaje enviado: ");
for (int i = 0; i < 8; i++) {
SERIAL_PORT_MONITOR.print(data[i]); SERIAL_PORT_MONITOR.print("\t");
}
SERIAL_PORT_MONITOR.println();
}
}
the microcontrollers are independent and there could be slight variations in clock speed etc
if you wish to get messages from devices in some specified order have a master poll the targets
How can I do this? The focus of the project is that the 4 modules are masters.
all nodes receive all message and use the ID to decide which are of interest, e.g. using the mask registers
if you wish messages to be transmitted in order node 2 could look out for message from node 1 when received transmit its message, node 3 waits for node 2 message etc etc
in this image the nodes are configured with a millis() function, to send every 100ms but there is evidence of uncoordinated times.
Ideally, in the rolling trace, each node should send a message every 10ms: Example
node 1: 1215.4569
node 2: 1215.4657
please help, because I think that the timing makes the priority uncoordinated.
to synchronise the modules the main module could transmit a message every 10mSec requesting data - the other modules receive this and respond
Sounds correct then the responders could have there priority set in the order they are to be received. This resolves clock skew as the main is controlling the process. Another way would be to have each responder wait until the previous required message is sent. This could cause the system to lock up so there will need be some checks.
It may also be that I have the network speed and baud rate set incorrectly.
How can I configure this correctly? Is there any table or reference
No clue what is the correct baud however since they are all talking what you have set is OK.
The table or reference is set up when the project is defined as is the priority etc.
If the values are not changing much you could use the last successful value.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.