view org/integration.org @ 296:1eed471e2ebf

first version of a mini motor-control script language
author Robert McIntyre <rlm@mit.edu>
date Thu, 16 Feb 2012 11:04:22 -0700
parents 67a4e92d4628
children d1206b11ae2d
line wrap: on
line source
1 #+title:
2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu
4 #+description:
5 #+keywords: simulation, jMonkeyEngine3, clojure
6 #+SETUPFILE: ../../aurellem/org/setup.org
7 #+INCLUDE: ../../aurellem/org/level-0.org
9 * Intro
11 This is the ultimate test which features all of the senses that I've
12 made so far. The blender file for the creature serves as an example of
13 a fully equipped creature in terms of senses. You can find it [[../assets/Models/test-creature/hand.blend][here]].
15 #+name: integration
16 #+begin_src clojure
17 (ns cortex.integration
18 "let's play!"
19 {:author "Robert McIntyre"}
20 (:use (cortex world util body sense
21 hearing touch vision proprioception movement))
22 (:import (com.jme3.math ColorRGBA Vector3f))
23 (:import java.io.File)
24 (:import com.jme3.audio.AudioNode)
25 (:import com.aurellem.capture.RatchetTimer))
27 (dorun (cortex.import/mega-import-jme3))
28 (rlm.rlm-commands/help)
30 (def hand "Models/test-creature/hand.blend")
32 (def output-base (File. "/home/r/proj/cortex/render/hand"))
34 (defn motor-control-program
35 "Create a function which will execute the motor script"
36 [muscle-positions
37 script]
38 (let [current-frame (atom -1)
39 keyed-script (group-by first script)
40 current-forces (atom {}) ]
41 (fn [effectors]
42 (let [indexed-effectors (vec effectors)]
43 (dorun
44 (for [[_ part force] (keyed-script (swap! current-frame inc))]
45 (swap! current-forces (fn [m] (assoc m part force)))))
46 (doall (map (fn [effector power]
47 (effector (int power)))
48 effectors
49 (map #(@current-forces % 0) muscle-positions)))))))
51 (def muscle-positions
52 [:pointer-2-e
53 :pointer-2-f
54 :thumb-1
55 :thumb-1
56 :pointer-1-e
57 :pointer-1-f
58 :thumb-2-e
59 :thumb-2-f
60 :middle-1-e
61 :middle-1-f
62 :pointer-3-f
63 :pointer-3-e
64 :middle-2-e
65 :middle-2-f
66 :middle-3-f
67 :middle-3-e
68 :pinky-2-e
69 :pinky-2-f
70 :pinky-3-f
71 :pinky-3-e
72 :ring-3-e
73 :ring-3-f
74 :ring-2-f
75 :ring-2-e
76 :ring-1-e
77 :ring-1-f
78 :thumb-1-e
79 :thumb-1-f
80 :pinky-1-f
81 :pinky-1-e])
83 (def full 9001)
84 ;; the systax here is [keyframe body-part force]
85 (def wiggle-each-finger-tip
86 [[200 :pointer-3-f full]
87 [200 :pointer-3-f 40]
89 [400 :middle-3-f full]
90 [401 :middle-3-f 40]
92 [600 :ring-3-f full]
93 [601 :ring-3-f 40]
95 [800 :pinky-3-f full]
96 [801 :pinky-3-f 40]])
101 (def control-list
102 [
103 0 ;;pointer-21 #<Vector3f (0.99999994, 0.0, 0.0)>
104 0 ;;pointer-21 #<Vector3f (-0.99999994, 0.0, 0.0)>
105 0 ;;thumb-11 #<Vector3f (-0.8802276, -0.39781287, -0.25873658)>
106 0 ;;thumb-11 #<Vector3f (0.8485723, 0.46149826, 0.2587364)>
107 0 ;;pointer-11 #<Vector3f (0.99999994, 0.0, 0.0)>
108 0 ;;pointer-11 #<Vector3f (-0.99999994, 0.0, 0.0)>
109 0 ;;thumb-2.0011 #<Vector3f (-0.71705645, -0.44753736, -0.5343599)>
110 0 ;;thumb-2.0011 #<Vector3f (-0.10567085, 0.83862597, 0.53435963)>
111 0 ;;middle-11 #<Vector3f (0.99999994, 0.0, 0.0)>
112 0 ;;middle-11 #<Vector3f (-0.99999994, 0.0, 0.0)>
113 1 ;;pointer-31 #<Vector3f (-0.99999994, 0.0, 0.0)>
114 0 ;;pointer-31 #<Vector3f (0.99999994, 0.0, 0.0)>
115 0 ;;middle-21 #<Vector3f (0.99999994, 0.0, 0.0)>
116 0 ;;middle-21 #<Vector3f (-0.99999994, 0.0, 0.0)>
117 0 ;;middle-31 #<Vector3f (-0.99999994, 0.0, 0.0)>
118 0 ;;middle-31 #<Vector3f (0.99999994, 0.0, 0.0)>
119 0 ;;pinky-21 #<Vector3f (0.99999994, 0.0, 0.0)>
120 0 ;;pinky-21 #<Vector3f (-0.99999994, 0.0, 0.0)>
121 0 ;;pinky-31 #<Vector3f (-1, 0.0, 0.0)>
122 0 ;;pinky-31 #<Vector3f (1.0, 0.0, 0.0)>
123 0 ;;ring-31 #<Vector3f (0.99999994, 0.0, 0.0)>
124 0 ;;ring-31 #<Vector3f (-0.99999994, 0.0, 0.0)>
125 0 ;;ring-21 #<Vector3f (-0.99999994, 0.0, 0.0)>
126 0 ;;ring-21 #<Vector3f (0.99999994, 0.0, 0.0)>
127 0 ;;ring-11 #<Vector3f (0.99999994, 0.0, 0.0)>
128 0 ;;ring-11 #<Vector3f (-0.99999994, 0.0, 0.0)>
129 0 ;;thumb-11 #<Vector3f (-0.43154645, 0.7302033, 0.5296894)>
130 0 ;;thumb-11 #<Vector3f (-0.8032993, -0.2722854, -0.5296895)>
131 0 ;;pinky-11 #<Vector3f (-0.99999994, 0.0, 0.0)>
132 0 ;;pinky-11 #<Vector3f (0.99999994, 0.0, 0.0)>
133 ])
135 (defn test-everything!
136 ([] (test-everything! false))
137 ([record?]
138 (let [me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
140 bell (AudioNode. (asset-manager)
141 "Sounds/pure.wav" false)
142 creature (doto (load-blender-model hand)
143 (body!))
145 ;;;;;;;;;;;; Sensors/Effectors ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
146 ;;touch (touch! creature)
147 ;;touch-display (view-touch)
149 ;;vision (vision! creature)
150 ;;vision-display (view-vision)
152 ;;hearing (hearing! creature)
153 ;;hearing-display (view-hearing)
155 prop (proprioception! creature)
156 prop-display (view-proprioception)
158 control-script (motor-control-program
159 muscle-positions wiggle-each-finger-tip)
160 muscles (movement! creature)
161 muscle-display (view-movement)
162 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
164 fix-display (gen-fix-display)]
165 (world
166 (nodify [creature
167 (box 10 2 10 :position (Vector3f. 0 -9 0)
168 :color ColorRGBA/Gray :mass 0)
169 me])
170 (merge standard-debug-controls
171 {"key-return"
172 (fn [_ value]
173 (if value
174 (.play bell)))})
176 (fn [world]
177 (.setTimer world (RatchetTimer. 60))
178 (light-up-everything world)
179 (enable-debug world)
180 (add-camera! world
181 (add-eye! creature
182 (.getChild
183 (.getChild creature "eyes") "eye"))
184 (comp (view-image) BufferedImage!))
185 (speed-up world))
186 (fn [world tpf]
187 ;;(prop-display (prop))
188 ;;(touch-display (map #(% (.getRootNode world)) touch))
189 ;;(vision-display (map #(% world) vision))
190 ;;(hearing-display (map #(% world) hearing))
192 ;;(muscle-display
193 (control-script muscles)
194 ;;)
196 (.setLocalTranslation me (.getLocation (.getCamera world)))
197 (fix-display world))))))
198 #+end_src
200 #+results: integration
201 : #'cortex.integration/test-everything!
203 * COMMENT purgatory
204 #+begin_src clojure
205 (defn bullet-trans* []
206 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
207 :position (Vector3f. 5 0 0)
208 :mass 90)
209 obj-b (sphere 0.5 :color ColorRGBA/Blue
210 :position (Vector3f. -5 0 0)
211 :mass 0)
212 control-a (.getControl obj-a RigidBodyControl)
213 control-b (.getControl obj-b RigidBodyControl)
214 move-up? (atom nil)
215 move-down? (atom nil)
216 move-left? (atom nil)
217 move-right? (atom nil)
218 roll-left? (atom nil)
219 roll-right? (atom nil)
220 force 100
221 swivel
222 (.toRotationMatrix
223 (doto (Quaternion.)
224 (.fromAngleAxis (/ Math/PI 2)
225 Vector3f/UNIT_X)))
226 x-move
227 (doto (Matrix3f.)
228 (.fromStartEndVectors Vector3f/UNIT_X
229 (.normalize (Vector3f. 1 1 0))))
231 timer (atom 0)]
232 (doto
233 (ConeJoint.
234 control-a control-b
235 (Vector3f. -8 0 0)
236 (Vector3f. 2 0 0)
237 ;;swivel swivel
238 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
239 x-move Matrix3f/IDENTITY
240 )
241 (.setCollisionBetweenLinkedBodys false)
242 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
243 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
244 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
245 (world (nodify
246 [obj-a obj-b])
247 (merge standard-debug-controls
248 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
249 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
250 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
251 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
252 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
253 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
255 (fn [world]
256 (enable-debug world)
257 (set-gravity world Vector3f/ZERO)
258 )
260 (fn [world _]
262 (if @move-up?
263 (.applyForce control-a
264 (Vector3f. force 0 0)
265 (Vector3f. 0 0 0)))
266 (if @move-down?
267 (.applyForce control-a
268 (Vector3f. (- force) 0 0)
269 (Vector3f. 0 0 0)))
270 (if @move-left?
271 (.applyForce control-a
272 (Vector3f. 0 force 0)
273 (Vector3f. 0 0 0)))
274 (if @move-right?
275 (.applyForce control-a
276 (Vector3f. 0 (- force) 0)
277 (Vector3f. 0 0 0)))
279 (if @roll-left?
280 (.applyForce control-a
281 (Vector3f. 0 0 force)
282 (Vector3f. 0 0 0)))
283 (if @roll-right?
284 (.applyForce control-a
285 (Vector3f. 0 0 (- force))
286 (Vector3f. 0 0 0)))
288 (if (zero? (rem (swap! timer inc) 100))
289 (.attachChild
290 (.getRootNode world)
291 (sphere 0.05 :color ColorRGBA/Yellow
292 :physical? false :position
293 (.getWorldTranslation obj-a)))))
294 )
295 ))
297 (defn test-joint [joint]
298 (let [[origin top bottom floor] (world-setup joint)
299 control (.getControl top RigidBodyControl)
300 move-up? (atom false)
301 move-down? (atom false)
302 move-left? (atom false)
303 move-right? (atom false)
304 roll-left? (atom false)
305 roll-right? (atom false)
306 timer (atom 0)]
308 (world
309 (nodify [top bottom floor origin])
310 (merge standard-debug-controls
311 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
312 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
313 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
314 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
315 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
316 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
318 (fn [world]
319 (light-up-everything world)
320 (enable-debug world)
321 (set-gravity world (Vector3f. 0 0 0))
322 )
324 (fn [world _]
325 (if (zero? (rem (swap! timer inc) 100))
326 (do
327 ;; (println-repl @timer)
328 (.attachChild (.getRootNode world)
329 (sphere 0.05 :color ColorRGBA/Yellow
330 :position (.getWorldTranslation top)
331 :physical? false))
332 (.attachChild (.getRootNode world)
333 (sphere 0.05 :color ColorRGBA/LightGray
334 :position (.getWorldTranslation bottom)
335 :physical? false))))
337 (if @move-up?
338 (.applyTorque control
339 (.mult (.getPhysicsRotation control)
340 (Vector3f. 0 0 10))))
341 (if @move-down?
342 (.applyTorque control
343 (.mult (.getPhysicsRotation control)
344 (Vector3f. 0 0 -10))))
345 (if @move-left?
346 (.applyTorque control
347 (.mult (.getPhysicsRotation control)
348 (Vector3f. 0 10 0))))
349 (if @move-right?
350 (.applyTorque control
351 (.mult (.getPhysicsRotation control)
352 (Vector3f. 0 -10 0))))
353 (if @roll-left?
354 (.applyTorque control
355 (.mult (.getPhysicsRotation control)
356 (Vector3f. -1 0 0))))
357 (if @roll-right?
358 (.applyTorque control
359 (.mult (.getPhysicsRotation control)
360 (Vector3f. 1 0 0))))))))
361 #+end_src
364 * COMMENT generate source
365 #+begin_src clojure :tangle ../src/cortex/integration.clj
366 <<integration>>
367 #+end_src