view org/self_organizing_touch.clj @ 555:c8f7d10b1a2a

further clarification about skin partitioning per winston's request
author Robert McIntyre <rlm@mit.edu>
date Fri, 02 May 2014 14:27:29 -0400
parents 0b891e0dd809
children 1943a5313f40
line wrap: on
line source
1 (ns org.aurellem.self-organizing-touch
2 "Using free play to automatically organize touch perception into regions."
3 {:author "Robert McIntyre"}
4 (:use (cortex world util import body sense
5 hearing touch vision proprioception movement
6 test))
7 (:use [clojure set pprint])
8 (:import (com.jme3.math ColorRGBA Vector3f))
9 (:import java.io.File)
10 (:import com.jme3.audio.AudioNode)
11 (:import com.aurellem.capture.RatchetTimer)
12 (:import (com.aurellem.capture Capture IsoTimer))
13 (:import (com.jme3.math Vector3f ColorRGBA)))
15 (use 'org.aurellem.worm-learn)
16 (dorun (cortex.import/mega-import-jme3))
18 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19 ;; A demonstration of self organiging touch maps through experience. ;
20 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
22 (def single-worm-segment-view
23 [(Vector3f. 2.0681207, -6.1406755, 1.6106138)
24 (Quaternion. -0.15558705, 0.843615, -0.3428654, -0.38281822)])
26 (def worm-single-segment-muscle-labels
27 [:lift-1 :lift-2 :roll-1 :roll-2])
29 (defn touch-kinesthetics []
30 [[170 :lift-1 40]
31 [190 :lift-1 19]
32 [206 :lift-1 0]
34 [400 :lift-2 40]
35 [410 :lift-2 0]
37 [570 :lift-2 40]
38 [590 :lift-2 21]
39 [606 :lift-2 0]
41 [800 :lift-1 30]
42 [809 :lift-1 0]
44 [900 :roll-2 40]
45 [905 :roll-2 20]
46 [910 :roll-2 0]
48 [1000 :roll-2 40]
49 [1005 :roll-2 20]
50 [1010 :roll-2 0]
52 [1100 :roll-2 40]
53 [1105 :roll-2 20]
54 [1110 :roll-2 0]
55 ])
57 (defn single-worm-segment []
58 (load-blender-model "Models/worm/worm-single-segment.blend"))
60 (defn worm-segment []
61 (let [model (single-worm-segment)]
62 {:body (doto model (body!))
63 :touch (touch! model)
64 :proprioception (proprioception! model)
65 :muscles (movement! model)}))
68 (defn worm-segment-defaults []
69 (let [direct-control (worm-direct-control worm-muscle-labels 40)]
70 (merge (worm-world-defaults)
71 {:worm worm-segment
72 :view single-worm-segment-view
73 :experience-watch nil
74 :motor-control
75 (motor-control-program
76 worm-single-segment-muscle-labels
77 (touch-kinesthetics))
78 :end-frame 1200})))
80 (def full-contact [(float 0.0) (float 0.1)])
82 (defn pure-touch?
83 "This is worm specific code to determine if a large region of touch
84 sensors is either all on or all off."
85 [[coords touch :as touch-data]]
86 (= (set (map first touch)) (set full-contact)))
88 (defn remove-similar
89 [coll]
90 (loop [result () coll (sort-by (comp - count) coll)]
91 (if (empty? coll) result
92 (let [[x & xs] coll
93 c (count x)]
94 (if (some
95 (fn [other-set]
96 (let [oc (count other-set)]
97 (< (- (count (union other-set x)) c) (* oc 0.1))))
98 xs)
99 (recur result xs)
100 (recur (cons x result) xs))))))
103 (defn view-touch-region
104 ([coords out]
105 (let [touched-region
106 (reduce
107 (fn [m k]
108 (assoc m k [0.0 0.1]))
109 (zipmap all-touch-coordinates (repeat [0.1 0.1])) coords)
110 data
111 [[(vec (keys touched-region)) (vec (vals touched-region))]]
112 touch-display (view-touch)]
113 (touch-display data out)))
114 ([coords] (view-touch-region nil)))
117 (defn learn-touch-regions []
118 (let [experiences (atom [])
119 world (apply-map
120 worm-world
121 (assoc (worm-segment-defaults)
122 :experiences experiences
123 :record false
124 ;;(File. "/home/r/proj/cortex/thesis/video/touch-learn-2/")
125 ))]
126 (run-world world)
127 (->>
128 @experiences
129 (drop 175)
130 ;; access the single segment's touch data
131 (map (comp first :touch))
132 ;; only deal with "pure" touch data to determine surfaces
133 (filter pure-touch?)
134 ;; associate coordinates with touch values
135 (map (partial apply zipmap))
136 ;; select those regions where contact is being made
137 (map (partial group-by second))
138 (map #(get % full-contact))
139 (map (partial map first))
140 ;; remove redundant/subset regions
141 (map set)
142 remove-similar)))
145 (def all-touch-coordinates
146 (concat
147 (rect-region [0 15] [7 22])
148 (rect-region [8 0] [14 29])
149 (rect-region [15 15] [22 22])))
151 (defn view-touch-region [coords]
152 (let [touched-region
153 (reduce
154 (fn [m k]
155 (assoc m k [0.0 0.1]))
156 (zipmap all-touch-coordinates (repeat [0.1 0.1])) coords)
157 data
158 [[(vec (keys touched-region)) (vec (vals touched-region))]]
159 touch-display (view-touch)]
160 (dorun (repeatedly 5 #(touch-display data)))))
162 (defn learn-and-view-touch-regions []
163 (map view-touch-region
164 (learn-touch-regions)))