steer_bot_hardware_gazebo.h
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2016, CIR-KIT
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 CIR-KIT 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 /*
00036  * Author: Masaru Morita
00037  */
00038 
00039 #ifndef STEER_BOT_HARDWARE_GAZEBO_H
00040 #define STEER_BOT_HARDWARE_GAZEBO_H
00041 
00042 #include <vector>
00043 #include <string>
00044 
00045 #include <control_toolbox/pid.h>
00046 
00047 #include <hardware_interface/robot_hw.h>
00048 #include <hardware_interface/joint_state_interface.h>
00049 #include <hardware_interface/joint_command_interface.h>
00050 #include <joint_limits_interface/joint_limits_interface.h>
00051 
00052 #include <gazebo_ros_control/robot_hw_sim.h>
00053 
00054 #include <gazebo/gazebo.hh>
00055 #include <gazebo/physics/physics.hh>
00056 #include <gazebo/common/common.hh>
00057 
00058 namespace steer_bot_hardware_gazebo
00059 {
00060 
00061 double clamp(const double val, const double min_val, const double max_val)
00062 {
00063   return std::min(std::max(val, min_val), max_val);
00064 }
00065 
00066 class SteerBotHardwareGazebo : public gazebo_ros_control::RobotHWSim
00067 {
00068 public:
00069 
00070   SteerBotHardwareGazebo();
00071 
00072   bool initSim(const std::string& robot_namespace,
00073       ros::NodeHandle model_nh,
00074       gazebo::physics::ModelPtr parent_model,
00075       const urdf::Model* const urdf_model,
00076       std::vector<transmission_interface::TransmissionInfo> transmissions);
00077 
00078   void readSim(ros::Time time, ros::Duration period);
00079 
00080   void writeSim(ros::Time time, ros::Duration period);
00081 
00082 private:
00083   void CleanUp();
00084 
00085   void GetJointNames(ros::NodeHandle &_nh);
00086   void GetWheelJointNames(ros::NodeHandle &_nh);
00087   void GetSteerJointNames(ros::NodeHandle &_nh);
00088 
00089   void RegisterHardwareInterfaces();
00090   void RegisterWheelInterface();
00091   void RegisterSteerInterface();
00092   void RegisterInterfaceHandles(
00093       hardware_interface::JointStateInterface& _jnt_state_interface,
00094           hardware_interface::JointCommandInterface& _jnt_cmd_interface,
00095           const std::string _jnt_name, double& _jnt_pos, double& _jnt_vel, double& _jnt_eff,  double& _jnt_cmd);
00096   void RegisterJointStateInterfaceHandle(
00097       hardware_interface::JointStateInterface& _jnt_state_interface,
00098       const std::string _jnt_name, double& _jnt_pos, double& _jnt_vel, double& _jnt_eff);
00099   void RegisterCommandJointInterfaceHandle(
00100       hardware_interface::JointStateInterface& _jnt_state_interface,
00101       hardware_interface::JointCommandInterface& _jnt_cmd_interface,
00102       const std::string _jnt_name, double& _jnt_cmd);
00103   double ComputeEffCommandFromVelError(const int _index, ros::Duration _period);
00104 
00105   void GetCurrentState(std::vector<double>& _jnt_pos, std::vector<double>& _jnt_vel, std::vector<double>& _jnt_eff,
00106                        const int _if_index, const int _sim_jnt_index);
00107 
00108 private:
00109   // constant
00110   enum {
00111       INDEX_RIGHT = 0,
00112       INDEX_LEFT = 1
00113   };
00114   // Raw data
00115   unsigned int n_dof_;
00116   std::vector<gazebo::physics::JointPtr> sim_joints_;
00117 
00118   // Joint limits interface
00119   joint_limits_interface::PositionJointSoftLimitsInterface jnt_limits_interface_;
00120 
00121   // PID controllers
00122   std::vector<control_toolbox::Pid> pids_;
00123 
00124   // new added
00125   ros::NodeHandle nh_;
00126   std::string ns_;
00127 
00128   // common
00129   hardware_interface::JointStateInterface jnt_state_interface_;
00130   //
00131   // rear wheel
00132   //-- actual joint(single actuator)
00133   //---- joint name
00134   std::string rear_wheel_jnt_name_;
00135   //---- joint interface parameters
00136   double rear_wheel_jnt_pos_;
00137   double rear_wheel_jnt_vel_;
00138   double rear_wheel_jnt_eff_;
00139   //---- joint interface command
00140   double rear_wheel_jnt_vel_cmd_;
00141   //---- Hardware interface: joint
00142   hardware_interface::VelocityJointInterface rear_wheel_jnt_vel_cmd_interface_;
00143   //hardware_interface::JointStateInterface wheel_jnt_state_interface_;
00144   //
00145   //-- virtual joints(two rear wheels)
00146   //---- joint name
00147   std::vector<std::string> virtual_rear_wheel_jnt_names_;
00148   //---- joint interface parameters
00149   std::vector<double> virtual_rear_wheel_jnt_pos_;
00150   std::vector<double> virtual_rear_wheel_jnt_vel_;
00151   std::vector<double> virtual_rear_wheel_jnt_eff_;
00152   //---- joint interface command
00153   std::vector<double> virtual_rear_wheel_jnt_vel_cmd_;
00154   //-- virtual joints(two front wheels)
00155   //---- joint name
00156   std::vector<std::string> virtual_front_wheel_jnt_names_;
00157   //---- joint interface parameters
00158   std::vector<double> virtual_front_wheel_jnt_pos_;
00159   std::vector<double> virtual_front_wheel_jnt_vel_;
00160   std::vector<double> virtual_front_wheel_jnt_eff_;
00161   //---- joint interface command
00162   std::vector<double> virtual_front_wheel_jnt_vel_cmd_;
00163 
00164   // front steer
00165   //-- actual joint(single actuator)
00166   //---- joint name
00167   std::string front_steer_jnt_name_;
00168   //---- joint interface parameters
00169   double front_steer_jnt_pos_;
00170   double front_steer_jnt_vel_;
00171   double front_steer_jnt_eff_;
00172   //---- joint interface command
00173   double front_steer_jnt_pos_cmd_;
00174   //---- Hardware interface: joint
00175   hardware_interface::PositionJointInterface front_steer_jnt_pos_cmd_interface_;
00176   //hardware_interface::JointStateInterface steer_jnt_state_interface_;
00177   //
00178   //-- virtual joints(two steers)
00179   //---- joint name
00180   std::vector<std::string> virtual_front_steer_jnt_names_;
00181   //---- joint interface parameters
00182   std::vector<double> virtual_front_steer_jnt_pos_;
00183   std::vector<double> virtual_front_steer_jnt_vel_;
00184   std::vector<double> virtual_front_steer_jnt_eff_;
00185   //---- joint interface command
00186   std::vector<double> virtual_front_steer_jnt_pos_cmd_;
00187   //---- Hardware interface: joint
00188   hardware_interface::VelocityJointInterface front_wheel_jnt_vel_cmd_interface_;
00189 
00190   // ackermann link mechanism
00191   bool enable_ackermann_link_ ;
00192   double wheel_separation_w_;
00193   double wheel_separation_h_;
00194 
00195   int log_cnt_;
00196 
00197   template <class T>
00198   std::string containerToString(const T& cont, const std::string& prefix)
00199   {
00200     std::stringstream ss;
00201     ss << prefix;
00202     std::copy(cont.begin(), --cont.end(), std::ostream_iterator<typename T::value_type>(ss, prefix.c_str()));
00203     ss << *(--cont.end());
00204     return ss.str();
00205   }
00206 
00207 };
00208 
00209 } // namespace steer_bot_hardware_gazebo
00210 
00211 #endif // STEER_BOT_HARDWARE_GAZEBO_H


steer_bot_hardware_gazebo
Author(s): Enrique Fernandez
autogenerated on Sat Jun 8 2019 19:20:22