annotate org/test-creature.org @ 171:15bde60217aa

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