RobilTaskPy.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 
00004 import roslib; roslib.load_manifest('robot_task')
00005 import rospy
00006 
00007 import actionlib
00008 import robot_task.msg
00009 from multi_goal_action_server import *
00010 import thread
00011 
00012 RobotTask_SUCCESS = 0
00013 RobotTask_PLAN = -1
00014 RobotTask_FAIL = 1;
00015 
00016 class RTResult(object):
00017         def __init__(self, SUCC_val, PLAN_val, DES_val, PREEPTED_val):
00018                 self.success = SUCC_val
00019                 self.plan = PLAN_val
00020                 self.description = DES_val
00021                 self.isPreepted = PREEPTED_val
00022                 
00023 def RTResult_PREEPTED():
00024         return RTResult(RobotTask_FAIL, "", "Preempted", True)
00025 def RTResult_SUCCESSED(desc):
00026         return RTResult(RobotTask_SUCCESS, "", desc, False)
00027 def RTResult_PLAN(plan,desc):
00028         return RTResult(RobotTask_PLAN, "", desc, False)
00029 def RTResult_ABORT(err,desc):
00030         if err<=RobotTask_SUCCESS:
00031                 err = RobotTask_FAIL
00032         return RTResult(err, "", desc, False)
00033         
00034 def parseParameters(params):
00035         def findBlock(s, to, tc, si):
00036                 stri=-1
00037                 endi=-1
00038                 counter=0
00039                 #print '>>>',s,':',to,':',tc,':',si
00040                 for i in xrange(si, len(s)):
00041                         c = s[i]
00042                         #print "    ",c,"-",i,'[',(counter==0),(c==to),(c==tc),']',
00043                         if c==to:
00044                                 if counter==0:
00045                                         stri=i
00046                                 counter+=1
00047                         if c==tc:
00048                                 counter-=1
00049                                 if counter==0:
00050                                         endi=i
00051                         #print '-',counter,'-',stri,'-',endi
00052                         if endi>-1:
00053                                 return (stri, endi)
00054                 return (stri, endi)
00055         def searchArgIndex(s, blocks):
00056                 def checkInBlock(i, blocks):
00057                         for b in blocks:
00058                                 if b[0]<=i and i<=b[1]: return True
00059                         return False
00060                 commas=[]
00061                 i = s.find(',',0)
00062                 while i>=0:
00063                         if not checkInBlock(i, blocks):
00064                                 commas.append(i)
00065                         i = s.find(',',i+1)
00066                 return commas
00067         def findAllBlocks(s, to, tc):
00068                 blocks=[]
00069                 bs,be = findBlock(params, to, tc,0)
00070                 while bs>=0 and be>0:
00071                         blocks.append( (bs, be) )
00072                         bs,be = findBlock(params, to, tc,be+1)
00073                 return blocks
00074         blocks=[]
00075         blocks = blocks + findAllBlocks(params, '(', ')')
00076         blocks = blocks + findAllBlocks(params, '[', ']')
00077         blocks = blocks + findAllBlocks(params, '{', '}')
00078         blocks = blocks + findAllBlocks(params, '<', '>')
00079         #print blocks
00080         commas = searchArgIndex(params, blocks)
00081         args=[]
00082         if len(commas)==0 : args.append(params)
00083         else :
00084                 com_s=0
00085                 for com_e in commas:
00086                         arg = params[com_s:com_e]
00087                         args.append(arg.strip())
00088                         com_s = com_e+1
00089                 arg = params[com_s:len(params)]
00090                 args.append(arg.strip())        
00091         vmap={}
00092         for a in args:
00093                 if len(a)==0: continue
00094                 if a.find('=')>=0:
00095                         k,v = a[:a.find('=')],a[a.find('=')+1:]
00096                         vmap[k.strip()] = v.strip()
00097                 else:
00098                         vmap[a.strip()]=""
00099         
00100         return vmap
00101 
00102 
00103 class RobotTask(object):
00104         _result   = robot_task.msg.RobotTaskResult()
00105 
00106         def __init__(self, name):
00107                 self._action_name = name
00108                 self._goals = {}
00109                 self._as = MGActionServer(self._action_name, robot_task.msg.RobotTaskAction, self.abstract_task, False)
00110                 rospy.loginfo('%s: Start create task object' % self._action_name)
00111                 self._as.start()
00112                 rospy.loginfo('%s: Task object created' % self._action_name)
00113 
00114         def currentGoal(self):
00115                 return self._goals[thread.get_ident()]
00116                 
00117         def finish(self, res):
00118                 #print "[dan] finish task"
00119                 if res.isPreepted:
00120                         rospy.loginfo('%s: Task Preempted' % self._action_name)
00121                         self._as.set_preempted(self.currentGoal())
00122                         return
00123                 if res.success<=RobotTask_SUCCESS:
00124                         self._result.success = res.success
00125                         if res.success == RobotTask_PLAN:
00126                                 self._result.plan = res.plan
00127                         self._result.description = res.description
00128                         rospy.loginfo('%s: Task Succeeded' % self._action_name)
00129                         self._as.set_succeeded(self.currentGoal(), self._result)
00130                         return
00131                 self._result.success = res.success
00132                 self._result.description = res.description
00133                 rospy.loginfo('%s: Task Aborted' % self._action_name)
00134                 self._as.set_aborted(self.currentGoal(), self._result)
00135                 
00136         def isPreepted(self):
00137                 return rospy.is_shutdown() or self._as.is_preempt_requested(self.currentGoal())
00138 
00139         def abstract_task(self, goal):
00140                 #print "[dan] start abstract task"
00141                 rospy.loginfo('%s: Task Started' % self._action_name)
00142                 self._goals[thread.get_ident()]=goal
00143                 arguments = parseParameters(goal.get_goal().parameters)
00144                 rospy.loginfo('%s: ... name=%s, id=%s, args=%s' , self._action_name , goal.get_goal().name , goal.get_goal().uid , str(arguments))
00145                 res = self.task(goal.get_goal().name, goal.get_goal().uid, arguments)
00146                 if rospy.is_shutdown(): return
00147                 self.finish(res)
00148                 del self._goals[thread.get_ident()]
00149         
00150         def task(self, name, uid, parameters):
00151                 #print "[dan] Empty Task"
00152                 return RTResult_ABORT(1,"Empty Task")
00153 
00154 
00155       
00156 if __name__ == '__main__':
00157         pass


robot_task
Author(s):
autogenerated on Wed Aug 26 2015 11:16:50