kni_wrapper.cpp
Go to the documentation of this file.
1 /**************************************************************************
2  * kni_wrapper.cpp -
3  * Implements a wrapper for the kni library so that the C++ library
4  * can be accessed by C based environments (MatLab, LabView)
5  * Copyright (C) Neuronics AG
6  * Philipp Keller, Tino Perucchi, 2008
7 **************************************************************************/
8 #include <iostream>
9 #include <memory>
10 #include <math.h>
11 #include <vector>
12 #include <string.h>
13 #include <map>
14 
15 #define EXPORT_FCNS
17 
18 //#include <pthread.h>
19 #include <sstream>
21 //KNI internal types
22 static std::unique_ptr<CLMBase> katana;
23 static std::unique_ptr<CCdlSocket> device;
24 static std::unique_ptr<CCplSerialCRC> protocol;
26 //A map containing movement vectors accessible by a string name:
27 std::map< std::string, std::vector<TMovement> > movements;
28 //A vector for storing encoders
29 std::vector<int> encoders;
30 //The number of motors, initialized in initKatana()
32 //variables to store communication data
33 byte packet[32]; //comm packet
34 byte buffer[256]; //comm readbuf
35 byte size = 0; //comm readbuf size
37 
38 
39 DLLEXPORT int initKatana(char* configFile, char* ipaddress){
40  try {
41  int port = 5566;
42  device.reset(new CCdlSocket(ipaddress, port));
43  protocol.reset(new CCplSerialCRC());
44  protocol->init(device.get());
45  katana.reset(new CLMBase());
46  katana->create(configFile, protocol.get());
47  numberOfMotors = katana->getNumberOfMotors();
48  for(int i = 0; i < numberOfMotors; i++){
49  encoders.push_back(0);
50  }
51  //MessageBox(NULL, "Katana successfully initiated!",TEXT(""),MB_OK);
52  }
53  catch(Exception &e){
54  std::cout << "ERROR: " << e.message() << std::endl;
55  #ifdef WIN32
56  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
57  #endif
58  return ERR_FAILED;
59  }
60  return ERR_SUCCESS;
61 }
63 DLLEXPORT int calibrate(int axis){
64  try{
65  katana->calibrate();
66  }
67  catch(Exception &e){
68  std::cout << "ERROR: " << e.message() << std::endl;
69  #ifdef WIN32
70  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
71  #endif
72  return ERR_FAILED;
73  }
74  return ERR_SUCCESS;
75 }
77 DLLEXPORT int moveMot(int axis, int enc, int speed, int accel){
78  try{
79  //set speed
80  katana->setMotorVelocityLimit(axis-1, speed);
81  //set acceleration
82  katana->setMotorAccelerationLimit(axis-1, accel);
83  //move
84  katana->moveMotorToEnc(axis-1, enc);
85  }
86  catch(...){
87  return ERR_FAILED;
88  }
89  return ERR_SUCCESS;
90 }
92 DLLEXPORT int waitForMot(int axis, int targetpos, int tolerance){
93  try{
94  katana->waitForMotor( (short)axis-1, targetpos, tolerance, 0, TM_ENDLESS);
95  }
96  catch(Exception &e){
97  std::cout << "ERROR: " << e.message() << std::endl;
98  #ifdef WIN32
99  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
100  #endif
101  return ERR_FAILED;
102  }
103  return ERR_SUCCESS;
104 }
106 DLLEXPORT int moveMotAndWait(int axis, int targetpos, int tolerance){
107  try{
108  katana->moveMotorToEnc(axis-1, targetpos);
109  waitForMot(axis, targetpos, tolerance);
110  }
111  catch(Exception &e){
112  std::cout << "ERROR: " << e.message() << std::endl;
113  #ifdef WIN32
114  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
115  #endif
116  return ERR_FAILED;
117  }
118  return ERR_SUCCESS;
119 }
121 DLLEXPORT int motorOn(int axis){
122  try{
123  katana->switchMotorOn(axis-1);
124  }
125  catch(Exception &e){
126  std::cout << "ERROR: " << e.message() << std::endl;
127  #ifdef WIN32
128  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
129  #endif
130  return ERR_FAILED;
131  }
132  return ERR_SUCCESS;
133 }
135 DLLEXPORT int motorOff(int axis){
136  try{
137  katana->switchMotorOff(axis-1);
138  }
139  catch(Exception &e){
140  std::cout << "ERROR: " << e.message() << std::endl;
141  #ifdef WIN32
142  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
143  #endif
144  return ERR_FAILED;
145  }
146  return ERR_SUCCESS;
147 }
150  try{
151  katana->switchRobotOff();
152  }
153  catch(Exception &e){
154  std::cout << "ERROR: " << e.message() << std::endl;
155  #ifdef WIN32
156  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
157  #endif
158  return ERR_FAILED;
159  }
160  return ERR_SUCCESS;
161 }
164  try{
165  katana->switchRobotOn();
166  }
167  catch(Exception &e){
168  std::cout << "ERROR: " << e.message() << std::endl;
169  #ifdef WIN32
170  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
171  #endif
172  return ERR_FAILED;
173  }
174  return ERR_SUCCESS;
175 }
177 DLLEXPORT int setGripper(bool hasGripper){
178  try{
179  // cannot extract configured values from KNI atm.
180  int openEncoders = 30770;
181  int closeEncoders = 12240;
182 
183  katana->setGripperParameters(hasGripper, openEncoders, closeEncoders);
184  }
185  catch(Exception &e){
186  std::cout << "ERROR: " << e.message() << std::endl;
187  #ifdef WIN32
188  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
189  #endif
190  return ERR_FAILED;
191  }
192  return ERR_SUCCESS;
193 }
196  try{
197  katana->closeGripper();
198  }
199  catch(Exception &e){
200  std::cout << "ERROR: " << e.message() << std::endl;
201  #ifdef WIN32
202  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
203  #endif
204  return ERR_FAILED;
205  }
206  return ERR_SUCCESS;
207 }
210  try{
211  katana->openGripper();
212  }
213  catch(Exception &e){
214  std::cout << "ERROR: " << e.message() << std::endl;
215  #ifdef WIN32
216  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
217  #endif
218  return ERR_FAILED;
219  }
220  return ERR_SUCCESS;
221 }
223 DLLEXPORT int moveToPosEnc(int enc1, int enc2, int enc3, int enc4, int enc5, int enc6, int velocity, int acceleration, int tolerance, bool _wait){
224  std::cout << "moving to: " << enc1 << ", " << enc2 << ", " << enc3 << ", " << enc4 << ", " << enc5 << ", " << enc6 << "\n";
225  try{
226  for(int i = 0; i < numberOfMotors; i++){
227  //set speed
228  katana->setMotorVelocityLimit(i, velocity);
229  //set acceleration
230  katana->setMotorAccelerationLimit(i, acceleration);
231  }
232  //move
233  std::vector<int> enc;
234  enc.push_back(enc1);
235  enc.push_back(enc2);
236  enc.push_back(enc3);
237  enc.push_back(enc4);
238  enc.push_back(enc5);
239  enc.push_back(enc6);
240  katana->moveRobotToEnc(enc.begin(), enc.end(), _wait, tolerance);
241  }
242  catch(Exception &e){
243  std::cout << "ERROR: " << e.message() << std::endl;
244  #ifdef WIN32
245  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
246  #endif
247  return ERR_FAILED;
248  }
249  return ERR_SUCCESS;
250 }
252 DLLEXPORT int sendSplineToMotor(int axis, int targetpos, int duration, int p0, int p1, int p2, int p3){
253  try{
254  katana->sendSplineToMotor((unsigned short) (axis-1), (short) targetpos, (short) duration, p0, p1, p2, p3);
255  }
256  catch(Exception &e){
257  std::cout << "ERROR: " << e.message() << std::endl;
258  #ifdef WIN32
259  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
260  #endif
261  return ERR_FAILED;
262  }
263  return ERR_SUCCESS;
264 }
266 DLLEXPORT int startSplineMovement(int contd, int exactflag){
267  try{
268  bool exact = (exactflag != 0);
269  katana->startSplineMovement(exact, contd);
270  }
271  catch(Exception &e){
272  std::cout << "ERROR: " << e.message() << std::endl;
273  #ifdef WIN32
274  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
275  #endif
276  return ERR_FAILED;
277  }
278  return ERR_SUCCESS;
279 }
282  try{
283  katana->flushMoveBuffers();
284  }
285  catch(Exception &e){
286  std::cout << "ERROR: " << e.message() << std::endl;
287  #ifdef WIN32
288  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
289  #endif
290  return ERR_FAILED;
291  }
292  return ERR_SUCCESS;
293 }
295 DLLEXPORT int getPosition(struct TPos *pos){
296  try{
297  katana->getCoordinates(pos->X, pos->Y, pos->Z, pos->phi, pos->theta, pos->psi);
298  }
299  catch(Exception &e){
300  std::cout << "ERROR: " << e.message() << std::endl;
301  #ifdef WIN32
302  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
303  #endif
304  return ERR_FAILED;
305  }
306  return ERR_SUCCESS;
307 }
309 DLLEXPORT int moveToPos(struct TPos *pos, int velocity, int acceleration){
310  try{
311  setMaxAccel(0, acceleration);
312  setMaxVelocity(0, velocity);
313  katana->moveRobotTo(pos->X, pos->Y, pos->Z, pos->phi, pos->theta, pos->psi);
314  }
315  catch(Exception &e){
316  std::cout << "ERROR: " << e.message() << std::endl;
317  #ifdef WIN32
318  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
319  #endif
320  return ERR_FAILED;
321  }
322  return ERR_SUCCESS;
323 }
325 DLLEXPORT int moveToPosLin(struct TPos *targetPos, int velocity, int acceleration){
326  try{
327  setMaxAccel(0, acceleration);
328  setMaxVelocity(0, velocity);
329  katana->moveRobotLinearTo(targetPos->X, targetPos->Y, targetPos->Z, targetPos->phi, targetPos->theta, targetPos->psi);
330  }
331  catch(Exception &e){
332  std::cout << "ERROR: " << e.message() << std::endl;
333  #ifdef WIN32
334  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
335  #endif
336  return ERR_FAILED;
337  }
338  return ERR_SUCCESS;
339 }
341 DLLEXPORT int pushMovementToStack(struct TMovement *movement, char* name){
342  try{
343  // name as string
344  std::string name_str(name);
345  // get according movement vector, a new entry is created and inserted
346  // automatically if it does not exist and store movement in it
347  movements[name_str].push_back(*movement);
348  }
349  catch(Exception &e){
350  std::cout << "ERROR: " << e.message() << std::endl;
351  #ifdef WIN32
352  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
353  #endif
354  return ERR_FAILED;
355  }
356  return ERR_SUCCESS;
357 }
359 DLLEXPORT int deleteMovementFromStack(char* name, int index){
360  try{
361  int status = ERR_SUCCESS;
362  // name as string
363  std::string name_str(name);
364  // find movement vector
365  std::map< std::string, std::vector<TMovement> >::iterator it;
366  it = movements.find(name_str);
367  if (it == movements.end()) {
368  // movement vector does not exist
369  status = ERR_FAILED;
370  } else {
371  // movement vector exists
372  std::vector<TMovement> movement_vector = (*it).second;
373  if ((int)movement_vector.size() >= index) {
374  // index out of range
375  status = ERR_FAILED;
376  } else {
377  // erase movement
378  movement_vector.erase(movement_vector.begin()+index);
379  status = ERR_SUCCESS;
380  }
381  }
382  if (status == ERR_FAILED) {
383  return ERR_FAILED;
384  }
385  }
386  catch(Exception &e){
387  std::cout << "ERROR: " << e.message() << std::endl;
388  #ifdef WIN32
389  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
390  #endif
391  return ERR_FAILED;
392  }
393  return ERR_SUCCESS;
394 }
397  try{
398  int status = ERR_SUCCESS;
399  // name as string
400  std::string name_str(name);
401  // find movement vector
402  std::map< std::string, std::vector<TMovement> >::iterator it;
403  it = movements.find(name_str);
404  if (it == movements.end()) {
405  // movement vector does not exist
406  status = ERR_FAILED;
407  } else {
408  // movement vector exists, erase it
409  movements.erase(it);
410  status = ERR_SUCCESS;
411  }
412  if (status == ERR_FAILED) {
413  return ERR_FAILED;
414  }
415  }
416  catch(Exception &e){
417  std::cout << "ERROR: " << e.message() << std::endl;
418  #ifdef WIN32
419  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
420  #endif
421  return ERR_FAILED;
422  }
423  return ERR_SUCCESS;
424 }
426 DLLEXPORT int runThroughMovementStack(char* name, int loops){
427  try{
428  int status = ERR_SUCCESS;
429  // name as string
430  std::string name_str(name);
431  // find movement vector
432  std::map< std::string, std::vector<TMovement> >::iterator it;
433  it = movements.find(name_str);
434  if (it == movements.end()) {
435  // movement vector does not exist
436  status = ERR_FAILED;
437  } else {
438  // movement vector exists
439  std::vector<TMovement> movement_vector = (*it).second;
440  // execute all movements in movement vector
441  int size = (int)movement_vector.size();
442  if (size == 1) {
443  status = executeMovement(&(movement_vector.at(0)));
444  } else {
445  bool first, last;
446  struct TPos *lastpos;
447  for (int j = 0; j < loops; ++j) {
448  last = false;
449  for (int i = 0; i < size; ++i) {
450  if (i == 0) {
451  first = true;
452  lastpos = NULL;
453  } else {
454  first = false;
455  lastpos = &((movement_vector.at(i-1)).pos);
456  }
457  if (i == (size - 1))
458  last = true;
459  status = executeConnectedMovement(&(movement_vector.at(i)),
460  lastpos, first, last);
461  if (status == ERR_FAILED) {
462  break;
463  }
464  }
465  }
466  }
467  }
468  if (status == ERR_FAILED) {
469  return ERR_FAILED;
470  }
471  }
472  catch(Exception &e){
473  std::cout << "ERROR: " << e.message() << std::endl;
474  #ifdef WIN32
475  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
476  #endif
477  return ERR_FAILED;
478  }
479  return ERR_SUCCESS;
480 }
482 DLLEXPORT int executeMovement(struct TMovement *movement){
483  int status = ERR_SUCCESS;
484  if (movement->transition == PTP) {
485  //move PTP
486  status = moveToPos(&(movement->pos), movement->velocity, movement->acceleration);
487  } else if (movement->transition == LINEAR) {
488  //move linear
489  status = moveToPosLin(&(movement->pos), movement->velocity, movement->acceleration);
490  } else {
491  status = ERR_FAILED;
492  }
493  return status;
494 }
496 DLLEXPORT int executeConnectedMovement(struct TMovement *movement, struct TPos *startPos,
497  bool first, bool last){
498  int status = ERR_SUCCESS;
499  setMaxAccel(0, movement->acceleration);
500  setMaxVelocity(0, movement->velocity);
501  bool wait = last; // only wait at the last of the connected movements
502  if (movement->transition == PTP) {
503  //move PTP
504  try{
505  if (first) {
506  // first of the connected movements, no startpos known
507  katana->moveRobotTo(movement->pos.X, movement->pos.Y, movement->pos.Z,
508  movement->pos.phi, movement->pos.theta, movement->pos.psi,
509  wait);
510  } else {
511  // startpos known
512  katana->movP2P(startPos->X, startPos->Y, startPos->Z, startPos->phi,
513  startPos->theta, startPos->psi, movement->pos.X, movement->pos.Y,
514  movement->pos.Z, movement->pos.phi, movement->pos.theta,
515  movement->pos.psi, true, movement->velocity, wait);
516  }
517  }
518  catch(...){
519  status = ERR_FAILED;
520  }
521  } else if (movement->transition == LINEAR) {
522  //move linear
523  try{
524  if (first) {
525  // first of the connected movements, no startpos known
526  katana->moveRobotLinearTo(movement->pos.X, movement->pos.Y,
527  movement->pos.Z, movement->pos.phi, movement->pos.theta,
528  movement->pos.psi, wait);
529  } else {
530  // startpos known
531  katana->movLM2P(startPos->X, startPos->Y, startPos->Z, startPos->phi,
532  startPos->theta, startPos->psi, movement->pos.X, movement->pos.Y,
533  movement->pos.Z, movement->pos.phi, movement->pos.theta,
534  movement->pos.psi, true, movement->velocity, wait);
535  }
536  }
537  catch(...){
538  status = ERR_FAILED;
539  }
540  } else {
541  status = ERR_FAILED;
542  }
543  return status;
544 }
546 DLLEXPORT int ModBusTCP_writeWord(int address, int value){
547  try{
548  byte packet[32];
549  byte size;
550  packet[0] = 'M';
551  packet[1] = 'W';
552  packet[2] = (byte)address;
553  packet[3] = (byte)(value >> 8);
554  packet[4] = (byte)value;
555  protocol->comm(packet, buffer, &size);
556  if (!buffer[0] || ((short) size != 4)){
557  return ERR_FAILED;
558  }
559  }
560  catch(Exception &e){
561  std::cout << "ERROR: " << e.message() << std::endl;
562  #ifdef WIN32
563  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
564  #endif
565  return ERR_FAILED;
566  }
567  return ERR_SUCCESS;
568 }
570 int ModBusTCP_readWord(int address, int &value){
571  try{
572  byte packet[32];
573  byte size;
574  packet[0] = 'M';
575  packet[1] = 'R';
576  packet[2] = (byte)address;
577  protocol->comm(packet, buffer, &size);
578  if (!buffer[0] || ((short) size != 4)){
579  return ERR_FAILED;
580  }
581  int val = buffer[2];
582  val <<= 8;
583  val += buffer[1];
584  value = val;
585  }
586  catch(Exception &e){
587  std::cout << "ERROR: " << e.message() << std::endl;
588  #ifdef WIN32
589  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
590  #endif
591  return ERR_FAILED;
592  }
593  return ERR_SUCCESS;
594 }
596 DLLEXPORT int IO_setOutput(char output, int value){
597  try{
598  byte packet[32];
599  byte size;
600  packet[0] = 'T';
601  packet[1] = 'w';
602  packet[2] = (byte)value << output;
603  packet[3] = 0;
604  packet[4] = 0;
605  protocol->comm(packet, buffer, &size);
606  if (!buffer[0] || ((short) size != 2)) {
607  std::cout << " command failed!" << std::endl;
608  return ERR_FAILED;
609  }
610  }
611  catch(Exception &e){
612  std::cout << "ERROR: " << e.message() << std::endl;
613  #ifdef WIN32
614  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
615  #endif
616  return ERR_FAILED;
617  }
618  return ERR_SUCCESS;
619 }
621 int IO_readInput(int inputNr, int &value){
622  try{
623  byte packet[32];
624  byte size;
625  packet[0] = 'T';
626  packet[1] = 'r';
627  packet[2] = 0;
628  packet[3] = 0;
629  packet[4] = 0;
630  protocol->comm(packet, buffer, &size);
631  if (!buffer[0] || ((short) size != 2)) {
632  std::cout << " command failed!" << std::endl;
633  return ERR_FAILED;
634  }
635  }
636  catch(Exception &e){
637  std::cout << "ERROR: " << e.message() << std::endl;
638  #ifdef WIN32
639  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
640  #endif
641  return ERR_FAILED;
642  }
643  byte mask = static_cast<byte>(pow(static_cast<double>(2), inputNr-1));
644  if(buffer[1] != 0xFF){
645  if((mask & (int)buffer[1]) > 0){
646  value = 0;
647  } else {
648  value = 1;
649  }
650  return ERR_SUCCESS;
651  }
652  else return ERR_FAILED;
653 }
656  try{
657  for(int i = 1; i <= getNumberOfMotors(); i++){
658  TMotTPS tps = { MCF_CLEAR_MOVEBUFFER, 0 };
659  katana->GetBase()->GetMOT()->arr[i].sendTPS(&tps);
660  }
661  }
662  catch(Exception &e){
663  std::cout << "ERROR: " << e.message() << std::endl;
664  #ifdef WIN32
665  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
666  #endif
667  return ERR_FAILED;
668  }
669  return ERR_SUCCESS;
670 }
672 int getVelocity(int axis, int &value){
673  try{
674  katana->GetBase()->GetMOT()->arr[axis-1].recvPVP();
675  short vel = katana->GetBase()->GetMOT()->arr[axis-1].GetPVP()->vel;
676  value = (int) vel;
677  }
678  catch(Exception &e){
679  std::cout << "ERROR: " << e.message() << std::endl;
680  #ifdef WIN32
681  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
682  #endif
683  return ERR_FAILED;
684  }
685  return ERR_SUCCESS;
686 }
688 int getDrive(int axis, int &value){
689  try{
690  katana->GetBase()->GetMOT()->arr[axis-1].recvPVP();
691  byte pwm = katana->GetBase()->GetMOT()->arr[axis-1].GetPVP()->pwm;
692  value = (int) pwm;
693  }
694  catch(Exception &e){
695  std::cout << "ERROR: " << e.message() << std::endl;
696  #ifdef WIN32
697  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
698  #endif
699  return ERR_FAILED;
700  }
701  return ERR_SUCCESS;
702 }
704 int getEncoder(int axis, int &value){
705  try{
706  katana->getRobotEncoders(encoders.begin(), encoders.end());
707  value = encoders.at(axis-1);
708  }
709  catch(Exception &e){
710  std::cout << "ERROR: " << e.message() << std::endl;
711  #ifdef WIN32
712  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
713  #endif
714  return ERR_FAILED;
715  }
716  return ERR_SUCCESS;
717 }
719 int getAxisFirmwareVersion(int axis, char value[]){
720  try{
721  katana->GetBase()->GetMOT()->arr[axis-1].recvSFW();
722  int v = (int) katana->GetBase()->GetMOT()->arr[axis-1].GetSFW()->version;
723  int sv = (int) katana->GetBase()->GetMOT()->arr[axis-1].GetSFW()->subversion;
724  int r = (int) katana->GetBase()->GetMOT()->arr[axis-1].GetSFW()->revision;
725  std::stringstream ss;
726  ss << v << "." << sv << "." << r;
727  const char* cstr = ss.str().c_str();
728  int index = -1;
729  do {
730  index++;
731  value[index] = cstr[index];
732  } while (cstr[index] != '\0');
733  }
734  catch(Exception &e){
735  std::cout << "ERROR: " << e.message() << std::endl;
736  #ifdef WIN32
737  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
738  #endif
739  return ERR_FAILED;
740  }
741  return ERR_SUCCESS;
742 }
744 int getVersion(char value[]){
745  try{
746  katana->GetBase()->recvMFW();
747  int v = (int) katana->GetBase()->GetMFW()->ver;
748  int r = (int) katana->GetBase()->GetMFW()->rev;
749  std::stringstream ss;
750  ss << v << "." << r;
751  const char* cstr = ss.str().c_str();
752  int index = -1;
753  do {
754  index++;
755  value[index] = cstr[index];
756  } while (cstr[index] != '\0');
757  }
758  catch(Exception &e){
759  std::cout << "ERROR: " << e.message() << std::endl;
760  #ifdef WIN32
761  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
762  #endif
763  return ERR_FAILED;
764  }
765  return ERR_SUCCESS;
766 }
768 DLLEXPORT int setCollisionDetection(int axis, bool state){
769  try{
770  if(state == true){
771  katana->enableCrashLimits();
772  }
773  else katana->disableCrashLimits();
774  }
775  catch(Exception &e){
776  std::cout << "ERROR: " << e.message() << std::endl;
777  #ifdef WIN32
778  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
779  #endif
780  return ERR_FAILED;
781  }
782  return ERR_SUCCESS;
783 }
785 DLLEXPORT int setPositionCollisionLimit(int axis, int limit){
786  try{
787  katana->setPositionCollisionLimit(axis-1, limit);
788  }
789  catch(Exception &e){
790  std::cout << "ERROR: " << e.message() << std::endl;
791  #ifdef WIN32
792  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
793  #endif
794  return ERR_FAILED;
795  }
796  return ERR_SUCCESS;
797 }
799 DLLEXPORT int setVelocityCollisionLimit(int axis, int limit){
800  try{
801  katana->setSpeedCollisionLimit(axis-1, limit);
802  }
803  catch(Exception &e){
804  std::cout << "ERROR: " << e.message() << std::endl;
805  #ifdef WIN32
806  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
807  #endif
808  return ERR_FAILED;
809  }
810  return ERR_SUCCESS;
811 }
813 DLLEXPORT int setCollisionParameters(int axis, int position, int velocity){
814  try{
815  setPositionCollisionLimit(axis, position);
816  setVelocityCollisionLimit(axis, velocity);
817  }
818  catch(Exception &e){
819  std::cout << "ERROR: " << e.message() << std::endl;
820  #ifdef WIN32
821  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
822  #endif
823  return ERR_FAILED;
824  }
825  return ERR_SUCCESS;
826 }
828 DLLEXPORT int setForceLimit(int axis, int limit){
829  try{
830  katana->setForceLimit(axis, limit);
831  }
832  catch(Exception &e){
833  std::cout << "ERROR: " << e.message() << std::endl;
834  #ifdef WIN32
835  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
836  #endif
837  return ERR_FAILED;
838  }
839  return ERR_SUCCESS;
840 }
842 DLLEXPORT int getForce(int axis){
843  try{
844  return katana->getForce(axis);
845  }
846  catch(Exception &e){
847  std::cout << "ERROR: " << e.message() << std::endl;
848  #ifdef WIN32
849  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
850  #endif
851  return ERR_FAILED;
852  }
853 }
856  try{
857  return katana->getCurrentControllerType(axis);
858  }
859  catch(Exception &e){
860  std::cout << "ERROR: " << e.message() << std::endl;
861  #ifdef WIN32
862  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
863  #endif
864  return ERR_FAILED;
865  }
866 }
869  try{
870  katana->unBlock();
871  }
872  catch(Exception &e){
873  std::cout << "ERROR: " << e.message() << std::endl;
874  #ifdef WIN32
875  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
876  #endif
877  return ERR_FAILED;
878  }
879  return ERR_SUCCESS;
880 }
882 DLLEXPORT int setControllerParameters(int axis, int ki, int kspeed, int kpos){
883  try{
884  katana->GetBase()->GetMOT()->arr[axis-1].setControllerParameters(kspeed, kpos, ki);
885  }
886  catch(Exception &e){
887  std::cout << "ERROR: " << e.message() << std::endl;
888  #ifdef WIN32
889  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
890  #endif
891  return ERR_FAILED;
892  }
893  return ERR_SUCCESS;
894 }
896 DLLEXPORT int setMaxVelocity(int axis, int vel){
897  try{
898  if (axis == 0){
899  for(int i = 0; i <= getNumberOfMotors()-1; i++){
900  katana->setMotorVelocityLimit(i, vel);
901  }
902  }
903  else{
904  katana->setMotorVelocityLimit(axis-1, vel);
905  }
906  katana->setMaximumLinearVelocity((double)vel);
907  }
908  catch(Exception &e){
909  std::cout << "ERROR: " << e.message() << std::endl;
910  #ifdef WIN32
911  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
912  #endif
913  return ERR_FAILED;
914  }
915  return ERR_SUCCESS;
916 }
918 DLLEXPORT int setMaxAccel(int axis, int acceleration){
919  try{
920  if (axis == 0){
921  for(int i = 0; i <= getNumberOfMotors()-1; i++){
922  katana->setMotorAccelerationLimit(i, acceleration);
923  }
924  }
925  else{
926  katana->setMotorAccelerationLimit(axis-1, acceleration);
927  }
928 
929  }
930  catch(Exception &e){
931  std::cout << "ERROR: " << e.message() << std::endl;
932  #ifdef WIN32
933  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
934  #endif
935  return ERR_FAILED;
936  }
937  return ERR_SUCCESS;
938 }
941  try{
942  return katana->getNumberOfMotors();
943  }
944  catch(Exception &e){
945  std::cout << "ERROR: " << e.message() << std::endl;
946  #ifdef WIN32
947  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
948  #endif
949  return ERR_FAILED;
950  }
951  return ERR_SUCCESS;
952 }
954 DLLEXPORT int ping(int axis){
955  try{
956  //throws ParameterReadingException if unsuccessful
957  katana->GetBase()->recvECH();
958  }
959  catch(Exception &e){
960  std::cout << "ERROR: " << e.message() << std::endl;
961  #ifdef WIN32
962  MessageBox(NULL, TEXT(e.message().c_str()),TEXT("KNI ERROR"),MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
963  #endif
964  return ERR_FAILED;
965  }
966  return ERR_SUCCESS;
967 }
969 
970 
971 
double phi
the orientation of the TCP
Definition: kni_wrapper.h:53
int acceleration
static std::unique_ptr< CCdlSocket > device
Definition: kni_wrapper.cpp:23
unsigned char byte
type specification (8 bit)
Definition: cdlBase.h:29
DLLEXPORT int executeMovement(struct TMovement *movement)
static std::unique_ptr< CCplSerialCRC > protocol
Definition: kni_wrapper.cpp:24
Point-to-point movement.
Definition: kni_wrapper.h:58
std::string message() const
ETransition transition
the transition to this position, PTP or LINEAR
Definition: kni_wrapper.h:67
DLLEXPORT int initKatana(char *configFile, char *ipaddress)
This initializes the Katana (communication etc)
Definition: kni_wrapper.cpp:39
DLLEXPORT int moveMotAndWait(int axis, int targetpos, int tolerance)
int getVersion(char value[])
gets the controlboard firmware version and returns it in the value argument
DLLEXPORT int setCollisionParameters(int axis, int position, int velocity)
DLLEXPORT int moveToPosLin(struct TPos *targetPos, int velocity, int acceleration)
moves in LM
DLLEXPORT int setPositionCollisionLimit(int axis, int limit)
#define TM_ENDLESS
timeout symbol for &#39;endless&#39; waiting
Definition: kmlBase.h:51
Implement the Serial-Zero protocol.
Definition: cplSerial.h:137
TPos pos
The position, see above struct TPos.
Definition: kni_wrapper.h:65
DLLEXPORT int motorOn(int axis)
DLLEXPORT int moveToPos(struct TPos *pos, int velocity, int acceleration)
moves in IK
#define DLLEXPORT
Definition: kni_wrapper.h:30
DLLEXPORT int pushMovementToStack(struct TMovement *movement, char *name)
int getAxisFirmwareVersion(int axis, char value[])
gets the axis firmware version and returns it in the value argument
DLLEXPORT int getNumberOfMotors()
returns the number of motors configured
[TPS] target position
Definition: kmlMotBase.h:103
DLLEXPORT int ModBusTCP_writeWord(int address, int value)
int getEncoder(int axis, int &value)
DLLEXPORT int allMotorsOff()
extern C because we want to access these interfaces from anywhere:
Definition: kni_wrapper.h:49
double Z
Definition: kni_wrapper.h:51
clear the movebuffer
Definition: kmlMotBase.h:53
DLLEXPORT int clearMoveBuffers()
clears the movebuffers
DLLEXPORT int closeGripper()
int velocity
The velocitiy for this particular movement.
Definition: kni_wrapper.h:69
DLLEXPORT int setMaxAccel(int axis, int acceleration)
DLLEXPORT int openGripper()
DLLEXPORT int allMotorsOn()
int ModBusTCP_readWord(int address, int &value)
reads a value from the register &#39;address&#39; (if not connected, connect to the IP in katana...
int numberOfMotors
Definition: kni_wrapper.cpp:31
int IO_readInput(int inputNr, int &value)
DLLEXPORT int motorOff(int axis)
DLLEXPORT int getCurrentControllerType(int axis)
DLLEXPORT int IO_setOutput(char output, int value)
std::vector< TMovement * > movement_vector
linear movement
Definition: kni_wrapper.h:60
DLLEXPORT int getPosition(struct TPos *pos)
gets a position
DLLEXPORT int unblock()
unblocks the robot after collision/instantstop
static std::unique_ptr< CLMBase > katana
Definition: kni_wrapper.cpp:22
DLLEXPORT int executeConnectedMovement(struct TMovement *movement, struct TPos *startPos, bool first, bool last)
Encapsulates the socket communication device.
Definition: cdlSocket.h:65
DLLEXPORT int moveToPosEnc(int enc1, int enc2, int enc3, int enc4, int enc5, int enc6, int velocity, int acceleration, int tolerance, bool _wait)
DLLEXPORT int setMaxVelocity(int axis, int vel)
int acceleration
the acceleration for this particular movement
Definition: kni_wrapper.h:71
double X
The position in cartesian space.
Definition: kni_wrapper.h:51
DLLEXPORT int deleteMovementFromStack(char *name, int index)
Linear movement Class.
Definition: lmBase.h:73
DLLEXPORT int runThroughMovementStack(char *name, int loops)
int enc[10]
int getDrive(int axis, int &value)
double psi
Definition: kni_wrapper.h:53
structure for the
Definition: kni_wrapper.h:63
int getVelocity(int axis, int &value)
DLLEXPORT int sendSplineToMotor(int axis, int targetpos, int duration, int p0, int p1, int p2, int p3)
DLLEXPORT int startSplineMovement(int contd, int exactflag)
DLLEXPORT int setForceLimit(int axis, int limit)
int velocity
double Y
Definition: kni_wrapper.h:51
DLLEXPORT int setCollisionDetection(int axis, bool state)
byte packet[32]
Definition: kni_wrapper.cpp:33
byte size
Definition: kni_wrapper.cpp:35
DLLEXPORT int setControllerParameters(int axis, int ki, int kspeed, int kpos)
DLLEXPORT int deleteMovementStack(char *name)
deletes a movemnt stack
DLLEXPORT int flushMoveBuffers()
flush all the movebuffers
DLLEXPORT int ping(int axis)
byte buffer[256]
Definition: kni_wrapper.cpp:34
DLLEXPORT int calibrate(int axis)
Definition: kni_wrapper.cpp:63
std::map< std::string, std::vector< TMovement > > movements
Definition: kni_wrapper.cpp:27
DLLEXPORT int moveMot(int axis, int enc, int speed, int accel)
Definition: kni_wrapper.cpp:77
DLLEXPORT int getForce(int axis)
set the current force
std::vector< int > encoders
Definition: kni_wrapper.cpp:29
DLLEXPORT int setGripper(bool hasGripper)
double theta
Definition: kni_wrapper.h:53
DLLEXPORT int waitForMot(int axis, int targetpos, int tolerance)
Definition: kni_wrapper.cpp:92
DLLEXPORT int setVelocityCollisionLimit(int axis, int limit)


kni
Author(s): Martin Günther
autogenerated on Fri Jan 3 2020 04:01:16