Open josejnra opened 3 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
}
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:
I appreciate any guidance.