You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is my first time working with CAN bus. I have connected Teensy 4.1 to MCP2551 module. So that I can transmit and receive data from this smart actuator. However, I could not able to either control the motor or read any data from it. I cannot find any issue yet.
This is my code.
#include <FlexCAN_T4.h>
// Initialize CAN bus
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> mycan;
// Motor control commands
const uint8_t MOTOR_OFF[8] = {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
const uint8_t MOTOR_STOP[8] = {0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
const uint8_t MOTOR_ON[8] = {0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
const uint8_t TORQUE_OPEN_LOOP[8] = {0xA0, 0x00, 0x00, 0x00, 0xC8, 0x00, 0x00, 0x00}; // Only for MS series
const uint8_t SPEED_CLOSED_LOOP[8] = {0xA2, 0x00, 0x00, 0x00, 0xA0, 0x8C, 0x00, 0x00};
const uint32_t motor_id = 0x141; // Base identifier for motor commands
void setup() {
Serial.begin(115200);
mycan.begin(); // CAN Initialize
mycan.setBaudRate(1000000); // Set baud rate to 1 Mbps
Serial.println("CAN OK");
// Turn motor on
sendCanMessage(motor_id, MOTOR_ON);
Serial.println("Motor ON");
delay(1000);
// Example: Set power control value for torque open loop control
sendCanMessage(motor_id, TORQUE_OPEN_LOOP);
Serial.println("Open Loop");
delay(10000);
// Turn motor off
sendCanMessage(motor_id, MOTOR_OFF);
Serial.println("Motor OFF");
}
void loop() {
// Receive and print CAN messages
CAN_message_t msg;
mycan.read(msg);
Serial.print("Received: ");
for (int i = 0; i < msg.len; i++) {
Serial.print(msg.buf[i], HEX);
Serial.print(" ");
}
Serial.println();
if (mycan.read(msg)) {
Serial.print("Received: ");
for (int i = 0; i < msg.len; i++) {
Serial.print(msg.buf[i], HEX);
Serial.print(" ");
}
Serial.println();
}
// else{Serial.println("Not received");}
delay(1000);
}
void sendCanMessage(uint32_t can_id, const uint8_t *data) {
CAN_message_t msg;
msg.id = can_id;
msg.len = 8;
for (int i = 0; i < 8; i++) {
msg.buf[i] = data[i];
}
mycan.write(msg);
Serial.print("Sent: ");
for (int i = 0; i < 8; i++) {
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.println();
}
What would be the issue? Can someone advise me regarding this matter?
The text was updated successfully, but these errors were encountered:
This is my first time working with CAN bus. I have connected Teensy 4.1 to MCP2551 module. So that I can transmit and receive data from this smart actuator. However, I could not able to either control the motor or read any data from it. I cannot find any issue yet.
This is my code.
What would be the issue? Can someone advise me regarding this matter?
The text was updated successfully, but these errors were encountered: