Forcemode Example
这个例子将开始以-10N在z轴上向下移动机器人2秒,然后以10N在z轴上向上移动2秒。
#include <ur_rtde/rtde_control_interface.h>
#include <thread>
#include <chrono>
using namespace ur_rtde;
using namespace std::chrono;
int main(int argc, char* argv[])
{
RTDEControlInterface rtde_control("127.0.0.1");
// Parameters
std::vector<double> task_frame = {0, 0, 0, 0, 0, 0}; //一个姿势向量,定义了相对于基本框架的力框架。
std::vector<int> selection_vector = {0, 0, 1, 0, 0, 0};//一个由0s和1s组成的6d矢量。1表示机器人将在任务框架的相应轴上柔顺。
std::vector<double> wrench_down = {0, 0, -10, 0, 0, 0};//机器人将对其环境施加的力/力矩。机器人会沿着/围绕柔顺轴调整其位置,以达到指定的力/扭矩。数值对非柔顺轴没有影响。
std::vector<double> wrench_up = {0, 0, 10, 0, 0, 0};
int force_type = 2;//指定机器人如何解释力的坐标系,详见API
double dt = 1.0/500; // 2ms,表示控制循环的时间步长
std::vector<double> limits = {2, 2, 1.5, 1, 1, 1};//6d向量。对于柔顺轴,这些值是沿轴/绕轴允许的最大tcp速度。对于非柔顺轴,这些值是实际tcp位置与程序设置的位置之间沿轴/关于轴的最大允许偏差。
std::vector<double> joint_q = {-1.54, -1.83, -2.28, -0.59, 1.60, 0.023};
// Move to initial joint position with a regular moveJ
rtde_control.moveJ(joint_q);
// Execute 500Hz control loop for a total of 4 seconds, each cycle is ~2ms
for (unsigned int i=0; i<2000; i++)
{
steady_clock::time_point t_start = rtde_control.initPeriod();
// First we move the robot down for 2 seconds, then up for 2 seconds
if (i > 1000)
rtde_control.forceMode(task_frame, selection_vector, wrench_up, force_type, limits);
else
rtde_control.forceMode(task_frame, selection_vector, wrench_down, force_type, limits);
rtde_control.waitPeriod(t_start);
}
rtde_control.forceModeStop();
rtde_control.stopScript();
return 0;
}