yumi_hw_gazebo.h
Go to the documentation of this file.
00001 #ifndef __YUMI_HW_GAZEBO_H
00002 #define __YUMI_HW_GAZEBO_H
00003 
00004 #include<yumi_hw/yumi_hw.h>
00005 
00006 // ROS
00007 #include <angles/angles.h>
00008 // #include <pluginlib/class_list_macros.h>
00009 
00010 // Gazebo hook
00011 #include <gazebo/common/common.hh>
00012 #include <gazebo/physics/physics.hh>
00013 #include <gazebo/gazebo.hh>
00014 
00015 class YumiHWGazebo : public YumiHW
00016 {
00017     public:
00018 
00019         YumiHWGazebo() : YumiHW() 
00020         {
00021             parent_set_=false;
00022         }
00023         ~YumiHWGazebo() {}
00024 
00025         void setParentModel(gazebo::physics::ModelPtr parent_model) 
00026         {
00027             parent_model_ = parent_model; 
00028             parent_set_ = true;
00029         };
00030 
00031         // Init, read, and write, with Gazebo hooks
00032         bool init()
00033         {
00034             if( !(parent_set_) )
00035             {
00036                 ROS_ERROR_STREAM("Did you forget to set the parent model?" << std::endl << "You must do that before init()" << std::endl << "Exiting...");
00037                 return false;
00038             }
00039 
00040             gazebo::physics::JointPtr joint;
00041             for(int j=0; j < n_joints_; j++)
00042             {
00043                 joint = parent_model_->GetJoint(joint_names_[j]);
00044                 if (!joint)
00045                 {
00046                     ROS_ERROR_STREAM("This robot has a joint named \"" << joint_names_[j] << "\" which is not in the gazebo model.");
00047                     return false;
00048                 }
00049                 sim_joints_.push_back(joint);
00050             }
00051 
00052             return true;
00053         }
00054 
00055         void read(ros::Time time, ros::Duration period)
00056         {
00057             for(int j=0; j < n_joints_; ++j)
00058             {
00059                 joint_position_prev_[j] = joint_position_[j];
00060                 joint_position_[j] += angles::shortest_angular_distance(joint_position_[j],
00061                         sim_joints_[j]->GetAngle(0).Radian());
00062                 //joint_position_kdl_(j) = joint_position_[j];
00063                 // derivate velocity as in the real hardware instead of reading it from simulation
00064                 joint_velocity_[j] = filters::exponentialSmoothing((joint_position_[j] - joint_position_prev_[j])/period.toSec(), joint_velocity_[j], 0.2);
00065                 joint_effort_[j] = sim_joints_[j]->GetForce((int)(0));
00066                 //joint_stiffness_[j] = joint_stiffness_command_[j];
00067             }
00068         }
00069 
00070         void write(ros::Time time, ros::Duration period)
00071         {
00072             enforceLimits(period);
00073 
00074             switch (getControlStrategy())
00075             {
00076 
00077                 case JOINT_POSITION:
00078                     for(int j=0; j < n_joints_; j++)
00079                     {
00080                         // according to the gazebo_ros_control plugin, this must *not* be called if SetForce is going to be called
00081                         // but should be called when SetPostion is going to be called
00082                         // so enable this when I find the SetMaxForce reset.
00083                         // sim_joints_[j]->SetMaxForce(0, joint_effort_limits_[j]);
00084 #if GAZEBO_MAJOR_VERSION >= 4
00085                         sim_joints_[j]->SetPosition(0, joint_position_command_[j]);
00086 #else
00087                         sim_joints_[j]->SetAngle(0, joint_position_command_[j]);
00088 #endif
00089                     }
00090                     break;
00091 
00092                 case JOINT_VELOCITY:
00093                     for(int j=0; j < n_joints_; j++)
00094                     {
00095                         //integrate forward to next position
00096 #if GAZEBO_MAJOR_VERSION >= 4
00097                         sim_joints_[j]->SetPosition(0, joint_velocity_command_[j]*period.toSec() + joint_position_[j]);
00098 #else
00099                         sim_joints_[j]->SetAngle(0, joint_velocity_command_[j]*period.toSec() + joint_position_[j]);
00100 #endif
00101                     }
00102                     break;
00103 
00104 #if 0
00105                 case JOINT_IMPEDANCE:
00106                     // compute the gracity term
00107                     f_dyn_solver_->JntToGravity(joint_position_kdl_, gravity_effort_);
00108 
00109                     for(int j=0; j < n_joints_; j++)
00110                     {
00111                         // replicate the joint impedance control strategy
00112                         // tau = k (q_FRI - q_msr) + tau_FRI + D(q_msr) + f_dyn(q_msr)
00113                         const double stiffness_effort = 0.0;//10.0*( joint_position_command_[j] - joint_position_[j] ); // joint_stiffness_command_[j]*( joint_position_command_[j] - joint_position_[j] );
00114                         //double damping_effort = joint_damping_command_[j]*( joint_velocity_[j] );
00115                         const double effort = stiffness_effort + joint_effort_command_[j] + gravity_effort_(j);
00116                         sim_joints_[j]->SetForce(0, effort);
00117                     }
00118                     break;
00119 #endif
00120                 default:
00121                     ROS_WARN("UNSUPPORTED CONTROL MODE");
00122                     break;
00123             }
00124         }
00125     private:
00126 
00127         // Gazebo stuff
00128         std::vector<gazebo::physics::JointPtr> sim_joints_;
00129         gazebo::physics::ModelPtr parent_model_;
00130         bool parent_set_;
00131 
00132 };
00133 
00134 
00135 #endif


yumi_hw
Author(s):
autogenerated on Sat Jun 8 2019 20:47:40