Mercurial > cortex
view org/proprioception.org @ 174:136349ac6972
organized imports for proprioception
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 04 Feb 2012 06:29:20 -0700 |
parents | 1943b3f581c2 |
children | 0b9ae09eaec3 |
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)18 (:import com.jme3.scene.Node)19 (:import (com.jme3.math Vector3f Quaternion)))21 (defvar22 ^{:arglists '([creature])}23 joints24 (sense-nodes "joints")25 "Return the children of the creature's \"joints\" node.")27 (defn right-handed?28 "true iff the three vectors form a right handed coordinate29 system. The three vectors do not have to be normalized or30 orthogonal."31 [vec1 vec2 vec3]32 (< 0 (.dot (.cross vec1 vec2) vec3)))34 (defn absolute-angle35 "The angle between 'vec1 and 'vec2. Positive if the angle to get36 from 'vec1 to 'vec2 is counterclockwise around 'axis, and negative37 otherwise."38 [vec1 vec2 axis]39 (let [angle (.angleBetween vec1 vec2)]40 (if (right-handed? vec1 vec2 axis)41 angle (- (* 2 Math/PI) angle))))43 (defn proprioception-fn44 "Returns a function which returns proprioceptive sensory data when45 called inside a running simulation."46 [#^Node parts #^Node joint]47 (let [[obj-a obj-b] (joint-targets parts joint)48 joint-rot (.getWorldRotation joint)49 x0 (.mult joint-rot Vector3f/UNIT_X)50 y0 (.mult joint-rot Vector3f/UNIT_Y)51 z0 (.mult joint-rot Vector3f/UNIT_Z)]52 (println-repl "x:" x0)53 (println-repl "y:" y0)54 (println-repl "z:" z0)55 (println-repl "init-a:" (.getWorldRotation obj-a))56 (println-repl "init-b:" (.getWorldRotation obj-b))58 (fn []59 (let [rot-a (.clone (.getWorldRotation obj-a))60 rot-b (.clone (.getWorldRotation obj-b))61 x (.mult rot-a x0)62 y (.mult rot-a y0)63 z (.mult rot-a z0)65 X (.mult rot-b x0)66 Y (.mult rot-b y0)67 Z (.mult rot-b z0)68 heading (Math/atan2 (.dot X z) (.dot X x))69 pitch (Math/atan2 (.dot X y) (.dot X x))71 ;; rotate x-vector back to origin72 reverse73 (doto (Quaternion.)74 (.fromAngleAxis75 (.angleBetween X x)76 (let [cross (.normalize (.cross X x))]77 (if (= 0 (.length cross)) y cross))))78 roll (absolute-angle (.mult reverse Y) y x)]79 [heading pitch roll]))))81 (defn proprioception!82 "Endow the creature with the sense of proprioception. Returns a83 sequence of functions, one for each child of the \"joints\" node in84 the creature, which each report proprioceptive information about85 that joint."86 [#^Node creature]87 ;; extract the body's joints88 (let [senses (map (partial proprioception-fn creature)89 (joints creature))]90 (fn []91 (map #(%) senses))))92 #+end_src94 * COMMENT generate source95 #+begin_src clojure :tangle ../src/cortex/proprioception.clj96 <<proprioception>>97 #+end_src