annotate org/test-creature.org @ 176:026f69582022

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