forked from TurBoss/turbo-grua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpolation.cpp
151 lines (125 loc) · 2.88 KB
/
interpolation.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "interpolation.h"
void Interpolation::setCurrentPos(float px, float py, float pz, float pe) {
Point p;
p.xmm = px;
p.ymm = py;
p.zmm = pz;
p.emm = pe;
setCurrentPos(p);
}
void Interpolation::setInterpolation(float px, float py, float pz, float pe, float v) {
Point p;
p.xmm = px;
p.ymm = py;
p.zmm = pz;
p.emm = pe;
setInterpolation(p, v);
}
void Interpolation::setInterpolation(float p1x, float p1y, float p1z, float p1e, float p2x, float p2y, float p2z, float p2e, float v) {
Point p1;
Point p2;
p1.xmm = p1x;
p1.ymm = p1y;
p1.zmm = p1z;
p1.emm = p1e;
p2.xmm = p2x;
p2.ymm = p2y;
p2.zmm = p2z;
p2.emm = p2e;
setInterpolation(p1, p2, v);
}
void Interpolation::setCurrentPos(Point p) {
xStartmm = p.xmm;
yStartmm = p.ymm;
zStartmm = p.zmm;
eStartmm = p.emm;
xDelta = 0;
yDelta = 0;
zDelta = 0;
eDelta = 0;
}
void Interpolation::setInterpolation(Point p1, float v) {
Point p0;
p0.xmm = xStartmm + xDelta;
p0.ymm = yStartmm + yDelta;
p0.zmm = zStartmm + zDelta;
p0.emm = eStartmm + eDelta;
setInterpolation(p0, p1, v);
}
void Interpolation::setInterpolation(Point p0, Point p1, float av) {
v = av; //mm/s
float a = (p1.xmm - p0.xmm);
float b = (p1.ymm - p0.ymm);
float c = (p1.zmm - p0.zmm);
float e = abs(p1.emm - p0.emm);
float dist = sqrt(a*a + b*b + c*c);
if (dist < e) {
dist = e;
}
if (v < 5) { //includes 0 = default value
v = sqrt(dist) * 10; //set a good value for v
}
if (v < 5) {
v = 5;
}
tmul = v / dist;
xStartmm = p0.xmm;
yStartmm = p0.ymm;
zStartmm = p0.zmm;
eStartmm = p0.emm;
xDelta = (p1.xmm - p0.xmm);
yDelta = (p1.ymm - p0.ymm);
zDelta = (p1.zmm - p0.zmm);
eDelta = (p1.emm - p0.emm);
state = 0;
startTime = micros();
}
void Interpolation::updateActualPosition() {
if (state != 0) {
return;
}
long microsek = micros();
float t = (microsek - startTime) / 1000000.0;
//ArcTan Approx.
/*float progress = atan((PI * t * tmul) - (PI * 0.5)) * 0.5 + 0.5;
if (progress >= 1.0) {
progress = 1.0;
state = 1;
}*/
//Cosin Approx.
float progress = -cos(t * tmul * PI) * 0.5 + 0.5;
if ((t * tmul) >= 1.0) {
progress = 1.0;
state = 1;
}
xPosmm = xStartmm + progress * xDelta;
yPosmm = yStartmm + progress * yDelta;
zPosmm = zStartmm + progress * zDelta;
ePosmm = eStartmm + progress * eDelta;
}
bool Interpolation::isFinished() const {
return state != 0;
}
float Interpolation::getXPosmm() const {
return xPosmm;
}
float Interpolation::getYPosmm() const {
return yPosmm;
}
float Interpolation::getZPosmm() const {
return zPosmm;
}
float Interpolation::getEPosmm() const {
return ePosmm;
}
Point Interpolation::getPosmm() const {
Point p;
p.xmm = xPosmm;
p.ymm = yPosmm;
p.zmm = zPosmm;
p.emm = ePosmm;
return p;
}
float Interpolation::getDistance() const {
return 0.0;
}