test_conditions.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 # kate: replace-tabs off; indent-width 4; indent-mode normal
00003 # vim: ts=4:sw=4:noexpandtab
00004 
00005 # Copyright (c) 2012 Stéphane Magnenat, ETHZ Zürich and other contributors
00006 # See file authors.txt for details.
00007 # All rights reserved.
00008 #
00009 # Redistribution and use in source and binary forms, with or without
00010 # modification, are permitted provided that the following conditions are met:
00011 #
00012 #   * Redistributions of source code must retain the above copyright notice,
00013 #     this list of conditions and the following disclaimer.
00014 #   * Redistributions in binary form must reproduce the above copyright notice,
00015 #     this list of conditions and the following disclaimer in the documentation
00016 #     and/or other materials provided with the distribution.
00017 #   * Neither the name of Stéphane Magnenat, ETHZ Zürich, nor the names
00018 #     of the contributors may be used to endorse or promote products derived
00019 #     from this software without specific prior written permission.
00020 #
00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00022 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00023 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00025 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00026 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00027 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00029 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00030 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 
00032 import sys
00033 sys.path.append('..')
00034 from teer import *
00035 import math
00036 
00037 class MyScheduler(TimerScheduler):
00038         chlorophyll_level = ConditionVariable(0.)
00039         energy_level = ConditionVariable(100)
00040 
00041 def chlorophyll_detector():
00042         yield WaitCondition(lambda: sched.chlorophyll_level > 2)
00043         print 'We found chlorophyll'
00044         yield WaitDuration(2)
00045         print 'Ok, I\'m green enough'
00046 
00047 def energy_monitoring():
00048         yield WaitCondition(lambda: sched.energy_level < 10)
00049         print 'No more energy, killing all tasks'
00050         my_tid = sched.get_current_tid()
00051         sched.kill_all_tasks_except([my_tid])
00052         print 'Going for lunch'
00053         yield WaitDuration(1)
00054         print 'Mission done'
00055         
00056 def main_task():
00057         chlorophyll_tid = sched.new_task(chlorophyll_detector())
00058         energy_tid = sched.new_task(energy_monitoring())
00059         while True:
00060                 print 'Performing main business'
00061                 yield WaitDuration(1)
00062 
00063 sched = MyScheduler()
00064 sched.new_task(main_task())
00065 print 'Running scheduler'
00066 while sched.taskmap:
00067         # simulate external conditions
00068         sched.energy_level -= 3
00069         sched.chlorophyll_level = math.sin(float(sched.energy_level) / 30.) * 4
00070         # run ready timers
00071         sched.timer_step()
00072         # wait
00073         time.sleep(0.3)
00074         print 'Ext var: energy_level=' +str(sched.energy_level)+', chlorophyll_level='+str(sched.chlorophyll_level)
00075 print 'All tasks are dead, we better leave this place'
00076 


teer_ros
Author(s): Stéphane Magnenat
autogenerated on Sun Oct 5 2014 23:56:00