Mercurial > cortex
comparison org/worm_learn.clj @ 400:6ba908c1a0a9
on the warpath to the final stretch.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sun, 16 Mar 2014 23:30:32 -0400 |
parents | 85393ec986dc |
children | 939bcc5950b2 |
comparison
equal
deleted
inserted
replaced
399:85393ec986dc | 400:6ba908c1a0a9 |
---|---|
12 | 12 |
13 | 13 |
14 (dorun (cortex.import/mega-import-jme3)) | 14 (dorun (cortex.import/mega-import-jme3)) |
15 (rlm.rlm-commands/help) | 15 (rlm.rlm-commands/help) |
16 | 16 |
17 (load-bullet) | |
17 | 18 |
18 (def hand "Models/test-creature/hand.blend") | 19 (def hand "Models/test-creature/hand.blend") |
19 | 20 |
20 (defn worm-model [] | 21 (defn worm-model [] |
21 (load-blender-model "Models/worm/worm.blend")) | 22 (load-blender-model "Models/worm/worm.blend")) |
22 | 23 |
23 (def output-base (File. "/home/r/proj/cortex/render/worm-learn/")) | 24 (def output-base (File. "/home/r/proj/cortex/render/worm-learn/curl")) |
24 | 25 |
25 | 26 |
26 (defn motor-control-program | 27 (defn motor-control-program |
27 "Create a function which will execute the motor script" | 28 "Create a function which will execute the motor script" |
28 [muscle-positions | 29 [muscle-positions |
38 (doall (map (fn [effector power] | 39 (doall (map (fn [effector power] |
39 (effector (int power))) | 40 (effector (int power))) |
40 effectors | 41 effectors |
41 (map #(@current-forces % 0) muscle-positions))))))) | 42 (map #(@current-forces % 0) muscle-positions))))))) |
42 | 43 |
44 | |
45 | |
46 | |
47 ;; These are scripts that direct the worm to move in two radically | |
48 ;; different patterns -- a sinusoidal wiggling motion, and a curling | |
49 ;; motions that causes the worm to form a circle. | |
50 | |
51 (def curl-script | |
52 [[370 :d-up 40] | |
53 [600 :d-up 0]]) | |
54 | |
55 (def period 18) | |
56 | |
43 (def muscle-labels | 57 (def muscle-labels |
44 [:base-up :base-down | 58 [:base-up :base-down |
45 :a-up :a-down | 59 :a-up :a-down |
46 :b-up :b-down | 60 :b-up :b-down |
47 :c-up :c-down | 61 :c-up :c-down |
48 :d-up :d-down | 62 :d-up :d-down]) |
49 ]) | |
50 | |
51 (def curl-script | |
52 [[370 :d-up 20] | |
53 [390 :d-up 0]]) | |
54 | |
55 (def period 18) | |
56 | 63 |
57 (defn gen-wiggle [[flexor extensor :as muscle-pair] time-base] | 64 (defn gen-wiggle [[flexor extensor :as muscle-pair] time-base] |
58 (let [period period | 65 (let [period period |
59 power 45] | 66 power 45] |
60 [[time-base flexor power] | 67 [[time-base flexor power] |
61 [(+ time-base period) flexor 0] | 68 [(+ time-base period) flexor 0] |
62 [(+ time-base period 1) extensor power] | 69 [(+ time-base period 1) extensor power] |
63 [(+ time-base (+ (* 2 period) 2)) extensor 0]])) | 70 [(+ time-base (+ (* 2 period) 2)) extensor 0]])) |
64 | 71 |
65 (def wiggle-script | 72 (def wiggle-script |
66 (mapcat gen-wiggle [[:d-up :d-down] | |
67 [:c-up :c-down] | |
68 [:b-up :b-down] | |
69 [:a-up :a-down]] (range 100 1000 12))) | |
70 | |
71 (def wiggle-script | |
72 (mapcat gen-wiggle (repeat 40 [:a-down :a-up]) | 73 (mapcat gen-wiggle (repeat 40 [:a-down :a-up]) |
73 (range 100 10000 (+ 3 (* period 2))))) | 74 (range 100 10000 (+ 3 (* period 2))))) |
74 | 75 |
75 | 76 |
77 ;; Normally, we'd use unsupervised/supervised machine learning to pick | |
78 ;; out the defining features of the different actions available to the | |
79 ;; worm. For this project, I am going to explicitely define functions | |
80 ;; that recognize curling and wiggling respectively. These functions | |
81 ;; are defined using all the information available from an embodied | |
82 ;; simulation of the action. Note how much easier they are to define | |
83 ;; than if I only had vision to work with. Things like scale/position | |
84 ;; invariance are complete non-issues here. This is the advantage of | |
85 ;; body-centered action recognition and what I hope to show with this | |
86 ;; thesis. | |
87 | |
88 (defn last-nth | |
89 "Create function that will, when called each frame with the senses | |
90 of a creature, will record those results and return the last n | |
91 results." | |
92 [n] | |
93 (let [last-n '()] | |
94 (fn [frame-num {:keys [touch proprioception muscles hearing]}] | |
95 (take n (cons [frame-num :stuff] last-n))))) | |
96 | |
97 | |
98 (defn wiggling? | |
99 "Generate a function which, when called each frame with the sensory | |
100 inputs of a worm, will determine whether the worm is wiggling." | |
101 [{:keys [touch proprioception muscles hearing]}] | |
102 (map (fn [f] (f)) proprioception | |
103 | |
104 | |
105 )) | |
106 | |
107 (defn curling? | |
108 "Is the worm curled up?" | |
109 [] | |
110 ) | |
111 | |
112 (defn resting? | |
113 "Is the worm on the ground in a neutral position?" | |
114 []) | |
115 | |
116 (def standard-world-view | |
117 [(Vector3f. 4.207176, -3.7366982, 3.0816958) | |
118 (Quaternion. 0.11118768, 0.87678415, 0.24434438, -0.3989771)]) | |
119 | |
120 (def worm-side-view | |
121 [(Vector3f. 4.207176, -3.7366982, 3.0816958) | |
122 (Quaternion. -0.11555642, 0.88188726, -0.2854942, -0.3569518)]) | |
123 | |
124 (def degenerate-worm-view | |
125 [(Vector3f. -0.0708936, -8.570261, 2.6487997) | |
126 (Quaternion. -2.318909E-4, 0.9985348, 0.053941682, 0.004291452)]) | |
76 | 127 |
77 (defn worm-world | 128 (defn worm-world |
78 "" | 129 "" |
79 ([] (worm-world curl-script)) | 130 ([] (worm-world curl-script)) |
80 ([motion-script] | 131 ([motion-script] |
81 (let [record? false | 132 (let [record? false ;;true |
82 worm (doto (worm-model) (body!)) | 133 worm (doto (worm-model) (body!)) |
83 touch '();;(touch! worm) | 134 touch (touch! worm) |
84 prop (proprioception! worm) | 135 prop (proprioception! worm) |
85 muscles (movement! worm) | 136 muscles (movement! worm) |
86 | 137 |
87 touch-display (view-touch) | 138 touch-display (view-touch) |
88 prop-display (view-proprioception) | 139 prop-display (view-proprioception) |
97 (nodify [worm floor]) | 148 (nodify [worm floor]) |
98 standard-debug-controls | 149 standard-debug-controls |
99 | 150 |
100 (fn [world] | 151 (fn [world] |
101 ;; (set-gravity world Vector3f/ZERO) | 152 ;; (set-gravity world Vector3f/ZERO) |
102 ;; (position-camera | 153 (position-camera world degenerate-worm-view) |
103 ;; world (Vector3f. 4.207176, -3.7366982, 3.0816958) | |
104 ;; (Quaternion. 0.11118768, 0.87678415, 0.24434438, -0.3989771)) | |
105 | |
106 | |
107 | |
108 (position-camera | |
109 world (Vector3f. 4.207176, -3.7366982, 3.0816958) | |
110 (Quaternion. -0.11555642, 0.88188726, -0.2854942, -0.3569518)) | |
111 | |
112 | |
113 | |
114 | |
115 (let [timer (IsoTimer. 60)] | 154 (let [timer (IsoTimer. 60)] |
116 (.setTimer world timer) | 155 (.setTimer world timer) |
117 (display-dilated-time world timer)) | 156 (display-dilated-time world timer)) |
118 (if record? | 157 (if record? |
119 (Capture/captureVideo | 158 (Capture/captureVideo |
120 world | 159 world |
121 (File. output-base "main-view"))) | 160 (File. output-base "main-view"))) |
122 (speed-up world) | 161 (speed-up world) |
123 (light-up-everything world)) | 162 (light-up-everything world)) |
124 | |
125 (fn [world tpf] | 163 (fn [world tpf] |
126 (muscle-display | 164 (muscle-display |
127 (control-script muscles) | 165 (control-script muscles) |
128 (if record? (File. output-base "muscle"))) | 166 (if record? (File. output-base "muscle"))) |
129 (prop-display | 167 (prop-display |