Go to the documentation of this file.00001
00002
00003 """
00004 parallel_example.py - Version 1.0 2013-09-22
00005
00006 Perform a number of parallel counting tasks
00007
00008 Created for the Pi Robot Project: http://www.pirobot.org
00009 Copyright (c) 2013 Patrick Goebel. All rights reserved.
00010
00011 This program is free software; you can redistribute it and/or modify
00012 it under the terms of the GNU General Public License as published by
00013 the Free Software Foundation; either version 2 of the License, or
00014 (at your option) any later version.
00015
00016 This program is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 GNU General Public License for more details at:
00020
00021 http://www.gnu.org/licenses/gpl.html
00022 """
00023
00024 from pi_trees_lib.pi_trees_lib import *
00025 import time
00026
00027 class ParallelExample():
00028 def __init__(self):
00029
00030 BEHAVE = Sequence("behave")
00031
00032
00033 PARALLEL_TASKS = ParallelOne("Counting in Parallel")
00034
00035
00036 COUNT2 = Count("Count+2", 1, 2, 1)
00037 COUNT5 = Count("Count-5", 5, 1, -1)
00038 COUNT16 = Count("Count+16", 1, 16, 1)
00039
00040
00041 PARALLEL_TASKS.add_child(COUNT5)
00042 PARALLEL_TASKS.add_child(COUNT2)
00043 PARALLEL_TASKS.add_child(COUNT16)
00044
00045
00046 BEHAVE.add_child(PARALLEL_TASKS)
00047
00048
00049 print "Behavior Tree Structure"
00050 print_tree(BEHAVE)
00051
00052
00053 while True:
00054 status = BEHAVE.run()
00055 if status == TaskStatus.SUCCESS:
00056 print "Finished running tree."
00057 break
00058
00059
00060 class Count(Task):
00061 def __init__(self, name, start, stop, step, *args, **kwargs):
00062 super(Count, self).__init__(name, *args, **kwargs)
00063
00064 self.name = name
00065 self.start = start
00066 self.stop = stop
00067 self.step = step
00068 self.count = self.start
00069 print "Creating task Count", self.start, self.stop, self.step
00070
00071 def run(self):
00072 if abs(self.count - self.stop - self.step) <= 0:
00073 return TaskStatus.SUCCESS
00074 else:
00075 print self.name, self.count
00076 time.sleep(0.5)
00077 self.count += self.step
00078 if abs(self.count - self.stop - self.step) <= 0:
00079 return TaskStatus.SUCCESS
00080 else:
00081 return TaskStatus.RUNNING
00082
00083
00084 def reset(self):
00085 self.count = self.start
00086
00087 if __name__ == '__main__':
00088 tree = ParallelExample()
00089