view org/worm_learn.clj @ 407:bd6d03596ea8

add worm segment to demonstrate self-organizing touch maps.
author Robert McIntyre <rlm@mit.edu>
date Tue, 18 Mar 2014 19:53:42 -0400
parents 40b67bb71430
children 3b4012b42611
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 (: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)))
13 (use 'clojure.pprint)
15 (dorun (cortex.import/mega-import-jme3))
16 (rlm.rlm-commands/help)
18 (load-bullet)
20 (def hand "Models/test-creature/hand.blend")
22 (defn worm-model []
23 (load-blender-model "Models/worm/worm.blend"))
25 (def output-base (File. "/home/r/proj/cortex/render/worm-learn/curl"))
28 (defn motor-control-program
29 "Create a function which will execute the motor script"
30 [muscle-labels
31 script]
32 (let [current-frame (atom -1)
33 keyed-script (group-by first script)
34 current-forces (atom {}) ]
35 (fn [effectors]
36 (let [indexed-effectors (vec effectors)]
37 (dorun
38 (for [[_ part force] (keyed-script (swap! current-frame inc))]
39 (swap! current-forces (fn [m] (assoc m part force)))))
40 (doall (map (fn [effector power]
41 (effector (int power)))
42 effectors
43 (map #(@current-forces % 0) muscle-labels)))))))
45 (defn worm-direct-control
46 "Create keybindings and a muscle control program that will enable
47 the user to control the worm via the keyboard."
48 [muscle-labels activation-strength]
49 (let [strengths (mapv (fn [_] (atom 0)) muscle-labels)
50 activator
51 (fn [n]
52 (fn [world pressed?]
53 (let [strength (if pressed? activation-strength 0)]
54 (swap! (nth strengths n) (constantly strength)))))
55 activators
56 (map activator (range (count muscle-labels)))
57 worm-keys
58 ["key-f" "key-r"
59 "key-g" "key-t"
60 "key-y" "key-h"
61 "key-j" "key-u"
62 "key-i" "key-k"
63 "key-o" "key-l"]]
64 {:motor-control
65 (fn [effectors]
66 (doall
67 (map (fn [strength effector]
68 (effector (deref strength)))
69 strengths effectors)))
70 :keybindings
71 ;; assume muscles are listed in pairs and map them to keys.
72 (zipmap worm-keys activators)}))
74 ;; These are scripts that direct the worm to move in two radically
75 ;; different patterns -- a sinusoidal wiggling motion, and a curling
76 ;; motions that causes the worm to form a circle.
78 (def curl-script
79 [[370 :d-up 40]
80 [600 :d-up 0]])
82 (def period 18)
84 (def worm-muscle-labels
85 [:base-up :base-down
86 :a-down :a-up
87 :b-up :b-down
88 :c-down :c-up
89 :d-up :d-down])
91 (defn gen-wiggle [[flexor extensor :as muscle-pair] time-base]
92 (let [period period
93 power 45]
94 [[time-base flexor power]
95 [(+ time-base period) flexor 0]
96 [(+ time-base period 1) extensor power]
97 [(+ time-base (+ (* 2 period) 2)) extensor 0]]))
99 (def wiggle-script
100 (mapcat gen-wiggle (repeat 4000 [:a-down :a-up])
101 (range 100 1000000 (+ 3 (* period 2)))))
104 ;; Normally, we'd use unsupervised/supervised machine learning to pick
105 ;; out the defining features of the different actions available to the
106 ;; worm. For this project, I am going to explicitely define functions
107 ;; that recognize curling and wiggling respectively. These functions
108 ;; are defined using all the information available from an embodied
109 ;; simulation of the action. Note how much easier they are to define
110 ;; than if I only had vision to work with. Things like scale/position
111 ;; invariance are complete non-issues here. This is the advantage of
112 ;; body-centered action recognition and what I hope to show with this
113 ;; thesis.
116 (defn straight?
117 "Is the worm straight?"
118 [experiences]
119 (every?
120 (fn [[_ _ bend]]
121 (< (Math/sin bend) 0.05))
122 (:proprioception (peek experiences))))
124 (defn curled?
125 "Is the worm curled up?"
126 [experiences]
127 (every?
128 (fn [[_ _ bend]]
129 (> (Math/sin bend) 0.64))
130 (:proprioception (peek experiences))))
132 (defn grand-circle?
133 "Does the worm form a majestic circle (one end touching the other)?"
134 [experiences]
135 (and (curled? experiences)
136 true)) ;; TODO: add code here.
138 (defn vector:last-n [v n]
139 (let [c (count v)]
140 (if (< c n) v
141 (subvec v (- c n) c))))
143 (defn touch-average [[coords touch]]
144 (/ (average (map first touch)) (average (map second touch))))
146 (def worm-segment-touch-bottom
147 [[8 15] [8 16] [8 17] [8 18] [8 19] [8 20] [8 21] [8 22] [9 15]
148 [9 16] [9 17] [9 18] [9 19] [9 20] [9 21] [9 22] [10 15] [10 16]
149 [10 17] [10 18] [10 19] [10 20] [10 21] [10 22] [11 15] [11 16]
150 [11 17] [11 18] [11 19] [11 20] [11 21] [11 22] [12 15] [12 16]
151 [12 17] [12 18] [12 19] [12 20] [12 21] [12 22] [13 15] [13 16]
152 [13 17] [13 18] [13 19] [13 20] [13 21] [13 22] [14 15] [14 16]
153 [14 17] [14 18] [14 19] [14 20] [14 21] [14 22]])
157 (defn floor-contact [[coords contact :as touch]]
158 (let [raw-average
159 (average
160 (map
161 first
162 (vals
163 (select-keys
164 (zipmap coords contact)
165 ))))]
166 (Math/abs (- 1. (* 10 raw-average)))))
169 (defn wiggling?
170 "Is the worm wiggling?"
171 [experiences]
172 (vector:last-n experiences 200)
174 )
176 (def standard-world-view
177 [(Vector3f. 4.207176, -3.7366982, 3.0816958)
178 (Quaternion. 0.11118768, 0.87678415, 0.24434438, -0.3989771)])
180 (def worm-side-view
181 [(Vector3f. 4.207176, -3.7366982, 3.0816958)
182 (Quaternion. -0.11555642, 0.88188726, -0.2854942, -0.3569518)])
184 (def degenerate-worm-view
185 [(Vector3f. -0.0708936, -8.570261, 2.6487997)
186 (Quaternion. -2.318909E-4, 0.9985348, 0.053941682, 0.004291452)])
188 (defn worm-world-defaults []
189 (let [direct-control (worm-direct-control worm-muscle-labels 40)]
190 {:view worm-side-view
191 :motor-control (:motor-control direct-control)
192 :keybindings (:keybindings direct-control)
193 :record nil
194 :experiences nil
195 :worm-model worm-model
196 :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-world
208 [& {:keys [record motor-control keybindings view experiences
209 worm-model end-frame] :as settings}]
210 (let [{:keys [record motor-control keybindings view experiences
211 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 (world
227 (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 record
234 (Capture/captureVideo
235 world
236 (dir! (File. record "main-view"))))
237 (speed-up world)
238 (light-up-everything world))
239 (fn [world tpf]
240 (if (> (.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 experiences
246 (record-experience!
247 experiences {:touch touch-data
248 :proprioception proprioception-data
249 :muscle muscle-data})
250 (if (curled? @experiences) (println "Curled"))
251 ;;(if (straight? @experiences) (println "Straight"))
252 ;; (println-repl
253 ;; (apply format "%.2f %.2f %.2f %.2f %.2f\n"
254 ;; (map floor-contact touch-data)))
256 )
257 (muscle-display
258 muscle-data
259 (if record (dir! (File. record "muscle"))))
260 (prop-display
261 proprioception-data
262 (if record (dir! (File. record "proprio"))))
263 (touch-display
264 touch-data
265 (if record (dir! (File. record "touch")))))))))
268 ;; A demonstration of self organiging touch maps through experience.
270 (def single-worm-segment-view
271 [(Vector3f. 2.0681207, -6.1406755, 1.6106138)
272 (Quaternion. -0.15558705, 0.843615, -0.3428654, -0.38281822)])
274 (def worm-single-segment-muscle-labels
275 [:lift-1 :lift-2 :roll-1 :roll-2])
277 (defn touch-kinesthetics []
278 [[170 :lift-1 40]
279 [190 :lift-1 20]
280 [206 :lift-1 0]
282 [400 :lift-2 40]
283 [410 :lift-2 0]
285 [570 :lift-2 40]
286 [590 :lift-2 20]
287 [606 :lift-2 0]
289 [800 :lift-1 40]
290 [809 :lift-1 0]
292 [900 :roll-2 40]
293 [905 :roll-2 20]
294 [910 :roll-2 0]
296 [1000 :roll-2 40]
297 [1005 :roll-2 20]
298 [1010 :roll-2 0]
300 [1100 :roll-2 40]
301 [1105 :roll-2 20]
302 [1110 :roll-2 0]
303 ])
305 (defn worm-segment-defaults []
306 (let [direct-control (worm-direct-control worm-muscle-labels 40)]
307 (merge (worm-world-defaults)
308 {:worm-model single-worm-segment
309 :view single-worm-segment-view
310 :motor-control
311 (motor-control-program
312 worm-single-segment-muscle-labels
313 (touch-kinesthetics))})))
315 (defn single-worm-segment []
316 (load-blender-model "Models/worm/worm-single-segment.blend"))
319 (defn pure-touch?
320 "This is worm specific code to determine if a large region of touch
321 sensors is either all on or all off."
322 [[coords touch :as touch-data]]
323 (= (set (map first touch)) #{(float 0.1) (float 0.0)}))