forked from kektobiologist/motion-simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller-wrapper.cpp
112 lines (104 loc) · 3.86 KB
/
controller-wrapper.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
#include "controller-wrapper.hpp"
#define PREDICTION_PACKET_DELAY 4
MiscData ControllerWrapper::genControls_(Pose s, Pose e, int &vl, int &vr, double finalVel) {
assert(ctrlType == POINTCTRL);
Pose x = s;
for(deque<pair<int,int> >::iterator it = uq.begin(); it != uq.end(); it++) {
x.updateNoDelay(it->first, it->second, timeLC);
}
// int prevSpeed = max(fabs(prevVl), fabs(prevVr));
double prevSpeed = (prevVl+prevVr)/2;
double prevOmega = (prevVr- prevVl)/(Constants::d);
MiscData m = (*fun)(x, e, vl, vr, prevSpeed,prevOmega, finalVel);
prevVl = vl; prevVr = vr;
uq.push_back(make_pair<int,int>((int)vl, (int)vr));
if(uq.size() > PREDICTION_PACKET_DELAY )
uq.pop_front();
// qDebug() << "herevsd" << uq.size() << endl;
return m;
}
MiscData ControllerWrapper::genControls_(Pose s, int &vl, int &vr, double time, bool useTime) {
assert(ctrlType == TRACKCTRL);
Pose x = s;
for(deque<pair<int,int> >::iterator it = uq.begin(); it != uq.end(); it++) {
x.updateNoDelay(it->first, it->second, timeLC);
}
if (isFirstCall) {
isFirstCall = false;
gettimeofday(&startTime, NULL);
}
double elapsedS;
if (!useTime) {
struct timeval nowTime;
gettimeofday(&nowTime, NULL);
elapsedS = (nowTime.tv_sec-startTime.tv_sec)+(nowTime.tv_usec-startTime.tv_usec)/1000000.0;
} else {
elapsedS = time;
}
// should offset elapsedS by the number of packet delay!!!
elapsedS += k*timeLCMs/1000.;
MiscData m = tracker.genControls(x, vl, vr, prevVl, prevVr, elapsedS);
prevVl = vl; prevVr = vr;
uq.push_back(make_pair<int,int>((int)vl, (int)vr));
uq.pop_front();
return m;
}
Pose ControllerWrapper::getNewStartPose(){
double elapsedS;
struct timeval nowTime;
gettimeofday(&nowTime, NULL);
elapsedS = (nowTime.tv_sec-startTime.tv_sec)+(nowTime.tv_usec-startTime.tv_usec)/1000000.0;
return tracker.getNewStartPose(elapsedS);
}
ControllerWrapper::ControllerWrapper(FType fun, int start_vl, int start_vr, int k):fun(fun), k(k), ctrlType(POINTCTRL), tracker(),
startTime(), isFirstCall(true){
for(int i = 0; i < k; i++)
uq.push_back(make_pair<int,int>((int)start_vl,(int)start_vr));
prevVl = prevVr = 0;
}
ControllerWrapper::ControllerWrapper(Trajectory *traj, int start_vl, int start_vr, int k):k(k), ctrlType(TRACKCTRL), tracker(traj),
startTime(), isFirstCall(true){
for(int i = 0; i < k; i++)
uq.push_back(make_pair<int,int>((int)start_vl,(int)start_vr));
prevVl = prevVr = 0;
}
void ControllerWrapper::reset() {
isFirstCall = true;
}
void ControllerWrapper::setTraj(Trajectory* traj) {
tracker.setTraj(traj);
reset();
}
Pose ControllerWrapper::getPredictedPose(Pose s) {
Pose x = s;
for(deque<pair<int,int> >::iterator it = uq.begin(); it != uq.end(); it++) {
x.updateNoDelay(it->first, it->second, timeLC);
}
return x;
}
double ControllerWrapper::getCurrentTimeS() const
{
if (isFirstCall)
return 0;
struct timeval nowTime;
gettimeofday(&nowTime, NULL);
double elapsedS = (nowTime.tv_sec-startTime.tv_sec)+(nowTime.tv_usec-startTime.tv_usec)/1000000.0;
return elapsedS;
}
MiscData ControllerWrapper::genControls(Pose s, Pose e, int &vl, int &vr, double finalVel) {
if (ctrlType == POINTCTRL) {
return genControls_(s, e, vl, vr, finalVel);
} else {
return genControls_(s, vl, vr);
}
}
pair<int, int> ControllerWrapper::getDelayedVel()
{
//qDebug() << "cfswdvgewsr" << uq.size() << "dfwgv" << endl;
return uq.front();
}
MiscData ControllerWrapper::genControlsTrajSim(Pose s, int &vl, int &vr, double t)
{
assert (ctrlType == TRACKCTRL);
return genControls_(s, vl, vr, t, true);
}