basic_move_controller.cpp
Go to the documentation of this file.
1 /*
2  Basic Move Controller - Provides simple robot behavior such as go forward backward
3 
4  LICENSE : BSD - https://raw.github.com/yujinrobot/yujin_ocs/license/LICENSE
5  */
7 
8 namespace yocs_navigator {
9 
11 : nh_(n), cmd_vel_topic_(BasicMoveControllerDefaultParam::PUB_CMD_VEL), odometry_topic_(BasicMoveControllerDefaultParam::SUB_ODOM)
12 {
13  init();
14 }
15 
16 BasicMoveController::BasicMoveController(ros::NodeHandle& n, const std::string& cmd_vel_topic, const std::string& odometry_topic)
17 : nh_(n), cmd_vel_topic_(cmd_vel_topic), odometry_topic_(odometry_topic)
18 {
19  init();
20 }
21 
23 {
24 }
25 
27 {
28  pub_cmd_vel_ = nh_.advertise<geometry_msgs::Twist>(cmd_vel_topic_, 5);
30 }
31 
32 void BasicMoveController::processOdometry(const nav_msgs::Odometry::ConstPtr& msg)
33 {
34  odometry_ = *msg;
35 }
36 
38 {
39  moveAt( 0.1, 0.0, 0.1);
40 }
41 
43 {
44  moveAt(-0.1, 0.0, 0.1);
45 }
46 
48 {
49  moveAt( 0.0, -0.5, 0.1);
50 }
51 
53 {
54  moveAt( 0.0, 0.5, 0.1);
55 }
56 
58 {
59  moveAt( 0.0, 0.0, 0.0);
60 }
61 
62 void BasicMoveController::moveAt(double v, double w, double t = 0.0)
63 {
64  geometry_msgs::Twist vel;
65  vel.linear.x = v;
66  vel.angular.z = w;
67  pub_cmd_vel_.publish(vel);
68  ros::Duration(t).sleep();
69 }
70 
71 void BasicMoveController::forward(double distance)
72 {
73  geometry_msgs::Point pos0 = odometry_.pose.pose.position;
74  while (mtk::distance2D(pos0, odometry_.pose.pose.position) < distance)
75  {
76  slowForward();
77  }
78 }
79 
80 void BasicMoveController::backward(double distance)
81 {
82  geometry_msgs::Point pos0 = odometry_.pose.pose.position;
83  while(mtk::distance2D(pos0, odometry_.pose.pose.position) < distance)
84  {
85  slowBackward();
86  }
87 }
88 
89 void BasicMoveController::turn(double angle)
90 {
91  double yaw0 = tf::getYaw(odometry_.pose.pose.orientation);
92  double yaw1 = mtk::wrapAngle(yaw0 + angle);
93 
94  ROS_DEBUG("%f %f %f", angle, yaw0, yaw1);
95  while (std::abs(mtk::wrapAngle(yaw1 - tf::getYaw(odometry_.pose.pose.orientation))) > 0.05)
96  moveAt(0.0, mtk::sign(angle)*0.5, 0.05);
97 }
98 
99 // aaaagh... TODO make a decent version checking sign change and turns over 2pi! I have no time now
100 void BasicMoveController::turn2(double angle)
101 {
102  double yaw0 = tf::getYaw(odometry_.pose.pose.orientation);
103  double yaw1 = mtk::wrapAngle(yaw0 + angle);
104  double sign = mtk::sign(yaw1 - yaw0);
105 
106  ROS_DEBUG("%f %f %f", angle, yaw0, yaw1);
107  while (mtk::sign(mtk::wrapAngle(tf::getYaw(odometry_.pose.pose.orientation) - yaw1)) == sign)
108  {
109  moveAt(0.0, mtk::sign(angle)*0.5, 0.05);
110  }
111 }
112 
114 {
115  // WARN; note that this requires that callbacks keep running!
116  double yaw0 = tf::getYaw(odometry_.pose.pose.orientation);
117 
118  for (int i = 0; i < 5 || tf::getYaw(odometry_.pose.pose.orientation) <= yaw0; ++i)
119  turnClockwise();
120 
121  for (int i = 0; i < 5 || tf::getYaw(odometry_.pose.pose.orientation) > yaw0; ++i)
122  turnClockwise();
123 }
124 
126 {
127  // WARN; note that this requires that callbacks keep running!
128  double yaw0 = tf::getYaw(odometry_.pose.pose.orientation);
129 
130  for (int i = 0; i < 5 || tf::getYaw(odometry_.pose.pose.orientation) >= yaw0; ++i)
132 
133  for (int i = 0; i < 5 || tf::getYaw(odometry_.pose.pose.orientation) < yaw0; ++i)
135 }
136 
137 } // namespace yocs
double wrapAngle(double a)
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
static double getYaw(const Quaternion &bt_q)
bool sleep() const
void moveAt(double v, double w, double t)
double distance2D(double ax, double ay, double bx, double by)
T sign(T x)
void processOdometry(const nav_msgs::Odometry::ConstPtr &msg)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
#define ROS_DEBUG(...)


yocs_navigator
Author(s): Jihoon Lee, Jorge Simon Santos
autogenerated on Mon Jun 10 2019 15:53:58