ros_plugin_demo
Demonstration of the ROS PlugInLib
 All Data Structures Namespaces Files Functions Variables Pages
path_planner.cpp
Go to the documentation of this file.
1 
6 
7 namespace plugin_demo_interface_namespace
8 {
10  {
11  init = false;
12  }
13 
14  void PathPlanner::initialize(ros::NodeHandle roshandle)
15  {
16  if (init) {
17  ROS_WARN("[interface] plugin was already initialized. Nothing will be done.");
18  } else {
19  getTargetSub = roshandle.subscribe("set_target", 10, &PathPlanner::getTargetCallback, this);
20  getStartSub = roshandle.subscribe("set_start", 10, &PathPlanner::getStarttCallback, this);
21  pathPub = roshandle.advertise<nav_msgs::Path>("path", 10);
22  sendPathTimer = roshandle.createTimer(ros::Duration(2.0), &PathPlanner::sendPathCallback, this);
23 
24  setPose2d(&start, 0, 0, 0);
25  target = start;
26  target.header.stamp = ros::Time::now();
27 
30 
31  current_path.header.stamp = ros::Time::now();
32  current_path.header.frame_id = "world";
33  current_path.poses.push_back(start);
34  current_path.poses.push_back(target);
35 
36  onInit(roshandle);
37  init = true;
38 
39  sendPath();
40  }
41  }
42 
43  int PathPlanner::onInit(ros::NodeHandle roshandle)
44  {
45  ROS_INFO("[interface] plugIn has no onInit function");
46  }
47 
48  void PathPlanner::setPose2d(geometry_msgs::PoseStamped *pose3d, double x, double y, double theta)
49  {
50  pose3d->header.stamp = ros::Time::now();
51  pose3d->pose.position.x = x;
52  pose3d->pose.position.y = y;
53  pose3d->pose.position.z = 0;
54  pose3d->pose.orientation.x = 0;
55  pose3d->pose.orientation.y = 0;
56  pose3d->pose.orientation.z = sin(theta * 0.5);
57  pose3d->pose.orientation.w = cos(theta * 0.5);
58  }
59 
60  int PathPlanner::setPoints(geometry_msgs::Pose2D *start, geometry_msgs::Pose2D *target)
61  {
62  setPose2d( &(this->start), start->x, start->y, start->theta);
63  setPose2d( &(this->target), target->x, target->y, target->theta);
64 
65  tfFrameFromPoseStamped(&(this->start), &start_tf);
66  tfFrameFromPoseStamped(&(this->target), &target_tf);
67 
68  current_path.poses.clear();
69  getPath();
70 
71  sendPath();
72 
73  return 0;
74  }
75 
76  int PathPlanner::getPoints(geometry_msgs::Pose2D *start, geometry_msgs::Pose2D *target)
77  {
78  start->x = this->start.pose.position.x;
79  start->y = this->start.pose.position.y;
80  start->theta = getYawFromQuat(this->start.pose.orientation);
81 
82  target->x = this->target.pose.position.x;
83  target->y = this->target.pose.position.y;
84  target->theta = getYawFromQuat(this->target.pose.orientation);
85 
86  return 0;
87  }
88 
89  double PathPlanner::getYawFromQuat(geometry_msgs::Quaternion quat)
90  {
91  //geometry_msgs::Quaternion quat = ;
92  tf::Quaternion q(quat.x, quat.y, quat.z, quat.w);
93  tf::Matrix3x3 m(q);
94  double roll, pitch, yaw;
95  m.getRPY(roll, pitch, yaw);
96 
97  return yaw;
98  }
99 
100  void PathPlanner::tfFrameFromPoseStamped(geometry_msgs::PoseStamped *pose, tf::Transform *tf_tf)
101  {
102  tf_tf->setOrigin( tf::Vector3(pose->pose.position.x, pose->pose.position.y, pose->pose.position.z) );
103  tf::Quaternion q(pose->pose.orientation.x, pose->pose.orientation.y, pose->pose.orientation.z, pose->pose.orientation.w);
104  tf_tf->setRotation(q);
105  }
106 
107  void PathPlanner::getStarttCallback(const geometry_msgs::Pose2D event)
108  {
109  setPose2d(&start, event.x, event.y, event.theta);
110 
112 
113  current_path.poses.clear();
114  getPath();
115 
116  sendPath();
117  }
118 
119  void PathPlanner::getTargetCallback(const geometry_msgs::PoseStamped event)
120  {
121  target = event;
122 
124 
125  current_path.poses.clear();
126  getPath();
127 
128  sendPath();
129  }
130 
131  void PathPlanner::sendPathCallback(const ros::TimerEvent& event)
132  {
133  sendPath();
134  }
135 
137  {
138  tf_br.sendTransform(tf::StampedTransform(start_tf, ros::Time::now(), "world", "start"));
139  tf_br.sendTransform(tf::StampedTransform(target_tf, ros::Time::now(), "world", "target"));
140 
141  current_path.header.stamp = ros::Time::now();
142  pathPub.publish(current_path);
143  }
144 }
ros::Timer sendPathTimer
periodic calling of the path publisher
void sendPathCallback(const ros::TimerEvent &event)
call sendPath()
void initialize(ros::NodeHandle roshandle)
Initalize the global variables.
int setPoints(geometry_msgs::Pose2D *start, geometry_msgs::Pose2D *target)
replace the current start and target poses and call getPath()
void getTargetCallback(const geometry_msgs::PoseStamped event)
receives a new target pose and call getPath().
ros::Publisher pathPub
send the path calculated by the plugIn
void getStarttCallback(const geometry_msgs::Pose2D event)
receives a new start pose and call getPath().
virtual int onInit(ros::NodeHandle roshandle)
In this function the plugIn can do some initialisation depending on a ROS NodeHandle.
double getYawFromQuat(geometry_msgs::Quaternion quat)
convert a quaternion into yaw
tf::TransformBroadcaster tf_br
send tf-transforms
void sendPath()
publish current_path and the start_tf and target_tf frames
void tfFrameFromPoseStamped(geometry_msgs::PoseStamped *pose, tf::Transform *tf_tf)
build a tf-frame for a given pose
int getPoints(geometry_msgs::Pose2D *start, geometry_msgs::Pose2D *target)
returns the current start and target poses
ros::Subscriber getStartSub
listen for a new start pose
tf::Transform start_tf
the start tf-frame
geometry_msgs::PoseStamped target
last pose of the path
ros::Subscriber getTargetSub
listen for a new target pose
Contain the Interface class.
nav_msgs::Path current_path
contain the path
tf::Transform target_tf
the target tf-frame
virtual int getPath()=0
calculate current_path from start and target inside a plugIn.
void setPose2d(geometry_msgs::PoseStamped *pose3d, double x, double y, double theta)
convert a given 2D-Pose into 3D-Stamped-Pose.
geometry_msgs::PoseStamped start
starting pose of the path