PowerCubeCtrl_OROCOS.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 
00018 #include "PowerCubeCtrl_OROCOS.h"
00019 
00020 using namespace RTT;
00021 
00022 PowerCubeCtrl_OROCOS::PowerCubeCtrl_OROCOS(std::string name, std::string xmlFile, int dof)
00023 : TaskContext(name),
00024         m_xmlFile(xmlFile),
00025         m_dof(dof),
00026         // initialize dataports:
00027         m_in_Angles("in_Angles"),
00028         m_in_Velocities("in_Velocities"),
00029         m_in_Currents("in_Currents"),
00030         m_out_Angles("out_Angles"),  // note: initial value
00031         m_out_Velocities("out_Velocities"),  // note: initial value
00032         // initialize method
00033         m_stop_method("stop_movement",&PowerCubeCtrl_OROCOS::stopArm, this),
00034         //m_moveJointSpace_method("moveJointSpace", &PowerCubeCtrl_OROCOS::moveJointSpace, this),
00035         m_CanDev_prop("CanDev_n", "Can Device No.", 0),
00036         m_CanBaud_prop("BaudRate", "Can Baud Rate", 1000),
00037         m_dof_prop("DOF", "Degrees of Freedom", 7),
00038         m_MaxVel_prop("Max_Vel", "Maximum Velocity", 0.5),
00039         m_MaxAcc_prop("Max_Acc", "Maximum Acceleration", 0.8),
00040         m_powercubectrl()
00041 {
00042     this->ports()->addPort( &m_in_Angles, "Port for desired Angles" );
00043     m_in_Angles_connected = false;
00044     this->ports()->addPort( &m_in_Velocities, "Port for desired velocities" );
00045     m_in_Velocities_connected = false;
00046     this->ports()->addPort( &m_in_Currents, "Port for desired currents" );
00047     m_in_Currents_connected = false;
00048 
00049     this->ports()->addPort( &m_out_Angles, "Port for current positions" );
00050     this->ports()->addPort( &m_out_Velocities, "Port for current velocities" );
00051 
00052     this->methods()->addMethod(&m_stop_method, "Stop the Arm");
00053     //this->methods()->addMethod(&m_moveJointSpace_method, "execute synchronized ramp commands");
00054 
00055         this->properties()->addProperty(&m_CanDev_prop);
00056         this->properties()->addProperty(&m_CanBaud_prop);
00057         this->properties()->addProperty(&m_dof_prop);
00058         this->properties()->addProperty(&m_MaxVel_prop);
00059         this->properties()->addProperty(&m_MaxAcc_prop);
00060 
00061         //m_mod_params.clear();
00062 
00063         m_modId_props.clear();
00064         m_ul_props.clear();
00065         m_ll_props.clear();
00066         m_offset_props.clear();
00067 
00068         for (int i=0; i<m_dof; i++)
00069         {
00070                 ostringstream os;
00071 
00072                 os << "modId" << i+1;
00073                 m_modId_props.push_back( Property<int>( os.str(), os.str(), 0 ) );
00074 
00075                 os.str("");     os.clear();
00076                 os << "upperLimit" << i+1;
00077                 m_ul_props.push_back( Property<double>( os.str(), os.str(), 0.0 ) );
00078 
00079                 os.str("");     os.clear();
00080                 os << "lowerLimit" << i+1;
00081                 m_ll_props.push_back( Property<double>( os.str(), os.str(), 0.0 ) );
00082 
00083                 os.str("");     os.clear();
00084                 os << "offset" << i+1;
00085                 m_offset_props.push_back( Property<double>( os.str(), os.str(), 0.0 ) );
00086 
00087         }
00088 
00089         m_mod_params.clear();
00090         for (int i=0; i<m_dof; i++)
00091         {
00092                 ostringstream os;
00093                 os << "modParams" << i+1;
00094 
00095                 PropertyBag bag( os.str() );
00096                 bag.add( &m_modId_props[i] );
00097                 bag.add( &m_ul_props[i] );
00098                 bag.add( &m_ll_props[i] );
00099                 bag.add( &m_offset_props[i] );
00100 
00101                 Property<PropertyBag>  prop_of_bag( os.str(), "", bag);
00102 
00103                 m_mod_params.push_back( prop_of_bag );
00104         }
00105 
00106         for (int i=0; i<m_dof; i++)
00107         {
00108                 this->properties()->addProperty( &(m_mod_params[i]) );
00109         }
00110     //m_stopped = false;
00111 }
00112 
00113 PowerCubeCtrl_OROCOS::~PowerCubeCtrl_OROCOS()
00114 {
00115         stop();
00116 }
00117 
00118 bool PowerCubeCtrl_OROCOS::configureHook()
00119 {
00120         this->marshalling()->readProperties(m_xmlFile);
00121 
00122         // COPY PROPERTIES INTO STRUCT POWERCUBEPARAMETERS
00123 
00124         PowerCubeParameters pcparams;
00125 
00126         pcparams.modIds.resize(m_dof);
00127         pcparams.upperlimits.resize(m_dof);
00128         pcparams.lowerlimits.resize(m_dof);
00129         pcparams.offsets.resize(m_dof);
00130 
00131         pcparams.dof=m_dof_prop.get();
00132         pcparams.CanDevice=m_CanDev_prop.get();
00133         pcparams.BaudRate=m_CanBaud_prop.get();
00134         pcparams.maxVel=m_MaxVel_prop.get();
00135         pcparams.maxAcc=m_MaxAcc_prop.get();
00136 
00137         int i;
00138         if(m_dof != pcparams.dof)
00139         {
00140                 log(Info) << "Degrees of freedom" << m_dof << "doesn't match with xml input DOF." << pcparams.dof << endlog();
00141             return false;
00142         }
00143 
00144         for (i=0 ; i< (m_dof) ; i++)
00145         {
00146                 pcparams.modIds[i] = m_modId_props[i].get();
00147                 pcparams.upperlimits[i] = m_ul_props[i].get();
00148                 pcparams.lowerlimits[i] = m_ll_props[i].get();
00149                 pcparams.offsets[i] = m_offset_props[i].get();
00150         }
00151 
00152 //      int id = m_modId_props[3].get();
00153 
00154         if ( m_powercubectrl.Init(pcparams) )
00155         {
00156                 log(Info) << "PowerCubeCtrl initialized successfully." << endlog();
00157                 return true;
00158         }
00159         else
00160         {
00161                 log(Info) << "Error while initializing PowerCubeCtrl:" << endlog();
00162                 log(Info) << m_powercubectrl.getErrorMessage() <<  endlog();
00163                 return false;
00164         }
00165         return true;
00166 }
00167 
00168 void PowerCubeCtrl_OROCOS::stopHook()
00169 {
00170         //m_stopped = true;
00171         stopArm();
00172 }
00173 
00174 bool PowerCubeCtrl_OROCOS::startHook()
00175 {
00176     if ( m_in_Angles.connected() )
00177     {
00178         // only one of the input ports should be used simultaniously
00179         if ( m_in_Velocities.connected() || m_in_Currents.connected() ) {
00180                 log(Info) << "Error in PowerCubeSim.startHook(): more than one input port is connected!" << endlog();
00181                 return false;
00182         }
00183         m_in_Angles_connected = true;
00184         log(Info) << "PowerCubeSim succesfully detected connection at in_Angles" << endlog();
00185         //m_stopped = false;
00186             return true;
00187     }
00188     if ( m_in_Velocities.connected() )
00189     {
00190         // only one of the input ports should be used simultaniously
00191         if ( m_in_Angles.connected() || m_in_Currents.connected() ) {
00192                 log(Info) << "Error in PowerCubeSim.startHook(): more than one input port is connected!" << endlog();
00193                 return false;
00194         }
00195         m_in_Velocities_connected = true;
00196         log(Info) << "PowerCubeSim succesfully detected connection at in_Velocities" << endlog();
00197         //m_stopped = false;
00198         return true;
00199     }
00200     if ( m_in_Currents.connected() )
00201     {
00202         log(Info) << "Error, port \"setCur_R\" is connected to PowerCubeSim_OROCOS.\"";
00203         log(Info) << "Current movement is not yet supported by PowerCubeSim." << endlog();
00204         return false;
00205         /*
00206         // only one of the input ports should be used simultaniously
00207         if ( m_in_Angles.connected() || m_in_Velocities.connected() ) {
00208                 log(Info) << "Error in PowerCubeSim.startHook(): more than one input port is connected!" << endlog();
00209                 return false;
00210         }
00211         m_in_Current_connected = true;
00212         //m_stopped = false;
00213             return true;
00214             */
00215     }
00216     else
00217     {
00218         log(Info) << "No Input port is connected, PowerCubeSim will only return the current state." << endlog();
00219     }
00220     return true;
00221 }
00222 
00223 void PowerCubeCtrl_OROCOS::updateHook()
00224 {
00225         // log(Info) << "updateHook is being executed." << endlog();
00226 
00227         // Execute desired movements:
00228         if ( m_in_Angles_connected )
00229         {
00230                 std::vector<double> angles_desired(7);
00231                 angles_desired = m_in_Angles.Get();
00232             m_powercubectrl.MovePos( angles_desired );
00233         }
00234 
00235         if ( m_in_Velocities_connected )
00236         {
00237                 std::vector<double> vel_desired(7);
00238                 vel_desired = m_in_Velocities.Get();
00239             m_powercubectrl.MoveVel( vel_desired );
00240         }
00241         if ( m_in_Currents_connected )
00242         {
00243                 std::vector<double> currents_desired(7);
00244                 currents_desired = m_in_Currents.Get();
00245             m_powercubectrl.MoveCur( currents_desired );
00246         }
00247 
00248     // get current angles & velocities
00249     std::vector<double> curConfig(7);
00250         m_powercubectrl.getConfig(curConfig);
00251     m_out_Angles.Set(curConfig);
00252 
00253     std::vector<double> curVelocities(7);
00254     m_powercubectrl.getJointVelocities(curVelocities);
00255     m_out_Velocities.Set(curVelocities);
00256 }
00257 
00258 bool PowerCubeCtrl_OROCOS::stopArm()
00259 {
00260     //stop
00261     m_powercubectrl.Stop();
00262     return true;
00263 }
00264 
00265 bool PowerCubeCtrl_OROCOS::moveJointSpace(std::vector<double> target)
00266 {
00267         return m_powercubectrl.MoveJointSpaceSync(target);
00268 }
00269 
00270 bool PowerCubeCtrl_OROCOS::isArmStopped()
00271 {
00272         return !m_powercubectrl.statusMoving();
00273 }


schunk_powercube_chain
Author(s): Florian Weisshardt
autogenerated on Sat Jun 8 2019 20:25:18