controller_node.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
4 
5 #define NSEC_2_SECS(A) ((float)A / 1000000000.0)
6 
7 int main(int argc, char** argv)
8 {
9  if (argc >= 2)
10  {
11  ros::init(argc, argv, argv[1]);
13 
15  ros::Rate r(20);
16 
17  while (ros::ok())
18  {
19  ros::spinOnce();
20  r.sleep();
21  }
22 
23  return 0;
24  }
25  else
26  {
27  ROS_INFO("Please specifie name \nrosrun simple_velocity_controller velocity_controller [name]");
28  }
29 }
30 
31 namespace velocity_controller
32 {
34 {
35  topic_pose_ = "pose";
36  n_param_.getParam("pose_topic", topic_pose_);
37 
38  topic_cmdVel_ = "cmd_vel";
39  n_param_.getParam("cmd_vel_topic", topic_cmdVel_);
40 
41  topic_path_ = "path";
42  n_param_.getParam("path_topic", topic_path_);
43 
44  max_vel_v_ = 0.8;
45  n_param_.getParam("max_v", max_vel_v_);
46 
47  max_vel_w_ = 1.0;
48  n_param_.getParam("max_w", max_vel_w_);
50 
51  goal_r_ = 0.2;
52  n_param_.getParam("goal_radius", goal_r_);
54 
55  Kp_val_ = 5.0;
56  n_param_.getParam("Kp", Kp_val_);
57 
58  Ki_val_ = 0.0;
59  n_param_.getParam("Ki", Ki_val_);
60 
61  Kd_val_ = 1.0;
62  n_param_.getParam("Kd", Kd_val_);
64 
65  topic_ctrl_ = "/ctrl";
66  n_param_.getParam("topic_control", topic_ctrl_);
67 
68  ROS_INFO("%s", topic_cmdVel_.c_str());
69 
70  pubCmdVel_ = n.advertise<geometry_msgs::Twist>(topic_cmdVel_, 1000);
74 }
75 
76 void ControllerNode::subPoseCb(const geometry_msgs::PoseWithCovarianceStampedConstPtr &_pose)
77 {
78  PathPoint pt;
79  pt.x = _pose->pose.pose.position.x;
80  pt.y = _pose->pose.pose.position.y;
81 
82  tf::Quaternion q(_pose->pose.pose.orientation.x, _pose->pose.pose.orientation.y, _pose->pose.pose.orientation.z,
83  _pose->pose.pose.orientation.w);
84  tf::Matrix3x3 m(q);
85  double roll, pitch, yaw;
86  m.getRPY(roll, pitch, yaw);
87 
88  pt.theta = yaw;
89 
90  ros::Time time = ros::Time::now();
91  ros::Duration d = time - last_update_;
92 
93  float delta_t = d.sec + NSEC_2_SECS(d.nsec);
94  update(pt, delta_t);
95 
96  geometry_msgs::Twist msg;
97 
98  float v, w;
99  getSpeed(&v, &w);
100  msg.linear.x = v;
101  msg.angular.z = w;
102 
103  pubCmdVel_.publish(msg);
104 }
105 
106 void ControllerNode::subPathCb(const nav_msgs::Path_<std::allocator<void>>::ConstPtr& _path)
107 {
108  if (_path->poses.size() == 0)
109  return;
110 
111  // start at nearest point on path to pose
112  // behavior controller resends full paths, therefore
113  // it is important to start from the robots location
114 
115  float nearest_dist = std::numeric_limits<float>::max();
116  float dist = 0;
117 
118  auto it = _path->poses.begin();
119  /*
120  bool changed = true;
121 
122  while (it != _path->poses.end() && changed)
123  {
124  dist = pow(current_pose_.x - it->pose.position.x, 2) + pow(current_pose_.y - it->pose.position.y, 2);
125  if (dist < nearest_dist)
126  {
127  nearest_dist = dist;
128  changed = true;
129  it++;
130  }
131  else
132  {
133  changed = false;
134  }
135  }*/
136 
137  std::vector<PathPoint> path;
138  for (; it != _path->poses.end(); ++it)
139  {
140  PathPoint pt;
141 
142  tf::Quaternion q(it->pose.orientation.x, it->pose.orientation.y, it->pose.orientation.z, it->pose.orientation.w);
143  tf::Matrix3x3 m(q);
144  double roll, pitch, yaw;
145  m.getRPY(roll, pitch, yaw);
146 
147  pt.x = it->pose.position.x;
148  pt.y = it->pose.position.y;
149  pt.theta = yaw;
150 
151  path.push_back(pt);
152  }
153 
154  setPath(std::make_shared<std::vector<PathPoint>>(path));
155 }
156 
157 void ControllerNode::subCtrlCb(const std_msgs::String _cmd)
158 {
159  std::string s = _cmd.data;
160 
161  ROS_INFO("Multi Robot Controller: received %s", s.c_str());
162  if (s.compare("run") == 0)
163  {
164  setState(run);
165  }
166  else if (s.compare("stop") == 0)
167  {
168  setState(stop);
169  }
170  else if (s.compare("step") == 0)
171  {
172  setState(step);
173  }
174  else
175  {
176  setState(run);
177  }
178 }
179 }
d
void publish(const boost::shared_ptr< M > &message) const
ros::NodeHandle n_param_
Node handler to the current node.
void setPath(std::shared_ptr< std::vector< PathPoint >> _path)
Set the path the contorler has to follow.
Definition: controller.cpp:14
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
XmlRpcServer s
int main(int argc, char **argv)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void subPoseCb(const geometry_msgs::PoseWithCovarianceStampedConstPtr &_pose)
void setState(state s)
Set the State (run stop step wait_step)
Definition: controller.cpp:58
void subPathCb(const nav_msgs::Path::ConstPtr &_path)
void setPID(float _Kp, float _Ki, float _Kd)
sets the control parameters
Definition: controller.cpp:51
void getSpeed(float *_v, float *_w)
Returns the current speed values.
Definition: controller.cpp:140
void subCtrlCb(const std_msgs::String _cmd)
void getRPY(tfScalar &roll, tfScalar &pitch, tfScalar &yaw, unsigned int solution_number=1) const
#define ROS_INFO(...)
void setSpeedParams(float _max_v, float _max_w)
Set the Speed Params of the controller.
Definition: controller.cpp:134
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ControllerNode(ros::NodeHandle &n)
Construct a new Controller Node object.
#define NSEC_2_SECS(A)
bool sleep()
TFSIMD_FORCE_INLINE const tfScalar & w() const
void setGoalRadius(float _r)
Set the Goal Radius.
Definition: controller.cpp:21
void update(PathPoint _odom, float _delta_t)
updates the controller with the current robot position
Definition: controller.cpp:63
bool getParam(const std::string &key, std::string &s) const
static Time now()
ROSCPP_DECL void spinOnce()


tuw_multi_robot_ctrl
Author(s): Benjamin Binder
autogenerated on Mon Jun 10 2019 15:42:29