annotate org/integration.org @ 296:1eed471e2ebf

first version of a mini motor-control script language
author Robert McIntyre <rlm@mit.edu>
date Thu, 16 Feb 2012 11:04:22 -0700
parents 67a4e92d4628
children d1206b11ae2d
rev   line source
rlm@281 1 #+title:
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@281 9 * Intro
rlm@129 10
rlm@281 11 This is the ultimate test which features all of the senses that I've
rlm@281 12 made so far. The blender file for the creature serves as an example of
rlm@281 13 a fully equipped creature in terms of senses. You can find it [[../assets/Models/test-creature/hand.blend][here]].
rlm@73 14
rlm@192 15 #+name: integration
rlm@73 16 #+begin_src clojure
rlm@192 17 (ns cortex.integration
rlm@73 18 "let's play!"
rlm@192 19 {:author "Robert McIntyre"}
rlm@281 20 (:use (cortex world util body sense
rlm@281 21 hearing touch vision proprioception movement))
rlm@192 22 (:import (com.jme3.math ColorRGBA Vector3f))
rlm@281 23 (:import java.io.File)
rlm@192 24 (:import com.jme3.audio.AudioNode)
rlm@192 25 (:import com.aurellem.capture.RatchetTimer))
rlm@73 26
rlm@281 27 (dorun (cortex.import/mega-import-jme3))
rlm@281 28 (rlm.rlm-commands/help)
rlm@74 29
rlm@281 30 (def hand "Models/test-creature/hand.blend")
rlm@78 31
rlm@281 32 (def output-base (File. "/home/r/proj/cortex/render/hand"))
rlm@189 33
rlm@296 34 (defn motor-control-program
rlm@296 35 "Create a function which will execute the motor script"
rlm@296 36 [muscle-positions
rlm@296 37 script]
rlm@296 38 (let [current-frame (atom -1)
rlm@296 39 keyed-script (group-by first script)
rlm@296 40 current-forces (atom {}) ]
rlm@296 41 (fn [effectors]
rlm@296 42 (let [indexed-effectors (vec effectors)]
rlm@296 43 (dorun
rlm@296 44 (for [[_ part force] (keyed-script (swap! current-frame inc))]
rlm@296 45 (swap! current-forces (fn [m] (assoc m part force)))))
rlm@296 46 (doall (map (fn [effector power]
rlm@296 47 (effector (int power)))
rlm@296 48 effectors
rlm@296 49 (map #(@current-forces % 0) muscle-positions)))))))
rlm@296 50
rlm@296 51 (def muscle-positions
rlm@296 52 [:pointer-2-e
rlm@296 53 :pointer-2-f
rlm@296 54 :thumb-1
rlm@296 55 :thumb-1
rlm@296 56 :pointer-1-e
rlm@296 57 :pointer-1-f
rlm@296 58 :thumb-2-e
rlm@296 59 :thumb-2-f
rlm@296 60 :middle-1-e
rlm@296 61 :middle-1-f
rlm@296 62 :pointer-3-f
rlm@296 63 :pointer-3-e
rlm@296 64 :middle-2-e
rlm@296 65 :middle-2-f
rlm@296 66 :middle-3-f
rlm@296 67 :middle-3-e
rlm@296 68 :pinky-2-e
rlm@296 69 :pinky-2-f
rlm@296 70 :pinky-3-f
rlm@296 71 :pinky-3-e
rlm@296 72 :ring-3-e
rlm@296 73 :ring-3-f
rlm@296 74 :ring-2-f
rlm@296 75 :ring-2-e
rlm@296 76 :ring-1-e
rlm@296 77 :ring-1-f
rlm@296 78 :thumb-1-e
rlm@296 79 :thumb-1-f
rlm@296 80 :pinky-1-f
rlm@296 81 :pinky-1-e])
rlm@296 82
rlm@296 83 (def full 9001)
rlm@296 84 ;; the systax here is [keyframe body-part force]
rlm@296 85 (def wiggle-each-finger-tip
rlm@296 86 [[200 :pointer-3-f full]
rlm@296 87 [200 :pointer-3-f 40]
rlm@296 88
rlm@296 89 [400 :middle-3-f full]
rlm@296 90 [401 :middle-3-f 40]
rlm@296 91
rlm@296 92 [600 :ring-3-f full]
rlm@296 93 [601 :ring-3-f 40]
rlm@296 94
rlm@296 95 [800 :pinky-3-f full]
rlm@296 96 [801 :pinky-3-f 40]])
rlm@296 97
rlm@296 98
rlm@296 99
rlm@296 100
rlm@295 101 (def control-list
rlm@295 102 [
rlm@295 103 0 ;;pointer-21 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@296 104 0 ;;pointer-21 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@295 105 0 ;;thumb-11 #<Vector3f (-0.8802276, -0.39781287, -0.25873658)>
rlm@295 106 0 ;;thumb-11 #<Vector3f (0.8485723, 0.46149826, 0.2587364)>
rlm@295 107 0 ;;pointer-11 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@295 108 0 ;;pointer-11 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@295 109 0 ;;thumb-2.0011 #<Vector3f (-0.71705645, -0.44753736, -0.5343599)>
rlm@295 110 0 ;;thumb-2.0011 #<Vector3f (-0.10567085, 0.83862597, 0.53435963)>
rlm@295 111 0 ;;middle-11 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@295 112 0 ;;middle-11 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@296 113 1 ;;pointer-31 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@295 114 0 ;;pointer-31 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@295 115 0 ;;middle-21 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@295 116 0 ;;middle-21 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@295 117 0 ;;middle-31 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@295 118 0 ;;middle-31 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@295 119 0 ;;pinky-21 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@295 120 0 ;;pinky-21 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@296 121 0 ;;pinky-31 #<Vector3f (-1, 0.0, 0.0)>
rlm@296 122 0 ;;pinky-31 #<Vector3f (1.0, 0.0, 0.0)>
rlm@295 123 0 ;;ring-31 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@295 124 0 ;;ring-31 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@295 125 0 ;;ring-21 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@295 126 0 ;;ring-21 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@295 127 0 ;;ring-11 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@295 128 0 ;;ring-11 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@295 129 0 ;;thumb-11 #<Vector3f (-0.43154645, 0.7302033, 0.5296894)>
rlm@295 130 0 ;;thumb-11 #<Vector3f (-0.8032993, -0.2722854, -0.5296895)>
rlm@295 131 0 ;;pinky-11 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@295 132 0 ;;pinky-11 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@295 133 ])
rlm@295 134
rlm@281 135 (defn test-everything!
rlm@281 136 ([] (test-everything! false))
rlm@281 137 ([record?]
rlm@281 138 (let [me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
rlm@281 139
rlm@192 140 bell (AudioNode. (asset-manager)
rlm@192 141 "Sounds/pure.wav" false)
rlm@281 142 creature (doto (load-blender-model hand)
rlm@281 143 (body!))
rlm@281 144
rlm@192 145 ;;;;;;;;;;;; Sensors/Effectors ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@294 146 ;;touch (touch! creature)
rlm@294 147 ;;touch-display (view-touch)
rlm@189 148
rlm@294 149 ;;vision (vision! creature)
rlm@294 150 ;;vision-display (view-vision)
rlm@189 151
rlm@294 152 ;;hearing (hearing! creature)
rlm@294 153 ;;hearing-display (view-hearing)
rlm@189 154
rlm@173 155 prop (proprioception! creature)
rlm@190 156 prop-display (view-proprioception)
rlm@148 157
rlm@296 158 control-script (motor-control-program
rlm@296 159 muscle-positions wiggle-each-finger-tip)
rlm@191 160 muscles (movement! creature)
rlm@281 161 muscle-display (view-movement)
rlm@281 162 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@189 163
rlm@281 164 fix-display (gen-fix-display)]
rlm@285 165 (world
rlm@285 166 (nodify [creature
rlm@285 167 (box 10 2 10 :position (Vector3f. 0 -9 0)
rlm@285 168 :color ColorRGBA/Gray :mass 0)
rlm@285 169 me])
rlm@285 170 (merge standard-debug-controls
rlm@285 171 {"key-return"
rlm@285 172 (fn [_ value]
rlm@285 173 (if value
rlm@296 174 (.play bell)))})
rlm@296 175
rlm@285 176 (fn [world]
rlm@285 177 (.setTimer world (RatchetTimer. 60))
rlm@285 178 (light-up-everything world)
rlm@285 179 (enable-debug world)
rlm@285 180 (add-camera! world
rlm@285 181 (add-eye! creature
rlm@285 182 (.getChild
rlm@285 183 (.getChild creature "eyes") "eye"))
rlm@285 184 (comp (view-image) BufferedImage!))
rlm@285 185 (speed-up world))
rlm@285 186 (fn [world tpf]
rlm@294 187 ;;(prop-display (prop))
rlm@294 188 ;;(touch-display (map #(% (.getRootNode world)) touch))
rlm@294 189 ;;(vision-display (map #(% world) vision))
rlm@294 190 ;;(hearing-display (map #(% world) hearing))
rlm@295 191
rlm@296 192 ;;(muscle-display
rlm@296 193 (control-script muscles)
rlm@296 194 ;;)
rlm@295 195
rlm@285 196 (.setLocalTranslation me (.getLocation (.getCamera world)))
rlm@285 197 (fix-display world))))))
rlm@87 198 #+end_src
rlm@83 199
rlm@282 200 #+results: integration
rlm@282 201 : #'cortex.integration/test-everything!
rlm@282 202
rlm@78 203 * COMMENT purgatory
rlm@78 204 #+begin_src clojure
rlm@77 205 (defn bullet-trans* []
rlm@77 206 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
rlm@77 207 :position (Vector3f. 5 0 0)
rlm@77 208 :mass 90)
rlm@77 209 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 210 :position (Vector3f. -5 0 0)
rlm@77 211 :mass 0)
rlm@77 212 control-a (.getControl obj-a RigidBodyControl)
rlm@77 213 control-b (.getControl obj-b RigidBodyControl)
rlm@77 214 move-up? (atom nil)
rlm@77 215 move-down? (atom nil)
rlm@77 216 move-left? (atom nil)
rlm@77 217 move-right? (atom nil)
rlm@77 218 roll-left? (atom nil)
rlm@77 219 roll-right? (atom nil)
rlm@77 220 force 100
rlm@77 221 swivel
rlm@77 222 (.toRotationMatrix
rlm@77 223 (doto (Quaternion.)
rlm@77 224 (.fromAngleAxis (/ Math/PI 2)
rlm@77 225 Vector3f/UNIT_X)))
rlm@77 226 x-move
rlm@77 227 (doto (Matrix3f.)
rlm@77 228 (.fromStartEndVectors Vector3f/UNIT_X
rlm@77 229 (.normalize (Vector3f. 1 1 0))))
rlm@77 230
rlm@77 231 timer (atom 0)]
rlm@77 232 (doto
rlm@77 233 (ConeJoint.
rlm@77 234 control-a control-b
rlm@77 235 (Vector3f. -8 0 0)
rlm@77 236 (Vector3f. 2 0 0)
rlm@77 237 ;;swivel swivel
rlm@77 238 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
rlm@77 239 x-move Matrix3f/IDENTITY
rlm@77 240 )
rlm@77 241 (.setCollisionBetweenLinkedBodys false)
rlm@77 242 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
rlm@77 243 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
rlm@77 244 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
rlm@77 245 (world (nodify
rlm@77 246 [obj-a obj-b])
rlm@77 247 (merge standard-debug-controls
rlm@77 248 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@77 249 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@77 250 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@77 251 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@77 252 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@77 253 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@77 254
rlm@77 255 (fn [world]
rlm@77 256 (enable-debug world)
rlm@77 257 (set-gravity world Vector3f/ZERO)
rlm@77 258 )
rlm@77 259
rlm@77 260 (fn [world _]
rlm@77 261
rlm@77 262 (if @move-up?
rlm@77 263 (.applyForce control-a
rlm@77 264 (Vector3f. force 0 0)
rlm@77 265 (Vector3f. 0 0 0)))
rlm@77 266 (if @move-down?
rlm@77 267 (.applyForce control-a
rlm@77 268 (Vector3f. (- force) 0 0)
rlm@77 269 (Vector3f. 0 0 0)))
rlm@77 270 (if @move-left?
rlm@77 271 (.applyForce control-a
rlm@77 272 (Vector3f. 0 force 0)
rlm@77 273 (Vector3f. 0 0 0)))
rlm@77 274 (if @move-right?
rlm@77 275 (.applyForce control-a
rlm@77 276 (Vector3f. 0 (- force) 0)
rlm@77 277 (Vector3f. 0 0 0)))
rlm@77 278
rlm@77 279 (if @roll-left?
rlm@77 280 (.applyForce control-a
rlm@77 281 (Vector3f. 0 0 force)
rlm@77 282 (Vector3f. 0 0 0)))
rlm@77 283 (if @roll-right?
rlm@77 284 (.applyForce control-a
rlm@77 285 (Vector3f. 0 0 (- force))
rlm@77 286 (Vector3f. 0 0 0)))
rlm@77 287
rlm@77 288 (if (zero? (rem (swap! timer inc) 100))
rlm@77 289 (.attachChild
rlm@77 290 (.getRootNode world)
rlm@77 291 (sphere 0.05 :color ColorRGBA/Yellow
rlm@77 292 :physical? false :position
rlm@77 293 (.getWorldTranslation obj-a)))))
rlm@77 294 )
rlm@77 295 ))
rlm@77 296
rlm@106 297 (defn test-joint [joint]
rlm@106 298 (let [[origin top bottom floor] (world-setup joint)
rlm@106 299 control (.getControl top RigidBodyControl)
rlm@106 300 move-up? (atom false)
rlm@106 301 move-down? (atom false)
rlm@106 302 move-left? (atom false)
rlm@106 303 move-right? (atom false)
rlm@106 304 roll-left? (atom false)
rlm@106 305 roll-right? (atom false)
rlm@106 306 timer (atom 0)]
rlm@106 307
rlm@106 308 (world
rlm@106 309 (nodify [top bottom floor origin])
rlm@106 310 (merge standard-debug-controls
rlm@106 311 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@106 312 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@106 313 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@106 314 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@106 315 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@106 316 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@106 317
rlm@106 318 (fn [world]
rlm@106 319 (light-up-everything world)
rlm@106 320 (enable-debug world)
rlm@106 321 (set-gravity world (Vector3f. 0 0 0))
rlm@106 322 )
rlm@106 323
rlm@106 324 (fn [world _]
rlm@106 325 (if (zero? (rem (swap! timer inc) 100))
rlm@106 326 (do
rlm@106 327 ;; (println-repl @timer)
rlm@106 328 (.attachChild (.getRootNode world)
rlm@106 329 (sphere 0.05 :color ColorRGBA/Yellow
rlm@106 330 :position (.getWorldTranslation top)
rlm@106 331 :physical? false))
rlm@106 332 (.attachChild (.getRootNode world)
rlm@106 333 (sphere 0.05 :color ColorRGBA/LightGray
rlm@106 334 :position (.getWorldTranslation bottom)
rlm@106 335 :physical? false))))
rlm@106 336
rlm@106 337 (if @move-up?
rlm@106 338 (.applyTorque control
rlm@106 339 (.mult (.getPhysicsRotation control)
rlm@106 340 (Vector3f. 0 0 10))))
rlm@106 341 (if @move-down?
rlm@106 342 (.applyTorque control
rlm@106 343 (.mult (.getPhysicsRotation control)
rlm@106 344 (Vector3f. 0 0 -10))))
rlm@106 345 (if @move-left?
rlm@106 346 (.applyTorque control
rlm@106 347 (.mult (.getPhysicsRotation control)
rlm@106 348 (Vector3f. 0 10 0))))
rlm@106 349 (if @move-right?
rlm@106 350 (.applyTorque control
rlm@106 351 (.mult (.getPhysicsRotation control)
rlm@106 352 (Vector3f. 0 -10 0))))
rlm@106 353 (if @roll-left?
rlm@106 354 (.applyTorque control
rlm@106 355 (.mult (.getPhysicsRotation control)
rlm@106 356 (Vector3f. -1 0 0))))
rlm@106 357 (if @roll-right?
rlm@106 358 (.applyTorque control
rlm@106 359 (.mult (.getPhysicsRotation control)
rlm@106 360 (Vector3f. 1 0 0))))))))
rlm@99 361 #+end_src
rlm@192 362
rlm@99 363
rlm@99 364 * COMMENT generate source
rlm@192 365 #+begin_src clojure :tangle ../src/cortex/integration.clj
rlm@192 366 <<integration>>
rlm@99 367 #+end_src
rlm@99 368
rlm@99 369
rlm@94 370
rlm@94 371