Mercurial > cortex
view org/movement.org @ 305:19c43ec6958d
modified cover letter, wonston-letter, and began uploading files to youtube for backup
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 18 Feb 2012 10:28:14 -0700 |
parents | 1eed471e2ebf |
children | 7e7f8d6d9ec5 |
line wrap: on
line source
1 #+title: Simulated Muscles2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description: muscles for a simulated creature5 #+keywords: simulation, jMonkeyEngine3, clojure6 #+SETUPFILE: ../../aurellem/org/setup.org7 #+INCLUDE: ../../aurellem/org/level-0.org10 * Muscles12 Surprisingly enough, terristerial creatures only move by using torque13 applied about their joints. There's not a single straight line of14 force in the human body at all! (A straight line of force would15 correspond to some sort of jet or rocket propulsion.)17 In humans, muscles are composed of muscle fibers which can contract to18 exert force. The muscle fibers which compose a muscle are partitioned19 into discrete groups which are each controlled by a single alpha motor20 neuton. A single alpha motor neuron might control as little as three21 or as many as one thousand muscle fibers. When the alpha motor neuron22 is engaged by the spinal cord, it activates all of the muscle fibers23 to which it is attached. The spinal cord generally engages the alpha24 motor neurons which control few muscle fibers before the motor neurons25 which control many muscle fibers. This recruitment stragety allows26 for percise movements at low strength. The collection of all motor27 neurons that control a muscle is called the motor pool. The brain28 essentially says "activate 30% of the motor pool" and the spinal cord29 recruits motor neurons untill 30% are activated. Since the30 distribution of power among motor neurons is unequal and recruitment31 goes from weakest to strongest, the first 30% of the motor pool might32 be 5% of the strength of the muscle.34 My simulated muscles follow a similiar design: Each muscle is defined35 by a 1-D array of numbers (the "motor pool"). Each entry in the array36 represents a motor neuron which controlls a number of muscle fibers37 equal to the value of the entry. Each muscle has a scalar strength38 factor which determines the total force the muscle can exert when all39 motor neurons are activated. The effector function for a muscle takes40 a number to index into the motor pool, and then "activates" all the41 motor neurons whose index is lower or equal to the number. Each42 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 the44 "beginning" of the 1-D array to simulate the layout of actual human45 muscles, which are capable of more percise movements when exerting46 less force. Or, the motor pool can simulate more exoitic recruitment47 strageties which do not correspond to human muscles.49 This 1D array is defined in an image file for ease of50 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-data57 In blender, each muscle is an empty node whose top level parent is58 named "muscles", just like eyes, ears, and joints.60 These functions define the expected meta-data for a muscle node.62 #+name: muscle-meta-data63 #+begin_src clojure64 (in-ns 'cortex.movement)66 (defvar67 ^{:arglists '([creature])}68 muscles69 (sense-nodes "muscles")70 "Return the children of the creature's \"muscles\" node.")72 (defn muscle-profile-image73 "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-strength79 "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-pool85 "Return a vector where each entry is the strength of the \"motor86 neuron\" at that part in the muscle."87 [#^Node muscle]88 (let [profile (muscle-profile-image muscle)]89 (vec90 (let [width (.getWidth profile)]91 (for [x (range width)]92 (- 25593 (bit-and94 0x0000FF95 (.getRGB profile x 0))))))))96 #+end_src98 Of note here is =motor-pool= which interprets the muscle-profile99 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 other101 senses. This is purely an aesthetic touch.103 * Creating Muscles104 #+name: muscle-kernel105 #+begin_src clojure106 (in-ns 'cortex.movement)108 (defn movement-kernel109 "Returns a function which when called with a integer value inside a110 running simulation will cause movement in the creature according111 to the muscle's position and strength profile. Each function112 returns the amount of force applied / max force."113 [#^Node creature #^Node muscle]114 (let [target (closest-node creature muscle)115 axis116 (.mult (.getWorldRotation muscle) Vector3f/UNIT_Y)117 strength (muscle-strength muscle)119 pool (motor-pool muscle)120 pool-integral (reductions + pool)121 forces122 (vec (map #(float (* strength (/ % (last pool-integral))))123 pool-integral))124 control (.getControl target RigidBodyControl)]125 (println-repl (.getName target) axis)126 (fn [n]127 (let [pool-index (max 0 (min n (dec (count pool))))128 force (forces pool-index)]129 (.applyTorque control (.mult axis force))130 (float (/ force strength))))))132 (defn movement!133 "Endow the creature with the power of movement. Returns a sequence134 of functions, each of which accept an integer value and will135 activate their corresponding muscle."136 [#^Node creature]137 (for [muscle (muscles creature)]138 (movement-kernel creature muscle)))139 #+end_src141 =movement-kernel= creates a function that will move the nearest142 physical object to the muscle node. The muscle exerts a rotational143 force dependant on it's orientation to the object in the blender144 file. The function returned by =movement-kernel= is also a sense145 function: it returns the percent of the total muscle strength that is146 currently being employed. This is analogous to muscle tension in147 humans and completes the sense of proprioception begun in the last148 post.150 * Visualizing Muscle Tension151 Muscle exertion is a percent of a total, so the visulazation is just a152 simple percent bar.154 #+name: visualization155 #+begin_src clojure156 (defn movement-display-kernel157 "Display muscle exertion data as a bar filling up with red."158 [exertion]159 (let [height 20160 width 300161 image (BufferedImage. width height162 BufferedImage/TYPE_INT_RGB)163 fill (min (int (* width exertion)) width)]164 (dorun165 (for [x (range fill)166 y (range height)]167 (.setRGB image x y 0xFF0000)))168 image))170 (defn view-movement171 "Creates a function which accepts a list of muscle-exertion data and172 displays each element of the list to the screen."173 []174 (view-sense movement-display-kernel))175 #+end_src177 * Adding Touch to the Worm179 To the worm, I add two new nodes which describe a single muscle.181 #+attr_html: width=755182 #+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.183 [[../images/worm-with-muscle.png]]185 #+name: test-movement186 #+begin_src clojure187 (defn test-worm-movement188 ([] (test-worm-movement false))189 ([record?]190 (let [creature (doto (worm) (body!))192 muscle-exertion (atom 0)193 muscles (movement! creature)194 muscle-display (view-movement)]195 (.setMass196 (.getControl (.getChild creature "worm-11") RigidBodyControl)197 (float 0))198 (world199 (nodify [creature (floor)])200 (merge standard-debug-controls201 {"key-h"202 (fn [_ value]203 (if value204 (swap! muscle-exertion (partial + 20))))205 "key-n"206 (fn [_ value]207 (if value208 (swap! muscle-exertion (fn [v] (- v 20)))))})209 (fn [world]210 (if record?211 (Capture/captureVideo212 world213 (File. "/home/r/proj/cortex/render/worm-muscles/main-view")))214 (light-up-everything world)215 (enable-debug world)216 (.setTimer world (RatchetTimer. 60))217 (set-gravity world (Vector3f. 0 0 0))218 (.setLocation (.getCamera world)219 (Vector3f. -4.912815, 2.004171, 0.15710819))220 (.setRotation (.getCamera world)221 (Quaternion. 0.13828252, 0.65516764,222 -0.12370994, 0.7323449)))223 (fn [world tpf]224 (muscle-display225 (map #(% @muscle-exertion) muscles)226 (if record?227 (File. "/home/r/proj/cortex/render/worm-muscles/muscles"))))))))228 #+end_src230 * Video Demonstration232 #+begin_html233 <div class="figure">234 <center>235 <video controls="controls" width="550">236 <source src="../video/worm-muscles.ogg" type="video/ogg"237 preload="none" poster="../images/aurellem-1280x480.png" />238 </video>239 </center>240 <p>The worm is now able to move. The bar in the lower right displays241 the power output of the muscle . Each jump causes 20 more motor neurons to242 be recruited. Notice that the power output increases non-linearly243 with motror neuron recruitement, similiar to a human muscle.</p>244 </div>245 #+end_html247 ** Making the Worm Muscles Video248 #+name: magick7249 #+begin_src clojure250 (ns cortex.video.magick7251 (:import java.io.File)252 (:use clojure.contrib.shell-out))254 (defn images [path]255 (sort (rest (file-seq (File. path)))))257 (def base "/home/r/proj/cortex/render/worm-muscles/")259 (defn pics [file]260 (images (str base file)))262 (defn combine-images []263 (let [main-view (pics "main-view")264 muscles (pics "muscles/0")265 targets (map266 #(File. (str base "out/" (format "%07d.png" %)))267 (range 0 (count main-view)))]268 (dorun269 (pmap270 (comp271 (fn [[ main-view muscles target]]272 (println target)273 (sh "convert"274 main-view275 muscles "-geometry" "+320+440" "-composite"276 target))277 (fn [& args] (map #(.getCanonicalPath %) args)))278 main-view muscles targets))))279 #+end_src281 #+begin_src sh :results silent282 cd ~/proj/cortex/render/worm-muscles283 ffmpeg -r 60 -i out/%07d.png -b:v 9000k -c:v libtheora worm-muscles.ogg284 #+end_src286 * Headers287 #+name: muscle-header288 #+begin_src clojure289 (ns cortex.movement290 "Give simulated creatures defined in special blender files the power291 to move around in a simulated environment."292 {:author "Robert McIntyre"}293 (:use (cortex world util sense body))294 (:use clojure.contrib.def)295 (:import java.awt.image.BufferedImage)296 (:import com.jme3.scene.Node)297 (:import com.jme3.math.Vector3f)298 (:import com.jme3.bullet.control.RigidBodyControl))299 #+end_src301 #+name: test-header302 #+begin_src clojure303 (ns cortex.test.movement304 (:use (cortex world util sense body movement))305 (:use cortex.test.body)306 (:use clojure.contrib.def)307 (:import java.io.File)308 (:import java.awt.image.BufferedImage)309 (:import com.jme3.scene.Node)310 (:import (com.jme3.math Quaternion Vector3f))311 (:import (com.aurellem.capture Capture RatchetTimer))312 (:import com.jme3.bullet.control.RigidBodyControl))313 #+end_src315 * Source Listing316 - [[../src/cortex/movement.clj][cortex.movement]]317 - [[../src/cortex/test/movement.clj][cortex.test.movement]]318 - [[../src/cortex/video/magick7.clj][cortex.video.magick7]]319 #+html: <ul> <li> <a href="../org/movement.org">This org file</a> </li> </ul>320 - [[http://hg.bortreb.com ][source-repository]]322 * COMMENT code generation323 #+begin_src clojure :tangle ../src/cortex/movement.clj324 <<muscle-header>>325 <<muscle-meta-data>>326 <<muscle-kernel>>327 <<visualization>>328 #+end_src330 #+begin_src clojure :tangle ../src/cortex/test/movement.clj331 <<test-header>>332 <<test-movement>>333 #+end_src335 #+begin_src clojure :tangle ../src/cortex/video/magick7.clj336 <<magick7>>337 #+end_src