Arduino and MCP2515 are used to control M2006 using DJI C610.
The CAN of the C610 and the H/L of the MCP2515 are interconnected and both terminators are enabled. Then each pin of Arduino UNO R4 WiFi and MCP2515 is connected as follows.
INT : D2
SCK : D13
SI: D11
SO : D12
CS: D10
GND : GND
VCC: 5V
As a result, CAN0.begin() operates normally, but CAN0.sendMsgBuf() does not. How can I get CAN0.sendMsgBuf() to work correctly?
The source code and operation results are described below.
code------
#include <SPI.h>
#include <mcp_can.h>
#define SS_Robomas_C610 10
MCP_CAN CAN0(SS_Robomas_C610);
const int CAN_bps = CAN_1000KBPS;
const int MCP_OSC = MCP_16MHZ;
void mcp2515_init() {
restart:
if(CAN0.begin(MCP_ANY, CAN_bps, MCP_OSC) == CAN_OK) {
Serial.println("Success : MCP2515 Initialization");
}
else {
Serial.println("Fail : MCP2515 Initialization");
goto restart;
}
delay(100);
CAN0.setMode(MCP_NORMAL);
}
void setup() {
Serial.begin(115200);
mcp2515_init();
}
void tx_can_message() {
uint32_t tx_message = 0x200;
uint8_t tx_data[8] = {0x07, 0xD0, 0x00, 0x00, 0x0, 0x0, 0x0, 0x0};
Serial.print("Sending CAN message: ID=");
Serial.print(tx_message, HEX);
Serial.print(" ");
Serial.print("Sending CAN data=");
byte sendStat = CAN0.sendMsgBuf(tx_message, 0, 8, tx_data);
for (int i = 0; i < 8; i++) {
Serial.print(tx_data[i], HEX);
Serial.print(" ");
}
Serial.print("sendStat= ");
if(sendStat == CAN_GETTXBFTIMEOUT){
Serial.println("CAN_GETTXBFTIMEOUT");
}
else if(sendStat == CAN_OK){
Serial.println("CAN_OK");
}
else if(sendStat == CAN_SENDMSGTIMEOUT){
Serial.println("CAN_SENDMSGTIMEOUT");
}
delay(100);
}
void rx_can_message() {
unsigned char len = 0;
unsigned char data[8];
unsigned long int rx_message;
if (CAN_MSGAVAIL == CAN0.checkReceive()) {
CAN0.readMsgBuf(&rx_message, &len, data);
if (rx_message == 0x201) {
Serial.print("Receiving CAN message: ID= ");
Serial.print(rx_message, HEX);
Serial.print(" ");
Serial.print("Receiving CAN data= ");
for (int i = 0; i < len; i++) {
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.println();
delay(100);
}
}
}
void loop() {
tx_can_message();
rx_can_message();
delay(100);
}
Serial Monitor-------
Entering Configuration Mode Successful!
Setting Baudrate Successful!
Success : MCP2515 Initialization
Sending CAN message: ID=200 Sending CAN data= 7 D0 0 0 0 0 0 0 sendStat=CAN_SENDMSGTIMEOUT
Sending CAN message: ID=200 Sending CAN data= 7 D0 0 0 0 0 0 0 sendStat=CAN_SENDMSGTIMEOUT
Sending CAN message: ID=200 Sending CAN data= 7 D0 0 0 0 0 0 0 sendStat=CAN_SENDMSGTIMEOUT
Sending CAN message: ID=200 Sending CAN data= 7 D0 0 0 0 0 0 0 sendStat=CAN_GETTXBFTIMEOUT
Sending CAN message: ID=200 Sending CAN data= 7 D0 0 0 0 0 0 0 sendStat=CAN_GETTXBFTIMEOUT
Sending CAN message: ID=200 Sending CAN data= 7 D0 0 0 0 0 0 0 sendStat=CAN_GETTXBFTIMEOUT
(From here on out, the same message keeps coming.)