basic_move_controller.cpp
Go to the documentation of this file.
00001 /*
00002   Basic Move Controller - Provides simple robot behavior such as go forward backward
00003 
00004   LICENSE : BSD - https://raw.github.com/yujinrobot/yujin_ocs/license/LICENSE
00005  */
00006 #include "yocs_navigator/basic_move_controller.hpp" 
00007 
00008 namespace yocs_navigator {
00009 
00010 BasicMoveController::BasicMoveController(ros::NodeHandle& n) 
00011 : nh_(n), cmd_vel_topic_(BasicMoveControllerDefaultParam::PUB_CMD_VEL), odometry_topic_(BasicMoveControllerDefaultParam::SUB_ODOM)
00012 {
00013   init();
00014 }
00015 
00016 BasicMoveController::BasicMoveController(ros::NodeHandle& n, const std::string& cmd_vel_topic, const std::string& odometry_topic) 
00017 : nh_(n), cmd_vel_topic_(cmd_vel_topic), odometry_topic_(odometry_topic)
00018 {
00019   init();
00020 }
00021 
00022 BasicMoveController::~BasicMoveController()
00023 {
00024 }
00025 
00026 void BasicMoveController::init() 
00027 {
00028   pub_cmd_vel_ = nh_.advertise<geometry_msgs::Twist>(cmd_vel_topic_, 5);
00029   sub_odom_    = nh_.subscribe(odometry_topic_, 1, &BasicMoveController::processOdometry, this);
00030 }
00031 
00032 void BasicMoveController::processOdometry(const nav_msgs::Odometry::ConstPtr& msg)
00033 {   
00034   odometry_ = *msg;
00035 }   
00036 
00037 void BasicMoveController::slowForward()
00038 {
00039   moveAt( 0.1,  0.0,  0.1);
00040 }
00041 
00042 void BasicMoveController::slowBackward()
00043 {
00044   moveAt(-0.1,  0.0,  0.1);
00045 }
00046 
00047 void BasicMoveController::turnClockwise()
00048 {
00049   moveAt( 0.0, -0.5,  0.1);
00050 }
00051 
00052 void BasicMoveController::turnCounterClockwise()
00053 {
00054   moveAt( 0.0,  0.5,  0.1);
00055 }
00056 
00057 void BasicMoveController::stop()
00058 {
00059   moveAt( 0.0,  0.0,  0.0);
00060 }
00061 
00062 void BasicMoveController::moveAt(double v, double w, double t = 0.0)
00063 {
00064   geometry_msgs::Twist vel;
00065   vel.linear.x  = v;
00066   vel.angular.z = w;
00067   pub_cmd_vel_.publish(vel);
00068   ros::Duration(t).sleep();
00069 }
00070 
00071 void BasicMoveController::forward(double distance)
00072 {
00073   geometry_msgs::Point pos0 = odometry_.pose.pose.position;
00074   while (mtk::distance2D(pos0, odometry_.pose.pose.position) < distance)
00075   {
00076     slowForward();
00077   }
00078 }
00079 
00080 void BasicMoveController::backward(double distance)
00081 {
00082   geometry_msgs::Point pos0 = odometry_.pose.pose.position;
00083   while(mtk::distance2D(pos0, odometry_.pose.pose.position) < distance)
00084   {
00085     slowBackward();
00086   }
00087 }
00088 
00089 void BasicMoveController::turn(double angle)
00090 {
00091   double yaw0 = tf::getYaw(odometry_.pose.pose.orientation);
00092   double yaw1 = mtk::wrapAngle(yaw0 + angle);
00093 
00094   ROS_DEBUG("%f  %f  %f", angle, yaw0, yaw1);
00095   while (std::abs(mtk::wrapAngle(yaw1 - tf::getYaw(odometry_.pose.pose.orientation))) > 0.05)
00096     moveAt(0.0, mtk::sign(angle)*0.5, 0.05);
00097 }
00098 
00099 // aaaagh...  TODO make a decent version checking sign change and turns over 2pi! I have no time now
00100 void BasicMoveController::turn2(double angle)
00101 {
00102   double yaw0 = tf::getYaw(odometry_.pose.pose.orientation);
00103   double yaw1 = mtk::wrapAngle(yaw0 + angle);
00104   double sign = mtk::sign(yaw1 - yaw0);
00105 
00106   ROS_DEBUG("%f  %f  %f", angle, yaw0, yaw1);
00107   while (mtk::sign(mtk::wrapAngle(tf::getYaw(odometry_.pose.pose.orientation) - yaw1)) == sign)
00108   {
00109     moveAt(0.0, mtk::sign(angle)*0.5, 0.05);
00110   }
00111 }
00112 
00113 void BasicMoveController::spinClockwise()
00114 {
00115   // WARN; note that this requires that callbacks keep running!
00116   double yaw0 = tf::getYaw(odometry_.pose.pose.orientation);
00117 
00118   for (int i = 0; i < 5 || tf::getYaw(odometry_.pose.pose.orientation) <= yaw0; ++i)
00119     turnClockwise();
00120 
00121   for (int i = 0; i < 5 || tf::getYaw(odometry_.pose.pose.orientation) >  yaw0; ++i)
00122     turnClockwise();
00123 }
00124 
00125 void BasicMoveController::spinCounterClockwise()
00126 {
00127   // WARN; note that this requires that callbacks keep running!
00128   double yaw0 = tf::getYaw(odometry_.pose.pose.orientation);
00129 
00130   for (int i = 0; i < 5 || tf::getYaw(odometry_.pose.pose.orientation) >= yaw0; ++i)
00131     turnCounterClockwise();
00132 
00133   for (int i = 0; i < 5 || tf::getYaw(odometry_.pose.pose.orientation) <  yaw0; ++i)
00134     turnCounterClockwise();
00135 }
00136 
00137 } // namespace yocs


yocs_navigator
Author(s): Jihoon Lee, Jorge Simon Santos
autogenerated on Thu Jun 6 2019 21:47:35