Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAN bus communication with a smart actuator and Teensy 4.1 (FlexCAN_T4 Library) & MCP2551 module #77

Open
PramVidu opened this issue Jun 24, 2024 · 0 comments

Comments

@PramVidu
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant