-
Notifications
You must be signed in to change notification settings - Fork 2
/
servoj_example.cpp
43 lines (35 loc) · 1.14 KB
/
servoj_example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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
double velocity = 0.5;
double acceleration = 0.5;
double dt = 1.0/500; // 2ms
double lookahead_time = 0.1;
double gain = 300;
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 2 seconds, each cycle is ~2ms
for (unsigned int i=0; i<1000; i++)
{
auto t_start = high_resolution_clock::now();
rtde_control.servoJ(joint_q, velocity, acceleration, dt, lookahead_time, gain);
joint_q[0] += 0.001;
joint_q[1] += 0.001;
auto t_stop = high_resolution_clock::now();
auto t_duration = std::chrono::duration<double>(t_stop - t_start);
if (t_duration.count() < dt)
{
std::this_thread::sleep_for(std::chrono::duration<double>(dt - t_duration.count()));
}
}
rtde_control.servoStop();
rtde_control.stopScript();
return 0;
}