Script demo_stickFilter_py
[hide private]
[frames] | no frames]

Source Code for Script script-demo_stickFilter_py

 1  from joy.decl import * 
 2  from joy import JoyApp, StickFilter 
 3  from pygame.event import Event as pygame_Event 
 4   
5 -class StickFilterApp( JoyApp ):
6 - def __init__(self,*arg,**kw):
7 JoyApp.__init__(self,*arg,**kw)
8
9 - def onStart( self ):
10 # Set up a StickFilter Plan 11 sf = StickFilter(self) 12 # Event channels can be names using a pygame event object, or using string 13 # names that follow a very strict structure. For tutorial puposes we 14 # show both options here. 15 evt = pygame_Event(JOYAXISMOTION,joy=0,axis=0,value=None) 16 # This channel is lowpass filtered -- useful for direct position control 17 sf.setLowpass( evt, 5 ) 18 # This channel is integrated, saturating at +/- 9. 19 sf.setIntegrator( "joy0axis1", lower=-9, upper=9) 20 # This channel is integrated, saturating at +/- 9. 21 sf.setIntegrator( "midi3sc1slider2", lower=-9, upper=9, func = lambda x : (x-63.5)/64 ) 22 # Start it up, otherwise nothing will happen... 23 sf.start() 24 self.sf = sf 25 # Set up a temporal filter for controlling the rate of our messages to the 26 # operator. In this case, we want 4 messages a second 27 self.timeToShow = self.onceEvery(0.25)
28
29 - def onEvent( self, evt ):
30 # If its time to show the operator a status message --> do so 31 if self.timeToShow(): 32 progress("State x %.3f y %.3f sc1slider2 %.3f" % ( 33 self.sf.getValue("joy0axis0"), self.sf.getValue("joy0axis1"), self.sf.getValue("midi3sc1slider2") 34 )) 35 # Send all joystick motion events to our StickFilter 36 if evt.type in [JOYAXISMOTION,MIDIEVENT]: 37 self.sf.push(evt) 38 return 39 # If we got this far, let our superclass handle the event 40 # This provides a debug printout and the option to quit via [escape] or [q] 41 JoyApp.onEvent(self,evt)
42 43 if __name__=="__main__": 44 print """ 45 Demonstration of the StickFilter plan 46 ------------------------------------- 47 48 Shows the use of the StickFilter plan, which allows arbitrary 49 linear filters to be applied to input channels. This includes 50 integrating joystick inputs, smoothing encoder measurements, 51 etc. 52 53 The example shows how axis 0 can be set to low-pass filtering, 54 and axis 1 to integration. 55 """ 56 app = StickFilterApp() 57 app.run() 58