view org/test-creature.org @ 188:22548d48cc85

added debug for vision
author Robert McIntyre <rlm@mit.edu>
date Sat, 04 Feb 2012 10:00:37 -0700
parents cfb71209ddc6
children facc2ef3fe5c
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)
135 creature (doto
136 (load-blender-model thing)
137 (body!))
138 touch (touch! creature)
139 touch-display (view-touch)
140 vision (vision! creature)
141 vision-display (view-vision)
142 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
143 hearing-senses (hearing! creature)
144 hearing-windows (map (fn [_] (debug-hearing-window 50))
145 hearing-senses)
146 bell (AudioNode. (asset-manager)
147 "Sounds/pure.wav" false)
148 prop (proprioception! creature)
149 prop-debug (proprioception-debug-window)
151 muscle-fns (movement! creature)
152 ;; dream
153 fix-display (runonce
154 (fn [world] (add-camera! world (.getCamera world) no-op)))
155 ]
158 (apply
159 world
160 (with-movement
161 (.getChild creature "worm-21")
162 ["key-r" "key-t"
163 "key-f" "key-g"
164 "key-v" "key-b"]
165 [10 10 10 10 1 1]
166 [(nodify [creature
167 (box 10 2 10 :position (Vector3f. 0 -9 0)
168 :color ColorRGBA/Gray :mass 0)
169 x-axis y-axis z-axis
170 me
171 ])
172 (merge standard-debug-controls
173 {"key-return"
174 (fn [_ value]
175 (if value
176 (do
177 (println-repl "play-sound")
178 (.play bell))))
179 "key-h"
180 (fn [_ value]
181 (if value
182 (do
183 (println-repl "muscle activating!")
184 ((first muscle-fns) 199))))
186 })
187 (fn [world]
188 (light-up-everything world)
189 (enable-debug world)
190 ;;(dorun (map #(% world) init-vision-fns))
191 ;;(dorun (map #(% world) init-hearing-fns))
193 (add-camera! world
194 (add-eye! creature (test-eye))
195 (comp (view-image) BufferedImage!))
198 ;;(set-gravity world (Vector3f. 0 0 0))
199 ;;(com.aurellem.capture.Capture/captureVideo
200 ;; world (file-str "/home/r/proj/ai-videos/hand"))
201 ;;(.setTimer world (RatchetTimer. 60))
202 (speed-up world)
203 (set-gravity world (Vector3f. 0 0 0))
204 )
205 (fn [world tpf]
206 ;;(dorun
207 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))
209 (prop-debug (prop))
211 (touch-display (map #(% (.getRootNode world)) touch))
213 (vision-display (map #(% world) vision))
215 (dorun
216 (map #(%1 (%2 world)) hearing-windows hearing-senses))
219 ;;(println-repl (vision-data))
220 (.setLocalTranslation me (.getLocation (.getCamera world)))
221 (fix-display world)
223 )]
224 ;;(let [timer (atom 0)]
225 ;; (fn [_ _]
226 ;; (swap! timer inc)
227 ;; (if (= (rem @timer 60) 0)
228 ;; (println-repl (float (/ @timer 60))))))
229 ))))
233 ;; the camera will stay in its initial position/rotation with relation
234 ;; to the spatial.
237 ;;dylan (defn follow-sense, adjoin-sense, attach-stimuli,
238 ;;anchor-qualia, augment-organ, with-organ
240 (defn follow-test
241 "show a camera that stays in the same relative position to a blue cube."
242 []
243 (let [camera-pos (Vector3f. 0 30 0)
244 rock (box 1 1 1 :color ColorRGBA/Blue
245 :position (Vector3f. 0 10 0)
246 :mass 30
247 )
248 rot (.getWorldRotation rock)
250 table (box 3 1 10 :color ColorRGBA/Gray :mass 0
251 :position (Vector3f. 0 -3 0))]
253 (world
254 (nodify [rock table])
255 standard-debug-controls
256 (fn [world]
257 (let
258 [cam (doto (.clone (.getCamera world))
259 (.setLocation camera-pos)
260 (.lookAt Vector3f/ZERO
261 Vector3f/UNIT_X))]
262 (bind-sense rock cam)
264 (.setTimer world (RatchetTimer. 60))
265 (add-camera! world cam (comp (view-image) BufferedImage!))
266 (add-camera! world (.getCamera world) no-op))
267 )
268 (fn [_ _] (println-repl rot)))))
272 #+end_src
274 #+results: body-1
275 : #'cortex.silly/follow-test
278 * COMMENT purgatory
279 #+begin_src clojure
281 (defn bullet-trans* []
282 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
283 :position (Vector3f. 5 0 0)
284 :mass 90)
285 obj-b (sphere 0.5 :color ColorRGBA/Blue
286 :position (Vector3f. -5 0 0)
287 :mass 0)
288 control-a (.getControl obj-a RigidBodyControl)
289 control-b (.getControl obj-b RigidBodyControl)
290 move-up? (atom nil)
291 move-down? (atom nil)
292 move-left? (atom nil)
293 move-right? (atom nil)
294 roll-left? (atom nil)
295 roll-right? (atom nil)
296 force 100
297 swivel
298 (.toRotationMatrix
299 (doto (Quaternion.)
300 (.fromAngleAxis (/ Math/PI 2)
301 Vector3f/UNIT_X)))
302 x-move
303 (doto (Matrix3f.)
304 (.fromStartEndVectors Vector3f/UNIT_X
305 (.normalize (Vector3f. 1 1 0))))
307 timer (atom 0)]
308 (doto
309 (ConeJoint.
310 control-a control-b
311 (Vector3f. -8 0 0)
312 (Vector3f. 2 0 0)
313 ;;swivel swivel
314 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
315 x-move Matrix3f/IDENTITY
316 )
317 (.setCollisionBetweenLinkedBodys false)
318 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
319 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
320 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
321 (world (nodify
322 [obj-a obj-b])
323 (merge standard-debug-controls
324 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
325 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
326 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
327 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
328 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
329 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
331 (fn [world]
332 (enable-debug world)
333 (set-gravity world Vector3f/ZERO)
334 )
336 (fn [world _]
338 (if @move-up?
339 (.applyForce control-a
340 (Vector3f. force 0 0)
341 (Vector3f. 0 0 0)))
342 (if @move-down?
343 (.applyForce control-a
344 (Vector3f. (- force) 0 0)
345 (Vector3f. 0 0 0)))
346 (if @move-left?
347 (.applyForce control-a
348 (Vector3f. 0 force 0)
349 (Vector3f. 0 0 0)))
350 (if @move-right?
351 (.applyForce control-a
352 (Vector3f. 0 (- force) 0)
353 (Vector3f. 0 0 0)))
355 (if @roll-left?
356 (.applyForce control-a
357 (Vector3f. 0 0 force)
358 (Vector3f. 0 0 0)))
359 (if @roll-right?
360 (.applyForce control-a
361 (Vector3f. 0 0 (- force))
362 (Vector3f. 0 0 0)))
364 (if (zero? (rem (swap! timer inc) 100))
365 (.attachChild
366 (.getRootNode world)
367 (sphere 0.05 :color ColorRGBA/Yellow
368 :physical? false :position
369 (.getWorldTranslation obj-a)))))
370 )
371 ))
373 (defn test-joint [joint]
374 (let [[origin top bottom floor] (world-setup joint)
375 control (.getControl top RigidBodyControl)
376 move-up? (atom false)
377 move-down? (atom false)
378 move-left? (atom false)
379 move-right? (atom false)
380 roll-left? (atom false)
381 roll-right? (atom false)
382 timer (atom 0)]
384 (world
385 (nodify [top bottom floor origin])
386 (merge standard-debug-controls
387 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
388 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
389 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
390 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
391 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
392 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
394 (fn [world]
395 (light-up-everything world)
396 (enable-debug world)
397 (set-gravity world (Vector3f. 0 0 0))
398 )
400 (fn [world _]
401 (if (zero? (rem (swap! timer inc) 100))
402 (do
403 ;; (println-repl @timer)
404 (.attachChild (.getRootNode world)
405 (sphere 0.05 :color ColorRGBA/Yellow
406 :position (.getWorldTranslation top)
407 :physical? false))
408 (.attachChild (.getRootNode world)
409 (sphere 0.05 :color ColorRGBA/LightGray
410 :position (.getWorldTranslation bottom)
411 :physical? false))))
413 (if @move-up?
414 (.applyTorque control
415 (.mult (.getPhysicsRotation control)
416 (Vector3f. 0 0 10))))
417 (if @move-down?
418 (.applyTorque control
419 (.mult (.getPhysicsRotation control)
420 (Vector3f. 0 0 -10))))
421 (if @move-left?
422 (.applyTorque control
423 (.mult (.getPhysicsRotation control)
424 (Vector3f. 0 10 0))))
425 (if @move-right?
426 (.applyTorque control
427 (.mult (.getPhysicsRotation control)
428 (Vector3f. 0 -10 0))))
429 (if @roll-left?
430 (.applyTorque control
431 (.mult (.getPhysicsRotation control)
432 (Vector3f. -1 0 0))))
433 (if @roll-right?
434 (.applyTorque control
435 (.mult (.getPhysicsRotation control)
436 (Vector3f. 1 0 0))))))))
437 #+end_src
440 * COMMENT generate source
441 #+begin_src clojure :tangle ../src/cortex/silly.clj
442 <<body-1>>
443 #+end_src