MoveToHomeNode.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of the SCHUNK Canopen Driver suite.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 SCHUNK GmbH, Lauffen/Neckar Germany
12 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
23 
24 #include <ros/ros.h>
25 #include <sensor_msgs/JointState.h>
26 
27 #include <boost/filesystem.hpp>
28 
31 
32 using namespace icl_hardware::canopen_schunk;
33 
34 int main(int argc, char** argv)
35 {
36  ros::init(argc, argv, "move_arm_to_zero_node");
37 
38  ros::NodeHandle nh;
39  ros::NodeHandle priv_nh("~");
40 
41  CanOpenController::Ptr my_controller;
42 
43  std::string can_device_name;
44  std::vector<std::string> chain_names;
45  std::map<std::string, std::vector<int> > chain_configuratuions;
46  std::vector<DS402Group::Ptr> chain_handles;
47  sensor_msgs::JointState joint_msg;
49  std::map<std::string, uint8_t> joint_name_mapping;
50 
51  priv_nh.getParam("chain_names", chain_names);
52  priv_nh.param<std::string>("can_device_name", can_device_name, "auto");
53  priv_nh.param<float> ("ppm_profile_velocity", ppm_config.profile_velocity, 0.2);
54  priv_nh.param<float> ("ppm_profile_acceleration", ppm_config.profile_acceleration, 0.2);
55  priv_nh.param<bool> ("ppm_use_relative_targets", ppm_config.use_relative_targets, false);
56  priv_nh.param<bool> ("ppm_change_set_immediately", ppm_config.change_set_immediately, true);
57  priv_nh.param<bool> ("ppm_use_blending", ppm_config.use_blending, true);
58 
59  // Create a canopen controller
60  try
61  {
62  my_controller = boost::make_shared<CanOpenController>(can_device_name);
63  }
64  catch (const DeviceException& e)
65  {
66  ROS_ERROR_STREAM ("Initializing CAN device failed. Reason: " << e.what());
67  ROS_INFO ("Shutting down now.");
68  return -1;
69  }
70 
71  // Load SCHUNK powerball specific error codes
72  char const* tmp = std::getenv("CANOPEN_RESOURCE_PATH");
73  if (tmp == NULL)
74  {
76  CanOpen,
78  "The environment variable 'CANOPEN_RESOURCE_PATH' could not be read. No Schunk specific error codes will be used." << endl);
79  }
80  else
81  {
82  std::string emcy_emergency_errors_filename = boost::filesystem::path(tmp / boost::filesystem::path("EMCY_schunk.ini")).string();
83  EMCY::addEmergencyErrorMap( emcy_emergency_errors_filename, "schunk_error_codes");
84  }
85 
86  // Get chain configuration from parameter server
87  ROS_INFO_STREAM ("Can device identifier: " << can_device_name);
88  ROS_INFO_STREAM ("Found " << chain_names.size() << " chains");
89 
90  // parse the robot configuration
91  for (size_t i = 0; i < chain_names.size(); ++i)
92  {
93  std::string name = "chain_" + chain_names[i];
94  my_controller->addGroup<DS402Group>(chain_names[i]);
95  chain_handles.push_back(my_controller->getGroup<DS402Group>(chain_names[i]));
96  std::vector<int> chain;
97  try
98  {
99  priv_nh.getParam(name, chain);
100  }
101  catch (ros::InvalidNameException e)
102  {
103  ROS_ERROR_STREAM("Parameter Error!");
104  }
105  if (chain.size() == 0)
106  {
107  ROS_ERROR_STREAM("Did not find device list for chain " << chain_names[i] << ". Make sure, that an entry " << name << " exists.");
108  continue;
109  }
110  ROS_INFO_STREAM ("Found chain with name " << name << " and " << chain.size() << " nodes");
111  chain_configuratuions[name] = chain;
112  for (size_t j = 0; j < chain.size(); ++j)
113  {
114  my_controller->addNode<SchunkPowerBallNode>(chain[j], chain_names[i]);
115 
116  std::string joint_name = "";
117  std::string mapping_key = "~node_mapping_" + boost::lexical_cast<std::string>( chain[j]);
118  ros::param::get(mapping_key, joint_name);
119 
120  joint_name_mapping[joint_name] = static_cast<uint8_t>(chain[j]);
121  joint_msg.name.push_back(joint_name);
122  }
123  }
124 
125  // initialize all nodes, by default this will start ProfilePosition mode, so we're good to enable nodes
126  try {
127  my_controller->initNodes();
128  }
129  catch (const ProtocolException& e)
130  {
131  ROS_ERROR_STREAM ("Caught ProtocolException while initializing devices: " << e.what());
132  ROS_INFO ("Going to shut down now");
133  exit (-1);
134  }
135  catch (const PDOException& e)
136  {
137  ROS_ERROR_STREAM ("Caught PDOException while initializing devices: " << e.what());
138  ROS_INFO ("Going to shut down now");
139  exit (-1);
140  }
141 
143 
144  for (size_t i = 0; i < chain_handles.size(); ++i)
145  {
146  try {
147  ROS_INFO_STREAM ("Setting up Profile Position mode for chain " << chain_handles[i]->getName());
148  chain_handles[i]->setupProfilePositionMode(ppm_config);
149  chain_handles[i]->enableNodes(mode);
150  }
151  catch (const ProtocolException& e)
152  {
153  ROS_ERROR_STREAM ("Caught ProtocolException while enabling nodes from chain " <<
154  chain_handles[i]->getName() << ". Nodes from this group won't be enabled.");
155  continue;
156  }
157  ROS_INFO_STREAM ("Enabled nodes from chain " << chain_handles[i]->getName());
158  std::vector<DS301Node::Ptr> nodes = chain_handles[i]->getNodes();
159  std::vector<float> targets (nodes.size(), 0.0);
160 
161  chain_handles[i]->setTarget(targets);
162  }
163  my_controller->enablePPMotion();
164 
165  std::vector<bool> foo;
166 
167  while ( true )
168  {
169  size_t num_reached = 0;
170  try {
171  my_controller->syncAll();
172  }
173  catch (const std::exception& e)
174  {
175  ROS_ERROR_STREAM (e.what());
176  return -11;
177  }
178  usleep(10000);
179 
180  for (size_t i = 0; i < chain_handles.size(); ++i)
181  {
182  if (chain_handles[i]->isTargetReached(foo))
183  {
184  num_reached++;
185  }
186  }
187  if (num_reached == chain_handles.size())
188  {
189  break;
190  }
191  }
192 
193  LOGGING_INFO (CanOpen, "All targets reached" << endl);
194 
195  try
196  {
197  for (size_t i = 0; i < chain_handles.size(); ++i)
198  {
199  chain_handles[i]->disableNodes();
200  }
201  }
202  catch (const ProtocolException& e)
203  {
204  ROS_ERROR_STREAM ( "Error while disabling nodes: " << e.what());
205  }
206 
207  return 0;
208 }
Configuration parameters for a Profile_Position_Mode according to CiA DSP-402 V1.1 section 12...
Definition: ds402.h:169
static void addEmergencyErrorMap(const std::string &filename, const std::string &block_identifier)
Adds new information from an ini file to the emergency error map.
Definition: EMCY.cpp:213
#define LOGGING_INFO(streamname, arg)
Basic CanOpen exception that contains the Object dictionary index and subindex.
Definition: exceptions.h:37
This class gives a device specific interface for Schunk Powerballs, as they need some "special" treat...
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
std::string getName(void *handle)
virtual const char * what() const _GLIBCXX_USE_NOEXCEPT
Definition: exceptions.h:144
The DS402Group class is the base class for canOpen devices implementing the DS402 device protocol (mo...
Definition: DS402Group.h:45
bool use_blending
If set to true, the device will blend over to a new setpoint. Defaults to true.
Definition: ds402.h:213
PDO related exceptions go here.
Definition: exceptions.h:114
#define LOGGING_WARNING_C(streamname, classname, arg)
int main(int argc, char **argv)
The CanOpenController class is the main entry point for any calls to the canOpen System.
unsigned char uint8_t
bool use_relative_targets
This parameter influences the interpretation of new set points. If set to true, new set point positio...
Definition: ds402.h:207
float profile_acceleration
This will be used for both acceleration ramps.
Definition: ds402.h:179
ROSCPP_DECL bool get(const std::string &key, std::string &s)
ThreadStream & endl(ThreadStream &stream)
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
If something goes wrong with the host&#39;s CAN controller, this exception will be used.
Definition: exceptions.h:135
virtual const char * what() const _GLIBCXX_USE_NOEXCEPT
Definition: exceptions.h:123
#define ROS_INFO_STREAM(args)
bool getParam(const std::string &key, std::string &s) const
virtual const char * what() const _GLIBCXX_USE_NOEXCEPT
Definition: exceptions.h:50
#define ROS_ERROR_STREAM(args)
bool change_set_immediately
If this is set to true the device will not ramp down at a setpoint if a following one is given...
Definition: ds402.h:199
int usleep(unsigned long useconds)


schunk_canopen_driver
Author(s): Felix Mauch , Georg Heppner
autogenerated on Mon Jun 10 2019 15:07:49