Arduino and servo RMD-x8pro

Hello! Can you help me! I have RMD-x8pro . I'trying to connection to it via can-bus. But nothing is working. My code is written in Arduino.

[code]
#include<mcp_can.h>
#include<SPI.h>

#define StepValue 500

const int SPI_CS_PIN = 10;
MCP_CAN CAN(SPI_CS_PIN);
long GenPos = 10;
long GenVel = 600;

//variable to receive the message
unsigned char len = 0;

//variable to send the message
unsigned char buf[8];


void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(1000);
  while (CAN_OK != CAN.begin(CAN_1000KBPS))
  {
    Serial.println("CAN BUS Shield init fail");
    Serial.println("Init CAN BUS Shield again");
    delay(100);
  }
  Serial.println("CAN BUS Shield again");
  //Read initial motor position
  buf[0] = 0x94;
  buf[1] = 0x00;
  buf[2] = 0x00;
  buf[3] = 0x00;
  buf[4] = 0x00;
  buf[5] = 0x00;
  buf[6] = 0x00;
  buf[7] = 0x00;
  CAN.sendMsgBuf(0x141, 0, 8, buf);

  //response
  delay(3);
  if (CAN_MSGAVAIL == CAN.checkReceive()) //check if data is coming
  {
    CAN.readMsgBuf(&len, buf); //read data
    unsigned long canId = CAN.getCanId();
    Serial.print((buf[6] << 8) | buf[7]);
    Serial.print("\t");
    unsigned int EstPos = (buf [7] << 8 | buf [6]);
    if (EstPos > 18000) {
      GenPos = -36000 + EstPos;
    }
    else {
      GenPos = EstPos;
    }
    Serial.print(buf[6], HEX);
    Serial.print("\t");
    Serial.print(buf[7], HEX);
    Serial.print("\t");
    Serial.print(EstPos);
    Serial.print("\t");
    Serial.println(GenPos);
  }
  //Write the PID values to the motor
  buf[0] = 0x31;
  buf[1] = 0x00;
  buf[2] = 15;
  buf[3] = 6;
  buf[4] = 15;
  buf[5] = 6;
  buf[6] = 15;
  buf[7] = 6;
  CAN.sendMsgBuf(0x141, 0, 8, buf);
  delay(1);
  if (CAN_MSGAVAIL == CAN.checkReceive())
  {
    CAN.readMsgBuf(&len, buf);
    unsigned long canId = CAN.getCanId();
    for (int i = 0; i < len; i++)
    {
      Serial.print(buf[i], HEX);
      Serial.print("\t");
    }
    unsigned int p_in = buf[3];
    Serial.println(p_in);
  }
  delay(1000);
}

void loop() {
  // put your main code here, to run repeatedly:
  GenPos = GenPos + StepValue;
  buf[0] = 0xA4;
  buf[1] = 0x00;
  buf[2] = GenVel;
  buf[3] = GenVel >> 8;
  buf[4] = GenPos;
  buf[5] = GenPos >> 8;
  buf[6] = GenPos >> 16;
  buf[7] = GenPos >> 24;
  CAN.sendMsgBuf(0x141, 0, 8, buf);
  delay(1);
  if (CAN_MSGAVAIL == CAN.checkReceive())
  {
    CAN.readMsgBuf(&len, buf);
    unsigned long canId = CAN.getCanId();
    //Command byte
    Serial.print("ComBit");
    Serial.print("\t");
    //NULL
    Serial.print("Null");
    Serial.print("\t");
    //speed limited low byte
    Serial.print("SpdLlB");
    Serial.print("\t");
    //speed limited high byte
    Serial.print("SpdLB");
    Serial.print("\t");
    //position control low byte
    Serial.print("PCLB");
    Serial.print("\t");
    //position control
    Serial.print("PC");
    Serial.print("\t");
    //position control
    Serial.print("buf[6]");
    Serial.print("\t");
    //
    Serial.print("buf[7]");
    Serial.print("\t");
    //position control high byte
    Serial.print("p_in");
    Serial.print("\n");

    for (int i = 0; i < len; i++)
    {
      Serial.print(buf[i], HEX);
      Serial.print("  ");
      Serial.print("\t");

    }
    unsigned int p_in = (buf[7] << 8) | buf [6];
    Serial.println(p_in);
  }
  delay(1);
}
[/code]

You are not getting any serial prints?

Thank you! My problem was with the library! The Video https://drive.google.com/file/d/1MCMsn4w1lAoWAaczPMCUCUlFrrpKCzD0/view
and code

#include <SPI.h>          //Library for using SPI Communication 
#include <mcp2515.h>      //Library for using CAN Communication

struct can_frame canMsg;
MCP2515 mcp2515(10);
int incomingByte = 1; // 1 стоп; 2 пуск;

void setup() 
{
  while (!Serial);
  Serial.begin(115200);
  
  mcp2515.reset();
  mcp2515.setBitrate(CAN_1000KBPS,MCP_8MHZ); //устанавливаем скорость шины CAN 500 кбит/с и частоту кварцевого генератора 8 МГц
  mcp2515.setNormalMode();

  Serial.print("Setup done\n\n");
}

void printserial(unsigned long ID, unsigned char buf[8]) {
  // преобразует buf[8] в строку символов 
    Serial.print(ID, HEX);
    Serial.print(": ");
    for (int i = 0; i < 8; i++)
    {
      Serial.print(buf[i], HEX);
      Serial.print("  ");
    }
}

void loop() 
{
  canMsg.can_id  = 0x141;           // устанавливаем CAN id
  canMsg.can_dlc = 0x08;
  
  if (2 == incomingByte) {
      canMsg.data[0] = 0xA2;
      canMsg.data[1] = 0x00;
      canMsg.data[2] = 0x00;
      canMsg.data[3] = 0x00;
      canMsg.data[4] = 0xA0;
      canMsg.data[5] = 0x86;
      canMsg.data[6] = 0x01;
      canMsg.data[7] = 0x00;
      Serial.print("Send(2): ");
  } else { if (3 == incomingByte) {
      canMsg.data[0] = 0xA2;
      canMsg.data[1] = 0x00;
      canMsg.data[2] = 0x00;
      canMsg.data[3] = 0x00;
      canMsg.data[4] = 0x40;
      canMsg.data[5] = 0xD0;
      canMsg.data[6] = 0x03;
      canMsg.data[7] = 0x00;
      Serial.print("Send(3): ");
  } else {
      canMsg.data[0] = 0x81;
      canMsg.data[1] = 0x00;
      canMsg.data[2] = 0x00;
      canMsg.data[3] = 0x00;
      canMsg.data[4] = 0x00;
      canMsg.data[5] = 0x00;
      canMsg.data[6] = 0x00;
      canMsg.data[7] = 0x00;
      Serial.print("Send(1): ");
  }}
  mcp2515.sendMessage(&canMsg);     //передаем CAN сообщение
  printserial(canMsg.can_id, canMsg.data);
  Serial.print("\n");

  int len = 10;
  while ((mcp2515.readMessage(&canMsg) != MCP2515::ERROR_OK))
  {
      /* проверка пришел ответ CAN */
      delay(1);
      len--;
      if ((len <= 0)) {
        break;
      }
  }

  if (len > 0) {
    // пришел ответ
    Serial.print("Recv   : ");
    printserial(canMsg.can_id, canMsg.data);
    Serial.print("\n");
  } else {
    // нет ответа
    Serial.print("Recv   : NO ANSWER");
    Serial.print("\n");
  }
  Serial.print("\n");

  int pause_3000 = 30;
  while (pause_3000) {
      if (Serial.available() > 0) {
        // read the incoming byte:
        incomingByte = Serial.read();
        incomingByte = incomingByte - 0x30; // "1" -> 1
        
        while (Serial.available() > 0) {
          Serial.read();
        }
        break;
      }

      pause_3000--;
      delay(100);
  }
}

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