warthog_suspension_plugin.cpp
Go to the documentation of this file.
00001 
00025 #include "ros/ros.h"
00026 #include "gazebo/gazebo.hh"
00027 #include "gazebo/physics/physics.hh"
00028 #include "gazebo/common/common.hh"
00029 #include "gazebo/transport/transport.hh"
00030 #include <boost/bind.hpp>
00031 #include <string>
00032 
00033 namespace gazebo
00034 {
00035   /*
00036    * Define joint names.
00037    */
00038   static const std::string L_SUS_JNT = "left_diff_unit_joint";
00039   static const std::string R_SUS_JNT = "right_diff_unit_joint";
00040 
00041   class WarthogSuspensionPlugin : public ModelPlugin
00042   {
00043   public:
00047     WarthogSuspensionPlugin()
00048     {
00049       int argc = 0;
00050       char** argv = NULL;
00051       ros::init(argc, argv, "warthog_suspension_plugin");
00052 
00053       // Initialize last_update_time_
00054       last_update_time_ = ros::Time::now();
00055     }
00056 
00062     void Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
00063     {
00064       // Save the pointer to the model
00065       this->model_ = _parent;
00066 
00067       // Load parameters
00068       sus_kf_ = _sdf->Get<double>("suspension_k");
00069       sus_bf_ = _sdf->Get<double>("suspension_b");
00070 
00071       // Retrieve the model joints
00072       suspension_jnts_["left"] = model_->GetJoint(L_SUS_JNT);
00073       suspension_jnts_["right"] = model_->GetJoint(R_SUS_JNT);
00074 
00075       // Listen for update event (every simulation iteration/step)
00076       this->updateConnection_ = event::Events::ConnectWorldUpdateBegin(
00077           boost::bind(&WarthogSuspensionPlugin::OnUpdate, this, _1) );
00078     }
00079 
00084     void OnUpdate(const common::UpdateInfo& _info)
00085     {
00086       // Calculate time interval
00087       ros::Time curr_time = ros::Time::now();
00088       double elapsed_t = (curr_time - last_update_time_).toSec();
00089       last_update_time_ = curr_time;
00090 
00091       // Apply the suspension forces based on current vehicle state
00092       applySuspension(sus_kf_, sus_bf_);
00093     }
00094 
00095   private:
00101   void applySuspension(double kf, double bf)
00102   {
00103     // Get the displacement and velocity of the left and right suspensions
00104     double l_d = suspension_jnts_["left"]->GetAngle(0).Radian();
00105     double r_d = suspension_jnts_["right"]->GetAngle(0).Radian();
00106 
00107     double l_v = suspension_jnts_["left"]->GetVelocity(0);
00108     double r_v = suspension_jnts_["right"]->GetVelocity(0);
00109 
00110     // Set spring and damper forces on each suspension
00111     suspension_jnts_["left"]->SetForce(0, -1*(kf*l_d)-(bf*l_v) );
00112     suspension_jnts_["right"]->SetForce(0, -1*(kf*r_d)-(bf*r_v) );
00113   }
00114 
00115   private:
00116     std::map<std::string, physics::JointPtr> suspension_jnts_;
00117     double sus_kf_, sus_bf_;
00118     ros::Time last_update_time_;
00119     physics::ModelPtr model_;
00120     event::ConnectionPtr updateConnection_;
00121   };
00122 
00123   // Register plugin
00124   GZ_REGISTER_MODEL_PLUGIN(WarthogSuspensionPlugin);
00125 };  // namespace gazebo


warthog_gazebo
Author(s): Ryan Gariepy
autogenerated on Sat Jun 8 2019 18:55:55