From 9f61ad1981febe1be8631c0ee24d8febd83db714 Mon Sep 17 00:00:00 2001 From: rcoh Date: Sun, 13 Feb 2011 18:14:01 -0500 Subject: Added TimeSwitch to switch between behaviors at set time intervals --- behaviors/Circle.py | 2 +- behaviors/TimeSwitch.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 behaviors/TimeSwitch.py (limited to 'behaviors') diff --git a/behaviors/Circle.py b/behaviors/Circle.py index 24d71f1..f3e103b 100644 --- a/behaviors/Circle.py +++ b/behaviors/Circle.py @@ -15,7 +15,7 @@ class Circle(Behavior): data[self['Id']+'Radius'] = self['Radius'] rad = data[self['Id']+'Radius'] cond = '>=' if self['Outside'] else '<=' - circleStr = 'math.sqrt(({x}-'+str(xLoc)+')**2+(({y}-'+str(yLoc)+')**2))'+cond+str(rad) + circleStr = 'math.sqrt(({x}-'+str(xLoc)+')**2+(({y}-'+str(yLoc)+')**2)*2)'+cond+str(rad) if self['Combine']: data['Location'] += ',' + circleStr else: diff --git a/behaviors/TimeSwitch.py b/behaviors/TimeSwitch.py new file mode 100644 index 0000000..2cedfcf --- /dev/null +++ b/behaviors/TimeSwitch.py @@ -0,0 +1,24 @@ +from operationscore.Behavior import * +import util.TimeOps as clock +import util.ComponentRegistry as compReg +from logger import main_log +class TimeSwitch(Behavior): + """TimeSwitch is a behavior that alternates between different behaviors for a set amount of time + (specify time in seconds. Specify in a python-style dict: + {'behaviorId1':60, 'behaviorId2':120} + Would alternate between the 2 behaviors, spending 1 minute on b1 and 2 minutes on b2. + """ + def behaviorInit(self): + self.keyIndex = 0 + self.currentBehaviorId = self['Behaviors'].keys()[self.keyIndex] + self.behaviorStart = clock.time() + + def processResponse(self, sensors, recurs): + if self.behaviorStart + self['Behaviors'][self.currentBehaviorId]*1000 <= clock.time(): + self.keyIndex += 1 + self.keyIndex = self.keyIndex % len(self['Behaviors']) + self.currentBehaviorId = self['Behaviors'].keys()[self.keyIndex] + self.behaviorStart = clock.time() + main_log.info('Switching behaviors') + return compReg.getComponent(self.currentBehaviorId).processResponse(sensors, recurs) + -- cgit v1.2.3