Advertisement
Guest User

Untitled

a guest
Mar 6th, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.21 KB | None | 0 0
  1. /*
  2.  * Copyright 2012 Open Source Robotics Foundation
  3.  * Copyright 2013 Dereck Wonnacott
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  *
  17. */
  18.  
  19. #include "physics/physics.hh"
  20. #include "transport/transport.hh"
  21. #include "plugins/SkidSteerDrivePlugin.hh"
  22.  
  23. using namespace gazebo;
  24. GZ_REGISTER_MODEL_PLUGIN(SkidSteerDrivePlugin)
  25.  
  26.  
  27. /////////////////////////////////////////////////
  28. SkidSteerDrivePlugin::SkidSteerDrivePlugin()
  29. {
  30.   this->MaxForce = 5.0;
  31.   this->wheelRadius = 0.0;
  32.   this->wheelSeparation = 0.0;
  33.   this->fatal_error = false;
  34. }
  35.  
  36. /////////////////////////////////////////////////
  37. void SkidSteerDrivePlugin::RegisterJoint(int index, std::string name)
  38. {  
  39.   this->Joints[index] = this->model->GetJoint(name);
  40.  
  41.   if (this->Joints[index]) return;
  42.  
  43.   gzerr << "Unable to find the " << name
  44.         <<  " joint in model " << this->model->GetName() << "." << std::endl;
  45.   this->fatal_error = true;
  46. }
  47.  
  48. /////////////////////////////////////////////////
  49. void SkidSteerDrivePlugin::Load(physics::ModelPtr _model,
  50.                            sdf::ElementPtr _sdf)
  51. {
  52.   this->model = _model;
  53.  
  54.   this->node = transport::NodePtr(new transport::Node());
  55.   this->node->Init(this->model->GetWorld()->GetName());
  56.  
  57.   this->velSub = this->node->Subscribe(
  58.     std::string("~/") + this->model->GetName() + std::string("/vel_cmd"),
  59.     &SkidSteerDrivePlugin::OnVelMsg, this);
  60.  
  61.   RegisterJoint(RIGHT_FRONT, "right_front");
  62.   RegisterJoint(RIGHT_REAR,  "right_rear");
  63.   RegisterJoint(LEFT_FRONT,  "left_front");
  64.   RegisterJoint(LEFT_REAR,   "left_rear");
  65.  
  66.   if (_sdf->HasElement("MaxForce"))
  67.     this->MaxForce = _sdf->GetElement("MaxForce")->GetValueDouble();
  68.   else
  69.     gzwarn << "No MaxForce value set in the model sdf, default value is 5.0.\n";
  70.  
  71.   // This assumes that front and rear wheel spacing is identical
  72.   this->wheelSeparation = this->Joints[RIGHT_FRONT]->GetAnchor(0).Distance(
  73.                           this->Joints[LEFT_FRONT]->GetAnchor(0));                
  74.                      
  75.   // This assumes that the largest dimension of the wheel is the diameter
  76.   // and that all wheels have the same diameter
  77.   physics::EntityPtr wheel_link = boost::shared_dynamic_cast<physics::Entity>(this->Joints[RIGHT_FRONT]->GetChild());
  78.   if(wheel_link)
  79.   {
  80.     math::Box bb = wheel_link->GetBoundingBox();
  81.     this->wheelRadius = bb.GetSize().GetMax() * 0.5;
  82.   }
  83.  
  84.   // Stupid checks...
  85.   if (this->wheelSeparation <= 0)
  86.   {
  87.     gzerr << "Unable to find the wheel separation distance." << std::endl
  88.           << "  This could mean that the right_front link and the left_front link are overlapping." << std::endl;
  89.     this->fatal_error = true;
  90.   }  
  91.   if (this->wheelRadius <= 0)
  92.   {
  93.     gzerr << "Unable to find the wheel radius." << std::endl
  94.           << "  This could mean that the sdf is missing a wheel link on the right_front joint." << std::endl;
  95.     this->fatal_error = true;
  96.   }
  97. }
  98.  
  99. /////////////////////////////////////////////////
  100. void SkidSteerDrivePlugin::OnVelMsg(ConstPosePtr &msg)
  101. {
  102.   gzmsg << "cmd_vel: " << msg->position().x() << ", " << msg->position().z();
  103.  
  104.   if(fatal_error) return;
  105.  
  106.   for(int i = 0; i < NUMBER_OF_WHEELS; i++)
  107.     this->Joints[i]->SetMaxForce(0, this->MaxForce);
  108.  
  109.   double vel_lin = msg->position().x() / this->wheelRadius;
  110.   double vel_rot = msg->position().z()
  111.   // msgs::Convert(msg->orientation()).GetAsEuler().z
  112.                    * (this->wheelSeparation / this->wheelRadius);
  113.  
  114.   this->Joints[RIGHT_FRONT]->SetVelocity(0, vel_lin - vel_rot);
  115.   this->Joints[RIGHT_REAR ]->SetVelocity(0, vel_lin - vel_rot);
  116.   this->Joints[LEFT_FRONT ]->SetVelocity(0, vel_lin + vel_rot);
  117.   this->Joints[LEFT_REAR  ]->SetVelocity(0, vel_lin + vel_rot);
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement