Mercurial > cortex
view org/worm_learn.clj @ 404:939bcc5950b2
completed debug control of worm.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Mon, 17 Mar 2014 17:29:59 -0400 |
parents | 6ba908c1a0a9 |
children | 9b4a4da08b78 |
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 movement))6 (:import (com.jme3.math ColorRGBA Vector3f))7 (:import java.io.File)8 (:import com.jme3.audio.AudioNode)9 (:import com.aurellem.capture.RatchetTimer)10 (:import (com.aurellem.capture Capture IsoTimer))11 (:import (com.jme3.math Vector3f ColorRGBA)))14 (dorun (cortex.import/mega-import-jme3))15 (rlm.rlm-commands/help)17 (load-bullet)19 (def hand "Models/test-creature/hand.blend")21 (defn worm-model []22 (load-blender-model "Models/worm/worm.blend"))24 (def output-base (File. "/home/r/proj/cortex/render/worm-learn/curl"))27 (defn motor-control-program28 "Create a function which will execute the motor script"29 [muscle-positions30 script]31 (let [current-frame (atom -1)32 keyed-script (group-by first script)33 current-forces (atom {}) ]34 (fn [effectors]35 (let [indexed-effectors (vec effectors)]36 (dorun37 (for [[_ part force] (keyed-script (swap! current-frame inc))]38 (swap! current-forces (fn [m] (assoc m part force)))))39 (doall (map (fn [effector power]40 (effector (int power)))41 effectors42 (map #(@current-forces % 0) muscle-positions)))))))44 (defn worm-direct-control45 "Create keybindings and a muscle control program that will enable46 the user to control the worm via the keyboard."47 [muscle-labels activation-strength]48 (let [strengths (mapv (fn [_] (atom 0)) muscle-labels)49 activator50 (fn [n]51 (fn [world pressed?]52 (let [strength (if pressed? activation-strength 0)]53 (swap! (nth strengths n) (constantly strength)))))54 activators55 (map activator (range (count muscle-labels)))56 worm-keys57 ["key-f" "key-r"58 "key-g" "key-t"59 "key-y" "key-h"60 "key-j" "key-u"61 "key-i" "key-k"62 "key-o" "key-l"]]63 {:motor-control64 (fn [effectors]65 (doall66 (map (fn [strength effector]67 (effector (deref strength)))68 strengths effectors)))69 :keybindings70 ;; assume muscles are listed in pairs and map them to keys.71 (zipmap worm-keys activators)}))73 ;; These are scripts that direct the worm to move in two radically74 ;; different patterns -- a sinusoidal wiggling motion, and a curling75 ;; motions that causes the worm to form a circle.77 (def curl-script78 [[370 :d-up 40]79 [600 :d-up 0]])81 (def period 18)83 (def worm-muscle-labels84 [:base-up :base-down85 :a-down :a-up86 :b-up :b-down87 :c-down :c-up88 :d-up :d-down])90 (defn gen-wiggle [[flexor extensor :as muscle-pair] time-base]91 (let [period period92 power 45]93 [[time-base flexor power]94 [(+ time-base period) flexor 0]95 [(+ time-base period 1) extensor power]96 [(+ time-base (+ (* 2 period) 2)) extensor 0]]))98 (def wiggle-script99 (mapcat gen-wiggle (repeat 40 [:a-down :a-up])100 (range 100 10000 (+ 3 (* period 2)))))103 ;; Normally, we'd use unsupervised/supervised machine learning to pick104 ;; out the defining features of the different actions available to the105 ;; worm. For this project, I am going to explicitely define functions106 ;; that recognize curling and wiggling respectively. These functions107 ;; are defined using all the information available from an embodied108 ;; simulation of the action. Note how much easier they are to define109 ;; than if I only had vision to work with. Things like scale/position110 ;; invariance are complete non-issues here. This is the advantage of111 ;; body-centered action recognition and what I hope to show with this112 ;; thesis.114 (defn last-nth115 "Create function that will, when called each frame with the senses116 of a creature, will record those results and return the last n117 results."118 [n]119 (let [last-n '()]120 (fn [frame-num {:keys [touch proprioception muscles hearing]}]121 (take n (cons [frame-num :stuff] last-n)))))124 (defn wiggling?125 "Generate a function which, when called each frame with the sensory126 inputs of a worm, will determine whether the worm is wiggling."127 [{:keys [touch proprioception muscles hearing]}]128 (map (fn [f] (f)) proprioception131 ))133 (defn curling?134 "Is the worm curled up?"135 []136 )138 (defn resting?139 "Is the worm on the ground in a neutral position?"140 [])142 (def standard-world-view143 [(Vector3f. 4.207176, -3.7366982, 3.0816958)144 (Quaternion. 0.11118768, 0.87678415, 0.24434438, -0.3989771)])146 (def worm-side-view147 [(Vector3f. 4.207176, -3.7366982, 3.0816958)148 (Quaternion. -0.11555642, 0.88188726, -0.2854942, -0.3569518)])150 (def degenerate-worm-view151 [(Vector3f. -0.0708936, -8.570261, 2.6487997)152 (Quaternion. -2.318909E-4, 0.9985348, 0.053941682, 0.004291452)])154 (defn worm-world-defaults []155 (let [direct-control (worm-direct-control worm-muscle-labels 40)]156 {:view worm-side-view157 :motor-control (:motor-control direct-control)158 :keybindings (:keybindings direct-control)159 :record nil}))161 (defn dir! [file]162 (if (not (.exists file))163 (.mkdir file))164 file)166 (defn worm-world167 [& {:keys [record motor-control keybindings view] :as settings}]168 (let [{:keys [record motor-control keybindings view]}169 (merge (worm-world-defaults) settings)170 worm (doto (worm-model) (body!))171 touch (touch! worm)172 prop (proprioception! worm)173 muscles (movement! worm)175 touch-display (view-touch)176 prop-display (view-proprioception)177 muscle-display (view-movement)179 floor (box 10 1 10 :position (Vector3f. 0 -10 0)180 :color ColorRGBA/Gray :mass 0)]182 (world183 (nodify [worm floor])184 (merge standard-debug-controls keybindings)185 (fn [world]186 (position-camera world view)187 (let [timer (IsoTimer. 60)]188 (.setTimer world timer)189 (display-dilated-time world timer))190 (if record191 (Capture/captureVideo192 world193 (dir! (File. record "main-view"))))194 (speed-up world)195 (light-up-everything world))196 (fn [world tpf]197 (let [strong! (motor-control muscles)]198 (println strong!)199 (muscle-display200 strong!201 (if record (dir! (File. record "muscle")))))202 (prop-display203 (prop)204 (if record (dir! (File. record "proprio"))))205 (touch-display206 (map #(% (.getRootNode world)) touch)207 (if record208 (File. record "touch")))))))