Mercurial > cortex
view 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 |
line wrap: on
line source
1 #+title:2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description:5 #+keywords: simulation, jMonkeyEngine3, clojure6 #+SETUPFILE: ../../aurellem/org/setup.org7 #+INCLUDE: ../../aurellem/org/level-0.org9 * Intro11 This is the ultimate test which features all of the senses that I've12 made so far. The blender file for the creature serves as an example of13 a fully equipped creature in terms of senses. You can find it [[../assets/Models/test-creature/hand.blend][here]].15 #+name: integration16 #+begin_src clojure17 (ns cortex.integration18 "let's play!"19 {:author "Robert McIntyre"}20 (:use (cortex world util body sense21 hearing touch vision proprioception movement))22 (:import (com.jme3.math ColorRGBA Vector3f))23 (:import java.io.File)24 (:import com.jme3.audio.AudioNode)25 (:import com.aurellem.capture.RatchetTimer))27 (dorun (cortex.import/mega-import-jme3))28 (rlm.rlm-commands/help)30 (def hand "Models/test-creature/hand.blend")32 (def output-base (File. "/home/r/proj/cortex/render/hand"))34 (defn motor-control-program35 "Create a function which will execute the motor script"36 [muscle-positions37 script]38 (let [current-frame (atom -1)39 keyed-script (group-by first script)40 current-forces (atom {}) ]41 (fn [effectors]42 (let [indexed-effectors (vec effectors)]43 (dorun44 (for [[_ part force] (keyed-script (swap! current-frame inc))]45 (swap! current-forces (fn [m] (assoc m part force)))))46 (doall (map (fn [effector power]47 (effector (int power)))48 effectors49 (map #(@current-forces % 0) muscle-positions)))))))51 (def muscle-positions52 [:pointer-2-e53 :pointer-2-f54 :thumb-155 :thumb-156 :pointer-1-e57 :pointer-1-f58 :thumb-2-e59 :thumb-2-f60 :middle-1-e61 :middle-1-f62 :pointer-3-f63 :pointer-3-e64 :middle-2-e65 :middle-2-f66 :middle-3-f67 :middle-3-e68 :pinky-2-e69 :pinky-2-f70 :pinky-3-f71 :pinky-3-e72 :ring-3-e73 :ring-3-f74 :ring-2-f75 :ring-2-e76 :ring-1-e77 :ring-1-f78 :thumb-1-e79 :thumb-1-f80 :pinky-1-f81 :pinky-1-e])83 (def full 9001)84 ;; the systax here is [keyframe body-part force]85 (def wiggle-each-finger-tip86 [[200 :pointer-3-f full]87 [200 :pointer-3-f 40]89 [400 :middle-3-f full]90 [401 :middle-3-f 40]92 [600 :ring-3-f full]93 [601 :ring-3-f 40]95 [800 :pinky-3-f full]96 [801 :pinky-3-f 40]])101 (def control-list102 [103 0 ;;pointer-21 #<Vector3f (0.99999994, 0.0, 0.0)>104 0 ;;pointer-21 #<Vector3f (-0.99999994, 0.0, 0.0)>105 0 ;;thumb-11 #<Vector3f (-0.8802276, -0.39781287, -0.25873658)>106 0 ;;thumb-11 #<Vector3f (0.8485723, 0.46149826, 0.2587364)>107 0 ;;pointer-11 #<Vector3f (0.99999994, 0.0, 0.0)>108 0 ;;pointer-11 #<Vector3f (-0.99999994, 0.0, 0.0)>109 0 ;;thumb-2.0011 #<Vector3f (-0.71705645, -0.44753736, -0.5343599)>110 0 ;;thumb-2.0011 #<Vector3f (-0.10567085, 0.83862597, 0.53435963)>111 0 ;;middle-11 #<Vector3f (0.99999994, 0.0, 0.0)>112 0 ;;middle-11 #<Vector3f (-0.99999994, 0.0, 0.0)>113 1 ;;pointer-31 #<Vector3f (-0.99999994, 0.0, 0.0)>114 0 ;;pointer-31 #<Vector3f (0.99999994, 0.0, 0.0)>115 0 ;;middle-21 #<Vector3f (0.99999994, 0.0, 0.0)>116 0 ;;middle-21 #<Vector3f (-0.99999994, 0.0, 0.0)>117 0 ;;middle-31 #<Vector3f (-0.99999994, 0.0, 0.0)>118 0 ;;middle-31 #<Vector3f (0.99999994, 0.0, 0.0)>119 0 ;;pinky-21 #<Vector3f (0.99999994, 0.0, 0.0)>120 0 ;;pinky-21 #<Vector3f (-0.99999994, 0.0, 0.0)>121 0 ;;pinky-31 #<Vector3f (-1, 0.0, 0.0)>122 0 ;;pinky-31 #<Vector3f (1.0, 0.0, 0.0)>123 0 ;;ring-31 #<Vector3f (0.99999994, 0.0, 0.0)>124 0 ;;ring-31 #<Vector3f (-0.99999994, 0.0, 0.0)>125 0 ;;ring-21 #<Vector3f (-0.99999994, 0.0, 0.0)>126 0 ;;ring-21 #<Vector3f (0.99999994, 0.0, 0.0)>127 0 ;;ring-11 #<Vector3f (0.99999994, 0.0, 0.0)>128 0 ;;ring-11 #<Vector3f (-0.99999994, 0.0, 0.0)>129 0 ;;thumb-11 #<Vector3f (-0.43154645, 0.7302033, 0.5296894)>130 0 ;;thumb-11 #<Vector3f (-0.8032993, -0.2722854, -0.5296895)>131 0 ;;pinky-11 #<Vector3f (-0.99999994, 0.0, 0.0)>132 0 ;;pinky-11 #<Vector3f (0.99999994, 0.0, 0.0)>133 ])135 (defn test-everything!136 ([] (test-everything! false))137 ([record?]138 (let [me (sphere 0.5 :color ColorRGBA/Blue :physical? false)140 bell (AudioNode. (asset-manager)141 "Sounds/pure.wav" false)142 creature (doto (load-blender-model hand)143 (body!))145 ;;;;;;;;;;;; Sensors/Effectors ;;;;;;;;;;;;;;;;;;;;;;;;;;;;146 ;;touch (touch! creature)147 ;;touch-display (view-touch)149 ;;vision (vision! creature)150 ;;vision-display (view-vision)152 ;;hearing (hearing! creature)153 ;;hearing-display (view-hearing)155 prop (proprioception! creature)156 prop-display (view-proprioception)158 control-script (motor-control-program159 muscle-positions wiggle-each-finger-tip)160 muscles (movement! creature)161 muscle-display (view-movement)162 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;164 fix-display (gen-fix-display)]165 (world166 (nodify [creature167 (box 10 2 10 :position (Vector3f. 0 -9 0)168 :color ColorRGBA/Gray :mass 0)169 me])170 (merge standard-debug-controls171 {"key-return"172 (fn [_ value]173 (if value174 (.play bell)))})176 (fn [world]177 (.setTimer world (RatchetTimer. 60))178 (light-up-everything world)179 (enable-debug world)180 (add-camera! world181 (add-eye! creature182 (.getChild183 (.getChild creature "eyes") "eye"))184 (comp (view-image) BufferedImage!))185 (speed-up world))186 (fn [world tpf]187 ;;(prop-display (prop))188 ;;(touch-display (map #(% (.getRootNode world)) touch))189 ;;(vision-display (map #(% world) vision))190 ;;(hearing-display (map #(% world) hearing))192 ;;(muscle-display193 (control-script muscles)194 ;;)196 (.setLocalTranslation me (.getLocation (.getCamera world)))197 (fix-display world))))))198 #+end_src200 #+results: integration201 : #'cortex.integration/test-everything!203 * COMMENT purgatory204 #+begin_src clojure205 (defn bullet-trans* []206 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red207 :position (Vector3f. 5 0 0)208 :mass 90)209 obj-b (sphere 0.5 :color ColorRGBA/Blue210 :position (Vector3f. -5 0 0)211 :mass 0)212 control-a (.getControl obj-a RigidBodyControl)213 control-b (.getControl obj-b RigidBodyControl)214 move-up? (atom nil)215 move-down? (atom nil)216 move-left? (atom nil)217 move-right? (atom nil)218 roll-left? (atom nil)219 roll-right? (atom nil)220 force 100221 swivel222 (.toRotationMatrix223 (doto (Quaternion.)224 (.fromAngleAxis (/ Math/PI 2)225 Vector3f/UNIT_X)))226 x-move227 (doto (Matrix3f.)228 (.fromStartEndVectors Vector3f/UNIT_X229 (.normalize (Vector3f. 1 1 0))))231 timer (atom 0)]232 (doto233 (ConeJoint.234 control-a control-b235 (Vector3f. -8 0 0)236 (Vector3f. 2 0 0)237 ;;swivel swivel238 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY239 x-move Matrix3f/IDENTITY240 )241 (.setCollisionBetweenLinkedBodys false)242 (.setLimit (* 1 (/ Math/PI 4)) ;; twist243 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane244 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane245 (world (nodify246 [obj-a obj-b])247 (merge standard-debug-controls248 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))249 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))250 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))251 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))252 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))253 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})255 (fn [world]256 (enable-debug world)257 (set-gravity world Vector3f/ZERO)258 )260 (fn [world _]262 (if @move-up?263 (.applyForce control-a264 (Vector3f. force 0 0)265 (Vector3f. 0 0 0)))266 (if @move-down?267 (.applyForce control-a268 (Vector3f. (- force) 0 0)269 (Vector3f. 0 0 0)))270 (if @move-left?271 (.applyForce control-a272 (Vector3f. 0 force 0)273 (Vector3f. 0 0 0)))274 (if @move-right?275 (.applyForce control-a276 (Vector3f. 0 (- force) 0)277 (Vector3f. 0 0 0)))279 (if @roll-left?280 (.applyForce control-a281 (Vector3f. 0 0 force)282 (Vector3f. 0 0 0)))283 (if @roll-right?284 (.applyForce control-a285 (Vector3f. 0 0 (- force))286 (Vector3f. 0 0 0)))288 (if (zero? (rem (swap! timer inc) 100))289 (.attachChild290 (.getRootNode world)291 (sphere 0.05 :color ColorRGBA/Yellow292 :physical? false :position293 (.getWorldTranslation obj-a)))))294 )295 ))297 (defn test-joint [joint]298 (let [[origin top bottom floor] (world-setup joint)299 control (.getControl top RigidBodyControl)300 move-up? (atom false)301 move-down? (atom false)302 move-left? (atom false)303 move-right? (atom false)304 roll-left? (atom false)305 roll-right? (atom false)306 timer (atom 0)]308 (world309 (nodify [top bottom floor origin])310 (merge standard-debug-controls311 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))312 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))313 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))314 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))315 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))316 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})318 (fn [world]319 (light-up-everything world)320 (enable-debug world)321 (set-gravity world (Vector3f. 0 0 0))322 )324 (fn [world _]325 (if (zero? (rem (swap! timer inc) 100))326 (do327 ;; (println-repl @timer)328 (.attachChild (.getRootNode world)329 (sphere 0.05 :color ColorRGBA/Yellow330 :position (.getWorldTranslation top)331 :physical? false))332 (.attachChild (.getRootNode world)333 (sphere 0.05 :color ColorRGBA/LightGray334 :position (.getWorldTranslation bottom)335 :physical? false))))337 (if @move-up?338 (.applyTorque control339 (.mult (.getPhysicsRotation control)340 (Vector3f. 0 0 10))))341 (if @move-down?342 (.applyTorque control343 (.mult (.getPhysicsRotation control)344 (Vector3f. 0 0 -10))))345 (if @move-left?346 (.applyTorque control347 (.mult (.getPhysicsRotation control)348 (Vector3f. 0 10 0))))349 (if @move-right?350 (.applyTorque control351 (.mult (.getPhysicsRotation control)352 (Vector3f. 0 -10 0))))353 (if @roll-left?354 (.applyTorque control355 (.mult (.getPhysicsRotation control)356 (Vector3f. -1 0 0))))357 (if @roll-right?358 (.applyTorque control359 (.mult (.getPhysicsRotation control)360 (Vector3f. 1 0 0))))))))361 #+end_src364 * COMMENT generate source365 #+begin_src clojure :tangle ../src/cortex/integration.clj366 <<integration>>367 #+end_src