annotate org/test-creature.org @ 163:985c73659923

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