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 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)
18 (:import com.jme3.scene.Node)
19 (:import (com.jme3.math Vector3f Quaternion)))
21 (defvar
22 ^{:arglists '([creature])}
23 joints
24 (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 coordinate
29 system. The three vectors do not have to be normalized or
30 orthogonal."
31 [vec1 vec2 vec3]
32 (< 0 (.dot (.cross vec1 vec2) vec3)))
34 (defn absolute-angle
35 "The angle between 'vec1 and 'vec2. Positive if the angle to get
36 from 'vec1 to 'vec2 is counterclockwise around 'axis, and negative
37 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-fn
44 "Returns a function which returns proprioceptive sensory data when
45 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 origin
72 reverse
73 (doto (Quaternion.)
74 (.fromAngleAxis
75 (.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 a
83 sequence of functions, one for each child of the \"joints\" node in
84 the creature, which each report proprioceptive information about
85 that joint."
86 [#^Node creature]
87 ;; extract the body's joints
88 (let [senses (map (partial proprioception-fn creature)
89 (joints creature))]
90 (fn []
91 (map #(%) senses))))
92 #+end_src
94 * COMMENT generate source
95 #+begin_src clojure :tangle ../src/cortex/proprioception.clj
96 <<proprioception>>
97 #+end_src