door_handler.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 
00003 #include <bwi_msgs/DoorHandlerInterface.h>
00004 #include <segbot_simulation_apps/door_handler.h>
00005 
00006 boost::shared_ptr<segbot_simulation_apps::DoorHandler> gh_;
00007 std::vector<boost::shared_ptr<ros::Timer> > timeout_queue_;
00008 
00009 void executeSingleTimeout(const ros::TimerEvent&, 
00010                           bwi_msgs::DoorHandlerInterface::Request req,
00011                           boost::shared_ptr<ros::Timer> timer) {
00012   if (req.all_doors) {
00013     gh_->closeAllDoors();
00014   } else {
00015     /* bool success = */ gh_->closeDoor(req.door);
00016     // TODO do something with the success result? This was an automated request.
00017   }
00018   timeout_queue_.erase(std::remove(timeout_queue_.begin(), timeout_queue_.end(), timer), timeout_queue_.end());
00019 }
00020 
00021 bool execute(bwi_msgs::DoorHandlerInterface::Request  &req,
00022              bwi_msgs::DoorHandlerInterface::Response &res) {
00023 
00024   res.status = "";
00025   if (req.all_doors) {
00026     if (req.open) {
00027       gh_->openAllDoors();
00028     } else {
00029       gh_->closeAllDoors();
00030     }
00031     res.success = true;
00032   } else {
00033     if (req.open) {
00034       res.success = gh_->openDoor(req.door);
00035     } else {
00036       res.success = gh_->closeDoor(req.door);
00037     }
00038     if (!res.success) {
00039       res.status = "Unable to resolved " + req.door + "!";
00040     }
00041   }
00042 
00043   if (req.open && req.open_timeout > 0) {
00044     boost::shared_ptr<ros::Timer> timer(new ros::Timer);
00045     ros::NodeHandle nh;
00046     *timer = nh.createTimer(ros::Duration(req.open_timeout), boost::bind(executeSingleTimeout, _1, req, timer), true);
00047     timeout_queue_.push_back(timer);
00048   }
00049   return true;
00050 }
00051 
00052 int main(int argc, char *argv[]) {
00053 
00054   ros::init(argc, argv, "gazebo_door_handler");
00055   ros::NodeHandle nh;
00056 
00057   ros::ServiceServer service = nh.advertiseService("update_doors", execute);
00058   gh_.reset(new segbot_simulation_apps::DoorHandler);
00059   ros::spin();
00060   return 0;
00061 }


segbot_simulation_apps
Author(s): Piyush Khandelwal
autogenerated on Thu Jun 6 2019 21:37:47