Mercurial > cortex
view org/proprioception.org @ 173:1943b3f581c2
renamed proprioception functions
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 04 Feb 2012 06:26:40 -0700 |
parents | 84c67be00abe |
children | 136349ac6972 |
line wrap: on
line source
1 #+title: The Sense of Proprioception2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description: proprioception for simulated creatures5 #+keywords: simulation, jMonkeyEngine3, clojure6 #+SETUPFILE: ../../aurellem/org/setup.org7 #+INCLUDE: ../../aurellem/org/level-0.org9 #+name: proprioception10 #+begin_src clojure11 (ns cortex.proprioception12 "Simulate the sense of proprioception (ability to detect the13 relative positions of body parts with repsect to other body parts)14 in jMonkeyEngine3. Reads specially prepared blender files to15 automatically generate proprioceptive senses."16 (:use (cortex world util sense body))17 (:use clojure.contrib.def))20 (cortex.import/mega-import-jme3)22 (defvar23 ^{:arglists '([creature])}24 joints25 (sense-nodes "joints")26 "Return the children of the creature's \"joints\" node.")28 (defn right-handed?29 "true iff the three vectors form a right handed coordinate30 system. The three vectors do not have to be normalized or31 orthogonal."32 [vec1 vec2 vec3]33 (< 0 (.dot (.cross vec1 vec2) vec3)))35 (defn absolute-angle36 "The angle between 'vec1 and 'vec2. Positive if the angle to get37 from 'vec1 to 'vec2 is counterclockwise around 'axis, and negative38 otherwise."39 [vec1 vec2 axis]40 (let [angle (.angleBetween vec1 vec2)]41 (if (right-handed? vec1 vec2 axis)42 angle (- (* 2 Math/PI) angle))))44 (defn proprioception-fn45 "Returns a function which returns proprioceptive sensory data when46 called inside a running simulation."47 [#^Node parts #^Node joint]48 (let [[obj-a obj-b] (joint-targets parts joint)49 joint-rot (.getWorldRotation joint)50 x0 (.mult joint-rot Vector3f/UNIT_X)51 y0 (.mult joint-rot Vector3f/UNIT_Y)52 z0 (.mult joint-rot Vector3f/UNIT_Z)]53 (println-repl "x:" x0)54 (println-repl "y:" y0)55 (println-repl "z:" z0)56 (println-repl "init-a:" (.getWorldRotation obj-a))57 (println-repl "init-b:" (.getWorldRotation obj-b))59 (fn []60 (let [rot-a (.clone (.getWorldRotation obj-a))61 rot-b (.clone (.getWorldRotation obj-b))62 x (.mult rot-a x0)63 y (.mult rot-a y0)64 z (.mult rot-a z0)66 X (.mult rot-b x0)67 Y (.mult rot-b y0)68 Z (.mult rot-b z0)69 heading (Math/atan2 (.dot X z) (.dot X x))70 pitch (Math/atan2 (.dot X y) (.dot X x))72 ;; rotate x-vector back to origin73 reverse74 (doto (Quaternion.)75 (.fromAngleAxis76 (.angleBetween X x)77 (let [cross (.normalize (.cross X x))]78 (if (= 0 (.length cross)) y cross))))79 roll (absolute-angle (.mult reverse Y) y x)]80 [heading pitch roll]))))82 (defn proprioception!83 "Endow the creature with the sense of proprioception. Returns a84 sequence of functions, one for each child of the \"joints\" node in85 the creature, which each report proprioceptive information about86 that joint."87 [#^Node creature]88 ;; extract the body's joints89 (let [senses (map (partial proprioception-fn creature)90 (joints creature))]91 (fn []92 (map #(%) senses))))98 #+end_src107 * COMMENT generate source108 #+begin_src clojure :tangle ../src/cortex/proprioception.clj109 <<proprioception>>110 #+end_src