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]