ConveyorBeltPlugin.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012-2016 Open Source Robotics Foundation
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *
00016 */
00017 
00018 #include <boost/algorithm/string/replace.hpp>
00019 #include <string>
00020 
00021 #include "ConveyorBeltPlugin.hh"
00022 #include <gazebo/transport/Node.hh>
00023 #include <gazebo/transport/Publisher.hh>
00024 
00025 using namespace gazebo;
00026 GZ_REGISTER_MODEL_PLUGIN(ConveyorBeltPlugin)
00027 
00028 
00029 ConveyorBeltPlugin::ConveyorBeltPlugin() : SideContactPlugin()
00030 {
00031 }
00032 
00034 ConveyorBeltPlugin::~ConveyorBeltPlugin()
00035 {
00036   event::Events::DisconnectWorldUpdateBegin(this->updateConnection);
00037   this->parentSensor.reset();
00038   this->world.reset();
00039 }
00040 
00042 std::string ConveyorBeltPlugin::Topic(std::string topicName) const
00043 {
00044   std::string globalTopicName = "~/";
00045   globalTopicName += this->parentSensor->Name() + "/" + this->GetHandle() + "/" + topicName;
00046   boost::replace_all(globalTopicName, "::", "/");
00047 
00048   return globalTopicName;
00049 }
00050 
00052 void ConveyorBeltPlugin::Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
00053 {
00054   SideContactPlugin::Load(_model, _sdf);
00055 
00056   if (!this->node)
00057   {
00058     this->node = transport::NodePtr(new transport::Node());
00059     this->node->Init(this->world->GetName());
00060   }
00061 
00062   if (_sdf->HasElement("belt_start_velocity"))
00063   {
00064     this->beltVelocity = _sdf->Get<double>("belt_start_velocity");
00065   }
00066   else {
00067     this->beltVelocity = 0.5;
00068   }
00069   gzdbg << "Using belt start velocity of: " << this->beltVelocity << " m/s\n";
00070 
00071   std::string controlCommandTopic;
00072   if (_sdf->HasElement("control_command_topic"))
00073   {
00074     controlCommandTopic = _sdf->Get<std::string>("control_command_topic");
00075   }
00076   else {
00077     controlCommandTopic = this->Topic("control_command");
00078   }
00079   this->controlCommandSub = this->node->Subscribe(controlCommandTopic,
00080       &ConveyorBeltPlugin::OnControlCommand, this);
00081 
00082   if (_sdf->HasElement("velocity_axis"))
00083   {
00084     this->velocityAxis = _sdf->Get<ignition::math::Vector3d>("velocity_axis");
00085   }
00086   else
00087   {
00088     gzerr << "'velocity_axis' tag not found\n";
00089   }
00090 }
00091 
00093 void ConveyorBeltPlugin::OnUpdate(const common::UpdateInfo &/*_info*/)
00094 {
00095   auto prevNumberContactingLinks = this->contactingLinks.size();
00096   this->CalculateContactingLinks();
00097   if (prevNumberContactingLinks != this->contactingLinks.size()) {
00098     gzdbg << "Number of links ontop of belt: " << this->contactingLinks.size() << "\n";
00099   }
00100 
00101   std::lock_guard<std::mutex> lock(this->mutex);
00102   double velocity = this->beltVelocity;
00103   this->ActOnContactingLinks(velocity);
00104 }
00105 
00107 void ConveyorBeltPlugin::ActOnContactingLinks(double velocity)
00108 {
00109   ignition::math::Vector3d velocity_beltFrame = velocity * this->velocityAxis;
00110   auto beltPose = this->parentLink->GetWorldPose().Ign();
00111   math::Vector3 velocity_worldFrame = beltPose.Rot().RotateVector(velocity_beltFrame);
00112   for (auto linkPtr : this->contactingLinks) {
00113     if (linkPtr) {
00114       linkPtr->SetLinearVel(velocity_worldFrame);
00115     }
00116   }
00117 }
00118 
00120 void ConveyorBeltPlugin::OnControlCommand(ConstHeaderPtr& _msg)
00121 {
00122   double requestedVelocity = std::stod(_msg->str_id());
00123   gzdbg << "Received control command of: " << requestedVelocity << "\n";
00124   this->SetVelocity(requestedVelocity);
00125 }
00126 
00128 void ConveyorBeltPlugin::SetVelocity(double velocity)
00129 {
00130   std::lock_guard<std::mutex> lock(this->mutex);
00131   gzdbg << "Setting velocity to: " << velocity << "\n";
00132   this->beltVelocity = velocity;
00133 }


osrf_gear
Author(s):
autogenerated on Mon Sep 5 2016 03:41:33