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 Proprioception
2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu
4 #+description: proprioception for simulated creatures
5 #+keywords: simulation, jMonkeyEngine3, clojure
6 #+SETUPFILE: ../../aurellem/org/setup.org
7 #+INCLUDE: ../../aurellem/org/level-0.org
9 #+name: proprioception
10 #+begin_src clojure
11 (ns cortex.proprioception
12 "Simulate the sense of proprioception (ability to detect the
13 relative positions of body parts with repsect to other body parts)
14 in jMonkeyEngine3. Reads specially prepared blender files to
15 automatically generate proprioceptive senses."
16 (:use (cortex world util sense body))
17 (:use clojure.contrib.def))
20 (cortex.import/mega-import-jme3)
22 (defvar
23 ^{:arglists '([creature])}
24 joints
25 (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 coordinate
30 system. The three vectors do not have to be normalized or
31 orthogonal."
32 [vec1 vec2 vec3]
33 (< 0 (.dot (.cross vec1 vec2) vec3)))
35 (defn absolute-angle
36 "The angle between 'vec1 and 'vec2. Positive if the angle to get
37 from 'vec1 to 'vec2 is counterclockwise around 'axis, and negative
38 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-fn
45 "Returns a function which returns proprioceptive sensory data when
46 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 origin
73 reverse
74 (doto (Quaternion.)
75 (.fromAngleAxis
76 (.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 a
84 sequence of functions, one for each child of the \"joints\" node in
85 the creature, which each report proprioceptive information about
86 that joint."
87 [#^Node creature]
88 ;; extract the body's joints
89 (let [senses (map (partial proprioception-fn creature)
90 (joints creature))]
91 (fn []
92 (map #(%) senses))))
98 #+end_src
107 * COMMENT generate source
108 #+begin_src clojure :tangle ../src/cortex/proprioception.clj
109 <<proprioception>>
110 #+end_src