view org/test-creature.org @ 189:facc2ef3fe5c

added hearing debug view.
author Robert McIntyre <rlm@mit.edu>
date Sat, 04 Feb 2012 10:13:15 -0700
parents 22548d48cc85
children 2902aca33c6e
line wrap: on
line source
1 #+title: First attempt at a creature!
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
12 * Intro
13 So far, I've made the following senses --
14 - Vision
15 - Hearing
16 - Touch
17 - Proprioception
19 And one effector:
20 - Movement
22 However, the code so far has only enabled these senses, but has not
23 actually implemented them. For example, there is still a lot of work
24 to be done for vision. I need to be able to create an /eyeball/ in
25 simulation that can be moved around and see the world from different
26 angles. I also need to determine weather to use log-polar or cartesian
27 for the visual input, and I need to determine how/wether to
28 disceritise the visual input.
30 I also want to be able to visualize both the sensors and the
31 effectors in pretty pictures. This semi-retarted creature will be my
32 first attempt at bringing everything together.
34 * The creature's body
36 Still going to do an eve-like body in blender, but due to problems
37 importing the joints, etc into jMonkeyEngine3, I'm going to do all
38 the connecting here in clojure code, using the names of the individual
39 components and trial and error. Later, I'll maybe make some sort of
40 creature-building modifications to blender that support whatever
41 discritized senses I'm going to make.
43 #+name: body-1
44 #+begin_src clojure
45 (ns cortex.silly
46 "let's play!"
47 {:author "Robert McIntyre"})
49 ;; TODO remove this!
50 (require 'cortex.import)
51 (cortex.import/mega-import-jme3)
52 (use '(cortex world util body hearing touch vision sense
53 proprioception movement))
55 (rlm.rlm-commands/help)
56 (import java.awt.image.BufferedImage)
57 (import javax.swing.JPanel)
58 (import javax.swing.SwingUtilities)
59 (import java.awt.Dimension)
60 (import javax.swing.JFrame)
61 (import java.awt.Dimension)
62 (import com.aurellem.capture.RatchetTimer)
64 (use 'clojure.contrib.def)
66 (defn load-blender-model
67 "Load a .blend file using an asset folder relative path."
68 [^String model]
69 (.loadModel
70 (doto (asset-manager)
71 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
72 model))
74 (def hand "Models/creature1/one.blend")
76 (def worm "Models/creature1/try-again.blend")
78 (defn worm-model [] (load-blender-model worm))
80 (defn x-ray [#^ColorRGBA color]
81 (doto (Material. (asset-manager)
82 "Common/MatDefs/Misc/Unshaded.j3md")
83 (.setColor "Color" color)
84 (-> (.getAdditionalRenderState)
85 (.setDepthTest false))))
87 (defn colorful []
88 (.getChild (worm-model) "worm-21"))
90 (import jme3tools.converters.ImageToAwt)
92 (import ij.ImagePlus)
96 (defn test-eye []
97 (.getChild
98 (.getChild (worm-model) "eyes")
99 "eye"))
103 ;; lower level --- nodes
104 ;; closest-node "parse/compile-x" -> makes organ, which is spatial, fn pair
106 ;; higher level -- organs
107 ;;
109 ;; higher level --- sense/effector
110 ;; these are the functions that provide world i/o, chinese-room style
112 (defn debug-hearing-window
113 "view audio data"
114 [height]
115 (let [vi (view-image)]
116 (fn [[coords sensor-data]]
117 (let [image (BufferedImage. (count coords) height
118 BufferedImage/TYPE_INT_RGB)]
119 (dorun
120 (for [x (range (count coords))]
121 (dorun
122 (for [y (range height)]
123 (let [raw-sensor (sensor-data x)]
124 (.setRGB image x y (gray raw-sensor)))))))
126 (vi image)))))
128 (defn test-creature [thing]
129 (let [x-axis
130 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
131 y-axis
132 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
133 z-axis
134 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
136 creature (doto (load-blender-model thing) (body!))
138 touch (touch! creature)
139 touch-display (view-touch)
141 vision (vision! creature)
142 vision-display (view-vision)
144 hearing (hearing! creature)
145 hearing-display (view-hearing)
147 prop (proprioception! creature)
148 prop-debug (proprioception-debug-window)
150 muscle-fns (movement! creature)
153 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
154 bell (AudioNode. (asset-manager)
155 "Sounds/pure.wav" false)
157 fix-display (runonce
158 (fn [world] (add-camera! world (.getCamera world) no-op)))
159 ]
162 (apply
163 world
164 (with-movement
165 (.getChild creature "worm-21")
166 ["key-r" "key-t"
167 "key-f" "key-g"
168 "key-v" "key-b"]
169 [10 10 10 10 1 1]
170 [(nodify [creature
171 (box 10 2 10 :position (Vector3f. 0 -9 0)
172 :color ColorRGBA/Gray :mass 0)
173 x-axis y-axis z-axis
174 me
175 ])
176 (merge standard-debug-controls
177 {"key-return"
178 (fn [_ value]
179 (if value
180 (do
181 (println-repl "play-sound")
182 (.play bell))))
183 "key-h"
184 (fn [_ value]
185 (if value
186 (do
187 (println-repl "muscle activating!")
188 ((first muscle-fns) 199))))
190 })
191 (fn [world]
192 (light-up-everything world)
193 (enable-debug world)
194 ;;(dorun (map #(% world) init-vision-fns))
195 ;;(dorun (map #(% world) init-hearing-fns))
197 (add-camera! world
198 (add-eye! creature (test-eye))
199 (comp (view-image) BufferedImage!))
202 ;;(set-gravity world (Vector3f. 0 0 0))
203 ;;(com.aurellem.capture.Capture/captureVideo
204 ;; world (file-str "/home/r/proj/ai-videos/hand"))
205 ;;(.setTimer world (RatchetTimer. 60))
206 (speed-up world)
207 (set-gravity world (Vector3f. 0 0 0))
208 )
209 (fn [world tpf]
210 ;;(dorun
211 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))
213 (prop-debug (prop))
215 (touch-display (map #(% (.getRootNode world)) touch))
217 (vision-display (map #(% world) vision))
219 (hearing-display (map #(% world) hearing))
221 ;;(println-repl (vision-data))
222 (.setLocalTranslation me (.getLocation (.getCamera world)))
223 (fix-display world)
225 )]
226 ;;(let [timer (atom 0)]
227 ;; (fn [_ _]
228 ;; (swap! timer inc)
229 ;; (if (= (rem @timer 60) 0)
230 ;; (println-repl (float (/ @timer 60))))))
231 ))))
235 ;; the camera will stay in its initial position/rotation with relation
236 ;; to the spatial.
239 ;;dylan (defn follow-sense, adjoin-sense, attach-stimuli,
240 ;;anchor-qualia, augment-organ, with-organ
242 (defn follow-test
243 "show a camera that stays in the same relative position to a blue cube."
244 []
245 (let [camera-pos (Vector3f. 0 30 0)
246 rock (box 1 1 1 :color ColorRGBA/Blue
247 :position (Vector3f. 0 10 0)
248 :mass 30
249 )
250 rot (.getWorldRotation rock)
252 table (box 3 1 10 :color ColorRGBA/Gray :mass 0
253 :position (Vector3f. 0 -3 0))]
255 (world
256 (nodify [rock table])
257 standard-debug-controls
258 (fn [world]
259 (let
260 [cam (doto (.clone (.getCamera world))
261 (.setLocation camera-pos)
262 (.lookAt Vector3f/ZERO
263 Vector3f/UNIT_X))]
264 (bind-sense rock cam)
266 (.setTimer world (RatchetTimer. 60))
267 (add-camera! world cam (comp (view-image) BufferedImage!))
268 (add-camera! world (.getCamera world) no-op))
269 )
270 (fn [_ _] (println-repl rot)))))
274 #+end_src
276 #+results: body-1
277 : #'cortex.silly/follow-test
280 * COMMENT purgatory
281 #+begin_src clojure
283 (defn bullet-trans* []
284 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
285 :position (Vector3f. 5 0 0)
286 :mass 90)
287 obj-b (sphere 0.5 :color ColorRGBA/Blue
288 :position (Vector3f. -5 0 0)
289 :mass 0)
290 control-a (.getControl obj-a RigidBodyControl)
291 control-b (.getControl obj-b RigidBodyControl)
292 move-up? (atom nil)
293 move-down? (atom nil)
294 move-left? (atom nil)
295 move-right? (atom nil)
296 roll-left? (atom nil)
297 roll-right? (atom nil)
298 force 100
299 swivel
300 (.toRotationMatrix
301 (doto (Quaternion.)
302 (.fromAngleAxis (/ Math/PI 2)
303 Vector3f/UNIT_X)))
304 x-move
305 (doto (Matrix3f.)
306 (.fromStartEndVectors Vector3f/UNIT_X
307 (.normalize (Vector3f. 1 1 0))))
309 timer (atom 0)]
310 (doto
311 (ConeJoint.
312 control-a control-b
313 (Vector3f. -8 0 0)
314 (Vector3f. 2 0 0)
315 ;;swivel swivel
316 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
317 x-move Matrix3f/IDENTITY
318 )
319 (.setCollisionBetweenLinkedBodys false)
320 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
321 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
322 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
323 (world (nodify
324 [obj-a obj-b])
325 (merge standard-debug-controls
326 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
327 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
328 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
329 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
330 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
331 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
333 (fn [world]
334 (enable-debug world)
335 (set-gravity world Vector3f/ZERO)
336 )
338 (fn [world _]
340 (if @move-up?
341 (.applyForce control-a
342 (Vector3f. force 0 0)
343 (Vector3f. 0 0 0)))
344 (if @move-down?
345 (.applyForce control-a
346 (Vector3f. (- force) 0 0)
347 (Vector3f. 0 0 0)))
348 (if @move-left?
349 (.applyForce control-a
350 (Vector3f. 0 force 0)
351 (Vector3f. 0 0 0)))
352 (if @move-right?
353 (.applyForce control-a
354 (Vector3f. 0 (- force) 0)
355 (Vector3f. 0 0 0)))
357 (if @roll-left?
358 (.applyForce control-a
359 (Vector3f. 0 0 force)
360 (Vector3f. 0 0 0)))
361 (if @roll-right?
362 (.applyForce control-a
363 (Vector3f. 0 0 (- force))
364 (Vector3f. 0 0 0)))
366 (if (zero? (rem (swap! timer inc) 100))
367 (.attachChild
368 (.getRootNode world)
369 (sphere 0.05 :color ColorRGBA/Yellow
370 :physical? false :position
371 (.getWorldTranslation obj-a)))))
372 )
373 ))
375 (defn test-joint [joint]
376 (let [[origin top bottom floor] (world-setup joint)
377 control (.getControl top RigidBodyControl)
378 move-up? (atom false)
379 move-down? (atom false)
380 move-left? (atom false)
381 move-right? (atom false)
382 roll-left? (atom false)
383 roll-right? (atom false)
384 timer (atom 0)]
386 (world
387 (nodify [top bottom floor origin])
388 (merge standard-debug-controls
389 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
390 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
391 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
392 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
393 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
394 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
396 (fn [world]
397 (light-up-everything world)
398 (enable-debug world)
399 (set-gravity world (Vector3f. 0 0 0))
400 )
402 (fn [world _]
403 (if (zero? (rem (swap! timer inc) 100))
404 (do
405 ;; (println-repl @timer)
406 (.attachChild (.getRootNode world)
407 (sphere 0.05 :color ColorRGBA/Yellow
408 :position (.getWorldTranslation top)
409 :physical? false))
410 (.attachChild (.getRootNode world)
411 (sphere 0.05 :color ColorRGBA/LightGray
412 :position (.getWorldTranslation bottom)
413 :physical? false))))
415 (if @move-up?
416 (.applyTorque control
417 (.mult (.getPhysicsRotation control)
418 (Vector3f. 0 0 10))))
419 (if @move-down?
420 (.applyTorque control
421 (.mult (.getPhysicsRotation control)
422 (Vector3f. 0 0 -10))))
423 (if @move-left?
424 (.applyTorque control
425 (.mult (.getPhysicsRotation control)
426 (Vector3f. 0 10 0))))
427 (if @move-right?
428 (.applyTorque control
429 (.mult (.getPhysicsRotation control)
430 (Vector3f. 0 -10 0))))
431 (if @roll-left?
432 (.applyTorque control
433 (.mult (.getPhysicsRotation control)
434 (Vector3f. -1 0 0))))
435 (if @roll-right?
436 (.applyTorque control
437 (.mult (.getPhysicsRotation control)
438 (Vector3f. 1 0 0))))))))
439 #+end_src
442 * COMMENT generate source
443 #+begin_src clojure :tangle ../src/cortex/silly.clj
444 <<body-1>>
445 #+end_src