annotate org/test-creature.org @ 191:66fbab414d45

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