view org/worm_learn.clj @ 403:92acbe7e5c91

begin final coding stretch.
author Robert McIntyre <rlm@mit.edu>
date Mon, 17 Mar 2014 14:01:02 -0400
parents 6ba908c1a0a9
children 939bcc5950b2
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)))))))
47 ;; These are scripts that direct the worm to move in two radically
48 ;; different patterns -- a sinusoidal wiggling motion, and a curling
49 ;; motions that causes the worm to form a circle.
51 (def curl-script
52 [[370 :d-up 40]
53 [600 :d-up 0]])
55 (def period 18)
57 (def muscle-labels
58 [:base-up :base-down
59 :a-up :a-down
60 :b-up :b-down
61 :c-up :c-down
62 :d-up :d-down])
64 (defn gen-wiggle [[flexor extensor :as muscle-pair] time-base]
65 (let [period period
66 power 45]
67 [[time-base flexor power]
68 [(+ time-base period) flexor 0]
69 [(+ time-base period 1) extensor power]
70 [(+ time-base (+ (* 2 period) 2)) extensor 0]]))
72 (def wiggle-script
73 (mapcat gen-wiggle (repeat 40 [:a-down :a-up])
74 (range 100 10000 (+ 3 (* period 2)))))
77 ;; Normally, we'd use unsupervised/supervised machine learning to pick
78 ;; out the defining features of the different actions available to the
79 ;; worm. For this project, I am going to explicitely define functions
80 ;; that recognize curling and wiggling respectively. These functions
81 ;; are defined using all the information available from an embodied
82 ;; simulation of the action. Note how much easier they are to define
83 ;; than if I only had vision to work with. Things like scale/position
84 ;; invariance are complete non-issues here. This is the advantage of
85 ;; body-centered action recognition and what I hope to show with this
86 ;; thesis.
88 (defn last-nth
89 "Create function that will, when called each frame with the senses
90 of a creature, will record those results and return the last n
91 results."
92 [n]
93 (let [last-n '()]
94 (fn [frame-num {:keys [touch proprioception muscles hearing]}]
95 (take n (cons [frame-num :stuff] last-n)))))
98 (defn wiggling?
99 "Generate a function which, when called each frame with the sensory
100 inputs of a worm, will determine whether the worm is wiggling."
101 [{:keys [touch proprioception muscles hearing]}]
102 (map (fn [f] (f)) proprioception
105 ))
107 (defn curling?
108 "Is the worm curled up?"
109 []
110 )
112 (defn resting?
113 "Is the worm on the ground in a neutral position?"
114 [])
116 (def standard-world-view
117 [(Vector3f. 4.207176, -3.7366982, 3.0816958)
118 (Quaternion. 0.11118768, 0.87678415, 0.24434438, -0.3989771)])
120 (def worm-side-view
121 [(Vector3f. 4.207176, -3.7366982, 3.0816958)
122 (Quaternion. -0.11555642, 0.88188726, -0.2854942, -0.3569518)])
124 (def degenerate-worm-view
125 [(Vector3f. -0.0708936, -8.570261, 2.6487997)
126 (Quaternion. -2.318909E-4, 0.9985348, 0.053941682, 0.004291452)])
128 (defn worm-world
129 ""
130 ([] (worm-world curl-script))
131 ([motion-script]
132 (let [record? false ;;true
133 worm (doto (worm-model) (body!))
134 touch (touch! worm)
135 prop (proprioception! worm)
136 muscles (movement! worm)
138 touch-display (view-touch)
139 prop-display (view-proprioception)
140 muscle-display (view-movement)
142 floor (box 10 1 10 :position (Vector3f. 0 -10 0)
143 :color ColorRGBA/Gray :mass 0)
145 control-script (motor-control-program
146 muscle-labels motion-script)]
147 (world
148 (nodify [worm floor])
149 standard-debug-controls
151 (fn [world]
152 ;; (set-gravity world Vector3f/ZERO)
153 (position-camera world degenerate-worm-view)
154 (let [timer (IsoTimer. 60)]
155 (.setTimer world timer)
156 (display-dilated-time world timer))
157 (if record?
158 (Capture/captureVideo
159 world
160 (File. output-base "main-view")))
161 (speed-up world)
162 (light-up-everything world))
163 (fn [world tpf]
164 (muscle-display
165 (control-script muscles)
166 (if record? (File. output-base "muscle")))
167 (prop-display
168 (prop)
169 (if record? (File. output-base "proprio")))
170 (touch-display
171 (map #(% (.getRootNode world)) touch)
172 (if record?
173 (File. output-base "touch"))))))))