MoveToHomeNode.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of the SCHUNK Canopen Driver suite.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 SCHUNK GmbH, Lauffen/Neckar Germany
00012 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00022 //----------------------------------------------------------------------
00023 
00024 #include <ros/ros.h>
00025 #include <sensor_msgs/JointState.h>
00026 
00027 #include <boost/filesystem.hpp>
00028 
00029 #include <icl_hardware_canopen/CanOpenController.h>
00030 #include <icl_hardware_canopen/SchunkPowerBallNode.h>
00031 
00032 using namespace icl_hardware::canopen_schunk;
00033 
00034 int main(int argc, char** argv)
00035 {
00036   ros::init(argc, argv, "move_arm_to_zero_node");
00037 
00038   ros::NodeHandle nh;
00039   ros::NodeHandle priv_nh("~");
00040 
00041   CanOpenController::Ptr my_controller;
00042 
00043   std::string can_device_name;
00044   std::vector<std::string> chain_names;
00045   std::map<std::string, std::vector<int> > chain_configuratuions;
00046   std::vector<DS402Group::Ptr> chain_handles;
00047   sensor_msgs::JointState joint_msg;
00048   ds402::ProfilePositionModeConfiguration ppm_config;
00049   std::map<std::string, uint8_t> joint_name_mapping;
00050 
00051   priv_nh.getParam("chain_names", chain_names);
00052   priv_nh.param<std::string>("can_device_name", can_device_name, "auto");
00053   priv_nh.param<float>  ("ppm_profile_velocity",       ppm_config.profile_velocity,         0.2);
00054   priv_nh.param<float>  ("ppm_profile_acceleration",   ppm_config.profile_acceleration,     0.2);
00055   priv_nh.param<bool>   ("ppm_use_relative_targets",   ppm_config.use_relative_targets,     false);
00056   priv_nh.param<bool>   ("ppm_change_set_immediately", ppm_config.change_set_immediately,   true);
00057   priv_nh.param<bool>   ("ppm_use_blending",           ppm_config.use_blending,             true);
00058 
00059   // Create a canopen controller
00060   try
00061   {
00062     my_controller = boost::make_shared<CanOpenController>(can_device_name);
00063   }
00064   catch (const DeviceException& e)
00065   {
00066     ROS_ERROR_STREAM ("Initializing CAN device failed. Reason: " << e.what());
00067     ROS_INFO ("Shutting down now.");
00068     return -1;
00069   }
00070 
00071   // Load SCHUNK powerball specific error codes
00072   char const* tmp = std::getenv("CANOPEN_RESOURCE_PATH");
00073   if (tmp == NULL)
00074   {
00075     LOGGING_WARNING_C(
00076         CanOpen,
00077         CanOpenController,
00078         "The environment variable 'CANOPEN_RESOURCE_PATH' could not be read. No Schunk specific error codes will be used." << endl);
00079   }
00080   else
00081   {
00082     std::string emcy_emergency_errors_filename = boost::filesystem::path(tmp / boost::filesystem::path("EMCY_schunk.ini")).string();
00083     EMCY::addEmergencyErrorMap( emcy_emergency_errors_filename, "schunk_error_codes");
00084   }
00085 
00086   // Get chain configuration from parameter server
00087   ROS_INFO_STREAM ("Can device identifier: " << can_device_name);
00088   ROS_INFO_STREAM ("Found " << chain_names.size() << " chains");
00089 
00090   // parse the robot configuration
00091   for (size_t i = 0; i < chain_names.size(); ++i)
00092   {
00093     std::string name = "chain_" + chain_names[i];
00094     my_controller->addGroup<DS402Group>(chain_names[i]);
00095     chain_handles.push_back(my_controller->getGroup<DS402Group>(chain_names[i]));
00096     std::vector<int> chain;
00097     try
00098     {
00099       priv_nh.getParam(name, chain);
00100     }
00101     catch (ros::InvalidNameException e)
00102     {
00103       ROS_ERROR_STREAM("Parameter Error!");
00104     }
00105     if (chain.size() == 0)
00106     {
00107       ROS_ERROR_STREAM("Did not find device list for chain " << chain_names[i] << ". Make sure, that an entry " << name << " exists.");
00108       continue;
00109     }
00110     ROS_INFO_STREAM ("Found chain with name " << name << " and " << chain.size() << " nodes");
00111     chain_configuratuions[name] = chain;
00112     for (size_t j = 0; j < chain.size(); ++j)
00113     {
00114       my_controller->addNode<SchunkPowerBallNode>(chain[j], chain_names[i]);
00115 
00116       std::string joint_name = "";
00117       std::string mapping_key = "~node_mapping_" + boost::lexical_cast<std::string>( chain[j]);
00118       ros::param::get(mapping_key, joint_name);
00119 
00120       joint_name_mapping[joint_name] =  static_cast<uint8_t>(chain[j]);
00121       joint_msg.name.push_back(joint_name);
00122     }
00123   }
00124 
00125   // initialize all nodes, by default this will start ProfilePosition mode, so we're good to enable nodes
00126   try {
00127     my_controller->initNodes();
00128   }
00129   catch (const ProtocolException& e)
00130   {
00131     ROS_ERROR_STREAM ("Caught ProtocolException while initializing devices: " << e.what());
00132     ROS_INFO ("Going to shut down now");
00133     exit (-1);
00134   }
00135   catch (const PDOException& e)
00136   {
00137     ROS_ERROR_STREAM ("Caught PDOException while initializing devices: " << e.what());
00138     ROS_INFO ("Going to shut down now");
00139     exit (-1);
00140   }
00141 
00142   ds402::eModeOfOperation mode = ds402::MOO_PROFILE_POSITION_MODE;
00143 
00144   for (size_t i = 0; i < chain_handles.size(); ++i)
00145   {
00146     try {
00147       ROS_INFO_STREAM ("Setting up Profile Position mode for chain " << chain_handles[i]->getName());
00148       chain_handles[i]->setupProfilePositionMode(ppm_config);
00149       chain_handles[i]->enableNodes(mode);
00150     }
00151     catch (const ProtocolException& e)
00152     {
00153       ROS_ERROR_STREAM ("Caught ProtocolException while enabling nodes from chain " <<
00154         chain_handles[i]->getName() << ". Nodes from this group won't be enabled.");
00155       continue;
00156     }
00157     ROS_INFO_STREAM ("Enabled nodes from chain " << chain_handles[i]->getName());
00158     std::vector<DS301Node::Ptr> nodes = chain_handles[i]->getNodes();
00159     std::vector<float> targets (nodes.size(), 0.0);
00160 
00161     chain_handles[i]->setTarget(targets);
00162   }
00163   my_controller->enablePPMotion();
00164 
00165   std::vector<bool> foo;
00166 
00167   while ( true )
00168   {
00169     size_t num_reached = 0;
00170     try {
00171       my_controller->syncAll();
00172     }
00173     catch (const std::exception& e)
00174     {
00175       ROS_ERROR_STREAM (e.what());
00176       return -11;
00177     }
00178     usleep(10000);
00179 
00180     for (size_t i = 0; i < chain_handles.size(); ++i)
00181     {
00182       if (chain_handles[i]->isTargetReached(foo))
00183       {
00184         num_reached++;
00185       }
00186     }
00187     if (num_reached == chain_handles.size())
00188     {
00189       break;
00190     }
00191   }
00192 
00193   LOGGING_INFO (CanOpen, "All targets reached" << endl);
00194 
00195   try
00196   {
00197     for (size_t i = 0; i < chain_handles.size(); ++i)
00198     {
00199       chain_handles[i]->disableNodes();
00200     }
00201   }
00202   catch (const ProtocolException& e)
00203   {
00204     ROS_ERROR_STREAM ( "Error while disabling nodes: " << e.what());
00205   }
00206 
00207   return 0;
00208 }


schunk_canopen_driver
Author(s): Felix Mauch , Georg Heppner
autogenerated on Thu Jun 6 2019 20:17:24