parallel_example.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 ParallelExample():
00007     def __init__(self):
00008         # The root node
00009         BEHAVE = Sequence("behave")
00010         
00011         # Create a ParallelOne composite task (returns SUCCESS as soon as any subtask returns SUCCESS)
00012         PARALLEL_TASKS = ParallelOne("Counting in Parallel")
00013         
00014         # Create three counting tasks
00015         COUNT2 = Count("Count+2", 1, 2, 1)
00016         COUNT5 = Count("Count-5", 5, 1, -1)
00017         COUNT16 = Count("Count+16", 1, 16, 1)
00018 
00019         # Add the tasks to the parallel composite task
00020         PARALLEL_TASKS.add_child(COUNT5)
00021         PARALLEL_TASKS.add_child(COUNT2)
00022         PARALLEL_TASKS.add_child(COUNT16)
00023         
00024         # Add the composite task to the root task
00025         BEHAVE.add_child(PARALLEL_TASKS)
00026         
00027         # Print a simple representation of the tree
00028         print "Behavior Tree Structure"
00029         print_tree(BEHAVE)
00030             
00031         # Run the tree
00032         while True:
00033             status = BEHAVE.run()
00034             if status == TaskStatus.SUCCESS:
00035                 print "Finished running tree."
00036                 break
00037 
00038 # A counting task that extends the base Task task
00039 class Count(Task):
00040     def __init__(self, name, start, stop, step, *args, **kwargs):
00041         super(Count, self).__init__(name, *args, **kwargs)
00042         
00043         self.name = name
00044         self.start = start
00045         self.stop = stop
00046         self.step = step
00047         self.count = self.start
00048         print "Creating task Count", self.start, self.stop, self.step
00049  
00050     def run(self):
00051         if abs(self.count - self.stop - self.step) <= 0:
00052             return TaskStatus.SUCCESS
00053         else:
00054             print self.name, self.count
00055             time.sleep(0.5)
00056             self.count += self.step
00057             if abs(self.count - self.stop - self.step) <= 0:
00058                 return TaskStatus.SUCCESS
00059             else:
00060                 return TaskStatus.RUNNING
00061 
00062     
00063     def reset(self):
00064         self.count = self.start
00065 
00066 if __name__ == '__main__':
00067     tree = ParallelExample()
00068 


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