composite_tasks.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 from pi_trees_lib.pi_trees_lib import *
00004 import time
00005 
00006 class CompositeTasks():
00007     def __init__(self):
00008         # The root node
00009         BEHAVE = Sequence("behave")
00010         
00011         # Create a Selector composite task (returns SUCCESS as soon as any subtask returns SUCCESS)
00012         SELECTOR_TASK = Selector("Selector Count")
00013 
00014         # Create a Sequence composite task (returns FAULURE as soon as any subtask returns FAILURE)
00015         SEQUENCE_TASK = Sequence("Sequence Count")
00016         
00017         WAIT_TASK = WaitTask("Wait Task", 5)
00018         
00019         # Create three counting tasks
00020         COUNT2 = Count("Count+2", 1, 2, 1)
00021         COUNT5 = Count("Count-5", 5, 1, -1)
00022         COUNT16 = Count("Count+16", 1, 16, 1)
00023 
00024         # Add the tasks to the sequence composite task
00025         SEQUENCE_TASK.add_child(COUNT2)
00026         SEQUENCE_TASK.add_child(WAIT_TASK)
00027         SEQUENCE_TASK.add_child(COUNT5)
00028         SEQUENCE_TASK.add_child(COUNT16)
00029         
00030         # Add the tasks to the selector composite task
00031         SELECTOR_TASK.add_child(COUNT5)
00032         SELECTOR_TASK.add_child(COUNT2)
00033         SELECTOR_TASK.add_child(COUNT16)
00034         
00035         # Add the composite task to the root task
00036         BEHAVE.add_child(SEQUENCE_TASK)
00037         BEHAVE.add_child(SELECTOR_TASK)
00038 
00039         # Print a simple representation of the tree
00040         print "Behavior Tree Structure"
00041         print_tree(BEHAVE, use_symbols=True)
00042             
00043         # Run the tree
00044         while True:
00045             status = BEHAVE.run()
00046             if status == TaskStatus.SUCCESS:
00047                 print "Finished running tree."
00048                 break
00049 
00050 # A counting task that extends the base Task task
00051 class Count(Task):
00052     def __init__(self, name, start, stop, step, *args, **kwargs):
00053         super(Count, self).__init__(name, *args, **kwargs)
00054         
00055         self.name = name
00056         self.start = start
00057         self.stop = stop
00058         self.step = step
00059         self.count = self.start
00060         print "Creating task Count", self.start, self.stop, self.step
00061  
00062     def run(self):
00063         if abs(self.count - self.stop - self.step) <= 0:
00064             return TaskStatus.SUCCESS
00065         else:
00066             print self.name, self.count
00067             time.sleep(0.5)
00068             self.count += self.step
00069             if abs(self.count - self.stop - self.step) <= 0:
00070                 return TaskStatus.SUCCESS
00071             else:
00072                 return TaskStatus.RUNNING
00073 
00074     
00075     def reset(self):
00076         self.count = self.start
00077 
00078 if __name__ == '__main__':
00079     tree = CompositeTasks()
00080 


pi_trees_lib
Author(s): Patrick Goebel
autogenerated on Thu Jun 6 2019 17:33:29