annotate org/movement.org @ 260:959127e21f81

fleshing out text in muscle.org
author Robert McIntyre <rlm@mit.edu>
date Tue, 14 Feb 2012 03:16:50 -0700
parents 66fbab414d45
children 2fdcbe8185b1
rev   line source
rlm@260 1 #+title: Simulated Muscles
rlm@158 2 #+author: Robert McIntyre
rlm@158 3 #+email: rlm@mit.edu
rlm@158 4 #+description: muscles for a simulated creature
rlm@158 5 #+keywords: simulation, jMonkeyEngine3, clojure
rlm@158 6 #+SETUPFILE: ../../aurellem/org/setup.org
rlm@158 7 #+INCLUDE: ../../aurellem/org/level-0.org
rlm@158 8
rlm@180 9
rlm@260 10 * Muscles
rlm@260 11
rlm@180 12 Surprisingly enough, terristerial creatures only move by using torque
rlm@180 13 applied about their joints. There's not a single straight line of
rlm@180 14 force in the human body at all! (A straight line of force would
rlm@260 15 correspond to some sort of jet or rocket propulsion.)
rlm@180 16
rlm@260 17 *(next paragraph is from memory and needs to be checked!)*
rlm@180 18
rlm@260 19 In humans, muscles are composed of millions of sarcomeres, which can
rlm@260 20 contract to exert force. A single motor neuron might control 100-1,000
rlm@260 21 sarcomeres. When the motor neuron is engaged by the brain, it
rlm@260 22 activates all of the sarcomeres to which it is attached. Some motor
rlm@260 23 neurons command many sarcomeres, and some command only a few. The
rlm@260 24 spinal cord generally engages the motor neurons which control few
rlm@260 25 sarcomeres before the motor neurons which control many sarcomeres.
rlm@260 26 This recruitment stragety allows for percise movements at low
rlm@260 27 strength. The collection of all motor neurons that control a muscle is
rlm@260 28 called the motor pool. The brain essentially says "activate 30% of the
rlm@260 29 motor pool" and the spinal cord recruits motor neurons untill 30% are
rlm@260 30 activated. Since the distribution of power among motor neurons is
rlm@260 31 unequal and recruitment goes from weakest to strongest, 30% of the
rlm@260 32 motor pool might be 5% of the strength of the muscle.
rlm@260 33
rlm@260 34 My simulated muscles follow a similiar design: Each muscle is defined
rlm@260 35 by a 1-D array of numbers (the "motor pool"). Each number represents a
rlm@260 36 motor neuron which controlls a number of sarcomeres equal to the
rlm@260 37 number. A muscle also has a scalar :strength factor which determines
rlm@260 38 the total force the muscle can exert when all motor neurons are
rlm@260 39 activated. The effector function for a muscle takes a number to index
rlm@260 40 into the motor pool, and that number "activates" all the motor neurons
rlm@260 41 whose index is lower or equal to the number. Each motor-neuron will
rlm@260 42 apply force in proportion to its value in the array. Lower values
rlm@260 43 cause less force. The lower values can be put at the "beginning" of
rlm@260 44 the 1-D array to simulate the layout of actual human muscles, which
rlm@260 45 are capable of more percise movements when exerting less force. Or,
rlm@260 46 the motor pool can simulate more exoitic recruitment strageties which
rlm@260 47 do not correspond to human muscles.
rlm@260 48
rlm@260 49 This 1D array is defined in an image file for ease of
rlm@260 50 creation/visualization. Here is an example muscle profile image.
rlm@260 51
rlm@260 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.
rlm@260 53 [[../images/basic-muscle.png]]
rlm@260 54
rlm@260 55 * Blender Meta-data
rlm@260 56
rlm@260 57 In blender, each muscle is an empty node whose top level parent is
rlm@260 58 named "muscles", just like eyes, ears, and joints.
rlm@260 59
rlm@260 60 These functions define the expected meta-data for a muscle node.
rlm@180 61
rlm@158 62 #+name: movement
rlm@158 63 #+begin_src clojure
rlm@260 64 (in-ns 'cortex.movement)
rlm@158 65
rlm@180 66 (defvar
rlm@180 67 ^{:arglists '([creature])}
rlm@180 68 muscles
rlm@180 69 (sense-nodes "muscles")
rlm@180 70 "Return the children of the creature's \"muscles\" node.")
rlm@158 71
rlm@260 72 (defn muscle-profile-image
rlm@260 73 "Get the muscle-profile image from the node's blender meta-data."
rlm@260 74 [#^Node muscle]
rlm@260 75 (if-let [image (meta-data muscle "muscle")]
rlm@260 76 (load-image image)))
rlm@260 77
rlm@260 78 (defn muscle-strength
rlm@260 79 "Return the strength of this muscle, or 1 if it is not defined."
rlm@260 80 [#^Node muscle]
rlm@260 81 (if-let [strength (meta-data muscle "strength")]
rlm@260 82 strength 1))
rlm@260 83
rlm@260 84 (defn motor-pool
rlm@260 85 "Return a vector where each entry is the strength of the \"motor
rlm@260 86 neuron\" at that part in the muscle."
rlm@260 87 [#^Node muscle]
rlm@260 88 (let [profile (muscle-profile-image muscle)]
rlm@260 89 (vec
rlm@260 90 (let [width (.getWidth profile)]
rlm@260 91 (for [x (range width)]
rlm@260 92 (- 255
rlm@260 93 (bit-and
rlm@260 94 0x0000FF
rlm@260 95 (.getRGB profile x 0))))))))
rlm@260 96 #+end_src
rlm@260 97
rlm@260 98 Of note here is =(motor-pool)= which interprets the muscle-profile
rlm@260 99 image in a way that allows me to use gradients between white and red,
rlm@260 100 instead of shades of gray as I've been using for all the other
rlm@260 101 senses. This is purely an aesthetic touch.
rlm@260 102
rlm@260 103 * Creating Muscles
rlm@260 104 #+begin_src clojure
rlm@260 105 (defn movement-kernel
rlm@180 106 "Returns a function which when called with a integer value inside a
rlm@191 107 running simulation will cause movement in the creature according
rlm@191 108 to the muscle's position and strength profile. Each function
rlm@191 109 returns the amount of force applied / max force."
rlm@260 110 [#^Node creature #^Node muscle]
rlm@260 111 (let [target (closest-node creature muscle)
rlm@158 112 axis
rlm@158 113 (.mult (.getWorldRotation muscle) Vector3f/UNIT_Y)
rlm@260 114 strength (muscle-strength muscle)
rlm@260 115
rlm@260 116 pool (motor-pool muscle)
rlm@260 117 pool-integral (reductions + pool)
rlm@191 118 force-index
rlm@260 119 (vec (map #(float (* strength (/ % (last pool-integral))))
rlm@260 120 pool-integral))
rlm@158 121 control (.getControl target RigidBodyControl)]
rlm@158 122 (fn [n]
rlm@260 123 (let [pool-index (max 0 (min n (dec (count pool))))
rlm@191 124 force (force-index pool-index)]
rlm@191 125 (.applyTorque control (.mult axis force))
rlm@191 126 (float (/ force strength))))))
rlm@158 127
rlm@180 128 (defn movement!
rlm@180 129 "Endow the creature with the power of movement. Returns a sequence
rlm@180 130 of functions, each of which accept an integer value and will
rlm@180 131 activate their corresponding muscle."
rlm@158 132 [#^Node creature]
rlm@180 133 (for [muscle (muscles creature)]
rlm@260 134 (movement-kernel creature muscle)))
rlm@260 135 #+end_src
rlm@158 136
rlm@260 137 =(movement-kernel)= creates a function that will move the nearest
rlm@260 138 physical object to the muscle node. The muscle exerts a rotational
rlm@260 139 force dependant on it's orientation to the object in the blender
rlm@260 140 file. The function returned by =(movement-kernel)= is also a sense
rlm@260 141 function: it returns the percent of the total muscle strength that is
rlm@260 142 currently being employed. This is analogous to muscle tension in
rlm@260 143 humans and completes the sense of proprioception begun in the last
rlm@260 144 post.
rlm@260 145
rlm@260 146 * Visualizing Muscle Tension
rlm@260 147 Muscle exertion is a percent of a total, so the visulazation is just a
rlm@260 148 simple percent bar.
rlm@260 149
rlm@260 150 #+begin_src clojure
rlm@191 151 (defn movement-display-kernel
rlm@191 152 "Display muscle exertion data as a bar filling up with red."
rlm@191 153 [exertion]
rlm@191 154 (let [height 20
rlm@191 155 width 300
rlm@191 156 image (BufferedImage. width height
rlm@191 157 BufferedImage/TYPE_INT_RGB)
rlm@191 158 fill (min (int (* width exertion)) width)]
rlm@191 159 (dorun
rlm@191 160 (for [x (range fill)
rlm@191 161 y (range height)]
rlm@191 162 (.setRGB image x y 0xFF0000)))
rlm@191 163 image))
rlm@191 164
rlm@191 165 (defn view-movement
rlm@191 166 "Creates a function which accepts a list of muscle-exertion data and
rlm@191 167 displays each element of the list to the screen."
rlm@191 168 []
rlm@191 169 (view-sense movement-display-kernel))
rlm@158 170 #+end_src
rlm@158 171
rlm@260 172 * Adding Touch to the Worm
rlm@158 173
rlm@158 174
rlm@260 175 * Headers
rlm@260 176 #+name: muscle-header
rlm@260 177 #+begin_src clojure
rlm@260 178 (ns cortex.movement
rlm@260 179 "Give simulated creatures defined in special blender files the power
rlm@260 180 to move around in a simulated environment."
rlm@260 181 {:author "Robert McIntyre"}
rlm@260 182 (:use (cortex world util sense body))
rlm@260 183 (:use clojure.contrib.def)
rlm@260 184 (:import java.awt.image.BufferedImage)
rlm@260 185 (:import com.jme3.scene.Node)
rlm@260 186 (:import com.jme3.math.Vector3f)
rlm@260 187 (:import com.jme3.bullet.control.RigidBodyControl))
rlm@260 188 #+end_src
rlm@260 189
rlm@158 190
rlm@158 191
rlm@158 192 * COMMENT code generation
rlm@158 193 #+begin_src clojure :tangle ../src/cortex/movement.clj
rlm@158 194 <<movement>>
rlm@158 195 #+end_src