Mercurial > cortex
view 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 |
line wrap: on
line source
1 #+title: First attempt at a creature!2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description:5 #+keywords: simulation, jMonkeyEngine3, clojure6 #+SETUPFILE: ../../aurellem/org/setup.org7 #+INCLUDE: ../../aurellem/org/level-0.org12 * Intro13 So far, I've made the following senses --14 - Vision15 - Hearing16 - Touch17 - Proprioception19 And one effector:20 - Movement22 However, the code so far has only enabled these senses, but has not23 actually implemented them. For example, there is still a lot of work24 to be done for vision. I need to be able to create an /eyeball/ in25 simulation that can be moved around and see the world from different26 angles. I also need to determine weather to use log-polar or cartesian27 for the visual input, and I need to determine how/wether to28 disceritise the visual input.30 I also want to be able to visualize both the sensors and the31 effectors in pretty pictures. This semi-retarted creature will be my32 first attempt at bringing everything together.34 * The creature's body36 Still going to do an eve-like body in blender, but due to problems37 importing the joints, etc into jMonkeyEngine3, I'm going to do all38 the connecting here in clojure code, using the names of the individual39 components and trial and error. Later, I'll maybe make some sort of40 creature-building modifications to blender that support whatever41 discritized senses I'm going to make.43 #+name: body-144 #+begin_src clojure45 (ns cortex.silly46 "let's play!"47 {:author "Robert McIntyre"})49 ;; TODO remove this!50 (require 'cortex.import)51 (cortex.import/mega-import-jme3)52 (use '(cortex world util body hearing touch vision sense53 proprioception movement))55 (rlm.rlm-commands/help)56 (import java.awt.image.BufferedImage)57 (import javax.swing.JPanel)58 (import javax.swing.SwingUtilities)59 (import java.awt.Dimension)60 (import javax.swing.JFrame)61 (import java.awt.Dimension)62 (import com.aurellem.capture.RatchetTimer)64 (use 'clojure.contrib.def)66 (defn load-blender-model67 "Load a .blend file using an asset folder relative path."68 [^String model]69 (.loadModel70 (doto (asset-manager)71 (.registerLoader BlenderModelLoader (into-array String ["blend"])))72 model))74 (def hand "Models/creature1/one.blend")76 (def worm "Models/creature1/try-again.blend")78 (defn worm-model [] (load-blender-model worm))80 (defn x-ray [#^ColorRGBA color]81 (doto (Material. (asset-manager)82 "Common/MatDefs/Misc/Unshaded.j3md")83 (.setColor "Color" color)84 (-> (.getAdditionalRenderState)85 (.setDepthTest false))))87 (defn colorful []88 (.getChild (worm-model) "worm-21"))90 (import jme3tools.converters.ImageToAwt)92 (import ij.ImagePlus)96 (defn test-eye []97 (.getChild98 (.getChild (worm-model) "eyes")99 "eye"))103 ;; lower level --- nodes104 ;; closest-node "parse/compile-x" -> makes organ, which is spatial, fn pair106 ;; higher level -- organs107 ;;109 ;; higher level --- sense/effector110 ;; these are the functions that provide world i/o, chinese-room style112 (defn debug-hearing-window113 "view audio data"114 [height]115 (let [vi (view-image)]116 (fn [[coords sensor-data]]117 (let [image (BufferedImage. (count coords) height118 BufferedImage/TYPE_INT_RGB)]119 (dorun120 (for [x (range (count coords))]121 (dorun122 (for [y (range height)]123 (let [raw-sensor (sensor-data x)]124 (.setRGB image x y (gray raw-sensor)))))))126 (vi image)))))128 (defn test-creature [thing]129 (let [x-axis130 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)131 y-axis132 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)133 z-axis134 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)136 creature (doto (load-blender-model thing) (body!))138 touch (touch! creature)139 touch-display (view-touch)141 vision (vision! creature)142 vision-display (view-vision)144 hearing (hearing! creature)145 hearing-display (view-hearing)147 prop (proprioception! creature)148 prop-display (view-proprioception)150 muscle-fns (movement! creature)153 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)154 bell (AudioNode. (asset-manager)155 "Sounds/pure.wav" false)157 fix-display (runonce158 (fn [world] (add-camera! world (.getCamera world) no-op)))159 ]162 (apply163 world164 (with-movement165 (.getChild creature "worm-21")166 ["key-r" "key-t"167 "key-f" "key-g"168 "key-v" "key-b"]169 [10 10 10 10 1 1]170 [(nodify [creature171 (box 10 2 10 :position (Vector3f. 0 -9 0)172 :color ColorRGBA/Gray :mass 0)173 x-axis y-axis z-axis174 me175 ])176 (merge standard-debug-controls177 {"key-return"178 (fn [_ value]179 (if value180 (do181 (println-repl "play-sound")182 (.play bell))))183 "key-h"184 (fn [_ value]185 (if value186 (do187 (println-repl "muscle activating!")188 ((first muscle-fns) 199))))190 })191 (fn [world]192 (light-up-everything world)193 (enable-debug world)194 ;;(dorun (map #(% world) init-vision-fns))195 ;;(dorun (map #(% world) init-hearing-fns))197 (add-camera! world198 (add-eye! creature (test-eye))199 (comp (view-image) BufferedImage!))202 ;;(set-gravity world (Vector3f. 0 0 0))203 ;;(com.aurellem.capture.Capture/captureVideo204 ;; world (file-str "/home/r/proj/ai-videos/hand"))205 ;;(.setTimer world (RatchetTimer. 60))206 (speed-up world)207 (set-gravity world (Vector3f. 0 0 0))208 )209 (fn [world tpf]210 ;;(dorun211 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))213 (prop-display (prop))215 (touch-display (map #(% (.getRootNode world)) touch))217 (vision-display (map #(% world) vision))219 (hearing-display (map #(% world) hearing))221 ;;(println-repl (vision-data))222 (.setLocalTranslation me (.getLocation (.getCamera world)))223 (fix-display world)225 )]226 ;;(let [timer (atom 0)]227 ;; (fn [_ _]228 ;; (swap! timer inc)229 ;; (if (= (rem @timer 60) 0)230 ;; (println-repl (float (/ @timer 60))))))231 ))))235 ;; the camera will stay in its initial position/rotation with relation236 ;; to the spatial.239 ;;dylan (defn follow-sense, adjoin-sense, attach-stimuli,240 ;;anchor-qualia, augment-organ, with-organ242 (defn follow-test243 "show a camera that stays in the same relative position to a blue cube."244 []245 (let [camera-pos (Vector3f. 0 30 0)246 rock (box 1 1 1 :color ColorRGBA/Blue247 :position (Vector3f. 0 10 0)248 :mass 30249 )250 rot (.getWorldRotation rock)252 table (box 3 1 10 :color ColorRGBA/Gray :mass 0253 :position (Vector3f. 0 -3 0))]255 (world256 (nodify [rock table])257 standard-debug-controls258 (fn [world]259 (let260 [cam (doto (.clone (.getCamera world))261 (.setLocation camera-pos)262 (.lookAt Vector3f/ZERO263 Vector3f/UNIT_X))]264 (bind-sense rock cam)266 (.setTimer world (RatchetTimer. 60))267 (add-camera! world cam (comp (view-image) BufferedImage!))268 (add-camera! world (.getCamera world) no-op))269 )270 (fn [_ _] (println-repl rot)))))274 #+end_src276 #+results: body-1277 : #'cortex.silly/follow-test280 * COMMENT purgatory281 #+begin_src clojure283 (defn bullet-trans* []284 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red285 :position (Vector3f. 5 0 0)286 :mass 90)287 obj-b (sphere 0.5 :color ColorRGBA/Blue288 :position (Vector3f. -5 0 0)289 :mass 0)290 control-a (.getControl obj-a RigidBodyControl)291 control-b (.getControl obj-b RigidBodyControl)292 move-up? (atom nil)293 move-down? (atom nil)294 move-left? (atom nil)295 move-right? (atom nil)296 roll-left? (atom nil)297 roll-right? (atom nil)298 force 100299 swivel300 (.toRotationMatrix301 (doto (Quaternion.)302 (.fromAngleAxis (/ Math/PI 2)303 Vector3f/UNIT_X)))304 x-move305 (doto (Matrix3f.)306 (.fromStartEndVectors Vector3f/UNIT_X307 (.normalize (Vector3f. 1 1 0))))309 timer (atom 0)]310 (doto311 (ConeJoint.312 control-a control-b313 (Vector3f. -8 0 0)314 (Vector3f. 2 0 0)315 ;;swivel swivel316 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY317 x-move Matrix3f/IDENTITY318 )319 (.setCollisionBetweenLinkedBodys false)320 (.setLimit (* 1 (/ Math/PI 4)) ;; twist321 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane322 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane323 (world (nodify324 [obj-a obj-b])325 (merge standard-debug-controls326 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))327 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))328 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))329 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))330 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))331 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})333 (fn [world]334 (enable-debug world)335 (set-gravity world Vector3f/ZERO)336 )338 (fn [world _]340 (if @move-up?341 (.applyForce control-a342 (Vector3f. force 0 0)343 (Vector3f. 0 0 0)))344 (if @move-down?345 (.applyForce control-a346 (Vector3f. (- force) 0 0)347 (Vector3f. 0 0 0)))348 (if @move-left?349 (.applyForce control-a350 (Vector3f. 0 force 0)351 (Vector3f. 0 0 0)))352 (if @move-right?353 (.applyForce control-a354 (Vector3f. 0 (- force) 0)355 (Vector3f. 0 0 0)))357 (if @roll-left?358 (.applyForce control-a359 (Vector3f. 0 0 force)360 (Vector3f. 0 0 0)))361 (if @roll-right?362 (.applyForce control-a363 (Vector3f. 0 0 (- force))364 (Vector3f. 0 0 0)))366 (if (zero? (rem (swap! timer inc) 100))367 (.attachChild368 (.getRootNode world)369 (sphere 0.05 :color ColorRGBA/Yellow370 :physical? false :position371 (.getWorldTranslation obj-a)))))372 )373 ))375 (defn test-joint [joint]376 (let [[origin top bottom floor] (world-setup joint)377 control (.getControl top RigidBodyControl)378 move-up? (atom false)379 move-down? (atom false)380 move-left? (atom false)381 move-right? (atom false)382 roll-left? (atom false)383 roll-right? (atom false)384 timer (atom 0)]386 (world387 (nodify [top bottom floor origin])388 (merge standard-debug-controls389 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))390 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))391 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))392 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))393 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))394 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})396 (fn [world]397 (light-up-everything world)398 (enable-debug world)399 (set-gravity world (Vector3f. 0 0 0))400 )402 (fn [world _]403 (if (zero? (rem (swap! timer inc) 100))404 (do405 ;; (println-repl @timer)406 (.attachChild (.getRootNode world)407 (sphere 0.05 :color ColorRGBA/Yellow408 :position (.getWorldTranslation top)409 :physical? false))410 (.attachChild (.getRootNode world)411 (sphere 0.05 :color ColorRGBA/LightGray412 :position (.getWorldTranslation bottom)413 :physical? false))))415 (if @move-up?416 (.applyTorque control417 (.mult (.getPhysicsRotation control)418 (Vector3f. 0 0 10))))419 (if @move-down?420 (.applyTorque control421 (.mult (.getPhysicsRotation control)422 (Vector3f. 0 0 -10))))423 (if @move-left?424 (.applyTorque control425 (.mult (.getPhysicsRotation control)426 (Vector3f. 0 10 0))))427 (if @move-right?428 (.applyTorque control429 (.mult (.getPhysicsRotation control)430 (Vector3f. 0 -10 0))))431 (if @roll-left?432 (.applyTorque control433 (.mult (.getPhysicsRotation control)434 (Vector3f. -1 0 0))))435 (if @roll-right?436 (.applyTorque control437 (.mult (.getPhysicsRotation control)438 (Vector3f. 1 0 0))))))))439 #+end_src442 * COMMENT generate source443 #+begin_src clojure :tangle ../src/cortex/silly.clj444 <<body-1>>445 #+end_src