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-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)))
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-program
28 "Create a function which will execute the motor script"
29 [muscle-positions
30 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 (dorun
37 (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 effectors
42 (map #(@current-forces % 0) muscle-positions)))))))
44 (defn worm-direct-control
45 "Create keybindings and a muscle control program that will enable
46 the user to control the worm via the keyboard."
47 [muscle-labels activation-strength]
48 (let [strengths (mapv (fn [_] (atom 0)) muscle-labels)
49 activator
50 (fn [n]
51 (fn [world pressed?]
52 (let [strength (if pressed? activation-strength 0)]
53 (swap! (nth strengths n) (constantly strength)))))
54 activators
55 (map activator (range (count muscle-labels)))
56 worm-keys
57 ["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-control
64 (fn [effectors]
65 (doall
66 (map (fn [strength effector]
67 (effector (deref strength)))
68 strengths effectors)))
69 :keybindings
70 ;; 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 radically
74 ;; different patterns -- a sinusoidal wiggling motion, and a curling
75 ;; motions that causes the worm to form a circle.
77 (def curl-script
78 [[370 :d-up 40]
79 [600 :d-up 0]])
81 (def period 18)
83 (def worm-muscle-labels
84 [:base-up :base-down
85 :a-down :a-up
86 :b-up :b-down
87 :c-down :c-up
88 :d-up :d-down])
90 (defn gen-wiggle [[flexor extensor :as muscle-pair] time-base]
91 (let [period period
92 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-script
99 (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 pick
104 ;; out the defining features of the different actions available to the
105 ;; worm. For this project, I am going to explicitely define functions
106 ;; that recognize curling and wiggling respectively. These functions
107 ;; are defined using all the information available from an embodied
108 ;; simulation of the action. Note how much easier they are to define
109 ;; than if I only had vision to work with. Things like scale/position
110 ;; invariance are complete non-issues here. This is the advantage of
111 ;; body-centered action recognition and what I hope to show with this
112 ;; thesis.
114 (defn last-nth
115 "Create function that will, when called each frame with the senses
116 of a creature, will record those results and return the last n
117 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 sensory
126 inputs of a worm, will determine whether the worm is wiggling."
127 [{:keys [touch proprioception muscles hearing]}]
128 (map (fn [f] (f)) proprioception
131 ))
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-view
143 [(Vector3f. 4.207176, -3.7366982, 3.0816958)
144 (Quaternion. 0.11118768, 0.87678415, 0.24434438, -0.3989771)])
146 (def worm-side-view
147 [(Vector3f. 4.207176, -3.7366982, 3.0816958)
148 (Quaternion. -0.11555642, 0.88188726, -0.2854942, -0.3569518)])
150 (def degenerate-worm-view
151 [(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-view
157 :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-world
167 [& {: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 (world
183 (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 record
191 (Capture/captureVideo
192 world
193 (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-display
200 strong!
201 (if record (dir! (File. record "muscle")))))
202 (prop-display
203 (prop)
204 (if record (dir! (File. record "proprio"))))
205 (touch-display
206 (map #(% (.getRootNode world)) touch)
207 (if record
208 (File. record "touch")))))))