comparison 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
comparison
equal deleted inserted replaced
406:40b67bb71430 407:bd6d03596ea8
141 (subvec v (- c n) c)))) 141 (subvec v (- c n) c))))
142 142
143 (defn touch-average [[coords touch]] 143 (defn touch-average [[coords touch]]
144 (/ (average (map first touch)) (average (map second touch)))) 144 (/ (average (map first touch)) (average (map second touch))))
145 145
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]])
154
155
156
146 (defn floor-contact [[coords contact :as touch]] 157 (defn floor-contact [[coords contact :as touch]]
147 (let [raw-average 158 (let [raw-average
148 (average 159 (average
149 (map 160 (map
150 first 161 first
151 (vals 162 (vals
152 (select-keys 163 (select-keys
153 (zipmap coords contact) 164 (zipmap coords contact)
154 [[8 15] [8 16] [8 17] [8 18] [8 19] [8 20] [8 21] [8 22] [9 15] 165 ))))]
155 [9 16] [9 17] [9 18] [9 19] [9 20] [9 21] [9 22] [10 15] [10 16]
156 [10 17] [10 18] [10 19] [10 20] [10 21] [10 22] [11 15] [11 16]
157 [11 17] [11 18] [11 19] [11 20] [11 21] [11 22] [12 15] [12 16]
158 [12 17] [12 18] [12 19] [12 20] [12 21] [12 22] [13 15] [13 16]
159 [13 17] [13 18] [13 19] [13 20] [13 21] [13 22] [14 15] [14 16]
160 [14 17] [14 18] [14 19] [14 20] [14 21] [14 22]]))))]
161 (Math/abs (- 1. (* 10 raw-average))))) 166 (Math/abs (- 1. (* 10 raw-average)))))
162 167
163 168
164 (defn wiggling? 169 (defn wiggling?
165 "Is the worm wiggling?" 170 "Is the worm wiggling?"
184 (let [direct-control (worm-direct-control worm-muscle-labels 40)] 189 (let [direct-control (worm-direct-control worm-muscle-labels 40)]
185 {:view worm-side-view 190 {:view worm-side-view
186 :motor-control (:motor-control direct-control) 191 :motor-control (:motor-control direct-control)
187 :keybindings (:keybindings direct-control) 192 :keybindings (:keybindings direct-control)
188 :record nil 193 :record nil
189 :experiences nil})) 194 :experiences nil
195 :worm-model worm-model
196 :end-frame nil}))
197
190 198
191 (defn dir! [file] 199 (defn dir! [file]
192 (if (not (.exists file)) 200 (if (not (.exists file))
193 (.mkdir file)) 201 (.mkdir file))
194 file) 202 file)
195 203
196 (defn record-experience! [experiences data] 204 (defn record-experience! [experiences data]
197 (swap! experiences #(conj % data))) 205 (swap! experiences #(conj % data)))
198 206
199 (defn worm-world 207 (defn worm-world
200 [& {:keys [record motor-control keybindings view experiences] :as settings}] 208 [& {:keys [record motor-control keybindings view experiences
201 (let [{:keys [record motor-control keybindings view]} 209 worm-model end-frame] :as settings}]
210 (let [{:keys [record motor-control keybindings view experiences
211 worm-model end-frame]}
202 (merge (worm-world-defaults) settings) 212 (merge (worm-world-defaults) settings)
203 worm (doto (worm-model) (body!)) 213 worm (doto (worm-model) (body!))
204 touch (touch! worm) 214 touch (touch! worm)
205 prop (proprioception! worm) 215 prop (proprioception! worm)
206 muscles (movement! worm) 216 muscles (movement! worm)
208 touch-display (view-touch) 218 touch-display (view-touch)
209 prop-display (view-proprioception) 219 prop-display (view-proprioception)
210 muscle-display (view-movement) 220 muscle-display (view-movement)
211 221
212 floor (box 10 1 10 :position (Vector3f. 0 -10 0) 222 floor (box 10 1 10 :position (Vector3f. 0 -10 0)
213 :color ColorRGBA/Gray :mass 0)] 223 :color ColorRGBA/Gray :mass 0)
224 timer (IsoTimer. 60)]
214 225
215 (world 226 (world
216 (nodify [worm floor]) 227 (nodify [worm floor])
217 (merge standard-debug-controls keybindings) 228 (merge standard-debug-controls keybindings)
218 (fn [world] 229 (fn [world]
219 (position-camera world view) 230 (position-camera world view)
220 (let [timer (IsoTimer. 60)] 231 (.setTimer world timer)
221 (.setTimer world timer) 232 (display-dilated-time world timer)
222 (display-dilated-time world timer))
223 (if record 233 (if record
224 (Capture/captureVideo 234 (Capture/captureVideo
225 world 235 world
226 (dir! (File. record "main-view")))) 236 (dir! (File. record "main-view"))))
227 (speed-up world) 237 (speed-up world)
228 (light-up-everything world)) 238 (light-up-everything world))
229 (fn [world tpf] 239 (fn [world tpf]
240 (if (> (.getTime timer) end-frame)
241 (.stop world))
230 (let [muscle-data (motor-control muscles) 242 (let [muscle-data (motor-control muscles)
231 proprioception-data (prop) 243 proprioception-data (prop)
232 touch-data (map #(% (.getRootNode world)) touch)] 244 touch-data (map #(% (.getRootNode world)) touch)]
233 (when experiences 245 (when experiences
234 (record-experience! 246 (record-experience!
235 experiences {:touch touch-data 247 experiences {:touch touch-data
236 :proprioception proprioception-data 248 :proprioception proprioception-data
237 :muscle muscle-data}) 249 :muscle muscle-data})
238 (if (curled? @experiences) (println "Curled")) 250 (if (curled? @experiences) (println "Curled"))
239 ;;(if (straight? @experiences) (println "Straight")) 251 ;;(if (straight? @experiences) (println "Straight"))
240 (println-repl 252 ;; (println-repl
241 (apply format "%.2f %.2f %.2f %.2f %.2f\n" 253 ;; (apply format "%.2f %.2f %.2f %.2f %.2f\n"
242 (map floor-contact touch-data))) 254 ;; (map floor-contact touch-data)))
243 255
244 ) 256 )
245 (muscle-display 257 (muscle-display
246 muscle-data 258 muscle-data
247 (if record (dir! (File. record "muscle")))) 259 (if record (dir! (File. record "muscle"))))
248 (prop-display 260 (prop-display
249 proprioception-data 261 proprioception-data
250 (if record (dir! (File. record "proprio")))) 262 (if record (dir! (File. record "proprio"))))
251 (touch-display 263 (touch-display
252 touch-data 264 touch-data
253 (if record (dir! (File. record "touch"))))))))) 265 (if record (dir! (File. record "touch")))))))))
266
267
268 ;; A demonstration of self organiging touch maps through experience.
269
270 (def single-worm-segment-view
271 [(Vector3f. 2.0681207, -6.1406755, 1.6106138)
272 (Quaternion. -0.15558705, 0.843615, -0.3428654, -0.38281822)])
273
274 (def worm-single-segment-muscle-labels
275 [:lift-1 :lift-2 :roll-1 :roll-2])
276
277 (defn touch-kinesthetics []
278 [[170 :lift-1 40]
279 [190 :lift-1 20]
280 [206 :lift-1 0]
281
282 [400 :lift-2 40]
283 [410 :lift-2 0]
284
285 [570 :lift-2 40]
286 [590 :lift-2 20]
287 [606 :lift-2 0]
288
289 [800 :lift-1 40]
290 [809 :lift-1 0]
291
292 [900 :roll-2 40]
293 [905 :roll-2 20]
294 [910 :roll-2 0]
295
296 [1000 :roll-2 40]
297 [1005 :roll-2 20]
298 [1010 :roll-2 0]
299
300 [1100 :roll-2 40]
301 [1105 :roll-2 20]
302 [1110 :roll-2 0]
303 ])
304
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))})))
314
315 (defn single-worm-segment []
316 (load-blender-model "Models/worm/worm-single-segment.blend"))
317
318
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)}))