rlm@394: (ns org.aurellem.worm-learn rlm@394: "General worm creation framework." rlm@394: {:author "Robert McIntyre"} rlm@394: (:use (cortex world util import body sense rlm@394: hearing touch vision proprioception movement)) rlm@394: (:import (com.jme3.math ColorRGBA Vector3f)) rlm@394: (:import java.io.File) rlm@394: (:import com.jme3.audio.AudioNode) rlm@397: (:import com.aurellem.capture.RatchetTimer) rlm@397: (:import (com.aurellem.capture Capture IsoTimer)) rlm@397: (:import (com.jme3.math Vector3f ColorRGBA))) rlm@397: rlm@394: rlm@394: (dorun (cortex.import/mega-import-jme3)) rlm@394: (rlm.rlm-commands/help) rlm@394: rlm@394: rlm@399: (def hand "Models/test-creature/hand.blend") rlm@394: rlm@399: (defn worm-model [] rlm@399: (load-blender-model "Models/worm/worm.blend")) rlm@394: rlm@399: (def output-base (File. "/home/r/proj/cortex/render/worm-learn/")) rlm@394: rlm@397: rlm@399: (defn motor-control-program rlm@399: "Create a function which will execute the motor script" rlm@399: [muscle-positions rlm@399: script] rlm@399: (let [current-frame (atom -1) rlm@399: keyed-script (group-by first script) rlm@399: current-forces (atom {}) ] rlm@399: (fn [effectors] rlm@399: (let [indexed-effectors (vec effectors)] rlm@399: (dorun rlm@399: (for [[_ part force] (keyed-script (swap! current-frame inc))] rlm@399: (swap! current-forces (fn [m] (assoc m part force))))) rlm@399: (doall (map (fn [effector power] rlm@399: (effector (int power))) rlm@399: effectors rlm@399: (map #(@current-forces % 0) muscle-positions))))))) rlm@397: rlm@399: (def muscle-labels rlm@399: [:base-up :base-down rlm@399: :a-up :a-down rlm@399: :b-up :b-down rlm@399: :c-up :c-down rlm@399: :d-up :d-down rlm@399: ]) rlm@397: rlm@399: (def curl-script rlm@399: [[370 :d-up 20] rlm@399: [390 :d-up 0]]) rlm@397: rlm@399: (def period 18) rlm@399: rlm@399: (defn gen-wiggle [[flexor extensor :as muscle-pair] time-base] rlm@399: (let [period period rlm@399: power 45] rlm@399: [[time-base flexor power] rlm@399: [(+ time-base period) flexor 0] rlm@399: [(+ time-base period 1) extensor power] rlm@399: [(+ time-base (+ (* 2 period) 2)) extensor 0]])) rlm@399: rlm@399: (def wiggle-script rlm@399: (mapcat gen-wiggle [[:d-up :d-down] rlm@399: [:c-up :c-down] rlm@399: [:b-up :b-down] rlm@399: [:a-up :a-down]] (range 100 1000 12))) rlm@399: rlm@399: (def wiggle-script rlm@399: (mapcat gen-wiggle (repeat 40 [:a-down :a-up]) rlm@399: (range 100 10000 (+ 3 (* period 2))))) rlm@399: rlm@399: rlm@399: rlm@399: (defn worm-world rlm@399: "" rlm@399: ([] (worm-world curl-script)) rlm@399: ([motion-script] rlm@399: (let [record? false rlm@399: worm (doto (worm-model) (body!)) rlm@399: touch '();;(touch! worm) rlm@399: prop (proprioception! worm) rlm@399: muscles (movement! worm) rlm@399: rlm@399: touch-display (view-touch) rlm@399: prop-display (view-proprioception) rlm@399: muscle-display (view-movement) rlm@399: rlm@399: floor (box 10 1 10 :position (Vector3f. 0 -10 0) rlm@399: :color ColorRGBA/Gray :mass 0) rlm@399: rlm@399: control-script (motor-control-program rlm@399: muscle-labels motion-script)] rlm@397: (world rlm@399: (nodify [worm floor]) rlm@397: standard-debug-controls rlm@397: rlm@397: (fn [world] rlm@399: ;; (set-gravity world Vector3f/ZERO) rlm@399: ;; (position-camera rlm@399: ;; world (Vector3f. 4.207176, -3.7366982, 3.0816958) rlm@399: ;; (Quaternion. 0.11118768, 0.87678415, 0.24434438, -0.3989771)) rlm@399: rlm@399: rlm@399: rlm@399: (position-camera rlm@399: world (Vector3f. 4.207176, -3.7366982, 3.0816958) rlm@399: (Quaternion. -0.11555642, 0.88188726, -0.2854942, -0.3569518)) rlm@399: rlm@399: rlm@399: rlm@399: rlm@397: (let [timer (IsoTimer. 60)] rlm@397: (.setTimer world timer) rlm@397: (display-dilated-time world timer)) rlm@397: (if record? rlm@397: (Capture/captureVideo rlm@397: world rlm@399: (File. output-base "main-view"))) rlm@397: (speed-up world) rlm@397: (light-up-everything world)) rlm@397: rlm@397: (fn [world tpf] rlm@399: (muscle-display rlm@399: (control-script muscles) rlm@399: (if record? (File. output-base "muscle"))) rlm@399: (prop-display rlm@399: (prop) rlm@399: (if record? (File. output-base "proprio"))) rlm@397: (touch-display rlm@397: (map #(% (.getRootNode world)) touch) rlm@397: (if record? rlm@399: (File. output-base "touch"))))))))