turtlebot3_drive.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2016 ROBOTIS CO., LTD.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16 
17 /* Authors: Taehun Lim (Darby) */
18 
20 
22  : nh_priv_("~")
23 {
24  //Init gazebo ros turtlebot3 node
25  ROS_INFO("TurtleBot3 Simulation Node Init");
26  ROS_ASSERT(init());
27 }
28 
30 {
31  updatecommandVelocity(0.0, 0.0);
32  ros::shutdown();
33 }
34 
35 /*******************************************************************************
36 * Init function
37 *******************************************************************************/
39 {
40  // initialize ROS parameter
41  nh_.param("is_debug", is_debug_, is_debug_);
42  std::string robot_model = nh_.param<std::string>("tb3_model", "");
43  std::string cmd_vel_topic_name = nh_.param<std::string>("cmd_vel_topic_name", "");
44 
45  if (!robot_model.compare("burger"))
46  {
47  turning_radius_ = 0.08;
48  rotate_angle_ = 50.0 * DEG2RAD;
51  }
52  else if (!robot_model.compare("waffle") || !robot_model.compare("waffle_pi"))
53  {
54  turning_radius_ = 0.1435;
55  rotate_angle_ = 40.0 * DEG2RAD;
58  }
59 
60  ROS_INFO("robot_model : %s", robot_model.c_str());
61  ROS_INFO("turning_radius_ : %lf", turning_radius_);
62  ROS_INFO("front_distance_limit_ = %lf", front_distance_limit_);
63  ROS_INFO("side_distance_limit_ = %lf", side_distance_limit_);
64 
65  // initialize variables
68  // initialize publishers
69  cmd_vel_pub_ = nh_.advertise<geometry_msgs::Twist>(cmd_vel_topic_name, 10);
70 
71  // initialize subscribers
74 
75  return true;
76 }
77 
78 void Turtlebot3Drive::jointStateMsgCallBack(const sensor_msgs::JointState::ConstPtr &msg)
79 {
80  right_joint_encoder_ = msg->position.at(0);
81 }
82 
83 void Turtlebot3Drive::laserScanMsgCallBack(const sensor_msgs::LaserScan::ConstPtr &msg)
84 {
85  uint16_t scan_angle[3] = {0, 30, 330};
86 
87  for (int num = 0; num < 3; num++)
88  {
89  if (std::isinf(msg->ranges.at(scan_angle[num])))
90  {
91  direction_vector_[num] = msg->range_max;
92  }
93  else
94  {
95  direction_vector_[num] = msg->ranges.at(scan_angle[num]);
96  }
97  }
98 }
99 
100 void Turtlebot3Drive::updatecommandVelocity(double linear, double angular)
101 {
102  geometry_msgs::Twist cmd_vel;
103 
104  cmd_vel.linear.x = linear;
105  cmd_vel.angular.z = angular;
106 
107  cmd_vel_pub_.publish(cmd_vel);
108 }
109 
110 /*******************************************************************************
111 * Control Loop function
112 *******************************************************************************/
114 {
115  static uint8_t turtlebot3_state_num = 0;
116  double wheel_radius = 0.033;
117  double turtlebot3_rotation = 0.0;
118 
119  turtlebot3_rotation = (rotate_angle_ * turning_radius_ / wheel_radius);
120 
121  switch(turtlebot3_state_num)
122  {
123  case GET_TB3_DIRECTION:
125  {
126  turtlebot3_state_num = TB3_DRIVE_FORWARD;
127  }
128 
130  {
131  priv_right_joint_encoder_ = right_joint_encoder_ - turtlebot3_rotation;
132  turtlebot3_state_num = TB3_RIGHT_TURN;
133  }
135  {
136  priv_right_joint_encoder_ = right_joint_encoder_ + turtlebot3_rotation;
137  turtlebot3_state_num = TB3_LEFT_TURN;
138  }
139  break;
140 
141  case TB3_DRIVE_FORWARD:
143  turtlebot3_state_num = GET_TB3_DIRECTION;
144  break;
145 
146  case TB3_RIGHT_TURN:
147  if (priv_right_joint_encoder_ == 0.0)
148  {
149  turtlebot3_state_num = GET_TB3_DIRECTION;
150  }
151  else
152  {
154  {
155  turtlebot3_state_num = GET_TB3_DIRECTION;
156  }
157  else
158  {
160  }
161  }
162  break;
163 
164  case TB3_LEFT_TURN:
165  if (priv_right_joint_encoder_ == 0.0)
166  {
167  turtlebot3_state_num = GET_TB3_DIRECTION;
168  }
169  else
170  {
172  {
173  turtlebot3_state_num = GET_TB3_DIRECTION;
174  }
175  else
176  {
178  }
179  }
180  break;
181 
182  default:
183  turtlebot3_state_num = GET_TB3_DIRECTION;
184  break;
185  }
186 
187  return true;
188 }
189 
190 /*******************************************************************************
191 * Main function
192 *******************************************************************************/
193 int main(int argc, char* argv[])
194 {
195  ros::init(argc, argv, "turtlebot3_drive");
196  Turtlebot3Drive turtlebot3_drive;
197 
198  ros::Rate loop_rate(125);
199 
200  while (ros::ok())
201  {
202  turtlebot3_drive.controlLoop();
203  ros::spinOnce();
204  loop_rate.sleep();
205  }
206 
207  return 0;
208 }
void laserScanMsgCallBack(const sensor_msgs::LaserScan::ConstPtr &msg)
#define TB3_RIGHT_TURN
#define CENTER
#define RIGHT
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())
double right_joint_encoder_
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void jointStateMsgCallBack(const sensor_msgs::JointState::ConstPtr &msg)
ros::NodeHandle nh_
double front_distance_limit_
double direction_vector_[3]
#define TB3_DRIVE_FORWARD
#define ANGULAR_VELOCITY
int main(int argc, char *argv[])
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
ROSCPP_DECL bool ok()
ros::Publisher cmd_vel_pub_
#define LINEAR_VELOCITY
#define LEFT
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool sleep()
#define DEG2RAD
double side_distance_limit_
#define GET_TB3_DIRECTION
ros::Subscriber joint_state_sub_
ROSCPP_DECL void shutdown()
void updatecommandVelocity(double linear, double angular)
double priv_right_joint_encoder_
#define TB3_LEFT_TURN
#define ROS_ASSERT(cond)
ros::Subscriber laser_scan_sub_
ROSCPP_DECL void spinOnce()


turtlebot3_gazebo_ros
Author(s): Pyo , Darby Lim
autogenerated on Fri Mar 16 2018 02:54:56