I am currently trying to control my robotic arm with my arduino using the uart interface. I have connected GND, Vin, Rx and Tx to the controller of the robot arm. I also got the library for the robot arm. But there were errors in it which I could correct. Now I wanted to start the program but it always breaks off at the same point, namely when he wants to perform the first movement. You can find the code example and more informations here
My Arduino sketch looks like
/*
* Use Serial2 serial port to communicate with the manipulator
*/
#include <Mirobot.h> // Include header file
UART_Mirobot myrobot; // Create a robotic arm object
void setup(){
Serial.begin(9600); // Initialize the serial port at the baud rate of 9600
myrobot.serialInit(2); // Set Serial2 to communicate with the manipulator at baud rate of 115200;
myrobot.setSerialMonitoring(ON); // Enabling Serial Port Monitoring
while(myrobot.getStatus() != Idle && myrobot.getStatus() != Alarm); // Determine whether the robotic arm can receive commands
myrobot.sendMsg("$H",OFF); // The robotic arm reset
while(myrobot.getStatus() != Idle); // Wait for the robotic arm to complete reset and become idle
myrobot.setMoveSpeed(3000); // Set the maximum movement speed of the manipulator at 3000
myrobot.moveJoints(-90,10,-90,60,10,10); // Control the manipulator to move 1-6 axis to (-90°, 10°, -90°, 60°, 10°, 10°) position
myrobot.zero(); // The manipulator moves to its initial position
myrobot.movePose(198,0,230.05,0,-60,0); // Control the end of the manipulator to move to XYZ(198,0,230.05),RPY(0,-60,0)position
myrobot.movePose(0,-100,-100,0,60,0,INC,MOVL); // Control the end of the manipulator in coordinate mode with relative position of linear interpolation motion
myrobot.movePose(250,0,100,0,0,0);
myrobot.moveArc(200,0,100,60,ABS,CCW); // control manipulator moves along a section of arc (inferior arc) with a radius of 60mm at the tangent point xyz (200,0,100) with the current coordinate point;
myrobot.gripper(ON); // gripper open
myrobot.timedPause(2.5); // waiting 2.5s
myrobot.gripper(OFF);
myrobot.zero();
myrobot.timedPause(0.5);
myrobot.runFile("test"); // Run the Gcode file "test.gcode" stored in the controller.
myrobot.homing(2);
}
void loop(){
}
The Mirobot library are
#include "Mirobot.h"
class HardwareSerial;
HardwareSerial *serial;
UART_Mirobot::UART_Mirobot(){
}
void UART_Mirobot::serialInit(int serialPort){
commMode = 0;
mirobotSerialPort = serialPort;
switch(mirobotSerialPort){
case 0: serial = &Serial; break;
case 1: serial = &Serial1; break;
case 2: serial = &Serial2; break;
case 3: serial = &Serial3; break;
default:serial = &Serial; break;
}
serial->begin(115200);
}
void UART_Mirobot::setSerialMonitoring(bool mode){
serialMonitoringMode = mode;
}
void UART_Mirobot::homing(int homingMode){
setCmdTimeout(30000);
switch(homingMode){
case 1: sendMsg("$H1\nF2000"); break;
case 2: sendMsg("$H2\nF2000"); break;
case 3: sendMsg("$H3\nF2000"); break;
case 4: sendMsg("$H4\nF2000"); break;
case 5: sendMsg("$H5\nF2000"); break;
case 6: sendMsg("$H6\nF2000"); break;
case 7: sendMsg("$H7\nF2000"); break;
default: sendMsg("$H\nF2000"); break;
}
setCmdTimeout(10000);
}
void UART_Mirobot::setMoveSpeed(int velocity){
mirobotVelocity = velocity;
sendMsg("F" + String(velocity));
}
void UART_Mirobot::setJumpPara(int height){
String strCmd = "$49=" + String(height);
sendMsg(strCmd);
}
void UART_Mirobot::timedPause(float seconds){
String strCmd = "G04 P";
String strPara = String(seconds,3);
sendMsg(strCmd + strPara);
}
void UART_Mirobot::zero(){
// sendMsg("$M");
sendMsg("M21 G90 G01 X0 Y0 Z0 A0 B0 C0");
}
void UART_Mirobot::moveJoints(float j1, float j2, float j3, float j4, float j5, float j6, bool motionMode){
String strCmd = "M21 G00 ";
if(motionMode) strCmd.concat("G90 ");
else strCmd.concat("G91 ");
String strParam = "X"+String(j1, 2)+" Y"+String(j2, 2)+" Z"+String(j3, 2)+" A"+String(j4, 2)+" B"+String(j5, 2)+" C"+String(j6, 2);
strCmd.concat(strParam);
sendMsg(strCmd);
}
void UART_Mirobot::movePose(float x, float y, float z, float rx, float ry, float rz, bool motionMode, int pathMode){
String strCmd = "M20 ";
switch(pathMode){
case MOVJ: strCmd.concat("G00 "); break;
case MOVL: strCmd.concat("G01 "); break;
case JUMP: strCmd.concat("G05 "); break;
default: strCmd.concat("G00 "); break;
}
if(motionMode) strCmd.concat("G90 ");
else strCmd.concat("G91 ");
String strParam = "X"+String(x, 2)+" Y"+String(y, 2)+" Z"+String(z, 2)+" A"+String(rx, 2)+" B"+String(ry, 2)+" C"+String(rz, 2);
strCmd.concat(strParam);
sendMsg(strCmd);
}
void UART_Mirobot::moveArc(float x, float y, float z, float r, bool motionMode, int pathMode){
String strCmd = "M20 ";
if(motionMode) strCmd.concat("G90 ");
else strCmd.concat("G91 ");
if(pathMode) strCmd.concat("G02 ");
else strCmd.concat("G03 ");
String strParam = "X"+String(x, 2)+" Y"+String(y, 2)+" Z"+String(z, 2)+" R"+String(r, 2);
strCmd.concat(strParam);
sendMsg(strCmd);
}
void UART_Mirobot::gripper(int gripperStatus){
if(gripperStatus) sendMsg("M3 S40");
else sendMsg("M3 S60");
}
void UART_Mirobot::softGripper(int softGripperStatus){
if(softGripperStatus == ON) sendMsg("M3 S1000");
else if(softGripperStatus == GRAB) sendMsg("M3 S500");
else sendMsg("M3 S0");
}
void UART_Mirobot::pump(int pumpStatus){
if(pumpStatus == ON) sendMsg("M3 S1000");
else if(pumpStatus == BLOW) sendMsg("M3 S500");
else sendMsg("M3 S0");
}
void UART_Mirobot::pwmWrite(int pwm){
String strPara = "S" + String(pwm);
sendMsg("M3 " + strPara);
}
void UART_Mirobot::moveConveyor(float d, bool motionMode){
sendMsg("$45=0");
String strCmd = "G01 ";
if(motionMode) strCmd.concat("G90 ");
else strCmd.concat("G91 ");
String strPara = "D " + String(d, 2);
sendMsg(strCmd + strPara);
}
void UART_Mirobot::moveRail(float d, bool motionMode){
sendMsg("$45=1");
String strCmd = "G01 ";
if(motionMode) strCmd.concat("G90 ");
else strCmd.concat("G91 ");
String strPara = "D " + String(d, 2);
sendMsg(strCmd + strPara);
}
void UART_Mirobot::setCmdTimeout(long cmdTimeout){
mirobotCmdTimeout = cmdTimeout;
}
void UART_Mirobot::sendMsg(String gcode, bool check){
if(commMode == 0){
while(serial->read() >= 0){};
if(serial == &Serial||serialMonitoringMode == 0) serial->println(gcode);
else{
serial->println(gcode);
Serial.println("Send message: " + gcode);
}
unsigned long rtime = millis();
strMsg = "";
while(strMsg.indexOf("ok")<0&&strMsg.indexOf("State")<0&&check&&strMsg.length()!=2){
//如果串口中有数据则全部读取
if(serial->available()) strMsg += serial->readString();
//如果等待结束符超时则跳出
if((millis()-rtime)>=(mirobotCmdTimeout)){
strMsg = "---Timeout---";
break;
}
delay(100);
}
if(serial != &Serial&&serialMonitoringMode == 1){
Serial.print("Mirobot: ");
Serial.println(strMsg);
}
}
else{
String rs485Cmd ="@" + address + gcode;
while(Serial1.read() >= 0){};
Serial1.println(rs485Cmd);
if(serialMonitoringMode == 1){
Serial.print("RS485 message: ");
Serial.println(rs485Cmd);
}
unsigned long rtime = millis();
strMsg = "";
while(strMsg.indexOf("ok")<0&&strMsg.indexOf("State")<0&&check&&strMsg.length()!=2){
//如果串口中有数据则全部读取
if(Serial1.available()) strMsg = Serial1.readString();
//如果等待结束符超时则跳出
if((millis()-rtime)>=(mirobotCmdTimeout)){
strMsg = "---Timeout---";
break;
}
delay(100);
}
if(serialMonitoringMode == 1){
Serial.print("Received: ");
Serial.println(strMsg);
}
}
}
int UART_Mirobot::getStatus(){
sendMsg("o103");
int Status;
if(strMsg.indexOf("Offline")>=0 || strMsg.indexOf("0")>=0) Status = Offline;
else if(strMsg.indexOf("Idle")>=0 || strMsg.indexOf("1")>=0) Status = Idle;
else if(strMsg.indexOf("Alarm")>=0 || strMsg.indexOf("2")>=0) Status = Alarm;
else if(strMsg.indexOf("Run")>=0 || strMsg.indexOf("5")>=0) Status = Run;
else if(strMsg.indexOf("Busy")>=0 || strMsg.indexOf("4")>=0) Status = Busy;
else Status = Offline;
return Status;
}
void UART_Mirobot::runFile(String fileName){
while(getStatus() != Idle && getStatus() != Alarm);
String strCmd = "o111" + fileName;
sendMsg(strCmd);
delay(1000);
while(getStatus() != Idle);
}
/*****************************************/
void RS485_Mirobot::rs485Init(int add, long baudRate){
commMode = 1;
address = String(add);
if(address.length()<2) address = "0" + address;
Serial1.begin(baudRate);
}
The header file is here:
#ifndef _MIROBOT_H_
#define _MIROBOT_H_
#include <Arduino.h>
#define MOVJ 1
#define MOVL 2
#define JUMP 3
#define ABS 1
#define INC 0
#define CW 1
#define CCW 0
#define ON 1
#define OFF 0
#define GRAB 2
#define BLOW 2
#define Offline 0
#define Idle 1
#define Alarm 2
#define Run 5
#define Busy 4
class UART_Mirobot{
public:
UART_Mirobot();
void serialInit(int serialPort = 0); //设置与Mirobot通信的串口(0/Serial; 1/Serial1; 2/Serial2; 3/Serial3)
void setSerialMonitoring(bool mode); //设置开启串口监视,通过Arduino串口监视器查看
void setCmdTimeout(long cmdTimeout); //设置每条指令发送后超时等待的时间(单位:毫秒ms)
void homing(int homingMode = 0); //控制机械臂归零(0/$H:快速归零;1/$H1:1轴归零;2/$H2:2轴归零;3/$H3:3轴归零;4/$H4:4轴归零;
//5/$H5:5轴归零;6/$H6:6轴归零(需对应硬件支持);7/$H7:滑轨归零;
void zero(); //机械臂初始位置
void setMoveSpeed(int velocity); //设置机械臂运动速度
void setJumpPara(int height); //设置JUMP门型轨迹抬起高度
void timedPause(float seconds); //暂停等待(单位:秒s)
void moveJoints(float j1, float j2, float j3, float j4, float j5, float j6, bool motionMode = ABS); //角度模式运动
void movePose(float x, float y, float z, float rx, float ry, float rz, bool motionMode = ABS, int pathMode = MOVJ); //坐标模式运动
void moveArc(float x, float y, float z, float r, bool motionMode = ABS, int pathMode = CW); //圆弧插补运动
void gripper(int gripperStatus); //舵机夹爪状态
void softGripper(int softGripperStatus); //三指柔爪状态
void pump(int pumpStatus); //气泵状态
void pwmWrite(int pwm); //pwm(控制器黄色接口)输出,范围:0~1000
void moveConveyor(float d, bool motionMode = ABS); //传送带运动控制
void moveRail(float d, bool motionMode = ABS); //滑轨运动控制
int getStatus(); //获取机械臂工作状态,0/1/2/3/4/5: 0机械臂离线 1正常 2锁定 3复位中 4设备忙 5运行中
void runFile(String fileName); //运行控制器中存储的Gcode文件
void sendMsg(String gcode, bool check = ON);
protected:
bool commMode;
int mirobotSerialPort = 0;
bool serialMonitoringMode = 1;
int mirobotVelocity = 2000;
long mirobotCmdTimeout = 10000L;
void mirobotReceived();
String strMsg = "";
String address;
};
class RS485_Mirobot: public UART_Mirobot{
public:
void rs485Init(int add = 1, long baudRate = 115200);
};
#endif
This is how i connected my Arduino with the Controller of the Robot arm: Controller with the UART Port Colors: Brown = 5V, White = GND, Yellow = Rx, Green = Tx
The Contoller is connected to the robotic arm with an IDC Cable (but with the uart wire and without the usb wire)4
This is how the Robotic arm looks like
What I tried so far: I tried switching the uart ports on the arduino (Rx1,Tx1,Rx3,Tx3 etc.) I also tried to do a simple sketch with 1-2 moving actions but that didnt work either.
[This ist how the serial monitoring looks like]. Luckly the robot arm can communicate with my arduino but at some point the communication cuts off.6 The Robotic arm sends the info in which state he is right now like Idle, Offline, Alarm or Run.
What is expected: the robot should correctly execute the commands given to it by the Arduino.
The result: The robot always stops at the same place and then only gives timeouts and he doesnt execute any commands given to him.
If you need the function descriptions for the Robotic arm with arduino feel free to contact me bc I cant upload pdf-files on stackoverflow!
If you have any questions or u didnt understand something feel free to ask me!!
EDIT: Forgot to say that there is a "error, sensor data checksum error!"
Should i try replacing the uart cable with another one?
Sry for my bad english!!