execute_button_commands.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 #################################################################
00003 ##\file
00004 #
00005 # \note
00006 #   Copyright (c) 2010 \n
00007 #   Fraunhofer Institute for Manufacturing Engineering
00008 #   and Automation (IPA) \n\n
00009 #
00010 #################################################################
00011 #
00012 # \note
00013 #   Project name: care-o-bot
00014 # \note
00015 #   ROS stack name: cob_environment_perception_intern
00016 # \note
00017 #   ROS package name: cob_3d_mapping_demonstrator
00018 #
00019 # \author
00020 #   Author: Georg Arbeiter, email:georg.arbeiter@ipa.fhg.de
00021 # \author
00022 #   Supervised by: Georg Arbeiter, email:georg.arbeiter@ipa.fhg.de
00023 #
00024 # \date Date of creation: 03/2012
00025 #
00026 # \brief
00027 #   Implementation of ROS node for script_server.
00028 #
00029 #################################################################
00030 #
00031 # Redistribution and use in source and binary forms, with or without
00032 # modification, are permitted provided that the following conditions are met:
00033 #
00034 #     - Redistributions of source code must retain the above copyright
00035 #       notice, this list of conditions and the following disclaimer. \n
00036 #     - Redistributions in binary form must reproduce the above copyright
00037 #       notice, this list of conditions and the following disclaimer in the
00038 #       documentation and/or other materials provided with the distribution. \n
00039 #     - Neither the name of the Fraunhofer Institute for Manufacturing
00040 #       Engineering and Automation (IPA) nor the names of its
00041 #       contributors may be used to endorse or promote products derived from
00042 #       this software without specific prior written permission. \n
00043 #
00044 # This program is free software: you can redistribute it and/or modify
00045 # it under the terms of the GNU Lesser General Public License LGPL as
00046 # published by the Free Software Foundation, either version 3 of the
00047 # License, or (at your option) any later version.
00048 #
00049 # This program is distributed in the hope that it will be useful,
00050 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00051 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00052 # GNU Lesser General Public License LGPL for more details.
00053 #
00054 # You should have received a copy of the GNU Lesser General Public
00055 # License LGPL along with this program.
00056 # If not, see <http://www.gnu.org/licenses/>.
00057 #
00058 #################################################################
00059 
00060 import time
00061 
00062 import roslib
00063 roslib.load_manifest('cob_3d_mapping_demonstrator')
00064 import rospy
00065 import actionlib
00066 
00067 from cob_script_server.msg import *
00068 from simple_script_server import *
00069 from cob_srvs.srv import *
00070 from cob_3d_mapping_msgs.msg import *
00071 
00072 sss = simple_script_server()
00073 
00074 ## Script server class which inherits from script class.
00075 #
00076 # Implements actionlib interface for the script server.
00077 #
00078 class execute_button_commands():
00079   ## Initializes the actionlib interface of the script server.
00080   #
00081   def __init__(self):
00082     self.ns_global_prefix = "/cob_3d_mapping_demonstrator"
00083     self.script_action_server = actionlib.SimpleActionServer("cob_3d_mapping_demonstrator", ScriptAction, self.execute_cb, False)
00084     self.script_action_server.register_preempt_callback(self.execute_stop)
00085     self.script_action_server.start()
00086     self.trigger_client = actionlib.SimpleActionClient('trigger_mapping', TriggerAction)
00087     self.step = 0.1
00088 
00089 #------------------- Actionlib section -------------------#
00090   ## Executes actionlib callbacks.
00091   #
00092   # \param server_goal ScriptActionGoal
00093   #
00094   def execute_cb(self, server_goal):
00095     print "execute_cb"
00096     server_result = ScriptActionResult().result
00097     if server_goal.function_name == "start":
00098       print "start"
00099       ret=self.execute_start()
00100     #elif server_goal.function_name == "stop":
00101     #  ret=self.execute_stop()
00102     elif server_goal.function_name == "reset":
00103       ret=self.execute_reset()
00104     elif server_goal.function_name == "clear":
00105       ret=self.execute_clear()
00106     elif server_goal.function_name == "step":
00107       ret=self.execute_step()
00108     elif server_goal.function_name == "recover":
00109       ret=self.execute_recover()
00110     else:
00111       rospy.logerr("function <<%s>> not supported", server_goal.function_name)
00112       self.script_action_server.set_aborted(server_result)
00113       return
00114 
00115     #server_result.error_code = handle01.get_error_code()
00116     #if server_result.error_code == 0:
00117     #  rospy.logdebug("action result success")
00118     if self.script_action_server.is_active():
00119       self.script_action_server.set_succeeded(server_result)
00120     #else:
00121     #  self.script_action_server.set_aborted(server_result)
00122     #else:
00123     #  rospy.logerr("action result error")
00124     #  self.script_action_server.set_aborted(server_result)
00125 
00126   def execute_start(self):
00127     print "start"
00128     #self.execute_reset()
00129     #sss.move("cob_3d_mapping_demonstrator",[[-0.5,0]])
00130         #return False
00131     #if self.script_action_server.is_preempt_requested():
00132     #  self.script_action_server.set_preempted()
00133     #  return False
00134     print self.script_action_server.is_preempt_requested()
00135     while not self.script_action_server.is_preempt_requested():
00136       print "move"
00137       sss.move("cob_3d_mapping_demonstrator",[[0.5,0],[-0.5,0],[-0.5,-0.5],[0.5,-0.5],[0.5,0],[0,0]])
00138       sss.sleep(1.0)
00139     #sss.move("cob_3d_mapping_demonstrator","home")
00140     #if self.script_action_server.is_preempt_requested():
00141     #  self.script_action_server.set_preempted()
00142     #  sss.stop("cob_3d_mapping_demonstrator")
00143     #  return False
00144     #sss.move("cob_3d_mapping_demonstrator",[[0.5,0]])
00145     #sss.sleep(0.5)
00146     #if self.script_action_server.is_preempt_requested():
00147     #  self.script_action_server.set_preempted()
00148     #  sss.stop("cob_3d_mapping_demonstrator")
00149     #  return False
00150     #sss.move("cob_3d_mapping_demonstrator",[[0.5,-0.5]])
00151     #if self.script_action_server.is_preempt_requested():
00152     #  self.script_action_server.set_preempted()
00153     #  sss.stop("cob_3d_mapping_demonstrator")
00154     #  return False
00155     #sss.move("cob_3d_mapping_demonstrator",[[0,-0.5]])
00156     #if self.script_action_server.is_preempt_requested():
00157     #  self.script_action_server.set_preempted()
00158     #  sss.stop("cob_3d_mapping_demonstrator")
00159     #  return False
00160     #sss.move("cob_3d_mapping_demonstrator",[[-0.5,-0.5]])
00161     #if self.script_action_server.is_preempt_requested():
00162     #  self.script_action_server.set_preempted()
00163     #  sss.stop("cob_3d_mapping_demonstrator")
00164     #  return False
00165     #sss.move("cob_3d_mapping_demonstrator","home")
00166 
00167   def execute_start_map(self):
00168     print "start"
00169     goal = TriggerGoal()
00170     if not self.trigger_client.wait_for_server(rospy.Duration.from_sec(2.0)):
00171       print "switched to /segmentation/trigger"
00172       self.trigger_client = actionlib.SimpleActionClient('/segmentation/trigger', TriggerAction)
00173 
00174     if not self.trigger_client.wait_for_server(rospy.Duration.from_sec(2.0)):
00175       rospy.logerr('server not available')
00176       #return False
00177     else:
00178       goal.start = True
00179       self.trigger_client.send_goal(goal)
00180       if not self.trigger_client.wait_for_result(rospy.Duration.from_sec(2.0)):
00181         print "no result"
00182         #return False
00183     sss.move("cob_3d_mapping_demonstrator",[[-0.5,0]])
00184     if self.script_action_server.is_preempt_requested():
00185       self.script_action_server.set_preempted()
00186       return False
00187     sss.move("cob_3d_mapping_demonstrator","home")
00188     if self.script_action_server.is_preempt_requested():
00189       self.script_action_server.set_preempted()
00190       sss.stop("cob_3d_mapping_demonstrator")
00191       return False
00192     sss.move("cob_3d_mapping_demonstrator",[[0.5,0]])
00193     sss.sleep(0.5)
00194     if self.script_action_server.is_preempt_requested():
00195       self.script_action_server.set_preempted()
00196       sss.stop("cob_3d_mapping_demonstrator")
00197       return False
00198     sss.move("cob_3d_mapping_demonstrator",[[0.5,-0.5]])
00199     if self.script_action_server.is_preempt_requested():
00200       self.script_action_server.set_preempted()
00201       sss.stop("cob_3d_mapping_demonstrator")
00202       return False
00203     sss.move("cob_3d_mapping_demonstrator",[[0,-0.5]])
00204     if self.script_action_server.is_preempt_requested():
00205       self.script_action_server.set_preempted()
00206       sss.stop("cob_3d_mapping_demonstrator")
00207       return False
00208     sss.move("cob_3d_mapping_demonstrator",[[-0.5,-0.5]])
00209     if self.script_action_server.is_preempt_requested():
00210       self.script_action_server.set_preempted()
00211       sss.stop("cob_3d_mapping_demonstrator")
00212       return False
00213     sss.move("cob_3d_mapping_demonstrator","home")
00214     goal.start = False
00215     self.trigger_client.send_goal(goal)
00216     if not self.trigger_client.wait_for_result(rospy.Duration.from_sec(2.0)):
00217       return False
00218     return True
00219 
00220 
00221   def execute_stop(self):
00222     print "stop"
00223     self.script_action_server.set_preempted()
00224     sss.stop("cob_3d_mapping_demonstrator")
00225     return True
00226 
00227   def execute_reset(self):
00228     print "reset"
00229     self.execute_stop()
00230     goal = TriggerGoal()
00231     goal.start = False
00232     self.trigger_client.send_goal(goal)
00233     if not self.trigger_client.wait_for_result(rospy.Duration.from_sec(2.0)):
00234       print "mapping not running"
00235       return False
00236     #sss.move("cob_3d_mapping_demonstrator","home")
00237     #self.execute_clear()
00238     #self.step = 0.1
00239     return True
00240 
00241     #TODO: move to home, stop mapping, clear map
00242 
00243   def execute_clear(self):
00244     print "clear"
00245     try:
00246       #rospy.wait_for_service('point_map/clear_point_map', timeout=2.0)
00247       clear = rospy.ServiceProxy('point_map/clear_map', Trigger)
00248       resp = clear()
00249     except rospy.ServiceException, e:
00250       print "Service call failed: %s"%e
00251     try:
00252       #rospy.wait_for_service('geometry_map/clear_geometry_map', timeout=2.0)
00253       clear = rospy.ServiceProxy('geometry_map/clear_map', Trigger)
00254       resp = clear()
00255     except rospy.ServiceException, e:
00256       print "Service call failed: %s"%e
00257     try:
00258       #rospy.wait_for_service('geometry_map/clear_geometry_map', timeout=2.0)
00259       clear = rospy.ServiceProxy('registration/reset', Trigger)
00260       resp = clear()
00261     except rospy.ServiceException, e:
00262       print "Service call failed: %s"%e
00263     return True
00264 
00265   def execute_recover(self):
00266     goal = TriggerGoal()
00267     if not self.trigger_client.wait_for_server(rospy.Duration.from_sec(2.0)):
00268       print "switched to /segmentation/trigger"
00269       self.trigger_client = actionlib.SimpleActionClient('/segmentation/trigger', TriggerAction)
00270 
00271     if not self.trigger_client.wait_for_server(rospy.Duration.from_sec(2.0)):
00272       rospy.logerr('server not available')
00273       #return False
00274     else:
00275       goal.start = True
00276       self.trigger_client.send_goal(goal)
00277       if not self.trigger_client.wait_for_result(rospy.Duration.from_sec(2.0)):
00278         print "no result"
00279     #print "recover"
00280     #sss.recover("cob_3d_mapping_demonstrator")
00281     return True
00282 
00283   def execute_step(self):
00284     sss.move("cob_3d_mapping_demonstrator",[[0, -0.5]])
00285     """if self.step==0.1:
00286       self.execute_clear()
00287     print "step"
00288     goal = TriggerGoal()
00289     if not self.trigger_client.wait_for_server(rospy.Duration.from_sec(2.0)):
00290       rospy.logerr('server not available')
00291       #return False
00292     else:
00293       goal.start = True
00294       self.trigger_client.send_goal(goal)
00295       if not self.trigger_client.wait_for_result(rospy.Duration.from_sec(2.0)):
00296         print "no result"
00297     sss.move("cob_3d_mapping_demonstrator",[[self.step,-0.3]])
00298     self.step += 0.1"""
00299 
00300 ## Main routine for running the script server
00301 #
00302 if __name__ == '__main__':
00303   rospy.init_node('execute_button_commands')
00304   execute_button_commands()
00305   rospy.loginfo("execute button commands is running")
00306   rospy.spin()


cob_3d_mapping_demonstrator
Author(s): Georg Arbeiter
autogenerated on Wed Aug 26 2015 11:03:46