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-learn
2 "General worm creation framework."
3 {:author "Robert McIntyre"}
4 (:use (cortex world util import body sense
5 hearing touch vision proprioception movement
6 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-program
34 "Create a function which will execute the motor script"
35 [muscle-labels
36 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 (dorun
43 (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 effectors
48 (map #(@current-forces % 0) muscle-labels)))))))
50 (defn worm-direct-control
51 "Create keybindings and a muscle control program that will enable
52 the user to control the worm via the keyboard."
53 [muscle-labels activation-strength]
54 (let [strengths (mapv (fn [_] (atom 0)) muscle-labels)
55 activator
56 (fn [n]
57 (fn [world pressed?]
58 (let [strength (if pressed? activation-strength 0)]
59 (swap! (nth strengths n) (constantly strength)))))
60 activators
61 (map activator (range (count muscle-labels)))
62 worm-keys
63 ["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-control
70 (fn [effectors]
71 (doall
72 (map (fn [strength effector]
73 (effector (deref strength)))
74 strengths effectors)))
75 :keybindings
76 ;; 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 radically
80 ;; different patterns -- a sinusoidal wiggling motion, and a curling
81 ;; motions that causes the worm to form a circle.
83 (def curl-script
84 [[370 :d-up 40]
85 [600 :d-up 0]])
87 (def period 18)
89 (def worm-muscle-labels
90 [:base-down :base-up
91 :a-down :a-up
92 :b-down :b-up
93 :c-down :c-up
94 :d-down :d-up])
96 (defn gen-wiggle [[flexor extensor :as muscle-pair] time-base]
97 (let [period period
98 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-script
105 (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 pick
110 ;; out the defining features of the different actions available to the
111 ;; worm. For this project, I am going to explicitely define functions
112 ;; that recognize curling and wiggling respectively. These functions
113 ;; are defined using all the information available from an embodied
114 ;; simulation of the action. Note how much easier they are to define
115 ;; than if I only had vision to work with. Things like scale/position
116 ;; invariance are complete non-issues here. This is the advantage of
117 ;; body-centered action recognition and what I hope to show with this
118 ;; 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) v
146 (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 (vec
153 (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 contact
160 "Determine how much contact a particular worm segment has with
161 other objects. Returns a value between 0 and 1, where 1 is full
162 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 (.transform
175 (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-view
188 [(Vector3f. 4.207176, -3.7366982, 3.0816958)
189 (Quaternion. 0.11118768, 0.87678415, 0.24434438, -0.3989771)])
191 (def worm-side-view
192 [(Vector3f. 4.207176, -3.7366982, 3.0816958)
193 (Quaternion. -0.11555642, 0.88188726, -0.2854942, -0.3569518)])
195 (def degenerate-worm-view
196 [(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-view
202 :motor-control (:motor-control direct-control)
203 :keybindings (:keybindings direct-control)
204 :record nil
205 :experiences nil
206 :worm-model worm-model
207 :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-world
218 [& {:keys [record motor-control keybindings view experiences
219 worm-model end-frame] :as settings}]
220 (let [{:keys [record motor-control keybindings view experiences
221 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 (world
237 (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 record
244 (Capture/captureVideo
245 world
246 (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 experiences
256 (record-experience!
257 experiences {:touch touch-data
258 :proprioception proprioception-data
259 :muscle muscle-data})
260 ;;(if (curled? @experiences) (println "Curled"))
261 ;;(if (straight? @experiences) (println "Straight"))
262 ;; (println-repl
263 ;; (apply format "%.2f %.2f %.2f %.2f %.2f\n"
264 ;; (map contact touch-data)))
266 )
267 (muscle-display
268 muscle-data
269 (if record (dir! (File. record "muscle"))))
270 (prop-display
271 proprioception-data
272 (if record (dir! (File. record "proprio"))))
273 (touch-display
274 touch-data
275 (if record (dir! (File. record "touch")))))))))