aboutsummaryrefslogtreecommitdiff
path: root/behaviors
diff options
context:
space:
mode:
authorGravatar rcoh <rcoh@mit.edu>2011-02-16 18:29:41 -0500
committerGravatar rcoh <rcoh@mit.edu>2011-02-16 18:29:41 -0500
commitfffc14c4672294d9bbf8c60edfe8c309f0d54698 (patch)
tree19057b2e98735cc30fa1e0b3932d416946f8d705 /behaviors
parent24aa31808de6a4dc06a651076e5b292aebd9240d (diff)
parent2df9e408a0ff74539862c4a4e562a878cc11a329 (diff)
Merge branch 'conner5' of https://github.com/dxiao/SmootLight into dxiao-conner5
Conflicts: config/C5Sign.xml layouts/SpecifiedLayout.py
Diffstat (limited to 'behaviors')
-rw-r--r--behaviors/Circle.py29
-rw-r--r--behaviors/ModifyParam.py1
-rw-r--r--behaviors/Oval.py36
-rw-r--r--behaviors/TimeSwitch.py25
-rw-r--r--behaviors/VerticalBar.py22
5 files changed, 112 insertions, 1 deletions
diff --git a/behaviors/Circle.py b/behaviors/Circle.py
new file mode 100644
index 0000000..24d71f1
--- /dev/null
+++ b/behaviors/Circle.py
@@ -0,0 +1,29 @@
+from operationscore.Behavior import *
+class Circle(Behavior):
+ def processResponse(self, sensors, recurs):
+ ret = []
+ for data in sensors:
+ #import pdb; pdb.set_trace()
+ if 'CenterLoc' in data:
+ xLoc = data['CenterLoc'][0]
+ yLoc = data['CenterLoc'][1]
+ else:
+ data['CenterLoc'] = tuple(data['Location'])
+ xLoc = data['Location'][0]
+ yLoc = data['Location'][1]
+ if not self['Id']+'Radius' in data:
+ 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)
+ if self['Combine']:
+ data['Location'] += ',' + circleStr
+ else:
+ data['Location'] = circleStr
+ ret.append(data)
+ return (ret, [])
+ def setLastOutput(self, output):
+ coutput = Behavior.deepCopyPacket(output)
+ for data in coutput:
+ data['Location'] = data['CenterLoc']
+ return coutput
diff --git a/behaviors/ModifyParam.py b/behaviors/ModifyParam.py
index 0ef3a60..6f81383 100644
--- a/behaviors/ModifyParam.py
+++ b/behaviors/ModifyParam.py
@@ -1,6 +1,5 @@
from operationscore.Behavior import *
import math
-import pdb
#Class to perform a given operation on some element of an argDict. Designed to be used a recursive hook, but can serve sensor-based functions as well. Specify ParamType (Sensor or Recurse), ParamName, and ParamOp, (a valid python statement with the old value represented as {val})
class ModifyParam(Behavior):
"""ModifyParam is a powerful class to perform an action on a specified key in the Argument
diff --git a/behaviors/Oval.py b/behaviors/Oval.py
new file mode 100644
index 0000000..b7486f5
--- /dev/null
+++ b/behaviors/Oval.py
@@ -0,0 +1,36 @@
+from operationscore.Behavior import *
+class Oval(Behavior):
+ def processResponse(self, sensors, recurs):
+ ret = []
+ for data in sensors:
+ #import pdb; pdb.set_trace()
+ height = width = 1
+ if 'Height' in self:
+ height = 1/float(self['Height'])
+ if 'Width' in self:
+ width = 1/float(self['Width'])
+ if 'CenterLoc' in data:
+ xLoc = data['CenterLoc'][0]
+ yLoc = data['CenterLoc'][1]
+ else:
+ data['CenterLoc'] = tuple(data['Location'])
+ xLoc = data['Location'][0]
+ yLoc = data['Location'][1]
+ if not self['Id']+'Radius' in data:
+ data[self['Id']+'Radius'] = self['Radius']
+ rad = data[self['Id']+'Radius']
+ cond = '>=' if self['Outside'] else '<='
+ circleStr = \
+ 'math.sqrt((({x}-%(xLoc)d))**2*%(width)d+(({y}-%(yLoc)d)**2)*%(height)d)%(cond)s%(rad)d' % \
+ locals()
+ if self['Combine']:
+ data['Location'] += ',' + circleStr
+ else:
+ data['Location'] = circleStr
+ ret.append(data)
+ return (ret, [])
+ def setLastOutput(self, output):
+ coutput = Behavior.deepCopyPacket(output)
+ for data in coutput:
+ data['Location'] = data['CenterLoc']
+ return coutput
diff --git a/behaviors/TimeSwitch.py b/behaviors/TimeSwitch.py
new file mode 100644
index 0000000..cfbfe4a
--- /dev/null
+++ b/behaviors/TimeSwitch.py
@@ -0,0 +1,25 @@
+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['TimeMap'].keys()[self.keyIndex]
+ self.behaviorStart = clock.time()
+
+ def processResponse(self, sensors, recurs):
+ if self.behaviorStart + self['TimeMap'][self.currentBehaviorId]*1000 <= clock.time():
+ self.keyIndex += 1
+ self.keyIndex = self.keyIndex % len(self['TimeMap'])
+ self.currentBehaviorId = self['TimeMap'].keys()[self.keyIndex]
+ self.behaviorStart = clock.time()
+ main_log.info('Switching behaviors')
+ sensors = [s for s in sensors if s['InputId'] == self['InputMap'][self.currentBehaviorId]]
+ return compReg.getComponent(self.currentBehaviorId).immediateProcessInput(sensors, recurs)
+
diff --git a/behaviors/VerticalBar.py b/behaviors/VerticalBar.py
new file mode 100644
index 0000000..85960cb
--- /dev/null
+++ b/behaviors/VerticalBar.py
@@ -0,0 +1,22 @@
+from operationscore.Behavior import *
+class VerticalBar(Behavior):
+
+ def processResponse(self, inputs, recurs):
+ ret = []
+ inputs = list(inputs)
+ for inputset in inputs:
+ inputset = dict(inputset)
+ if 'xLoc' not in inputset:
+ inputset['xLoc'] = inputset['Location'][0]
+ xLoc = inputset['xLoc']
+
+ condition = '{x} == ' + str(xLoc)
+
+ if self['Combine']:
+ inputset['Location'] += ',' + condition
+ else:
+ inputset['Location'] = condition
+
+ ret.append(inputset)
+ return (ret, [])
+