neo_platformctrl_node.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2011, Neobotix GmbH
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the Neobotix nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 
00036 #include <ros/ros.h>
00037 #include <boost/thread.hpp>
00038 #include <trajectory_msgs/JointTrajectory.h>
00039 #include <neo_PlatformCtrl/Kinematics.h>
00040 #include <neo_PlatformCtrl/DiffDrive2WKinematics.h>
00041 #include <tf/transform_broadcaster.h>
00042 
00043 class PlatformCtrlNode 
00044 {
00045         public:
00046         PlatformCtrlNode();
00047         virtual ~PlatformCtrlNode();
00048         ros::NodeHandle n;
00049         ros::Publisher topicPub_Odometry;       
00050         ros::Subscriber topicSub_DriveState;
00051         ros::Publisher topicPub_DriveCommands;  
00052         ros::Subscriber topicSub_ComVel;
00053         tf::TransformBroadcaster odom_broadcaster;
00054 
00055         int init();
00056         void receiveCmd(const geometry_msgs::Twist& twist);
00057         void receiveOdo(const sensor_msgs::JointState& js);
00058         private:
00059         boost::mutex mutex;
00060         OdomPose p;
00061         Kinematics* kin;
00062         bool sendTransform;
00063 };
00064 
00065 PlatformCtrlNode::PlatformCtrlNode()
00066 {
00067         kin = NULL;
00068 }
00069 
00070 PlatformCtrlNode::~PlatformCtrlNode()
00071 {
00072        if( kin != NULL ) delete kin;
00073 }
00074 
00075 
00076 int PlatformCtrlNode::init()
00077 {
00078         std::string kinType;
00079         n.param<bool>("sendTransform",sendTransform,false);
00080         if(sendTransform)
00081         {
00082                 ROS_INFO("platform ctrl node: sending transformation");
00083         } else {
00084 
00085                 ROS_INFO("platform ctrl node: sending no transformation");
00086         }
00087         n.getParam("kinematics", kinType);
00088 
00089         if (kinType == "DiffDrive2W")
00090         {
00091 
00092                 double wheelDiameter;
00093                 double axisLength;
00094                 DiffDrive2WKinematics* diffKin = new DiffDrive2WKinematics;
00095                 kin = diffKin;
00096                 if(n.hasParam("wheelDiameter"))
00097                 {
00098                         n.getParam("wheelDiameter",wheelDiameter);
00099                         diffKin->setWheelDiameter(wheelDiameter);
00100                         ROS_INFO("got wheeldieameter from config file");
00101                 }
00102                 else diffKin->setWheelDiameter(0.3);
00103                 if(n.hasParam("robotWidth"))
00104                 {
00105                         n.getParam("robotWidth",axisLength);
00106                         diffKin->setAxisLength(axisLength);
00107                         ROS_INFO("got axis from config file");
00108                 }
00109                 else diffKin->setAxisLength(1);
00110         }
00111         else
00112         {
00113                 ROS_ERROR("neo_PlatformCtrl-Error: unknown kinematic model");
00114 
00115         }
00116         if(kin == NULL) return 1;
00117         p.xAbs = 0; p.yAbs = 0; p.phiAbs = 0;
00118         topicPub_Odometry = n.advertise<nav_msgs::Odometry>("/odom",1); 
00119         topicSub_DriveState = n.subscribe("/drive_states",1,&PlatformCtrlNode::receiveOdo, this);
00120         topicPub_DriveCommands = n.advertise<trajectory_msgs::JointTrajectory>("/cmd_drives",1);
00121         topicSub_ComVel = n.subscribe("/cmd_vel",1,&PlatformCtrlNode::receiveCmd, this);
00122         return 0;
00123 }
00124 
00125 
00126 
00127 void PlatformCtrlNode::receiveCmd(const geometry_msgs::Twist& twist)
00128 {
00129    mutex.lock();
00130         trajectory_msgs::JointTrajectory traj;
00131         kin->execInvKin(twist,traj);
00132         topicPub_DriveCommands.publish(traj);
00133    mutex.unlock();
00134 }
00135 
00136 void PlatformCtrlNode::receiveOdo(const sensor_msgs::JointState& js)
00137 {
00138    mutex.lock();
00139         //odometry msgs
00140         nav_msgs::Odometry odom;
00141         odom.header.stamp = ros::Time::now();
00142         odom.header.frame_id = "odom";
00143         odom.child_frame_id = "base_link";
00144         kin->execForwKin(js, odom, p);
00145         topicPub_Odometry.publish(odom);
00146         //odometry transform:
00147         if(sendTransform)
00148         {
00149                 geometry_msgs::TransformStamped odom_trans;
00150                 odom_trans.header.stamp = odom.header.stamp;
00151                 odom_trans.header.frame_id = odom.header.frame_id;
00152                 odom_trans.child_frame_id = odom.child_frame_id;
00153                 odom_trans.transform.translation.x = odom.pose.pose.position.x;
00154                 odom_trans.transform.translation.y = odom.pose.pose.position.y;
00155                 odom_trans.transform.translation.z = odom.pose.pose.position.z;
00156                 odom_trans.transform.rotation = odom.pose.pose.orientation;
00157                 odom_broadcaster.sendTransform(odom_trans);
00158         }
00159    mutex.unlock();
00160 }
00161 
00162 
00163 int main (int argc, char** argv)
00164 {
00165         ros::init(argc, argv, "diff_node");
00166         PlatformCtrlNode node;
00167         if(node.init() != 0) ROS_ERROR("can't initialize neo_platformctrl_node");
00168         ros::spin();
00169         return 0;
00170 }
00171 
00172 


neo_platformctrl_diff
Author(s): Jan-Niklas Nieland
autogenerated on Thu Jun 6 2019 21:37:04