Go to the documentation of this file.00001 #include <ros/ros.h>
00002 #include <tf/transform_listener.h>
00003 #include "geometry_msgs/Twist.h"
00004
00005 #define RATE 20
00006
00007 int main(int argc, char** argv){
00008 ros::init(argc, argv, "vel_control");
00009
00010 std::string proj_tf_frame;
00011 double p_lin, p_ang, vmax_lin,vmax_ang;
00012
00013 ros::NodeHandle node;
00014
00015 node.param<std::string>("projection_frame", proj_tf_frame, "fixed_goal");
00016 node.param("p_lin", p_lin, 0.5);
00017 node.param("p_ang", p_ang, 1.0);
00018 node.param("vmax_lin", vmax_lin, 0.3);
00019 node.param("vmax_ang", vmax_ang, 0.5);
00020
00021 ros::Publisher vel_msg_pub = node.advertise<geometry_msgs::Twist>("cmd_vel", 10);
00022
00023 tf::TransformListener listener;
00024
00025 ros::Rate rate(RATE);
00026
00027 bool foundOnce = false;
00028 sleep(5);
00029
00030 while (node.ok()){
00031
00032 tf::StampedTransform transform;
00033 geometry_msgs::Twist vel_msg;
00034
00035 try {
00036 if (!foundOnce)
00037 {
00038 ros::Time now = ros::Time::now();
00039 listener.waitForTransform("base_footprint", proj_tf_frame, now, ros::Duration(5.0) );
00040 }
00041
00042 foundOnce = true;
00043 listener.lookupTransform("base_footprint", proj_tf_frame, ros::Time(0), transform);
00044
00045 if(fabs(transform.getOrigin().x()) > 0.15)
00046 {
00047 vel_msg.linear.x = p_lin * transform.getOrigin().x();
00048 }
00049 else
00050 {
00051 vel_msg.linear.x = 0.0;
00052 }
00053 tfScalar roll, pitch, yaw;
00054 tf::Matrix3x3(transform.getRotation()).getRPY(roll, pitch, yaw);
00055 vel_msg.angular.z = p_ang * yaw;
00056
00057 if(fabs(vel_msg.linear.x) > vmax_lin) vel_msg.linear.x = vmax_lin * vel_msg.linear.x/fabs(vel_msg.linear.x);
00058 if(fabs(vel_msg.angular.z) > vmax_ang) vel_msg.angular.z = vmax_ang * vel_msg.angular.z/fabs(vel_msg.angular.z);
00059
00060 } catch (tf::TransformException ex) {
00061 vel_msg.linear.x = 0.0;
00062 vel_msg.angular.z = 0.0;
00063 }
00064
00065 vel_msg_pub.publish(vel_msg);
00066
00067 ros::spinOnce();
00068 rate.sleep();
00069 }
00070
00071 return 0;
00072
00073 };