denso_robot.cpp
Go to the documentation of this file.
00001 
00025 #include "denso_robot_core/denso_robot.h"
00026 
00027 #define NAME_ARMGROUP "_armgroup"
00028 
00029 namespace denso_robot_core {
00030 
00031 DensoRobot::DensoRobot(DensoBase* parent,
00032     Service_Vec& service, Handle_Vec& handle,
00033     const std::string& name, const int* mode)
00034   : DensoBase(parent, service, handle, name, mode),
00035     m_ArmGroup(0)
00036 {
00037 
00038 }
00039 
00040 DensoRobot::~DensoRobot()
00041 {
00042 
00043 }
00044 
00045 HRESULT DensoRobot::InitializeBCAP(XMLElement *xmlElem)
00046 {
00047   return AddVariable(xmlElem);
00048 }
00049 
00050 HRESULT DensoRobot::StartService(ros::NodeHandle& node)
00051 {
00052   std::string tmpName = DensoBase::RosName();
00053 
00054   if(*m_mode == 0) {
00055     m_subArmGroup = node.subscribe<Int32>(
00056         tmpName + NAME_ARMGROUP, MESSAGE_QUEUE,
00057         &DensoRobot::Callback_ArmGroup, this);
00058   }
00059 
00060   DensoVariable_Vec::iterator itVar;
00061   for(itVar  = m_vecVar.begin();
00062       itVar != m_vecVar.end();
00063       itVar++)
00064   {
00065     (*itVar)->StartService(node);
00066   }
00067 
00068   m_serving = true;
00069 
00070   return S_OK;
00071 }
00072 
00073 HRESULT DensoRobot::StopService()
00074 {
00075   m_mtxSrv.lock();
00076   m_serving = false;
00077   m_mtxSrv.unlock();
00078 
00079   m_subArmGroup.shutdown();
00080 
00081   DensoVariable_Vec::iterator itVar;
00082   for(itVar  = m_vecVar.begin();
00083       itVar != m_vecVar.end();
00084       itVar++)
00085   {
00086     (*itVar)->StopService();
00087   }
00088 
00089   return S_OK;
00090 }
00091 
00092 bool DensoRobot::Update()
00093 {
00094   boost::mutex::scoped_lock lockSrv(m_mtxSrv);
00095   if(!m_serving) return false;
00096 
00097   DensoVariable_Vec::iterator itVar;
00098   for(itVar  = m_vecVar.begin();
00099       itVar != m_vecVar.end();
00100       itVar++)
00101   {
00102     (*itVar)->Update();
00103   }
00104 
00105   return true;
00106 }
00107 
00108 void DensoRobot::ChangeArmGroup(int number)
00109 {
00110   m_ArmGroup = number;
00111 }
00112 
00113 void DensoRobot::Callback_ArmGroup(const Int32::ConstPtr& msg)
00114 {
00115   ChangeArmGroup(msg->data);
00116 }
00117 
00118 HRESULT DensoRobot::get_Variable(const std::string& name, DensoVariable_Ptr* var)
00119 {
00120   if(var == NULL) {
00121     return E_INVALIDARG;
00122   }
00123 
00124   DensoBase_Vec vecBase;
00125   vecBase.insert(vecBase.end(), m_vecVar.begin(), m_vecVar.end());
00126 
00127   DensoBase_Ptr pBase;
00128   HRESULT hr = DensoBase::get_Object(vecBase, name, &pBase);
00129   if(SUCCEEDED(hr)) {
00130     *var = boost::dynamic_pointer_cast<DensoVariable>(pBase);
00131   }
00132 
00133   return hr;
00134 }
00135 
00136 HRESULT DensoRobot::AddVariable(const std::string& name)
00137 {
00138   return DensoBase::AddVariable(ID_ROBOT_GETVARIABLE,
00139       name, m_vecVar);
00140 }
00141 
00142 HRESULT DensoRobot::AddVariable(XMLElement *xmlElem)
00143 {
00144   HRESULT hr = S_OK;
00145   XMLElement *xmlVar;
00146 
00147   for(xmlVar = xmlElem->FirstChildElement(XML_VARIABLE_NAME);
00148       xmlVar!= NULL;
00149       xmlVar = xmlVar->NextSiblingElement(XML_VARIABLE_NAME))
00150   {
00151     hr = DensoBase::AddVariable(ID_ROBOT_GETVARIABLE,
00152         xmlVar, m_vecVar);
00153     if(FAILED(hr)) break;
00154   }
00155 
00156   return hr;
00157 }
00158 
00159 HRESULT DensoRobot::CreatePoseData(
00160     const PoseData &pose, VARIANT &vnt)
00161 {
00162   uint32_t i, j;
00163   uint32_t num = 3 +
00164     (((pose.exjoints.mode != 0) && (pose.exjoints.joints.size() > 0)) ? 1 : 0);
00165   float   *pfltval;
00166   VARIANT *pvntval;
00167 
00168   vnt.vt = (VT_ARRAY | VT_VARIANT);
00169   vnt.parray = SafeArrayCreateVector(VT_VARIANT, 0, num);
00170 
00171   SafeArrayAccessData(vnt.parray, (void**)&pvntval);
00172 
00173   for(i = 0; i < num; i++) {
00174     switch(i){
00175       case 0:
00176         pvntval[i].vt = (VT_ARRAY | VT_R4);
00177         pvntval[i].parray = SafeArrayCreateVector(VT_R4, 0, pose.value.size());
00178         SafeArrayAccessData(pvntval[i].parray, (void**)&pfltval);
00179         std::copy(pose.value.begin(), pose.value.end(), pfltval);
00180         SafeArrayUnaccessData(pvntval[i].parray);
00181         break;
00182       case 1:
00183         pvntval[i].vt = VT_I4;
00184         pvntval[i].lVal  = pose.type;
00185         break;
00186       case 2:
00187         pvntval[i].vt = VT_I4;
00188         pvntval[i].lVal  = pose.pass;
00189         break;
00190       case 3:
00191         CreateExJoints(pose.exjoints, pvntval[i]);
00192         break;
00193     }
00194   }
00195 
00196   SafeArrayUnaccessData(vnt.parray);
00197 
00198   return S_OK;
00199 }
00200 
00201 HRESULT DensoRobot::CreateExJoints(
00202     const ExJoints &exjoints, VARIANT &vnt)
00203 {
00204   uint32_t i, j;
00205   uint32_t num = 1 + exjoints.joints.size();
00206   VARIANT *pvntval, *pjntval;
00207 
00208   vnt.vt = (VT_ARRAY | VT_VARIANT);
00209   vnt.parray = SafeArrayCreateVector(VT_VARIANT, 0, num);
00210 
00211   SafeArrayAccessData(vnt.parray, (void**)&pvntval);
00212 
00213   for(i = 0; i < num; i++) {
00214     if(i == 0) {
00215       pvntval[0].vt   = VT_I4;
00216       pvntval[0].lVal = exjoints.mode;
00217     } else {
00218       pvntval[i].vt = (VT_ARRAY | VT_VARIANT);
00219       pvntval[i].parray = SafeArrayCreateVector(VT_VARIANT, 0, 2);
00220       SafeArrayAccessData(pvntval[i].parray, (void**)&pjntval);
00221       pjntval[0].vt     = VT_I4;
00222       pjntval[0].lVal   = exjoints.joints.at(i-1).joint;
00223       pjntval[1].vt     = VT_R4;
00224       pjntval[1].fltVal = exjoints.joints.at(i-1).value;
00225       SafeArrayUnaccessData(pvntval[i].parray);
00226     }
00227   }
00228 
00229   SafeArrayUnaccessData(vnt.parray);
00230 
00231   return S_OK;
00232 }
00233 
00234 }


denso_robot_core
Author(s): DENSO WAVE INCORPORATED
autogenerated on Thu Jun 6 2019 21:00:11