ForceTorqueCtrl.cpp
Go to the documentation of this file.
00001 /****************************************************************
00002  *
00003  * Copyright 2016 Intelligent Industrial Robotics (IIROB) Group,
00004  * Institute for Anthropomatics and Robotics (IAR) -
00005  * Intelligent Process Control and Robotics (IPR),
00006  * Karlsruhe Institute of Technology
00007  *
00008  * Maintainer: Denis Štogl, email: denis.stogl@kit.edu
00009  *
00010  * Date of update: 2014-2016
00011  *
00012  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00013  *
00014  * Copyright (c) 2010
00015  *
00016  * Fraunhofer Institute for Manufacturing Engineering
00017  * and Automation (IPA)
00018  *
00019  * Date of creation: June 2010
00020  *
00021  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00022  *
00023  * Redistribution and use in source and binary forms, with or without
00024  * modification, are permitted provided that the following conditions are met:
00025  *
00026  *     * Redistributions of source code must retain the above copyright
00027  *       notice, this list of conditions and the following disclaimer.
00028  *     * Redistributions in binary form must reproduce the above copyright
00029  *       notice, this list of conditions and the following disclaimer in the
00030  *       documentation and/or other materials provided with the distribution.
00031  *     * Neither the name of the copyright holder nor the names of its
00032  *       contributors may be used to endorse or promote products derived from
00033  *       this software without specific prior written permission.
00034  *
00035  * This program is free software: you can redistribute it and/or modify
00036  * it under the terms of the GNU Lesser General Public License LGPL as
00037  * published by the Free Software Foundation, either version 3 of the
00038  * License, or (at your option) any later version.
00039  *
00040  * This program is distributed in the hope that it will be useful,
00041  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00042  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00043  * GNU Lesser General Public License LGPL for more details.
00044  *
00045  * You should have received a copy of the GNU Lesser General Public
00046  * License LGPL along with this program.
00047  * If not, see <http://www.gnu.org/licenses/>.
00048  *
00049  ****************************************************************/
00050 
00051 // general includes
00052 #include <unistd.h>
00053 
00054 #include <cob_forcetorque/ForceTorqueCtrl.h>
00055 
00056 // Headrs provided by cob-packages
00057 //#include <cob_generic_can/CanESD.h>
00058 //#include <cob_generic_can/CanPeakSys.h>
00059 #include <cob_generic_can/CanPeakSysUSB.h>
00060 #include <cob_generic_can/SocketCan.h>
00061 
00062 ForceTorqueCtrl::ForceTorqueCtrl()
00063 {
00064   m_pCanCtrl = NULL;
00065 
00066   // for types and baudrates see:
00067   // https://github.com/ipa320/cob_robots/blob/hydro_dev/cob_hardware_config/raw3-5/config/base/CanCtrl.ini
00068   m_CanType = CANITFTYPE_CAN_PEAK_USB;
00069   m_CanDevice = "/dev/pcan32";
00070   m_CanBaudrate = CANITFBAUD_250K;
00071   m_CanBaseIdentifier = 0x20 << 4;
00072 }
00073 
00074 ForceTorqueCtrl::ForceTorqueCtrl(int can_type, std::string can_path, int can_baudrate, int base_identifier)
00075 {
00076   m_pCanCtrl = NULL;
00077 
00078   // for types and baudrates see:
00079   // https://github.com/ipa320/cob_robots/blob/hydro_dev/cob_hardware_config/raw3-5/config/base/CanCtrl.ini
00080   m_CanType = can_type;
00081   m_CanDevice = can_path;
00082   m_CanBaudrate = can_baudrate;
00083   m_CanBaseIdentifier = base_identifier << 4;
00084 }
00085 
00086 ForceTorqueCtrl::~ForceTorqueCtrl()
00087 {
00088   if (m_pCanCtrl != NULL)
00089   {
00090     delete m_pCanCtrl;
00091   }
00092 }
00093 
00094 bool ForceTorqueCtrl::Init()
00095 {
00096   bool ret = true;
00097 
00098   if (initCan())
00099   {
00100     // This is way of testig if communication is also successful
00101     if (!ReadFTSerialNumber())
00102     {
00103       std::cout << "Can not read Serial Number from FTS!" << std::endl;
00104       ret = false;
00105     }
00106     if (!ReadFirmwareVersion())
00107     {
00108       std::cout << "Can not read Firmware version from FTS!" << std::endl;
00109       ret = false;
00110     }
00111     if (!ReadCountsPerUnit())
00112     {
00113       std::cout << "Can not read Counts Per Unit from FTS!" << std::endl;
00114       ret = false;
00115     }
00116     if (!ReadUnitCodes())
00117     {
00118       std::cout << "Can not read Unit Codes from FTS!" << std::endl;
00119       ret = false;
00120     }
00121     // Add return values and checking
00122     SetActiveCalibrationMatrix(0);
00123     ReadCalibrationMatrix();
00124   }
00125   else
00126   {
00127     std::cout << "CAN initialisation unsuccessful!" << std::endl;
00128     ret = false;
00129   }
00130 
00131   return ret;
00132 }
00133 
00134 bool ForceTorqueCtrl::initCan()
00135 {
00136   bool ret = true;
00137 
00138   // current implementation only for CanPeakSysUSB and SocketCan
00139   switch (m_CanType)
00140   {
00141     case CANITFTYPE_CAN_PEAK_USB:
00142       m_pCanCtrl = new CANPeakSysUSB(m_CanDevice.c_str(), m_CanBaudrate);
00143       ret = m_pCanCtrl->init_ret();
00144       break;
00145 
00146     case CANITFTYPE_SOCKET_CAN:
00147       m_pCanCtrl = new SocketCan(m_CanDevice.c_str());
00148       ret = m_pCanCtrl->init_ret();
00149       break;
00150   }
00151   return ret;
00152 }
00153 
00154 bool ForceTorqueCtrl::ReadFTSerialNumber()
00155 {
00156 #if DEBUG
00157   std::cout << "\n\n*********FTSerialNumber**********" << std::endl;
00158 #endif
00159   bool ret = true;
00160   CanMsg CMsg;
00161   CMsg.setID(m_CanBaseIdentifier | READ_SERIALNR);
00162   CMsg.setLength(0);
00163   ret = m_pCanCtrl->transmitMsg(CMsg, true);
00164   if (ret)
00165   {
00166     CanMsg replyMsg;
00167     replyMsg.set(0, 0, 0, 0, 0, 0, 0, 0);
00168     ret = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
00169 
00170     if (ret)
00171     {
00172 #if DEBUG
00173       std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
00174       std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
00175 #endif
00176       if (replyMsg.getID() == (m_CanBaseIdentifier | READ_SERIALNR))
00177       {
00178 #if DEBUG
00179         std::cout << "Reading Serial Number Succeeded!" << std::endl;
00180         std::cout << "reply Data: \t" << (char)replyMsg.getAt(0) << " " << (char)replyMsg.getAt(1) << " "
00181                   << (char)replyMsg.getAt(2) << " " << (char)replyMsg.getAt(3) << " " << (char)replyMsg.getAt(4) << " "
00182                   << (char)replyMsg.getAt(5) << " " << (char)replyMsg.getAt(6) << " " << (char)replyMsg.getAt(7)
00183                   << std::endl;
00184 #endif
00185       }
00186       else
00187       {
00188 #if DEBUG
00189         std::cout << "Error: Received wrong opcode!" << std::endl;
00190 #endif
00191         ret = false;
00192       }
00193     }
00194     else
00195     {
00196 #if DEBUG
00197       std::cout << "ForceTorqueCtrl::ReadFTSerialNumber(): Can not read message!" << std::endl;
00198 #endif
00199     }
00200   }
00201   else
00202   {
00203 #if DEBUG
00204     std::cout << "ForceTorqueCtrl::ReadFTSerialNumber(): Can not transmit message!" << std::endl;
00205 #endif
00206   }
00207 
00208   return ret;
00209 }
00210 
00211 bool ForceTorqueCtrl::ReadCountsPerUnit()
00212 {
00213 #if DEBUG
00214   std::cout << "\n\n*********Read Counts Per Unit**********" << std::endl;
00215 #endif
00216   bool ret = true;
00217   float countsPerForce = 0, countsPerTorque = 0;
00218   CanMsg CMsg;
00219   CMsg.setID(m_CanBaseIdentifier | READ_COUNTSPERU);
00220   CMsg.setLength(0);
00221 
00222   ret = m_pCanCtrl->transmitMsg(CMsg, true);
00223 
00224   if (ret)
00225   {
00226     CanMsg replyMsg;
00227     ret = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
00228 
00229     if (ret)
00230     {
00231 #if DEBUG
00232       std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
00233       std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
00234 #endif
00235       if (replyMsg.getID() == (m_CanBaseIdentifier | READ_COUNTSPERU))
00236       {
00237 #if DEBUG
00238         std::cout << "Reading Counts Per Unit Succeeded!" << std::endl;
00239         std::cout << "reply Data: \t" << (char)replyMsg.getAt(0) << " " << (char)replyMsg.getAt(1) << " "
00240                   << (char)replyMsg.getAt(2) << " " << (char)replyMsg.getAt(3) << " " << (char)replyMsg.getAt(4) << " "
00241                   << (char)replyMsg.getAt(5) << " " << (char)replyMsg.getAt(6) << " " << (char)replyMsg.getAt(7)
00242                   << std::endl;
00243 #endif
00244       }
00245       else
00246       {
00247 #if DEBUG
00248         std::cout << "Error: Received wrong opcode!" << std::endl;
00249 #endif
00250         ret = false;
00251       }
00252 
00253       intbBuf.bytes[0] = replyMsg.getAt(3);
00254       intbBuf.bytes[1] = replyMsg.getAt(2);
00255       intbBuf.bytes[2] = replyMsg.getAt(1);
00256       intbBuf.bytes[3] = replyMsg.getAt(0);
00257       countsPerForce = intbBuf.value;
00258 
00259       intbBuf.bytes[0] = replyMsg.getAt(7);
00260       intbBuf.bytes[1] = replyMsg.getAt(6);
00261       intbBuf.bytes[2] = replyMsg.getAt(5);
00262       intbBuf.bytes[3] = replyMsg.getAt(4);
00263       countsPerTorque = intbBuf.value;
00264     }
00265     else
00266     {
00267       std::cout << "ForceTorqueCtrl::ReadCountsPerUnit(): Can not read message!" << std::endl;
00268     }
00269   }
00270   else
00271   {
00272     std::cout << "ForceTorqueCtrl::ReadCountsPerUnit(): Can not transmit message!" << std::endl;
00273   }
00274 #if DEBUG
00275   std::cout << "CountsPerforce: " << countsPerForce << "  CountsPerTorque: " << countsPerTorque << std::endl;
00276 #endif
00277   return ret;
00278 }
00279 
00280 bool ForceTorqueCtrl::ReadUnitCodes()
00281 {
00282 #if DEBUG
00283   std::cout << "\n\n*********Read Unit Codes**********" << std::endl;
00284 #endif
00285   bool ret = true;
00286   CanMsg CMsg;
00287   CMsg.setID(m_CanBaseIdentifier | READ_UNITCODE);
00288   CMsg.setLength(0);
00289 
00290   ret = m_pCanCtrl->transmitMsg(CMsg, true);
00291 
00292   if (ret)
00293   {
00294     CanMsg replyMsg;
00295     ret = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
00296 
00297     if (ret)
00298     {
00299 #if DEBUG
00300       std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
00301       std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
00302 #endif
00303       if (replyMsg.getID() == (m_CanBaseIdentifier | READ_UNITCODE))
00304       {
00305 #if DEBUG
00306         std::cout << "Reading Unit codes Succeed!" << std::endl;
00307         std::cout << "reply Data: \t" << replyMsg.getAt(0) << " " << replyMsg.getAt(1) << std::endl;
00308 #endif
00309       }
00310     }
00311     else
00312     {
00313       std::cout << "ForceTorqueCtrl::ReadUnitCodes(): Can not read message!" << std::endl;
00314     }
00315   }
00316   else
00317   {
00318     std::cout << "ForceTorqueCtrl::ReadUnitCodes(): Can not transmit message!" << std::endl;
00319   }
00320 
00321   return ret;
00322 }
00323 
00324 bool ForceTorqueCtrl::ReadDiagnosticADCVoltages(int index, short int &value)
00325 {
00326   // TODO: Check for Init
00327 #if DEBUG
00328   std::cout << "\n\n*******Read Diagnostic ADC Voltages on index: " << index << "********" << std::endl;
00329 #endif
00330   bool ret = true;
00331   CanMsg CMsg;
00332   CMsg.setID(m_CanBaseIdentifier | READ_DIAGNOV);
00333   CMsg.setLength(1);
00334   // TODO: offset or typecast index
00335   CMsg.setAt(index, 0);
00336 
00337   ret = m_pCanCtrl->transmitMsg(CMsg, true);
00338 
00339   if (ret)
00340   {
00341     CanMsg replyMsg;
00342     ret = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
00343     if (ret)
00344     {
00345 #if DEBUG
00346       std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
00347       std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
00348 #endif
00349       if (replyMsg.getID() == (m_CanBaseIdentifier | READ_DIAGNOV))
00350       {
00351 #if DEBUG
00352         std::cout << "Reading Diagnostic ADC Voltage succeed!" << std::endl;
00353         std::cout << "ADC Voltage of diagnostic value " << index << " : " << replyMsg.getAt(0) << " "
00354                   << replyMsg.getAt(1) << std::endl;
00355 #endif
00356 
00357         ibBuf.bytes[0] = replyMsg.getAt(1);
00358         ibBuf.bytes[1] = replyMsg.getAt(0);
00359         value = ibBuf.value;
00360       }
00361       else
00362         std::cout << "Error: Received wrong opcode!" << std::endl;
00363     }
00364     else
00365       std::cout << "Error: Receiving Message failed!" << std::endl;
00366   }
00367   else
00368   {
00369     std::cout << "ForceTorqueCtrl::ReadDiagnosticADCVoltages(byte index): Can not transmit message!" << std::endl;
00370   }
00371 
00372   return ret;
00373 }
00374 
00375 bool ForceTorqueCtrl::SetActiveCalibrationMatrix(int num)
00376 {
00377 #if DEBUG
00378   std::cout << "\n\n*******Setting Active Calibration Matrix Num to: " << num << "********" << std::endl;
00379 #endif
00380   bool ret = true;
00381   CanMsg CMsg;
00382   CMsg.setID(m_CanBaseIdentifier | SET_CALIB);
00383   CMsg.setLength(1);
00384   CMsg.setAt(num, 0);
00385 
00386   ret = m_pCanCtrl->transmitMsg(CMsg, true);
00387 
00388   if (ret)
00389   {
00390     CanMsg replyMsg;
00391     ret = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
00392     if (ret)
00393     {
00394 #if DEBUG
00395       std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
00396       std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
00397 #endif
00398       if (replyMsg.getID() == (m_CanBaseIdentifier | SET_CALIB))
00399       {
00400 #if DEBUG
00401         std::cout << "Setting Calibration Matrix succeed!" << std::endl;
00402         std::cout << "Calibration Matrix: " << replyMsg.getAt(0) << " is Activ!" << std::endl;
00403 #endif
00404       }
00405       else
00406       {
00407 #if DEBUG
00408         std::cout << "Error: Received wrong opcode!" << std::endl;
00409 #endif
00410         ret = false;
00411       }
00412     }
00413     else
00414     {
00415       std::cout << "Error: Receiving Message failed!" << std::endl;
00416       ret = false;
00417     }
00418   }
00419   else
00420   {
00421     std::cout << "ForceTorqueCtrl::SetActiveCalibrationMatrix(int num): Can not transmit message!" << std::endl;
00422     ret = false;
00423   }
00424 
00425   return ret;
00426 }
00427 
00428 bool ForceTorqueCtrl::SetBaudRate(int value)
00429 {
00430 #if DEBUG
00431   std::cout << "\n\n*******Setting Baud Rate value to: " << value << "********" << std::endl;
00432 #endif
00433   bool ret = true;
00434   CanMsg CMsg;
00435   CMsg.setID(m_CanBaseIdentifier | SET_BAUD);
00436   CMsg.setLength(1);
00437   CMsg.setAt(value, 0);
00438 
00439   ret = m_pCanCtrl->transmitMsg(CMsg, true);
00440 
00441   if (ret)
00442   {
00443     CanMsg replyMsg;
00444     ret = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
00445     if (ret)
00446     {
00447 #if DEBUG
00448       std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
00449       std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
00450 #endif
00451       if (replyMsg.getID() == (m_CanBaseIdentifier | SET_BAUD))
00452       {
00453 #if DEBUG
00454         std::cout << "Send Baud Rate value: " << CMsg.getAt(0) << "!" << std::endl;
00455         std::cout << "Setting Baud Rate succeed!" << std::endl;
00456 #endif
00457       }
00458       else
00459       {
00460         std::cout << "Error: Received wrong opcode!" << std::endl;
00461         ret = false;
00462       }
00463     }
00464     else
00465     {
00466       std::cout << "Error: Receiving Message failed!" << std::endl;
00467       ret = false;
00468     }
00469   }
00470   else
00471   {
00472     std::cout << "ForceTorqueCtrl::SetBaudRate(int value): Can not transmit message!" << std::endl;
00473     ret = false;
00474   }
00475 
00476   return ret;
00477 }
00478 
00479 bool ForceTorqueCtrl::Reset()
00480 {
00481   std::cout << "\n\n******* Reseting the NETCANOEM ********" << std::endl;
00482   bool ret = true;
00483   CanMsg CMsg;
00484   CMsg.setID(m_CanBaseIdentifier | RESET);
00485   CMsg.setLength(0);
00486 
00487   ret = m_pCanCtrl->transmitMsg(CMsg, true);
00488 
00489   if (!ret)
00490   {
00491     std::cout << "ForceTorqueCtrl::Reset(): Can not transmit message!" << std::endl;
00492     ret = false;
00493   }
00494 
00495   usleep(10000);
00496 
00497   return ret;
00498 }
00499 
00500 bool ForceTorqueCtrl::SetBaseIdentifier(int identifier)
00501 {
00502 #if DEBUG
00503   std::cout << "\n\n*******Setting Base Identifier value to HEX : " << std::hex << identifier << " ********"
00504             << std::endl;
00505 #endif
00506   bool ret = true;
00507   CanMsg CMsg;
00508   CMsg.setID(m_CanBaseIdentifier | SET_BASEID);
00509   CMsg.setLength(1);
00510   CMsg.setAt(identifier, 0);
00511 
00512   ret = m_pCanCtrl->transmitMsg(CMsg, true);
00513 
00514   if (ret)
00515   {
00516     CanMsg replyMsg;
00517     ret = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
00518     if (ret)
00519     {
00520 #if DEBUG
00521       std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
00522       std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
00523 #endif
00524       if (replyMsg.getID() == (m_CanBaseIdentifier | SET_BASEID))
00525       {
00526 #if DEBUG
00527         std::cout << "Setting Base Identifier succeed!" << std::endl;
00528         std::cout << "Send Base Identifier value: " << std::hex << CMsg.getAt(0) << "!" << std::endl;
00529 #endif
00530         ret = true;
00531       }
00532       else
00533       {
00534         std::cout << "Error: Received wrong opcode!" << std::endl;
00535         ret = false;
00536       }
00537     }
00538     else
00539     {
00540       std::cout << "Error: Receiving Message failed!" << std::endl;
00541       ret = false;
00542     }
00543   }
00544   else
00545   {
00546     std::cout << "ForceTorqueCtrl::SetBaseIdentifier(int identifier): Can not transmit message!" << std::endl;
00547     ret = false;
00548   }
00549 
00550   return ret;
00551 }
00552 
00553 void ForceTorqueCtrl::ReadCalibrationMatrix()
00554 {
00555   Eigen::VectorXf vCoef(6);
00556 
00557   // Read Fx coefficients
00558   ReadMatrix(0, vCoef);
00559   m_v3FXGain = vCoef;
00560 
00561   // Read Fy coefficients
00562   ReadMatrix(1, vCoef);
00563   m_v3FYGain = vCoef;
00564 
00565   // Read Fz coefficients
00566   ReadMatrix(2, vCoef);
00567   m_v3FZGain = vCoef;
00568 
00569   // Read Tx coefficients
00570   ReadMatrix(3, vCoef);
00571   m_v3TXGain = vCoef;
00572 
00573   // Read Ty coefficients
00574   ReadMatrix(4, vCoef);
00575   m_v3TYGain = vCoef;
00576 
00577   // Read Tz coefficients
00578   ReadMatrix(5, vCoef);
00579   m_v3TZGain = vCoef;
00580   SetCalibMatrix();
00581 }
00582 
00583 void ForceTorqueCtrl::ReadMatrix(int axis, Eigen::VectorXf &vec)
00584 {
00585 #if DEBUG
00586   std::cout << "\n\n*******Read Matrix**********" << std::endl;
00587 #endif
00588   float statusCode = 0, sg0 = 0.0, sg1 = 0.0, sg2 = 0.0, sg3 = 0.0, sg4 = 0.0, sg5 = 0.0;
00589 
00590   CanMsg CMsg;
00591   CMsg.setID(m_CanBaseIdentifier | READ_MATRIX);
00592   CMsg.setLength(1);
00593   CMsg.setAt(axis, 0);
00594 
00595   bool ret = m_pCanCtrl->transmitMsg(CMsg, true);
00596   if (!ret)
00597   {
00598     std::cout << "Error: Requesting Calibration Matrix!" << std::endl;
00599     return;
00600   }
00601 
00602   CanMsg replyMsg;
00603   bool ret2 = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
00604   if (ret2)
00605   {
00606 #if DEBUG
00607     std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
00608     std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
00609 #endif
00610     if (replyMsg.getID() == (m_CanBaseIdentifier | READ_MATRIX))
00611     {
00612 #if DEBUG
00613       std::cout << "Reading Matrix Succeeded!" << std::endl;
00614       std::cout << "reply Data: \t" << replyMsg.getAt(0) << " " << replyMsg.getAt(1) << " " << replyMsg.getAt(2) << " "
00615                 << replyMsg.getAt(3) << " " << replyMsg.getAt(4) << " " << replyMsg.getAt(5) << " " << replyMsg.getAt(6)
00616                 << " " << replyMsg.getAt(7) << std::endl;
00617 #endif
00618     }
00619     else
00620     {
00621       std::cout << "Error: Received wrong opcode!" << std::endl;
00622       ret = false;
00623     }
00624 
00625     fbBuf.bytes[0] = replyMsg.getAt(3);
00626     fbBuf.bytes[1] = replyMsg.getAt(2);
00627     fbBuf.bytes[2] = replyMsg.getAt(1);
00628     fbBuf.bytes[3] = replyMsg.getAt(0);
00629     sg0 = fbBuf.value;
00630 
00631     fbBuf.bytes[0] = replyMsg.getAt(7);
00632     fbBuf.bytes[1] = replyMsg.getAt(6);
00633     fbBuf.bytes[2] = replyMsg.getAt(5);
00634     fbBuf.bytes[3] = replyMsg.getAt(4);
00635     sg1 = fbBuf.value;
00636   }
00637   else
00638     return;
00639 
00640   ret2 = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
00641   if (ret2)
00642   {
00643 #if DEBUG
00644     std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
00645     std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
00646     std::cout << "reply Data: \t" << replyMsg.getAt(0) << " " << replyMsg.getAt(1) << " " << replyMsg.getAt(2) << " "
00647               << replyMsg.getAt(3) << " " << replyMsg.getAt(4) << " " << replyMsg.getAt(5) << " " << replyMsg.getAt(6)
00648               << " " << replyMsg.getAt(7) << std::endl;
00649 #endif
00650 
00651     fbBuf.bytes[0] = replyMsg.getAt(3);
00652     fbBuf.bytes[1] = replyMsg.getAt(2);
00653     fbBuf.bytes[2] = replyMsg.getAt(1);
00654     fbBuf.bytes[3] = replyMsg.getAt(0);
00655     sg2 = fbBuf.value;
00656 
00657     fbBuf.bytes[0] = replyMsg.getAt(7);
00658     fbBuf.bytes[1] = replyMsg.getAt(6);
00659     fbBuf.bytes[2] = replyMsg.getAt(5);
00660     fbBuf.bytes[3] = replyMsg.getAt(4);
00661     sg3 = fbBuf.value;
00662   }
00663   else
00664     return;
00665 
00666   ret2 = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
00667   if (ret2)
00668   {
00669 #if DEBUG
00670     std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
00671     std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
00672     std::cout << "reply Data: \t" << replyMsg.getAt(0) << " " << replyMsg.getAt(1) << " " << replyMsg.getAt(2) << " "
00673               << replyMsg.getAt(3) << " " << replyMsg.getAt(4) << " " << replyMsg.getAt(5) << " " << replyMsg.getAt(6)
00674               << " " << replyMsg.getAt(7) << std::endl;
00675 #endif
00676 
00677     fbBuf.bytes[0] = replyMsg.getAt(3);
00678     fbBuf.bytes[1] = replyMsg.getAt(2);
00679     fbBuf.bytes[2] = replyMsg.getAt(1);
00680     fbBuf.bytes[3] = replyMsg.getAt(0);
00681     sg4 = fbBuf.value;
00682 
00683     fbBuf.bytes[0] = replyMsg.getAt(7);
00684     fbBuf.bytes[1] = replyMsg.getAt(6);
00685     fbBuf.bytes[2] = replyMsg.getAt(5);
00686     fbBuf.bytes[3] = replyMsg.getAt(4);
00687     sg5 = fbBuf.value;
00688   }
00689   else
00690     return;
00691 
00692   vec[0] = sg0;
00693   vec[1] = sg1;
00694   vec[2] = sg2;
00695   vec[3] = sg3;
00696   vec[4] = sg4;
00697   vec[5] = sg5;
00698 #if DEBUG
00699   std::cout << "Matix:  SG0: " << sg0 << " SG1: " << sg1 << " SG2: " << sg2 << " SG3: " << sg3 << " SG4: " << sg4
00700             << " SG5: " << sg5 << std::endl;
00701 #endif
00702 }
00703 
00704 bool ForceTorqueCtrl::ReadFirmwareVersion()
00705 {
00706 #if DEBUG
00707   std::cout << "\n\n*******Reading Firmware Version*******" << std::endl;
00708 #endif
00709   bool ret = true;
00710   CanMsg CMsg;
00711   CMsg.setID(m_CanBaseIdentifier | READ_FIRMWARE);
00712   CMsg.setLength(0);
00713 
00714   ret = m_pCanCtrl->transmitMsg(CMsg, true);
00715 
00716   if (ret)
00717   {
00718     CanMsg replyMsg;
00719     ret = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
00720     if (ret)
00721     {
00722 #if DEBUG
00723       std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
00724       std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
00725 #endif
00726       if (replyMsg.getID() == (m_CanBaseIdentifier | READ_FIRMWARE))
00727       {
00728 #if DEBUG
00729         std::cout << "Reading Firmware Succeed!" << std::endl;
00730         std::cout << "reply Data: \t" << replyMsg.getAt(0) << " " << replyMsg.getAt(1) << " " << replyMsg.getAt(2)
00731                   << " " << replyMsg.getAt(3) << std::endl;
00732 #endif
00733       }
00734       else
00735       {
00736         std::cout << "Error: Received wrong opcode!" << std::endl;
00737         ret = false;
00738       }
00739     }
00740     else
00741     {
00742       std::cout << "Error: Receiving Message failed!" << std::endl;
00743       ret = false;
00744     }
00745   }
00746   else
00747   {
00748     std::cout << "Error: Transmiting Message failed!" << std::endl;
00749     ret = false;
00750   }
00751 
00752   return ret;
00753 }
00754 
00755 bool ForceTorqueCtrl::ReadSGData(int statusCode, double &Fx, double &Fy, double &Fz, double &Tx, double &Ty, double &Tz)
00756 {
00757   int sg0 = 0, sg1 = 0, sg2 = 0, sg3 = 0, sg4 = 0, sg5 = 0;
00758 
00759   CanMsg CMsg;
00760   CMsg.setID(m_CanBaseIdentifier | READ_SG);
00761   CMsg.setLength(0);
00762   bool ret = m_pCanCtrl->transmitMsg(CMsg, true);
00763 
00764   if (!ret)
00765   {
00766     std::cout << "ForceTorqueCtrl::ReadSGData: Error: Transmiting message failed!" << std::endl;
00767     return false;
00768   }
00769 
00770   CanMsg replyMsg;
00771   bool ret2 = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
00772   unsigned char c[2];
00773   if (ret2)
00774   {
00775     if (replyMsg.getID() != (m_CanBaseIdentifier | 0x0))
00776     {
00777       std::cout << "ForceTorqueCtrl::ReadSGData: Error: Received wrong opcode (Should be 0x200)!" << std::endl;
00778       std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
00779       std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
00780       std::cout << "reply Data: \t" << replyMsg.getAt(0) << " " << replyMsg.getAt(1) << " " << replyMsg.getAt(2) << " "
00781                 << replyMsg.getAt(3) << " " << replyMsg.getAt(4) << " " << replyMsg.getAt(5) << " " << replyMsg.getAt(6)
00782                 << " " << replyMsg.getAt(7) << std::endl;
00783       return false ;
00784     }
00785 
00786     c[0] = replyMsg.getAt(0);  // status code
00787     c[1] = replyMsg.getAt(1);
00788     statusCode = (short)((c[0] << 8) | c[1]);
00789 
00790     if (statusCode != 0)
00791     {
00792       if (statusCode & 0x4000)
00793       {
00794         std::cout << "ForceTorqueCtrl::ReadSGData: CAN bus error detected!" << std::endl;
00795         Reset();
00796         std::cout << "ForceTorqueCtrl::ReadSGData: FTS reseted!" << std::endl;
00797       }
00798       else
00799       {
00800         std::cout << "ForceTorqueCtrl::ReadSGData: Error: Something is wrong with sensor!" << std::endl;
00801         std::cout << std::hex << statusCode << std::endl;
00802       }
00803     }
00804 
00805     c[0] = replyMsg.getAt(2);  // sg0
00806     c[1] = replyMsg.getAt(3);
00807     sg0 = (short)((c[0] << 8) | c[1]);
00808 
00809     c[0] = replyMsg.getAt(4);  // sg2
00810     c[1] = replyMsg.getAt(5);
00811     sg2 = (short)((c[0] << 8) | c[1]);
00812 
00813     c[0] = replyMsg.getAt(6);  // sg4
00814     c[1] = replyMsg.getAt(7);
00815     sg4 = (short)((c[0] << 8) | c[1]);
00816   }
00817   else
00818     return false;
00819 
00820   ret2 = m_pCanCtrl->receiveMsgRetry(&replyMsg, 10);
00821   if (ret2)
00822   {
00823     if (replyMsg.getID() != (m_CanBaseIdentifier | 0x1))
00824     {
00825       std::cout << "ForceTorqueCtrl::ReadSGData: Error: Received wrong opcode (Should be 0x201)!" << std::endl;
00826       std::cout << "reply ID: \t" << std::hex << replyMsg.getID() << std::endl;
00827       std::cout << "reply Length: \t" << replyMsg.getLength() << std::endl;
00828       std::cout << "reply Data: \t" << replyMsg.getAt(0) << " " << replyMsg.getAt(1) << " " << replyMsg.getAt(2) << " "
00829                 << replyMsg.getAt(3) << " " << replyMsg.getAt(4) << " " << replyMsg.getAt(5) << " " << replyMsg.getAt(6)
00830                 << " " << replyMsg.getAt(7) << std::endl;
00831       return false ;
00832     }
00833 
00834     c[0] = replyMsg.getAt(0);  // sg1
00835     c[1] = replyMsg.getAt(1);
00836     sg1 = (short)((c[0] << 8) | c[1]);
00837 
00838     c[0] = replyMsg.getAt(2);  // sg3
00839     c[1] = replyMsg.getAt(3);
00840     sg3 = (short)((c[0] << 8) | c[1]);
00841 
00842     c[0] = replyMsg.getAt(4);  // sg5
00843     c[1] = replyMsg.getAt(5);
00844     sg5 = (short)((c[0] << 8) | c[1]);
00845   }
00846   else
00847     return false;
00848 
00849   StrainGaugeToForce(sg0, sg1, sg2, sg3, sg4, sg5);
00850   Fx = m_vForceData(0);
00851   Fy = m_vForceData(1);
00852   Fz = m_vForceData(2);
00853   Tx = m_vForceData(3);
00854   Ty = m_vForceData(4);
00855   Tz = m_vForceData(5);
00856   return true;
00857 }
00858 
00859 void ForceTorqueCtrl::StrainGaugeToForce(int &sg0, int &sg1, int &sg2, int &sg3, int &sg4, int &sg5)
00860 {
00861   Eigen::VectorXf v6SG(6);
00862   Eigen::VectorXf v6tmp(6);
00863   Eigen::VectorXf test(6);
00864 
00865   v6SG[0] = sg0;
00866   v6SG[1] = sg1;
00867   v6SG[2] = sg2;
00868   v6SG[3] = sg3;
00869   v6SG[4] = sg4;
00870   v6SG[5] = sg5;
00871   test = m_mXCalibMatrix * v6SG;
00872   m_vForceData = test * 0.000001;
00873 }
00874 
00875 void ForceTorqueCtrl::SetGaugeOffset(float sg0Off, float sg1Off, float sg2Off, float sg3Off, float sg4Off, float sg5Off)
00876 {
00877   Eigen::VectorXf tmp(6);
00878   tmp[0] = sg0Off;
00879   tmp[1] = sg1Off;
00880   tmp[2] = sg2Off;
00881   tmp[3] = sg3Off;
00882   tmp[4] = sg4Off;
00883   tmp[5] = sg5Off;
00884   m_v3StrainGaigeOffset = tmp;
00885 }
00886 void ForceTorqueCtrl::SetGaugeGain(float gg0, float gg1, float gg2, float gg3, float gg4, float gg5)
00887 {
00888   Eigen::VectorXf tmp(6);
00889   tmp[0] = gg0;
00890   tmp[1] = gg1;
00891   tmp[2] = gg2;
00892   tmp[3] = gg3;
00893   tmp[4] = gg4;
00894   tmp[5] = gg5;
00895   m_v3GaugeGain = tmp;
00896   // std::cout<<"GaugeGain: \n"<<m_v3GaugeGain<<"\n\n";
00897 }
00898 
00899 void ForceTorqueCtrl::SetFXGain(float fxg0, float fxg1, float fxg2, float fxg3, float fxg4, float fxg5)
00900 {
00901   Eigen::VectorXf tmp(6);
00902   tmp[0] = fxg0;
00903   tmp[1] = fxg1;
00904   tmp[2] = fxg2;
00905   tmp[3] = fxg3;
00906   tmp[4] = fxg4;
00907   tmp[5] = fxg5;
00908   m_v3FXGain = tmp;
00909   // std::cout<<"FXGain: \n"<<m_v3FXGain<<"\n\n";
00910 }
00911 void ForceTorqueCtrl::SetFYGain(float fyg0, float fyg1, float fyg2, float fyg3, float fyg4, float fyg5)
00912 {
00913   Eigen::VectorXf tmp(6);
00914   tmp[0] = fyg0;
00915   tmp[1] = fyg1;
00916   tmp[2] = fyg2;
00917   tmp[3] = fyg3;
00918   tmp[4] = fyg4;
00919   tmp[5] = fyg5;
00920   m_v3FYGain = tmp;
00921   // std::cout<<"FYGain: \n"<<m_v3FYGain<<"\n\n";
00922 }
00923 void ForceTorqueCtrl::SetFZGain(float fzg0, float fzg1, float fzg2, float fzg3, float fzg4, float fzg5)
00924 {
00925   Eigen::VectorXf tmp(6);
00926   tmp[0] = fzg0;
00927   tmp[1] = fzg1;
00928   tmp[2] = fzg2;
00929   tmp[3] = fzg3;
00930   tmp[4] = fzg4;
00931   tmp[5] = fzg5;
00932   m_v3FZGain = tmp;
00933   // std::cout<<"FZGain: \n"<<m_v3FZGain<<"\n\n";
00934 }
00935 void ForceTorqueCtrl::SetTXGain(float txg0, float txg1, float txg2, float txg3, float txg4, float txg5)
00936 {
00937   Eigen::VectorXf tmp(6);
00938   tmp[0] = txg0;
00939   tmp[1] = txg1;
00940   tmp[2] = txg2;
00941   tmp[3] = txg3;
00942   tmp[4] = txg4;
00943   tmp[5] = txg5;
00944   m_v3TXGain = tmp;
00945   // std::cout<<"TXGain: \n"<<m_v3TXGain<<"\n\n";
00946 }
00947 void ForceTorqueCtrl::SetTYGain(float tyg0, float tyg1, float tyg2, float tyg3, float tyg4, float tyg5)
00948 {
00949   Eigen::VectorXf tmp(6);
00950   tmp[0] = tyg0;
00951   tmp[1] = tyg1;
00952   tmp[2] = tyg2;
00953   tmp[3] = tyg3;
00954   tmp[4] = tyg4;
00955   tmp[5] = tyg5;
00956   m_v3TYGain = tmp;
00957   // std::cout<<"TYGain: \n"<<m_v3TYGain<<"\n\n";
00958 }
00959 void ForceTorqueCtrl::SetTZGain(float tzg0, float tzg1, float tzg2, float tzg3, float tzg4, float tzg5)
00960 {
00961   Eigen::VectorXf tmp(6);
00962   tmp[0] = tzg0;
00963   tmp[1] = tzg1;
00964   tmp[2] = tzg2;
00965   tmp[3] = tzg3;
00966   tmp[4] = tzg4;
00967   tmp[5] = tzg5;
00968   m_v3TZGain = tmp;
00969   // std::cout<<"TZGain: \n"<<m_v3TZGain<<"\n\n";
00970 }
00971 
00972 void ForceTorqueCtrl::CalcCalibMatrix()
00973 {
00974   Eigen::MatrixXf tmp(6, 6);
00975   tmp(0) = m_v3FXGain[0] / m_v3GaugeGain[0];
00976   tmp(1) = m_v3FXGain[1] / m_v3GaugeGain[1];
00977   tmp(2) = m_v3FXGain[2] / m_v3GaugeGain[2];
00978   tmp(3) = m_v3FXGain[3] / m_v3GaugeGain[3];
00979   tmp(4) = m_v3FXGain[4] / m_v3GaugeGain[4];
00980   tmp(5) = m_v3FXGain[5] / m_v3GaugeGain[5];
00981 
00982   tmp(6) = m_v3FYGain[0] / m_v3GaugeGain[0];
00983   tmp(7) = m_v3FYGain[1] / m_v3GaugeGain[1];
00984   tmp(8) = m_v3FYGain[2] / m_v3GaugeGain[2];
00985   tmp(9) = m_v3FYGain[3] / m_v3GaugeGain[3];
00986   tmp(10) = m_v3FYGain[4] / m_v3GaugeGain[4];
00987   tmp(11) = m_v3FYGain[5] / m_v3GaugeGain[5];
00988 
00989   tmp(12) = m_v3FZGain[0] / m_v3GaugeGain[0];
00990   tmp(13) = m_v3FZGain[1] / m_v3GaugeGain[1];
00991   tmp(14) = m_v3FZGain[2] / m_v3GaugeGain[2];
00992   tmp(15) = m_v3FZGain[3] / m_v3GaugeGain[3];
00993   tmp(16) = m_v3FZGain[4] / m_v3GaugeGain[4];
00994   tmp(17) = m_v3FZGain[5] / m_v3GaugeGain[5];
00995 
00996   tmp(18) = m_v3TXGain[0] / m_v3GaugeGain[0];
00997   tmp(19) = m_v3TXGain[1] / m_v3GaugeGain[1];
00998   tmp(20) = m_v3TXGain[2] / m_v3GaugeGain[2];
00999   tmp(21) = m_v3TXGain[3] / m_v3GaugeGain[3];
01000   tmp(22) = m_v3TXGain[4] / m_v3GaugeGain[4];
01001   tmp(23) = m_v3TXGain[5] / m_v3GaugeGain[5];
01002 
01003   tmp(24) = m_v3TYGain[0] / m_v3GaugeGain[0];
01004   tmp(25) = m_v3TYGain[1] / m_v3GaugeGain[1];
01005   tmp(26) = m_v3TYGain[2] / m_v3GaugeGain[2];
01006   tmp(27) = m_v3TYGain[3] / m_v3GaugeGain[3];
01007   tmp(28) = m_v3TYGain[4] / m_v3GaugeGain[4];
01008   tmp(29) = m_v3TYGain[5] / m_v3GaugeGain[5];
01009 
01010   tmp(30) = m_v3TZGain[0] / m_v3GaugeGain[0];
01011   tmp(31) = m_v3TZGain[1] / m_v3GaugeGain[1];
01012   tmp(32) = m_v3TZGain[2] / m_v3GaugeGain[2];
01013   tmp(33) = m_v3TZGain[3] / m_v3GaugeGain[3];
01014   tmp(34) = m_v3TZGain[4] / m_v3GaugeGain[4];
01015   tmp(35) = m_v3TZGain[5] / m_v3GaugeGain[5];
01016 
01017   m_mXCalibMatrix = tmp;
01018 }
01019 
01020 void ForceTorqueCtrl::SetCalibMatrix()
01021 {
01022   Eigen::MatrixXf tmp(6, 6);
01023   tmp(0) = m_v3FXGain[0];
01024   tmp(1) = m_v3FXGain[1];
01025   tmp(2) = m_v3FXGain[2];
01026   tmp(3) = m_v3FXGain[3];
01027   tmp(4) = m_v3FXGain[4];
01028   tmp(5) = m_v3FXGain[5];
01029 
01030   tmp(6) = m_v3FYGain[0];
01031   tmp(7) = m_v3FYGain[1];
01032   tmp(8) = m_v3FYGain[2];
01033   tmp(9) = m_v3FYGain[3];
01034   tmp(10) = m_v3FYGain[4];
01035   tmp(11) = m_v3FYGain[5];
01036 
01037   tmp(12) = m_v3FZGain[0];
01038   tmp(13) = m_v3FZGain[1];
01039   tmp(14) = m_v3FZGain[2];
01040   tmp(15) = m_v3FZGain[3];
01041   tmp(16) = m_v3FZGain[4];
01042   tmp(17) = m_v3FZGain[5];
01043 
01044   tmp(18) = m_v3TXGain[0];
01045   tmp(19) = m_v3TXGain[1];
01046   tmp(20) = m_v3TXGain[2];
01047   tmp(21) = m_v3TXGain[3];
01048   tmp(22) = m_v3TXGain[4];
01049   tmp(23) = m_v3TXGain[5];
01050 
01051   tmp(24) = m_v3TYGain[0];
01052   tmp(25) = m_v3TYGain[1];
01053   tmp(26) = m_v3TYGain[2];
01054   tmp(27) = m_v3TYGain[3];
01055   tmp(28) = m_v3TYGain[4];
01056   tmp(29) = m_v3TYGain[5];
01057 
01058   tmp(30) = m_v3TZGain[0];
01059   tmp(31) = m_v3TZGain[1];
01060   tmp(32) = m_v3TZGain[2];
01061   tmp(33) = m_v3TZGain[3];
01062   tmp(34) = m_v3TZGain[4];
01063   tmp(35) = m_v3TZGain[5];
01064 
01065   m_mXCalibMatrix = tmp.transpose();
01066 }


ati_force_torque
Author(s): Denis Štogl, Alexander Bubeck
autogenerated on Thu Jun 6 2019 21:13:29