使用案例
这段代码是一个控制 MyCobot 机械臂的程序,主要包括对机械臂控制器的连接检测、机械臂的基本动作控制(如上电、停止、设置输出引脚、获取输入引脚状态)、以及关节角度的设定和检查等功能。程序还包括异常处理,以确保在发生错误时程序能够正确退出。
项目中的myCobotExample.cpp为使用案例,您可以根据需要在此基础上进行修改:
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(); // 上电,启动机械臂
// 等待 1 秒,以确保上电动作完成
mycobot::MyCobot::I().SleepSecond(1);
// 设置 IO 输出,设置 M5 上的引脚 2、5、26 输出高电平
mycobot::MyCobot::I().SetBasicOut(2, 1);
mycobot::MyCobot::I().SleepSecond(1);
mycobot::MyCobot::I().SetBasicOut(5, 1);
mycobot::MyCobot::I().SleepSecond(1);
mycobot::MyCobot::I().SetBasicOut(26, 1);
mycobot::MyCobot::I().SleepSecond(1);
// 读取 M5 的输入引脚状态(注释掉的代码)
// 第一次读取引脚 35 和 36 状态时会出现延迟
/*
for (int i = 0; i < 2; i++) {
std::cout << "35= " << mycobot::MyCobot::I().GetBasicIn(35) << std::endl;
mycobot::MyCobot::I().SleepSecond(1);
std::cout << "36= " << mycobot::MyCobot::I().GetBasicIn(36) << std::endl;
mycobot::MyCobot::I().SleepSecond(1);
}
*/
// 设置 atom 输出引脚 23、33 为高电平(注释掉的代码)
/*
mycobot::MyCobot::I().SetDigitalOut(23, 1);
mycobot::MyCobot::I().SleepSecond(1);
mycobot::MyCobot::I().SetDigitalOut(33, 1);
mycobot::MyCobot::I().SleepSecond(1);
*/
// 读取 atom 输入引脚状态(注释掉的代码)
// 第一次读取引脚 22 和 19 状态时会出现延迟
/*
for (int i = 0; i < 2; i++) {
std::cout << "22= " << mycobot::MyCobot::I().GetDigitalIn(22) << std::endl;
mycobot::MyCobot::I().SleepSecond(1);
std::cout << "19= " << mycobot::MyCobot::I().GetDigitalIn(19) << std::endl;
mycobot::MyCobot::I().SleepSecond(1);
}
*/
// 控制自适应夹爪的开合(注释掉的代码)
// 由于第一次会出现延迟,因此发送两次
/*
for (int i = 0; i < 2; i++) {
mycobot::MyCobot::I().SetGriper(1); // 打开夹爪
mycobot::MyCobot::I().SleepSecond(3);
mycobot::MyCobot::I().SetGriper(0); // 关闭夹爪
mycobot::MyCobot::I().SleepSecond(3);
}
*/
// 控制电动夹爪的开合(注释掉的代码)
// 由于第一次会出现延迟,因此发送两次
/*
for (int i = 0; i < 2; i++) {
mycobot::MyCobot::I().SetElectricGriper(1); // 打开电动夹爪
mycobot::MyCobot::I().SleepSecond(1);
mycobot::MyCobot::I().SetElectricGriper(0); // 关闭电动夹爪
mycobot::MyCobot::I().SleepSecond(1);
}
*/
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] << "]";
// 设置机械臂的目标角度,并以 180 度/秒的速度移动到目标位置
mycobot::Angles goal_angles = { 1, 0, 0, 0, 0, 0 };
mycobot::MyCobot::I().WriteAngles(goal_angles, 180);
// 等待机械臂移动到目标位置,并持续输出当前角度
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 毫秒
}
// 停止机械臂的所有动作(注释掉的代码)
// 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); // 异常退出程序
}
代码中的关键功能解释:
- 连接检测:首先检查 MyCobot 机械臂的控制器是否连接成功,未连接则退出程序。
- 机械臂上电:如果连接成功,机械臂上电并启动,随后设置机械臂上的输出引脚。
- IO 引脚控制:控制 M5 和 atom 的输入输出引脚,通过设置或读取这些引脚的电平来进行特定的硬件控制操作(如输出信号或读取状态)。
- 夹爪控制:通过注释掉的代码可以看到如何控制自适应夹爪和电动夹爪的开合。
- 关节角度控制:设置机械臂的目标角度,并监控其是否已经移动到目标位置,程序持续输出机械臂的