Mcp can.h cannot receive data with the specified ID

Hello, I have a very strange problem when using the mcp_can library.
The development board I use is arduino uno development board, mcp2515 module and mcp_canh function library.
my question:
When used, it reads the ID 0203 but it also reads the information 0x201, 0x202, 0x204
Here is the code I wrote.

#include <mcp_can.h>
#include <SPI.h>

// 定义 PID 控制相关参数
int setpoint = 0x2BC, Output = 0x2BC; // 设定值
float kp = 1; // 比例系数
float ki = 0.1; // 积分系数
float kd = 1; // 微分系数
float last_error = 0;
float integral = 0;

const int SPI_CS_PIN2 = 9; //电机1
MCP_CAN CAN2(SPI_CS_PIN2); //电机1

//PID
unsigned char buf2[8];
int rotor_angle1;
int torque1;
int rotor_speed1;

void setup() {
Serial.begin(9600);
if (CAN2.begin(MCP_ANY, CAN_1000KBPS, MCP_8MHZ) == CAN_OK) {
Serial.println("1MCP2515 Initialized Successfully!22222222");
CAN2.setMode(MCP_NORMAL);
} else {
Serial.println("1Error Initializing MCP2515...22");
}
}
unsigned char len = 0;
unsigned char buf[8];
int rotor_angle;
int torque;
int rotor_speed;
//测试得知 向c610发送扭矩 最大发送间隔为2500ms
void loop() {
delay(10);
CAN2.readMsgBuf(0x203, 8, buf1); // 读取CAN消息
rotor_angle1 = (buf1[0] << 8) | buf1[1]; // 转子机械角度
torque1 = (buf1[4] << 8) | buf1[5]; // 实际输出转矩
rotor_speed1 = (buf1[2] << 8) | buf1[3]; // 转子转速
Serial.println(rotor_speed1);
delay(2);
//}
}

This is part of my code to control the motor, and the problem is not only that the motor and the motor that communicate with can will stop rotating every time the motor data is read. At present, I know that the motor's motor will stop rotating when the can bus does not accept data for a long time (about 2000 milliseconds).
Can this situation be resolved? Thank you

Welcome!Start by reading the forum guidelines and post your code accordingly using code tags. It appears you do not have any of the filters set so you will see everything on the CAN bus.

Wow. @burning_brain, you are off to a terrible start. Cross-posting, hijacking, and both. Do not do that again.

1 Like

The MCP2515 board requires a 16MHz crystal to do 1 Mbit/s.

post your complete sketch as a code-section.
How should anybody analyse your code if you only present a part of it?

Sure this situation can be resolved. But it needs that you provide much much more detail-information.

Before going on to post in this manner

welcome to the arduino-forum.
I'm pretty sure that you agree and will follow the way how to solve your problem mimimum 200 minutes faster.
This requires to invest 20 minutes of your precious time to read how to speedup solving your problems.

Directly after registering you got presented informations how to speed up solving your problem.
You should really read it.

best regards Stefan

Brother, excuse me how to set mask filter ah, previously understood, but the high probability is set wrong, I am a small white, big man teach me

So, bro, what would be better

Really? I see the crystal oscillator on the module I'm using is 8000

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.