From 5fb3ea060025241105dc8e9a174513c112f9a133 Mon Sep 17 00:00:00 2001 From: rcoh Date: Thu, 27 Jan 2011 16:50:59 -0500 Subject: A metric $#%$-ton of changes. Added doc-strings to EVERYTHING. Phew. Fixed a massive bug that increases performance in by up to a factor of 60. A bunch of new behaviors for the class. --- behaviors/AddPixelEvent.py | 5 +++++ behaviors/AllPixels.py | 11 +++++++---- behaviors/AllPixelsLeft.py | 1 + behaviors/BehaviorChain.py | 23 +++++++++++++++++++--- behaviors/ColorChangerBehavior.py | 18 ++++++++++++++++-- behaviors/DebugBehavior.py | 3 +++ behaviors/DecayBehavior.py | 1 + behaviors/DimColor.xml | 2 +- behaviors/DoorBehavior.py | 4 ---- behaviors/EchoBehavior.py | 2 ++ behaviors/Expand.py | 4 ++++ behaviors/MITDoors.py | 20 ++++++++++++++++++++ behaviors/ModifyParam.py | 9 +++++++++ behaviors/MoveBehavior.py | 3 +++ behaviors/RandomWalk.py | 3 +++ behaviors/RecursiveDecay.py | 5 +++++ behaviors/ResponseMover.py | 6 ++++-- behaviors/RestrictLocation.py | 10 ++++++++++ behaviors/RiseFall.py | 40 +++++++++++++++++++++++++++++++++++++++ behaviors/RiseFall.xml | 7 +++++++ behaviors/RunningBehavior.py | 5 +++++ behaviors/Square.py | 25 ++++++++++++++---------- behaviors/TimedDie.py | 15 +++++++++++++++ behaviors/Timeout.py | 16 ++++++++++++++++ behaviors/XYMove.py | 7 +++++++ 25 files changed, 219 insertions(+), 26 deletions(-) delete mode 100644 behaviors/DoorBehavior.py create mode 100644 behaviors/MITDoors.py create mode 100644 behaviors/RiseFall.py create mode 100644 behaviors/RiseFall.xml create mode 100644 behaviors/TimedDie.py create mode 100644 behaviors/Timeout.py (limited to 'behaviors') diff --git a/behaviors/AddPixelEvent.py b/behaviors/AddPixelEvent.py index 7f134e1..821f432 100644 --- a/behaviors/AddPixelEvent.py +++ b/behaviors/AddPixelEvent.py @@ -2,6 +2,9 @@ from operationscore.Behavior import * import util.Strings as Strings from logger import main_log class AddPixelEvent(Behavior): + """AddPixelEvent is a behavior to append an arbitrary PixelEvent to a behavior response. The + classname of the PixelEvent should be specified in the Class field of Args. All arguments normally + passed to the PixelEvent should also be specified in Args.""" def behaviorInit(self): [module, className] = self['Class'].split('.') try: @@ -18,6 +21,8 @@ class AddPixelEvent(Behavior): ret = [] for sensory in sensors: outDict = {} + if not 'Location' in sensory: + pdb.set_trace() outDict[Strings.LOCATION] = sensory[Strings.LOCATION] settingsDict = dict(self.argDict) settingsDict['Color'] = sensory['Color'] diff --git a/behaviors/AllPixels.py b/behaviors/AllPixels.py index e155e55..7f66ad6 100644 --- a/behaviors/AllPixels.py +++ b/behaviors/AllPixels.py @@ -1,6 +1,9 @@ from operationscore.Behavior import * class AllPixels(Behavior): - def processResponse(self, sensorInputs, recursiveInputs): - for sensory in sensorInputs:#TODO: consider replicating the dict - sensory['Location'] = 'True' - return (sensorInputs, recursiveInputs) + """Turns on all Pixels in the installation. Must use SimpleMapper, or other Mapper supporting + conditional pixel locations.""" + + def processResponse(self, sensorInputs, recursiveInputs): + for sensory in sensorInputs:#TODO: consider replicating the dict + sensory['Location'] = 'True' + return (sensorInputs, recursiveInputs) diff --git a/behaviors/AllPixelsLeft.py b/behaviors/AllPixelsLeft.py index b48bfc1..b223156 100644 --- a/behaviors/AllPixelsLeft.py +++ b/behaviors/AllPixelsLeft.py @@ -2,6 +2,7 @@ from operationscore.Behavior import * import util.ComponentRegistry as compReg import pdb class AllPixelsLeft(Behavior): + """Behavior which returns all points left of its input. No Args.""" def processResponse(self, sensorInputs, recursiveInputs): for sensory in sensorInputs: xLoc = sensory['Location'][0] diff --git a/behaviors/BehaviorChain.py b/behaviors/BehaviorChain.py index 39f4402..631ad98 100644 --- a/behaviors/BehaviorChain.py +++ b/behaviors/BehaviorChain.py @@ -3,6 +3,24 @@ import util.ComponentRegistry as compReg from logger import main_log import pdb class BehaviorChain(Behavior): + """BehaviorChain is a class which chains together multiple behavior. BehaviorChain is in itself a + behavior, and behaves and can be used accordingly. BehaviorChain also supports recursive hooks to + be set on its constituent behaviors. ChainedBehaviors should be specified in as follows: + + + behavior1Id + behavior2Id + + + Behaviors may also be appended programmatically via the appendBehavior method. + + Recursive hooks should be specified with Python dict syntax as follows: + + {'behavior1Id':'hookid'} + + Behavior Chain manages all recurrences that its constituents propogate. At this point, it does not + support recurrences in its hooks.""" + def behaviorInit(self): self.feedback = {} #dictionary to allow feedback of recursives self.hooks = self['RecursiveHooks'] @@ -27,11 +45,10 @@ class BehaviorChain(Behavior): hookBehavior.immediateProcessInput(recurrence, \ []) if hookRecurrence != []: - main_log.warn('Hook recurrences are not currently supported. Implement it\ -yourself or bug russell') + main_log.warn('Hook recurrences are not currently supported.') self.feedback[behaviorId] = recurrence return (response, []) - + def appendBehavior(behavior): bid = compReg.registerComponent(behavior) #register behavior (will make #a new id if there isn't one) diff --git a/behaviors/ColorChangerBehavior.py b/behaviors/ColorChangerBehavior.py index 2a8d974..e2f8bd3 100644 --- a/behaviors/ColorChangerBehavior.py +++ b/behaviors/ColorChangerBehavior.py @@ -2,12 +2,26 @@ from operationscore.Behavior import * import util.ColorOps as color import pdb class ColorChangerBehavior(Behavior): + """ColorChangerBehavior is a behavior for adding colors to responses. If given no arguments, it + will generate a random color. If it is given a list of colors [as below] it will pick randomly + from them. + + + (255,0,0) + (30,79,200) + + + ColorList also supports specification of a single color.""" + def processResponse(self, sensorInputs, recursiveInputs): ret = [] for sensory in sensorInputs: - newDict = dict(sensory) #don't run into shallow copy issues + newDict = dict(sensory) if self['ColorList'] != None: - newDict['Color'] = color.chooseRandomColor(self['ColorList']) #TODO: this doesn't work. + if isinstance(self['ColorList'], list): + newDict['Color'] = color.chooseRandomColor(self['ColorList']) #Pick randomly + else: + newDict['Color'] = self['ColorList'] #Unless there is only one else: newDict['Color'] = color.randomColor() ret.append(newDict) diff --git a/behaviors/DebugBehavior.py b/behaviors/DebugBehavior.py index 17383db..8f81954 100644 --- a/behaviors/DebugBehavior.py +++ b/behaviors/DebugBehavior.py @@ -2,6 +2,9 @@ from operationscore.Behavior import * from logger import main_log import pdb class DebugBehavior(Behavior): + """DebugBehavior simply writes all of its inputs to the logs, currently at the ERROR level for + easy visibility. Will be changed to DEBUG or INFO in the future""" + def processResponse(self, sensorInputs, recursiveInputs): if sensorInputs != []: main_log.error('Sensor Inputs: ' + str(sensorInputs)) diff --git a/behaviors/DecayBehavior.py b/behaviors/DecayBehavior.py index c1f6f92..f19ffc8 100644 --- a/behaviors/DecayBehavior.py +++ b/behaviors/DecayBehavior.py @@ -3,6 +3,7 @@ from pixelevents.DecayEvent import * import util.Strings as Strings import pdb class DecayBehavior(Behavior): + """DecayBehavior is obsolete. Use AddPixelEvent instead""" def processResponse(self, sensorInputs, recursiveInputs): ret = [] for sensory in sensorInputs: diff --git a/behaviors/DimColor.xml b/behaviors/DimColor.xml index b01908e..58b0673 100644 --- a/behaviors/DimColor.xml +++ b/behaviors/DimColor.xml @@ -3,6 +3,6 @@ Sensor Color - [chan*.99 for chan in {val}] + [chan*.95 for chan in {val}] diff --git a/behaviors/DoorBehavior.py b/behaviors/DoorBehavior.py deleted file mode 100644 index 5058f69..0000000 --- a/behaviors/DoorBehavior.py +++ /dev/null @@ -1,4 +0,0 @@ -from operationscore.behavior import * -class DoorBehavior.py(Behavior): - def behaviorInit(self): - pyt diff --git a/behaviors/EchoBehavior.py b/behaviors/EchoBehavior.py index 589c42b..6ef4fcb 100644 --- a/behaviors/EchoBehavior.py +++ b/behaviors/EchoBehavior.py @@ -2,6 +2,8 @@ from operationscore.Behavior import * import util.Strings as Strings import pdb class EchoBehavior(Behavior): + """EchoBehavior generates a RED response at all locations specified in sensorInputs. Useful for + debugging""" def processResponse(self, sensorInputs, recursiveInputs): ret = [] for sensory in sensorInputs: diff --git a/behaviors/Expand.py b/behaviors/Expand.py index 3415966..323e71f 100644 --- a/behaviors/Expand.py +++ b/behaviors/Expand.py @@ -1,5 +1,9 @@ from operationscore.Behavior import * class Expand(Behavior): + """Expand is a behavior that generates a response that grows horizontally starting a location + specifed in input. Required Args: + 123 which is the expandrate in units/response""" + def processResponse(self, sensorInputs, recurs): ret = [] for data in sensorInputs: diff --git a/behaviors/MITDoors.py b/behaviors/MITDoors.py new file mode 100644 index 0000000..d602a55 --- /dev/null +++ b/behaviors/MITDoors.py @@ -0,0 +1,20 @@ +from operationscore.Behavior import * +class MITDoors(Behavior): + """MITDoors is a case-specific behavior to map keypresses to specific locations. Written for + Kuan 1/26/11 by RCOH""" + + def behaviorInit(self): + self.keymapping = {'q':[2,19], 'w':[22,36], 'e':[37,49], 'r':[52,69], 't':[76,91], 'y':[94,105], + 'u':[106,117], 'i':[123,154], 'o':[158,161], 'p':[164,167], '[':[172,184]} + def processResponse(self, sensorInputs, recursiveInputs): + ret = [] + for data in sensorInputs: + key = chr(data['Key']) + if key in self.keymapping: + bounds = self.keymapping[key] + data = dict(data) + data['Left'], data['Right'] = bounds + data['Bottom'] = self['Bottom'] + data['Location'] = (sum(bounds) / 2., self['Bottom']) + ret.append(data) + return (ret, []) diff --git a/behaviors/ModifyParam.py b/behaviors/ModifyParam.py index f589e05..4f45be0 100644 --- a/behaviors/ModifyParam.py +++ b/behaviors/ModifyParam.py @@ -2,8 +2,17 @@ from operationscore.Behavior import * 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 + Dictionary of a response. Specify: + -- Sensor or Recurse + -- The name of the parameter you wish to modify + -- The modification you wish to do. Use {val} to specify the current value of the + parameter in question. Special hooks for {x} and {y} exist in some versions""" + def processResponse(self, sensorInputs, recursiveInputs): paramType = self['ParamType'] + if paramType == None: + paramType = 'Sensor' paramName = self['ParamName'] paramOp = str(self['ParamOp']) if paramType == 'Sensor': diff --git a/behaviors/MoveBehavior.py b/behaviors/MoveBehavior.py index 590f0e4..d2f60a0 100644 --- a/behaviors/MoveBehavior.py +++ b/behaviors/MoveBehavior.py @@ -4,6 +4,9 @@ import util.Geo as Geo import util.Strings as Strings class MoveBehavior(Behavior): + """Moves current location by the x and y components of sensorInput. Uses recurrences to track + current input. @Author: Euguene""" + def processResponse(self, sensorInputs, recursiveInputs): if recursiveInputs: currRecLocs = recursiveInputs diff --git a/behaviors/RandomWalk.py b/behaviors/RandomWalk.py index fd6c2c8..c9049af 100644 --- a/behaviors/RandomWalk.py +++ b/behaviors/RandomWalk.py @@ -5,6 +5,9 @@ import util.Strings as Strings import random import pdb class RandomWalk(Behavior): + """Behavior to move the curent location by a random distance specified by + -- StepSize in units/response""" + def processResponse(self, sensors, recursives): ret = [] s = self['StepSize'] diff --git a/behaviors/RecursiveDecay.py b/behaviors/RecursiveDecay.py index 218813d..0ae21ea 100644 --- a/behaviors/RecursiveDecay.py +++ b/behaviors/RecursiveDecay.py @@ -1,6 +1,11 @@ from operationscore.Behavior import * import pdb class RecursiveDecay(Behavior): + """RecursiveDecay is an event to allow recursive hooks to stop recursing after a certain number + of iterations specified in + -- Int, number of total responses. + Designed to be used as part of a recursive hook. + """ def processResponse(self, sensorInputs, recursiveInputs): ret = [] for response in sensorInputs: diff --git a/behaviors/ResponseMover.py b/behaviors/ResponseMover.py index e1faccb..59e353a 100644 --- a/behaviors/ResponseMover.py +++ b/behaviors/ResponseMover.py @@ -1,9 +1,11 @@ import pdb from operationscore.Behavior import * import util.ComponentRegistry as compReg -#ResponseMover is a scaffold for behaviors that spawn 'walkers' which act autonomously on input. -#Add a recursive hook to control the movement. class ResponseMover(Behavior): + """ResponseMover is a scaffold for behaviors that spawn 'walkers' which act autonomously on input. + To control the movment, use the behavior as part of a BehaviorChain and add a recursive hook which + modulates the location.""" + def processResponse(self, sensorInputs, recursiveInputs): newResponses = sensorInputs ret = [] diff --git a/behaviors/RestrictLocation.py b/behaviors/RestrictLocation.py index f6c26ff..5e12440 100644 --- a/behaviors/RestrictLocation.py +++ b/behaviors/RestrictLocation.py @@ -6,6 +6,16 @@ import util.Strings as Strings import random import pdb class RestrictLocation(Behavior): + """RestrictLocation is a Behavior which does an action -- A ModifyParam, actually, when a certain + location based condition is met. It takes arguments as follows: + + -- Operation to perform, using ModifyParam syntax. Use {val} to reference the variable + specified by ParamName. + -- the name of the parameter to modify. + -- either a tuple of (xmin,ymin,xmax,ymax) or a python-correct conditional. Use {x} and + {y} to reference x and y. Use < and > to get < and > in XML. EG: + {x}<0 or {x}>800""" + def behaviorInit(self): action = self['Action'] modifyParamArgs = {'ParamType': 'Sensor', diff --git a/behaviors/RiseFall.py b/behaviors/RiseFall.py new file mode 100644 index 0000000..109cd10 --- /dev/null +++ b/behaviors/RiseFall.py @@ -0,0 +1,40 @@ +from operationscore.Behavior import * +import math +import util.TimeOps as timeOps +#Required Args: +#Period (ms), MaxHeight, Width +class RiseFall(Behavior): + """RiseFall is a behavior that creates a rising and falling column of light. Specify: + -- the maximum height that it rises to. + -- the width of the column OR and + -- the period of oscillation in ms + + Designed to be used as part of a recursive hook. + """ + + def processResponse(self, sensorInputs, recurInputs): + ret = [] + for data in sensorInputs: + #first time with behavior: + data = dict(data) + if not 'StartTime' in data: + data['StartTime'] = timeOps.time() + data['Period'] = self['Period'] + data['MaxHeight'] = self['MaxHeight'] #Consider just using += + if not 'Bottom' in data: + data['Bottom'] = data['Location'][1] + if 'Width' in self: #TODO: improve + data['Width'] = self['Width'] + data['Left'] = data['Location'][0]-data['Width']/2. + data['Right'] = data['Location'][0]+data['Width']/2. + currentTime = timeOps.time() + deltaTime = currentTime-data['StartTime'] + data['Height'] = data['MaxHeight']*math.sin(deltaTime/data['Period']*(math.pi*2)) + data['Location'] = "{x}>"+str(data['Left']) + ", " +\ + "{x}<"+str(data['Right'])+", {y}<" + str(data['Bottom']) + ",\ + {y}>"+str(data['Bottom']-data['Height']) + + ret.append(data) + return (ret, []) + + diff --git a/behaviors/RiseFall.xml b/behaviors/RiseFall.xml new file mode 100644 index 0000000..eaadd7b --- /dev/null +++ b/behaviors/RiseFall.xml @@ -0,0 +1,7 @@ + + behaviors.RiseFall + + 2000 + 50 + + diff --git a/behaviors/RunningBehavior.py b/behaviors/RunningBehavior.py index 5eb33f7..ab3dc80 100644 --- a/behaviors/RunningBehavior.py +++ b/behaviors/RunningBehavior.py @@ -3,6 +3,11 @@ import util.ComponentRegistry as compReg import util.Geo as Geo import pdb class RunningBehavior(Behavior): + """RunningBehavior is a straightforward behavior that makes a Location run back and forth across + a screen. Specify: + -- the length of movment in units when the response moves. + """ + def processResponse(self, sensorInputs, recursiveInputs): newResponses = sensorInputs ret = [] diff --git a/behaviors/Square.py b/behaviors/Square.py index aef3622..9d3223a 100644 --- a/behaviors/Square.py +++ b/behaviors/Square.py @@ -1,12 +1,17 @@ from operationscore.Behavior import * class Square(Behavior): - def processResponse(self, sensorInputs, recursiveInputs): - for sensory in sensorInputs:#TODO: consider replicating the dict - xLoc = sensory['Location'][0] - yLoc = sensory['Location'][1] - width = self['Width'] - #sensory['Location'] = 'True' - sensory['Location'] =\ - '{x}<'+str(xLoc+width)+',{x}>'+str(xLoc-width)+\ - ',{y}<'+str(yLoc+width)+',{y}>'+str(yLoc-width) - return (sensorInputs, recursiveInputs) + """Square is a simple behavior that makes a square with side lengths Width*2 around locations in + the sensor input. Specify: + -- the sidelength/2 + """ + + def processResponse(self, sensorInputs, recursiveInputs): + for sensory in sensorInputs:#TODO: consider replicating the dict + xLoc = sensory['Location'][0] + yLoc = sensory['Location'][1] + width = self['Width'] + #sensory['Location'] = 'True' + sensory['Location'] =\ + '{x}<'+str(xLoc+width)+',{x}>'+str(xLoc-width)+\ + ',{y}<'+str(yLoc+width)+',{y}>'+str(yLoc-width) + return (sensorInputs, recursiveInputs) diff --git a/behaviors/TimedDie.py b/behaviors/TimedDie.py new file mode 100644 index 0000000..e75e9dd --- /dev/null +++ b/behaviors/TimedDie.py @@ -0,0 +1,15 @@ +from operationscore.Behavior import * +class Timeout(Behavior): + """Timeout is a behavior designed to be used in recursive hooks to stop responses after a certain + amount of time. It is the Time-version of RecursiveDecay. Specify: + -- the time in ms that the response will run. + """ + + def processResponse(self, sensorInputs, recur): + ret = [] + for data in sensorInputs: + if not 'StartTime' in data: + data['StartTime'] = timeops.time() + if timeops.time()-data['StartTime'] < self['Timeout']: + ret.append(data) + return (ret, []) diff --git a/behaviors/Timeout.py b/behaviors/Timeout.py new file mode 100644 index 0000000..14d4873 --- /dev/null +++ b/behaviors/Timeout.py @@ -0,0 +1,16 @@ +from operationscore.Behavior import * +import util.TimeOps as timeops +class Timeout(Behavior): + """Timeout is a behavior designed to be used in recursive hooks to stop responses after a certain + amount of time. It is the Time-version of RecursiveDecay. Specify: + -- the time in ms that the response will run. + """ + + def processResponse(self,sensorInputs, recur): + ret = [] + for data in sensorInputs: + if not 'StartTime' in data: + data['StartTime'] = timeops.time() + if timeops.time()-data['StartTime'] < self['Timeout']: + ret.append(data) + return (ret,[]) diff --git a/behaviors/XYMove.py b/behaviors/XYMove.py index 8acbba8..11cee96 100644 --- a/behaviors/XYMove.py +++ b/behaviors/XYMove.py @@ -1,6 +1,13 @@ from operationscore.Behavior import * import util.Geo as Geo class XYMove(Behavior): + """XYMove is a behavior designed to be used as a recursive hook to ResponseMover to move pixels by + XStep and YStep. As XStep and YStep are maintained in the responses itself, they can be + modulated to facilitate, acceleration, modulation, bouncing, etc. Specify: + -- the starting XStep + -- the starting YStep + """ + def processResponse(self, sensor, recurs): ret = [] for loc in sensor: -- cgit v1.2.3