keyNavSafety.cpp
Go to the documentation of this file.
00001 
00008 #include <youbot_overhead_localization/keyNavSafety.h>
00009 
00010 keyNavSafety::keyNavSafety()
00011 {
00012   moveInitialized = false;
00013   localizationInitialized = false;
00014   currentCost.cost = 0;
00015 
00016   std::string moveCmdTopicName;
00017 
00018   if (!n.getParam("/key_nav_safety/timeBetweenStopCmds", timeBetweenStopCmds))
00019     timeBetweenStopCmds = 5;
00020 
00021   //subscriber and publisher initialization
00022   if (!n.getParam("/key_nav_safety/moveCommandTopic", moveCmdTopicName))
00023     baseCommandPublisher = n.advertise<geometry_msgs::Twist>("/cmd_vel", -1);
00024   else
00025     baseCommandPublisher = n.advertise<geometry_msgs::Twist>(moveCmdTopicName, -1);
00026 
00027   baseCommandSubscriber = n.subscribe("/key_nav_safety/vel", -1, &keyNavSafety::baseCommandCallback, this);
00028   costSubscriber = n.subscribe("/base_localizer/cost", 1, &keyNavSafety::costCallback, this);
00029   localizationSubscriber = n.subscribe("/base_localizer/pose_2d", 1, &keyNavSafety::localizationCallback, this);
00030   costClient = n.serviceClient<youbot_overhead_localization::GetCost>("/path_planner/get_cost");
00031   stopCommandFound = true;
00032   lastStopCmdTime = 0;
00033 }
00034 
00035 void keyNavSafety::costCallback(const youbot_overhead_localization::CostmapCost& newCost)
00036 {
00037   currentCost.cost = newCost.cost;
00038   //adjust speed based on cost
00039   if (moveInitialized && localizationInitialized)
00040   {
00041     geometry_msgs::Twist modifiedCommand;
00042     modifiedCommand.linear.x = moveCommand.linear.x;
00043     modifiedCommand.linear.y = moveCommand.linear.y;
00044     modifiedCommand.angular.z = moveCommand.angular.z;
00045 
00046     if (currentCost.cost > 0)
00047     {
00048       float speedReduction = .000444 * pow(currentCost.cost - 254, 2) + .1;
00049       if (fabs(moveCommand.linear.x) > .1 || fabs(moveCommand.linear.y) > .1)
00050       {
00051         //Determine if robot is moving away from an obstacle
00052         float alpha = atan2(-moveCommand.linear.x, moveCommand.linear.y) + PI / 2.0 + robotPose.theta;
00053         while (alpha > PI)
00054           alpha -= 2 * PI;
00055         while (alpha < -PI)
00056           alpha += 2 * PI;
00057 
00058         youbot_overhead_localization::GetCost srv;
00059         srv.request.x = robotPose.x
00060             + .2 * sqrt(pow(moveCommand.linear.x, 2) + pow(moveCommand.linear.y, 2)) * cos(alpha);
00061         srv.request.y = robotPose.y
00062             + .2 * sqrt(pow(moveCommand.linear.x, 2) + pow(moveCommand.linear.y, 2)) * sin(alpha);
00063         //ROS_INFO("Current: (%f,%f); Predicted: (%f,%f)", robotPose.x, robotPose.y, srv.request.x, srv.request.y);
00064         //ROS_INFO("Movement angle: %f; Robot angle: %f", alpha, robotPose.theta);
00065         if (costClient.call(srv))
00066         {
00067           float futureCost = srv.response.cost;
00068           if (futureCost < currentCost.cost)
00069           {
00070             //ROS_INFO("Detected movement away from obstacles");
00071             speedReduction = .75;
00072           }
00073         }
00074       }
00075 
00076       if (speedReduction < .1)
00077         speedReduction = .1;
00078       modifiedCommand.linear.x *= speedReduction;
00079       modifiedCommand.linear.y *= speedReduction;
00080       modifiedCommand.angular.z *= speedReduction;
00081 
00082       //ROS_INFO("Speed adjusted by %f for safety", speedReduction);
00083     }
00084     else
00085     {
00086       //ROS_INFO("Speed adjusted by 1.0 for safety");
00087     }
00088 
00089     //Checking to see if the the command is a stop command.  If it is
00090 
00091     if (stopCommandFound)
00092     {
00093       time_t currentT = time(NULL);
00094       long timeDiff = currentT - lastStopCmdTime;
00095 
00096       if (timeDiff > timeBetweenStopCmds)
00097       {
00098         baseCommandPublisher.publish(modifiedCommand);
00099         lastStopCmdTime = currentT;
00100       }
00101     }
00102     else
00103       baseCommandPublisher.publish(modifiedCommand);
00104   }
00105 }
00106 
00107 void keyNavSafety::baseCommandCallback(const geometry_msgs::Twist& requestedCommand)
00108 {
00109   moveCommand = requestedCommand;
00110   if (!moveInitialized)
00111     moveInitialized = true;
00112 
00113   if (moveCommand.linear.x == 0 && moveCommand.linear.y == 0 && moveCommand.angular.z == 0)
00114   {
00115     stopCommandFound = true;
00116   }
00117   else
00118   {
00119     stopCommandFound = false;
00120     lastStopCmdTime = 0;
00121   }
00122 }
00123 
00124 void keyNavSafety::localizationCallback(const youbot_overhead_localization::Pose2d& pose)
00125 {
00126   robotPose = pose;
00127   if (!localizationInitialized)
00128     localizationInitialized = true;
00129 }
00130 
00134 int main(int argc, char **argv)
00135 {
00136   ros::init(argc, argv, "key_nav_safety");
00137 
00138   keyNavSafety navSafety;
00139 
00140   ros::spin();
00141 
00142   return 0;
00143 }
00144 


youbot_overhead_localization
Author(s): David Kent
autogenerated on Thu Jan 2 2014 12:14:20