carrot_teleop.cpp
Go to the documentation of this file.
00001 #include "ros/ros.h"
00002 #include "geometry_msgs/Twist.h"
00003 #include "tf/transform_listener.h"
00004 
00005 //This node looks up a transform and then uses this transform to generate a cmd_vel for the robot so that the robot follows the transform
00006 
00007 const double max_speed_x = 0.1; // m/s
00008 const double max_speed_y = 0.05; // m/s
00009 const double max_rotation = 0.2; // rad/s
00010 const double minimal_distance_x = 0.1;
00011 const double minimal_distance_y = 0.2;
00012 
00013 template <typename T> int sgn(T val) {
00014     return (T(0) < val) - (val < T(0));
00015 }
00016 
00017 int main(int argc, char **argv) {
00018         ros::init(argc, argv, "biped_goalpose");
00019         ros::NodeHandle pn ("~");
00020         
00021         tf::TransformListener tfListener;
00022 
00023         ros::Publisher pub = pn.advertise<geometry_msgs::Twist>("cmd_vel", 10);
00024 
00025         std::string carrotFrame = "/carrot";
00026         std::string baseFrame = "/base_footprint";
00027         double carrot_distance_x;
00028         pn.param("carrot_frame_id", carrotFrame, carrotFrame);
00029         pn.param("base_frame_id", baseFrame, baseFrame);
00030         pn.param("carrot_distance_x", carrot_distance_x, -0.6);
00031         
00032         ros::Rate loop_rate(100.0); //publishen the Twist message with 100 Hz
00033         
00034         while (pn.ok()) {
00035                 tf::StampedTransform transform;
00036                 double dx, dy, da;
00037                 
00038                 ros::Time now = ros::Time::now();
00039                 
00040                 try {
00041                         tfListener.waitForTransform(baseFrame, carrotFrame, now-ros::Duration(0.5), ros::Duration(0.5)); //makes sure that the carrot was detected in the last 0.5s
00042                         tfListener.lookupTransform(baseFrame, carrotFrame, ros::Time(0), transform); //uses the last available carrot transform
00043                         dx = transform.getOrigin().x() + carrot_distance_x;
00044                         dy = transform.getOrigin().y();
00045                         da = atan2(dy, dx);
00046                 } catch (tf::TransformException ex) {
00047                         //ROS_ERROR("%s",ex.what());
00048                         ROS_INFO("Carrot not found! Please move the carrot into the field of view of the robot!");
00049                         dx = 0;
00050                         dy = 0;
00051                         da = 0;
00052                 }
00053 
00054                 ROS_INFO("Carrot is at position  x:%f, y:%f", dx, dy);
00055                 
00056                 if (fabs(dx) < minimal_distance_x) dx = 0;
00057                 if (fabs(dy) < minimal_distance_y) dy = 0;
00058                 da = atan2(dy, dx);
00059                 
00060                 geometry_msgs::Twist cmd_vel;
00061                 cmd_vel.linear.x = sgn(dx) * fmin(fabs(dx)*0.5, max_speed_x);
00062                 cmd_vel.linear.y = sgn(dy) * fmin(fabs(dy)*0.5, max_speed_y)*0;
00063                 cmd_vel.angular.z = sgn(da) * fmin(fabs(da), max_rotation);
00064                 
00065                 pub.publish(cmd_vel);
00066                 
00067                 loop_rate.sleep();
00068                 ros::spinOnce();
00069         }
00070 
00071         return 0;
00072 }
00073 


biped_robin_teleop
Author(s): Johannes Mayr, Johannes Mayr
autogenerated on Mon Jan 6 2014 11:09:14