Mercurial > cortex
view org/worm_learn.clj @ 410:e6a7e80f885a
refactor, fix null pointer bug.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 18 Mar 2014 22:29:03 -0400 |
parents | 3b4012b42611 |
children | a331d5ff73e0 |
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 (use 'clojure.pprint)15 (use 'clojure.set)16 (dorun (cortex.import/mega-import-jme3))17 (rlm.rlm-commands/help)19 (load-bullet)21 (def hand "Models/test-creature/hand.blend")23 (defn worm-model []24 (load-blender-model "Models/worm/worm.blend"))26 (def output-base (File. "/home/r/proj/cortex/render/worm-learn/curl"))29 (defn motor-control-program30 "Create a function which will execute the motor script"31 [muscle-labels32 script]33 (let [current-frame (atom -1)34 keyed-script (group-by first script)35 current-forces (atom {}) ]36 (fn [effectors]37 (let [indexed-effectors (vec effectors)]38 (dorun39 (for [[_ part force] (keyed-script (swap! current-frame inc))]40 (swap! current-forces (fn [m] (assoc m part force)))))41 (doall (map (fn [effector power]42 (effector (int power)))43 effectors44 (map #(@current-forces % 0) muscle-labels)))))))46 (defn worm-direct-control47 "Create keybindings and a muscle control program that will enable48 the user to control the worm via the keyboard."49 [muscle-labels activation-strength]50 (let [strengths (mapv (fn [_] (atom 0)) muscle-labels)51 activator52 (fn [n]53 (fn [world pressed?]54 (let [strength (if pressed? activation-strength 0)]55 (swap! (nth strengths n) (constantly strength)))))56 activators57 (map activator (range (count muscle-labels)))58 worm-keys59 ["key-f" "key-r"60 "key-g" "key-t"61 "key-y" "key-h"62 "key-j" "key-u"63 "key-i" "key-k"64 "key-o" "key-l"]]65 {:motor-control66 (fn [effectors]67 (doall68 (map (fn [strength effector]69 (effector (deref strength)))70 strengths effectors)))71 :keybindings72 ;; assume muscles are listed in pairs and map them to keys.73 (zipmap worm-keys activators)}))75 ;; These are scripts that direct the worm to move in two radically76 ;; different patterns -- a sinusoidal wiggling motion, and a curling77 ;; motions that causes the worm to form a circle.79 (def curl-script80 [[370 :d-up 40]81 [600 :d-up 0]])83 (def period 18)85 (def worm-muscle-labels86 [:base-up :base-down87 :a-down :a-up88 :b-up :b-down89 :c-down :c-up90 :d-up :d-down])92 (defn gen-wiggle [[flexor extensor :as muscle-pair] time-base]93 (let [period period94 power 45]95 [[time-base flexor power]96 [(+ time-base period) flexor 0]97 [(+ time-base period 1) extensor power]98 [(+ time-base (+ (* 2 period) 2)) extensor 0]]))100 (def wiggle-script101 (mapcat gen-wiggle (repeat 4000 [:a-down :a-up])102 (range 100 1000000 (+ 3 (* period 2)))))105 ;; Normally, we'd use unsupervised/supervised machine learning to pick106 ;; out the defining features of the different actions available to the107 ;; worm. For this project, I am going to explicitely define functions108 ;; that recognize curling and wiggling respectively. These functions109 ;; are defined using all the information available from an embodied110 ;; simulation of the action. Note how much easier they are to define111 ;; than if I only had vision to work with. Things like scale/position112 ;; invariance are complete non-issues here. This is the advantage of113 ;; body-centered action recognition and what I hope to show with this114 ;; thesis.117 (defn straight?118 "Is the worm straight?"119 [experiences]120 (every?121 (fn [[_ _ bend]]122 (< (Math/sin bend) 0.05))123 (:proprioception (peek experiences))))125 (defn curled?126 "Is the worm curled up?"127 [experiences]128 (every?129 (fn [[_ _ bend]]130 (> (Math/sin bend) 0.64))131 (:proprioception (peek experiences))))133 (defn grand-circle?134 "Does the worm form a majestic circle (one end touching the other)?"135 [experiences]136 (and (curled? experiences)137 true)) ;; TODO: add code here.139 (defn vector:last-n [v n]140 (let [c (count v)]141 (if (< c n) v142 (subvec v (- c n) c))))144 (defn touch-average [[coords touch]]145 (/ (average (map first touch)) (average (map second touch))))147 (def worm-segment-touch-bottom148 [[8 15] [8 16] [8 17] [8 18] [8 19] [8 20] [8 21] [8 22] [9 15]149 [9 16] [9 17] [9 18] [9 19] [9 20] [9 21] [9 22] [10 15] [10 16]150 [10 17] [10 18] [10 19] [10 20] [10 21] [10 22] [11 15] [11 16]151 [11 17] [11 18] [11 19] [11 20] [11 21] [11 22] [12 15] [12 16]152 [12 17] [12 18] [12 19] [12 20] [12 21] [12 22] [13 15] [13 16]153 [13 17] [13 18] [13 19] [13 20] [13 21] [13 22] [14 15] [14 16]154 [14 17] [14 18] [14 19] [14 20] [14 21] [14 22]])158 (defn floor-contact [[coords contact :as touch]]159 (let [raw-average160 (average161 (map162 first163 (vals164 (select-keys165 (zipmap coords contact)166 ))))]167 (Math/abs (- 1. (* 10 raw-average)))))170 (defn wiggling?171 "Is the worm wiggling?"172 [experiences]173 (vector:last-n experiences 200)175 )177 (def standard-world-view178 [(Vector3f. 4.207176, -3.7366982, 3.0816958)179 (Quaternion. 0.11118768, 0.87678415, 0.24434438, -0.3989771)])181 (def worm-side-view182 [(Vector3f. 4.207176, -3.7366982, 3.0816958)183 (Quaternion. -0.11555642, 0.88188726, -0.2854942, -0.3569518)])185 (def degenerate-worm-view186 [(Vector3f. -0.0708936, -8.570261, 2.6487997)187 (Quaternion. -2.318909E-4, 0.9985348, 0.053941682, 0.004291452)])189 (defn worm-world-defaults []190 (let [direct-control (worm-direct-control worm-muscle-labels 40)]191 {:view worm-side-view192 :motor-control (:motor-control direct-control)193 :keybindings (:keybindings direct-control)194 :record nil195 :experiences nil196 :worm-model worm-model197 :end-frame nil}))199 (defn dir! [file]200 (if-not (.exists file)201 (.mkdir file))202 file)204 (defn record-experience! [experiences data]205 (swap! experiences #(conj % data)))207 (defn worm-world208 [& {:keys [record motor-control keybindings view experiences209 worm-model end-frame] :as settings}]210 (let [{:keys [record motor-control keybindings view experiences211 worm-model end-frame]}212 (merge (worm-world-defaults) settings)213 worm (doto (worm-model) (body!))214 touch (touch! worm)215 prop (proprioception! worm)216 muscles (movement! worm)218 touch-display (view-touch)219 prop-display (view-proprioception)220 muscle-display (view-movement)222 floor (box 10 1 10 :position (Vector3f. 0 -10 0)223 :color ColorRGBA/Gray :mass 0)224 timer (IsoTimer. 60)]226 (world227 (nodify [worm floor])228 (merge standard-debug-controls keybindings)229 (fn [world]230 (position-camera world view)231 (.setTimer world timer)232 (display-dilated-time world timer)233 (if record234 (Capture/captureVideo235 world236 (dir! (File. record "main-view"))))237 (speed-up world)238 (light-up-everything world))239 (fn [world tpf]240 (if (and end-frame (> (.getTime timer) end-frame))241 (.stop world))242 (let [muscle-data (motor-control muscles)243 proprioception-data (prop)244 touch-data (map #(% (.getRootNode world)) touch)]245 (when experiences246 (record-experience!247 experiences {:touch touch-data248 :proprioception proprioception-data249 :muscle muscle-data})250 ;;(if (curled? @experiences) (println "Curled"))251 ;;(if (straight? @experiences) (println "Straight"))252 ;; (println-repl253 ;; (apply format "%.2f %.2f %.2f %.2f %.2f\n"254 ;; (map floor-contact touch-data)))256 )257 (muscle-display258 muscle-data259 (if record (dir! (File. record "muscle"))))260 (prop-display261 proprioception-data262 (if record (dir! (File. record "proprio"))))263 (touch-display264 touch-data265 (if record (dir! (File. record "touch")))))))))