aubo_hardware_interface.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2015, University of Colorado, Boulder
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
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the Univ of CO, Boulder nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************
00034 
00035  Author: Dave Coleman
00036  */
00037 
00038 #include <aubo_new_driver/aubo_hardware_interface.h>
00039 
00040 
00041 namespace ros_control_aubo {
00042 
00043 AuboHardwareInterface::AuboHardwareInterface(ros::NodeHandle& nh, AuboNewDriver* robot) :
00044                 nh_(nh), robot_(robot) {
00045         // Initialize shared memory and interfaces here
00046         init(); // this implementation loads from rosparam
00047 
00048         max_vel_change_ = 0.12; // equivalent of an acceleration of 15 rad/sec^2
00049 
00050         ROS_INFO_NAMED("aubo_hardware_interface", "Loaded aubo_hardware_interface.");
00051 }
00052 
00053 void AuboHardwareInterface::init() {
00054         ROS_INFO_STREAM_NAMED("aubo_hardware_interface",
00055                         "Reading rosparams from namespace: " << nh_.getNamespace());
00056 
00057         // Get joint names
00058         nh_.getParam("hardware_interface/joints", joint_names_);
00059         if (joint_names_.size() == 0) {
00060                 ROS_FATAL_STREAM_NAMED("aubo_hardware_interface",
00061                                 "No joints found on parameter server for controller, did you load the proper yaml file?" << " Namespace: " << nh_.getNamespace());
00062                 exit(-1);
00063         }
00064         num_joints_ = joint_names_.size();
00065 
00066         // Resize vectors
00067         joint_position_.resize(num_joints_);
00068         joint_velocity_.resize(num_joints_);
00069         joint_effort_.resize(num_joints_);
00070         joint_position_command_.resize(num_joints_);
00071         last_joint_position_command_.resize(num_joints_);
00072         joint_velocity_command_.resize(num_joints_);
00073         prev_joint_velocity_command_.resize(num_joints_);
00074 
00075         // Initialize controller
00076         for (std::size_t i = 0; i < num_joints_; ++i) {
00077                 ROS_DEBUG_STREAM_NAMED("aubo_hardware_interface",
00078                                 "Loading joint name: " << joint_names_[i]);
00079 
00080                 // Create joint state interface
00081                 joint_state_interface_.registerHandle(
00082                                 hardware_interface::JointStateHandle(joint_names_[i],
00083                                                 &joint_position_[i], &joint_velocity_[i],
00084                                                 &joint_effort_[i]));
00085 
00086                 // Create position joint interface
00087                 position_joint_interface_.registerHandle(
00088                                 hardware_interface::JointHandle(
00089                                                 joint_state_interface_.getHandle(joint_names_[i]),
00090                                                 &joint_position_command_[i]));
00091 
00092                 // Create velocity joint interface
00093                 velocity_joint_interface_.registerHandle(
00094                                 hardware_interface::JointHandle(
00095                                                 joint_state_interface_.getHandle(joint_names_[i]),
00096                                                 &joint_velocity_command_[i]));
00097                 prev_joint_velocity_command_[i] = 0.;
00098         }
00099 
00100         // Create force torque interface
00101         force_torque_interface_.registerHandle(
00102                         hardware_interface::ForceTorqueSensorHandle("wrench", "",
00103                                         robot_force_, robot_torque_));
00104 
00105         registerInterface(&joint_state_interface_); // From RobotHW base class.
00106         registerInterface(&position_joint_interface_); // From RobotHW base class.
00107         registerInterface(&velocity_joint_interface_); // From RobotHW base class.
00108         registerInterface(&force_torque_interface_); // From RobotHW base class.
00109         velocity_interface_running_ = false;
00110         position_interface_running_ = false;
00111 }
00112 
00113 void AuboHardwareInterface::read() {
00114         std::vector<double> pos, vel, current, tcp;
00115     pos = robot_->rt_interface_->robot_state_->getJonitPosition();
00116     vel = robot_->rt_interface_->robot_state_->getJonitVelocity();
00117     current = robot_->rt_interface_->robot_state_->getJointCurrent();
00118     tcp = robot_->rt_interface_->robot_state_->getTcpForce();
00119         for (std::size_t i = 0; i < num_joints_; ++i) {
00120                 joint_position_[i] = pos[i];
00121                 joint_velocity_[i] = vel[i];
00122                 joint_effort_[i] = current[i];
00123         }
00124         for (std::size_t i = 0; i < 3; ++i) {
00125                 robot_force_[i] = tcp[i];
00126                 robot_torque_[i] = tcp[i + 3];
00127         }
00128 }
00129 
00130 void AuboHardwareInterface::setMaxVelChange(double inp) {
00131         max_vel_change_ = inp;
00132 }
00133 
00134 void AuboHardwareInterface::write() {
00135         int i=0;
00136         if (velocity_interface_running_) {
00137                 std::vector<double> cmd;
00138                 //do some rate limiting
00139                 cmd.resize(joint_velocity_command_.size());
00140                 for (unsigned int i = 0; i < joint_velocity_command_.size(); i++) {
00141                         cmd[i] = joint_velocity_command_[i];
00142                         if (cmd[i] > prev_joint_velocity_command_[i] + max_vel_change_) {
00143                                 cmd[i] = prev_joint_velocity_command_[i] + max_vel_change_;
00144                         } else if (cmd[i]
00145                                         < prev_joint_velocity_command_[i] - max_vel_change_) {
00146                                 cmd[i] = prev_joint_velocity_command_[i] - max_vel_change_;
00147                         }
00148                         prev_joint_velocity_command_[i] = cmd[i];
00149                 }
00150         robot_->setSpeed(cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], cmd[5],  max_vel_change_*125);
00151     }
00152     else if (position_interface_running_) {
00153 
00154           for(i=0;i<6;i++)
00155           {
00156             if(fabs(joint_position_command_[i]-last_joint_position_command_[i])>=0.000001)
00157             {
00158                break;
00159             }
00160           }
00161 
00162           if(i<6)
00163           {
00164             robot_->servoj(joint_position_command_);
00165             last_joint_position_command_ = joint_position_command_;
00166           }
00167         }
00168 }
00169 
00170 bool AuboHardwareInterface::canSwitch(
00171                 const std::list<hardware_interface::ControllerInfo> &start_list,
00172                 const std::list<hardware_interface::ControllerInfo> &stop_list) const {
00173         for (std::list<hardware_interface::ControllerInfo>::const_iterator controller_it =
00174                         start_list.begin(); controller_it != start_list.end();
00175                         ++controller_it) {
00176         if (controller_it->type
00177                 == "velocity_controllers/JointTrajectoryController") {
00178                         if (velocity_interface_running_) {
00179                                 ROS_ERROR(
00180                                                 "%s: An interface of that type (%s) is already running",
00181                                                 controller_it->name.c_str(),
00182                         controller_it->type.c_str());
00183                                 return false;
00184                         }
00185                         if (position_interface_running_) {
00186                                 bool error = true;
00187                                 for (std::list<hardware_interface::ControllerInfo>::const_iterator stop_controller_it =
00188                                                 stop_list.begin();
00189                                                 stop_controller_it != stop_list.end();
00190                                                 ++stop_controller_it) {
00191                     if (stop_controller_it->type
00192                             == "position_controllers/JointTrajectoryController") {
00193                                                 error = false;
00194                                                 break;
00195                                         }
00196                                 }
00197                                 if (error) {
00198                                         ROS_ERROR(
00199                                                         "%s (type %s) can not be run simultaneously with a PositionJointInterface",
00200                                                         controller_it->name.c_str(),
00201                             controller_it->type.c_str());
00202                     return false;
00203                                 }
00204                         }
00205         } else if (controller_it->type
00206                 == "position_controllers/JointTrajectoryController") {
00207                         if (position_interface_running_) {
00208                                 ROS_ERROR(
00209                                                 "%s: An interface of that type (%s) is already running",
00210                                                 controller_it->name.c_str(),
00211                         controller_it->type.c_str());
00212                                 return false;
00213                         }
00214                         if (velocity_interface_running_) {
00215                                 bool error = true;
00216                                 for (std::list<hardware_interface::ControllerInfo>::const_iterator stop_controller_it =
00217                                                 stop_list.begin();
00218                                                 stop_controller_it != stop_list.end();
00219                                                 ++stop_controller_it) {
00220                     if (stop_controller_it->type
00221                             == "velocity_controllers/JointTrajectoryController") {
00222                                                 error = false;
00223                                                 break;
00224                                         }
00225                                 }
00226                                 if (error) {
00227                                         ROS_ERROR(
00228                                                         "%s (type %s) can not be run simultaneously with a VelocityJointInterface",
00229                                                         controller_it->name.c_str(),
00230                             controller_it->type.c_str());
00231                                         return false;
00232                                 }
00233                         }
00234                 }
00235         }
00236 
00237 // we can always stop a controller
00238         return true;
00239 }
00240 
00241 void AuboHardwareInterface::doSwitch(
00242                 const std::list<hardware_interface::ControllerInfo>& start_list,
00243                 const std::list<hardware_interface::ControllerInfo>& stop_list) {
00244         for (std::list<hardware_interface::ControllerInfo>::const_iterator controller_it =
00245                         stop_list.begin(); controller_it != stop_list.end();
00246                         ++controller_it) {
00247         if (controller_it->type
00248                 == "velocity_controllers/JointTrajectoryController") {
00249                         velocity_interface_running_ = false;
00250                         ROS_DEBUG("Stopping velocity interface");
00251                 }
00252         if (controller_it->type
00253                 == "position_controllers/JointTrajectoryController") {
00254                         position_interface_running_ = false;
00255                         std::vector<double> tmp;
00256             robot_->closeServo(tmp);
00257                         ROS_DEBUG("Stopping position interface");
00258                 }
00259         }
00260         for (std::list<hardware_interface::ControllerInfo>::const_iterator controller_it =
00261                         start_list.begin(); controller_it != start_list.end();
00262                         ++controller_it) {
00263 
00264         if (controller_it->type
00265                 == "velocity_controllers/JointTrajectoryController") {
00266                         velocity_interface_running_ = true;
00267                         ROS_DEBUG("Starting velocity interface");
00268         }
00269         if (controller_it->type
00270                 == "position_controllers/JointTrajectoryController") {
00271                         position_interface_running_ = true;
00272             robot_->openServo();
00273                         ROS_DEBUG("Starting position interface");
00274                 }
00275         }
00276 
00277 }
00278 
00279 } // namespace
00280 


aubo_new_driver
Author(s): liuxin
autogenerated on Wed Sep 6 2017 03:06:44