Mercurial > cortex
view org/worm_learn.clj @ 413:54ef2e06c3ef
gave main worm a consistent muscle order.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Wed, 19 Mar 2014 14:24:13 -0400 |
parents | a331d5ff73e0 |
children | 634795361af8 |
line wrap: on
line source
1 (ns org.aurellem.worm-learn2 "General worm creation framework."3 {:author "Robert McIntyre"}4 (:use (cortex world util import body sense5 hearing touch vision proprioception movement6 test))7 (:import (com.jme3.math ColorRGBA Vector3f))8 (:import java.io.File)9 (:import com.jme3.audio.AudioNode)10 (:import com.aurellem.capture.RatchetTimer)11 (:import (com.aurellem.capture Capture IsoTimer))12 (:import (com.jme3.math Vector3f ColorRGBA)))14 (import org.apache.commons.math3.transform.TransformType)15 (import org.apache.commons.math3.transform.FastFourierTransformer)16 (import org.apache.commons.math3.transform.DftNormalization)18 (use 'clojure.pprint)19 (use 'clojure.set)20 (dorun (cortex.import/mega-import-jme3))21 (rlm.rlm-commands/help)23 (load-bullet)25 (def hand "Models/test-creature/hand.blend")27 (defn worm-model []28 (load-blender-model "Models/worm/worm.blend"))30 (def output-base (File. "/home/r/proj/cortex/render/worm-learn/curl"))33 (defn motor-control-program34 "Create a function which will execute the motor script"35 [muscle-labels36 script]37 (let [current-frame (atom -1)38 keyed-script (group-by first script)39 current-forces (atom {}) ]40 (fn [effectors]41 (let [indexed-effectors (vec effectors)]42 (dorun43 (for [[_ part force] (keyed-script (swap! current-frame inc))]44 (swap! current-forces (fn [m] (assoc m part force)))))45 (doall (map (fn [effector power]46 (effector (int power)))47 effectors48 (map #(@current-forces % 0) muscle-labels)))))))50 (defn worm-direct-control51 "Create keybindings and a muscle control program that will enable52 the user to control the worm via the keyboard."53 [muscle-labels activation-strength]54 (let [strengths (mapv (fn [_] (atom 0)) muscle-labels)55 activator56 (fn [n]57 (fn [world pressed?]58 (let [strength (if pressed? activation-strength 0)]59 (swap! (nth strengths n) (constantly strength)))))60 activators61 (map activator (range (count muscle-labels)))62 worm-keys63 ["key-f" "key-r"64 "key-g" "key-t"65 "key-h" "key-y"66 "key-j" "key-u"67 "key-k" "key-i"68 "key-l" "key-o"]]69 {:motor-control70 (fn [effectors]71 (doall72 (map (fn [strength effector]73 (effector (deref strength)))74 strengths effectors)))75 :keybindings76 ;; assume muscles are listed in pairs and map them to keys.77 (zipmap worm-keys activators)}))79 ;; These are scripts that direct the worm to move in two radically80 ;; different patterns -- a sinusoidal wiggling motion, and a curling81 ;; motions that causes the worm to form a circle.83 (def curl-script84 [[370 :d-up 40]85 [600 :d-up 0]])87 (def period 18)89 (def worm-muscle-labels90 [:base-down :base-up91 :a-down :a-up92 :b-down :b-up93 :c-down :c-up94 :d-down :d-up])96 (defn gen-wiggle [[flexor extensor :as muscle-pair] time-base]97 (let [period period98 power 45]99 [[time-base flexor power]100 [(+ time-base period) flexor 0]101 [(+ time-base period 1) extensor power]102 [(+ time-base (+ (* 2 period) 2)) extensor 0]]))104 (def wiggle-script105 (mapcat gen-wiggle (repeat 4000 [:a-down :a-up])106 (range 100 1000000 (+ 3 (* period 2)))))109 ;; Normally, we'd use unsupervised/supervised machine learning to pick110 ;; out the defining features of the different actions available to the111 ;; worm. For this project, I am going to explicitely define functions112 ;; that recognize curling and wiggling respectively. These functions113 ;; are defined using all the information available from an embodied114 ;; simulation of the action. Note how much easier they are to define115 ;; than if I only had vision to work with. Things like scale/position116 ;; invariance are complete non-issues here. This is the advantage of117 ;; body-centered action recognition and what I hope to show with this118 ;; thesis.121 (defn straight?122 "Is the worm straight?"123 [experiences]124 (every?125 (fn [[_ _ bend]]126 (< (Math/sin bend) 0.05))127 (:proprioception (peek experiences))))129 (defn curled?130 "Is the worm curled up?"131 [experiences]132 (every?133 (fn [[_ _ bend]]134 (> (Math/sin bend) 0.64))135 (:proprioception (peek experiences))))137 (defn grand-circle?138 "Does the worm form a majestic circle (one end touching the other)?"139 [experiences]140 (and (curled? experiences)141 true)) ;; TODO: add code here.143 (defn vector:last-n [v n]144 (let [c (count v)]145 (if (< c n) v146 (subvec v (- c n) c))))148 (defn touch-average [[coords touch]]149 (/ (average (map first touch)) (average (map second touch))))151 (defn rect-region [[x0 y0] [x1 y1]]152 (vec153 (for [x (range x0 (inc x1))154 y (range y0 (inc y1))]155 [x y])))157 (def worm-segment-touch-bottom (rect-region [8 15] [14 22]))159 (defn contact160 "Determine how much contact a particular worm segment has with161 other objects. Returns a value between 0 and 1, where 1 is full162 contact and 0 is no contact."163 [[coords contact :as touch]]164 (-> (zipmap coords contact)165 (select-keys worm-segment-touch-bottom)166 (vals)167 (#(map first %))168 (average)169 (* 10)170 (- 1)171 (Math/abs)))173 (defn fft [nums]174 (.transform175 (FastFourierTransformer. DftNormalization/UNITARY)176 (double-array nums) TransformType/FORWARD))178 (def indexed (partial map-indexed vector))180 (defn wiggling?181 "Is the worm wiggling?"182 [experiences]183 (map (comp first :muscle) (vector:last-n experiences 200))185 )187 (def standard-world-view188 [(Vector3f. 4.207176, -3.7366982, 3.0816958)189 (Quaternion. 0.11118768, 0.87678415, 0.24434438, -0.3989771)])191 (def worm-side-view192 [(Vector3f. 4.207176, -3.7366982, 3.0816958)193 (Quaternion. -0.11555642, 0.88188726, -0.2854942, -0.3569518)])195 (def degenerate-worm-view196 [(Vector3f. -0.0708936, -8.570261, 2.6487997)197 (Quaternion. -2.318909E-4, 0.9985348, 0.053941682, 0.004291452)])199 (defn worm-world-defaults []200 (let [direct-control (worm-direct-control worm-muscle-labels 40)]201 {:view worm-side-view202 :motor-control (:motor-control direct-control)203 :keybindings (:keybindings direct-control)204 :record nil205 :experiences nil206 :worm-model worm-model207 :end-frame nil}))209 (defn dir! [file]210 (if-not (.exists file)211 (.mkdir file))212 file)214 (defn record-experience! [experiences data]215 (swap! experiences #(conj % data)))217 (defn worm-world218 [& {:keys [record motor-control keybindings view experiences219 worm-model end-frame] :as settings}]220 (let [{:keys [record motor-control keybindings view experiences221 worm-model end-frame]}222 (merge (worm-world-defaults) settings)223 worm (doto (worm-model) (body!))224 touch (touch! worm)225 prop (proprioception! worm)226 muscles (movement! worm)228 touch-display (view-touch)229 prop-display (view-proprioception)230 muscle-display (view-movement)232 floor (box 10 1 10 :position (Vector3f. 0 -10 0)233 :color ColorRGBA/Gray :mass 0)234 timer (IsoTimer. 60)]236 (world237 (nodify [worm floor])238 (merge standard-debug-controls keybindings)239 (fn [world]240 (position-camera world view)241 (.setTimer world timer)242 (display-dilated-time world timer)243 (if record244 (Capture/captureVideo245 world246 (dir! (File. record "main-view"))))247 (speed-up world)248 (light-up-everything world))249 (fn [world tpf]250 (if (and end-frame (> (.getTime timer) end-frame))251 (.stop world))252 (let [muscle-data (motor-control muscles)253 proprioception-data (prop)254 touch-data (map #(% (.getRootNode world)) touch)]255 (when experiences256 (record-experience!257 experiences {:touch touch-data258 :proprioception proprioception-data259 :muscle muscle-data})260 ;;(if (curled? @experiences) (println "Curled"))261 ;;(if (straight? @experiences) (println "Straight"))262 ;; (println-repl263 ;; (apply format "%.2f %.2f %.2f %.2f %.2f\n"264 ;; (map contact touch-data)))266 )267 (muscle-display268 muscle-data269 (if record (dir! (File. record "muscle"))))270 (prop-display271 proprioception-data272 (if record (dir! (File. record "proprio"))))273 (touch-display274 touch-data275 (if record (dir! (File. record "touch")))))))))