annotate org/test-creature.org @ 190:2902aca33c6e

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