ForceTorqueCtrl.cpp
Go to the documentation of this file.
1 /****************************************************************
2  *
3  * Copyright 2016 Intelligent Industrial Robotics (IIROB) Group,
4  * Institute for Anthropomatics and Robotics (IAR) -
5  * Intelligent Process Control and Robotics (IPR),
6  * Karlsruhe Institute of Technology
7  *
8  * Maintainer: Denis Štogl, email: denis.stogl@kit.edu
9  *
10  * Date of update: 2014-2016
11  *
12  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13  *
14  * Copyright (c) 2010
15  *
16  * Fraunhofer Institute for Manufacturing Engineering
17  * and Automation (IPA)
18  *
19  * Date of creation: June 2010
20  *
21  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions are met:
25  *
26  * * Redistributions of source code must retain the above copyright
27  * notice, this list of conditions and the following disclaimer.
28  * * Redistributions in binary form must reproduce the above copyright
29  * notice, this list of conditions and the following disclaimer in the
30  * documentation and/or other materials provided with the distribution.
31  * * Neither the name of the copyright holder nor the names of its
32  * contributors may be used to endorse or promote products derived from
33  * this software without specific prior written permission.
34  *
35  * This program is free software: you can redistribute it and/or modify
36  * it under the terms of the GNU Lesser General Public License LGPL as
37  * published by the Free Software Foundation, either version 3 of the
38  * License, or (at your option) any later version.
39  *
40  * This program is distributed in the hope that it will be useful,
41  * but WITHOUT ANY WARRANTY; without even the implied warranty of
42  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43  * GNU Lesser General Public License LGPL for more details.
44  *
45  * You should have received a copy of the GNU Lesser General Public
46  * License LGPL along with this program.
47  * If not, see <http://www.gnu.org/licenses/>.
48  *
49  ****************************************************************/
50 
51 // general includes
52 #include <unistd.h>
53 
55 
56 // Headrs provided by cob-packages
57 //#include <cob_generic_can/CanESD.h>
58 //#include <cob_generic_can/CanPeakSys.h>
61 
63 {
64  m_pCanCtrl = NULL;
65 
66  // for types and baudrates see:
67  // https://github.com/ipa320/cob_robots/blob/hydro_dev/cob_hardware_config/raw3-5/config/base/CanCtrl.ini
69  m_CanDevice = "/dev/pcan32";
71  m_CanBaseIdentifier = 0x20 << 4;
72 }
73 
74 ForceTorqueCtrl::ForceTorqueCtrl(int can_type, std::string can_path, int can_baudrate, int base_identifier)
75 {
76  m_pCanCtrl = NULL;
77 
78  // for types and baudrates see:
79  // https://github.com/ipa320/cob_robots/blob/hydro_dev/cob_hardware_config/raw3-5/config/base/CanCtrl.ini
80  m_CanType = can_type;
81  m_CanDevice = can_path;
82  m_CanBaudrate = can_baudrate;
83  m_CanBaseIdentifier = base_identifier << 4;
84 }
85 
87 {
88  if (m_pCanCtrl != NULL)
89  {
90  delete m_pCanCtrl;
91  }
92 }
93 
95 {
96  bool ret = true;
97 
98  if (initCan())
99  {
100  // This is way of testig if communication is also successful
101  if (!ReadFTSerialNumber())
102  {
103  std::cout << "Can not read Serial Number from FTS!" << std::endl;
104  ret = false;
105  }
106  if (!ReadFirmwareVersion())
107  {
108  std::cout << "Can not read Firmware version from FTS!" << std::endl;
109  ret = false;
110  }
111  if (!ReadCountsPerUnit())
112  {
113  std::cout << "Can not read Counts Per Unit from FTS!" << std::endl;
114  ret = false;
115  }
116  if (!ReadUnitCodes())
117  {
118  std::cout << "Can not read Unit Codes from FTS!" << std::endl;
119  ret = false;
120  }
121  // Add return values and checking
124  }
125  else
126  {
127  std::cout << "CAN initialisation unsuccessful!" << std::endl;
128  ret = false;
129  }
130 
131  return ret;
132 }
133 
135 {
136  bool ret = true;
137 
138  // current implementation only for CanPeakSysUSB and SocketCan
139  switch (m_CanType)
140  {
143  ret = m_pCanCtrl->init_ret();
144  break;
145 
147  m_pCanCtrl = new SocketCan(m_CanDevice.c_str());
148  ret = m_pCanCtrl->init_ret();
149  break;
150  }
151  return ret;
152 }
153 
155 {
156 #if DEBUG
157  std::cout << "\n\n*********FTSerialNumber**********" << std::endl;
158 #endif
159  bool ret = true;
160  CanMsg CMsg;
162  CMsg.setLength(0);
163  ret = m_pCanCtrl->transmitMsg(CMsg, true);
164  if (ret)
165  {
166  CanMsg replyMsg;
167  replyMsg.set(0, 0, 0, 0, 0, 0, 0, 0);
168  ret = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
169 
170  if (ret)
171  {
172 #if DEBUG
173  std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
174  std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
175 #endif
176  if (replyMsg.getID() == (m_CanBaseIdentifier | READ_SERIALNR))
177  {
178 #if DEBUG
179  std::cout << "Reading Serial Number Succeeded!" << std::endl;
180  std::cout << "reply Data: \t" << (char)replyMsg.getAt(0) << " " << (char)replyMsg.getAt(1) << " "
181  << (char)replyMsg.getAt(2) << " " << (char)replyMsg.getAt(3) << " " << (char)replyMsg.getAt(4) << " "
182  << (char)replyMsg.getAt(5) << " " << (char)replyMsg.getAt(6) << " " << (char)replyMsg.getAt(7)
183  << std::endl;
184 #endif
185  }
186  else
187  {
188 #if DEBUG
189  std::cout << "Error: Received wrong opcode!" << std::endl;
190 #endif
191  ret = false;
192  }
193  }
194  else
195  {
196 #if DEBUG
197  std::cout << "ForceTorqueCtrl::ReadFTSerialNumber(): Can not read message!" << std::endl;
198 #endif
199  }
200  }
201  else
202  {
203 #if DEBUG
204  std::cout << "ForceTorqueCtrl::ReadFTSerialNumber(): Can not transmit message!" << std::endl;
205 #endif
206  }
207 
208  return ret;
209 }
210 
212 {
213 #if DEBUG
214  std::cout << "\n\n*********Read Counts Per Unit**********" << std::endl;
215 #endif
216  bool ret = true;
217  float countsPerForce = 0, countsPerTorque = 0;
218  CanMsg CMsg;
220  CMsg.setLength(0);
221 
222  ret = m_pCanCtrl->transmitMsg(CMsg, true);
223 
224  if (ret)
225  {
226  CanMsg replyMsg;
227  ret = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
228 
229  if (ret)
230  {
231 #if DEBUG
232  std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
233  std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
234 #endif
235  if (replyMsg.getID() == (m_CanBaseIdentifier | READ_COUNTSPERU))
236  {
237 #if DEBUG
238  std::cout << "Reading Counts Per Unit Succeeded!" << std::endl;
239  std::cout << "reply Data: \t" << (char)replyMsg.getAt(0) << " " << (char)replyMsg.getAt(1) << " "
240  << (char)replyMsg.getAt(2) << " " << (char)replyMsg.getAt(3) << " " << (char)replyMsg.getAt(4) << " "
241  << (char)replyMsg.getAt(5) << " " << (char)replyMsg.getAt(6) << " " << (char)replyMsg.getAt(7)
242  << std::endl;
243 #endif
244  }
245  else
246  {
247 #if DEBUG
248  std::cout << "Error: Received wrong opcode!" << std::endl;
249 #endif
250  ret = false;
251  }
252 
253  intbBuf.bytes[0] = replyMsg.getAt(3);
254  intbBuf.bytes[1] = replyMsg.getAt(2);
255  intbBuf.bytes[2] = replyMsg.getAt(1);
256  intbBuf.bytes[3] = replyMsg.getAt(0);
257  countsPerForce = intbBuf.value;
258 
259  intbBuf.bytes[0] = replyMsg.getAt(7);
260  intbBuf.bytes[1] = replyMsg.getAt(6);
261  intbBuf.bytes[2] = replyMsg.getAt(5);
262  intbBuf.bytes[3] = replyMsg.getAt(4);
263  countsPerTorque = intbBuf.value;
264  }
265  else
266  {
267  std::cout << "ForceTorqueCtrl::ReadCountsPerUnit(): Can not read message!" << std::endl;
268  }
269  }
270  else
271  {
272  std::cout << "ForceTorqueCtrl::ReadCountsPerUnit(): Can not transmit message!" << std::endl;
273  }
274 #if DEBUG
275  std::cout << "CountsPerforce: " << countsPerForce << " CountsPerTorque: " << countsPerTorque << std::endl;
276 #endif
277  return ret;
278 }
279 
281 {
282 #if DEBUG
283  std::cout << "\n\n*********Read Unit Codes**********" << std::endl;
284 #endif
285  bool ret = true;
286  CanMsg CMsg;
288  CMsg.setLength(0);
289 
290  ret = m_pCanCtrl->transmitMsg(CMsg, true);
291 
292  if (ret)
293  {
294  CanMsg replyMsg;
295  ret = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
296 
297  if (ret)
298  {
299 #if DEBUG
300  std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
301  std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
302 #endif
303  if (replyMsg.getID() == (m_CanBaseIdentifier | READ_UNITCODE))
304  {
305 #if DEBUG
306  std::cout << "Reading Unit codes Succeed!" << std::endl;
307  std::cout << "reply Data: \t" << replyMsg.getAt(0) << " " << replyMsg.getAt(1) << std::endl;
308 #endif
309  }
310  }
311  else
312  {
313  std::cout << "ForceTorqueCtrl::ReadUnitCodes(): Can not read message!" << std::endl;
314  }
315  }
316  else
317  {
318  std::cout << "ForceTorqueCtrl::ReadUnitCodes(): Can not transmit message!" << std::endl;
319  }
320 
321  return ret;
322 }
323 
325 {
326  // TODO: Check for Init
327 #if DEBUG
328  std::cout << "\n\n*******Read Diagnostic ADC Voltages on index: " << index << "********" << std::endl;
329 #endif
330  bool ret = true;
331  CanMsg CMsg;
333  CMsg.setLength(1);
334  // TODO: offset or typecast index
335  CMsg.setAt(index, 0);
336 
337  ret = m_pCanCtrl->transmitMsg(CMsg, true);
338 
339  if (ret)
340  {
341  CanMsg replyMsg;
342  ret = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
343  if (ret)
344  {
345 #if DEBUG
346  std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
347  std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
348 #endif
349  if (replyMsg.getID() == (m_CanBaseIdentifier | READ_DIAGNOV))
350  {
351 #if DEBUG
352  std::cout << "Reading Diagnostic ADC Voltage succeed!" << std::endl;
353  std::cout << "ADC Voltage of diagnostic value " << index << " : " << replyMsg.getAt(0) << " "
354  << replyMsg.getAt(1) << std::endl;
355 #endif
356 
357  ibBuf.bytes[0] = replyMsg.getAt(1);
358  ibBuf.bytes[1] = replyMsg.getAt(0);
359  value = ibBuf.value;
360  }
361  else
362  std::cout << "Error: Received wrong opcode!" << std::endl;
363  }
364  else
365  std::cout << "Error: Receiving Message failed!" << std::endl;
366  }
367  else
368  {
369  std::cout << "ForceTorqueCtrl::readDiagnosticADCVoltages(byte index): Can not transmit message!" << std::endl;
370  }
371 
372  return ret;
373 }
374 
376 {
377 #if DEBUG
378  std::cout << "\n\n*******Setting Active Calibration Matrix Num to: " << num << "********" << std::endl;
379 #endif
380  bool ret = true;
381  CanMsg CMsg;
383  CMsg.setLength(1);
384  CMsg.setAt(num, 0);
385 
386  ret = m_pCanCtrl->transmitMsg(CMsg, true);
387 
388  if (ret)
389  {
390  CanMsg replyMsg;
391  ret = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
392  if (ret)
393  {
394 #if DEBUG
395  std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
396  std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
397 #endif
398  if (replyMsg.getID() == (m_CanBaseIdentifier | SET_CALIB))
399  {
400 #if DEBUG
401  std::cout << "Setting Calibration Matrix succeed!" << std::endl;
402  std::cout << "Calibration Matrix: " << replyMsg.getAt(0) << " is Activ!" << std::endl;
403 #endif
404  }
405  else
406  {
407 #if DEBUG
408  std::cout << "Error: Received wrong opcode!" << std::endl;
409 #endif
410  ret = false;
411  }
412  }
413  else
414  {
415  std::cout << "Error: Receiving Message failed!" << std::endl;
416  ret = false;
417  }
418  }
419  else
420  {
421  std::cout << "ForceTorqueCtrl::SetActiveCalibrationMatrix(int num): Can not transmit message!" << std::endl;
422  ret = false;
423  }
424 
425  return ret;
426 }
427 
429 {
430 #if DEBUG
431  std::cout << "\n\n*******Setting Baud Rate value to: " << value << "********" << std::endl;
432 #endif
433  bool ret = true;
434  CanMsg CMsg;
436  CMsg.setLength(1);
437  CMsg.setAt(value, 0);
438 
439  ret = m_pCanCtrl->transmitMsg(CMsg, true);
440 
441  if (ret)
442  {
443  CanMsg replyMsg;
444  ret = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
445  if (ret)
446  {
447 #if DEBUG
448  std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
449  std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
450 #endif
451  if (replyMsg.getID() == (m_CanBaseIdentifier | SET_BAUD))
452  {
453 #if DEBUG
454  std::cout << "Send Baud Rate value: " << CMsg.getAt(0) << "!" << std::endl;
455  std::cout << "Setting Baud Rate succeed!" << std::endl;
456 #endif
457  }
458  else
459  {
460  std::cout << "Error: Received wrong opcode!" << std::endl;
461  ret = false;
462  }
463  }
464  else
465  {
466  std::cout << "Error: Receiving Message failed!" << std::endl;
467  ret = false;
468  }
469  }
470  else
471  {
472  std::cout << "ForceTorqueCtrl::SetBaudRate(int value): Can not transmit message!" << std::endl;
473  ret = false;
474  }
475 
476  return ret;
477 }
478 
480 {
481  std::cout << "\n\n******* Reseting the NETCANOEM ********" << std::endl;
482  bool ret = true;
483  CanMsg CMsg;
485  CMsg.setLength(0);
486 
487  ret = m_pCanCtrl->transmitMsg(CMsg, true);
488 
489  if (!ret)
490  {
491  std::cout << "ForceTorqueCtrl::Reset(): Can not transmit message!" << std::endl;
492  ret = false;
493  }
494 
495  usleep(10000);
496 
497  return ret;
498 }
499 
501 {
502 #if DEBUG
503  std::cout << "\n\n*******Setting Base Identifier value to HEX : " << std::hex << identifier << " ********"
504  << std::endl;
505 #endif
506  bool ret = true;
507  CanMsg CMsg;
509  CMsg.setLength(1);
510  CMsg.setAt(identifier, 0);
511 
512  ret = m_pCanCtrl->transmitMsg(CMsg, true);
513 
514  if (ret)
515  {
516  CanMsg replyMsg;
517  ret = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
518  if (ret)
519  {
520 #if DEBUG
521  std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
522  std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
523 #endif
524  if (replyMsg.getID() == (m_CanBaseIdentifier | SET_BASEID))
525  {
526 #if DEBUG
527  std::cout << "Setting Base Identifier succeed!" << std::endl;
528  std::cout << "Send Base Identifier value: " << std::hex << CMsg.getAt(0) << "!" << std::endl;
529 #endif
530  ret = true;
531  }
532  else
533  {
534  std::cout << "Error: Received wrong opcode!" << std::endl;
535  ret = false;
536  }
537  }
538  else
539  {
540  std::cout << "Error: Receiving Message failed!" << std::endl;
541  ret = false;
542  }
543  }
544  else
545  {
546  std::cout << "ForceTorqueCtrl::SetBaseIdentifier(int identifier): Can not transmit message!" << std::endl;
547  ret = false;
548  }
549 
550  return ret;
551 }
552 
554 {
555  Eigen::VectorXf vCoef(6);
556 
557  // Read Fx coefficients
558  ReadMatrix(0, vCoef);
559  m_v3FXGain = vCoef;
560 
561  // Read Fy coefficients
562  ReadMatrix(1, vCoef);
563  m_v3FYGain = vCoef;
564 
565  // Read Fz coefficients
566  ReadMatrix(2, vCoef);
567  m_v3FZGain = vCoef;
568 
569  // Read Tx coefficients
570  ReadMatrix(3, vCoef);
571  m_v3TXGain = vCoef;
572 
573  // Read Ty coefficients
574  ReadMatrix(4, vCoef);
575  m_v3TYGain = vCoef;
576 
577  // Read Tz coefficients
578  ReadMatrix(5, vCoef);
579  m_v3TZGain = vCoef;
580  SetCalibMatrix();
581 }
582 
583 void ForceTorqueCtrl::ReadMatrix(int axis, Eigen::VectorXf &vec)
584 {
585 #if DEBUG
586  std::cout << "\n\n*******Read Matrix**********" << std::endl;
587 #endif
588  float statusCode = 0, sg0 = 0.0, sg1 = 0.0, sg2 = 0.0, sg3 = 0.0, sg4 = 0.0, sg5 = 0.0;
589 
590  CanMsg CMsg;
592  CMsg.setLength(1);
593  CMsg.setAt(axis, 0);
594 
595  bool ret = m_pCanCtrl->transmitMsg(CMsg, true);
596  if (!ret)
597  {
598  std::cout << "Error: Requesting Calibration Matrix!" << std::endl;
599  return;
600  }
601 
602  CanMsg replyMsg;
603  bool ret2 = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
604  if (ret2)
605  {
606 #if DEBUG
607  std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
608  std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
609 #endif
610  if (replyMsg.getID() == (m_CanBaseIdentifier | READ_MATRIX))
611  {
612 #if DEBUG
613  std::cout << "Reading Matrix Succeeded!" << std::endl;
614  std::cout << "reply Data: \t" << replyMsg.getAt(0) << " " << replyMsg.getAt(1) << " " << replyMsg.getAt(2) << " "
615  << replyMsg.getAt(3) << " " << replyMsg.getAt(4) << " " << replyMsg.getAt(5) << " " << replyMsg.getAt(6)
616  << " " << replyMsg.getAt(7) << std::endl;
617 #endif
618  }
619  else
620  {
621  std::cout << "Error: Received wrong opcode!" << std::endl;
622  ret = false;
623  }
624 
625  fbBuf.bytes[0] = replyMsg.getAt(3);
626  fbBuf.bytes[1] = replyMsg.getAt(2);
627  fbBuf.bytes[2] = replyMsg.getAt(1);
628  fbBuf.bytes[3] = replyMsg.getAt(0);
629  sg0 = fbBuf.value;
630 
631  fbBuf.bytes[0] = replyMsg.getAt(7);
632  fbBuf.bytes[1] = replyMsg.getAt(6);
633  fbBuf.bytes[2] = replyMsg.getAt(5);
634  fbBuf.bytes[3] = replyMsg.getAt(4);
635  sg1 = fbBuf.value;
636  }
637  else
638  return;
639 
640  ret2 = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
641  if (ret2)
642  {
643 #if DEBUG
644  std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
645  std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
646  std::cout << "reply Data: \t" << replyMsg.getAt(0) << " " << replyMsg.getAt(1) << " " << replyMsg.getAt(2) << " "
647  << replyMsg.getAt(3) << " " << replyMsg.getAt(4) << " " << replyMsg.getAt(5) << " " << replyMsg.getAt(6)
648  << " " << replyMsg.getAt(7) << std::endl;
649 #endif
650 
651  fbBuf.bytes[0] = replyMsg.getAt(3);
652  fbBuf.bytes[1] = replyMsg.getAt(2);
653  fbBuf.bytes[2] = replyMsg.getAt(1);
654  fbBuf.bytes[3] = replyMsg.getAt(0);
655  sg2 = fbBuf.value;
656 
657  fbBuf.bytes[0] = replyMsg.getAt(7);
658  fbBuf.bytes[1] = replyMsg.getAt(6);
659  fbBuf.bytes[2] = replyMsg.getAt(5);
660  fbBuf.bytes[3] = replyMsg.getAt(4);
661  sg3 = fbBuf.value;
662  }
663  else
664  return;
665 
666  ret2 = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
667  if (ret2)
668  {
669 #if DEBUG
670  std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
671  std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
672  std::cout << "reply Data: \t" << replyMsg.getAt(0) << " " << replyMsg.getAt(1) << " " << replyMsg.getAt(2) << " "
673  << replyMsg.getAt(3) << " " << replyMsg.getAt(4) << " " << replyMsg.getAt(5) << " " << replyMsg.getAt(6)
674  << " " << replyMsg.getAt(7) << std::endl;
675 #endif
676 
677  fbBuf.bytes[0] = replyMsg.getAt(3);
678  fbBuf.bytes[1] = replyMsg.getAt(2);
679  fbBuf.bytes[2] = replyMsg.getAt(1);
680  fbBuf.bytes[3] = replyMsg.getAt(0);
681  sg4 = fbBuf.value;
682 
683  fbBuf.bytes[0] = replyMsg.getAt(7);
684  fbBuf.bytes[1] = replyMsg.getAt(6);
685  fbBuf.bytes[2] = replyMsg.getAt(5);
686  fbBuf.bytes[3] = replyMsg.getAt(4);
687  sg5 = fbBuf.value;
688  }
689  else
690  return;
691 
692  vec[0] = sg0;
693  vec[1] = sg1;
694  vec[2] = sg2;
695  vec[3] = sg3;
696  vec[4] = sg4;
697  vec[5] = sg5;
698 #if DEBUG
699  std::cout << "Matix: SG0: " << sg0 << " SG1: " << sg1 << " SG2: " << sg2 << " SG3: " << sg3 << " SG4: " << sg4
700  << " SG5: " << sg5 << std::endl;
701 #endif
702 }
703 
705 {
706 #if DEBUG
707  std::cout << "\n\n*******Reading Firmware Version*******" << std::endl;
708 #endif
709  bool ret = true;
710  CanMsg CMsg;
712  CMsg.setLength(0);
713 
714  ret = m_pCanCtrl->transmitMsg(CMsg, true);
715 
716  if (ret)
717  {
718  CanMsg replyMsg;
719  ret = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
720  if (ret)
721  {
722 #if DEBUG
723  std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
724  std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
725 #endif
726  if (replyMsg.getID() == (m_CanBaseIdentifier | READ_FIRMWARE))
727  {
728 #if DEBUG
729  std::cout << "Reading Firmware Succeed!" << std::endl;
730  std::cout << "reply Data: \t" << replyMsg.getAt(0) << " " << replyMsg.getAt(1) << " " << replyMsg.getAt(2)
731  << " " << replyMsg.getAt(3) << std::endl;
732 #endif
733  }
734  else
735  {
736  std::cout << "Error: Received wrong opcode!" << std::endl;
737  ret = false;
738  }
739  }
740  else
741  {
742  std::cout << "Error: Receiving Message failed!" << std::endl;
743  ret = false;
744  }
745  }
746  else
747  {
748  std::cout << "Error: Transmiting Message failed!" << std::endl;
749  ret = false;
750  }
751 
752  return ret;
753 }
754 
755 bool ForceTorqueCtrl::ReadSGData(int statusCode, double &Fx, double &Fy, double &Fz, double &Tx, double &Ty, double &Tz)
756 {
757  int sg0 = 0, sg1 = 0, sg2 = 0, sg3 = 0, sg4 = 0, sg5 = 0;
758 
759  CanMsg CMsg;
761  CMsg.setLength(0);
762  bool ret = m_pCanCtrl->transmitMsg(CMsg, true);
763 
764  if (!ret)
765  {
766  std::cout << "ForceTorqueCtrl::ReadSGData: Error: Transmiting message failed!" << std::endl;
767  return false;
768  }
769 
770  CanMsg replyMsg;
771  bool ret2 = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
772  unsigned char c[2];
773  if (ret2)
774  {
775  if (replyMsg.getID() != (m_CanBaseIdentifier | 0x0))
776  {
777  std::cout << "ForceTorqueCtrl::ReadSGData: Error: Received wrong opcode (Should be 0x200)!" << std::endl;
778  std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
779  std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
780  std::cout << "reply Data: \t" << replyMsg.getAt(0) << " " << replyMsg.getAt(1) << " " << replyMsg.getAt(2) << " "
781  << replyMsg.getAt(3) << " " << replyMsg.getAt(4) << " " << replyMsg.getAt(5) << " " << replyMsg.getAt(6)
782  << " " << replyMsg.getAt(7) << std::endl;
783  return false ;
784  }
785 
786  c[0] = replyMsg.getAt(0); // status code
787  c[1] = replyMsg.getAt(1);
788  statusCode = (short)((c[0] << 8) | c[1]);
789 
790  if (statusCode != 0)
791  {
792  if (statusCode & 0x4000)
793  {
794  std::cout << "ForceTorqueCtrl::ReadSGData: CAN bus error detected!" << std::endl;
795  Reset();
796  std::cout << "ForceTorqueCtrl::ReadSGData: FTS reseted!" << std::endl;
797  }
798  else
799  {
800  std::cout << "ForceTorqueCtrl::ReadSGData: Error: Something is wrong with sensor!" << std::endl;
801  std::cout << std::hex << statusCode << std::endl;
802  }
803  }
804 
805  c[0] = replyMsg.getAt(2); // sg0
806  c[1] = replyMsg.getAt(3);
807  sg0 = (short)((c[0] << 8) | c[1]);
808 
809  c[0] = replyMsg.getAt(4); // sg2
810  c[1] = replyMsg.getAt(5);
811  sg2 = (short)((c[0] << 8) | c[1]);
812 
813  c[0] = replyMsg.getAt(6); // sg4
814  c[1] = replyMsg.getAt(7);
815  sg4 = (short)((c[0] << 8) | c[1]);
816  }
817  else
818  return false;
819 
820  ret2 = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
821  if (ret2)
822  {
823  if (replyMsg.getID() != (m_CanBaseIdentifier | 0x1))
824  {
825  std::cout << "ForceTorqueCtrl::ReadSGData: Error: Received wrong opcode (Should be 0x201)!" << std::endl;
826  std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
827  std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
828  std::cout << "reply Data: \t" << replyMsg.getAt(0) << " " << replyMsg.getAt(1) << " " << replyMsg.getAt(2) << " "
829  << replyMsg.getAt(3) << " " << replyMsg.getAt(4) << " " << replyMsg.getAt(5) << " " << replyMsg.getAt(6)
830  << " " << replyMsg.getAt(7) << std::endl;
831  return false ;
832  }
833 
834  c[0] = replyMsg.getAt(0); // sg1
835  c[1] = replyMsg.getAt(1);
836  sg1 = (short)((c[0] << 8) | c[1]);
837 
838  c[0] = replyMsg.getAt(2); // sg3
839  c[1] = replyMsg.getAt(3);
840  sg3 = (short)((c[0] << 8) | c[1]);
841 
842  c[0] = replyMsg.getAt(4); // sg5
843  c[1] = replyMsg.getAt(5);
844  sg5 = (short)((c[0] << 8) | c[1]);
845  }
846  else
847  return false;
848 
849  StrainGaugeToForce(sg0, sg1, sg2, sg3, sg4, sg5);
850  Fx = m_vForceData(0);
851  Fy = m_vForceData(1);
852  Fz = m_vForceData(2);
853  Tx = m_vForceData(3);
854  Ty = m_vForceData(4);
855  Tz = m_vForceData(5);
856  return true;
857 }
858 
859 void ForceTorqueCtrl::StrainGaugeToForce(int &sg0, int &sg1, int &sg2, int &sg3, int &sg4, int &sg5)
860 {
861  Eigen::VectorXf v6SG(6);
862  Eigen::VectorXf v6tmp(6);
863  Eigen::VectorXf test(6);
864 
865  v6SG[0] = sg0;
866  v6SG[1] = sg1;
867  v6SG[2] = sg2;
868  v6SG[3] = sg3;
869  v6SG[4] = sg4;
870  v6SG[5] = sg5;
871  test = m_mXCalibMatrix * v6SG;
872  m_vForceData = test * 0.000001;
873 }
874 
875 void ForceTorqueCtrl::SetGaugeOffset(float sg0Off, float sg1Off, float sg2Off, float sg3Off, float sg4Off, float sg5Off)
876 {
877  Eigen::VectorXf tmp(6);
878  tmp[0] = sg0Off;
879  tmp[1] = sg1Off;
880  tmp[2] = sg2Off;
881  tmp[3] = sg3Off;
882  tmp[4] = sg4Off;
883  tmp[5] = sg5Off;
884  m_v3StrainGaigeOffset = tmp;
885 }
886 void ForceTorqueCtrl::SetGaugeGain(float gg0, float gg1, float gg2, float gg3, float gg4, float gg5)
887 {
888  Eigen::VectorXf tmp(6);
889  tmp[0] = gg0;
890  tmp[1] = gg1;
891  tmp[2] = gg2;
892  tmp[3] = gg3;
893  tmp[4] = gg4;
894  tmp[5] = gg5;
895  m_v3GaugeGain = tmp;
896  // std::cout<<"GaugeGain: \n"<<m_v3GaugeGain<<"\n\n";
897 }
898 
899 void ForceTorqueCtrl::SetFXGain(float fxg0, float fxg1, float fxg2, float fxg3, float fxg4, float fxg5)
900 {
901  Eigen::VectorXf tmp(6);
902  tmp[0] = fxg0;
903  tmp[1] = fxg1;
904  tmp[2] = fxg2;
905  tmp[3] = fxg3;
906  tmp[4] = fxg4;
907  tmp[5] = fxg5;
908  m_v3FXGain = tmp;
909  // std::cout<<"FXGain: \n"<<m_v3FXGain<<"\n\n";
910 }
911 void ForceTorqueCtrl::SetFYGain(float fyg0, float fyg1, float fyg2, float fyg3, float fyg4, float fyg5)
912 {
913  Eigen::VectorXf tmp(6);
914  tmp[0] = fyg0;
915  tmp[1] = fyg1;
916  tmp[2] = fyg2;
917  tmp[3] = fyg3;
918  tmp[4] = fyg4;
919  tmp[5] = fyg5;
920  m_v3FYGain = tmp;
921  // std::cout<<"FYGain: \n"<<m_v3FYGain<<"\n\n";
922 }
923 void ForceTorqueCtrl::SetFZGain(float fzg0, float fzg1, float fzg2, float fzg3, float fzg4, float fzg5)
924 {
925  Eigen::VectorXf tmp(6);
926  tmp[0] = fzg0;
927  tmp[1] = fzg1;
928  tmp[2] = fzg2;
929  tmp[3] = fzg3;
930  tmp[4] = fzg4;
931  tmp[5] = fzg5;
932  m_v3FZGain = tmp;
933  // std::cout<<"FZGain: \n"<<m_v3FZGain<<"\n\n";
934 }
935 void ForceTorqueCtrl::SetTXGain(float txg0, float txg1, float txg2, float txg3, float txg4, float txg5)
936 {
937  Eigen::VectorXf tmp(6);
938  tmp[0] = txg0;
939  tmp[1] = txg1;
940  tmp[2] = txg2;
941  tmp[3] = txg3;
942  tmp[4] = txg4;
943  tmp[5] = txg5;
944  m_v3TXGain = tmp;
945  // std::cout<<"TXGain: \n"<<m_v3TXGain<<"\n\n";
946 }
947 void ForceTorqueCtrl::SetTYGain(float tyg0, float tyg1, float tyg2, float tyg3, float tyg4, float tyg5)
948 {
949  Eigen::VectorXf tmp(6);
950  tmp[0] = tyg0;
951  tmp[1] = tyg1;
952  tmp[2] = tyg2;
953  tmp[3] = tyg3;
954  tmp[4] = tyg4;
955  tmp[5] = tyg5;
956  m_v3TYGain = tmp;
957  // std::cout<<"TYGain: \n"<<m_v3TYGain<<"\n\n";
958 }
959 void ForceTorqueCtrl::SetTZGain(float tzg0, float tzg1, float tzg2, float tzg3, float tzg4, float tzg5)
960 {
961  Eigen::VectorXf tmp(6);
962  tmp[0] = tzg0;
963  tmp[1] = tzg1;
964  tmp[2] = tzg2;
965  tmp[3] = tzg3;
966  tmp[4] = tzg4;
967  tmp[5] = tzg5;
968  m_v3TZGain = tmp;
969  // std::cout<<"TZGain: \n"<<m_v3TZGain<<"\n\n";
970 }
971 
973 {
974  Eigen::MatrixXf tmp(6, 6);
975  tmp(0) = m_v3FXGain[0] / m_v3GaugeGain[0];
976  tmp(1) = m_v3FXGain[1] / m_v3GaugeGain[1];
977  tmp(2) = m_v3FXGain[2] / m_v3GaugeGain[2];
978  tmp(3) = m_v3FXGain[3] / m_v3GaugeGain[3];
979  tmp(4) = m_v3FXGain[4] / m_v3GaugeGain[4];
980  tmp(5) = m_v3FXGain[5] / m_v3GaugeGain[5];
981 
982  tmp(6) = m_v3FYGain[0] / m_v3GaugeGain[0];
983  tmp(7) = m_v3FYGain[1] / m_v3GaugeGain[1];
984  tmp(8) = m_v3FYGain[2] / m_v3GaugeGain[2];
985  tmp(9) = m_v3FYGain[3] / m_v3GaugeGain[3];
986  tmp(10) = m_v3FYGain[4] / m_v3GaugeGain[4];
987  tmp(11) = m_v3FYGain[5] / m_v3GaugeGain[5];
988 
989  tmp(12) = m_v3FZGain[0] / m_v3GaugeGain[0];
990  tmp(13) = m_v3FZGain[1] / m_v3GaugeGain[1];
991  tmp(14) = m_v3FZGain[2] / m_v3GaugeGain[2];
992  tmp(15) = m_v3FZGain[3] / m_v3GaugeGain[3];
993  tmp(16) = m_v3FZGain[4] / m_v3GaugeGain[4];
994  tmp(17) = m_v3FZGain[5] / m_v3GaugeGain[5];
995 
996  tmp(18) = m_v3TXGain[0] / m_v3GaugeGain[0];
997  tmp(19) = m_v3TXGain[1] / m_v3GaugeGain[1];
998  tmp(20) = m_v3TXGain[2] / m_v3GaugeGain[2];
999  tmp(21) = m_v3TXGain[3] / m_v3GaugeGain[3];
1000  tmp(22) = m_v3TXGain[4] / m_v3GaugeGain[4];
1001  tmp(23) = m_v3TXGain[5] / m_v3GaugeGain[5];
1002 
1003  tmp(24) = m_v3TYGain[0] / m_v3GaugeGain[0];
1004  tmp(25) = m_v3TYGain[1] / m_v3GaugeGain[1];
1005  tmp(26) = m_v3TYGain[2] / m_v3GaugeGain[2];
1006  tmp(27) = m_v3TYGain[3] / m_v3GaugeGain[3];
1007  tmp(28) = m_v3TYGain[4] / m_v3GaugeGain[4];
1008  tmp(29) = m_v3TYGain[5] / m_v3GaugeGain[5];
1009 
1010  tmp(30) = m_v3TZGain[0] / m_v3GaugeGain[0];
1011  tmp(31) = m_v3TZGain[1] / m_v3GaugeGain[1];
1012  tmp(32) = m_v3TZGain[2] / m_v3GaugeGain[2];
1013  tmp(33) = m_v3TZGain[3] / m_v3GaugeGain[3];
1014  tmp(34) = m_v3TZGain[4] / m_v3GaugeGain[4];
1015  tmp(35) = m_v3TZGain[5] / m_v3GaugeGain[5];
1016 
1017  m_mXCalibMatrix = tmp;
1018 }
1019 
1021 {
1022  Eigen::MatrixXf tmp(6, 6);
1023  tmp(0) = m_v3FXGain[0];
1024  tmp(1) = m_v3FXGain[1];
1025  tmp(2) = m_v3FXGain[2];
1026  tmp(3) = m_v3FXGain[3];
1027  tmp(4) = m_v3FXGain[4];
1028  tmp(5) = m_v3FXGain[5];
1029 
1030  tmp(6) = m_v3FYGain[0];
1031  tmp(7) = m_v3FYGain[1];
1032  tmp(8) = m_v3FYGain[2];
1033  tmp(9) = m_v3FYGain[3];
1034  tmp(10) = m_v3FYGain[4];
1035  tmp(11) = m_v3FYGain[5];
1036 
1037  tmp(12) = m_v3FZGain[0];
1038  tmp(13) = m_v3FZGain[1];
1039  tmp(14) = m_v3FZGain[2];
1040  tmp(15) = m_v3FZGain[3];
1041  tmp(16) = m_v3FZGain[4];
1042  tmp(17) = m_v3FZGain[5];
1043 
1044  tmp(18) = m_v3TXGain[0];
1045  tmp(19) = m_v3TXGain[1];
1046  tmp(20) = m_v3TXGain[2];
1047  tmp(21) = m_v3TXGain[3];
1048  tmp(22) = m_v3TXGain[4];
1049  tmp(23) = m_v3TXGain[5];
1050 
1051  tmp(24) = m_v3TYGain[0];
1052  tmp(25) = m_v3TYGain[1];
1053  tmp(26) = m_v3TYGain[2];
1054  tmp(27) = m_v3TYGain[3];
1055  tmp(28) = m_v3TYGain[4];
1056  tmp(29) = m_v3TYGain[5];
1057 
1058  tmp(30) = m_v3TZGain[0];
1059  tmp(31) = m_v3TZGain[1];
1060  tmp(32) = m_v3TZGain[2];
1061  tmp(33) = m_v3TZGain[3];
1062  tmp(34) = m_v3TZGain[4];
1063  tmp(35) = m_v3TZGain[5];
1064 
1065  m_mXCalibMatrix = tmp.transpose();
1066 }
Eigen::VectorXf m_v3GaugeGain
#define READ_SERIALNR
Eigen::MatrixXf m_vForceData
std::string m_CanDevice
void setLength(int len)
#define READ_COUNTSPERU
Eigen::VectorXf m_v3FXGain
void SetFYGain(float fyg0, float fyg1, float fyg2, float fyg3, float fyg4, float fyg5)
void SetTYGain(float tyg0, float tyg1, float tyg2, float tyg3, float tyg4, float tyg5)
int getID()
bool ReadSGData(int statusCode, double &Fx, double &Fy, double &Fz, double &Tx, double &Ty, double &Tz)
Eigen::MatrixXf m_mXCalibMatrix
void SetTZGain(float tzg0, float tzg1, float tzg2, float tzg3, float tzg4, float tzg5)
#define READ_MATRIX
void setAt(BYTE data, int iNr)
void SetFZGain(float fzg0, float fzg1, float fzg2, float fzg3, float fzg4, float fzg5)
bool readDiagnosticADCVoltages(int index, short int &value)
void SetTXGain(float txg0, float txg1, float txg2, float txg3, float txg4, float txg5)
Eigen::VectorXf m_v3FYGain
bool SetBaudRate(int value)
#define CANITFBAUD_250K
#define READ_DIAGNOV
#define READ_UNITCODE
bool SetActiveCalibrationMatrix(int num)
Eigen::VectorXf m_v3TZGain
void SetFXGain(float fxg0, float fxg1, float fxg2, float fxg3, float fxg4, float fxg5)
void ReadMatrix(int axis, Eigen::VectorXf &vec)
union ForceTorqueCtrl::@3 ibBuf
void SetGaugeOffset(float sg0Off, float sg1Off, float sg2Off, float sg3Off, float sg4Off, float sg5Off)
Eigen::VectorXf m_v3TYGain
virtual bool init_ret()=0
union ForceTorqueCtrl::@4 intbBuf
bool SetBaseIdentifier(int identifier)
void setID(int id)
Eigen::VectorXf m_v3FZGain
virtual bool receiveMsgRetry(CanMsg *pCMsg, int iNrOfRetry)=0
void set(BYTE Data0=0, BYTE Data1=0, BYTE Data2=0, BYTE Data3=0, BYTE Data4=0, BYTE Data5=0, BYTE Data6=0, BYTE Data7=0)
#define CANITFTYPE_SOCKET_CAN
#define SET_BAUD
union ForceTorqueCtrl::@5 fbBuf
#define RESET
int getLength()
#define READ_FIRMWARE
#define SET_BASEID
Eigen::VectorXf m_v3StrainGaigeOffset
#define READ_SG
Eigen::VectorXf m_v3TXGain
virtual bool transmitMsg(CanMsg CMsg, bool bBlocking=true)=0
int getAt(int iNr)
void StrainGaugeToForce(int &sg0, int &sg1, int &sg2, int &sg3, int &sg4, int &sg5)
#define CANITFTYPE_CAN_PEAK_USB
#define SET_CALIB
void SetGaugeGain(float gg0, float gg1, float gg2, float gg3, float gg4, float gg5)


ati_force_torque
Author(s): Denis Štogl, Alexander Bubeck
autogenerated on Thu Sep 17 2020 03:18:35