00001 /* 00002 Copyright (c) 2010, Daniel Hewlett, Antons Rebguns 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without 00006 modification, are permitted provided that the following conditions are met: 00007 * Redistributions of source code must retain the above copyright 00008 notice, this list of conditions and the following disclaimer. 00009 * Redistributions in binary form must reproduce the above copyright 00010 notice, this list of conditions and the following disclaimer in the 00011 documentation and/or other materials provided with the distribution. 00012 * Neither the name of the <organization> nor the 00013 names of its contributors may be used to endorse or promote products 00014 derived from this software without specific prior written permission. 00015 00016 THIS SOFTWARE IS PROVIDED BY Antons Rebguns <email> ''AS IS'' AND ANY 00017 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00018 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00019 DISCLAIMED. IN NO EVENT SHALL Antons Rebguns <email> BE LIABLE FOR ANY 00020 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00021 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00022 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00023 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00024 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00025 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 */ 00027 00028 #include <ros/ros.h> 00029 00030 #include <gazebo/common/common.h> 00031 #include <gazebo/physics/physics.h> 00032 00033 #include <tf/transform_broadcaster.h> 00034 #include <tf/transform_listener.h> 00035 #include <geometry_msgs/Twist.h> 00036 #include <nav_msgs/Odometry.h> 00037 00038 // Custom Callback Queue 00039 #include <ros/callback_queue.h> 00040 #include <ros/advertise_options.h> 00041 00042 #include <boost/thread.hpp> 00043 #include <boost/bind.hpp> 00044 00045 namespace gazebo 00046 { 00047 00048 enum 00049 { 00050 RIGHT_WHEEL, 00051 LEFT_WHEEL, 00052 }; 00053 00054 class DiffDrivePlugin : public ModelPlugin 00055 { 00056 00057 public: 00058 00059 DiffDrivePlugin(); 00060 ~DiffDrivePlugin(); 00061 00062 protected: 00063 virtual void Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf); 00064 virtual void Update(); 00065 00066 private: 00067 void writePositionData(); 00068 void publishOdometry(); 00069 void GetPositionCmd(); 00070 00071 physics::WorldPtr world; 00072 physics::ModelPtr parent; 00073 event::ConnectionPtr updateConnection; 00074 00075 std::string leftJointName; 00076 std::string rightJointName; 00077 00078 double wheelSeparation; 00079 double wheelDiameter; 00080 double torque; 00081 double wheelSpeed[2]; 00082 00083 double odomPose[3]; 00084 double odomVel[3]; 00085 00086 physics::JointPtr joints[2]; 00087 physics::PhysicsEnginePtr physicsEngine; 00088 00089 // ROS STUFF 00090 ros::NodeHandle* rosnode_; 00091 ros::Publisher pub_; 00092 ros::Subscriber sub_; 00093 tf::TransformBroadcaster *transform_broadcaster_; 00094 nav_msgs::Odometry odom_; 00095 std::string tf_prefix_; 00096 00097 boost::mutex lock; 00098 00099 std::string robotNamespace; 00100 std::string cmdVelTopicName; 00101 std::string odomTopicName; 00102 00103 // Custom Callback Queue 00104 ros::CallbackQueue queue_; 00105 boost::thread callback_queue_thread_; 00106 void QueueThread(); 00107 00108 // DiffDrive stuff 00109 void cmdVelCallback(const geometry_msgs::Twist::ConstPtr& cmd_msg); 00110 00111 double x_; 00112 double rot_; 00113 bool alive_; 00114 }; 00115 00116 } 00117