warthog_suspension_plugin.cpp
Go to the documentation of this file.
1 
25 #include "ros/ros.h"
26 #include "gazebo/gazebo.hh"
27 #include "gazebo/physics/physics.hh"
28 #include "gazebo/common/common.hh"
29 #include "gazebo/transport/transport.hh"
30 #include <boost/bind.hpp>
31 #include <string>
32 
33 namespace gazebo
34 {
35  /*
36  * Define joint names.
37  */
38  static const std::string L_SUS_JNT = "left_diff_unit_joint";
39  static const std::string R_SUS_JNT = "right_diff_unit_joint";
40 
41  class WarthogSuspensionPlugin : public ModelPlugin
42  {
43  public:
48  {
49  int argc = 0;
50  char** argv = NULL;
51  ros::init(argc, argv, "warthog_suspension_plugin");
52 
53  // Initialize last_update_time_
55  }
56 
62  void Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
63  {
64  // Save the pointer to the model
65  this->model_ = _parent;
66 
67  // Load parameters
68  sus_kf_ = _sdf->Get<double>("suspension_k");
69  sus_bf_ = _sdf->Get<double>("suspension_b");
70 
71  // Retrieve the model joints
72  suspension_jnts_["left"] = model_->GetJoint(L_SUS_JNT);
73  suspension_jnts_["right"] = model_->GetJoint(R_SUS_JNT);
74 
75  // Listen for update event (every simulation iteration/step)
76  this->updateConnection_ = event::Events::ConnectWorldUpdateBegin(
77  boost::bind(&WarthogSuspensionPlugin::OnUpdate, this, _1) );
78  }
79 
84  void OnUpdate(const common::UpdateInfo& _info)
85  {
86  // Calculate time interval
87  ros::Time curr_time = ros::Time::now();
88  double elapsed_t = (curr_time - last_update_time_).toSec();
89  last_update_time_ = curr_time;
90 
91  // Apply the suspension forces based on current vehicle state
93  }
94 
95  private:
101  void applySuspension(double kf, double bf)
102  {
103  // Get the displacement and velocity of the left and right suspensions
104  double l_d = suspension_jnts_["left"]->GetAngle(0).Radian();
105  double r_d = suspension_jnts_["right"]->GetAngle(0).Radian();
106 
107  double l_v = suspension_jnts_["left"]->GetVelocity(0);
108  double r_v = suspension_jnts_["right"]->GetVelocity(0);
109 
110  // Set spring and damper forces on each suspension
111  suspension_jnts_["left"]->SetForce(0, -1*(kf*l_d)-(bf*l_v) );
112  suspension_jnts_["right"]->SetForce(0, -1*(kf*r_d)-(bf*r_v) );
113  }
114 
115  private:
116  std::map<std::string, physics::JointPtr> suspension_jnts_;
117  double sus_kf_, sus_bf_;
119  physics::ModelPtr model_;
120  event::ConnectionPtr updateConnection_;
121  };
122 
123  // Register plugin
125 }; // namespace gazebo
void Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
std::map< std::string, physics::JointPtr > suspension_jnts_
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
static const std::string R_SUS_JNT
void OnUpdate(const common::UpdateInfo &_info)
void applySuspension(double kf, double bf)
static Time now()
GZ_REGISTER_MODEL_PLUGIN(GazeboRosP3D)
static const std::string L_SUS_JNT


warthog_gazebo
Author(s): Ryan Gariepy
autogenerated on Sun Aug 16 2020 04:05:23