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