-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathrobotmodel.cpp
217 lines (198 loc) · 6.06 KB
/
robotmodel.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "robotmodel.h"
#include <QTime>
#include <cmath>
#include <iostream>
#include "defaultvalues.h"
#define PI 3.14159265
RobotModel::RobotModel(QObject *parent) : QObject(parent)
{
lastUpdateTime = QTime::currentTime().msecsSinceStartOfDay();
currentPosition = DefaultValues::getMachineCoordinates();
for(int i = 0; i < COORDINATES_COUNT; i++) {
jointsSpeeds.append(DEFAULT_JOINTS_SPEED);
}
}
void RobotModel::forceSetCurrentPosition(MachineCoordinates newCoordinates)
{
currentPosition = newCoordinates;
}
void RobotModel::appendMachineCoordinate(MachineCoordinates newCoordinates)
{
if(coordinatesQueue.isEmpty()) {
lastUpdateTime = QTime::currentTime().msecsSinceStartOfDay();
}
coordinatesQueue.append(newCoordinates);
//std::cout << "RobotModel::appendMachineCoordinate: queue size: " << coordinatesQueue.size() << std::endl;
}
void RobotModel::clearMachineCoordinatesQueue()
{
coordinatesQueue.clear();
}
void RobotModel::setJointsSpeeds(QVector<double> speeds)
{
jointsSpeeds = speeds;
}
void RobotModel::setJointsSpeed(double speed)
{
jointsSpeeds.clear();
for(int i = 0; i < COORDINATES_COUNT; i++) {
jointsSpeeds.append(speed);
}
}
void RobotModel::setParameters(MachineParameters parameters)
{
machineParameters = parameters;
}
MachineCoordinates RobotModel::getCurrentPosition()
{
return currentPosition;
}
QVector<Point> RobotModel::getCurrentJointsPoints()
{
return getJointsPoints(currentPosition);
}
QVector<Point> RobotModel::getJointsPoints(MachineCoordinates coords)
{
QVector<Point> jointsPoints;
Point p;
double x, y, z;
x = y = z = 0;
double C1, S1, C2, S2, C5, S5, C23, S23, C234, S234, C15, S15/*, Ctheta, Stheta, Cpsi, Spsi*/;
C1 = cos(deg2rad(coords.f_1));
S1 = sin(deg2rad(coords.f_1));
C2 = cos(deg2rad(coords.f_2));
S2 = sin(deg2rad(coords.f_2));
C5 = cos(deg2rad(coords.f_5));
S5 = sin(deg2rad(coords.f_5));
C23 = cos(deg2rad(coords.f_2 + coords.f_3));
S23 = sin(deg2rad(coords.f_2 + coords.f_3));
C234 = cos(deg2rad(coords.f_2 + coords.f_3 + coords.f_4));
S234 = sin(deg2rad(coords.f_2 + coords.f_3 + coords.f_4));
C15 = cos(deg2rad(coords.f_1 + coords.f_5));
S15 = sin(deg2rad(coords.f_1 + coords.f_5));
//Ctheta = cos(deg2rad(approachVector.theta));
//Stheta = sin(deg2rad(approachVector.theta));
//Cpsi = cos(deg2rad(approachVector.psi));
//Spsi = sin(deg2rad(approachVector.psi));
// P0
p = Point(0,0,machineParameters.h);
jointsPoints.append(p);
// P1
p = Point(p.x + x, p.y + y, p.z + z);
jointsPoints.append(p);
// P2
x = machineParameters.l_1 * C1;
y = machineParameters.l_1 * S1;
z = 0;
p = Point(p.x + x, p.y + y, p.z + z);
jointsPoints.append(p);
// P3
x = machineParameters.d * S1;
y = -machineParameters.d * C1;
z = 0;
p = Point(p.x + x, p.y + y, p.z + z);
jointsPoints.append(p);
// P4
x = machineParameters.l_2 * C1 * C2;
y = machineParameters.l_2 * S1 * C2;
z = machineParameters.l_2 * S2;
p = Point(p.x + x, p.y + y, p.z + z);
jointsPoints.append(p);
// P5
x = -(machineParameters.d - machineParameters.e) * S1;
y = (machineParameters.d - machineParameters.e) * C1;
z = 0;
p = Point(p.x + x, p.y + y, p.z + z);
jointsPoints.append(p);
// P6
x = machineParameters.l_3 * C23 * C1;
y = machineParameters.l_3 * S1 * C23;
z = machineParameters.l_3 * S23;
p = Point(p.x + x, p.y + y, p.z + z);
jointsPoints.append(p);
// P7
x = machineParameters.l_4 * C234 * C1;
y = machineParameters.l_4 * C234 * S1;
z = machineParameters.l_4 * S234;
Point p7 = p = Point(p.x + x, p.y + y, p.z + z);
jointsPoints.append(p);
// P8
x = machineParameters.l_5 * (C1 * C234 * C5 - S1 * S5);
y = machineParameters.l_5 * (S1 * C234 * C5 + C1 * S5);
z = machineParameters.l_5 * S234 * C5;
p = Point(p.x + x, p.y + y, p.z + z);
jointsPoints.append(p);
// P9
/*double l = machineParameters.l_5 + machineParameters.l_6;
x = l * Ctheta * Cpsi;
y = l * Ctheta * Spsi;
z = l * Stheta;
p = Point(p.x + x, p.y + y, p.z + z);*/
x = machineParameters.l_6 * (C1 * C234 * C5 - S1 * S5);
y = machineParameters.l_6 * (S1 * C234 * C5 + C1 * S5);
z = machineParameters.l_6 * S234 * C5;
p = Point(p.x + x, p.y + y, p.z + z);
//P8
//P8 calculations using machine coordinates only doesn't work (it has to do something with
//deltas, I don't have time to dig into that, let's cheat a bit)
//P8 is average of point P7 and P9
/*x = (p7.x * machineParameters.l_5 + p.x * machineParameters.l_6) / l;
y = (p7.y * machineParameters.l_5 + p.y * machineParameters.l_6) / l;
z = (p7.z * machineParameters.l_5 + p.z * machineParameters.l_6) / l;
Point p8 = Point(x,y,z);
jointsPoints.append(p8);*/
jointsPoints.append(p);
//int i = 0;
/*std::cout << "RobotModel: joints points: " << std::endl;
for(Point pt : jointsPoints) {
std::cout << "P" << i << " " << pt << std::endl;
i++;
}
std::cout << "------" << std::endl;*/
return jointsPoints;
}
void RobotModel::update()
{
if(coordinatesQueue.isEmpty()) {
return;
}
MachineCoordinates desiredPosition = coordinatesQueue.first();
int dt = QTime::currentTime().msecsSinceStartOfDay() - lastUpdateTime; // in ms
for(int i = 0; i < COORDINATES_COUNT; i++) {
double maxAngularMovement = jointsSpeeds.at(i) * (double) dt / 1000.0;
double desiredAngularMovement = desiredPosition.getFi(i) - currentPosition.getFi(i);
if(fabs(desiredAngularMovement) <= maxAngularMovement) {
currentPosition.setFi(i, desiredPosition.getFi(i));
} else {
if(fabs(desiredAngularMovement) > 180) {
maxAngularMovement = -maxAngularMovement;
}
float sign = 1.0;
if(desiredAngularMovement < 0.0)
sign = -1.0;
double currentFi = currentPosition.getFi(i) + sign * maxAngularMovement;
if(currentFi < -180) {
currentFi += 360;
}
if(currentFi > 180) {
currentFi -= 360;
}
currentPosition.setFi(i, currentFi);
}
}
bool removeFromQueue = true;
for(int i = 0; i < COORDINATES_COUNT; i++) {
if(currentPosition.getFi(i) != desiredPosition.getFi(i)) {
removeFromQueue = false;
break;
}
}
if(removeFromQueue) {
coordinatesQueue.removeFirst();
}
lastUpdateTime = QTime::currentTime().msecsSinceStartOfDay();
}
double RobotModel::deg2rad(double deg)
{
return (PI * deg / 180.0);
}