annotate org/integration.org @ 201:1c915cc1118b

minor edits for sense.org
author Robert McIntyre <rlm@mit.edu>
date Tue, 07 Feb 2012 07:13:45 -0700
parents 305439cec54d
children 7351c9c0c471
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@192 43 #+name: integration
rlm@73 44 #+begin_src clojure
rlm@192 45 (ns cortex.integration
rlm@73 46 "let's play!"
rlm@192 47 {:author "Robert McIntyre"}
rlm@192 48 (:use (cortex world util body
rlm@192 49 hearing touch vision sense proprioception movement))
rlm@192 50 (:import (com.jme3.math ColorRGBA Vector3f))
rlm@192 51 (:import com.jme3.audio.AudioNode)
rlm@192 52 (:import com.aurellem.capture.RatchetTimer))
rlm@73 53
rlm@78 54 (def hand "Models/creature1/one.blend")
rlm@74 55
rlm@78 56 (def worm "Models/creature1/try-again.blend")
rlm@78 57
rlm@106 58 (defn test-creature [thing]
rlm@106 59 (let [x-axis
rlm@106 60 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
rlm@106 61 y-axis
rlm@106 62 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
rlm@106 63 z-axis
rlm@106 64 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
rlm@189 65
rlm@192 66 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
rlm@192 67 bell (AudioNode. (asset-manager)
rlm@192 68 "Sounds/pure.wav" false)
rlm@192 69
rlm@192 70 fix-display
rlm@192 71 (runonce (fn [world]
rlm@192 72 (add-camera! world (.getCamera world) no-op)))
rlm@189 73 creature (doto (load-blender-model thing) (body!))
rlm@189 74
rlm@192 75 ;;;;;;;;;;;; Sensors/Effectors ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@185 76 touch (touch! creature)
rlm@188 77 touch-display (view-touch)
rlm@189 78
rlm@188 79 vision (vision! creature)
rlm@188 80 vision-display (view-vision)
rlm@189 81
rlm@189 82 hearing (hearing! creature)
rlm@189 83 hearing-display (view-hearing)
rlm@189 84
rlm@173 85 prop (proprioception! creature)
rlm@190 86 prop-display (view-proprioception)
rlm@148 87
rlm@191 88 muscle-exertion (atom 0)
rlm@191 89 muscles (movement! creature)
rlm@192 90 muscle-display (view-movement)]
rlm@189 91
rlm@143 92 (apply
rlm@143 93 world
rlm@143 94 (with-movement
rlm@143 95 (.getChild creature "worm-21")
rlm@143 96 ["key-r" "key-t"
rlm@143 97 "key-f" "key-g"
rlm@143 98 "key-v" "key-b"]
rlm@143 99 [10 10 10 10 1 1]
rlm@143 100 [(nodify [creature
rlm@143 101 (box 10 2 10 :position (Vector3f. 0 -9 0)
rlm@143 102 :color ColorRGBA/Gray :mass 0)
rlm@143 103 x-axis y-axis z-axis
rlm@192 104 me])
rlm@143 105 (merge standard-debug-controls
rlm@143 106 {"key-return"
rlm@143 107 (fn [_ value]
rlm@143 108 (if value
rlm@143 109 (do
rlm@143 110 (println-repl "play-sound")
rlm@148 111 (.play bell))))
rlm@148 112 "key-h"
rlm@148 113 (fn [_ value]
rlm@148 114 (if value
rlm@191 115 (swap! muscle-exertion (partial + 20))))
rlm@191 116 "key-n"
rlm@191 117 (fn [_ value]
rlm@191 118 (if value
rlm@192 119 (swap! muscle-exertion (fn [v] (- v 20)))))})
rlm@143 120 (fn [world]
rlm@143 121 (light-up-everything world)
rlm@143 122 (enable-debug world)
rlm@169 123 (add-camera! world
rlm@192 124 (add-eye! creature
rlm@192 125 (.getChild
rlm@192 126 (.getChild creature "eyes") "eye"))
rlm@143 127 (comp (view-image) BufferedImage!))
rlm@192 128 (.setTimer world (RatchetTimer. 60))
rlm@143 129 (speed-up world)
rlm@148 130 (set-gravity world (Vector3f. 0 0 0))
rlm@192 131 (comment
rlm@192 132 (com.aurellem.capture.Capture/captureVideo
rlm@192 133 world (file-str "/home/r/proj/ai-videos/hand"))))
rlm@143 134 (fn [world tpf]
rlm@190 135 (prop-display (prop))
rlm@188 136 (touch-display (map #(% (.getRootNode world)) touch))
rlm@188 137 (vision-display (map #(% world) vision))
rlm@189 138 (hearing-display (map #(% world) hearing))
rlm@191 139 (muscle-display (map #(% @muscle-exertion) muscles))
rlm@143 140 (.setLocalTranslation me (.getLocation (.getCamera world)))
rlm@192 141 (fix-display world))]))))
rlm@87 142 #+end_src
rlm@83 143
rlm@87 144 #+results: body-1
rlm@133 145 : #'cortex.silly/follow-test
rlm@78 146
rlm@78 147
rlm@78 148 * COMMENT purgatory
rlm@78 149 #+begin_src clojure
rlm@74 150
rlm@77 151 (defn bullet-trans* []
rlm@77 152 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
rlm@77 153 :position (Vector3f. 5 0 0)
rlm@77 154 :mass 90)
rlm@77 155 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 156 :position (Vector3f. -5 0 0)
rlm@77 157 :mass 0)
rlm@77 158 control-a (.getControl obj-a RigidBodyControl)
rlm@77 159 control-b (.getControl obj-b RigidBodyControl)
rlm@77 160 move-up? (atom nil)
rlm@77 161 move-down? (atom nil)
rlm@77 162 move-left? (atom nil)
rlm@77 163 move-right? (atom nil)
rlm@77 164 roll-left? (atom nil)
rlm@77 165 roll-right? (atom nil)
rlm@77 166 force 100
rlm@77 167 swivel
rlm@77 168 (.toRotationMatrix
rlm@77 169 (doto (Quaternion.)
rlm@77 170 (.fromAngleAxis (/ Math/PI 2)
rlm@77 171 Vector3f/UNIT_X)))
rlm@77 172 x-move
rlm@77 173 (doto (Matrix3f.)
rlm@77 174 (.fromStartEndVectors Vector3f/UNIT_X
rlm@77 175 (.normalize (Vector3f. 1 1 0))))
rlm@77 176
rlm@77 177 timer (atom 0)]
rlm@77 178 (doto
rlm@77 179 (ConeJoint.
rlm@77 180 control-a control-b
rlm@77 181 (Vector3f. -8 0 0)
rlm@77 182 (Vector3f. 2 0 0)
rlm@77 183 ;;swivel swivel
rlm@77 184 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
rlm@77 185 x-move Matrix3f/IDENTITY
rlm@77 186 )
rlm@77 187 (.setCollisionBetweenLinkedBodys false)
rlm@77 188 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
rlm@77 189 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
rlm@77 190 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
rlm@77 191 (world (nodify
rlm@77 192 [obj-a obj-b])
rlm@77 193 (merge standard-debug-controls
rlm@77 194 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@77 195 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@77 196 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@77 197 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@77 198 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@77 199 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@77 200
rlm@77 201 (fn [world]
rlm@77 202 (enable-debug world)
rlm@77 203 (set-gravity world Vector3f/ZERO)
rlm@77 204 )
rlm@77 205
rlm@77 206 (fn [world _]
rlm@77 207
rlm@77 208 (if @move-up?
rlm@77 209 (.applyForce control-a
rlm@77 210 (Vector3f. force 0 0)
rlm@77 211 (Vector3f. 0 0 0)))
rlm@77 212 (if @move-down?
rlm@77 213 (.applyForce control-a
rlm@77 214 (Vector3f. (- force) 0 0)
rlm@77 215 (Vector3f. 0 0 0)))
rlm@77 216 (if @move-left?
rlm@77 217 (.applyForce control-a
rlm@77 218 (Vector3f. 0 force 0)
rlm@77 219 (Vector3f. 0 0 0)))
rlm@77 220 (if @move-right?
rlm@77 221 (.applyForce control-a
rlm@77 222 (Vector3f. 0 (- force) 0)
rlm@77 223 (Vector3f. 0 0 0)))
rlm@77 224
rlm@77 225 (if @roll-left?
rlm@77 226 (.applyForce control-a
rlm@77 227 (Vector3f. 0 0 force)
rlm@77 228 (Vector3f. 0 0 0)))
rlm@77 229 (if @roll-right?
rlm@77 230 (.applyForce control-a
rlm@77 231 (Vector3f. 0 0 (- force))
rlm@77 232 (Vector3f. 0 0 0)))
rlm@77 233
rlm@77 234 (if (zero? (rem (swap! timer inc) 100))
rlm@77 235 (.attachChild
rlm@77 236 (.getRootNode world)
rlm@77 237 (sphere 0.05 :color ColorRGBA/Yellow
rlm@77 238 :physical? false :position
rlm@77 239 (.getWorldTranslation obj-a)))))
rlm@77 240 )
rlm@77 241 ))
rlm@77 242
rlm@106 243 (defn test-joint [joint]
rlm@106 244 (let [[origin top bottom floor] (world-setup joint)
rlm@106 245 control (.getControl top RigidBodyControl)
rlm@106 246 move-up? (atom false)
rlm@106 247 move-down? (atom false)
rlm@106 248 move-left? (atom false)
rlm@106 249 move-right? (atom false)
rlm@106 250 roll-left? (atom false)
rlm@106 251 roll-right? (atom false)
rlm@106 252 timer (atom 0)]
rlm@106 253
rlm@106 254 (world
rlm@106 255 (nodify [top bottom floor origin])
rlm@106 256 (merge standard-debug-controls
rlm@106 257 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@106 258 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@106 259 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@106 260 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@106 261 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@106 262 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@106 263
rlm@106 264 (fn [world]
rlm@106 265 (light-up-everything world)
rlm@106 266 (enable-debug world)
rlm@106 267 (set-gravity world (Vector3f. 0 0 0))
rlm@106 268 )
rlm@106 269
rlm@106 270 (fn [world _]
rlm@106 271 (if (zero? (rem (swap! timer inc) 100))
rlm@106 272 (do
rlm@106 273 ;; (println-repl @timer)
rlm@106 274 (.attachChild (.getRootNode world)
rlm@106 275 (sphere 0.05 :color ColorRGBA/Yellow
rlm@106 276 :position (.getWorldTranslation top)
rlm@106 277 :physical? false))
rlm@106 278 (.attachChild (.getRootNode world)
rlm@106 279 (sphere 0.05 :color ColorRGBA/LightGray
rlm@106 280 :position (.getWorldTranslation bottom)
rlm@106 281 :physical? false))))
rlm@106 282
rlm@106 283 (if @move-up?
rlm@106 284 (.applyTorque control
rlm@106 285 (.mult (.getPhysicsRotation control)
rlm@106 286 (Vector3f. 0 0 10))))
rlm@106 287 (if @move-down?
rlm@106 288 (.applyTorque control
rlm@106 289 (.mult (.getPhysicsRotation control)
rlm@106 290 (Vector3f. 0 0 -10))))
rlm@106 291 (if @move-left?
rlm@106 292 (.applyTorque control
rlm@106 293 (.mult (.getPhysicsRotation control)
rlm@106 294 (Vector3f. 0 10 0))))
rlm@106 295 (if @move-right?
rlm@106 296 (.applyTorque control
rlm@106 297 (.mult (.getPhysicsRotation control)
rlm@106 298 (Vector3f. 0 -10 0))))
rlm@106 299 (if @roll-left?
rlm@106 300 (.applyTorque control
rlm@106 301 (.mult (.getPhysicsRotation control)
rlm@106 302 (Vector3f. -1 0 0))))
rlm@106 303 (if @roll-right?
rlm@106 304 (.applyTorque control
rlm@106 305 (.mult (.getPhysicsRotation control)
rlm@106 306 (Vector3f. 1 0 0))))))))
rlm@99 307 #+end_src
rlm@192 308
rlm@99 309
rlm@99 310 * COMMENT generate source
rlm@192 311 #+begin_src clojure :tangle ../src/cortex/integration.clj
rlm@192 312 <<integration>>
rlm@99 313 #+end_src
rlm@99 314
rlm@99 315
rlm@94 316
rlm@94 317