RoboDK API for C++ - Documentation
Loading...
Searching...
No Matches
robodk_api.cpp
1#include "robodk_api.h"
2#include <QtNetwork/QTcpSocket>
3#include <QtCore/QProcess>
4#include <cmath>
5#include <algorithm>
6#include <QFile>
7
8
9#ifdef _WIN32
10// Default path on Windows:
11#define ROBODK_DEFAULT_PATH_BIN "C:/RoboDK/bin/RoboDK.exe"
12
13#elif __APPLE__
14// Default Install Path on Mac
15#define ROBODK_DEFAULT_PATH_BIN "~/RoboDK/Applications/RoboDK.app/Contents/MacOS/RoboDK"
16
17#else
18
19// Default Install Path on Linux:
20#define ROBODK_DEFAULT_PATH_BIN "~/RoboDK/bin/RoboDK"
21
22#endif
23
24#define ROBODK_DEFAULT_PORT 20500
25
26#define ROBODK_API_TIMEOUT 1000 // communication timeout. Raise this value for slow computers
27#define ROBODK_API_START_STRING "CMD_START"
28#define ROBODK_API_READY_STRING "READY"
29#define ROBODK_API_LF "\n"
30
31
32
33#define M_PI 3.14159265358979323846264338327950288
34
35
36#ifndef RDK_SKIP_NAMESPACE
37namespace RoboDK_API {
38#endif
39
40
41//----------------------------------- Joints class ------------------------
43 _nDOFs = qMin(ndofs, RDK_SIZE_JOINTS_MAX);
44 for (int i=0; i<_nDOFs; i++){
45 _Values[i] = 0.0;
46 }
47}
49 SetValues(copy._Values, copy._nDOFs);
50}
51tJoints::tJoints(const double *joints, int ndofs){
52 SetValues(joints, ndofs);
53}
54tJoints::tJoints(const float *joints, int ndofs){
55 int ndofs_ok = qMin(ndofs, RDK_SIZE_JOINTS_MAX);
56 double jnts[RDK_SIZE_JOINTS_MAX];
57 for (int i=0; i<ndofs_ok; i++){
58 jnts[i] = joints[i];
59 }
60 SetValues(jnts, ndofs_ok);
61}
62tJoints::tJoints(const tMatrix2D *mat2d, int column, int ndofs){
63 int ncols = Matrix2D_Size(mat2d, 2);
64 if (column >= ncols){
65 _nDOFs = 0;
66 qDebug()<<"Warning: tMatrix2D column outside range when creating joints";
67 }
68 if (ndofs < 0){
69 ndofs = Matrix2D_Size(mat2d, 1);
70 }
71 _nDOFs = qMin(ndofs, RDK_SIZE_JOINTS_MAX);
72 double *ptr = Matrix2D_Get_col(mat2d, column);
73 SetValues(ptr, _nDOFs);
74}
75tJoints::tJoints(const QString &str){
76 _nDOFs = 0;
77 FromString(str);
78}
79
80const double* tJoints::ValuesD() const{
81 return _Values;
82}
83const float* tJoints::ValuesF() const{
84 for (int i=0; i<RDK_SIZE_JOINTS_MAX; i++){
85 ((float*)_ValuesF)[i] = _Values[i];
86 }
87 return _ValuesF;
88}
89#ifdef ROBODK_API_FLOATS
90const float* tJoints::Values() const{
91 return ValuesF();
92}
93#else
94const double* tJoints::Values() const{
95 return _Values;
96}
97#endif
98
99double* tJoints::Data(){
100 return _Values;
101}
102
103
104void tJoints::SetValues(const double *values, int ndofs){
105 if (ndofs >= 0){
106 _nDOFs = qMin(ndofs, RDK_SIZE_JOINTS_MAX);
107 }
108 for (int i=0; i<_nDOFs; i++){
109 _Values[i] = values[i];
110 }
111}
112
113void tJoints::SetValues(const float *values, int ndofs){
114 if (ndofs >= 0){
115 _nDOFs = qMin(ndofs, RDK_SIZE_JOINTS_MAX);
116 }
117 for (int i=0; i<_nDOFs; i++){
118 _Values[i] = values[i];
119 }
120}
121int tJoints::GetValues(double *values){
122 for (int i=0; i<_nDOFs; i++){
123 values[i] = _Values[i];
124 }
125 return _nDOFs;
126}
127QString tJoints::ToString(const QString &separator, int precision) const {
128 if (!Valid()){
129 return "tJoints(Invalid)";
130 }
131 QString values;//("tJoints({");
132 if (_nDOFs <= 0){
133 return values;
134 }
135 values.append(QString::number(_Values[0],'f',precision));
136 for (int i=1; i<_nDOFs; i++){
137 values.append(separator);
138 values.append(QString::number(_Values[i],'f',precision));
139 }
140 //values.append("} , " + QString::number(_nDOFs) + ")");
141 return values;
142}
143bool tJoints::FromString(const QString &str){
144 QStringList jnts_list = QString(str).replace(";",",").replace("\t",",").split(",", Qt::SkipEmptyParts);
145 _nDOFs = qMin(jnts_list.length(), RDK_SIZE_JOINTS_MAX);
146 for (int i=0; i<_nDOFs; i++){
147 QString stri(jnts_list.at(i));
148 _Values[i] = stri.trimmed().toDouble();
149 }
150 return true;
151}
152
153int tJoints::Length() const {
154 return _nDOFs;
155}
156
157void tJoints::setLength(int new_length) {
158 if (new_length >= 0 && new_length < _nDOFs){
159 _nDOFs = new_length;
160 }
161}
162
163bool tJoints::Valid() const {
164 return _nDOFs > 0;
165}
166//---------------------------------------------------------------------
167
168
169
170
171
172
173Mat transl(double x, double y, double z){
174 return Mat::transl(x,y,z);
175}
176
177Mat rotx(double rx){
178 return Mat::rotx(rx);
179}
180
181Mat roty(double ry){
182 return Mat::roty(ry);
183}
184
185Mat rotz(double rz){
186 return Mat::rotz(rz);
187}
188
189Mat::Mat() : QMatrix4x4() {
190 _valid = true;
191 setToIdentity();
192}
193Mat::Mat(bool valid) : QMatrix4x4() {
194 _valid = valid;
195 setToIdentity();
196}
197
198Mat::Mat(const QMatrix4x4 &matrix) : QMatrix4x4(matrix) {
199 // just copy
200 _valid = true;
201}
202Mat::Mat(const Mat &matrix) : QMatrix4x4(matrix) {
203 // just copy
204 _valid = matrix._valid;
205}
206
207Mat::Mat(double nx, double ox, double ax, double tx, double ny, double oy, double ay, double ty, double nz, double oz, double az, double tz) :
208 QMatrix4x4(nx, ox, ax, tx, ny, oy, ay, ty, nz, oz, az, tz, 0,0,0,1)
209{
210 _valid = true;
211}
212Mat::Mat(const double v[16]) :
213 QMatrix4x4(v[0], v[4], v[8], v[12], v[1], v[5], v[9], v[13], v[2], v[6], v[10], v[14], v[3], v[7], v[11], v[15])
214{
215 _valid = true;
216}
217Mat::Mat(const float v[16]) :
218 QMatrix4x4(v[0], v[4], v[8], v[12], v[1], v[5], v[9], v[13], v[2], v[6], v[10], v[14], v[3], v[7], v[11], v[15])
219{
220 _valid = true;
221}
222
223
224
225Mat::~Mat(){
226
227}
228
229
230void Mat::VX(tXYZ xyz) const {
231 xyz[0] = Get(0, 0);
232 xyz[1] = Get(1, 0);
233 xyz[2] = Get(2, 0);
234}
235void Mat::VY(tXYZ xyz) const {
236 xyz[0] = Get(0, 1);
237 xyz[1] = Get(1, 1);
238 xyz[2] = Get(2, 1);
239}
240void Mat::VZ(tXYZ xyz) const {
241 xyz[0] = Get(0, 2);
242 xyz[1] = Get(1, 2);
243 xyz[2] = Get(2, 2);
244}
245void Mat::Pos(tXYZ xyz) const {
246 xyz[0] = Get(0, 3);
247 xyz[1] = Get(1, 3);
248 xyz[2] = Get(2, 3);
249}
250void Mat::setVX(double x, double y, double z){
251 Set(0,0, x);
252 Set(1,0, y);
253 Set(2,0, z);
254}
255void Mat::setVY(double x, double y, double z){
256 Set(0,1, x);
257 Set(1,1, y);
258 Set(2,1, z);
259}
260
261void Mat::setVZ(double x, double y, double z){
262 Set(0,2, x);
263 Set(1,2, y);
264 Set(2,2, z);
265}
266
267void Mat::setPos(double x, double y, double z){
268 Set(0,3, x);
269 Set(1,3, y);
270 Set(2,3, z);
271}
272void Mat::setVX(double xyz[3]){
273 Set(0,0, xyz[0]);
274 Set(1,0, xyz[1]);
275 Set(2,0, xyz[2]);
276}
277void Mat::setVY(double xyz[3]){
278 Set(0,1, xyz[0]);
279 Set(1,1, xyz[1]);
280 Set(2,1, xyz[2]);
281}
282void Mat::setVZ(double xyz[3]){
283 Set(0,2, xyz[0]);
284 Set(1,2, xyz[1]);
285 Set(2,2, xyz[2]);
286}
287void Mat::setPos(double xyz[3]){
288 Set(0,3, xyz[0]);
289 Set(1,3, xyz[1]);
290 Set(2,3, xyz[2]);
291}
292
293void Mat::Set(int i, int j, double value){
294 QVector4D rw(this->row(i));
295 rw[j] = value;
296 setRow(i, rw);
297 // the following should not crash!!
298 //float **dt_ok = (float**) data();
299 //dt_ok[i][j] = value;
300}
301
302double Mat::Get(int i, int j) const{
303 return row(i)[j];
304 // the following hsould be allowed!!
305 //return ((const float**)data())[i][j];
306}
307
308
309Mat Mat::inv() const{
310 return this->inverted();
311}
312
313bool Mat::isHomogeneous() const {
314 const bool debug_info = false;
315 tXYZ vx, vy, vz;
316 const double tol = 1e-7;
317 VX(vx);
318 VY(vy);
319 VZ(vz);
320 if (fabs((double) DOT(vx,vy)) > tol){
321 if (debug_info){
322 qDebug() << "Vector X and Y are not perpendicular!";
323 }
324 return false;
325 } else if (fabs((double) DOT(vx,vz)) > tol){
326 if (debug_info){
327 qDebug() << "Vector X and Z are not perpendicular!";
328 }
329 return false;
330 } else if (fabs((double) DOT(vy,vz)) > tol){
331 if (debug_info){
332 qDebug() << "Vector Y and Z are not perpendicular!";
333 }
334 return false;
335 } else if (fabs((double) (NORM(vx)-1.0)) > tol){
336 if (debug_info){
337 qDebug() << "Vector X is not unitary! " << NORM(vx);
338 }
339 return false;
340 } else if (fabs((double) (NORM(vy)-1.0)) > tol){
341 if (debug_info){
342 qDebug() << "Vector Y is not unitary! " << NORM(vy);
343 }
344 return false;
345 } else if (fabs((double) (NORM(vz)-1.0)) > tol){
346 if (debug_info){
347 qDebug() << "Vector Z is not unitary! " << NORM(vz);
348 }
349 return false;
350 }
351 return true;
352}
353
355 tXYZ vx, vy, vz;
356 VX(vx);
357 VY(vy);
358 VZ(vz);
359 bool is_homogeneous = isHomogeneous();
360 //if (is_homogeneous){
361 // return false;
362 //}
363
364 NORMALIZE(vx);
365 CROSS(vz, vx, vy);
366 NORMALIZE(vz);
367 CROSS(vy, vz, vx);
368 NORMALIZE(vy);
369 setVX(vx);
370 setVY(vy);
371 setVZ(vz);
372 Set(3,0, 0.0);
373 Set(3,1, 0.0);
374 Set(3,2, 0.0);
375 Set(3,3, 1.0);
376 return !is_homogeneous;
377}
378
379
380//----------------------------------------------------
381
382void Mat::ToXYZRPW(tXYZWPR xyzwpr) const{
383 double x = Get(0,3);
384 double y = Get(1,3);
385 double z = Get(2,3);
386 double w, p, r;
387 if (Get(2,0) > (1.0 - 1e-6)){
388 p = -M_PI*0.5;
389 r = 0;
390 w = atan2(-Get(1,2), Get(1,1));
391 } else if (Get(2,0) < -1.0 + 1e-6){
392 p = 0.5*M_PI;
393 r = 0;
394 w = atan2(Get(1,2),Get(1,1));
395 } else {
396 p = atan2(-Get(2, 0), sqrt(Get(0, 0) * Get(0, 0) + Get(1, 0) * Get(1, 0)));
397 w = atan2(Get(1, 0), Get(0, 0));
398 r = atan2(Get(2, 1), Get(2, 2));
399 }
400 xyzwpr[0] = x;
401 xyzwpr[1] = y;
402 xyzwpr[2] = z;
403 xyzwpr[3] = r*180.0/M_PI;
404 xyzwpr[4] = p*180.0/M_PI;
405 xyzwpr[5] = w*180.0/M_PI;
406}
407
408QString Mat::ToString(const QString &separator, int precision, bool xyzwpr_only) const {
409 if (!Valid()){
410 return "Mat(Invalid)";
411 }
412 QString str;
413 if (!isHomogeneous()){
414 str.append("Warning!! Pose is not homogeneous! Use Mat::MakeHomogeneous() to make this matrix homogeneous\n");
415 }
416 //str.append("Mat(XYZRPW_2_Mat(");
417
418 tXYZWPR xyzwpr;
419 ToXYZRPW(xyzwpr);
420 str.append(QString::number(xyzwpr[0],'f',precision));
421 for (int i=1; i<6; i++){
422 str.append(separator);
423 str.append(QString::number(xyzwpr[i],'f',precision));
424 }
425 //str.append("))");
426
427 if (xyzwpr_only){
428 return str;
429 }
430 str.append("\n");
431 for (int i=0; i<4; i++){
432 str.append("[");
433 for (int j=0; j<4; j++){
434 str.append(QString::number(row(i)[j], 'f', precision));
435 if (j < 3){
436 str.append(separator);
437 }
438 }
439 str.append("];\n");
440 }
441 return str;
442}
443
444bool Mat::FromString(const QString &pose_str){
445 QStringList values_list = QString(pose_str).replace(";",",").replace("\t",",").split(",", Qt::SkipEmptyParts);
446 int nvalues = qMin(values_list.length(), 6);
447 tXYZWPR xyzwpr;
448 for (int i=0; i<6; i++){
449 xyzwpr[i] = 0.0;
450 }
451 if (nvalues < 6){
452 FromXYZRPW(xyzwpr);
453 return false;
454 }
455 for (int i=0; i<nvalues; i++){
456 QString stri(values_list.at(i));
457 xyzwpr[i] = stri.trimmed().toDouble();
458 }
459 FromXYZRPW(xyzwpr);
460 return true;
461}
462
463
464Mat Mat::XYZRPW_2_Mat(double x, double y, double z, double r, double p, double w){
465 double a = r * M_PI / 180.0;
466 double b = p * M_PI / 180.0;
467 double c = w * M_PI / 180.0;
468 double ca = cos(a);
469 double sa = sin(a);
470 double cb = cos(b);
471 double sb = sin(b);
472 double cc = cos(c);
473 double sc = sin(c);
474 return Mat(cb * cc, cc * sa * sb - ca * sc, sa * sc + ca * cc * sb, x, cb * sc, ca * cc + sa * sb * sc, ca * sb * sc - cc * sa, y, -sb, cb * sa, ca * cb, z);
475}
477 return XYZRPW_2_Mat(xyzwpr[0], xyzwpr[1], xyzwpr[2], xyzwpr[3], xyzwpr[4], xyzwpr[5]);
478}
479
481 Mat newmat = Mat::XYZRPW_2_Mat(xyzwpr[0], xyzwpr[1], xyzwpr[2], xyzwpr[3], xyzwpr[4], xyzwpr[5]);
482 for (int i=0; i<4; i++){
483 for (int j=0; j<4; j++){
484 this->Set(i,j, newmat.Get(i,j));
485 }
486 }
487}
488
489const double* Mat::ValuesD() const {
490 double* _ddata16_non_const = (double*) _ddata16;
491 for(int i=0; i<16; ++i){
492 _ddata16_non_const[i] = constData()[i];
493 }
494 return _ddata16;
495}
496const float* Mat::ValuesF() const {
497 return constData();
498}
499
500#ifdef ROBODK_API_FLOATS
501const float* Mat::Values() const {
502 return constData();
503}
504#else
505const double* Mat::Values() const {
506 return ValuesD();
507}
508
509#endif
510
511
512
513void Mat::Values(double data[16]) const{
514 for(int i=0; i<16; ++i){
515 data[i] = constData()[i];
516 }
517}
518void Mat::Values(float data[16]) const{
519 for(int i=0; i<16; ++i){
520 data[i] = constData()[i];
521 }
522}
523bool Mat::Valid() const{
524 return _valid;
525}
526
527Mat Mat::transl(double x, double y, double z){
528 Mat mat;
529 mat.setToIdentity();
530 mat.setPos(x, y, z);
531 return mat;
532}
533
534Mat Mat::rotx(double rx){
535 double cx = cos(rx);
536 double sx = sin(rx);
537 return Mat(1, 0, 0, 0, 0, cx, -sx, 0, 0, sx, cx, 0);
538}
539
540Mat Mat::roty(double ry){
541 double cy = cos(ry);
542 double sy = sin(ry);
543 return Mat(cy, 0, sy, 0, 0, 1, 0, 0, -sy, 0, cy, 0);
544}
545
546Mat Mat::rotz(double rz){
547 double cz = cos(rz);
548 double sz = sin(rz);
549 return Mat(cz, -sz, 0, 0, sz, cz, 0, 0, 0, 0, 1, 0);
550}
551
552
553
554
555//---------------------------------------------------------------------------------------------------
556//---------------------------------------------------------------------------------------------------
557//---------------------------------------------------------------------------------------------------
558//---------------------------------------------------------------------------------------------------
560Item::Item(RoboDK *rdk, quint64 ptr, qint32 type) {
561 _RDK = rdk;
562 _PTR = ptr;
563 _TYPE = type;
564}
565Item::Item(const Item &other) {
566 _RDK = other._RDK;
567 _PTR = other._PTR;
568 _TYPE = other._TYPE;
569}
570Item::~Item(){
571
572}
573
574QString Item::ToString() const {
575 if (this->Valid()){
576 return QString("Item(&RDK, %1, %2); // %3").arg(_PTR).arg(_TYPE).arg(Name());
577 }
578 return "Item(Invalid)";
579}
580
581
587 return _RDK;
588}
589
594 _RDK = new RoboDK();
595}
596
602int Item::Type() const{
603 _RDK->_check_connection();
604 _RDK->_send_Line("G_Item_Type");
605 _RDK->_send_Item(this);
606 int itemtype = _RDK->_recv_Int();
607 _RDK->_check_status();
608 return itemtype;
609}
610
612
617void Item::Save(const QString &filename){
618 _RDK->Save(filename, this);
619}
620
625 _RDK->_check_connection();
626 _RDK->_send_Line("Remove");
627 _RDK->_send_Item(this);
628 _RDK->_check_status();
629 _PTR = 0;
630 _TYPE = -1;
631}
632
637bool Item::Valid(bool check_deleted) const {
638 if (check_deleted){
639 return Type() > 0;
640 }
641 return _PTR != 0;
642}
648 _RDK->_check_connection();
649 _RDK->_send_Line("S_Parent");
650 _RDK->_send_Item(this);
651 _RDK->_send_Item(parent);
652 _RDK->_check_status();
653}
654
661 _RDK->_check_connection();
662 _RDK->_send_Line("S_Parent_Static");
663 _RDK->_send_Item(this);
664 _RDK->_send_Item(parent);
665 _RDK->_check_status();
666}
667
673 _RDK->_check_connection();
674 _RDK->_send_Line("Attach_Closest");
675 _RDK->_send_Item(this);
676 Item item_attached = _RDK->_recv_Item();
677 _RDK->_check_status();
678 return item_attached;
679}
680
686 _RDK->_check_connection();
687 _RDK->_send_Line("Detach_Closest");
688 _RDK->_send_Item(this);
689 _RDK->_send_Item(parent);
690 Item item_detached = _RDK->_recv_Item();
691 _RDK->_check_status();
692 return item_detached;
693}
694
698void Item::DetachAll(Item parent) {
699 _RDK->_check_connection();
700 _RDK->_send_Line("Detach_All");
701 _RDK->_send_Item(this);
702 _RDK->_send_Item(parent);
703 _RDK->_check_status();
704}
705
706
712 _RDK->_check_connection();
713 _RDK->_send_Line("G_Parent");
714 _RDK->_send_Item(this);
715 Item itm_parent = _RDK->_recv_Item();
716 _RDK->_check_status();
717 return itm_parent;
718}
719
720
726QList<Item> Item::Childs() const {
727 _RDK->_check_connection();
728 _RDK->_send_Line("G_Childs");
729 _RDK->_send_Item(this);
730 int nitems = _RDK->_recv_Int();
731 QList<Item> itemlist;
732 for (int i = 0; i < nitems; i++)
733 {
734 itemlist.append(_RDK->_recv_Item());
735 }
736 _RDK->_check_status();
737 return itemlist;
738}
739
744bool Item::Visible() const {
745 _RDK->_check_connection();
746 _RDK->_send_Line("G_Visible");
747 _RDK->_send_Item(this);
748 int visible = _RDK->_recv_Int();
749 _RDK->_check_status();
750 return (visible != 0);
751}
757void Item::setVisible(bool visible, int visible_frame){
758 if (visible_frame < 0)
759 {
760 visible_frame = visible ? 1 : 0;
761 }
762 _RDK->_check_connection();
763 _RDK->_send_Line("S_Visible");
764 _RDK->_send_Item(this);
765 _RDK->_send_Int(visible ? 1 : 0);
766 _RDK->_send_Int(visible_frame);
767 _RDK->_check_status();
768}
769
774QString Item::Name() const {
775 _RDK->_check_connection();
776 _RDK->_send_Line("G_Name");
777 _RDK->_send_Item(this);
778 QString name = _RDK->_recv_Line();
779 _RDK->_check_status();
780 return name;
781}
782
787void Item::setName(const QString &name){
788 _RDK->_check_connection();
789 _RDK->_send_Line("S_Name");
790 _RDK->_send_Item(this);
791 _RDK->_send_Line(name);
792 _RDK->_check_status();
793}
794
795// add more methods
796
802void Item::setPose(const Mat pose){
803 _RDK->_check_connection();
804 _RDK->_send_Line("S_Hlocal");
805 _RDK->_send_Item(this);
806 _RDK->_send_Pose(pose);
807 _RDK->_check_status();
808}
809
816 _RDK->_check_connection();
817 _RDK->_send_Line("G_Hlocal");
818 _RDK->_send_Item(this);
819 Mat pose = _RDK->_recv_Pose();
820 _RDK->_check_status();
821 return pose;
822}
823
828void Item::setGeometryPose(const Mat pose){
829 _RDK->_check_connection();
830 _RDK->_send_Line("S_Hgeom");
831 _RDK->_send_Item(this);
832 _RDK->_send_Pose(pose);
833 _RDK->_check_status();
834}
835
841 _RDK->_check_connection();
842 _RDK->_send_Line("G_Hgeom");
843 _RDK->_send_Item(this);
844 Mat pose = _RDK->_recv_Pose();
845 _RDK->_check_status();
846 return pose;
847}
848/*
853void Item::setHtool(Mat pose){
854 _RDK->_check_connection();
855 _RDK->_send_Line("S_Htool");
856 _RDK->_send_Item(this);
857 _RDK->_send_Pose(pose);
858 _RDK->_check_status();
859}
860
866Mat Item::Htool(){
867 _RDK->_check_connection();
868 _RDK->_send_Line("G_Htool");
869 _RDK->_send_Item(this);
870 Mat pose = _RDK->_recv_Pose();
871 _RDK->_check_status();
872 return pose;
873}
874*/
880 _RDK->_check_connection();
881 _RDK->_send_Line("G_Tool");
882 _RDK->_send_Item(this);
883 Mat pose = _RDK->_recv_Pose();
884 _RDK->_check_status();
885 return pose;
886}
887
893 _RDK->_check_connection();
894 _RDK->_send_Line("G_Frame");
895 _RDK->_send_Item(this);
896 Mat pose = _RDK->_recv_Pose();
897 _RDK->_check_status();
898 return pose;
899}
900
906void Item::setPoseFrame(const Mat frame_pose){
907 _RDK->_check_connection();
908 _RDK->_send_Line("S_Frame");
909 _RDK->_send_Pose(frame_pose);
910 _RDK->_send_Item(this);
911 _RDK->_check_status();
912}
913
919void Item::setPoseFrame(const Item frame_item){
920 _RDK->_check_connection();
921 _RDK->_send_Line("S_Frame_ptr");
922 _RDK->_send_Item(frame_item);
923 _RDK->_send_Item(this);
924 _RDK->_check_status();
925}
926
932void Item::setPoseTool(const Mat tool_pose){
933 _RDK->_check_connection();
934 _RDK->_send_Line("S_Tool");
935 _RDK->_send_Pose(tool_pose);
936 _RDK->_send_Item(this);
937 _RDK->_check_status();
938}
939
945void Item::setPoseTool(const Item tool_item){
946 _RDK->_check_connection();
947 _RDK->_send_Line("S_Tool_ptr");
948 _RDK->_send_Item(tool_item);
949 _RDK->_send_Item(this);
950 _RDK->_check_status();
951}
952
957void Item::setPoseAbs(const Mat pose){
958 _RDK->_check_connection();
959 _RDK->_send_Line("S_Hlocal_Abs");
960 _RDK->_send_Item(this);
961 _RDK->_send_Pose(pose);
962 _RDK->_check_status();
963
964}
965
971 _RDK->_check_connection();
972 _RDK->_send_Line("G_Hlocal_Abs");
973 _RDK->_send_Item(this);
974 Mat pose = _RDK->_recv_Pose();
975 _RDK->_check_status();
976 return pose;
977}
978
983void Item::setColor(double colorRGBA[4]){
984 _RDK->_check_connection();
985 _RDK->_send_Line("S_Color");
986 _RDK->_send_Item(this);
987 _RDK->_send_Array(colorRGBA, 4);
988 _RDK->_check_status();
989
990}
991
996
997
1003void Item::Scale(double scale){
1004 double scale_xyz[3];
1005 scale_xyz[0] = scale;
1006 scale_xyz[1] = scale;
1007 scale_xyz[2] = scale;
1008 Scale(scale_xyz);
1009}
1010
1016void Item::Scale(double scale_xyz[3]){
1017 _RDK->_check_connection();
1018 _RDK->_send_Line("Scale");
1019 _RDK->_send_Item(this);
1020 _RDK->_send_Array(scale_xyz, 3);
1021 _RDK->_check_status();
1022}
1023
1024GetPointsResult Item::GetPoints(int featureType, int featureId) {
1025 GetPointsResult result;
1026 result.featureType = RoboDK::FEATURE_NONE;
1027 result.featureId = 0;
1028 result.points = nullptr;
1029
1030 if (featureType >= RoboDK::FEATURE_HOVER_OBJECT_MESH) {
1031 return result;
1032 }
1033
1034 _RDK->_check_connection();
1035 _RDK->_send_Line("G_ObjPoint");
1036 _RDK->_send_Item(this);
1037 _RDK->_send_Int(featureType);
1038 _RDK->_send_Int(featureId);
1039 _RDK->_recv_Matrix2D(&result.points);
1040 result.featureName = _RDK->_recv_Line();
1041 result.featureType = featureType;
1042 result.featureId = featureId;
1043 _RDK->_check_status();
1044 return result;
1045}
1046
1047bool Item::SelectedFeature(int& featureType, int& featureId)
1048{
1049 _RDK->_check_connection();
1050 _RDK->_send_Line("G_ObjSelection");
1051 _RDK->_send_Item(this);
1052 int isSelected = _RDK->_recv_Int();
1053 featureType = _RDK->_recv_Int();
1054 featureId = _RDK->_recv_Int();
1055 _RDK->_check_status();
1056 return isSelected != 0;
1057}
1058
1059
1060
1061
1071Item Item::setMachiningParameters(QString ncfile, Item part_obj, QString options)
1072{
1073 _RDK->_check_connection();
1074 _RDK->_send_Line("S_MachiningParams");
1075 _RDK->_send_Item(this);
1076 _RDK->_send_Line(ncfile);
1077 _RDK->_send_Item(part_obj);
1078 _RDK->_send_Line("NO_UPDATE " + options);
1079 _RDK->_TIMEOUT = 3600 * 1000;
1080 Item program = _RDK->_recv_Item();
1081 _RDK->_TIMEOUT = ROBODK_API_TIMEOUT;
1082 double status = _RDK->_recv_Int() / 1000.0;
1083 _RDK->_check_status();
1084 return program;
1085}
1086
1091 _RDK->_check_connection();
1092 _RDK->_send_Line("S_Target_As_RT");
1093 _RDK->_send_Item(this);
1094 _RDK->_check_status();
1095}
1096
1101 _RDK->_check_connection();
1102 _RDK->_send_Line("S_Target_As_JT");
1103 _RDK->_send_Item(this);
1104 _RDK->_check_status();
1105}
1106
1111 _RDK->_check_connection();
1112 _RDK->_send_Line("Target_Is_JT");
1113 _RDK->_send_Item(this);
1114 int is_jt = _RDK->_recv_Int();
1115 _RDK->_check_status();
1116 return is_jt > 0;
1117}
1118
1119//#####Robot item calls####
1120
1126 tJoints jnts;
1127 _RDK->_check_connection();
1128 _RDK->_send_Line("G_Thetas");
1129 _RDK->_send_Item(this);
1130 _RDK->_recv_Array(&jnts);
1131 _RDK->_check_status();
1132 return jnts;
1133}
1134
1135// add more methods
1136
1142 tJoints jnts;
1143 _RDK->_check_connection();
1144 _RDK->_send_Line("G_Home");
1145 _RDK->_send_Item(this);
1146 _RDK->_recv_Array(&jnts);
1147 _RDK->_check_status();
1148 return jnts;
1149}
1150
1151
1157 _RDK->_check_connection();
1158 _RDK->_send_Line("S_Home");
1159 _RDK->_send_Array(&jnts);
1160 _RDK->_send_Item(this);
1161 _RDK->_check_status();
1162}
1163
1170 _RDK->_check_connection();
1171 _RDK->_send_Line("G_LinkObjId");
1172 _RDK->_send_Item(this);
1173 _RDK->_send_Int(link_id);
1174 Item item = _RDK->_recv_Item();
1175 _RDK->_check_status();
1176 return item;
1177}
1178
1184Item Item::getLink(int type_linked){
1185 _RDK->_check_connection();
1186 _RDK->_send_Line("G_LinkType");
1187 _RDK->_send_Item(this);
1188 _RDK->_send_Int(type_linked);
1189 Item item = _RDK->_recv_Item();
1190 _RDK->_check_status();
1191 return item;
1192}
1193
1194
1199void Item::setJoints(const tJoints &jnts){
1200 _RDK->_check_connection();
1201 _RDK->_send_Line("S_Thetas");
1202 _RDK->_send_Array(&jnts);
1203 _RDK->_send_Item(this);
1204 _RDK->_check_status();
1205}
1206
1212void Item::JointLimits(tJoints *lower_limits, tJoints *upper_limits){
1213 _RDK->_check_connection();
1214 _RDK->_send_Line("G_RobLimits");
1215 _RDK->_send_Item(this);
1216 _RDK->_recv_Array(lower_limits);
1217 _RDK->_recv_Array(upper_limits);
1218 double joints_type = _RDK->_recv_Int() / 1000.0;
1219 _RDK->_check_status();
1220}
1221
1227void Item::setJointLimits(const tJoints &lower_limits, const tJoints &upper_limits){
1228 _RDK->_check_connection();
1229 _RDK->_send_Line("S_RobLimits");
1230 _RDK->_send_Item(this);
1231 _RDK->_send_Array(&lower_limits);
1232 _RDK->_send_Array(&upper_limits);
1233 //double joints_type = _RDK->_recv_Int() / 1000.0;
1234 _RDK->_check_status();
1235}
1236
1242void Item::setRobot(const Item &robot){
1243 _RDK->_check_connection();
1244 _RDK->_send_Line("S_Robot");
1245 _RDK->_send_Item(this);
1246 _RDK->_send_Item(robot);
1247 _RDK->_check_status();
1248}
1249
1250
1257Item Item::AddTool(const Mat &tool_pose, const QString &tool_name){
1258 _RDK->_check_connection();
1259 _RDK->_send_Line("AddToolEmpty");
1260 _RDK->_send_Item(this);
1261 _RDK->_send_Pose(tool_pose);
1262 _RDK->_send_Line(tool_name);
1263 Item newtool = _RDK->_recv_Item();
1264 _RDK->_check_status();
1265 return newtool;
1266}
1267
1273Mat Item::SolveFK(const tJoints &joints, const Mat *tool, const Mat *ref){
1274 _RDK->_check_connection();
1275 _RDK->_send_Line("G_FK");
1276 _RDK->_send_Array(&joints);
1277 _RDK->_send_Item(this);
1278 Mat pose = _RDK->_recv_Pose();
1279 Mat base2flange(pose);
1280 if (tool != nullptr){
1281 base2flange = pose*(*tool);
1282 }
1283 if (ref != nullptr){
1284 base2flange = ref->inv() * base2flange;
1285 }
1286 _RDK->_check_status();
1287 return base2flange;
1288}
1289
1295void Item::JointsConfig(const tJoints &joints, tConfig config){
1296 _RDK->_check_connection();
1297 _RDK->_send_Line("G_Thetas_Config");
1298 _RDK->_send_Array(&joints);
1299 _RDK->_send_Item(this);
1300 int sz = RDK_SIZE_MAX_CONFIG;
1301 _RDK->_recv_Array(config, &sz);
1302 _RDK->_check_status();
1303 //return config;
1304}
1305
1313tJoints Item::SolveIK(const Mat &pose, const Mat *tool, const Mat *ref){
1314 tJoints jnts;
1315 Mat base2flange(pose);
1316 if (tool != nullptr){
1317 base2flange = pose*tool->inv();
1318 }
1319 if (ref != nullptr){
1320 base2flange = (*ref) * base2flange;
1321 }
1322 _RDK->_check_connection();
1323 _RDK->_send_Line("G_IK");
1324 _RDK->_send_Pose(base2flange);
1325 _RDK->_send_Item(this);
1326 _RDK->_recv_Array(&jnts);
1327 _RDK->_check_status();
1328 return jnts;
1329}
1330
1331
1332
1341tJoints Item::SolveIK(const Mat pose, tJoints joints_approx, const Mat *tool, const Mat *ref){
1342 Mat base2flange(pose);
1343 if (tool != nullptr){
1344 base2flange = pose*tool->inv();
1345 }
1346 if (ref != nullptr){
1347 base2flange = (*ref) * base2flange;
1348 }
1349 _RDK->_check_connection();
1350 _RDK->_send_Line("G_IK_jnts");
1351 _RDK->_send_Pose(base2flange);
1352 _RDK->_send_Array(&joints_approx);
1353 _RDK->_send_Item(this);
1354 tJoints jnts;
1355 _RDK->_recv_Array(&jnts);
1356 _RDK->_check_status();
1357 return jnts;
1358}
1359
1360
1366tMatrix2D* Item::SolveIK_All_Mat2D(const Mat &pose, const Mat *tool, const Mat *ref){
1367 tMatrix2D *mat2d = nullptr;
1368 Mat base2flange(pose);
1369 if (tool != nullptr){
1370 base2flange = pose*tool->inv();
1371 }
1372 if (ref != nullptr){
1373 base2flange = (*ref) * base2flange;
1374 }
1375 _RDK->_check_connection();
1376 _RDK->_send_Line("G_IK_cmpl");
1377 _RDK->_send_Pose(base2flange);
1378 _RDK->_send_Item(this);
1379 _RDK->_recv_Matrix2D(&mat2d);
1380 _RDK->_check_status();
1381 return mat2d;
1382}
1383QList<tJoints> Item::SolveIK_All(const Mat &pose, const Mat *tool, const Mat *ref){
1384 tMatrix2D *mat2d = SolveIK_All_Mat2D(pose, tool, ref);
1385 QList<tJoints> jnts_list;
1386 int ndofs = Matrix2D_Size(mat2d, 1) - 2;
1387 int nsol = Matrix2D_Size(mat2d, 2);
1388 for (int i=0; i<nsol; i++){
1389 tJoints jnts = tJoints(mat2d, i);
1390 jnts.setLength(jnts.Length() - 2);
1391 jnts_list.append(jnts);
1392 }
1393 return jnts_list;
1394}
1395
1401bool Item::Connect(const QString &robot_ip){
1402 _RDK->_check_connection();
1403 _RDK->_send_Line("Connect");
1404 _RDK->_send_Item(this);
1405 _RDK->_send_Line(robot_ip);
1406 int status = _RDK->_recv_Int();
1407 _RDK->_check_status();
1408 return status != 0;
1409}
1410
1416 _RDK->_check_connection();
1417 _RDK->_send_Line("Disconnect");
1418 _RDK->_send_Item(this);
1419 int status = _RDK->_recv_Int();
1420 _RDK->_check_status();
1421 return status != 0;
1422}
1423
1429void Item::MoveJ(const Item &itemtarget, bool blocking){
1431 _RDK->_check_connection();
1432 _RDK->_send_Line("Add_INSMOVE");
1433 _RDK->_send_Item(itemtarget);
1434 _RDK->_send_Item(this);
1435 _RDK->_send_Int(1);
1436 _RDK->_check_status();
1437 } else {
1438 _RDK->_moveX(&itemtarget, nullptr, nullptr, this, 1, blocking);
1439 }
1440}
1441
1447void Item::MoveJ(const tJoints &joints, bool blocking){
1448 _RDK->_moveX(nullptr, &joints, nullptr, this, 1, blocking);
1449}
1450
1456void Item::MoveJ(const Mat &target, bool blocking){
1457 _RDK->_moveX(nullptr, nullptr, &target, this, 1, blocking);
1458}
1459
1465void Item::MoveL(const Item &itemtarget, bool blocking){
1467 _RDK->_check_connection();
1468 _RDK->_send_Line("Add_INSMOVE");
1469 _RDK->_send_Item(itemtarget);
1470 _RDK->_send_Item(this);
1471 _RDK->_send_Int(2);
1472 _RDK->_check_status();
1473 } else {
1474 _RDK->_moveX(&itemtarget, nullptr, nullptr, this, 2, blocking);
1475 }
1476}
1477
1483void Item::MoveL(const tJoints &joints, bool blocking){
1484 _RDK->_moveX(nullptr, &joints, nullptr, this, 2, blocking);
1485}
1486
1492void Item::MoveL(const Mat &target, bool blocking){
1493 _RDK->_moveX(nullptr, nullptr, &target, this, 2, blocking);
1494}
1495
1502void Item::MoveC(const Item &itemtarget1, const Item &itemtarget2, bool blocking){
1503 _RDK->_moveC(&itemtarget1, nullptr, nullptr, &itemtarget2, nullptr, nullptr, this, blocking);
1504}
1505
1512void Item::MoveC(const tJoints &joints1, const tJoints &joints2, bool blocking){
1513 _RDK->_moveC(nullptr, &joints1, nullptr, nullptr, &joints2, nullptr, this, blocking);
1514}
1515
1522void Item::MoveC(const Mat &target1, const Mat &target2, bool blocking){
1523 _RDK->_moveC(nullptr, nullptr, &target1, nullptr, nullptr, &target2, this, blocking);
1524}
1525
1533int Item::MoveJ_Test(const tJoints &j1, const tJoints &j2, double minstep_deg){
1534 _RDK->_check_connection();
1535 _RDK->_send_Line("CollisionMove");
1536 _RDK->_send_Item(this);
1537 _RDK->_send_Array(&j1);
1538 _RDK->_send_Array(&j2);
1539 _RDK->_send_Int((int)(minstep_deg * 1000.0));
1540 _RDK->_TIMEOUT = 3600 * 1000;
1541 int collision = _RDK->_recv_Int();
1542 _RDK->_TIMEOUT = ROBODK_API_TIMEOUT;
1543 _RDK->_check_status();
1544 return collision;
1545}
1546
1554int Item::MoveL_Test(const tJoints &j1, const Mat &pose2, double minstep_deg){
1555 _RDK->_check_connection();
1556 _RDK->_send_Line("CollisionMoveL");
1557 _RDK->_send_Item(this);
1558 _RDK->_send_Array(&j1);
1559 _RDK->_send_Pose(pose2);
1560 _RDK->_send_Int((int)(minstep_deg * 1000.0));
1561 _RDK->_TIMEOUT = 3600 * 1000;
1562 int collision = _RDK->_recv_Int();
1563 _RDK->_TIMEOUT = ROBODK_API_TIMEOUT;
1564 _RDK->_check_status();
1565 return collision;
1566}
1567
1568
1576void Item::setSpeed(double speed_linear, double speed_joints, double accel_linear, double accel_joints){
1577 _RDK->_check_connection();
1578 _RDK->_send_Line("S_Speed4");
1579 _RDK->_send_Item(this);
1580 double speed_accel[4];
1581 speed_accel[0] = speed_linear;
1582 speed_accel[1] = speed_joints;
1583 speed_accel[2] = accel_linear;
1584 speed_accel[3] = accel_joints;
1585 _RDK->_send_Array(speed_accel, 4);
1586 _RDK->_check_status();
1587}
1588
1593void Item::setRounding(double zonedata){
1594 _RDK->_check_connection();
1595 _RDK->_send_Line("S_ZoneData");
1596 _RDK->_send_Int((int)(zonedata * 1000.0));
1597 _RDK->_send_Item(this);
1598 _RDK->_check_status();
1599}
1600
1606 _RDK->_check_connection();
1607 _RDK->_send_Line("Show_Seq");
1608 _RDK->_send_Matrix2D(sequence);
1609 _RDK->_send_Item(this);
1610 _RDK->_check_status();
1611}
1612
1613
1619 _RDK->_check_connection();
1620 _RDK->_send_Line("IsBusy");
1621 _RDK->_send_Item(this);
1622 int busy = _RDK->_recv_Int();
1623 _RDK->_check_status();
1624 return (busy > 0);
1625}
1626
1632 _RDK->_check_connection();
1633 _RDK->_send_Line("Stop");
1634 _RDK->_send_Item(this);
1635 _RDK->_check_status();
1636}
1637
1642void Item::WaitMove(double timeout_sec) const{
1643 _RDK->_check_connection();
1644 _RDK->_send_Line("WaitMove");
1645 _RDK->_send_Item(this);
1646 _RDK->_check_status();
1647 _RDK->_TIMEOUT = (int)(timeout_sec * 1000.0);
1648 _RDK->_check_status();//will wait here;
1649 _RDK->_TIMEOUT = ROBODK_API_TIMEOUT;
1650 //int isbusy = _RDK->Busy(this);
1651 //while (isbusy)
1652 //{
1653 // busy = _RDK->Busy(item);
1654 //}
1655}
1656
1657
1662void Item::setAccuracyActive(int accurate){
1663 _RDK->_check_connection();
1664 _RDK->_send_Line("S_AbsAccOn");
1665 _RDK->_send_Item(this);
1666 _RDK->_send_Int(accurate);
1667 _RDK->_check_status();
1668}
1669
1671
1672
1673// ---- Program item calls -----
1674
1680bool Item::MakeProgram(const QString &filename){
1681 _RDK->_check_connection();
1682 _RDK->_send_Line("MakeProg");
1683 _RDK->_send_Item(this);
1684 _RDK->_send_Line(filename);
1685 int prog_status = _RDK->_recv_Int();
1686 QString prog_log_str = _RDK->_recv_Line();
1687 _RDK->_check_status();
1688 bool success = false;
1689 if (prog_status > 1) {
1690 success = true;
1691 }
1692 return success; // prog_log_str
1693}
1694
1700void Item::setRunType(int program_run_type){
1701 _RDK->_check_connection();
1702 _RDK->_send_Line("S_ProgRunType");
1703 _RDK->_send_Item(this);
1704 _RDK->_send_Int(program_run_type);
1705 _RDK->_check_status();
1706}
1707
1718 _RDK->_check_connection();
1719 _RDK->_send_Line("RunProg");
1720 _RDK->_send_Item(this);
1721 int prog_status = _RDK->_recv_Int();
1722 _RDK->_check_status();
1723 return prog_status;
1724}
1725
1726
1736int Item::RunCode(const QString &parameters){
1737 _RDK->_check_connection();
1738 if (parameters.isEmpty()){
1739 _RDK->_send_Line("RunProg");
1740 _RDK->_send_Item(this);
1741 } else {
1742 _RDK->_send_Line("RunProgParam");
1743 _RDK->_send_Item(this);
1744 _RDK->_send_Line(parameters);
1745 }
1746 int progstatus = _RDK->_recv_Int();
1747 _RDK->_check_status();
1748 return progstatus;
1749}
1750
1756int Item::RunInstruction(const QString &code, int run_type){
1757 _RDK->_check_connection();
1758 _RDK->_send_Line("RunCode2");
1759 _RDK->_send_Item(this);
1760 _RDK->_send_Line(QString(code).replace("\n\n", "<br>").replace("\n", "<br>"));
1761 _RDK->_send_Int(run_type);
1762 int progstatus = _RDK->_recv_Int();
1763 _RDK->_check_status();
1764 return progstatus;
1765}
1766
1771void Item::Pause(double time_ms){
1772 _RDK->_check_connection();
1773 _RDK->_send_Line("RunPause");
1774 _RDK->_send_Item(this);
1775 _RDK->_send_Int((int)(time_ms * 1000.0));
1776 _RDK->_check_status();
1777}
1778
1779
1785void Item::setDO(const QString &io_var, const QString &io_value){
1786 _RDK->_check_connection();
1787 _RDK->_send_Line("setDO");
1788 _RDK->_send_Item(this);
1789 _RDK->_send_Line(io_var);
1790 _RDK->_send_Line(io_value);
1791 _RDK->_check_status();
1792}
1798void Item::setAO(const QString &io_var, const QString &io_value){
1799 _RDK->_check_connection();
1800 _RDK->_send_Line("setAO");
1801 _RDK->_send_Item(this);
1802 _RDK->_send_Line(io_var);
1803 _RDK->_send_Line(io_value);
1804 _RDK->_check_status();
1805}
1806
1811QString Item::getDI(const QString &io_var){
1812 _RDK->_check_connection();
1813 _RDK->_send_Line("getDI");
1814 _RDK->_send_Item(this);
1815 _RDK->_send_Line(io_var);
1816 QString io_value(_RDK->_recv_Line());
1817 _RDK->_check_status();
1818 return io_value;
1819}
1820
1825QString Item::getAI(const QString &io_var){
1826 _RDK->_check_connection();
1827 _RDK->_send_Line("getAI");
1828 _RDK->_send_Item(this);
1829 _RDK->_send_Line(io_var);
1830 QString di_value(_RDK->_recv_Line());
1831 _RDK->_check_status();
1832 return di_value;
1833}
1834
1841void Item::waitDI(const QString &io_var, const QString &io_value, double timeout_ms){
1842 _RDK->_check_connection();
1843 _RDK->_send_Line("waitDI");
1844 _RDK->_send_Item(this);
1845 _RDK->_send_Line(io_var);
1846 _RDK->_send_Line(io_value);
1847 _RDK->_send_Int((int)(timeout_ms * 1000.0));
1848 _RDK->_check_status();
1849}
1850
1851
1861void Item::customInstruction(const QString &name, const QString &path_run, const QString &path_icon, bool blocking, const QString &cmd_run_on_robot){
1862 _RDK->_check_connection();
1863 _RDK->_send_Line("InsCustom2");
1864 _RDK->_send_Item(this);
1865 _RDK->_send_Line(name);
1866 _RDK->_send_Line(path_run);
1867 _RDK->_send_Line(path_icon);
1868 _RDK->_send_Line(cmd_run_on_robot);
1869 _RDK->_send_Int(blocking ? 1 : 0);
1870 _RDK->_check_status();
1871}
1872
1873/*
1879void Item::addMoveJ(const Item &itemtarget){
1880 _RDK->_check_connection();
1881 _RDK->_send_Line("Add_INSMOVE");
1882 _RDK->_send_Item(itemtarget);
1883 _RDK->_send_Item(this);
1884 _RDK->_send_Int(1);
1885 _RDK->_check_status();
1886}
1887
1892void Item::addMoveL(const Item &itemtarget){
1893 _RDK->_check_connection();
1894 _RDK->_send_Line("Add_INSMOVE");
1895 _RDK->_send_Item(itemtarget);
1896 _RDK->_send_Item(this);
1897 _RDK->_send_Int(2);
1898 _RDK->_check_status();
1899}
1900*/
1901
1906void Item::ShowInstructions(bool visible){
1907 _RDK->_check_connection();
1908 _RDK->_send_Line("Prog_ShowIns");
1909 _RDK->_send_Item(this);
1910 _RDK->_send_Int(visible ? 1 : 0);
1911 _RDK->_check_status();
1912}
1913
1918void Item::ShowTargets(bool visible){
1919 _RDK->_check_connection();
1920 _RDK->_send_Line("Prog_ShowTargets");
1921 _RDK->_send_Item(this);
1922 _RDK->_send_Int(visible ? 1 : 0);
1923 _RDK->_check_status();
1924}
1925
1926
1933 _RDK->_check_connection();
1934 _RDK->_send_Line("Prog_Nins");
1935 _RDK->_send_Item(this);
1936 int nins = _RDK->_recv_Int();
1937 _RDK->_check_status();
1938 return nins;
1939}
1940
1951void Item::Instruction(int ins_id, QString &name, int &instype, int &movetype, bool &isjointtarget, Mat &target, tJoints &joints){
1952 _RDK->_check_connection();
1953 _RDK->_send_Line("Prog_GIns");
1954 _RDK->_send_Item(this);
1955 _RDK->_send_Int(ins_id);
1956 name = _RDK->_recv_Line();
1957 instype = _RDK->_recv_Int();
1958 movetype = 0;
1959 isjointtarget = false;
1960 //target = null;
1961 //joints = null;
1962 if (instype == RoboDK::INS_TYPE_MOVE) {
1963 movetype = _RDK->_recv_Int();
1964 isjointtarget = _RDK->_recv_Int() > 0 ? true : false;
1965 target = _RDK->_recv_Pose();
1966 _RDK->_recv_Array(&joints);
1967 }
1968 _RDK->_check_status();
1969}
1970
1981void Item::setInstruction(int ins_id, const QString &name, int instype, int movetype, bool isjointtarget, const Mat &target, const tJoints &joints){
1982 _RDK->_check_connection();
1983 _RDK->_send_Line("Prog_SIns");
1984 _RDK->_send_Item(this);
1985 _RDK->_send_Int(ins_id);
1986 _RDK->_send_Line(name);
1987 _RDK->_send_Int(instype);
1988 if (instype == RoboDK::INS_TYPE_MOVE)
1989 {
1990 _RDK->_send_Int(movetype);
1991 _RDK->_send_Int(isjointtarget ? 1 : 0);
1992 _RDK->_send_Pose(target);
1993 _RDK->_send_Array(&joints);
1994 }
1995 _RDK->_check_status();
1996}
1997
1998
2005 _RDK->_check_connection();
2006 _RDK->_send_Line("G_ProgInsList");
2007 _RDK->_send_Item(this);
2008 _RDK->_recv_Matrix2D(&instructions);
2009 int errors = _RDK->_recv_Int();
2010 _RDK->_check_status();
2011 return errors;
2012}
2013
2014
2015
2026double Item::Update(int collision_check, int timeout_sec, double *out_nins_time_dist, double mm_step, double deg_step){
2027 _RDK->_check_connection();
2028 _RDK->_send_Line("Update2");
2029 _RDK->_send_Item(this);
2030 double values[5];
2031 values[0] = collision_check;
2032 values[1] = mm_step;
2033 values[2] = deg_step;
2034 _RDK->_send_Array(values, 3);
2035 _RDK->_TIMEOUT = timeout_sec * 1000;
2036 double return_values[10];
2037 int nvalues = 10;
2038 _RDK->_recv_Array(return_values, &nvalues);
2039 _RDK->_TIMEOUT = ROBODK_API_TIMEOUT;
2040 QString readable_msg = _RDK->_recv_Line();
2041 _RDK->_check_status();
2042 double ratio_ok = return_values[3];
2043 if (out_nins_time_dist != nullptr)
2044 {
2045 out_nins_time_dist[0] = return_values[0]; // number of correct instructions
2046 out_nins_time_dist[1] = return_values[1]; // estimated time to complete the program (cycle time)
2047 out_nins_time_dist[2] = return_values[2]; // estimated travel distance
2048 }
2049 return ratio_ok;
2050}
2051
2052
2065int Item::InstructionListJoints(QString &error_msg, tMatrix2D **joint_list, double mm_step, double deg_step, const QString &save_to_file, bool collision_check, int result_flag, double time_step_s){
2066 _RDK->_check_connection();
2067 _RDK->_send_Line("G_ProgJointList");
2068 _RDK->_send_Item(this);
2069 double step_mm_deg[5] = { mm_step, deg_step, collision_check ? 1.0 : 0.0, (double) result_flag, time_step_s };
2070 _RDK->_send_Array(step_mm_deg, 5);
2071 _RDK->_TIMEOUT = 3600 * 1000;
2072 //joint_list = save_to_file;
2073 if (save_to_file.isEmpty()) {
2074 _RDK->_send_Line("");
2075 _RDK->_recv_Matrix2D(joint_list);
2076 } else {
2077 _RDK->_send_Line(save_to_file);
2078 joint_list = nullptr;
2079 }
2080 int error_code = _RDK->_recv_Int();
2081 _RDK->_TIMEOUT = ROBODK_API_TIMEOUT;
2082 error_msg = _RDK->_recv_Line();
2083 _RDK->_check_status();
2084 return error_code;
2085}
2086
2095QString Item::setParam(const QString &param, const QString &value){
2096 _RDK->_check_connection();
2097 _RDK->_send_Line("ICMD");
2098 _RDK->_send_Item(this);
2099 _RDK->_send_Line(param);
2100 _RDK->_send_Line(value);
2101 QString result =_RDK->_recv_Line();
2102 _RDK->_check_status();
2103 return result;
2104}
2105
2106void Item::setParam(const QString& param, const QByteArray& value) {
2107 _RDK->_check_connection();
2108 _RDK->_send_Line("S_ItmDataParam");
2109 _RDK->_send_Item(this);
2110 _RDK->_send_Line(param);
2111 _RDK->_send_Bytes(value);
2112 _RDK->_check_status();
2113}
2114
2115QByteArray Item::getParam(const QString& param) {
2116 _RDK->_check_connection();
2117 _RDK->_send_Line("G_ItmDataParam");
2118 _RDK->_send_Item(this);
2119 _RDK->_send_Line(param);
2120 QByteArray result = _RDK->_recv_Bytes();
2121 _RDK->_check_status();
2122 return result;
2123}
2124
2130 _RDK->Finish();
2131 return true;
2132}
2133
2134quint64 Item::GetID(){
2135 return _PTR;
2136}
2137
2138//---------------------------------------- add more
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151//---------------------------------------------------------------------------------------------------
2152//---------------------------------------------------------------------------------------------------
2153//---------------------------------------------------------------------------------------------------
2154//---------------------------------------------------------------------------------------------------
2156RoboDK::RoboDK(QAbstractSocket* socket, bool fUseExceptions) {
2157 _COM = socket;
2158 _USE_EXCPETIONS = fUseExceptions;
2159 _OWN_SOCKET = false;
2160 _TIMEOUT = ROBODK_API_TIMEOUT;
2161 _PROCESS = 0;
2162 _PORT = ROBODK_DEFAULT_PORT;
2163 _ROBODK_BIN = ROBODK_DEFAULT_PATH_BIN;
2164 _connect_smart();
2165}
2166
2167RoboDK::RoboDK(const QString &robodk_ip, int com_port, const QString &args, const QString &path, bool fUseExceptions) {
2168 _COM = nullptr;
2169 _USE_EXCPETIONS = fUseExceptions;
2170 _OWN_SOCKET = true;
2171 _IP = robodk_ip;
2172 _TIMEOUT = ROBODK_API_TIMEOUT;
2173 _PROCESS = 0;
2174 _PORT = com_port;
2175 _ROBODK_BIN = path;
2176 if (_PORT < 0){
2177 _PORT = ROBODK_DEFAULT_PORT;
2178 }
2179 if (_ROBODK_BIN.isEmpty()){
2180 _ROBODK_BIN = ROBODK_DEFAULT_PATH_BIN;
2181 }
2182 _ARGUMENTS = args;
2183 if (com_port > 0){
2184 _ARGUMENTS.append(" /PORT=" + QString::number(com_port));
2185 }
2186 _connect_smart();
2187}
2188
2189RoboDK::~RoboDK(){
2190 _disconnect();
2191}
2192
2193quint64 RoboDK::ProcessID(){
2194 if (_PROCESS == 0) {
2195 QString response = Command("MainProcess_ID");
2196 _PROCESS = response.toULongLong();
2197 }
2198 return _PROCESS;
2199}
2200
2201quint64 RoboDK::WindowID(){
2202 qint64 window_id;
2203 QString response = Command("MainWindow_ID");
2204 window_id = response.toULongLong();
2205 return window_id;
2206}
2207
2208bool RoboDK::Connected(){
2209 return _connected();
2210}
2211
2212bool RoboDK::Connect(){
2213 return _connect();
2214}
2220 _disconnect();
2221}
2227 Disconnect();
2228}
2229
2230// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2231// public methods
2238Item RoboDK::getItem(QString name, int itemtype){
2239 _check_connection();
2240 if (itemtype < 0){
2241 _send_Line("G_Item");
2242 _send_Line(name);
2243 } else {
2244 _send_Line("G_Item2");
2245 _send_Line(name);
2246 _send_Int(itemtype);
2247 }
2248 Item item = _recv_Item();
2249 _check_status();
2250 return item;
2251}
2252
2259QStringList RoboDK::getItemListNames(int filter){
2260 _check_connection();
2261 if (filter < 0) {
2262 _send_Line("G_List_Items");
2263 } else {
2264 _send_Line("G_List_Items_Type");
2265 _send_Int(filter);
2266 }
2267 qint32 numitems = _recv_Int();
2268 QStringList listnames;
2269 for (int i = 0; i < numitems; i++) {
2270 listnames.append(_recv_Line());
2271 }
2272 _check_status();
2273 return listnames;
2274}
2275
2282QList<Item> RoboDK::getItemList(int filter) {
2283 _check_connection();
2284 if (filter < 0) {
2285 _send_Line("G_List_Items_ptr");
2286 } else {
2287 _send_Line("G_List_Items_Type_ptr");
2288 _send_Int(filter);
2289 }
2290 int numitems = _recv_Int();
2291 QList<Item> listitems;
2292 for (int i = 0; i < numitems; i++) {
2293 listitems.append(_recv_Item());
2294 }
2295 _check_status();
2296 return listitems;
2297}
2298
2300
2308Item RoboDK::ItemUserPick(const QString &message, int itemtype) {
2309 _check_connection();
2310 _send_Line("PickItem");
2311 _send_Line(message);
2312 _send_Int(itemtype);
2313 _TIMEOUT = 3600 * 1000;
2314 Item item = _recv_Item();//item);
2315 _TIMEOUT = ROBODK_API_TIMEOUT;
2316 _check_status();
2317 return item;
2318}
2319
2324 _check_connection();
2325 _send_Line("RAISE");
2326 _check_status();
2327}
2328
2333 _check_connection();
2334 _send_Line("HIDE");
2335 _check_status();
2336}
2337
2342 _check_connection();
2343 _send_Line("QUIT");
2344 _check_status();
2345 _disconnect();
2346 _PROCESS = 0;
2347}
2348
2350{
2351 _check_connection();
2352 _send_Line("Version");
2353 QString appName = _recv_Line();
2354 int bitArch = _recv_Int();
2355 QString ver4 = _recv_Line();
2356 QString dateBuild = _recv_Line();
2357 _check_status();
2358 return ver4;
2359
2360}
2361
2362
2367void RoboDK::setWindowState(int windowstate){
2368 _check_connection();
2369 _send_Line("S_WindowState");
2370 _send_Int(windowstate);
2371 _check_status();
2372}
2373
2374
2380 _check_connection();
2381 _send_Line("S_RoboDK_Rights");
2382 _send_Int(flags);
2383 _check_status();
2384}
2385
2391void RoboDK::setFlagsItem(Item item, int flags){
2392 _check_connection();
2393 _send_Line("S_Item_Rights");
2394 _send_Item(item);
2395 _send_Int(flags);
2396 _check_status();
2397}
2398
2405 _check_connection();
2406 _send_Line("S_Item_Rights");
2407 _send_Item(item);
2408 int flags = _recv_Int();
2409 _check_status();
2410 return flags;
2411}
2412
2418void RoboDK::ShowMessage(const QString &message, bool popup){
2419 _check_connection();
2420 if (popup)
2421 {
2422 _send_Line("ShowMessage");
2423 _send_Line(message);
2424 _TIMEOUT = 3600 * 1000;
2425 _check_status();
2426 _TIMEOUT = ROBODK_API_TIMEOUT;
2427 }
2428 else
2429 {
2430 _send_Line("ShowMessageStatus");
2431 _send_Line(message);
2432 _check_status();
2433 }
2434
2435}
2436
2441void RoboDK::Copy(const Item &tocopy){
2442 _check_connection();
2443 _send_Line("Copy");
2444 _send_Item(tocopy);
2445 _check_status();
2446}
2447
2453Item RoboDK::Paste(const Item *paste_to){
2454 _check_connection();
2455 _send_Line("Paste");
2456 _send_Item(paste_to);
2457 Item newitem = _recv_Item();
2458 _check_status();
2459 return newitem;
2460}
2461
2468Item RoboDK::AddFile(const QString &filename, const Item *parent){
2469 _check_connection();
2470 _send_Line("Add");
2471 _send_Line(filename);
2472 _send_Item(parent);
2473 _TIMEOUT = 3600 * 1000;
2474 Item newitem = _recv_Item();
2475 _TIMEOUT = ROBODK_API_TIMEOUT;
2476 _check_status();
2477 return newitem;
2478}
2479
2485void RoboDK::Save(const QString &filename, const Item *itemsave){
2486 _check_connection();
2487 _send_Line("Save");
2488 _send_Line(filename);
2489 _send_Item(itemsave);
2490 _check_status();
2491}
2492
2505Item RoboDK::AddShape(tMatrix2D *trianglePoints, Item *addTo, bool shapeOverride, Color *color)
2506{
2507 double colorArray[4] = {0.6,0.6,0.8,1.0};
2508 if (color != nullptr){
2509 colorArray[0] = color->r;
2510 colorArray[1] = color->g;
2511 colorArray[2] = color->b;
2512 colorArray[3] = color->a;
2513 }
2514 _check_connection();
2515 _send_Line("AddShape3");
2516 _send_Matrix2D(trianglePoints);
2517 _send_Item(addTo);
2518 _send_Int(shapeOverride? 1 : 0);
2519 _send_Array(colorArray,4);
2520 Item newitem = _recv_Item();
2521 _check_status();
2522 return newitem;
2523}
2524
2541Item RoboDK::AddCurve(tMatrix2D *curvePoints, Item *referenceObject, bool addToRef, int ProjectionType)
2542{
2543 _check_connection();
2544 _send_Line("AddWire");
2545 _send_Matrix2D(curvePoints);
2546 _send_Item(referenceObject);
2547 _send_Int(addToRef ? 1:0);
2548 _send_Int(ProjectionType);
2549 Item newitem = _recv_Item();
2550 _check_status();
2551 return newitem;
2552}
2553
2562Item RoboDK::AddPoints(tMatrix2D *points, Item *referenceObject, bool addToRef, int ProjectionType)
2563{
2564 _check_connection();
2565 _send_Line("AddPoints");
2566 _send_Matrix2D(points);
2567 _send_Item(referenceObject);
2568 _send_Int(addToRef? 1 : 0);
2569 _send_Int(ProjectionType);Item newitem = _recv_Item();
2570 _check_status();
2571 return newitem;
2572}
2573
2574void RoboDK::ProjectPoints(tMatrix2D *points, tMatrix2D **projected, Item objectProject, int ProjectionType)
2575{
2576 _check_connection();
2577 _send_Line("ProjectPoints");
2578 _send_Matrix2D(points);
2579 _send_Item(objectProject);
2580 _send_Int(ProjectionType);
2581 _recv_Matrix2D(projected);
2582 _check_status();
2583}
2584
2590Item RoboDK::AddStation(const QString &name)
2591{
2592 _check_connection();
2593 _send_Line("NewStation");
2594 _send_Line(name);
2595 Item newitem = _recv_Item();
2596 _check_status();
2597 return newitem;
2598}
2599
2600
2601
2602
2607 _check_connection();
2608 _send_Line("RemoveStn");
2609 _check_status();
2610}
2611
2619Item RoboDK::AddTarget(const QString &name, Item *itemparent, Item *itemrobot){
2620 _check_connection();
2621 _send_Line("Add_TARGET");
2622 _send_Line(name);
2623 _send_Item(itemparent);
2624 _send_Item(itemrobot);
2625 Item newitem = _recv_Item();
2626 _check_status();
2627 return newitem;
2628}
2629
2636Item RoboDK::AddFrame(const QString &name, Item *itemparent){
2637 _check_connection();
2638 _send_Line("Add_FRAME");
2639 _send_Line(name);
2640 _send_Item(itemparent);
2641 Item newitem = _recv_Item();
2642 _check_status();
2643 return newitem;
2644}
2645
2652Item RoboDK::AddProgram(const QString &name, Item *itemrobot){
2653 _check_connection();
2654 _send_Line("Add_PROG");
2655 _send_Line(name);
2656 _send_Item(itemrobot);
2657 Item newitem = _recv_Item();
2658 _check_status();
2659 return newitem;
2660}
2661
2670Item RoboDK::AddMachiningProject(const QString &name, Item *itemrobot)
2671{
2672 _check_connection();
2673 _send_Line("Add_MACHINING");
2674 _send_Line(name);
2675 _send_Item(itemrobot);
2676 Item newitem = _recv_Item();
2677 _check_status();
2678 return newitem;
2679}
2680
2681
2682
2684{
2685 _check_connection();
2686 _send_Line("G_AllStn");
2687 int nstn = _recv_Int();
2688 QList<Item> *listStn = new QList<Item>();
2689 for (int i = 0;i < nstn;i++){
2690 Item station = _recv_Item();
2691 listStn->append(station);
2692 }
2693 _check_status();
2694 return *listStn;
2695}
2696
2697
2703 _check_connection();
2704 _send_Line("G_ActiveStn");
2705 Item station = _recv_Item();
2706 _check_status();
2707 return station;
2708}
2709
2715 _check_connection();
2716 _send_Line("S_ActiveStn");
2717 _send_Item(station);
2718 _check_status();
2719}
2720
2721
2722//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2728int RoboDK::RunProgram(const QString &function_w_params){
2729 return RunCode(function_w_params, true);
2730}
2731
2738int RoboDK::RunCode(const QString &code, bool code_is_fcn_call){
2739 _check_connection();
2740 _send_Line("RunCode");
2741 _send_Int(code_is_fcn_call ? 1 : 0);
2742 _send_Line(code);
2743 int prog_status = _recv_Int();
2744 _check_status();
2745 return prog_status;
2746}
2747
2753void RoboDK::RunMessage(const QString &message, bool message_is_comment){
2754 _check_connection();
2755 _send_Line("RunMessage");
2756 _send_Int(message_is_comment ? 1 : 0);
2757 _send_Line(message);
2758 _check_status();
2759}
2760
2765void RoboDK::Render(bool always_render){
2766 bool auto_render = !always_render;
2767 _check_connection();
2768 _send_Line("Render");
2769 _send_Int(auto_render ? 1 : 0);
2770 _check_status();
2771}
2772
2778{
2779 _check_connection();
2780 _send_Line("Refresh");
2781 _send_Int(0);
2782 _check_status();
2783}
2784
2791bool RoboDK::IsInside(Item object_inside, Item object_parent){
2792 _check_connection();
2793 _send_Line("IsInside");
2794 _send_Item(object_inside);
2795 _send_Item(object_parent);
2796 int inside = _recv_Int();
2797 _check_status();
2798 return inside > 0;
2799}
2800
2806int RoboDK::setCollisionActive(int check_state){
2807 _check_connection();
2808 _send_Line("Collision_SetState");
2809 _send_Int(check_state);
2810 int ncollisions = _recv_Int();
2811 _check_status();
2812 return ncollisions;
2813}
2814
2825bool RoboDK::setCollisionActivePair(int check_state, Item item1, Item item2, int id1, int id2){
2826 _check_connection();
2827 _send_Line("Collision_SetPair");
2828 _send_Item(item1);
2829 _send_Item(item2);
2830 _send_Int(id1);
2831 _send_Int(id2);
2832 _send_Int(check_state);
2833 int success = _recv_Int();
2834 _check_status();
2835 return success > 0;
2836}
2837
2843 _check_connection();
2844 _send_Line("Collisions");
2845 int ncollisions = _recv_Int();
2846 _check_status();
2847 return ncollisions;
2848}
2849
2856int RoboDK::Collision(Item item1, Item item2){
2857 _check_connection();
2858 _send_Line("Collided");
2859 _send_Item(item1);
2860 _send_Item(item2);
2861 int ncollisions = _recv_Int();
2862 _check_status();
2863 return ncollisions;
2864}
2865
2866QList<Item> RoboDK::getCollisionItems(QList<int> link_id_list)
2867{
2868 _check_connection();
2869 _send_Line("Collision Items");
2870 int nitems = _recv_Int();
2871 QList<Item> itemList = QList<Item>();
2872 if (!link_id_list.isEmpty()){
2873 link_id_list.clear();
2874 }
2875 for (int i = 0; i < nitems; i++){
2876 itemList.append(_recv_Item());
2877 int linkId = _recv_Int();
2878 if (!link_id_list.isEmpty()){
2879 link_id_list.append(linkId);
2880 }
2881 int collisionTimes = _recv_Int();
2882 }
2883 _check_status();
2884 return itemList;
2885}
2886
2892 _check_connection();
2893 _send_Line("SimulateSpeed");
2894 _send_Int((int)(speed * 1000.0));
2895 _check_status();
2896}
2897
2903 _check_connection();
2904 _send_Line("GetSimulateSpeed");
2905 double speed = ((double)_recv_Int()) / 1000.0;
2906 _check_status();
2907 return speed;
2908}
2919void RoboDK::setRunMode(int run_mode){
2920 _check_connection();
2921 _send_Line("S_RunMode");
2922 _send_Int(run_mode);
2923 _check_status();
2924}
2925
2935 _check_connection();
2936 _send_Line("G_RunMode");
2937 int runmode = _recv_Int();
2938 _check_status();
2939 return runmode;
2940}
2947QList<QPair<QString,QString>> RoboDK::getParams(){
2948 _check_connection();
2949 _send_Line("G_Params");
2950 QList<QPair<QString,QString>> paramlist;
2951 int nparam = _recv_Int();
2952 for (int i = 0; i < nparam; i++) {
2953 QString param = _recv_Line();
2954 QString value = _recv_Line();
2955 paramlist.append(qMakePair(param, value));
2956 }
2957 _check_status();
2958 return paramlist;
2959}
2960
2972QString RoboDK::getParam(const QString &param){
2973 _check_connection();
2974 _send_Line("G_Param");
2975 _send_Line(param);
2976 QString value = _recv_Line();
2977 if (value.startsWith("UNKNOWN ")) {
2978 value = "";
2979 }
2980 _check_status();
2981 return value;
2982}
2983
2991void RoboDK::setParam(const QString &param, const QString &value){
2992 _check_connection();
2993 _send_Line("S_Param");
2994 _send_Line(param);
2995 _send_Line(value);
2996 _check_status();
2997}
2998
3005QString RoboDK::Command(const QString &cmd, const QString &value){
3006 _check_connection();
3007 _send_Line("SCMD");
3008 _send_Line(cmd);
3009 _send_Line(value);
3010 QString answer = _recv_Line();
3011 _check_status();
3012 return answer;
3013}
3014
3015bool RoboDK::LaserTrackerMeasure(tXYZ xyz, tXYZ estimate, bool search)
3016{
3017 _check_connection();
3018 _send_Line("MeasLT");
3019 _send_XYZ(estimate);
3020 _send_Int(search ? 1 : 0);
3021 _recv_XYZ(xyz);
3022 _check_status();
3023 if (xyz[0]*xyz[0] + xyz[1]*xyz[1] + xyz[2]*xyz[2] < 0.0001){
3024 return false;
3025 }
3026
3027 return true;
3028}
3029
3030void RoboDK::ShowAsCollided(QList<Item> itemList, QList<bool> collidedList, QList<int> *robot_link_id)
3031{
3032 int nitems = qMin(itemList.length(),collidedList.length());
3033 if (robot_link_id != nullptr){
3034 nitems = qMin(nitems, robot_link_id->length());
3035 }
3036 _check_connection();
3037 _send_Line("ShowAsCollidedList");
3038 _send_Int(nitems);
3039 for (int i = 0; i < nitems; i++){
3040 _send_Item(itemList[i]);
3041 _send_Int(collidedList[i] ? 1 : 0);
3042 int link_id = 0;
3043 if (robot_link_id != nullptr){
3044 link_id = robot_link_id->at(i);
3045 }
3046 _send_Int(link_id);
3047 }
3048 _check_status();
3049}
3050
3051//---------------------------------------------- ADD MORE (getParams, setParams, calibrate TCP, calibrate ref...)
3052
3053
3064void RoboDK::CalibrateTool(tMatrix2D *poses_joints, tXYZ tcp_xyz, int format, int algorithm, Item *robot, double *error_stats){
3065 _check_connection();
3066 _send_Line("CalibTCP2");
3067 _send_Matrix2D(poses_joints);
3068 _send_Int(format);
3069 _send_Int(algorithm);
3070 _send_Item(robot);
3071 int nxyz = 3;
3072 _recv_Array(tcp_xyz, &nxyz);
3073 if (error_stats != nullptr){
3074 _recv_Array(error_stats);
3075 } else {
3076 double errors_ignored[20];
3077 _recv_Array(errors_ignored);
3078 }
3079 tMatrix2D *error_graph = Matrix2D_Create();
3080 _recv_Matrix2D(&error_graph);
3081 Matrix2D_Delete(&error_graph);
3082 _check_status();
3083}
3084
3093Mat RoboDK::CalibrateReference(tMatrix2D *poses_joints, int method, bool use_joints, Item *robot){
3094 _check_connection();
3095 _send_Line("CalibFrame");
3096 _send_Matrix2D(poses_joints);
3097 _send_Int(use_joints ? -1 : 0);
3098 _send_Int(method);
3099 _send_Item(robot);
3100 Mat reference_pose = _recv_Pose();
3101 double error_stats[20];
3102 _recv_Array(error_stats);
3103 _check_status();
3104 return reference_pose;
3105}
3106
3107
3108int RoboDK::ProgramStart(const QString &progname, const QString &defaultfolder, const QString &postprocessor, Item *robot){
3109 _check_connection();
3110 _send_Line("ProgramStart");
3111 _send_Line(progname);
3112 _send_Line(defaultfolder);
3113 _send_Line(postprocessor);
3114 _send_Item(robot);
3115 int errors = _recv_Int();
3116 _check_status();
3117 return errors;
3118}
3119
3124void RoboDK::setViewPose(const Mat &pose){
3125 _check_connection();
3126 _send_Line("S_ViewPose");
3127 _send_Pose(pose);
3128 _check_status();
3129}
3130
3136 _check_connection();
3137 _send_Line("G_ViewPose");
3138 Mat pose = _recv_Pose();
3139 _check_status();
3140 return pose;
3141}
3142
3143//INCOMPLETE function!
3144/*bool RoboDK::SetRobotParams(Item *robot, tMatrix2D dhm, Mat poseBase, Mat poseTool)
3145{
3146 _check_connection();
3147 _send_Line("S_AbsAccParam");
3148 _send_Item(robot);
3149 Mat r2b;
3150 r2b.setToIdentity();
3151 _send_Pose(r2b);
3152 _send_Pose(poseBase);
3153 _send_Pose(poseTool);
3154 int *ndofs = dhm.size;
3155 _send_Int(*ndofs);
3156 for (int i = 0; i < *ndofs; i++){
3157 _send_Array(dhm);
3158 }
3159
3160 _send_Pose(poseBase);
3161 _send_Pose(poseTool);
3162 _send_Int(*ndofs);
3163 for (int i = 0; i < *ndofs; i++){
3164 _send_Array(dhm[i]);
3165 }
3166
3167 _send_Array(nullptr);
3168 _send_Array(nullptr);
3169 _check_status();
3170 return true;
3171}*/
3172
3173Item RoboDK::Cam2D_Add(const Item &item_object, const QString &cam_params, const Item *cam_item){
3174 _check_connection();
3175 _send_Line("Cam2D_PtrAdd");
3176 _send_Item(item_object);
3177 _send_Item(cam_item);
3178 _send_Line(cam_params);
3179 Item cam_item_return = _recv_Item();
3180 _check_status();
3181 return cam_item_return;
3182}
3183int RoboDK::Cam2D_Snapshot(const QString &file_save_img, const Item &cam_item, const QString &params){
3184 _check_connection();
3185 _send_Line("Cam2D_PtrSnapshot");
3186 _send_Item(cam_item);
3187 _send_Line(file_save_img);
3188 _send_Line(params);
3189 _TIMEOUT = 3600 * 1000;
3190 int status = _recv_Int();
3191 _TIMEOUT = ROBODK_API_TIMEOUT;
3192 _check_status();
3193 return status;
3194}
3195
3196int RoboDK::Cam2D_SetParams(const QString &params, const Item &cam_item){
3197 _check_connection();
3198 _send_Line("Cam2D_PtrSetParams");
3199 _send_Item(cam_item);
3200 _send_Line(params);
3201 int status = _recv_Int();
3202 _check_status();
3203 return status;
3204}
3205
3206Item RoboDK::getCursorXYZ(int x, int y, tXYZ xyzStation)
3207{
3208 _check_connection();
3209 _send_Line("Proj2d3d");
3210 _send_Int(x);
3211 _send_Int(y);
3212 int selection = _recv_Int();
3213 Item selectedItem = _recv_Item();
3214 tXYZ xyz;
3215 _recv_XYZ(xyz);
3216 if (xyzStation != nullptr){
3217 xyzStation[0] = xyz[0];
3218 xyzStation[1] = xyz[1];
3219 xyzStation[2] = xyz[2];
3220 }
3221 _check_status();
3222 return selectedItem;
3223}
3224
3226 GetPointsResult result;
3227 result.featureType = RoboDK::FEATURE_NONE;
3228 result.featureId = 0;
3229 result.points = nullptr;
3230
3231 if (featureType < FEATURE_HOVER_OBJECT_MESH) {
3232 return result;
3233 }
3234
3235 _check_connection();
3236 _send_Line("G_ObjPoint");
3237 _send_Item(nullptr);
3238 _send_Int(featureType);
3239 _send_Int(0);
3240
3241 if (featureType == RoboDK::FEATURE_HOVER_OBJECT_MESH) {
3242 _recv_Matrix2D(&result.points);
3243 }
3244
3245 result.item = _recv_Item();
3246 _recv_Int(); // Skip isFrame
3247 result.featureType = _recv_Int();
3248 result.featureId = _recv_Int();
3249 result.featureName = _recv_Line();
3250 _check_status();
3251 return result;
3252}
3253
3254
3256
3257
3263 _check_connection();
3264 _send_Line("G_License");
3265 QString license = _recv_Line();
3266 _check_status();
3267 return license;
3268}
3269
3274QList<Item> RoboDK::Selection(){
3275 _check_connection();
3276 _send_Line("G_Selection");
3277 int nitems = _recv_Int();
3278 QList<Item> list_items;
3279 for (int i = 0; i < nitems; i++) {
3280 list_items.append(_recv_Item());
3281 }
3282 _check_status();
3283 return list_items;
3284}
3285
3290void RoboDK::setSelection(QList<Item> list_items){
3291 _check_connection();
3292 _send_Line("S_Selection");
3293 _send_Int(list_items.length());
3294 for (int i = 0; i < list_items.length(); i++) {
3295 _send_Item(list_items[i]);
3296 }
3297 _check_status();
3298}
3299
3300
3304void RoboDK::PluginLoad(const QString &pluginName, int load){
3305 _check_connection();
3306 _send_Line("PluginLoad");
3307 _send_Line(pluginName);
3308 _send_Int(load);
3309 _check_status();
3310}
3311
3315QString RoboDK::PluginCommand(const QString &pluginName, const QString &pluginCommand, const QString &pluginValue){
3316 _check_connection();
3317 _send_Line("PluginCommand");
3318 _send_Line(pluginName);
3319 _send_Line(pluginCommand);
3320 _send_Line(pluginValue);
3321 QString result = _recv_Line();
3322 _check_status();
3323 return result;
3324}
3325
3326
3331Item RoboDK::Popup_ISO9283_CubeProgram(Item *robot, tXYZ center, double side, bool blocking){
3332 //_require_build(5177);
3333 Item iso_program;
3334 _check_connection();
3335 if (center == nullptr){
3336 _send_Line("Popup_ProgISO9283");
3337 _send_Item(robot);
3338 _TIMEOUT = 3600 * 1000;
3339 iso_program = _recv_Item();
3340 _TIMEOUT = ROBODK_API_TIMEOUT;
3341 _check_status();
3342 } else {
3343 _send_Line("Popup_ProgISO9283_Param");
3344 _send_Item(robot);
3345 double values[5];
3346 values[0] = center[0];
3347 values[1] = center[1];
3348 values[2] = center[2];
3349 values[3] = side;
3350 values[4] = blocking ? 1 : 0;
3351 _send_Array(values, 4);
3352 if (blocking){
3353 _TIMEOUT = 3600 * 1000;
3354 iso_program = _recv_Item();
3355 _TIMEOUT = ROBODK_API_TIMEOUT;
3356 _check_status();
3357 }
3358 }
3359 return iso_program;
3360}
3361
3362
3363
3364
3365bool RoboDK::FileSet(const QString &path_file_local, const QString &file_remote, bool load_file, Item *attach_to){
3366 if (!_check_connection()){ return false; }
3367 if (!_send_Line("FileRecvBin")){ return false; }
3368 QFile file(path_file_local);
3369 if (!_send_Line(file_remote)){ return false; }
3370 int nbytes = file.size();
3371 if (!_send_Int(nbytes)){ return false; }
3372 if (!_send_Item(attach_to)){ return false; }
3373 if (!_send_Int(load_file ? 1 : 0)){ return false; }
3374 if (!_check_status()){ return false; }
3375 int sz_sent = 0;
3376 if (!file.open(QFile::ReadOnly)){
3377 return false;
3378 }
3379 while (true){
3380 QByteArray buffer(file.read(1024));
3381 if (buffer.size() == 0){
3382 break;
3383 }
3384 // warning! Nothing guarantees that all bytes are sent
3385 sz_sent += _COM->write(buffer);
3386 qDebug() << "Sending file " << path_file_local << 100*sz_sent/nbytes;
3387 }
3388 file.close();
3389 return true;
3390}
3391
3392bool RoboDK::FileGet(const QString &path_file_local, Item *station, const QString path_file_remote){
3393 if (!_check_connection()){ return false; }
3394 if (!_send_Line("FileSendBin")){ return false; }
3395 if (!_send_Item(station)){ return false; }
3396 if (!_send_Line(path_file_remote)){ return false; }
3397 int nbytes = _recv_Int();
3398 int remaining = nbytes;
3399 QFile file(path_file_local);
3400 if (!file.open(QFile::WriteOnly)){
3401 qDebug() << "Can not open file for writting " << path_file_local;
3402 return false;
3403 }
3404 while (remaining > 0){
3405 QByteArray buffer(_COM->read(qMin(remaining, 1024)));
3406 remaining -= buffer.size();
3407 file.write(buffer);
3408 }
3409 file.close();
3410 if (!_check_status()){ return false;}
3411 return true;
3412}
3413
3414
3415bool RoboDK::EmbedWindow(QString window_name, QString docked_name, int size_w, int size_h, quint64 pid, int area_add, int area_allowed, int timeout)
3416{
3417 if (!_check_connection()){ return false; }
3418 if (docked_name == "") {
3419 docked_name = window_name;
3420 }
3421 _check_connection();
3422 _send_Line("WinProcDock");
3423 _send_Line(docked_name);
3424 _send_Line(window_name);
3425 double sizeArray[2] = {(double)size_w, (double)size_h};
3426 _send_Array(sizeArray,2);
3427 _send_Line(QString::number(pid));
3428 _send_Int(area_allowed);
3429 _send_Int(area_add);
3430 _send_Int(timeout);
3431 int result = _recv_Int();
3432 _check_status();
3433 return result > 0;
3434}
3435
3436
3437bool RoboDK::EventsListen()
3438{
3439 _COM_EVT = new QTcpSocket();
3440 if (_IP.isEmpty()){
3441 _COM_EVT->connectToHost("127.0.0.1", _PORT); //QHostAddress::LocalHost, _PORT);
3442 } else {
3443 _COM_EVT->connectToHost(_IP, _PORT);
3444 }
3445 qDebug() << _COM_EVT->state();
3446 _COM_EVT->waitForConnected(_TIMEOUT);
3447 qDebug() << _COM_EVT->state();
3448 _send_Line("RDK_EVT", _COM_EVT);
3449 _send_Int(0, _COM_EVT);
3450 QString response = _recv_Line(_COM_EVT);
3451 qDebug() << response;
3452 int ver_evt = _recv_Int(_COM_EVT);
3453 int status = _recv_Int(_COM_EVT);
3454 if (response != "RDK_EVT" || status != 0)
3455 {
3456 return false;
3457 }
3458 //_COM_EVT.ReceiveTimeout = 3600 * 1000;
3459 //return EventsLoop();
3460 return true;
3461}
3462
3463bool RoboDK::WaitForEvent(int &evt, Item &itm)
3464{
3465 evt = _recv_Int(_COM_EVT);
3466 itm = _recv_Item(_COM_EVT);
3467 return true;
3468}
3469
3470//Receives 24 doubles of data from the event loop
3471bool RoboDK::Event_Receive_3D_POS(double *data, int *valueCount) {
3472 return _recv_Array(data,valueCount,_COM_EVT);
3473}
3474
3475//Receives the 3 bytes of mouse data
3476bool RoboDK::Event_Receive_Mouse_data(int *data) {
3477 data[0] = _recv_Int(_COM_EVT);
3478 data[1] = _recv_Int(_COM_EVT);
3479 data[2] = _recv_Int(_COM_EVT);
3480
3481 return true;
3482}
3483
3484bool RoboDK::Event_Receive_Event_Moved(Mat *pose_rel_out) {
3485 int nvalues = _recv_Int(_COM_EVT);
3486 Mat pose_rel = _recv_Pose(_COM_EVT);
3487 *pose_rel_out = pose_rel;
3488 if (nvalues > 16)
3489 {
3490 return false;
3491 // future compatibility
3492 }
3493
3494 return true;
3495}
3496
3497bool RoboDK::Event_Connected()
3498{
3499 return _COM_EVT != nullptr && _COM_EVT->state() == QTcpSocket::ConnectedState;
3500}
3501
3502//-------------------------- private ---------------------------------------
3503
3504bool RoboDK::_connected(){
3505 return _COM != nullptr && _COM->state() == QTcpSocket::ConnectedState;
3506}
3507
3508
3509bool RoboDK::_check_connection(){
3510 if (_connected()){
3511 return true;
3512 }
3513 bool connection_ok = _connect_smart();
3514 //if (!connection_ok){
3515 // throw -1;
3516 //}
3517 return connection_ok;
3518}
3519
3520bool RoboDK::_check_status(){
3521 qint32 status = _recv_Int();
3522 if (status == 0) {
3523 // everything is OK
3524 //status = status
3525 } else if (status > 0 && status < 10) {
3526 QString strproblems("Unknown error");
3527 if (status == 1) {
3528 strproblems = "Invalid item provided: The item identifier provided is not valid or it does not exist.";
3529 } else if (status == 2) { //output warning only
3530 strproblems = _recv_Line();
3531 qDebug() << "RoboDK API WARNING: " << strproblems;
3532 return 0;
3533 } else if (status == 3){ // output error
3534 strproblems = _recv_Line();
3535 qDebug() << "RoboDK API ERROR: " << strproblems;
3536 } else if (status == 9) {
3537 qDebug() << "Invalid RoboDK License";
3538 }
3539 //print(strproblems);
3540 if (_USE_EXCPETIONS == true) {
3541 throw new std::exception(strproblems.toStdString().c_str(),status);
3542 }
3543 } else if (status < 100){
3544 QString strproblems = _recv_Line();
3545 qDebug() << "RoboDK API ERROR: " << strproblems;
3546 if (_USE_EXCPETIONS == true) {
3547 QString errorMessage = QString("RoboDK API ERROR: ") + strproblems;
3548 throw new std::exception(errorMessage.toStdString().c_str(),status);
3549 }
3550 } else {
3551 if (_USE_EXCPETIONS == true) {
3552 throw new std::exception("Communication problems with the RoboDK API",status);
3553 }
3554 qDebug() << "Communication problems with the RoboDK API";
3555 }
3556 return status;
3557}
3558
3559
3560
3561void RoboDK::_disconnect(){
3562 if (!_COM || !_OWN_SOCKET) {
3563 return;
3564 }
3565
3566 _COM->deleteLater();
3567 _COM = nullptr;
3568}
3569
3570// attempt a simple connection to RoboDK and start RoboDK if it is not running
3571bool RoboDK::_connect_smart(){
3572 //Establishes a connection with robodk. robodk must be running, otherwise, it will attempt to start it
3573 if (_connect()){
3574 qDebug() << "The RoboDK API is connected";
3575 return true;
3576 }
3577
3578 // Don't try to start RoboDK in case we have custom socket
3579 if (!_OWN_SOCKET) {
3580 return false;
3581 }
3582
3583 qDebug() << "...Trying to start RoboDK: " << _ROBODK_BIN << " " << _ARGUMENTS;
3584 // Start RoboDK
3585 QProcess *p = new QProcess();
3586 //_ARGUMENTS = "/DEBUG";
3587 p->start(_ROBODK_BIN, _ARGUMENTS.split(" ", Qt::SkipEmptyParts));
3588 p->setReadChannel(QProcess::StandardOutput);
3589 //p->waitForReadyRead(10000);
3590 _PROCESS = p->processId();
3591 while (p->canReadLine() || p->waitForReadyRead(5000)){
3592 QString line = QString::fromUtf8(p->readLine().trimmed());
3593 //qDebug() << "RoboDK process: " << line;
3594 if (line.contains("Running", Qt::CaseInsensitive)){
3595 qDebug() << "RoboDK is Running... Connecting API";
3596 bool is_connected = _connect();
3597 if (is_connected){
3598 qDebug() << "The RoboDK API is connected";
3599 } else {
3600 qDebug() << "The RoboDK API is NOT connected!";
3601 }
3602 return is_connected;
3603 }
3604 }
3605 qDebug() << "Could not start RoboDK!";
3606 return false;
3607}
3608
3609// attempt a simple connection to RoboDK
3610bool RoboDK::_connect(){
3611 // Do nothing with custom socket, just return its state
3612 if (!_OWN_SOCKET) {
3613 return _connected();
3614 }
3615
3616 _disconnect();
3617 _COM = new QTcpSocket();
3618 if (_IP.isEmpty()){
3619 _COM->connectToHost("127.0.0.1", _PORT); //QHostAddress::LocalHost, _PORT);
3620 } else {
3621 _COM->connectToHost(_IP, _PORT);
3622 }
3623 // usually, 5 msec should be enough for localhost
3624 if (!_COM->waitForConnected(_TIMEOUT)){
3625 _COM->deleteLater();
3626 _COM = nullptr;
3627 return false;
3628 }
3629
3630 // RoboDK protocol to check that we are connected to the right port
3631 _COM->write(ROBODK_API_START_STRING); _COM->write(ROBODK_API_LF, 1);
3632 _COM->write("1 0"); _COM->write(ROBODK_API_LF, 1);
3633
3634 // 5 msec should be enough for localhost
3635 /*if (!_COM->waitForBytesWritten(_TIMEOUT)){
3636 _COM->deleteLater();
3637 _COM = nullptr;
3638 return false;
3639 }*/
3640 // 10 msec should be enough for localhost
3641 if (!_COM->canReadLine() && !_COM->waitForReadyRead(_TIMEOUT)){
3642 _COM->deleteLater();
3643 _COM = nullptr;
3644 return false;
3645 }
3646 QString read(_COM->readAll());
3647 // make sure we receive the OK from RoboDK
3648 if (!read.startsWith(ROBODK_API_READY_STRING)){
3649 _COM->deleteLater();
3650 _COM = nullptr;
3651 return false;
3652 }
3653 return true;
3654}
3655
3656
3658bool RoboDK::_waitline(QAbstractSocket* com){
3659 if (com == nullptr) {
3660 com = _COM;
3661 }
3662 if (com == nullptr){ return false; }
3663 while (!com->canReadLine()){
3664 if (!com->waitForReadyRead(_TIMEOUT)){
3665 return false;
3666 }
3667 }
3668 return true;
3669}
3670QString RoboDK::_recv_Line(QAbstractSocket* com){//QString &string){
3671 if (com == nullptr) {
3672 com = _COM;
3673 }
3674 QString string;
3675 if (!_waitline(com)){
3676 if (com != nullptr){
3677 //if this happens it means that there are problems: delete buffer
3678 com->readAll();
3679 }
3680 return string;
3681 }
3682 QByteArray line = _COM->readLine().trimmed();//remove last character \n //.trimmed();
3683 string.append(QString::fromUtf8(line));
3684 return string;
3685}
3686bool RoboDK::_send_Line(const QString& string, QAbstractSocket* com){
3687 if (com == nullptr) {
3688 com = _COM;
3689 }
3690 if (com == nullptr || !com->isOpen()){ return false; }
3691 com->write(string.toUtf8());
3692 com->write(ROBODK_API_LF, 1);
3693 return true;
3694}
3695
3696int RoboDK::_recv_Int(QAbstractSocket* com){//qint32 &value){
3697 if (com == nullptr){
3698 com = _COM;
3699 }
3700 qint32 value; // do not change type
3701 if (com == nullptr){ return false; }
3702 if (com->bytesAvailable() < sizeof(qint32)){
3703 com->waitForReadyRead(_TIMEOUT);
3704 if (com->bytesAvailable() < sizeof(qint32)){
3705 return -1;
3706 }
3707 }
3708 QDataStream ds(com);
3709 ds >> value;
3710 return value;
3711}
3712bool RoboDK::_send_Int(qint32 value, QAbstractSocket* com){
3713 if (com == nullptr) {
3714 com = _COM;
3715 }
3716 if (com == nullptr || !com->isOpen()){ return false; }
3717 QDataStream ds(com);
3718 ds << value;
3719 return true;
3720}
3721
3722Item RoboDK::_recv_Item(QAbstractSocket* com){//Item *item){
3723 if (com == nullptr) {
3724 com = _COM;
3725 }
3726 Item item(this);
3727 if (com == nullptr){ return item; }
3728 item._PTR = 0;
3729 item._TYPE = -1;
3730 if (com->bytesAvailable() < sizeof(quint64)){
3731 com->waitForReadyRead(_TIMEOUT);
3732 if (com->bytesAvailable() < sizeof(quint64)){
3733 return item;
3734 }
3735 }
3736 QDataStream ds(com);
3737 ds >> item._PTR;
3738 ds >> item._TYPE;
3739 return item;
3740}
3741bool RoboDK::_send_Item(const Item *item){
3742 if (_COM == nullptr || !_COM->isOpen()){ return false; }
3743 QDataStream ds(_COM);
3744 quint64 ptr = 0;
3745 if (item != nullptr){
3746 ptr = item->_PTR;
3747 }
3748 ds << ptr;
3749 return true;
3750}
3751bool RoboDK::_send_Item(const Item &item){
3752 return _send_Item(&item);
3753}
3754
3755Mat RoboDK::_recv_Pose(QAbstractSocket* com){//Mat &pose){
3756 if (com == nullptr) {
3757 com = _COM;
3758 }
3759
3760 Mat pose;
3761 if (com == nullptr){ return pose; }
3762 int size = 16*sizeof(double);
3763 if (com->bytesAvailable() < size){
3764 com->waitForReadyRead(_TIMEOUT);
3765 if (com->bytesAvailable() < size){
3766 return pose;
3767 }
3768 }
3769 QDataStream ds(com);
3770 ds.setFloatingPointPrecision(QDataStream::DoublePrecision);
3771 //ds.setByteOrder(QDataStream::LittleEndian);
3772 double valuei;
3773 for (int j=0; j<4; j++){
3774 for (int i=0; i<4; i++){
3775 ds >> valuei;
3776 pose.Set(i,j,valuei);
3777 //pose.data()[i][j] = valuei;
3778 }
3779 }
3780 return pose;
3781}
3782bool RoboDK::_send_Pose(const Mat &pose){
3783 if (_COM == nullptr || !_COM->isOpen()){ return false; }
3784 QDataStream ds(_COM);
3785 ds.setFloatingPointPrecision(QDataStream::DoublePrecision);
3786 //ds.setByteOrder(QDataStream::LittleEndian);
3787 double valuei;
3788 for (int j=0; j<4; j++){
3789 for (int i=0; i<4; i++){
3790 valuei = pose.Get(i,j);
3791 ds << valuei;
3792 }
3793 }
3794 return true;
3795}
3796bool RoboDK::_recv_XYZ(tXYZ pos){
3797 if (_COM == nullptr){ return false; }
3798 int size = 3*sizeof(double);
3799 if (_COM->bytesAvailable() < size){
3800 _COM->waitForReadyRead(_TIMEOUT);
3801 if (_COM->bytesAvailable() < size){
3802 return false;
3803 }
3804 }
3805 QDataStream ds(_COM);
3806 ds.setFloatingPointPrecision(QDataStream::DoublePrecision);
3807 //ds.setByteOrder(QDataStream::LittleEndian);
3808 double valuei;
3809 for (int i=0; i<3; i++){
3810 ds >> valuei;
3811 pos[i] = valuei;
3812 }
3813 return true;
3814}
3815bool RoboDK::_send_XYZ(const tXYZ pos){
3816 if (_COM == nullptr || !_COM->isOpen()){ return false; }
3817 QDataStream ds(_COM);
3818 ds.setFloatingPointPrecision(QDataStream::DoublePrecision);
3819 //ds.setByteOrder(QDataStream::LittleEndian);
3820 double valuei;
3821 for (int i=0; i<3; i++){
3822 valuei = pos[i];
3823 ds << valuei;
3824 }
3825 return true;
3826}
3827bool RoboDK::_recv_Array(tJoints *jnts){
3828 return _recv_Array(jnts->_Values, &(jnts->_nDOFs));
3829}
3830bool RoboDK::_send_Array(const tJoints *jnts){
3831 if (jnts == nullptr){
3832 return _send_Int(0);
3833 }
3834 return _send_Array(jnts->_Values, jnts->_nDOFs);
3835}
3836bool RoboDK::_send_Array(const Mat *mat){
3837 if (mat == nullptr){
3838 return _send_Int(0);
3839 }
3840 double m44[16];
3841 for (int c=0; c<4; c++){
3842 for (int r=0; r<4; r++){
3843 m44[c*4+r] = mat->Get(r,c);
3844 }
3845 }
3846 return _send_Array(m44, 16);
3847}
3848bool RoboDK::_recv_Array(double *values, int *psize, QAbstractSocket* com){
3849 if (com == nullptr) {
3850 com = _COM;
3851 }
3852 int nvalues = _recv_Int();
3853 if (com == nullptr || nvalues < 0) {return false;}
3854 if (psize != nullptr){
3855 *psize = nvalues;
3856 }
3857 if (nvalues < 0 || nvalues > 50){return false;} //check if the value is not too big
3858 int size = nvalues*sizeof(double);
3859 if (com->bytesAvailable() < size){
3860 com->waitForReadyRead(_TIMEOUT);
3861 if (com->bytesAvailable() < size){
3862 return false;
3863 }
3864 }
3865 QDataStream ds(com);
3866 ds.setFloatingPointPrecision(QDataStream::DoublePrecision);
3867 //ds.setByteOrder(QDataStream::LittleEndian);
3868 double valuei;
3869 for (int i=0; i<nvalues; i++){
3870 ds >> valuei;
3871 values[i] = valuei;
3872 }
3873 return true;
3874}
3875
3876bool RoboDK::_send_Array(const double *values, int nvalues){
3877 if (_COM == nullptr || !_COM->isOpen()){ return false; }
3878 if (!_send_Int((qint32)nvalues)){ return false; }
3879 QDataStream ds(_COM);
3880 ds.setFloatingPointPrecision(QDataStream::DoublePrecision);
3881 //ds.setByteOrder(QDataStream::LittleEndian);
3882 double valuei;
3883 for (int i=0; i<nvalues; i++){
3884 valuei = values[i];
3885 ds << valuei;
3886 }
3887 return true;
3888}
3889
3890bool RoboDK::_recv_Matrix2D(tMatrix2D **mat){ // needs to delete after!
3891 qint32 dim1 = _recv_Int();
3892 qint32 dim2 = _recv_Int();
3893 *mat = Matrix2D_Create();
3894 //emxInit_real_T(mat, 2);
3895 if (dim1 < 0 || dim2 < 0){ return false; }
3896 Matrix2D_Set_Size(*mat, dim1, dim2);
3897 if (dim1*dim2 <= 0){
3898 return true;
3899 }
3900 QByteArray buffer;
3901 int count = 0;
3902 double value;
3903 while (true){
3904 int remaining = dim1*dim2 - count;
3905 if (remaining <= 0){ return true; }
3906 if (_COM->bytesAvailable() <= 0 && !_COM->waitForReadyRead(_TIMEOUT)){
3907 Matrix2D_Delete(mat);
3908 return false;
3909 }
3910 buffer.append(_COM->read(remaining * sizeof(double) - buffer.size()));
3911 int np = buffer.size() / sizeof(double);
3912 QDataStream indata(buffer);
3913 indata.setFloatingPointPrecision(QDataStream::DoublePrecision);
3914 for (int i=0; i<np; i++){
3915 indata >> value;
3916 (*mat)->data[count] = value;
3917 count = count + 1;
3918 }
3919 buffer = buffer.mid(np * sizeof(double));
3920 }
3921 return false;// we should never arrive here...
3922}
3923bool RoboDK::_send_Matrix2D(tMatrix2D *mat){
3924 if (_COM == nullptr || !_COM->isOpen()){ return false; }
3925 QDataStream ds(_COM);
3926 ds.setFloatingPointPrecision(QDataStream::DoublePrecision);
3927 //ds.setByteOrder(QDataStream::LittleEndian);
3928 qint32 dim1 = Matrix2D_Size(mat, 1);
3929 qint32 dim2 = Matrix2D_Size(mat, 2);
3930 bool ok1 = _send_Int(dim1);
3931 bool ok2 = _send_Int(dim2);
3932 if (!ok1 || !ok2) {return false; }
3933 double valuei;
3934 for (int j=0; j<dim2; j++){
3935 for (int i=0; i<dim1; i++){
3936 valuei = Matrix2D_Get_ij(mat, i, j);
3937 ds << valuei;
3938 }
3939 }
3940 return true;
3941}
3942
3943bool RoboDK::_send_Bytes(const QByteArray &data, QAbstractSocket *com) {
3944 if (!com) {
3945 com = _COM;
3946 }
3947 if (!com || !com->isOpen()) {
3948 return false;
3949 }
3950
3951 {
3952 QDataStream ds(com);
3953 ds << data.size();
3954 }
3955 com->write(data);
3956 return true;
3957}
3958
3959QByteArray RoboDK::_recv_Bytes(QAbstractSocket* com) {
3960 if (!com) {
3961 com = _COM;
3962 }
3963 if (!com || !com->isOpen()) {
3964 return QByteArray();
3965 }
3966
3967 int size = _recv_Int(com);
3968
3969 while (com->bytesAvailable() < size) {
3970 if (!com->waitForReadyRead(_TIMEOUT)) {
3971 return QByteArray();
3972 }
3973 }
3974
3975 return com->read(size);
3976}
3977
3978// private move type, to be used by public methods (MoveJ and MoveL)
3979void RoboDK::_moveX(const Item *target, const tJoints *joints, const Mat *mat_target, const Item *itemrobot, int movetype, bool blocking){
3980 itemrobot->WaitMove();
3981 _send_Line("MoveX");
3982 _send_Int(movetype);
3983 if (target != nullptr){
3984 _send_Int(3);
3985 _send_Array((tJoints*)nullptr);
3986 _send_Item(target);
3987 } else if (joints != nullptr){
3988 _send_Int(1);
3989 _send_Array(joints);
3990 _send_Item(nullptr);
3991 } else if (mat_target != nullptr){// && mat_target.IsHomogeneous()) {
3992 _send_Int(2);
3993 _send_Array(mat_target); // keep it as array!
3994 _send_Item(nullptr);
3995 } else {
3996 if (_USE_EXCPETIONS == true) {
3997 throw new std::exception("Invalid target type");
3998 }
3999 throw 0;
4000 }
4001 _send_Item(itemrobot);
4002 _check_status();
4003 if (blocking){
4004 itemrobot->WaitMove();
4005 }
4006}
4007// private move type, to be used by public methods (MoveJ and MoveL)
4008void RoboDK::_moveC(const Item *target1, const tJoints *joints1, const Mat *mat_target1, const Item *target2, const tJoints *joints2, const Mat *mat_target2, const Item *itemrobot, bool blocking){
4009 itemrobot->WaitMove();
4010 _send_Line("MoveC");
4011 _send_Int(3);
4012 if (target1 != nullptr){
4013 _send_Int(3);
4014 _send_Array((tJoints*)nullptr);
4015 _send_Item(target1);
4016 } else if (joints1 != nullptr) {
4017 _send_Int(1);
4018 _send_Array(joints1);
4019 _send_Item(nullptr);
4020 } else if (mat_target1 != nullptr){// && mat_target1.IsHomogeneous()) {
4021 _send_Int(2);
4022 _send_Array(mat_target1);
4023 _send_Item(nullptr);
4024 } else {
4025 if (_USE_EXCPETIONS == true) {
4026 throw new std::exception("Invalid type of target 1");
4027 }
4028 }
4030 if (target2 != nullptr) {
4031 _send_Int(3);
4032 _send_Array((tJoints*)nullptr);
4033 _send_Item(target2);
4034 } else if (joints2 != nullptr) {
4035 _send_Int(1);
4036 _send_Array(joints2);
4037 _send_Item(nullptr);
4038 } else if (mat_target2 != nullptr){// && mat_target2.IsHomogeneous()) {
4039 _send_Int(2);
4040 _send_Array(mat_target2);
4041 _send_Item(nullptr);
4042 } else {
4043 if (_USE_EXCPETIONS == true) {
4044 throw new std::exception("Invalid type of target 2");
4045 }
4046 }
4048 _send_Item(itemrobot);
4049 _check_status();
4050 if (blocking){
4051 itemrobot->WaitMove();
4052 }
4053}
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067//---------------------------------------------------------------------------------------------------
4068//---------------------------------------------------------------------------------------------------
4069//---------------------------------------------------------------------------------------------------
4070//---------------------------------------------------------------------------------------------------
4071//---------------------------------------------------------------------------------------------------
4072//---------------------------------------------------------------------------------------------------
4074// 2D matrix functions
4076void emxInit_real_T(tMatrix2D **pEmxArray, int numDimensions)
4077{
4078 tMatrix2D *emxArray;
4079 int i;
4080 *pEmxArray = (tMatrix2D *)malloc(sizeof(tMatrix2D));
4081 emxArray = *pEmxArray;
4082 emxArray->data = (double *)NULL;
4083 emxArray->numDimensions = numDimensions;
4084 emxArray->size = (int *)malloc((unsigned int)(sizeof(int) * numDimensions));
4085 emxArray->allocatedSize = 0;
4086 emxArray->canFreeData = true;
4087 for (i = 0; i < numDimensions; i++) {
4088 emxArray->size[i] = 0;
4089 }
4090}
4093 tMatrix2D *matrix;
4094 emxInit_real_T((tMatrix2D**)(&matrix), 2);
4095 return matrix;
4096}
4097
4098
4099void emxFree_real_T(tMatrix2D **pEmxArray){
4100 if (*pEmxArray != (tMatrix2D *)NULL) {
4101 if (((*pEmxArray)->data != (double *)NULL) && (*pEmxArray)->canFreeData) {
4102 free((void *)(*pEmxArray)->data);
4103 }
4104 free((void *)(*pEmxArray)->size);
4105 free((void *)*pEmxArray);
4106 *pEmxArray = (tMatrix2D *)NULL;
4107 }
4108}
4109
4111 emxFree_real_T((tMatrix2D**)(mat));
4112}
4113
4114
4115
4116void emxEnsureCapacity(tMatrix2D *emxArray, int oldNumel, unsigned int elementSize){
4117 int newNumel;
4118 int i;
4119 double *newData;
4120 if (oldNumel < 0) {
4121 oldNumel = 0;
4122 }
4123 newNumel = 1;
4124 for (i = 0; i < emxArray->numDimensions; i++) {
4125 newNumel *= emxArray->size[i];
4126 }
4127 if (newNumel > emxArray->allocatedSize) {
4128 i = emxArray->allocatedSize;
4129 if (i < 16) {
4130 i = 16;
4131 }
4132 while (i < newNumel) {
4133 if (i > 1073741823) {
4134 i =(2147483647);//MAX_int32_T;
4135 } else {
4136 i <<= 1;
4137 }
4138 }
4139 newData = (double*) calloc((unsigned int)i, elementSize);
4140 if (emxArray->data != NULL) {
4141 memcpy(newData, emxArray->data, elementSize * oldNumel);
4142 if (emxArray->canFreeData) {
4143 free(emxArray->data);
4144 }
4145 }
4146 emxArray->data = newData;
4147 emxArray->allocatedSize = i;
4148 emxArray->canFreeData = true;
4149 }
4150}
4151
4152void Matrix2D_Set_Size(tMatrix2D *mat, int rows, int cols) {
4153 int old_numel;
4154 int numbel;
4155 old_numel = mat->size[0] * mat->size[1];
4156 mat->size[0] = rows;
4157 mat->size[1] = cols;
4158 numbel = rows*cols;
4159 emxEnsureCapacity(mat, old_numel, sizeof(double));
4160 /*for (i=0; i<numbel; i++){
4161 mat->data[i] = 0.0;
4162 }*/
4163}
4164
4165int Matrix2D_Size(const tMatrix2D *var, int dim) { // ONE BASED!!
4166 if (var->numDimensions >= dim) {
4167 return var->size[dim - 1];
4168 }
4169 else {
4170 return 0;
4171 }
4172}
4174 return Matrix2D_Size(var, 2);
4175}
4177 return Matrix2D_Size(var, 1);
4178}
4179double Matrix2D_Get_ij(const tMatrix2D *var, int i, int j) { // ZERO BASED!!
4180 return var->data[var->size[0] * j + i];
4181}
4182void Matrix2D_SET_ij(const tMatrix2D *var, int i, int j, double value) { // ZERO BASED!!
4183 var->data[var->size[0] * j + i] = value;
4184}
4185
4186double *Matrix2D_Get_col(const tMatrix2D *var, int col) { // ZERO BASED!!
4187 return (var->data + var->size[0] * col);
4188}
4189
4190
4191void Matrix2D_Add(tMatrix2D *var, const double *array, int numel){
4192 int oldnumel;
4193 int size1 = var->size[0];
4194 int size2 = var->size[1];
4195 oldnumel = size1*size2;
4196 var->size[1] = size2 + 1;
4197 emxEnsureCapacity(var, oldnumel, (int)sizeof(double));
4198 numel = qMin(numel, size1);
4199 for (int i=0; i<numel; i++){
4200 var->data[size1*size2 + i] = array[i];
4201 }
4202}
4203
4204void Matrix2D_Add(tMatrix2D *var, const tMatrix2D *varadd){
4205 int oldnumel;
4206 int size1 = var->size[0];
4207 int size2 = var->size[1];
4208 int size1_ap = varadd->size[0];
4209 int size2_ap = varadd->size[1];
4210 int numel = size1_ap*size2_ap;
4211 if (size1 != size1_ap){
4212 return;
4213 }
4214 oldnumel = size1*size2;
4215 var->size[1] = size2 + size2_ap;
4216 emxEnsureCapacity(var, oldnumel, (int)sizeof(double));
4217 for (int i=0; i<numel; i++){
4218 var->data[size1*size2 + i] = varadd->data[i];
4219 }
4220}
4221
4222void Debug_Array(const double *array, int arraysize) {
4223 int i;
4224 for (i = 0; i < arraysize; i++) {
4225 //char chararray[500]; // You had better have room for what you are sprintf()ing!
4226 //sprintf(chararray, "%.3f", array[i]);
4227 //std::cout << chararray;
4228 printf("%.3f", array[i]);
4229 if (i < arraysize - 1) {
4230 //std::cout << " , ";
4231 printf(" , ");
4232 }
4233 }
4234}
4235
4236void Debug_Matrix2D(const tMatrix2D *emx) {
4237 int size1;
4238 int size2;
4239 int j;
4240 double *column;
4241 size1 = Matrix2D_Get_nrows(emx);
4242 size2 = Matrix2D_Get_ncols(emx);
4243 printf("Matrix size = [%i, %i]\n", size1, size2);
4244 //std::out << "Matrix size = [%i, %i]" << size1 << " " << size2 << "]\n";
4245 for (j = 0; j<size2; j++) {
4246 column = Matrix2D_Get_col(emx, j);
4247 Debug_Array(column, size1);
4248 printf("\n");
4249 //std::cout << "\n";
4250 }
4251}
4252/*
4253void Debug_Mat(Mat pose, char show_full_pose) {
4254 tMatrix4x4 pose_tr;
4255 double xyzwpr[6];
4256 int j;
4257 if (show_full_pose > 0) {
4258 POSE_TR(pose_tr, pose);
4259 printf("Pose size = [4x4]\n");
4260 //std::cout << "Pose size = [4x4]\n";
4261 for (j = 0; j < 4; j++) {
4262 Debug_Array(pose_tr + j * 4, 4);
4263 printf("\n");
4264 //std::cout << "\n";
4265 }
4266 }
4267 else {
4268 POSE_2_XYZWPR(xyzwpr, pose);
4269 //std::cout << "XYZWPR = [ ";
4270 printf("XYZWPR = [ ");
4271 Debug_Array(xyzwpr, 6);
4272 printf(" ]\n");
4273 //std::cout << " ]\n";
4274 }
4275}
4276*/
4277
4278#ifndef RDK_SKIP_NAMESPACE
4279}
4280#endif
The Item class represents an item in RoboDK station. An item can be a robot, a frame,...
Item ObjectLink(int link_id=0)
Returns an item pointer to the geometry of a robot link. This is useful to show/hide certain robot li...
int RunInstruction(const QString &code, int run_type=RoboDK::INSTRUCTION_CALL_PROGRAM)
Adds a program call, code, message or comment inside a program.
RoboDK * RDK()
Returns the RoboDK link Robolink().
Mat GeometryPose()
Returns the position (pose) the object geometry with respect to its own reference frame....
Item getLink(int type_linked=RoboDK::ITEM_TYPE_ROBOT)
Returns an item linked to a robot, object, tool, program or robot machining project....
Item setMachiningParameters(QString ncfile="", Item part_obj=nullptr, QString options="")
Update the robot milling path input and parameters. Parameter input can be an NC file (G-code or APT ...
void Stop()
Stops a program or a robot.
void MoveC(const Item &itemtarget1, const Item &itemtarget2, bool blocking=true)
Moves a robot to a specific target ("Move Circular" mode). By default, this function blocks until the...
tMatrix2D * SolveIK_All_Mat2D(const Mat &pose, const Mat *tool=nullptr, const Mat *ref=nullptr)
Computes the inverse kinematics for the specified robot and pose. The function returns all available ...
void setRunType(int program_run_type)
Sets if the program will be run in simulation mode or on the real robot. Use: "PROGRAM_RUN_ON_SIMULAT...
void setSpeed(double speed_linear, double accel_linear=-1, double speed_joints=-1, double accel_joints=-1)
Sets the speed and/or the acceleration of a robot.
int InstructionList(tMatrix2D *instructions)
Returns the list of program instructions as an MxN matrix, where N is the number of instructions and ...
void NewLink()
Create a new communication link for RoboDK. Use this for robots if you use a multithread application ...
Mat SolveFK(const tJoints &joints, const Mat *tool=nullptr, const Mat *ref=nullptr)
Computes the forward kinematics of the robot for the provided joints. The tool and the reference fram...
bool Connect(const QString &robot_ip="")
Connect to a real robot using the corresponding robot driver.
void setPoseTool(const Mat tool_pose)
Sets the tool of a robot or a tool object (Tool Center Point, or TCP). The tool pose can be either an...
void setJoints(const tJoints &jnts)
Set robot joints or the joints of a target.
bool Valid(bool check_pointer=false) const
Check if an item is valid (not null and available in the open station)
void Instruction(int ins_id, QString &name, int &instype, int &movetype, bool &isjointtarget, Mat &target, tJoints &joints)
Returns the program instruction at position id.
qint32 _TYPE
Item type.
void setAccuracyActive(int accurate=1)
Sets the accuracy of the robot active or inactive. A robot must have been calibrated to properly use ...
Mat Pose() const
Returns the local position (pose) of an object, target or reference frame. For example,...
void setColor(double colorRGBA[4])
Changes the color of a robot/object/tool. A color must must in the format COLOR=[R,...
bool SelectedFeature(int &featureType, int &featureId)
Retrieve the currently selected feature for this object.
int Type() const
Item type (object, robot, tool, reference, robot machining project, ...)
void customInstruction(const QString &name, const QString &path_run, const QString &path_icon="", bool blocking=true, const QString &cmd_run_on_robot="")
Add a custom instruction. This instruction will execute a Python file or an executable file.
void setDO(const QString &io_var, const QString &io_value)
Sets a variable (output) to a given value. This can also be used to set any variables to a desired va...
void setPose(const Mat pose)
Sets the local position (pose) of an object, target or reference frame. For example,...
void setGeometryPose(const Mat pose)
Sets the position (pose) the object geometry with respect to its own reference frame....
bool Busy()
Checks if a robot or program is currently running (busy or moving)
void setAsJointTarget()
Sets a target as a joint target. A joint target moves to a joints position without regarding the cart...
int RunCode(const QString &parameters)
Runs a program. It returns the number of instructions that can be executed successfully (a quick prog...
Mat PoseTool()
Returns the tool pose of an item. If a robot is provided it will get the tool pose of the active tool...
bool Disconnect()
Disconnect from a real robot (when the robot driver is used)
void setRobot(const Item &robot)
Sets the robot of a program or a target. You must set the robot linked to a program or a target every...
void setPoseAbs(const Mat pose)
Sets the global position (pose) of an item. For example, the position of an object/frame/target with ...
void setJointsHome(const tJoints &jnts)
Set robot joints for the home position.
void setParent(Item parent)
Attaches the item to a new parent while maintaining the relative position with its parent....
void setPoseFrame(const Mat frame_pose)
Sets the reference frame of a robot(user frame). The frame can be either an item or a pose....
void setRounding(double zonedata)
Sets the robot movement smoothing accuracy (also known as zone data value).
void JointsConfig(const tJoints &joints, tConfig config)
Returns the robot configuration state for a set of robot joints.
void setParentStatic(Item parent)
Attaches the item to another parent while maintaining the current absolute position in the station....
int InstructionListJoints(QString &error_msg, tMatrix2D **joint_list, double mm_step=10.0, double deg_step=5.0, const QString &save_to_file="", bool collision_check=false, int flags=0, double time_step_s=0.1)
Returns a list of joints an MxN matrix, where M is the number of robot axes plus 4 columns....
void ShowTargets(bool visible=true)
Show or hide targets of a program in the RoboDK tree.
tJoints Joints() const
Returns the current joints of a robot or the joints of a target. If the item is a cartesian target,...
void WaitMove(double timeout_sec=300) const
Waits (blocks) until the robot finishes its movement.
Mat PoseFrame()
Returns the reference frame pose of an item. If a robot is provided it will get the tool pose of the ...
Mat PoseAbs()
Returns the global position (pose) of an item. For example, the position of an object/frame/target wi...
QByteArray getParam(const QString &param)
Get a specific binary item parameter.
RoboDK * _RDK
Pointer to RoboDK link object.
QList< tJoints > SolveIK_All(const Mat &pose, const Mat *tool=nullptr, const Mat *ref=nullptr)
Computes the inverse kinematics for the specified robot and pose. The function returns all available ...
bool isJointTarget() const
Returns True if a target is a joint target (green icon). Otherwise, the target is a Cartesian target ...
quint64 _PTR
Pointer to the item inside RoboDK.
void MoveJ(const Item &itemtarget, bool blocking=true)
Moves a robot to a specific target ("Move Joint" mode). By default, this function blocks until the ro...
QList< Item > Childs() const
Returns a list of the item childs that are attached to the provided item.
int InstructionCount()
Returns the number of instructions of a program.
void setName(const QString &name)
Set the name of a RoboDK item.
double Update(int collision_check=RoboDK::COLLISION_OFF, int timeout_sec=3600, double *out_nins_time_dist=nullptr, double mm_step=-1, double deg_step=-1)
Updates a program and returns the estimated time and the number of valid instructions....
void Save(const QString &filename)
Save a station, a robot, a tool or an object to a file.
void Pause(double time_ms=-1)
Generates a pause instruction for a robot or a program when generating code. Set it to -1 (default) i...
tJoints JointsHome() const
Returns the home joints of a robot. These joints can be manually set in the robot "Parameters" menu,...
Item AttachClosest()
Attach the closest object to the tool. Returns the item that was attached.
void DetachAll(Item parent)
Detach any object attached to a tool.
void ShowInstructions(bool visible=true)
Show or hide instruction items of a program in the RoboDK tree.
void setAO(const QString &io_var, const QString &io_value)
Set an analog Output.
void JointLimits(tJoints *lower_limits, tJoints *upper_limits)
Retrieve the joint limits of a robot.
bool Visible() const
Returns 1 if the item is visible, otherwise, returns 0.
bool MakeProgram(const QString &filename)
Saves a program to a file.
void Delete()
Deletes an item and its childs from the station.
void waitDI(const QString &io_var, const QString &io_value, double timeout_ms=-1)
Waits for an input io_id to attain a given value io_value. Optionally, a timeout can be provided.
QString getDI(const QString &io_var)
Get a Digital Input (DI). This function is only useful when connected to a real robot using the robot...
void setVisible(bool visible, int visible_frame=-1)
Sets the item visiblity status.
void MoveL(const Item &itemtarget, bool blocking=true)
Moves a robot to a specific target ("Move Linear" mode). By default, this function blocks until the r...
QString setParam(const QString &param, const QString &value)
Set a specific item parameter. Select Tools-Run Script-Show Commands to see all available commands fo...
int RunProgram()
Runs a program. It returns the number of instructions that can be executed successfully (a quick prog...
GetPointsResult GetPoints(int featureType=RoboDK::FEATURE_SURFACE, int featureId=0)
Retrieves the point under the mouse cursor, a curve or the 3D points of an object....
void setAsCartesianTarget()
Sets a target as a cartesian target. A cartesian target moves to cartesian coordinates.
void ShowSequence(tMatrix2D *sequence)
Displays a sequence of joints.
int MoveL_Test(const tJoints &joints1, const Mat &pose2, double minstep_mm=-1)
Checks if a linear movement is free of issues and, optionally, collisions.
QString Name() const
Returns the name of an item. The name of the item is always displayed in the RoboDK station tree.
bool Finish()
Disconnect from the RoboDK API. This flushes any pending program generation.
Item AddTool(const Mat &tool_pose, const QString &tool_name="New TCP")
Adds an empty tool to the robot provided the tool pose (4x4 Matrix) and the tool name.
tJoints SolveIK(const Mat &pose, const Mat *tool=nullptr, const Mat *ref=nullptr)
Computes the inverse kinematics for the specified robot and pose. The joints returned are the closest...
quint64 GetID()
Get the item pointer.
void Scale(double scale)
Apply a scale to an object to make it bigger or smaller. The scale can be uniform (if scale is a floa...
int MoveJ_Test(const tJoints &j1, const tJoints &j2, double minstep_deg=-1)
Checks if a joint movement is possible and, optionally, free of collisions.
void setInstruction(int ins_id, const QString &name, int instype, int movetype, bool isjointtarget, const Mat &target, const tJoints &joints)
Sets the program instruction at position id.
QString getAI(const QString &io_var)
Get an Analog Input (AI). This function is only useful when connected to a real robot using the robot...
Item DetachClosest(Item parent)
Detach the closest object attached to the tool (see also setParentStatic).
void setJointLimits(const tJoints &lower_limits, const tJoints &upper_limits)
Set the joint limits of a robot.
Item Parent() const
Return the parent item of this item.
The Mat class represents a 4x4 pose matrix. The main purpose of this object is to represent a pose in...
Definition robodk_api.h:505
Mat()
Create the identity matrix.
void VX(tXYZ xyz) const
Get the X vector (N vector)
void setVX(double x, double y, double z)
Set the X vector values (N vector)
void FromXYZRPW(tXYZWPR xyzwpr)
Calculates the pose from the position and euler angles ([x,y,z,r,p,w] vector) The result is the same ...
void VY(tXYZ xyz) const
Get the Y vector (O vector)
static Mat transl(double x, double y, double z)
Return a translation matrix.
void ToXYZRPW(tXYZWPR xyzwpr) const
Calculates the equivalent position and euler angles ([x,y,z,r,p,w] vector) of the given pose Note: tr...
void setVZ(double x, double y, double z)
Set the Z vector values (A vector)
double Get(int r, int c) const
Get a matrix value.
bool Valid() const
Check if the matrix is valid.
void setVY(double x, double y, double z)
Set the Y vector values (O vector)
static Mat roty(double ry)
Return a Y-axis rotation matrix.
const double * Values() const
Get a pointer to the 16-digit array (doubles or floats if ROBODK_API_FLOATS is defined).
void Pos(tXYZ xyz) const
Get the position (T position), in mm.
bool MakeHomogeneous()
Forces 4x4 matrix to be homogeneous (vx,vy,vz must be unitary vectors and respect: vx x vy = vz)....
static Mat rotz(double rz)
Return a Z-axis rotation matrix.
const double * ValuesD() const
Get a pointer to the 16-digit double array.
double _ddata16[16]
Copy of the data as a double array.
Definition robodk_api.h:752
QString ToString(const QString &separator=", ", int precision=3, bool xyzwpr_only=false) const
Retrieve a string representation of the pose.
bool FromString(const QString &str)
Set the matrix given a XYZRPW string array (6-values)
Mat inv() const
Invert the pose (homogeneous matrix assumed)
void setPos(double x, double y, double z)
Set the position (T position) in mm.
void VZ(tXYZ xyz) const
Get the Z vector (A vector)
bool isHomogeneous() const
Returns true if the matrix is homogeneous, otherwise it returns false.
bool _valid
Flags if a matrix is not valid.
Definition robodk_api.h:747
const float * ValuesF() const
Get a pointer to the 16-digit array as an array of floats.
void Set(int r, int c, double value)
Set a matrix value.
static Mat rotx(double rx)
Return the X-axis rotation matrix.
static Mat XYZRPW_2_Mat(double x, double y, double z, double r, double p, double w)
Calculates the pose from the position and euler angles ([x,y,z,r,p,w] vector) The result is the same ...
This class is the iterface to the RoboDK API. With the RoboDK API you can automate certain tasks and ...
Definition robodk_api.h:761
void setViewPose(const Mat &pose)
Set the pose of the wold reference frame with respect to the user view (camera/screen).
void setSimulationSpeed(double speed)
Sets the current simulation speed. Set the speed to 1 for a real-time simulation. The slowest speed a...
@ ITEM_TYPE_PROGRAM
Program item.
Item getItem(QString name, int itemtype=-1)
Returns an item by its name. If there is no exact match it will return the last closest match.
Item Paste(const Item *paste_to=nullptr)
Paste the copied item as a dependency of another item (same as Ctrl+V). Paste should be used after Co...
QList< Item > getCollisionItems(QList< int > link_id_list)
Return the list of items that are in a collision state. This function can be used after calling Colli...
void CloseRoboDK()
Closes RoboDK window and finishes RoboDK execution.
QString Command(const QString &cmd, const QString &value="")
Send a special command. These commands are meant to have a specific effect in RoboDK,...
void ShowMessage(const QString &message, bool popup=true)
Show a message in RoboDK (it can be blocking or non blocking in the status bar)
Item AddPoints(tMatrix2D *points, Item *referenceObject=nullptr, bool addToRef=false, int ProjectionType=PROJECTION_ALONG_NORMAL_RECALC)
Adds a list of points to an object. The provided points must be a list of vertices....
int RunCode(const QString &code, bool code_is_fcn_call=false)
Adds code to run in the program output. If the program exists it will also run the program in simulat...
bool LaserTrackerMeasure(tXYZ xyz, tXYZ estimate, bool search=false)
Takes a laser tracker measurement with respect to its own reference frame. If an estimate point is pr...
int Collision(Item item1, Item item2)
Returns 1 if item1 and item2 collided. Otherwise returns 0.
void Save(const QString &filename, const Item *itemsave=nullptr)
Save an item to a file. If no item is provided, the open station is saved.
bool IsInside(Item object_inside, Item object_parent)
Check if an object is fully inside another one.
QString License()
Returns the license as a readable string (same name shown in the RoboDK's title bar,...
void setRunMode(int run_mode=1)
Sets the behavior of the RoboDK API. By default, RoboDK shows the path simulation for movement instru...
void PluginLoad(const QString &pluginName, int load=1)
Load or unload the specified plugin (path to DLL, dylib or SO file). If the plugin is already loaded ...
QString Version()
Return the vesion of RoboDK as a 4 digit string: Major.Minor.Revision.Build.
Item AddFile(const QString &filename, const Item *parent=nullptr)
Loads a file and attaches it to parent. It can be any file supported by RoboDK.
Item AddStation(const QString &name)
Add a new empty station. It returns the station item added.
void ShowAsCollided(QList< Item > itemList, QList< bool > collidedList, QList< int > *robot_link_id=nullptr)
Show a list of items as collided.
bool setCollisionActivePair(int check_state, Item item1, Item item2, int id1=0, int id2=0)
Set collision checking ON or OFF (COLLISION_ON/COLLISION_OFF) for a specific pair of objects....
bool FileGet(const QString &path_file_local, Item *station=nullptr, const QString path_file_remote="")
Retrieve a file from the RoboDK running instance.
int RunMode()
Returns the behavior of the RoboDK API. By default, RoboDK shows the path simulation for movement ins...
Item getActiveStation()
Returns the active station item (station currently visible).
QList< QPair< QString, QString > > getParams()
Gets all the user parameters from the open RoboDK station. The parameters can also be modified by rig...
void ShowRoboDK()
Shows or raises the RoboDK window.
void CalibrateTool(tMatrix2D *poses_joints, tXYZ tcp_xyz, int format=EULER_RX_RY_RZ, int algorithm=CALIBRATE_TCP_BY_POINT, Item *robot=nullptr, double *error_stats=nullptr)
Calibrate a tool (TCP) given a number of points or calibration joints. Important: If the robot is cal...
void Disconnect()
Disconnect from the RoboDK API. This flushes any pending program generation.
Item Popup_ISO9283_CubeProgram(Item *robot=nullptr, tXYZ center=nullptr, double side=-1, bool blocking=true)
Show the popup menu to create the ISO9283 path for position accuracy, repeatability and path accuracy...
int setCollisionActive(int check_state=COLLISION_ON)
Turn collision checking ON or OFF (COLLISION_OFF/COLLISION_OFF) according to the collision map....
Item getCursorXYZ(int x=-1, int y=-1, tXYZ xyzStation=nullptr)
Returns the position of the cursor as XYZ coordinates (by default), or the 3D position of a given set...
Mat ViewPose()
Get the pose of the wold reference frame with respect to the user view (camera/screen).
Item ItemUserPick(const QString &message="Pick one item", int itemtype=-1)
Shows a RoboDK popup to select one object from the open RoboDK station. An item type can be specified...
Item AddTarget(const QString &name, Item *itemparent=nullptr, Item *itemrobot=nullptr)
Adds a new target that can be reached with a robot.
void setSelection(QList< Item > list_items)
Sets the selection in the tree (it can be one or more items).
QList< Item > Selection()
Returns the list of items selected (it can be one or more items).
Item AddMachiningProject(const QString &name="Curve follow settings", Item *itemrobot=nullptr)
Add a new robot machining project. Machining projects can also be used for 3D printing,...
Item AddCurve(tMatrix2D *curvePoints, Item *referenceObject=nullptr, bool addToRef=false, int ProjectionType=PROJECTION_ALONG_NORMAL_RECALC)
Adds a curve provided point coordinates. The provided points must be a list of vertices....
Item AddProgram(const QString &name, Item *itemrobot=nullptr)
Adds a new Frame that can be referenced by a robot.
int Cam2D_Snapshot(const QString &file_save_img, const Item &cam_item, const QString &params="")
Take a snapshot of a simulated camera and save it as an image.
QList< Item > getOpenStation()
Returns the list of open stations in RoboDK.
int RunProgram(const QString &function_w_params)
Adds a function call in the program output. RoboDK will handle the syntax when the code is generated ...
int ProgramStart(const QString &progname, const QString &defaultfolder="", const QString &postprocessor="", Item *robot=nullptr)
Defines the name of the program when the program is generated. It is also possible to specify the nam...
QList< Item > getItemList(int filter=-1)
Returns a list of items (list of names or pointers) of all available items in the currently open stat...
int Cam2D_SetParams(const QString &cam_params, const Item &cam_item)
Set the camera parameters.
Mat CalibrateReference(tMatrix2D *poses_joints, int method=CALIBRATE_FRAME_3P_P1_ON_X, bool use_joints=false, Item *robot=nullptr)
Calibrate a Reference Frame given a list of points or joint values. Important: If the robot is calibr...
void RunMessage(const QString &message, bool message_is_comment=false)
Shows a message or a comment in the output robot program.
@ FEATURE_HOVER_OBJECT_MESH
Object mesh (under the mouse cursor) feature type flag.
@ FEATURE_NONE
No selection.
Item AddFrame(const QString &name, Item *itemparent=nullptr)
Adds a new Frame that can be referenced by a robot.
int getFlagsItem(Item item)
Retrieve current item flags. Item flags allow defining how much access the user has to item-specific ...
void setActiveStation(Item stn)
Set the active station (project currently visible).
Item AddShape(tMatrix2D *trianglePoints, Item *addTo=nullptr, bool shapeOverride=false, Color *color=nullptr)
Adds a shape provided triangle coordinates. Triangles must be provided as a list of vertices....
Item Cam2D_Add(const Item &item_object, const QString &cam_params, const Item *cam_item=nullptr)
Add a simulated 2D or depth camera as an item. Use Delete to delete it.
QStringList getItemListNames(int filter=-1)
Returns a list of items (list of names or Items) of all available items in the currently open station...
void setWindowState(int windowstate=WINDOWSTATE_NORMAL)
Set the state of the RoboDK window.
QString PluginCommand(const QString &pluginName, const QString &pluginCommand, const QString &pluginValue="")
Send a specific command to a RoboDK plugin. The command and value (optional) must be handled by your ...
void HideRoboDK()
Hides the RoboDK window. RoboDK will continue running in the background.
@ INS_TYPE_MOVE
Linear or joint movement instruction.
int Collisions()
Returns the number of pairs of objects that are currently in a collision state.
void Copy(const Item &tocopy)
Makes a copy of an item (same as Ctrl+C), which can be pasted (Ctrl+V) using Paste().
void CloseStation()
Close the current station without asking to save.
GetPointsResult GetPoints(int featureType=FEATURE_HOVER_OBJECT_MESH)
Retrieves the object under the mouse cursor.
void setParam(const QString &param, const QString &value)
Sets a global parameter from the RoboDK station. If the parameters exists, it will be modified....
void Update()
Update the screen. This updates the position of all robots and internal links according to previously...
QString getParam(const QString &param)
Gets a global or a user parameter from the open RoboDK station. The parameters can also be modified b...
void ProjectPoints(tMatrix2D *points, tMatrix2D **projected, Item objectProject, int ProjectionType=PROJECTION_ALONG_NORMAL_RECALC)
Projects a point given its coordinates. The provided points must be a list of [XYZ] coordinates....
void setFlagsRoboDK(int flags=FLAG_ROBODK_ALL)
Update the RoboDK flags. RoboDK flags allow defining how much access the user has to certain RoboDK f...
void Finish()
Disconnect from the RoboDK API. This flushes any pending program generation.
void setFlagsItem(Item item, int flags=FLAG_ITEM_ALL)
Update item flags. Item flags allow defining how much access the user has to item-specific features....
void Render(bool always_render=false)
Update the scene.
double SimulationSpeed()
Gets the current simulation speed. Set the speed to 1 for a real-time simulation.
bool FileSet(const QString &file_local, const QString &file_remote="", bool load_file=true, Item *attach_to=nullptr)
Send file from the current location to the RoboDK instance.
The tJoints class represents a joint position of a robot (robot axes).
Definition robodk_api.h:385
int GetValues(double *joints)
GetValues.
tJoints(int ndofs=0)
tJoints
QString ToString(const QString &separator=", ", int precision=3) const
Retrieve a string representation of the joint values.
bool Valid() const
Check if the joints are valid. For example, when we request the Inverse kinematics and there is no so...
int Length() const
Number of joint axes of the robot (or degrees of freedom)
const double * Values() const
Joint values.
int _nDOFs
number of degrees of freedom
Definition robodk_api.h:486
const double * ValuesD() const
Joint values.
double _Values[RDK_SIZE_JOINTS_MAX]
joint values (doubles, used to store the joint values)
Definition robodk_api.h:489
bool FromString(const QString &str)
Set the joint values given a comma-separated string. Tabs and spaces are also allowed.
void setLength(int new_length)
Set the length of the array (only shrinking the array is allowed)
float _ValuesF[RDK_SIZE_JOINTS_MAX]
joint values (floats, used to return a copy as a float pointer)
Definition robodk_api.h:492
const float * ValuesF() const
Joint values.
void SetValues(const double *joints, int ndofs=-1)
Set the joint values in deg or mm. You can also important provide the number of degrees of freedom (6...
All RoboDK API functions are wrapped in the RoboDK_API namespace. If you prefer to forget about the R...
double * Matrix2D_Get_col(const tMatrix2D *var, int col)
Returns the pointer of a column of a tMatrix2D. A column has Matrix2D_Get_nrows values that can be ac...
tMatrix2D * Matrix2D_Create()
Creates a new 2D matrix tMatrix2D.. Use Matrix2D_Delete to delete the matrix (to free the memory)....
Mat roty(double ry)
Translation matrix class: Mat::roty.
int Matrix2D_Get_ncols(const tMatrix2D *var)
Returns the number of columns of a tMatrix2D.
double Matrix2D_Get_ij(const tMatrix2D *var, int i, int j)
Returns the value at location [i,j] of a tMatrix2D.
Mat rotz(double rz)
Translation matrix class: Mat::rotz.
double tConfig[RDK_SIZE_MAX_CONFIG]
The robot configuration defines a specific state of the robot without crossing any singularities....
Definition robodk_api.h:311
Mat transl(double x, double y, double z)
Translation matrix class: Mat::transl.
void Matrix2D_Set_Size(tMatrix2D *mat, int rows, int cols)
Sets the size of a tMatrix2D.
int Matrix2D_Size(const tMatrix2D *var, int dim)
Sets the size of a tMatrix2D.
void Debug_Matrix2D(const tMatrix2D *emx)
Display the content of a tMatrix2D through STDOUT. This is only intended for debug purposes.
Mat rotx(double rx)
Translation matrix class: Mat::rotx.
double tXYZ[3]
tXYZ (mm) represents a position or a vector in mm
Definition robodk_api.h:300
int Matrix2D_Get_nrows(const tMatrix2D *var)
Returns the number of rows of a tMatrix2D.
void Debug_Array(const double *array, int arraysize)
Show an array through STDOUT Given an array of doubles, it generates a string.
void Matrix2D_Delete(tMatrix2D **mat)
Deletes a tMatrix2D.
double tXYZWPR[6]
Six doubles that represent robot joints (usually in degrees)
Definition robodk_api.h:297
The Color struct represents an RGBA color (each color component should be in the range [0-1])
Definition robodk_api.h:337
float r
Red color.
Definition robodk_api.h:339
float a
Alpha value (0 = transparent; 1 = opaque)
Definition robodk_api.h:348
float b
Blue color.
Definition robodk_api.h:345
float g
Green color.
Definition robodk_api.h:342
The GetPointsResult method represents the results of executing the GetPoints function.
The tMatrix2D struct represents a variable size 2d Matrix. Use the Matrix2D_... functions to oeprate ...
Definition robodk_api.h:362
double * data
Pointer to the data.
Definition robodk_api.h:364
int * size
Pointer to the size array.
Definition robodk_api.h:367
int numDimensions
Number of dimensions (usually 2)
Definition robodk_api.h:373