Forum Posts

Matt NG
May 13, 2022
In Help and Getting Started
I’m having difficulties with controlling RMD X6 S2 motors using an Arduino micro controller. I have confirmed that the motors work through the GUI but am unable to make any motion by sending commands using the Arduino. Eventually I will be controlling 3 motors using joystick inputs but for now I am just trying to make a single motor move at a constant speed to confirm I have a functional system. Current code is copied below. I'm fairly confident that I'm sending the correct commands to the motor and am able to read responses from the motor. Despite sending the 0x77 open brake command I receive an error response from the motor saying brake is on. In the GUI there is an enable option which takes a second or two to process and results in an audible whine coming from the motor. I am unable to replicate this using anything from the commands list and I'm guessing my problem is related to this in some capacity. #include <mcp_can.h> #include <SPI.h> /*SAMD core*/ #ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE #define SERIAL SerialUSB #else #define SERIAL Serial #endif // Define Joystick connection pins #define UP A1 #define DOWN A3 #define LEFT A2 #define RIGHT A5 #define CLICK A4 //Define LED pins #define LED2 8 #define LED3 7 #define StepValue 30 const int SPI_CS_PIN = 10; MCP_CAN CAN(SPI_CS_PIN); // Set CS pin void setup() { SERIAL.begin(115200); delay(1000); while (CAN_OK != CAN.begin(CAN_1000KBPS)) // init can bus : baudrate = 1000k { SERIAL.println("CAN BUS Shield init fail"); SERIAL.println(" Init CAN BUS Shield again"); delay(100); } SERIAL.println("CAN BUS Shield init ok!"); //Initialize pins as necessary pinMode(UP, INPUT); pinMode(DOWN, INPUT); pinMode(LED2, OUTPUT); pinMode(LED3, OUTPUT); //Pull analog pins high to enable reading of joystick movements digitalWrite(UP, HIGH); digitalWrite(DOWN, HIGH); //Write LED pins low to turn them off by default digitalWrite(LED2, LOW); digitalWrite(LED3, LOW); } long genPos = 350000; bool startUp = false; void loop() { unsigned char len = 0; unsigned char buf[8]; if (startUp == false) { buf[0] = 0x76; buf[1] = 0x00; buf[2] = 0x00; buf[3] = 0x00; buf[4] = 0x00; buf[5] = 0x00; buf[6] = 0x00; buf[7] = 0x00; CAN.sendMsgBuf(141, 0, 8, buf); delay(5); SERIAL.print("Reset Command: "); SERIAL.print(" | "); for (int i = 0; i < 8; i++) // print the data { SERIAL.print(buf[i], HEX); SERIAL.print(" "); } SERIAL.println("\t"); buf[0] = 0x88; buf[1] = 0x00; buf[2] = 0x00; buf[3] = 0x00; buf[4] = 0x00; buf[5] = 0x00; buf[6] = 0x00; buf[7] = 0x00; CAN.sendMsgBuf(141, 0, 8, buf); delay(5); SERIAL.print("Runing Command: "); SERIAL.print(" | "); for (int i = 0; i < 8; i++) // print the data { SERIAL.print(buf[i], HEX); SERIAL.print(" "); } SERIAL.println("\t"); buf[0] = 0x77; buf[1] = 0x00; buf[2] = 0x00; buf[3] = 0x00; buf[4] = 0x00; buf[5] = 0x00; buf[6] = 0x00; buf[7] = 0x00; CAN.sendMsgBuf(141, 0, 8, buf); delay(5); SERIAL.print("Open Brake Command: "); SERIAL.print(" | "); for (int i = 0; i < 8; i++) // print the data { SERIAL.print(buf[i], HEX); SERIAL.print(" "); } SERIAL.println("\t"); startUp = true; buf[0] = 0x00; buf[1] = 0x00; buf[2] = 0x00; buf[3] = 0x00; buf[4] = 0x00; buf[5] = 0x00; buf[6] = 0x00; buf[7] = 0x00; delay (5000); } buf[0] = 0xA2; buf[1] = 0x00; buf[2] = 0x00; buf[3] = 0x00; buf[4] = genPos; buf[5] = genPos >> 8; buf[6] = genPos >> 16; buf[7] = genPos >> 24; CAN.sendMsgBuf(0x141, 0, 8, buf); delay(100); SERIAL.print("Sent Command: "); SERIAL.print(" | "); for (int i = 0; i < 8; i++) // print the data { SERIAL.print(buf[i], HEX); SERIAL.print(" "); } SERIAL.println("\t"); if (CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming { CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf unsigned long canId = CAN.getCanId(); SERIAL.println("-----------------------------"); SERIAL.print("Get data from ID: 0x"); SERIAL.println(canId, HEX); for (int i = 0; i < len; i++) // print the data { SERIAL.print(buf[i], HEX); SERIAL.print("\t"); } SERIAL.println(); } // delay(1000); // send data per 100ms } // END FILE
0
0
10

Matt NG

More actions