aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar rcoh <rcoh@mit.edu>2011-02-13 18:14:01 -0500
committerGravatar rcoh <rcoh@mit.edu>2011-02-13 18:14:01 -0500
commit9f61ad1981febe1be8631c0ee24d8febd83db714 (patch)
treee45b046be9739e08f2c795a843a85feeb33b04b4
parent938a4e8b26d79b3f1935e03bde319c6d1d42e2cd (diff)
Added TimeSwitch to switch between behaviors at set time intervals
-rw-r--r--behaviors/Circle.py2
-rw-r--r--behaviors/TimeSwitch.py24
-rw-r--r--config/C5Sign.xml32
-rw-r--r--util/Config.py8
4 files changed, 47 insertions, 19 deletions
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:
+ <Behaviors>{'behaviorId1':60, 'behaviorId2':120}</Behaviors>
+ 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)
+
diff --git a/config/C5Sign.xml b/config/C5Sign.xml
index 02bd0a2..12cf6e6 100644
--- a/config/C5Sign.xml
+++ b/config/C5Sign.xml
@@ -164,6 +164,7 @@
<Id>xymove</Id>
<Id>ybounce</Id>
<Id>xbounce</Id>
+ <Id>longrecursivedecay</Id>
</ChainedBehaviors>
</Args>
</Behavior>
@@ -176,6 +177,17 @@
<ParamOp>4*math.sin({x}/float(40))</ParamOp>
</Args>
</Behavior>
+ <Behavior>
+ <Class>behaviors.TimeSwitch</Class>
+ <Args>
+ <Id>main</Id>
+ <Inputs>
+ <Id>center</Id>
+ </Inputs>
+ <Behaviors>{'runcolordecay':10,'expandingcircles':10}</Behaviors>
+ <RenderToScreen>True</RenderToScreen>
+ </Args>
+ </Behavior>
<Behavior>
<Class>behaviors.DebugBehavior</Class>
<Args>
@@ -194,19 +206,16 @@
<Width>20</Width>
</Args>
</Behavior>
- <Behavior Id="recursivedecay">
+ <Behavior Id="recursivedecay" InitialResponseCount="80">
+ <InheritsFrom>behaviors/LoopAndDie.xml</InheritsFrom>
+ </Behavior>
+ <Behavior Id="longrecursivedecay" InitialResponseCount="400">
<InheritsFrom>behaviors/LoopAndDie.xml</InheritsFrom>
- <Args>
- <InitialResponseCount>80</InitialResponseCount>
- </Args>
</Behavior>
<Behavior>
<Class>behaviors.BehaviorChain</Class>
<Args>
<Id>runcolordecay</Id>
- <Inputs>
- <Id>pygameclick</Id>
- </Inputs>
<ChainedBehaviors>
<Id>colorchange</Id>
<Id>mover</Id>
@@ -311,18 +320,13 @@
<Behavior>
<Class>behaviors.BehaviorChain</Class>
<Args>
- <Id>expandingcirlces</Id>
- <Inputs>
- <Id>pygameclick</Id>
- <Id>center</Id>
- </Inputs>
+ <Id>expandingcircles</Id>
<ChainedBehaviors>
<Id>colorchange</Id>
<Id>mover</Id>
- <Id>decay</Id>
+ <Id>singleframe</Id>
</ChainedBehaviors>
<RecursiveHooks>{'mover':'circle_expand'}</RecursiveHooks>
- <RenderToScreen>True</RenderToScreen>
</Args>
</Behavior>
<Behavior Id="running">
diff --git a/util/Config.py b/util/Config.py
index 25018a8..962aa25 100644
--- a/util/Config.py
+++ b/util/Config.py
@@ -26,8 +26,8 @@ def loadConfigFile(fileName): #TODO: error handling etc.
resolveDocumentInheritances(config.getroot())
return config
except Exception as inst:
- main_log.error('Error loading config file ' + fileName)#, inst) TODO: log exception too
- main_log.error(str(inst))
+ main_log.info('Error loading config file ' + fileName)#, inst) TODO: log exception too
+ main_log.info(str(inst))
return None
def getElement(el):
"""Takes an Element or an ElementTree. If it is a tree, it returns its root. Otherwise, just returns
@@ -89,7 +89,7 @@ def fileToDict(fileName):
for line in f:
fileText += line.rstrip('\n').lstrip('\t') + ' '
except IOError:
- exception_log.exception('Failure reading ' + fileName)
+ main_log.info('Failure reading ' + fileName)
return {}
if fileText == '':
return {}
@@ -98,7 +98,7 @@ def fileToDict(fileName):
main_log.info(fileName + ' read and parsed')
return resultDict
except:
- exception_log.info(fileName + ' is not a well formed python dict. Parsing failed')
+ main_log.exception(fileName + ' is not a well formed python dict. Parsing failed')
return eval(fileText)
def pullArgsFromItem(parentNode):