view org/movement.org @ 292:e27a91b1f997

fixed termonology in movement.org
author Robert McIntyre <rlm@mit.edu>
date Thu, 16 Feb 2012 07:25:03 -0700
parents 23aadf376e9d
children 67a4e92d4628
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, terristerial 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 neuton. 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 stragety allows
26 for percise 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 untill 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 similiar 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 controlls 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 percise movements when exerting
46 less force. Or, the motor pool can simulate more exoitic recruitment
47 strageties 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 (defvar
67 ^{:arglists '([creature])}
68 muscles
69 (sense-nodes "muscles")
70 "Return the children of the creature's \"muscles\" node.")
72 (defn muscle-profile-image
73 "Get the muscle-profile image from the node's blender meta-data."
74 [#^Node muscle]
75 (if-let [image (meta-data muscle "muscle")]
76 (load-image image)))
78 (defn muscle-strength
79 "Return the strength of this muscle, or 1 if it is not defined."
80 [#^Node muscle]
81 (if-let [strength (meta-data muscle "strength")]
82 strength 1))
84 (defn motor-pool
85 "Return a vector where each entry is the strength of the \"motor
86 neuron\" at that part in the muscle."
87 [#^Node muscle]
88 (let [profile (muscle-profile-image muscle)]
89 (vec
90 (let [width (.getWidth profile)]
91 (for [x (range width)]
92 (- 255
93 (bit-and
94 0x0000FF
95 (.getRGB profile x 0))))))))
96 #+end_src
98 Of note here is =motor-pool= which interprets the muscle-profile
99 image in a way that allows me to use gradients between white and red,
100 instead of shades of gray as I've been using for all the other
101 senses. This is purely an aesthetic touch.
103 * Creating Muscles
104 #+name: muscle-kernel
105 #+begin_src clojure
106 (in-ns 'cortex.movement)
108 (defn movement-kernel
109 "Returns a function which when called with a integer value inside a
110 running simulation will cause movement in the creature according
111 to the muscle's position and strength profile. Each function
112 returns the amount of force applied / max force."
113 [#^Node creature #^Node muscle]
114 (let [target (closest-node creature muscle)
115 axis
116 (.mult (.getWorldRotation muscle) Vector3f/UNIT_Y)
117 strength (muscle-strength muscle)
119 pool (motor-pool muscle)
120 pool-integral (reductions + pool)
121 force-index
122 (vec (map #(float (* strength (/ % (last pool-integral))))
123 pool-integral))
124 control (.getControl target RigidBodyControl)]
125 (fn [n]
126 (let [pool-index (max 0 (min n (dec (count pool))))
127 force (force-index pool-index)]
128 (.applyTorque control (.mult axis force))
129 (float (/ force strength))))))
131 (defn movement!
132 "Endow the creature with the power of movement. Returns a sequence
133 of functions, each of which accept an integer value and will
134 activate their corresponding muscle."
135 [#^Node creature]
136 (for [muscle (muscles creature)]
137 (movement-kernel creature muscle)))
138 #+end_src
140 =movement-kernel= creates a function that will move the nearest
141 physical object to the muscle node. The muscle exerts a rotational
142 force dependant on it's orientation to the object in the blender
143 file. The function returned by =movement-kernel= is also a sense
144 function: it returns the percent of the total muscle strength that is
145 currently being employed. This is analogous to muscle tension in
146 humans and completes the sense of proprioception begun in the last
147 post.
149 * Visualizing Muscle Tension
150 Muscle exertion is a percent of a total, so the visulazation is just a
151 simple percent bar.
153 #+name: visualization
154 #+begin_src clojure
155 (defn movement-display-kernel
156 "Display muscle exertion data as a bar filling up with red."
157 [exertion]
158 (let [height 20
159 width 300
160 image (BufferedImage. width height
161 BufferedImage/TYPE_INT_RGB)
162 fill (min (int (* width exertion)) width)]
163 (dorun
164 (for [x (range fill)
165 y (range height)]
166 (.setRGB image x y 0xFF0000)))
167 image))
169 (defn view-movement
170 "Creates a function which accepts a list of muscle-exertion data and
171 displays each element of the list to the screen."
172 []
173 (view-sense movement-display-kernel))
174 #+end_src
176 * Adding Touch to the Worm
178 To the worm, I add two new nodes which describe a single muscle.
180 #+attr_html: width=755
181 #+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.
182 [[../images/worm-with-muscle.png]]
184 #+name: test-movement
185 #+begin_src clojure
186 (defn test-worm-movement
187 ([] (test-worm-movement false))
188 ([record?]
189 (let [creature (doto (worm) (body!))
191 muscle-exertion (atom 0)
192 muscles (movement! creature)
193 muscle-display (view-movement)]
194 (.setMass
195 (.getControl (.getChild creature "worm-11") RigidBodyControl)
196 (float 0))
197 (world
198 (nodify [creature (floor)])
199 (merge standard-debug-controls
200 {"key-h"
201 (fn [_ value]
202 (if value
203 (swap! muscle-exertion (partial + 20))))
204 "key-n"
205 (fn [_ value]
206 (if value
207 (swap! muscle-exertion (fn [v] (- v 20)))))})
208 (fn [world]
209 (if record?
210 (Capture/captureVideo
211 world
212 (File. "/home/r/proj/cortex/render/worm-muscles/main-view")))
213 (light-up-everything world)
214 (enable-debug world)
215 (.setTimer world (RatchetTimer. 60))
216 (set-gravity world (Vector3f. 0 0 0))
217 (.setLocation (.getCamera world)
218 (Vector3f. -4.912815, 2.004171, 0.15710819))
219 (.setRotation (.getCamera world)
220 (Quaternion. 0.13828252, 0.65516764,
221 -0.12370994, 0.7323449)))
222 (fn [world tpf]
223 (muscle-display
224 (map #(% @muscle-exertion) muscles)
225 (if record?
226 (File. "/home/r/proj/cortex/render/worm-muscles/muscles"))))))))
227 #+end_src
229 * Video Demonstration
231 #+begin_html
232 <div class="figure">
233 <center>
234 <video controls="controls" width="550">
235 <source src="../video/worm-muscles.ogg" type="video/ogg"
236 preload="none" poster="../images/aurellem-1280x480.png" />
237 </video>
238 </center>
239 <p>The worm is now able to move. The bar in the lower right displays
240 the power output of the muscle . Each jump causes 20 more motor neurons to
241 be recruited. Notice that the power output increases non-linearly
242 with motror neuron recruitement, similiar to a human muscle.</p>
243 </div>
244 #+end_html
246 ** Making the Worm Muscles Video
247 #+name: magick7
248 #+begin_src clojure
249 (ns cortex.video.magick7
250 (:import java.io.File)
251 (:use clojure.contrib.shell-out))
253 (defn images [path]
254 (sort (rest (file-seq (File. path)))))
256 (def base "/home/r/proj/cortex/render/worm-muscles/")
258 (defn pics [file]
259 (images (str base file)))
261 (defn combine-images []
262 (let [main-view (pics "main-view")
263 muscles (pics "muscles/0")
264 targets (map
265 #(File. (str base "out/" (format "%07d.png" %)))
266 (range 0 (count main-view)))]
267 (dorun
268 (pmap
269 (comp
270 (fn [[ main-view muscles target]]
271 (println target)
272 (sh "convert"
273 main-view
274 muscles "-geometry" "+320+440" "-composite"
275 target))
276 (fn [& args] (map #(.getCanonicalPath %) args)))
277 main-view muscles targets))))
278 #+end_src
280 #+begin_src sh :results silent
281 cd ~/proj/cortex/render/worm-muscles
282 ffmpeg -r 60 -i out/%07d.png -b:v 9000k -c:v libtheora worm-muscles.ogg
283 #+end_src
285 * Headers
286 #+name: muscle-header
287 #+begin_src clojure
288 (ns cortex.movement
289 "Give simulated creatures defined in special blender files the power
290 to move around in a simulated environment."
291 {:author "Robert McIntyre"}
292 (:use (cortex world util sense body))
293 (:use clojure.contrib.def)
294 (:import java.awt.image.BufferedImage)
295 (:import com.jme3.scene.Node)
296 (:import com.jme3.math.Vector3f)
297 (:import com.jme3.bullet.control.RigidBodyControl))
298 #+end_src
300 #+name: test-header
301 #+begin_src clojure
302 (ns cortex.test.movement
303 (:use (cortex world util sense body movement))
304 (:use cortex.test.body)
305 (:use clojure.contrib.def)
306 (:import java.io.File)
307 (:import java.awt.image.BufferedImage)
308 (:import com.jme3.scene.Node)
309 (:import (com.jme3.math Quaternion Vector3f))
310 (:import (com.aurellem.capture Capture RatchetTimer))
311 (:import com.jme3.bullet.control.RigidBodyControl))
312 #+end_src
314 * Source Listing
315 - [[../src/cortex/movement.clj][cortex.movement]]
316 - [[../src/cortex/test/movement.clj][cortex.test.movement]]
317 - [[../src/cortex/video/magick7.clj][cortex.video.magick7]]
318 #+html: <ul> <li> <a href="../org/movement.org">This org file</a> </li> </ul>
319 - [[http://hg.bortreb.com ][source-repository]]
321 * COMMENT code generation
322 #+begin_src clojure :tangle ../src/cortex/movement.clj
323 <<muscle-header>>
324 <<muscle-meta-data>>
325 <<muscle-kernel>>
326 <<visualization>>
327 #+end_src
329 #+begin_src clojure :tangle ../src/cortex/test/movement.clj
330 <<test-header>>
331 <<test-movement>>
332 #+end_src
334 #+begin_src clojure :tangle ../src/cortex/video/magick7.clj
335 <<magick7>>
336 #+end_src