坐标控制
坐标控制是让机械臂以指定姿态移动到指定点,分为x,y,z,rx,ry,rz。X,Y,Z 表示的是机械臂头部在空间中的位置(该坐标系为直角坐标系),rx,ry,rz表示的是机械臂头部在该点的姿态(该坐标系为欧拉坐标)。
单参数控制
发送单参数坐标
WriteCoord(Axis axis, double value, int speed = DefaultSpeed)
返回值:无
参数说明:参数1:坐标号(Axis枚举类型,int:1-6(X-RZ)),参数2:坐标(X、Y、Z取值范围 -300-300.00 单位 mm RX、RY、RZ,取值范围-180-180),参数3:速度(0-100),默认为30
示例:
mycobot::MyCobot::I().WriteCoord(mycobot::Axis::X, 10, 30);
多参数控制
获取全部坐标
GetCoords()
返回值:Coords类型
参数说明:无
示例:
mycobot::Coords coords = mycobot::MyCobot::I().GetCoords();
发送全部坐标
WriteCoords(const Coords& coords, int speed = DefaultSpeed)
返回值:无
参数说明:参数1:坐标(X、Y、Z取值范围 -300-300.00 单位 mm RX、RY、RZ,取值范围-180-180)
参数2:速度(0-100),默认为30
示例:
mycobot::Coords goal_coords = { 5, 5, 5, 5, 5, 5 };
mycobot::MyCobot::I().WriteCoords(goal_coords, 30);
完整使用案例
下面这段代码是一个控制 MyCobot 机械臂的控制程序的主要入口。在这个程序中,程序首先检查控制器是否连接到机械臂。如果连接成功,程序会将机械臂上电并停止当前的动作。接下来,它会获取并输出机械臂的当前角度和坐标,并尝试将所有关节移动到指定的目标角度。程序在目标角度就位前,会持续输出当前的角度信息。最后,程序对某个关节进行角度调整,并在调整后停止机械臂的动作。如果过程中发生错误,程序会捕获异常并退出。
int main(int argc, char* argv[])
try {
QCoreApplication a(argc, argv); // 创建一个 QCoreApplication 对象以支持 Qt 事件循环
using namespace std::chrono_literals; // 引入用于时间处理的字面量(如 200ms)
// 检查机械臂控制器是否连接
if (!mycobot::MyCobot::I().IsControllerConnected()) {
std::cerr << "Robot is not connected\n"; // 如果未连接,输出错误信息
exit(EXIT_FAILURE); // 退出程序,返回失败状态
}
std::cout << "Robot is connected\n"; // 输出连接成功的信息
mycobot::MyCobot::I().PowerOn(); // 上电,启动机械臂
mycobot::MyCobot::I().StopRobot(); // 停止机械臂的所有动作
// 检查机械臂是否正在移动,并输出状态
std::cout << "Robot is moving: " << mycobot::MyCobot::I().IsMoving() << "\n";
// 获取当前机械臂各关节的角度
mycobot::Angles angles = mycobot::MyCobot::I().GetAngles();
std::this_thread::sleep_for(200ms); // 延迟 200 毫秒
// 获取当前机械臂末端的位置坐标
mycobot::Coords coords = mycobot::MyCobot::I().GetCoords();
// 再次获取各关节的角度
angles = mycobot::MyCobot::I().GetAngles();
// 输出各关节的角度
std::cout << "[" << angles[mycobot::J1] << ", " << angles[mycobot::J2] << ", "
<< angles[mycobot::J3] << ", " << angles[mycobot::J4] << ", "
<< angles[mycobot::J5] << ", " << angles[mycobot::J6] << "]";
// 定义一个目标角度数组,所有关节移动到 5 度
mycobot::Angles goal_angles = { 5, 5, 5, 5, 5, 5 };
mycobot::MyCobot::I().WriteAngles(goal_angles); // 将机械臂移动到目标角度
// 等待机械臂移动到目标位置
while (!mycobot::MyCobot::I().IsInPosition(goal_angles, false)) {
angles = mycobot::MyCobot::I().GetAngles(); // 获取当前角度
std::cout << "[" << angles[mycobot::J1] << ", " << angles[mycobot::J2] << ", "
<< angles[mycobot::J3] << ", " << angles[mycobot::J4] << ", "
<< angles[mycobot::J5] << ", " << angles[mycobot::J6] << "]" << std::flush;
std::this_thread::sleep_for(200ms); // 每次循环延迟 200 毫秒
}
// 调整 J1 关节角度,增量为 1 度,速度为 5
mycobot::MyCobot::I().JogAngle(mycobot::Joint::J1, 1, 5);
std::this_thread::sleep_for(5000ms); // 延迟 5 秒
mycobot::MyCobot::I().StopRobot(); // 停止机械臂的所有动作
std::cout << "\n"; // 输出一个换行符
exit(EXIT_SUCCESS); // 正常退出程序
} catch (std::error_code&) {
// 捕获 std::error_code 异常,并输出错误信息
std::cerr << "System error. Exiting.\n";
exit(EXIT_FAILURE); // 异常退出程序
} catch (...) {
// 捕获所有其他异常,并输出错误信息
std::cerr << "Unknown exception thrown. Exiting.\n";
exit(EXIT_FAILURE); // 异常退出程序
}