00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2018, 3M 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions are met: 00009 * 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * * Neither the name of the copyright holder, nor the names of its 00016 * contributors may be used to endorse or promote products derived 00017 * from this software without specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00023 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00024 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00025 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00026 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00027 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00029 * POSSIBILITY OF SUCH DAMAGE. 00030 */ 00031 00032 // Author: Brett Hemes (3M) <brhemes@mmm.com> 00033 00034 00035 #ifndef KUKA_EKI_HW_INTERFACE 00036 #define KUKA_EKI_HW_INTERFACE 00037 00038 #include <vector> 00039 #include <string> 00040 00041 #include <boost/asio.hpp> 00042 00043 #include <ros/ros.h> 00044 #include <controller_manager/controller_manager.h> 00045 #include <hardware_interface/joint_command_interface.h> 00046 #include <hardware_interface/joint_state_interface.h> 00047 #include <hardware_interface/robot_hw.h> 00048 00049 00050 namespace kuka_eki_hw_interface 00051 { 00052 00053 class KukaEkiHardwareInterface : public hardware_interface::RobotHW 00054 { 00055 private: 00056 ros::NodeHandle nh_; 00057 00058 const unsigned int n_dof_ = 6; 00059 std::vector<std::string> joint_names_; 00060 std::vector<double> joint_position_; 00061 std::vector<double> joint_velocity_; 00062 std::vector<double> joint_effort_; 00063 std::vector<double> joint_position_command_; 00064 00065 // EKI 00066 std::string eki_server_address_; 00067 std::string eki_server_port_; 00068 int eki_cmd_buff_len_; 00069 int eki_max_cmd_buff_len_ = 5; // by default, limit command buffer to 5 (size of advance run in KRL) 00070 00071 // Timing 00072 ros::Duration control_period_; 00073 ros::Duration elapsed_time_; 00074 double loop_hz_; 00075 00076 // Interfaces 00077 hardware_interface::JointStateInterface joint_state_interface_; 00078 hardware_interface::PositionJointInterface position_joint_interface_; 00079 00080 // EKI socket read/write 00081 int eki_read_state_timeout_ = 5; // [s]; settable by parameter (default = 5) 00082 boost::asio::io_service ios_; 00083 boost::asio::deadline_timer deadline_; 00084 boost::asio::ip::udp::endpoint eki_server_endpoint_; 00085 boost::asio::ip::udp::socket eki_server_socket_; 00086 void eki_check_read_state_deadline(); 00087 static void eki_handle_receive(const boost::system::error_code &ec, size_t length, 00088 boost::system::error_code* out_ec, size_t* out_length); 00089 bool eki_read_state(std::vector<double> &joint_position, std::vector<double> &joint_velocity, 00090 std::vector<double> &joint_effort, int &cmd_buff_len); 00091 bool eki_write_command(const std::vector<double> &joint_position); 00092 00093 public: 00094 00095 KukaEkiHardwareInterface(); 00096 ~KukaEkiHardwareInterface(); 00097 00098 void init(); 00099 void start(); 00100 void read(const ros::Time &time, const ros::Duration &period); 00101 void write(const ros::Time &time, const ros::Duration &period); 00102 }; 00103 00104 } // namespace kuka_eki_hw_interface 00105 00106 #endif // KUKA_EKI_HW_INTERFACE