ideal_motion_controller.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002    STDR Simulator - Simple Two DImensional Robot Simulator
00003    Copyright (C) 2013 STDR Simulator
00004    This program is free software; you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 3 of the License, or
00007    (at your option) any later version.
00008    This program is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011    GNU General Public License for more details.
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software Foundation,
00014    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015    
00016    Authors : 
00017    * Manos Tsardoulias, etsardou@gmail.com
00018    * Aris Thallas, aris.thallas@gmail.com
00019    * Chris Zalidis, zalidis@gmail.com 
00020 ******************************************************************************/
00021 
00022 #include <stdr_robot/motion/ideal_motion_controller.h>
00023 
00024 namespace stdr_robot {
00025     
00034   IdealMotionController::IdealMotionController(
00035     const geometry_msgs::Pose2D& pose, 
00036     tf::TransformBroadcaster& tf, 
00037     ros::NodeHandle& n, 
00038     const std::string& name)
00039       : MotionController(pose, tf, name)
00040   {
00041     _velocitySubscrider = n.subscribe(
00042       _namespace + "/cmd_vel", 
00043       1, 
00044       &IdealMotionController::velocityCallback, 
00045       this);
00046     
00047     _calcTimer = n.createTimer(
00048       _freq, 
00049       &IdealMotionController::calculateMotion, 
00050       this);
00051   }
00052 
00058   void IdealMotionController::velocityCallback(const geometry_msgs::Twist& msg) 
00059   {
00060     _currentTwist = msg;
00061   }
00062     
00067   void IdealMotionController::stop() 
00068   {
00069     _currentTwist.linear.x = 0;
00070     _currentTwist.angular.z = 0;
00071   }
00072 
00078   void IdealMotionController::calculateMotion(const ros::TimerEvent& event) 
00079   {
00081     
00082     ros::Duration dt = ros::Time::now() - event.last_real;
00083     
00084     if (_currentTwist.angular.z == 0) {
00085       
00086       _pose.x += _currentTwist.linear.x * dt.toSec() * cosf(_pose.theta);
00087       _pose.y += _currentTwist.linear.x * dt.toSec() * sinf(_pose.theta);
00088     }
00089     else {
00090       
00091       _pose.x += - _currentTwist.linear.x / _currentTwist.angular.z * 
00092         sinf(_pose.theta) + 
00093         _currentTwist.linear.x / _currentTwist.angular.z * 
00094         sinf(_pose.theta + dt.toSec() * _currentTwist.angular.z);
00095       
00096       _pose.y -= - _currentTwist.linear.x / _currentTwist.angular.z * 
00097         cosf(_pose.theta) + 
00098         _currentTwist.linear.x / _currentTwist.angular.z * 
00099         cosf(_pose.theta + dt.toSec() * _currentTwist.angular.z);
00100     }
00101     _pose.theta += _currentTwist.angular.z * dt.toSec();
00102   }
00103   
00108   IdealMotionController::~IdealMotionController(void)
00109   {
00110     
00111   }
00112     
00113 }


stdr_robot
Author(s): Manos Tsardoulias, Chris Zalidis, Aris Thallas
autogenerated on Wed Sep 2 2015 03:36:25