view org/movement.org @ 336:70469ff8eb56

more Viewable stuff.
author Robert McIntyre <rlm@mit.edu>
date Fri, 20 Jul 2012 13:04:25 -0500
parents 702b5c78c2de
children 4f5a5d5f1613
line wrap: on
line source
1 #+title: Simulated Muscles
2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu
4 #+description: muscles for a simulated creature
5 #+keywords: simulation, jMonkeyEngine3, clojure
6 #+SETUPFILE: ../../aurellem/org/setup.org
7 #+INCLUDE: ../../aurellem/org/level-0.org
10 * Muscles
12 Surprisingly enough, terrestrial creatures only move by using torque
13 applied about their joints. There's not a single straight line of
14 force in the human body at all! (A straight line of force would
15 correspond to some sort of jet or rocket propulsion.)
17 In humans, muscles are composed of muscle fibers which can contract to
18 exert force. The muscle fibers which compose a muscle are partitioned
19 into discrete groups which are each controlled by a single alpha motor
20 neuron. A single alpha motor neuron might control as little as three
21 or as many as one thousand muscle fibers. When the alpha motor neuron
22 is engaged by the spinal cord, it activates all of the muscle fibers
23 to which it is attached. The spinal cord generally engages the alpha
24 motor neurons which control few muscle fibers before the motor neurons
25 which control many muscle fibers. This recruitment strategy allows
26 for precise movements at low strength. The collection of all motor
27 neurons that control a muscle is called the motor pool. The brain
28 essentially says "activate 30% of the motor pool" and the spinal cord
29 recruits motor neurons until 30% are activated. Since the
30 distribution of power among motor neurons is unequal and recruitment
31 goes from weakest to strongest, the first 30% of the motor pool might
32 be 5% of the strength of the muscle.
34 My simulated muscles follow a similar design: Each muscle is defined
35 by a 1-D array of numbers (the "motor pool"). Each entry in the array
36 represents a motor neuron which controls a number of muscle fibers
37 equal to the value of the entry. Each muscle has a scalar strength
38 factor which determines the total force the muscle can exert when all
39 motor neurons are activated. The effector function for a muscle takes
40 a number to index into the motor pool, and then "activates" all the
41 motor neurons whose index is lower or equal to the number. Each
42 motor-neuron will apply force in proportion to its value in the array.
43 Lower values cause less force. The lower values can be put at the
44 "beginning" of the 1-D array to simulate the layout of actual human
45 muscles, which are capable of more precise movements when exerting
46 less force. Or, the motor pool can simulate more exotic recruitment
47 strategies which do not correspond to human muscles.
49 This 1D array is defined in an image file for ease of
50 creation/visualization. Here is an example muscle profile image.
52 #+caption: A muscle profile image that describes the strengths of each motor neuron in a muscle. White is weakest and dark red is strongest. This particular pattern has weaker motor neurons at the beginning, just like human muscle.
53 [[../images/basic-muscle.png]]
55 * Blender Meta-data
57 In blender, each muscle is an empty node whose top level parent is
58 named "muscles", just like eyes, ears, and joints.
60 These functions define the expected meta-data for a muscle node.
62 #+name: muscle-meta-data
63 #+begin_src clojure
64 (in-ns 'cortex.movement)
66 (def
67 ^{:doc "Return the children of the creature's \"muscles\" node."
68 :arglists '([creature])}
69 muscles
70 (sense-nodes "muscles"))
73 (defn muscle-profile-image
74 "Get the muscle-profile image from the node's blender meta-data."
75 [#^Node muscle]
76 (if-let [image (meta-data muscle "muscle")]
77 (load-image image)))
79 (defn muscle-strength
80 "Return the strength of this muscle, or 1 if it is not defined."
81 [#^Node muscle]
82 (if-let [strength (meta-data muscle "strength")]
83 strength 1))
85 (defn motor-pool
86 "Return a vector where each entry is the strength of the \"motor
87 neuron\" at that part in the muscle."
88 [#^Node muscle]
89 (let [profile (muscle-profile-image muscle)]
90 (vec
91 (let [width (.getWidth profile)]
92 (for [x (range width)]
93 (- 255
94 (bit-and
95 0x0000FF
96 (.getRGB profile x 0))))))))
97 #+end_src
99 Of note here is =motor-pool= which interprets the muscle-profile
100 image in a way that allows me to use gradients between white and red,
101 instead of shades of gray as I've been using for all the other
102 senses. This is purely an aesthetic touch.
104 * Creating Muscles
105 #+name: muscle-kernel
106 #+begin_src clojure
107 (in-ns 'cortex.movement)
109 (defn movement-kernel
110 "Returns a function which when called with a integer value inside a
111 running simulation will cause movement in the creature according
112 to the muscle's position and strength profile. Each function
113 returns the amount of force applied / max force."
114 [#^Node creature #^Node muscle]
115 (let [target (closest-node creature muscle)
116 axis
117 (.mult (.getWorldRotation muscle) Vector3f/UNIT_Y)
118 strength (muscle-strength muscle)
120 pool (motor-pool muscle)
121 pool-integral (reductions + pool)
122 forces
123 (vec (map #(float (* strength (/ % (last pool-integral))))
124 pool-integral))
125 control (.getControl target RigidBodyControl)]
126 ;;(println-repl (.getName target) axis)
127 (fn [n]
128 (let [pool-index (max 0 (min n (dec (count pool))))
129 force (forces pool-index)]
130 (.applyTorque control (.mult axis force))
131 (float (/ force strength))))))
133 (defn movement!
134 "Endow the creature with the power of movement. Returns a sequence
135 of functions, each of which accept an integer value and will
136 activate their corresponding muscle."
137 [#^Node creature]
138 (for [muscle (muscles creature)]
139 (movement-kernel creature muscle)))
140 #+end_src
142 =movement-kernel= creates a function that will move the nearest
143 physical object to the muscle node. The muscle exerts a rotational
144 force dependent on it's orientation to the object in the blender
145 file. The function returned by =movement-kernel= is also a sense
146 function: it returns the percent of the total muscle strength that is
147 currently being employed. This is analogous to muscle tension in
148 humans and completes the sense of proprioception begun in the last
149 post.
151 * Visualizing Muscle Tension
152 Muscle exertion is a percent of a total, so the visualization is just a
153 simple percent bar.
155 #+name: visualization
156 #+begin_src clojure
157 (defn movement-display-kernel
158 "Display muscle exertion data as a bar filling up with red."
159 [exertion]
160 (let [height 20
161 width 300
162 image (BufferedImage. width height
163 BufferedImage/TYPE_INT_RGB)
164 fill (min (int (* width exertion)) width)]
165 (dorun
166 (for [x (range fill)
167 y (range height)]
168 (.setRGB image x y 0xFF0000)))
169 image))
171 (defn view-movement
172 "Creates a function which accepts a list of muscle-exertion data and
173 displays each element of the list to the screen."
174 []
175 (view-sense movement-display-kernel))
176 #+end_src
178 * Adding Touch to the Worm
180 To the worm, I add two new nodes which describe a single muscle.
182 #+attr_html: width=755
183 #+caption: The node highlighted in orange is the parent node of all muscles in the worm. The arrow highlighted in yellow represents the creature's single muscle, which moves the top segment. The other nodes which are not highlighted are joints, eyes, and ears.
184 [[../images/worm-with-muscle.png]]
186 #+name: test-movement
187 #+begin_src clojure
188 (defn test-worm-movement
189 "Testing movement:
190 You should see the worm suspended in mid air and a display on the
191 right which shows the current relative power being exerted by the
192 muscle. As you increase muscle strength, the bar should fill with
193 red, and the worm's upper segment should move.
195 Keys:
196 h : increase muscle exertion
197 n : decrease muscle exertion"
198 ([] (test-worm-movement false))
199 ([record?]
200 (let [creature (doto (worm) (body!))
202 muscle-exertion (atom 0)
203 muscles (movement! creature)
204 muscle-display (view-movement)]
205 (.setMass
206 (.getControl (.getChild creature "worm-11") RigidBodyControl)
207 (float 0))
208 (world
209 (nodify [creature (floor)])
210 (merge standard-debug-controls
211 {"key-h"
212 (fn [_ value]
213 (if value
214 (swap! muscle-exertion (partial + 20))))
215 "key-n"
216 (fn [_ value]
217 (if value
218 (swap! muscle-exertion (fn [v] (- v 20)))))})
219 (fn [world]
220 (if record?
221 (Capture/captureVideo
222 world
223 (File. "/home/r/proj/cortex/render/worm-muscles/main-view")))
224 (light-up-everything world)
225 (enable-debug world)
226 (.setTimer world (RatchetTimer. 60))
227 (set-gravity world (Vector3f. 0 0 0))
228 (.setLocation (.getCamera world)
229 (Vector3f. -4.912815, 2.004171, 0.15710819))
230 (.setRotation (.getCamera world)
231 (Quaternion. 0.13828252, 0.65516764,
232 -0.12370994, 0.7323449)))
233 (fn [world tpf]
234 (muscle-display
235 (map #(% @muscle-exertion) muscles)
236 (if record?
237 (File. "/home/r/proj/cortex/render/worm-muscles/muscles"))))))))
238 #+end_src
240 * Video Demonstration
242 #+begin_html
243 <div class="figure">
244 <center>
245 <video controls="controls" width="550">
246 <source src="../video/worm-muscles.ogg" type="video/ogg"
247 preload="none" poster="../images/aurellem-1280x480.png" />
248 </video>
249 <br> <a href="http://youtu.be/8Rp4jEGMDWU"> YouTube </a>
250 </center>
251 <p>The worm is now able to move. The bar in the lower right displays
252 the power output of the muscle . Each jump causes 20 more motor neurons to
253 be recruited. Notice that the power output increases non-linearly
254 with motor neuron recruitment, similar to a human muscle.</p>
255 </div>
256 #+end_html
258 ** Making the Worm Muscles Video
259 #+name: magick7
260 #+begin_src clojure
261 (ns cortex.video.magick7
262 (:import java.io.File)
263 (:use clojure.java.shell))
265 (defn images [path]
266 (sort (rest (file-seq (File. path)))))
268 (def base "/home/r/proj/cortex/render/worm-muscles/")
270 (defn pics [file]
271 (images (str base file)))
273 (defn combine-images []
274 (let [main-view (pics "main-view")
275 muscles (pics "muscles/0")
276 targets (map
277 #(File. (str base "out/" (format "%07d.png" %)))
278 (range 0 (count main-view)))]
279 (dorun
280 (pmap
281 (comp
282 (fn [[ main-view muscles target]]
283 (println target)
284 (sh "convert"
285 main-view
286 muscles "-geometry" "+320+440" "-composite"
287 target))
288 (fn [& args] (map #(.getCanonicalPath %) args)))
289 main-view muscles targets))))
290 #+end_src
292 #+begin_src sh :results silent
293 cd ~/proj/cortex/render/worm-muscles
294 ffmpeg -r 60 -i out/%07d.png -b:v 9000k -c:v libtheora worm-muscles.ogg
295 #+end_src
297 * Headers
298 #+name: muscle-header
299 #+begin_src clojure
300 (ns cortex.movement
301 "Give simulated creatures defined in special blender files the power
302 to move around in a simulated environment."
303 {:author "Robert McIntyre"}
304 (:use (cortex world util sense body))
305 (:import java.awt.image.BufferedImage)
306 (:import com.jme3.scene.Node)
307 (:import com.jme3.math.Vector3f)
308 (:import com.jme3.bullet.control.RigidBodyControl))
309 #+end_src
311 #+name: test-header
312 #+begin_src clojure
313 (ns cortex.test.movement
314 (:use (cortex world util sense body movement))
315 (:use cortex.test.body)
316 (:import java.io.File)
317 (:import java.awt.image.BufferedImage)
318 (:import com.jme3.scene.Node)
319 (:import (com.jme3.math Quaternion Vector3f))
320 (:import (com.aurellem.capture Capture RatchetTimer))
321 (:import com.jme3.bullet.control.RigidBodyControl))
322 #+end_src
324 * Source Listing
325 - [[../src/cortex/movement.clj][cortex.movement]]
326 - [[../src/cortex/test/movement.clj][cortex.test.movement]]
327 - [[../src/cortex/video/magick7.clj][cortex.video.magick7]]
328 #+html: <ul> <li> <a href="../org/movement.org">This org file</a> </li> </ul>
329 - [[http://hg.bortreb.com ][source-repository]]
331 * COMMENT code generation
332 #+begin_src clojure :tangle ../src/cortex/movement.clj
333 <<muscle-header>>
334 <<muscle-meta-data>>
335 <<muscle-kernel>>
336 <<visualization>>
337 #+end_src
339 #+begin_src clojure :tangle ../src/cortex/test/movement.clj
340 <<test-header>>
341 <<test-movement>>
342 #+end_src
344 #+begin_src clojure :tangle ../src/cortex/video/magick7.clj
345 <<magick7>>
346 #+end_src