comparison org/worm_learn.clj @ 406:40b67bb71430

save progress.
author Robert McIntyre <rlm@mit.edu>
date Tue, 18 Mar 2014 18:34:10 -0400
parents 9b4a4da08b78
children bd6d03596ea8
comparison
equal deleted inserted replaced
405:9b4a4da08b78 406:40b67bb71430
7 (:import java.io.File) 7 (:import java.io.File)
8 (:import com.jme3.audio.AudioNode) 8 (:import com.jme3.audio.AudioNode)
9 (:import com.aurellem.capture.RatchetTimer) 9 (:import com.aurellem.capture.RatchetTimer)
10 (:import (com.aurellem.capture Capture IsoTimer)) 10 (:import (com.aurellem.capture Capture IsoTimer))
11 (:import (com.jme3.math Vector3f ColorRGBA))) 11 (:import (com.jme3.math Vector3f ColorRGBA)))
12 12
13 (use 'clojure.pprint)
13 14
14 (dorun (cortex.import/mega-import-jme3)) 15 (dorun (cortex.import/mega-import-jme3))
15 (rlm.rlm-commands/help) 16 (rlm.rlm-commands/help)
16 17
17 (load-bullet) 18 (load-bullet)
24 (def output-base (File. "/home/r/proj/cortex/render/worm-learn/curl")) 25 (def output-base (File. "/home/r/proj/cortex/render/worm-learn/curl"))
25 26
26 27
27 (defn motor-control-program 28 (defn motor-control-program
28 "Create a function which will execute the motor script" 29 "Create a function which will execute the motor script"
29 [muscle-positions 30 [muscle-labels
30 script] 31 script]
31 (let [current-frame (atom -1) 32 (let [current-frame (atom -1)
32 keyed-script (group-by first script) 33 keyed-script (group-by first script)
33 current-forces (atom {}) ] 34 current-forces (atom {}) ]
34 (fn [effectors] 35 (fn [effectors]
37 (for [[_ part force] (keyed-script (swap! current-frame inc))] 38 (for [[_ part force] (keyed-script (swap! current-frame inc))]
38 (swap! current-forces (fn [m] (assoc m part force))))) 39 (swap! current-forces (fn [m] (assoc m part force)))))
39 (doall (map (fn [effector power] 40 (doall (map (fn [effector power]
40 (effector (int power))) 41 (effector (int power)))
41 effectors 42 effectors
42 (map #(@current-forces % 0) muscle-positions))))))) 43 (map #(@current-forces % 0) muscle-labels)))))))
43 44
44 (defn worm-direct-control 45 (defn worm-direct-control
45 "Create keybindings and a muscle control program that will enable 46 "Create keybindings and a muscle control program that will enable
46 the user to control the worm via the keyboard." 47 the user to control the worm via the keyboard."
47 [muscle-labels activation-strength] 48 [muscle-labels activation-strength]
94 [(+ time-base period) flexor 0] 95 [(+ time-base period) flexor 0]
95 [(+ time-base period 1) extensor power] 96 [(+ time-base period 1) extensor power]
96 [(+ time-base (+ (* 2 period) 2)) extensor 0]])) 97 [(+ time-base (+ (* 2 period) 2)) extensor 0]]))
97 98
98 (def wiggle-script 99 (def wiggle-script
99 (mapcat gen-wiggle (repeat 40 [:a-down :a-up]) 100 (mapcat gen-wiggle (repeat 4000 [:a-down :a-up])
100 (range 100 10000 (+ 3 (* period 2))))) 101 (range 100 1000000 (+ 3 (* period 2)))))
101 102
102 103
103 ;; Normally, we'd use unsupervised/supervised machine learning to pick 104 ;; Normally, we'd use unsupervised/supervised machine learning to pick
104 ;; out the defining features of the different actions available to the 105 ;; out the defining features of the different actions available to the
105 ;; worm. For this project, I am going to explicitely define functions 106 ;; worm. For this project, I am going to explicitely define functions
130 131
131 (defn grand-circle? 132 (defn grand-circle?
132 "Does the worm form a majestic circle (one end touching the other)?" 133 "Does the worm form a majestic circle (one end touching the other)?"
133 [experiences] 134 [experiences]
134 (and (curled? experiences) 135 (and (curled? experiences)
135 true)) 136 true)) ;; TODO: add code here.
136 137
137 (defn vector:last-n [v n] 138 (defn vector:last-n [v n]
138 (let [c (count v)] 139 (let [c (count v)]
139 (if (< c n) v 140 (if (< c n) v
140 (subvec v (- c n) c)))) 141 (subvec v (- c n) c))))
141 142
143 (defn touch-average [[coords touch]]
144 (/ (average (map first touch)) (average (map second touch))))
145
146 (defn floor-contact [[coords contact :as touch]]
147 (let [raw-average
148 (average
149 (map
150 first
151 (vals
152 (select-keys
153 (zipmap coords contact)
154 [[8 15] [8 16] [8 17] [8 18] [8 19] [8 20] [8 21] [8 22] [9 15]
155 [9 16] [9 17] [9 18] [9 19] [9 20] [9 21] [9 22] [10 15] [10 16]
156 [10 17] [10 18] [10 19] [10 20] [10 21] [10 22] [11 15] [11 16]
157 [11 17] [11 18] [11 19] [11 20] [11 21] [11 22] [12 15] [12 16]
158 [12 17] [12 18] [12 19] [12 20] [12 21] [12 22] [13 15] [13 16]
159 [13 17] [13 18] [13 19] [13 20] [13 21] [13 22] [14 15] [14 16]
160 [14 17] [14 18] [14 19] [14 20] [14 21] [14 22]]))))]
161 (Math/abs (- 1. (* 10 raw-average)))))
162
163
142 (defn wiggling? 164 (defn wiggling?
143 "Is the worm wiggling?" 165 "Is the worm wiggling?"
144 [experiences] 166 [experiences]
145 (vector:last-n 167 (vector:last-n experiences 200)
146 168
147 169 )
148 ))
149
150
151
152
153
154 (defn resting?
155 "Is the worm on the ground in a neutral position?"
156 [])
157 170
158 (def standard-world-view 171 (def standard-world-view
159 [(Vector3f. 4.207176, -3.7366982, 3.0816958) 172 [(Vector3f. 4.207176, -3.7366982, 3.0816958)
160 (Quaternion. 0.11118768, 0.87678415, 0.24434438, -0.3989771)]) 173 (Quaternion. 0.11118768, 0.87678415, 0.24434438, -0.3989771)])
161 174
221 (record-experience! 234 (record-experience!
222 experiences {:touch touch-data 235 experiences {:touch touch-data
223 :proprioception proprioception-data 236 :proprioception proprioception-data
224 :muscle muscle-data}) 237 :muscle muscle-data})
225 (if (curled? @experiences) (println "Curled")) 238 (if (curled? @experiences) (println "Curled"))
226 (if (straight? @experiences) (println "Straight")) 239 ;;(if (straight? @experiences) (println "Straight"))
240 (println-repl
241 (apply format "%.2f %.2f %.2f %.2f %.2f\n"
242 (map floor-contact touch-data)))
243
227 ) 244 )
228 (muscle-display 245 (muscle-display
229 muscle-data 246 muscle-data
230 (if record (dir! (File. record "muscle")))) 247 (if record (dir! (File. record "muscle"))))
231 (prop-display 248 (prop-display