io控制
机械臂底部M5Stack-basic和末端Atom上都有引脚,可以通过io控制设置引脚的高低电平,控制系泵等工具,各个机械臂类型输入输出引脚号请看下面的说明表:
底部M5Stack-basic输入输出引脚说明表:
机械臂型号 | 输入引脚号 | 输出引脚号 |
---|---|---|
myCobot 280-M5 | 35、36 | 2、5、26 |
myCobot 320-M5 | 35、36 | 5、15 |
末端Atom输入输出引脚说明表:
机械臂型号 | 输入引脚号 | 输出引脚号 |
---|---|---|
myCobot 280-M5 | 19、22 | 23、33 |
myCobot 320-M5 | 无 | 无 |
1 basic io控制(m5)
1.1 设置输出io高低电平
SetBasicOut(int pin_number, int pin_signal)
返回值:无
参数说明:参数1:引脚号(basic输出引脚号),参数2:状态(0--低电平,1--高电平)
案例:
mycobot::MyCobot::I().SetBasicOut(2, 1);
1.2 获取输入io状态
GetBasicIn(int pin_number)
返回值:引脚状态(0--低电平,1--高电平)
参数说明:引脚号(basic输入引脚号)
案例:设置输出引脚2为高电平
mycobot::MyCobot::I().GetBasicIn(35);
2 Atom io控制
注意:320m5无atom io,所以不需要用到此模块API
2.1 设置输出io高低电平
SetDigitalOut(int pin_number, int pin_signal)
返回值:无
参数说明:参数1:引脚号(atom输出引脚号),参数2:状态(0--低电平,1--高电平)
案例:
mycobot::MyCobot::I().SetDigitalOut(23, 1);
2.2 获取输入io状态
GetDigitalIn(int pin_number)
返回值:引脚状态(0--低电平,1--高电平)
参数说明:引脚号(atom输入引脚号)
案例:
mycobot::MyCobot::I().GetDigitalIn(19);
3 完整使用案例
int main(int argc, char* argv[])
try {
QCoreApplication a(argc, argv);
using namespace std::chrono_literals;
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().SleepSecond(1);//需要等待1S,让前面的动作做完
//设置io输出,2、5、26为m5输出引脚
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);
}*/
//自适应夹爪 1--open 0--close 由于第一次有延迟,发送两次
/*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);
}*/
//电动夹爪 1-开 0-关 由于第一次有延迟,发送两次
/*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);
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] << "]";
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);
}
//mycobot::MyCobot::I().JogAngle(mycobot::Joint::J1, 1, 5);
std::this_thread::sleep_for(5000ms);
mycobot::MyCobot::I().StopRobot();*/
std::cout << "\n";
exit(EXIT_SUCCESS);
} catch (std::error_code&) {
std::cerr << "System error. Exiting.\n";
exit(EXIT_FAILURE);
} catch (...) {
std::cerr << "Unknown exception thrown. Exiting.\n";
exit(EXIT_FAILURE);
}