-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbaserobot.cpp
387 lines (333 loc) · 13.7 KB
/
baserobot.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include "baserobot.h"
#include "elemento.h"
#include "efectorfinal.h"
BaseRobot::BaseRobot() : QObject() {
}
BaseRobot::~BaseRobot(){
}
Qt3DCore::QEntity *BaseRobot::init(){
// Root entity
Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();
// Montaje de la escena
this->p1 = new Elemento(rootEntity, QUrl(QStringLiteral("qrc:/assets/pieza1.obj")));
this->p1->setVel(0.1);
this->p2 = new Elemento(rootEntity, QUrl(QStringLiteral("qrc:/assets/pieza2.obj")));
this->p2->setPoint(0, QVector3D(0.65f, 0.0f, 0.1f));
this->p2->setVel(0.1);
this->p3 = new Elemento(rootEntity, QUrl(QStringLiteral("qrc:/assets/pieza3.obj")));
this->p3->setPoint(0, QVector3D(0.65f, 0.0f, 0.1f));
this->p3->setVel(0.1);
this->p4 = new Elemento(rootEntity, QUrl(QStringLiteral("qrc:/assets/pieza4.obj")));
this->p4->setPoint(0, QVector3D(0.65f, 0.0f, 0.1f));
this->p4->setVel(0.1);
this->ef = new EfectorFinal(rootEntity, QUrl(QStringLiteral("qrc:/assets/pieza5.obj")));
this->ef->setPoint(0, QVector3D(0.65f, 0.0f, 0.1f));
this->ef->setVel(0.1);
return rootEntity;
}
void BaseRobot::loadProgram(const QString &msg){
std::ifstream file;
file.open(msg.toStdString());
std::cout << "Loading program..." << std::endl;
std::string comando;
if (file.is_open()){
while (std::getline(file, comando)){
std::cout << comando << std::endl;
this->instruct.push(comando);
}
file.close();
} else {
std::cout << "PROBLEM OPENING FILE" << std::endl;
}
this->lastProgram = msg;
this->start();
}
void BaseRobot::executeLastProgram(){
if (this->lastProgram != nullptr){
this->loadProgram(this->lastProgram);
}
}
/* ----------------------------------------------------------------------------------------
* INTERPRETE DE COMANDO
* Las instrucciones disponibles son:
* - Consigna de posición: G0AXPY
* Donde X es el número de articulación a modificar e Y el ángulo deseado.
* - Consigna de velocidad: G1AXVY
* Donde X es el número de articulación a modificar e Y la velocidad deseada.
* - Homing: G28
* - Turn OFF: M0
* - Consigna tarea del efector final: TX
* Donde X es:
* - 0: PINTAR
* - 1: SOSTENER
* - 2: SOLTAR
* - 3: ROTAR
* - Consigna de duración de tarea del efector final: DXX
* Donde XX es el valor en segundos.
* - Inicio de operación del efector final: EF
-----------------------------------------------------------------------------------------*/
void BaseRobot::interpreteComando(std::string comando){
std::cout << comando << std::endl;
if (comando[0] == 'G'){
if (comando[1] == '0'){ // Consigna de ángulo
std::string aux = comando.substr(5, 4);
char aux1[4];
strcpy(aux1, aux.c_str());
if (comando[3] == '1'){
this->externalGdl1(atoi(aux1));
} else if (comando[3] == '2'){
this->externalGdl2(atoi(aux1));
} else if (comando[3] == '3'){
this->externalGdl3(atoi(aux1));
}
} else if (comando[1] == '1'){ // Consigna de velocidad
std::string aux = comando.substr(5, 4);
char aux1[4];
strcpy(aux1, aux.c_str());
if (comando[3] == '1'){
this->externalV1(atof(aux1));
} else if (comando[3] == '2'){
this->externalV2(atof(aux1));
} else if (comando[3] == '3'){
this->externalV3(atof(aux1));
}
} else if (comando[1] == '2' && comando[2] == '8'){ // Comando homing
std::cout << "HOMING" << std::endl;
this->turnON();
}
} else if (comando[0] == 'M'){
if (comando[1] == '0'){
this->turnOFF();
}
} else if (comando[0] == 'T'){
if (comando[1] == '0'){
this->ef->setTarea(PINTAR);
} else if (comando[1] == '1'){
this->ef->setTarea(SOSTENER);
} else if (comando [1] == '2'){
this->ef->setTarea(SOLTAR);
} else if (comando[1] == '3'){
this->ef->setTarea(ROTAR);
}
} else if (comando[0] == 'D'){
std::string aux = comando.substr(1, 2);
char aux1[2];
strcpy(aux1, aux.c_str());
this->ef->setDuration(atoi(aux1));
} else if (comando[0] == 'E' && comando[1] == 'F'){
this->ef->work();
this->endReceiver();
}
}
void BaseRobot::start(){
this->interpreteComando(this->instruct.front());
this->instruct.pop();
}
void BaseRobot::turnON(){
std::cout << "ON" << std::endl;
this->estado = ACTIVE;
this->emptyInstruct();
this->instruct.push("G0A1P90");
//this->instruct.push("G0A2P0");
//this->instruct.push("G0A3P0");
this->start();
}
void BaseRobot::turnOFF(){
this->emptyInstruct();
this->estado = INACTIVE;
if (this->currentAnimation != nullptr){
this->currentAnimation->stop();
}
}
void BaseRobot::gdl1Changed(int value){
if (this->estado == ACTIVE){
std::cout << "GDL1: " << value << std::endl;
// Alarma
QSound::play(QStringLiteral("qrc:/assets/sound.wav"));
this->p2->setAxis(0, QVector3D(0, 1, 0));
this->p3->setAxis(0, QVector3D(0, 1, 0));
this->p4->setAxis(0, QVector3D(0, 1, 0));
this->ef->setAxis(0, QVector3D(0, 1, 0));
this->p2->setPoint(0, QVector3D(0.65f, 0.0f, 0.1f));
this->p3->setPoint(0, QVector3D(0.65f, 0.0f, 0.1f));
this->p4->setPoint(0, QVector3D(0.65f, 0.0f, 0.1f));
this->ef->setPoint(0, QVector3D(0.65f, 0.0f, 0.1f));
this->p2->setAngle(0, value);
this->p3->setAngle(0, value);
this->p4->setAngle(0, value);
this->ef->setAngle(0, value);
}
}
void BaseRobot::gdl2Changed(int value){
if (this->estado == ACTIVE){
std::cout << "GDL2: " << value << std::endl;
// Alarma
QSound::play(QStringLiteral("qrc:/assets/sound.wav"));
/*
float axisX = float(m_sin(double(this->p2->getAngle(0))));
float axisY = 0.0f;
float axisZ = float(m_cos(double(this->p2->getAngle(0))));
this->p3->setAxis(1, QVector3D(axisX, axisY, axisZ));
this->p4->setAxis(1, QVector3D(axisX, axisY, axisZ));
this->ef->setAxis(1, QVector3D(axisX, axisY, axisZ));
float pointX = this->p1->getPoint(1).x() + 2.35f * float(m_cos(double(this->p2->getAngle(0))));
float pointY = this->p1->getPoint(1).y() - 0.9f + 10.0f;
float pointZ = this->p1->getPoint(1).z() - 0.1f - 2.35f * float(m_sin(double(this->p2->getAngle(0))));
this->p3->setPoint(1, QVector3D(pointX, pointY, pointZ));
this->p4->setPoint(1, QVector3D(pointX, pointY, pointZ));
this->ef->setPoint(1, QVector3D(pointX, pointY, pointZ));
this->p3->setAngle(1, value);
this->p4->setAngle(1, value);
this->ef->setAngle(1, value);
*/
//this->p2->setAxis(1, QVector3D(0, 0, 1));
this->p3->setAxis(1, QVector3D(0, 0, 1));
this->p4->setAxis(1, QVector3D(0, 0, 1));
this->ef->setAxis(1, QVector3D(0, 0, 1));
//this->p2->setPoint(1, QVector3D(0.65 + 2.35, 9.1f, 0));
this->p3->setPoint(1, QVector3D(0.65 + 2.35, 9.1f, 0));
this->p4->setPoint(1, QVector3D(0.65 + 2.35, 9.1f, 0));
this->ef->setPoint(1, QVector3D(0.65 + 2.35, 9.1f, 0));
this->p2->setAngle(1, value);
this->p3->setAngle(1, value);
this->p4->setAngle(1, value);
this->ef->setAngle(1, value);
}
}
void BaseRobot::gdl3Changed(int value){
if (this->estado == ACTIVE){
std::cout << "GDL3: " << value << std::endl;
// Alarma
QSound::play(QStringLiteral("qrc:/assets/sound.wav"));
/*
// El casteo de las siguientes lineas debe corregirse
double aux = - PIEZA3_LONG * m_sin(this->p3->getAngle(0));
float auxX = static_cast<float>(this->p1->getPoint(2).x()) + 2.0f * static_cast<float>(m_cos(this->p2->getAngle(0)));
float auxY = static_cast<float>(this->p1->getPoint(2).y()) + static_cast<float>(PIEZA3_LONG * m_cos(this->p3->getAngle(0))) + 10.0f - 1.0f;
float auxZ = static_cast<float>(this->p1->getPoint(2).z()) - 0.1f - 2.35f * static_cast<float>(m_sin(this->p2->getAngle(0)) - aux * m_sin(this->p2->getAngle(0)));
this->p4->setAxis(2, QVector3D(static_cast<float>(m_sin(this->p2->getAngle(0))), 0, static_cast<float>(m_cos(this->p2->getAngle(0)))));
this->p4->setPoint(2, QVector3D(auxX, auxY, auxZ));
this->p4->setAngle(2, value);
this->ef->setAxis(2, QVector3D(static_cast<float>(m_sin(this->p2->getAngle(0))), 0, static_cast<float>(m_cos(this->p2->getAngle(0)))));
this->ef->setPoint(2, QVector3D(auxX, auxY, auxZ));
this->ef->setAngle(2, value);
*/
this->p4->setAxis(2, QVector3D(0, 0, 1));
this->ef->setAxis(2, QVector3D(0, 0, 1));
this->p4->setPoint(2, QVector3D(0.65 + 2.35, 9.1f + float(PIEZA3_LONG), 0));
this->ef->setPoint(2, QVector3D(0.65 + 2.35, 9.1f + float(PIEZA3_LONG), 0));
this->p4->setAngle(2, value);
this->ef->setAngle(2, value);
}
}
void BaseRobot::externalGdl1(int value){
if (this->estado == ACTIVE){
QParallelAnimationGroup *motion = new QParallelAnimationGroup();
this->gdl1Changed(value);
motion->addAnimation(this->p2->animate(this->p2->getPrevious_angle(0), this->p2->getAngle(0), this->p2->getDuration(), 0));
motion->addAnimation(this->p3->animate(this->p3->getPrevious_angle(0), this->p3->getAngle(0), this->p3->getDuration(), 0));
motion->addAnimation(this->p4->animate(this->p4->getPrevious_angle(0), this->p4->getAngle(0), this->p4->getDuration(), 0));
motion->addAnimation(this->ef->animate(this->ef->getPrevious_angle(0), this->ef->getAngle(0), this->ef->getDuration(), 0));
this->currentAnimation = motion;
connect(motion, &QParallelAnimationGroup::finished, this, &BaseRobot::endReceiver);
motion->start();
this->estado = RUNNING;
}
}
void BaseRobot::externalGdl2(int value){
if (this->estado == ACTIVE){
QParallelAnimationGroup *motion = new QParallelAnimationGroup();
this->gdl2Changed(value);
motion->addAnimation(this->p3->animate(this->p3->getPrevious_angle(1), this->p3->getAngle(1), this->p3->getDuration(), 1));
motion->addAnimation(this->p4->animate(this->p4->getPrevious_angle(1), this->p4->getAngle(1), this->p4->getDuration(), 1));
motion->addAnimation(this->ef->animate(this->ef->getPrevious_angle(1), this->ef->getAngle(1), this->ef->getDuration(), 1));
this->currentAnimation = motion;
connect(motion, &QParallelAnimationGroup::finished, this, &BaseRobot::endReceiver);
motion->start();
this->estado = RUNNING;
}
}
void BaseRobot::externalGdl3(int value){
if (this->estado == ACTIVE){
QParallelAnimationGroup *motion = new QParallelAnimationGroup;
this->gdl3Changed(value);
motion->addAnimation(this->p4->animate(this->p4->getPrevious_angle(2), this->p4->getAngle(2), this->p4->getDuration(), 2));
motion->addAnimation(this->ef->animate(this->ef->getPrevious_angle(2), this->ef->getAngle(2), this->ef->getDuration(), 2));
this->currentAnimation = motion;
connect(motion, &QParallelAnimationGroup::finished, this, &BaseRobot::endReceiver);
motion->start();
this->estado = RUNNING;
}
}
void BaseRobot::externalV1(double value){
std::cout << "V1: " << value/10 << std::endl;
this->p2->setVel(value);
this->endReceiver();
}
void BaseRobot::externalV2(double value){
std::cout << "V2: " << value/10 << std::endl;
this->p3->setVel(value);
this->endReceiver();
}
void BaseRobot::externalV3(double value){
std::cout << "V3: " << value/10 << std::endl;
this->p4->setVel(value);
this->ef->setVel(value);
this->endReceiver();
}
void BaseRobot::endReceiver(){
if (this->estado == RUNNING){
this->estado = ACTIVE;
}
if (this->instruct.empty()){
std::cout << "END" << std::endl;
} else {
this->interpreteComando(this->instruct.front());
this->instruct.pop();
}
}
QString BaseRobot::toQString(){
QString data;
data+= ("Estado actual: " + this->getEstado() + "\n");
data += ("Ángulo de cada pieza:\n");
data += ("'Angulo art.1: " + QString::number(int(this->p2->getCurrentAngle(0))) + "\n");
data += ("'Angulo art.2: " + QString::number(int(this->p3->getCurrentAngle(1))) + "\n");
data += ("'Angulo art.3: " + QString::number(int(this->p4->getCurrentAngle(2))) + "\n");
data += ("Velocidad relativa de cada articulación:\n");
data += ("Velocidad art.1: " + QString::number(this->p2->getVel()) + "\n");
data += ("Velocidad art.2: " + QString::number(this->p3->getVel()) + "\n");
data += ("Velocidad art.3: " + QString::number(this->p4->getVel()) + "\n");
data += ("Velocidad de trabajo efector final: " + QString::number(this->ef->getVel()) + "\n");
data += ("Conexión: IP: " + this->ip_adress + "PORT: " + QString::number(this->PORT));
return data;
}
QString BaseRobot::getEstado(){
QString estado;
switch (this->estado) {
case INACTIVE:
estado = "INACTIVO";
break;
case ACTIVE:
estado = "ACTIVO";
break;
case RUNNING:
estado = "EN MOVIMIENTO";
break;
}
return estado;
}
QString BaseRobot::get_efTarea(){
QString data;
data = this->ef->getTarea();
return data;
}
QString BaseRobot::get_efDuracion(){
QString data;
data = QString::number(this->ef->getDuracion());
return data;
}
void BaseRobot::emptyInstruct(){
while (!this->instruct.empty()){
this->instruct.pop();
}
}