1 from joy import *
2
4
5
6
7
8 SHEET = loadCSV("inchworm.csv")
9
13
15
16 gait = GaitCyclePlan( self, self.SHEET, maxFreq = 1)
17 gait.setFrequency(0.2)
18
19
20
21
22
23
24
25 gait.onStart = curry(progress,">>> START")
26 gait.onStop = curry(progress,">>> STOP")
27 self.gait = gait
28
29
30
31 sf = StickFilter(self)
32 sf.setIntegrator( 'joy0axis0',10 ,lower=-9,upper=9)
33 sf.setIntegrator( 'joy0axis1',10 ,lower=-9,upper=9)
34 sf.setIntegrator( 'joy0axis2', 10 ,lower=-9,upper=9)
35 sf.setIntegrator( 'joy0axis3', 10 ,lower=-9,upper=9)
36 sf.setIntegrator( 'joy0axis4', 10 ,lower=-9,upper=9)
37 sf.start()
38 self.sf = sf
39
40 self.joyMode = False
41
42 self.oncePer = self.onceEvery(0.5)
43
47
49
50
51
52
53
54
55
56 if evt.type==QUIT or (evt.type==KEYDOWN and evt.key in [K_q,K_ESCAPE]):
57 self.stop()
58
59
60 if evt.type==JOYBUTTONDOWN:
61
62 if evt.button>3:
63 self.joyMode = not self.joyMode
64 if self.joyMode:
65 if self.gait.isRunning():
66 self.gait.stop()
67 progress("*"*20 + " joystick mode ON")
68 else:
69 progress("*"*20 + " joystick mode OFF")
70 self.robot.off()
71 return
72 else:
73 progress("!"*20 + " RESET: joystick OFF, gait STOPPED, modules SLACK")
74 if self.gait.isRunning():
75 self.gait.stop()
76 self.joyMode = False
77 self.robot.off()
78 return True
79
80
81 if self.joyMode:
82 if self.onEvent_joyMode(evt):
83 return
84 else:
85 if self.onEvent_gaitMode(evt):
86 return
87
88
89
90
91 JoyApp.onEvent(self,evt)
92
94
95 if evt.type==JOYAXISMOTION:
96 self.sf.push(evt)
97 return True
98 if evt.type==TIMEREVENT and :
99 lClaw = self.sf.getValue("joy0axis1")
100 lAng = self.sf.getValue("joy0axis0")
101 rClaw = self.sf.getValue("joy0axis3")
102 rAng = self.sf.getValue("joy0axis2")
103 body = self.sf.getValue("joy0axis4")
104 self.robot.at.w1.set_pos(lClaw * 1000)
105 self.robot.at.w2.set_pos(lAng * 1000)
106 self.robot.at.w3.set_pos(body * 1000)
107 self.robot.at.w4.set_pos(rAng * 1000)
108 self.robot.at.w5.set_pos(rClaw * 1000)
109 if self.oncePer():
110 progress("<<< %g %g < %g > %g %g >>>" % (lClaw,lAng,body,rClaw,rAng) )
111 return True
112
114
115 if evt.type!=KEYDOWN:
116 return False
117
118
119 if evt.key==K_ENTER:
120 if self.gait.isRunning():
121 self.gait.stop()
122 progress( "Gait stopped" )
123 return True
124 else:
125 self.gait.start()
126 progress( "Gait starting" )
127 return True
128
129
130 if evt.key==K_SPACE:
131 self.gait.setFrequency(0)
132 progress('Stopped motion')
133 return True
134
135
136 if evt.key in (K_UP,K_DOWN):
137 f = self.gait.getFrequency()
138
139 if evt.key==K_DOWN:
140 f -= 0.03
141 else:
142 f += 0.03
143 self.gait.setFrequency(f)
144 progress('Frequency changed to %.2f Hz' % self.gait.getFrequency())
145 return True
146
147
148 if evt.key in range(K_0,K_9+1):
149 self.gait.setFrequency(0)
150 phi = ('1234567890'.find(chr(evt.key)))/9.0
151 self.gait.moveToPhase( phi )
152 progress('Moved to phase %.2f' % phi)
153 return True
154
155 if __name__=="__main__":
156 print """
157 Inchworm Robot Demo
158 -------------------
159
160 This demo combines several JoyApp capabilities to give a full-fleged
161 application for controlling a 5-segment worm-like robot.
162
163 The demo uses a gait stored in 'inchworm.csv', which can be generated
164 by piping the output of demo-makeInchwormCSV.py, as follows:
165
166 $ python demos/demo-makeInchwormCSV.py > demos/inchworm.csv
167
168 To make this work with your own robot, make sure to name your modules
169 w1,w2,w3,w4 and w5 in order (the nodeNames entry in JoyApp.yml), and to
170 make sure that the line:
171 signs = array([1,-1,1,1,-1])
172 is changed to reflect the rotation directions of your modules.
173
174 Operation
175 ---------
176 When the demo runs, the operation can switch between two modes: the "joystick"
177 mode and the "gait" mode. In joystick mode, each of the first 5 game
178 controller axes controls a robot DOF directly. In gait mode, the keyboard
179 controls starting, stopping, stepping and jumping around in the gait cycle.
180
181 In addition, the first 4 game controller buttons are "panic" buttons that
182 reset the robot to a known state.
183
184 All other controller buttons toggle between joystick and gait modes.
185
186 In gait mode, the following keys are used:
187 [up] / [down] -- control frequency
188 [space] -- freeze in place
189 [1]...[9],[0] -- jump to a relative location in the cycle [1] start, [0] end
190 [enter] -- toggle gait on and off.
191 """
192 app=InchwormApp()
193 app.run()
194