annotate org/worm_learn.clj @ 399:85393ec986dc

completed action definitions.
author Robert McIntyre <rlm@mit.edu>
date Tue, 04 Mar 2014 23:04:43 -0500
parents 36d492f4deab
children 6ba908c1a0a9
rev   line source
rlm@394 1 (ns org.aurellem.worm-learn
rlm@394 2 "General worm creation framework."
rlm@394 3 {:author "Robert McIntyre"}
rlm@394 4 (:use (cortex world util import body sense
rlm@394 5 hearing touch vision proprioception movement))
rlm@394 6 (:import (com.jme3.math ColorRGBA Vector3f))
rlm@394 7 (:import java.io.File)
rlm@394 8 (:import com.jme3.audio.AudioNode)
rlm@397 9 (:import com.aurellem.capture.RatchetTimer)
rlm@397 10 (:import (com.aurellem.capture Capture IsoTimer))
rlm@397 11 (:import (com.jme3.math Vector3f ColorRGBA)))
rlm@397 12
rlm@394 13
rlm@394 14 (dorun (cortex.import/mega-import-jme3))
rlm@394 15 (rlm.rlm-commands/help)
rlm@394 16
rlm@394 17
rlm@399 18 (def hand "Models/test-creature/hand.blend")
rlm@394 19
rlm@399 20 (defn worm-model []
rlm@399 21 (load-blender-model "Models/worm/worm.blend"))
rlm@394 22
rlm@399 23 (def output-base (File. "/home/r/proj/cortex/render/worm-learn/"))
rlm@394 24
rlm@397 25
rlm@399 26 (defn motor-control-program
rlm@399 27 "Create a function which will execute the motor script"
rlm@399 28 [muscle-positions
rlm@399 29 script]
rlm@399 30 (let [current-frame (atom -1)
rlm@399 31 keyed-script (group-by first script)
rlm@399 32 current-forces (atom {}) ]
rlm@399 33 (fn [effectors]
rlm@399 34 (let [indexed-effectors (vec effectors)]
rlm@399 35 (dorun
rlm@399 36 (for [[_ part force] (keyed-script (swap! current-frame inc))]
rlm@399 37 (swap! current-forces (fn [m] (assoc m part force)))))
rlm@399 38 (doall (map (fn [effector power]
rlm@399 39 (effector (int power)))
rlm@399 40 effectors
rlm@399 41 (map #(@current-forces % 0) muscle-positions)))))))
rlm@397 42
rlm@399 43 (def muscle-labels
rlm@399 44 [:base-up :base-down
rlm@399 45 :a-up :a-down
rlm@399 46 :b-up :b-down
rlm@399 47 :c-up :c-down
rlm@399 48 :d-up :d-down
rlm@399 49 ])
rlm@397 50
rlm@399 51 (def curl-script
rlm@399 52 [[370 :d-up 20]
rlm@399 53 [390 :d-up 0]])
rlm@397 54
rlm@399 55 (def period 18)
rlm@399 56
rlm@399 57 (defn gen-wiggle [[flexor extensor :as muscle-pair] time-base]
rlm@399 58 (let [period period
rlm@399 59 power 45]
rlm@399 60 [[time-base flexor power]
rlm@399 61 [(+ time-base period) flexor 0]
rlm@399 62 [(+ time-base period 1) extensor power]
rlm@399 63 [(+ time-base (+ (* 2 period) 2)) extensor 0]]))
rlm@399 64
rlm@399 65 (def wiggle-script
rlm@399 66 (mapcat gen-wiggle [[:d-up :d-down]
rlm@399 67 [:c-up :c-down]
rlm@399 68 [:b-up :b-down]
rlm@399 69 [:a-up :a-down]] (range 100 1000 12)))
rlm@399 70
rlm@399 71 (def wiggle-script
rlm@399 72 (mapcat gen-wiggle (repeat 40 [:a-down :a-up])
rlm@399 73 (range 100 10000 (+ 3 (* period 2)))))
rlm@399 74
rlm@399 75
rlm@399 76
rlm@399 77 (defn worm-world
rlm@399 78 ""
rlm@399 79 ([] (worm-world curl-script))
rlm@399 80 ([motion-script]
rlm@399 81 (let [record? false
rlm@399 82 worm (doto (worm-model) (body!))
rlm@399 83 touch '();;(touch! worm)
rlm@399 84 prop (proprioception! worm)
rlm@399 85 muscles (movement! worm)
rlm@399 86
rlm@399 87 touch-display (view-touch)
rlm@399 88 prop-display (view-proprioception)
rlm@399 89 muscle-display (view-movement)
rlm@399 90
rlm@399 91 floor (box 10 1 10 :position (Vector3f. 0 -10 0)
rlm@399 92 :color ColorRGBA/Gray :mass 0)
rlm@399 93
rlm@399 94 control-script (motor-control-program
rlm@399 95 muscle-labels motion-script)]
rlm@397 96 (world
rlm@399 97 (nodify [worm floor])
rlm@397 98 standard-debug-controls
rlm@397 99
rlm@397 100 (fn [world]
rlm@399 101 ;; (set-gravity world Vector3f/ZERO)
rlm@399 102 ;; (position-camera
rlm@399 103 ;; world (Vector3f. 4.207176, -3.7366982, 3.0816958)
rlm@399 104 ;; (Quaternion. 0.11118768, 0.87678415, 0.24434438, -0.3989771))
rlm@399 105
rlm@399 106
rlm@399 107
rlm@399 108 (position-camera
rlm@399 109 world (Vector3f. 4.207176, -3.7366982, 3.0816958)
rlm@399 110 (Quaternion. -0.11555642, 0.88188726, -0.2854942, -0.3569518))
rlm@399 111
rlm@399 112
rlm@399 113
rlm@399 114
rlm@397 115 (let [timer (IsoTimer. 60)]
rlm@397 116 (.setTimer world timer)
rlm@397 117 (display-dilated-time world timer))
rlm@397 118 (if record?
rlm@397 119 (Capture/captureVideo
rlm@397 120 world
rlm@399 121 (File. output-base "main-view")))
rlm@397 122 (speed-up world)
rlm@397 123 (light-up-everything world))
rlm@397 124
rlm@397 125 (fn [world tpf]
rlm@399 126 (muscle-display
rlm@399 127 (control-script muscles)
rlm@399 128 (if record? (File. output-base "muscle")))
rlm@399 129 (prop-display
rlm@399 130 (prop)
rlm@399 131 (if record? (File. output-base "proprio")))
rlm@397 132 (touch-display
rlm@397 133 (map #(% (.getRootNode world)) touch)
rlm@397 134 (if record?
rlm@399 135 (File. output-base "touch"))))))))