khi_robot_hardware_interface.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2019, Kawasaki Heavy Industries, LTD.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
37 #include <urdf/model.h>
38 
39 namespace khi_robot_control
40 {
42 {
43 }
44 
46 {
47  deactivate();
48  close();
49 }
50 
51 bool KhiRobotHardwareInterface::open( const std::string& robot_name, const std::string& ip_address, const double& period, const bool in_simulation )
52 {
53  ros::NodeHandle nh_joints;
54  std::vector<std::string> controller_names, joint_names;
55  std::stringstream param[KHI_MAX_ARM];
56  int jt;
57 
58  std::string ns = ros::this_node::getNamespace();
59  urdf::Model model;
60  model.initParam( ns + "/robot_description" );
61  data.robot_name = robot_name;
62 
63  data.arm_num = 0;
64  for ( int ano = 0; ano < KHI_MAX_ARM; ano++ )
65  {
66  jt = 0;
67  data.arm[ano].jt_num = 0;
68  param[ano] << "khi_robot_param/arm/arm" << ano + 1;
69  if ( nh_joints.getParam( param[ano].str(), controller_names ) )
70  {
71  data.arm_num++;
72  for ( int cno = 0; cno < controller_names.size(); cno++ )
73  {
74  if ( nh_joints.getParam( controller_names[cno] + "/joints", joint_names ) )
75  {
76  for ( int n = 0; n < joint_names.size(); n++ )
77  {
78  data.arm[ano].name[jt] = joint_names[n];
79  auto jt_ptr = model.getJoint( joint_names[n] );
80  data.arm[ano].type[jt] = jt_ptr->type;
81  hardware_interface::JointStateHandle state_handle( data.arm[ano].name[jt], &data.arm[ano].pos[jt], &data.arm[ano].vel[jt], &data.arm[ano].eff[jt] );
82  joint_state_interface.registerHandle( state_handle );
85  ros::NodeHandle nh_limits(robot_name);
87 
88  joint_limits_interface::getJointLimits( data.arm[ano].name[jt], nh_limits, limits );
90  joint_limit_interface.registerHandle( limits_handle );
91  jt++;
92  }
93  }
94  else
95  {
96  ROS_ERROR( "Failed to get param '/joints'" );
97  return false;
98  }
99  }
100  data.arm[ano].jt_num = jt;
101  }
102  }
103 
104  if ( in_simulation )
105  {
106  ROS_INFO_STREAM_NAMED( "khi_robot","KHI Robot Hardware Interface in simulation mode" );
107  }
108 
111 
112  /* start KhiRobotClient */
113  client = new KhiRobotClient();
114  return client->open( ip_address, period, data, in_simulation );
115 }
116 
118 {
120  return client->activate( data );
121 }
122 
124 {
125  return client->hold( data );
126 }
127 
129 {
130  client->deactivate( data );
131 }
132 
134 {
135  client->close();
136  delete client;
137 }
138 
140 {
141  client->read( data );
142 }
143 
145 {
147  client->write( data );
148 }
149 
151 {
152  return client->updateState( data );
153 }
154 
156 {
157  return client->getStateTrigger();
158 }
159 
161 {
162  return client->getPeriodDiff( diff );
163 }
164 } // namespace
bool param(const std::string &param_name, T &param_val, const T &default_val)
joint_limits_interface::PositionJointSaturationInterface joint_limit_interface
bool hold(const KhiRobotData &data)
void write(const ros::Time &time, const ros::Duration &period) override
bool activate(KhiRobotData &data)
void deactivate(const KhiRobotData &data)
#define ROS_INFO_STREAM_NAMED(name, args)
int updateState(const KhiRobotData &data)
hardware_interface::PositionJointInterface joint_position_interface
hardware_interface::JointStateInterface joint_state_interface
void enforceLimits(const ros::Duration &period)
KhiRobotArmData arm[KHI_MAX_ARM]
ROSCPP_DECL const std::string & getNamespace()
bool open(const std::string &ip, const double &period, KhiRobotData &data, const bool in_simulation=false)
URDF_EXPORT bool initParam(const std::string &param)
#define KHI_MAX_ARM
void registerHandle(const JointStateHandle &handle)
JointStateHandle getHandle(const std::string &name)
std::string name[KHI_MAX_JOINT]
void read(const ros::Time &time, const ros::Duration &period) override
void write(const KhiRobotData &data)
bool getParam(const std::string &key, std::string &s) const
bool getJointLimits(urdf::JointConstSharedPtr urdf_joint, JointLimits &limits)
#define ROS_ERROR(...)
bool open(const std::string &robot_name, const std::string &ip_address, const double &period, const bool in_simulation=false)


khi_robot_control
Author(s): nakamichi_d
autogenerated on Fri Mar 26 2021 02:34:21