-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathLinearTrajectoryThread.cpp
123 lines (95 loc) · 3.01 KB
/
LinearTrajectoryThread.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
#include "LinearTrajectoryThread.hpp"
#include <algorithm>
#include <functional>
#include <yarp/os/LogStream.h>
#include <yarp/os/Time.h>
#include <kdl/path_line.hpp>
#include <kdl/rotational_interpolation_sa.hpp>
#include <kdl/trajectory_segment.hpp>
#include <kdl/velocityprofile_rect.hpp>
#include "KdlVectorConverter.hpp"
using namespace roboticslab;
LinearTrajectoryThread::LinearTrajectoryThread(int _period, ICartesianControl * _iCartesianControl)
: yarp::os::PeriodicThread(_period * 0.001),
period(_period),
iCartesianControl(_iCartesianControl),
trajectory(nullptr),
startTime(0.0),
usingStreamingCommandConfig(false),
usingTcpFrame(false)
{}
LinearTrajectoryThread::~LinearTrajectoryThread()
{
delete trajectory;
trajectory = nullptr;
}
bool LinearTrajectoryThread::checkStreamingConfig()
{
std::map<int, double> params;
if (!iCartesianControl->getParameters(params))
{
yWarning() << "getParameters failed";
return false;
}
usingStreamingCommandConfig = params.find(VOCAB_CC_CONFIG_STREAMING_CMD) != params.end();
return true;
}
bool LinearTrajectoryThread::configure(const std::vector<double> & vels)
{
if (usingStreamingCommandConfig && !iCartesianControl->setParameter(VOCAB_CC_CONFIG_STREAMING_CMD, VOCAB_CC_MOVI))
{
yWarning() << "Unable to preset streaming command";
return false;
}
if (usingTcpFrame)
{
std::vector<double> deltaX(vels.size());
std::transform(vels.begin(), vels.end(), deltaX.begin(), std::bind1st(std::multiplies<double>(), period / 1000.0));
mtx.lock();
this->deltaX = deltaX;
mtx.unlock();
return true;
}
std::vector<double> x;
if (!iCartesianControl->stat(x))
{
yError() << "stat failed";
return false;
}
KDL::Frame H = KdlVectorConverter::vectorToFrame(x);
KDL::Twist tw = KdlVectorConverter::vectorToTwist(vels);
mtx.lock();
if (trajectory)
{
// discard previous state
delete trajectory;
trajectory = nullptr;
}
KDL::Path * path = new KDL::Path_Line(H, tw, new KDL::RotationalInterpolation_SingleAxis(), 1.0);
KDL::VelocityProfile * profile = new KDL::VelocityProfile_Rectangular(10.0);
profile->SetProfileDuration(0.0, 10.0, 10.0 / path->PathLength());
trajectory = new KDL::Trajectory_Segment(path, profile);
startTime = yarp::os::Time::now();
mtx.unlock();
return true;
}
void LinearTrajectoryThread::run()
{
if (usingTcpFrame)
{
std::vector<double> deltaX;
mtx.lock();
deltaX = this->deltaX;
mtx.unlock();
iCartesianControl->movi(deltaX);
}
else
{
mtx.lock();
KDL::Frame H = trajectory->Pos(yarp::os::Time::now() - startTime);
mtx.unlock();
std::vector<double> position = KdlVectorConverter::frameToVector(H);
iCartesianControl->movi(position);
}
}