view org/test-creature.org @ 173:1943b3f581c2

renamed proprioception functions
author Robert McIntyre <rlm@mit.edu>
date Sat, 04 Feb 2012 06:26:40 -0700
parents 1a00b4918529
children 0b9ae09eaec3
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 (declare blender-creature)
76 (defn blender-creature
77 "Return a creature with all joints in place."
78 [blender-path]
79 (let [model (load-blender-model blender-path)
80 joints (creature-joints model)]
81 (assemble-creature model joints)))
83 (def hand "Models/creature1/one.blend")
85 (def worm "Models/creature1/try-again.blend")
87 (defn worm-model [] (load-blender-model worm))
89 (defn x-ray [#^ColorRGBA color]
90 (doto (Material. (asset-manager)
91 "Common/MatDefs/Misc/Unshaded.j3md")
92 (.setColor "Color" color)
93 (-> (.getAdditionalRenderState)
94 (.setDepthTest false))))
96 (defn colorful []
97 (.getChild (worm-model) "worm-21"))
99 (import jme3tools.converters.ImageToAwt)
101 (import ij.ImagePlus)
105 (defn test-eye []
106 (.getChild
107 (.getChild (worm-model) "eyes")
108 "eye"))
112 ;; lower level --- nodes
113 ;; closest-node "parse/compile-x" -> makes organ, which is spatial, fn pair
115 ;; higher level -- organs
116 ;;
118 ;; higher level --- sense/effector
119 ;; these are the functions that provide world i/o, chinese-room style
125 (defn gray-scale [num]
126 (+ num
127 (bit-shift-left num 8)
128 (bit-shift-left num 16)))
130 (defn debug-touch-window
131 "creates function that offers a debug view of sensor data"
132 []
133 (let [vi (view-image)]
134 (fn
135 [[coords sensor-data]]
136 (let [image (points->image coords)]
137 (dorun
138 (for [i (range (count coords))]
139 (.setRGB image ((coords i) 0) ((coords i) 1)
140 (gray-scale (sensor-data i)))))
143 (vi image)))))
145 (defn debug-vision-window
146 "creates function that offers a debug view of sensor data"
147 []
148 (let [vi (view-image)]
149 (fn
150 [[coords sensor-data]]
151 (let [image (points->image coords)]
152 (dorun
153 (for [i (range (count coords))]
154 (.setRGB image ((coords i) 0) ((coords i) 1)
155 (sensor-data i))))
156 (vi image)))))
158 (defn debug-hearing-window
159 "view audio data"
160 [height]
161 (let [vi (view-image)]
162 (fn [[coords sensor-data]]
163 (let [image (BufferedImage. (count coords) height
164 BufferedImage/TYPE_INT_RGB)]
165 (dorun
166 (for [x (range (count coords))]
167 (dorun
168 (for [y (range height)]
169 (let [raw-sensor (sensor-data x)]
170 (.setRGB image x y (gray-scale raw-sensor)))))))
172 (vi image)))))
174 (defn test-creature [thing]
175 (let [x-axis
176 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
177 y-axis
178 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
179 z-axis
180 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
181 creature (blender-creature thing)
182 touch-nerves (touch creature)
183 touch-debug-windows (map (fn [_] (debug-touch-window)) touch-nerves)
184 vision-data (vision! creature)
185 vision-debug (map (fn [_] (debug-vision-window)) vision-data)
186 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
187 hearing-senses (hearing! creature)
188 hearing-windows (map (fn [_] (debug-hearing-window 50))
189 hearing-senses)
190 bell (AudioNode. (asset-manager)
191 "Sounds/pure.wav" false)
192 prop (proprioception! creature)
193 prop-debug (proprioception-debug-window)
195 muscle-fns (enable-muscles creature)
196 ;; dream
197 fix-display (runonce
198 (fn [world] (add-camera! world (.getCamera world) no-op)))
199 ]
202 (apply
203 world
204 (with-movement
205 (.getChild creature "worm-21")
206 ["key-r" "key-t"
207 "key-f" "key-g"
208 "key-v" "key-b"]
209 [10 10 10 10 1 1]
210 [(nodify [creature
211 (box 10 2 10 :position (Vector3f. 0 -9 0)
212 :color ColorRGBA/Gray :mass 0)
213 x-axis y-axis z-axis
214 me
215 ])
216 (merge standard-debug-controls
217 {"key-return"
218 (fn [_ value]
219 (if value
220 (do
221 (println-repl "play-sound")
222 (.play bell))))
223 "key-h"
224 (fn [_ value]
225 (if value
226 (do
227 (println-repl "muscle activating!")
228 ((first muscle-fns) 199))))
230 })
231 (fn [world]
232 (light-up-everything world)
233 (enable-debug world)
234 ;;(dorun (map #(% world) init-vision-fns))
235 ;;(dorun (map #(% world) init-hearing-fns))
237 (add-camera! world
238 (add-eye! creature (test-eye))
239 (comp (view-image) BufferedImage!))
242 ;;(set-gravity world (Vector3f. 0 0 0))
243 ;;(com.aurellem.capture.Capture/captureVideo
244 ;; world (file-str "/home/r/proj/ai-videos/hand"))
245 ;;(.setTimer world (RatchetTimer. 60))
246 (speed-up world)
247 (set-gravity world (Vector3f. 0 0 0))
248 )
249 (fn [world tpf]
250 ;;(dorun
251 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))
253 (prop-debug (prop))
255 (dorun
256 (map #(%1 (%2 (.getRootNode world)))
257 touch-debug-windows touch-nerves))
259 (dorun
260 (map #(%1 (%2 world))
261 vision-debug vision-data))
262 (dorun
263 (map #(%1 (%2 world)) hearing-windows hearing-senses))
266 ;;(println-repl (vision-data))
267 (.setLocalTranslation me (.getLocation (.getCamera world)))
268 (fix-display world)
270 )]
271 ;;(let [timer (atom 0)]
272 ;; (fn [_ _]
273 ;; (swap! timer inc)
274 ;; (if (= (rem @timer 60) 0)
275 ;; (println-repl (float (/ @timer 60))))))
276 ))))
280 ;; the camera will stay in its initial position/rotation with relation
281 ;; to the spatial.
284 ;;dylan (defn follow-sense, adjoin-sense, attach-stimuli,
285 ;;anchor-qualia, augment-organ, with-organ
287 (defn follow-test
288 "show a camera that stays in the same relative position to a blue cube."
289 []
290 (let [camera-pos (Vector3f. 0 30 0)
291 rock (box 1 1 1 :color ColorRGBA/Blue
292 :position (Vector3f. 0 10 0)
293 :mass 30
294 )
295 rot (.getWorldRotation rock)
297 table (box 3 1 10 :color ColorRGBA/Gray :mass 0
298 :position (Vector3f. 0 -3 0))]
300 (world
301 (nodify [rock table])
302 standard-debug-controls
303 (fn [world]
304 (let
305 [cam (doto (.clone (.getCamera world))
306 (.setLocation camera-pos)
307 (.lookAt Vector3f/ZERO
308 Vector3f/UNIT_X))]
309 (bind-sense rock cam)
311 (.setTimer world (RatchetTimer. 60))
312 (add-camera! world cam (comp (view-image) BufferedImage!))
313 (add-camera! world (.getCamera world) no-op))
314 )
315 (fn [_ _] (println-repl rot)))))
319 #+end_src
321 #+results: body-1
322 : #'cortex.silly/follow-test
325 * COMMENT purgatory
326 #+begin_src clojure
328 (defn bullet-trans* []
329 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
330 :position (Vector3f. 5 0 0)
331 :mass 90)
332 obj-b (sphere 0.5 :color ColorRGBA/Blue
333 :position (Vector3f. -5 0 0)
334 :mass 0)
335 control-a (.getControl obj-a RigidBodyControl)
336 control-b (.getControl obj-b RigidBodyControl)
337 move-up? (atom nil)
338 move-down? (atom nil)
339 move-left? (atom nil)
340 move-right? (atom nil)
341 roll-left? (atom nil)
342 roll-right? (atom nil)
343 force 100
344 swivel
345 (.toRotationMatrix
346 (doto (Quaternion.)
347 (.fromAngleAxis (/ Math/PI 2)
348 Vector3f/UNIT_X)))
349 x-move
350 (doto (Matrix3f.)
351 (.fromStartEndVectors Vector3f/UNIT_X
352 (.normalize (Vector3f. 1 1 0))))
354 timer (atom 0)]
355 (doto
356 (ConeJoint.
357 control-a control-b
358 (Vector3f. -8 0 0)
359 (Vector3f. 2 0 0)
360 ;;swivel swivel
361 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
362 x-move Matrix3f/IDENTITY
363 )
364 (.setCollisionBetweenLinkedBodys false)
365 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
366 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
367 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
368 (world (nodify
369 [obj-a obj-b])
370 (merge standard-debug-controls
371 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
372 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
373 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
374 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
375 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
376 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
378 (fn [world]
379 (enable-debug world)
380 (set-gravity world Vector3f/ZERO)
381 )
383 (fn [world _]
385 (if @move-up?
386 (.applyForce control-a
387 (Vector3f. force 0 0)
388 (Vector3f. 0 0 0)))
389 (if @move-down?
390 (.applyForce control-a
391 (Vector3f. (- force) 0 0)
392 (Vector3f. 0 0 0)))
393 (if @move-left?
394 (.applyForce control-a
395 (Vector3f. 0 force 0)
396 (Vector3f. 0 0 0)))
397 (if @move-right?
398 (.applyForce control-a
399 (Vector3f. 0 (- force) 0)
400 (Vector3f. 0 0 0)))
402 (if @roll-left?
403 (.applyForce control-a
404 (Vector3f. 0 0 force)
405 (Vector3f. 0 0 0)))
406 (if @roll-right?
407 (.applyForce control-a
408 (Vector3f. 0 0 (- force))
409 (Vector3f. 0 0 0)))
411 (if (zero? (rem (swap! timer inc) 100))
412 (.attachChild
413 (.getRootNode world)
414 (sphere 0.05 :color ColorRGBA/Yellow
415 :physical? false :position
416 (.getWorldTranslation obj-a)))))
417 )
418 ))
420 (defn test-joint [joint]
421 (let [[origin top bottom floor] (world-setup joint)
422 control (.getControl top RigidBodyControl)
423 move-up? (atom false)
424 move-down? (atom false)
425 move-left? (atom false)
426 move-right? (atom false)
427 roll-left? (atom false)
428 roll-right? (atom false)
429 timer (atom 0)]
431 (world
432 (nodify [top bottom floor origin])
433 (merge standard-debug-controls
434 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
435 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
436 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
437 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
438 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
439 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
441 (fn [world]
442 (light-up-everything world)
443 (enable-debug world)
444 (set-gravity world (Vector3f. 0 0 0))
445 )
447 (fn [world _]
448 (if (zero? (rem (swap! timer inc) 100))
449 (do
450 ;; (println-repl @timer)
451 (.attachChild (.getRootNode world)
452 (sphere 0.05 :color ColorRGBA/Yellow
453 :position (.getWorldTranslation top)
454 :physical? false))
455 (.attachChild (.getRootNode world)
456 (sphere 0.05 :color ColorRGBA/LightGray
457 :position (.getWorldTranslation bottom)
458 :physical? false))))
460 (if @move-up?
461 (.applyTorque control
462 (.mult (.getPhysicsRotation control)
463 (Vector3f. 0 0 10))))
464 (if @move-down?
465 (.applyTorque control
466 (.mult (.getPhysicsRotation control)
467 (Vector3f. 0 0 -10))))
468 (if @move-left?
469 (.applyTorque control
470 (.mult (.getPhysicsRotation control)
471 (Vector3f. 0 10 0))))
472 (if @move-right?
473 (.applyTorque control
474 (.mult (.getPhysicsRotation control)
475 (Vector3f. 0 -10 0))))
476 (if @roll-left?
477 (.applyTorque control
478 (.mult (.getPhysicsRotation control)
479 (Vector3f. -1 0 0))))
480 (if @roll-right?
481 (.applyTorque control
482 (.mult (.getPhysicsRotation control)
483 (Vector3f. 1 0 0))))))))
484 #+end_src
487 * COMMENT generate source
488 #+begin_src clojure :tangle ../src/cortex/silly.clj
489 <<body-1>>
490 #+end_src