makerbase-motor / MKS-SERVO57D

Makerbase MKS SERVO57D NEMA23 closed loop stepper motor Driver FOC quiet and efficient RS485 / CAN
37 stars 6 forks source link

Using CAN version with ESP32 #11

Open josejnra opened 3 months ago

josejnra commented 3 months ago

I've been trying to use MKS SERVO42_CAN with an ESP32 but no ideia why I'm getting no response back. It looks like the commands are making to MKS, but not sure whether or not it was sent correctly.

I'm using the lib arduino-CAN.

Here it is the code I've been using:

#include <CAN.h>

#define SERVO_RX_PIN 16
#define SERVO_TX_PIN 17

void setup() {
  CAN.setPins(SERVO_RX_PIN, SERVO_TX_PIN);

  // start the CAN bus at 500 kbps
  if (!CAN.begin(500E3)) {
    Serial.println("Starting CAN failed!");
    while (1)
      ;
  } else {
    Serial.println("CAN Initialized");
  }

  Serial.begin(115200);
  while (!Serial) {
    ;  // wait for serial port to connect. Needed for native USB port only
  }
  delay(3000);
}

void loop() {
  sendCommand();
  readResponse();

  delay(1000);
}

void sendCommand() {
  // CAN.beginPacket(1);  // CAN ID

  CAN.beginPacket(1, 2, false);  // CAN ID, DLC, RTR
  CAN.write(0x30);               // function code
  CAN.write(0x31);               // CRC
  CAN.endPacket();
}

void readResponse() {
  unsigned long sTime;  //timing start time

  sTime = millis();  //get the current moment
  while (1) {
    // try to parse packet
    int packetSize = CAN.parsePacket();

    if (packetSize) {
      // received a packet
      Serial.print("Received ");

      if (CAN.packetExtended()) {
        Serial.print("extended ");
      }

      if (CAN.packetRtr()) {
        // Remote transmission request, packet contains no data
        Serial.print("RTR ");
      }

      Serial.print("packet with id 0x");
      Serial.print(CAN.packetId(), HEX);

      if (CAN.packetRtr()) {
        Serial.print(" and requested length ");
        Serial.println(CAN.packetDlc());
      } else {
        Serial.print(" and length ");
        Serial.println(packetSize);

        // only print packet data for non-RTR packets
        while (CAN.available()) {
          Serial.print((char)CAN.read());
        }
        Serial.println();
      }

      Serial.println();
    }

    if ((millis() - sTime) > 2000) {  //Judging whether to time out
      break;                          //timeout, exit while(1)
    }
  }
}

I appreciate any guidance.

Danez-DK commented 2 months ago

Use a TJA1050 module, and then this would work.

RangeIOT_CAN.h

#pragma once
#include "main.h"

#include "driver/gpio.h"
#include "driver/twai.h"

static const twai_message_t stepperMotor_Encoder_Value = {{0, 0, 1, 0, 0}, 0x01, 2, {0x30, 0x31}};
static const twai_message_t stepperMotor_RT_Speed = {{0, 0, 1, 0, 0}, 0x01, 2, {0x32, 0x33}};

static const twai_message_t stepperMotor_Calibrate = {{0, 0, 1, 0, 0}, 0x01, 3, {0x80, 0x00, 0x81}};

void initCanBus();
void canBusSend(twai_message_t twai_message);
void canBusCheck();

RangeIOT_CAN.cpp

#include "RangeIOT_CAN.h"

void initCanBus() {
    // Initialize configuration structures using macro initializers
    twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(GPIO_NUM_5, GPIO_NUM_4, TWAI_MODE_NORMAL);
    twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS();
    twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();

    // Install TWAI driver
    if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
        printf("Driver installed\n");
    } else {
        printf("Failed to install driver\n");
        return;
    }

    // Start TWAI driver
    if (twai_start() == ESP_OK) {
        printf("Driver started\n");
    } else {
        printf("Failed to start driver\n");
        return;
    }

}

void canBusSend(twai_message_t twai_message) {
    /*
        .extd = 1,              // Standard vs extended format
        .rtr = 0,               // Data vs RTR frame
        .ss = 0,                // Whether the message is single shot (i.e., does not repeat on error)
        .self = 0,              // Whether the message is a self reception request (loopback)
        .dlc_non_comp = 0,      // DLC is less than 8
        // Message ID and payload
        .identifier = 0xAAAA,
        .data_length_code = 4,
        .data = {0, 1, 2, 3},
    */
    twai_message_t message = twai_message;

    // Queue message for transmission
    if (twai_transmit(&message, pdMS_TO_TICKS(1000)) == ESP_OK) {
        printf("Message queued for transmission\n");
    } else {
        printf("Failed to queue message for transmission\n");
    }
}

void canBusCheck() {
    twai_message_t message;
    if (twai_receive(&message, pdMS_TO_TICKS(10)) == ESP_OK) {
        printf("Message received\n");
        if (message.extd) {
            printf("Message is in Extended Format\n");
        } else {
            printf("Message is in Standard Format\n");
        }
        printf("ID is %d\n", message.identifier);
        if (!(message.rtr)) {
            for (int i = 0; i < message.data_length_code; i++) {
                printf("Data byte %d = %d\n", i, message.data[i]);
            }
        }
    }

    // Process received message
}