pattern_generator_node.cpp
Go to the documentation of this file.
00001 #include <vigir_pattern_generator/pattern_generator_node.h>
00002 
00003 namespace vigir_footstep_planning
00004 {
00005 PatternGeneratorNode::PatternGeneratorNode(ros::NodeHandle& nh)
00006   : pattern_generator(nh)
00007 {
00008   double update_rate;
00009   nh.param("pattern_generator/update_rate", update_rate, 10.0);
00010   nh.param("pattern_generator/republish_complete_step_plan", republish_complete_step_plan, false);
00011 
00012   // subscribe topics
00013   generate_pattern_sub = nh.subscribe("pattern_generator/generate_pattern", 1, &PatternGeneratorNode::generatePattern, this);
00014   set_params_sub = nh.subscribe("pattern_generator/set_params", 1, &PatternGeneratorNode::setParams, this);
00015 
00016   // publish topics
00017   step_plan_pub = nh.advertise<msgs::StepPlan>("step_plan", 1);
00018   step_plan_vis_pub = nh.advertise<msgs::StepPlan>("vis/step_plan", 1);
00019 
00020   // start own services
00021   generate_pattern_srv = nh.advertiseService("pattern_generator/generate_pattern", &PatternGeneratorNode::generatePatternService, this);
00022   set_params_srv = nh.advertiseService("pattern_generator/set_params", &PatternGeneratorNode::setParamsService, this);
00023 
00024   // init action client
00025   execute_step_plan_ac = SimpleActionClient<msgs::ExecuteStepPlanAction>::create(nh, "/execute_step_plan");
00026 
00027   // init action servers
00028   generate_pattern_as = SimpleActionServer<msgs::GeneratePatternAction>::create(nh, "pattern_generator/generate_pattern", true, boost::bind(&PatternGeneratorNode::generatePatternAction, this, boost::ref(generate_pattern_as)));
00029 
00030   // init timer to periodically call the pattern generator
00031   update_intervall = ros::Duration(1.0/update_rate);
00032   timer = nh.createTimer(update_intervall, &PatternGeneratorNode::update, this);
00033 }
00034 
00035 PatternGeneratorNode::~PatternGeneratorNode()
00036 {
00037 }
00038 
00039 void PatternGeneratorNode::generatePattern(const msgs::StepPlanRequestConstPtr& step_plan_request)
00040 {
00041   msgs::StepPlan step_plan;
00042   msgs::ErrorStatus status = pattern_generator.generatePattern(*step_plan_request, step_plan);
00043 
00044   if (status.error == msgs::ErrorStatus::NO_ERROR)
00045   {
00046     step_plan_pub.publish(step_plan);
00047     step_plan_vis_pub.publish(step_plan);
00048   }
00049 }
00050 
00051 void PatternGeneratorNode::setParams(const msgs::PatternGeneratorParametersConstPtr& params)
00052 {
00053   pattern_generator.setParams(*params);
00054 }
00055 
00056 bool PatternGeneratorNode::generatePatternService(msgs::GeneratePatternService::Request& req, msgs::GeneratePatternService::Response& resp)
00057 {
00058   resp.status = pattern_generator.generatePattern(req.plan_request, resp.step_plan);
00059 
00060   if (resp.status.error == msgs::ErrorStatus::NO_ERROR)
00061     step_plan_vis_pub.publish(resp.step_plan);
00062 
00063   return true; // return always true so the message is returned
00064 }
00065 
00066 bool PatternGeneratorNode::setParamsService(msgs::PatternGeneratorParametersService::Request& req, msgs::PatternGeneratorParametersService::Response& /*resp*/)
00067 {
00068   pattern_generator.setParams(req.params);
00069   return true; // return always true so the message is returned
00070 }
00071 
00072 void PatternGeneratorNode::generatePatternAction(SimpleActionServer<msgs::GeneratePatternAction>::Ptr& as)
00073 {
00074   const msgs::GeneratePatternGoalConstPtr& goal(as->acceptNewGoal());
00075 
00076   // check if new goal was preempted in the meantime
00077   if (as->isPreemptRequested())
00078   {
00079     as->setPreempted();
00080     return;
00081   }
00082 
00083   msgs::GeneratePatternResult result;
00084   result.status = pattern_generator.generatePattern(goal->plan_request, result.step_plan);
00085 
00086   if (result.status.error == msgs::ErrorStatus::NO_ERROR)
00087     step_plan_vis_pub.publish(result.step_plan);
00088 
00089   as->finish(result);
00090 }
00091 
00092 void PatternGeneratorNode::executeStepPlanFeedback(const msgs::ExecuteStepPlanFeedbackConstPtr& feedback)
00093 {
00094   pattern_generator.updateLastPerformedStepIndex(feedback->last_performed_step_index);
00095   pattern_generator.updateFirstChangeableStepIndex(feedback->first_changeable_step_index);
00096 }
00097 
00098 void PatternGeneratorNode::update(const ros::TimerEvent& timer)
00099 {
00100   pattern_generator.update(timer);
00101 
00102   if (!pattern_generator.hasNewSteps())
00103     return;
00104 
00105   // publish vis of entire plan
00106   msgs::StepPlan complete_step_plan;
00107   pattern_generator.getCompleteStepPlan(complete_step_plan);
00108   step_plan_vis_pub.publish(complete_step_plan);
00109 
00110   msgs::ExecuteStepPlanGoal goal;
00111   pattern_generator.getNewestStepPlan(goal.step_plan); // must be getted to reset hasNew flag
00112 
00113   if (republish_complete_step_plan)
00114     goal.step_plan = complete_step_plan;
00115 
00116   // send to controller if available
00117   if (!pattern_generator.isSimulationMode() && execute_step_plan_ac->waitForServer(update_intervall))
00118   {
00119     execute_step_plan_ac->sendGoal(goal,
00120                                    SimpleActionClient<msgs::ExecuteStepPlanAction>::SimpleDoneCallback(),
00121                                    SimpleActionClient<msgs::ExecuteStepPlanAction>::SimpleActiveCallback(),
00122                                    boost::bind(&PatternGeneratorNode::executeStepPlanFeedback, this, _1));
00123   }
00124 }
00125 }
00126 
00127 int main(int argc, char **argv)
00128 {
00129   ros::init(argc, argv, "vigir_pattern_generator");
00130   ros::NodeHandle nh;
00131   vigir_footstep_planning::PatternGeneratorNode patternGeneratorNode(nh);
00132   ros::spin();
00133 
00134   return 0;
00135 }


vigir_pattern_generator
Author(s): Alexander Stumpf
autogenerated on Fri Apr 7 2017 02:59:51