view org/test-creature.org @ 191:66fbab414d45

added muscle exertion debug view.
author Robert McIntyre <rlm@mit.edu>
date Sat, 04 Feb 2012 11:02:19 -0700
parents 2902aca33c6e
children
line wrap: on
line source
1 #+title: First attempt at a creature!
2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu
4 #+description:
5 #+keywords: simulation, jMonkeyEngine3, clojure
6 #+SETUPFILE: ../../aurellem/org/setup.org
7 #+INCLUDE: ../../aurellem/org/level-0.org
12 * Intro
13 So far, I've made the following senses --
14 - Vision
15 - Hearing
16 - Touch
17 - Proprioception
19 And one effector:
20 - Movement
22 However, the code so far has only enabled these senses, but has not
23 actually implemented them. For example, there is still a lot of work
24 to be done for vision. I need to be able to create an /eyeball/ in
25 simulation that can be moved around and see the world from different
26 angles. I also need to determine weather to use log-polar or cartesian
27 for the visual input, and I need to determine how/wether to
28 disceritise the visual input.
30 I also want to be able to visualize both the sensors and the
31 effectors in pretty pictures. This semi-retarted creature will be my
32 first attempt at bringing everything together.
34 * The creature's body
36 Still going to do an eve-like body in blender, but due to problems
37 importing the joints, etc into jMonkeyEngine3, I'm going to do all
38 the connecting here in clojure code, using the names of the individual
39 components and trial and error. Later, I'll maybe make some sort of
40 creature-building modifications to blender that support whatever
41 discritized senses I'm going to make.
43 #+name: body-1
44 #+begin_src clojure
45 (ns cortex.silly
46 "let's play!"
47 {:author "Robert McIntyre"})
49 ;; TODO remove this!
50 (require 'cortex.import)
51 (cortex.import/mega-import-jme3)
52 (use '(cortex world util body hearing touch vision sense
53 proprioception movement))
55 (rlm.rlm-commands/help)
56 (import java.awt.image.BufferedImage)
57 (import javax.swing.JPanel)
58 (import javax.swing.SwingUtilities)
59 (import java.awt.Dimension)
60 (import javax.swing.JFrame)
61 (import java.awt.Dimension)
62 (import com.aurellem.capture.RatchetTimer)
64 (use 'clojure.contrib.def)
66 (defn load-blender-model
67 "Load a .blend file using an asset folder relative path."
68 [^String model]
69 (.loadModel
70 (doto (asset-manager)
71 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
72 model))
74 (def hand "Models/creature1/one.blend")
76 (def worm "Models/creature1/try-again.blend")
78 (defn worm-model [] (load-blender-model worm))
80 (defn x-ray [#^ColorRGBA color]
81 (doto (Material. (asset-manager)
82 "Common/MatDefs/Misc/Unshaded.j3md")
83 (.setColor "Color" color)
84 (-> (.getAdditionalRenderState)
85 (.setDepthTest false))))
87 (defn colorful []
88 (.getChild (worm-model) "worm-21"))
90 (import jme3tools.converters.ImageToAwt)
92 (import ij.ImagePlus)
96 (defn test-eye []
97 (.getChild
98 (.getChild (worm-model) "eyes")
99 "eye"))
103 ;; lower level --- nodes
104 ;; closest-node "parse/compile-x" -> makes organ, which is spatial, fn pair
106 ;; higher level -- organs
107 ;;
109 ;; higher level --- sense/effector
110 ;; these are the functions that provide world i/o, chinese-room style
112 (defn debug-hearing-window
113 "view audio data"
114 [height]
115 (let [vi (view-image)]
116 (fn [[coords sensor-data]]
117 (let [image (BufferedImage. (count coords) height
118 BufferedImage/TYPE_INT_RGB)]
119 (dorun
120 (for [x (range (count coords))]
121 (dorun
122 (for [y (range height)]
123 (let [raw-sensor (sensor-data x)]
124 (.setRGB image x y (gray raw-sensor)))))))
126 (vi image)))))
128 (defn test-creature [thing]
129 (let [x-axis
130 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
131 y-axis
132 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
133 z-axis
134 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
136 creature (doto (load-blender-model thing) (body!))
138 touch (touch! creature)
139 touch-display (view-touch)
141 vision (vision! creature)
142 vision-display (view-vision)
144 hearing (hearing! creature)
145 hearing-display (view-hearing)
147 prop (proprioception! creature)
148 prop-display (view-proprioception)
150 muscle-exertion (atom 0)
151 muscles (movement! creature)
152 muscle-display (view-movement)
154 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
155 bell (AudioNode. (asset-manager)
156 "Sounds/pure.wav" false)
158 fix-display (runonce
159 (fn [world] (add-camera! world (.getCamera world) no-op)))
160 ]
163 (apply
164 world
165 (with-movement
166 (.getChild creature "worm-21")
167 ["key-r" "key-t"
168 "key-f" "key-g"
169 "key-v" "key-b"]
170 [10 10 10 10 1 1]
171 [(nodify [creature
172 (box 10 2 10 :position (Vector3f. 0 -9 0)
173 :color ColorRGBA/Gray :mass 0)
174 x-axis y-axis z-axis
175 me
176 ])
177 (merge standard-debug-controls
178 {"key-return"
179 (fn [_ value]
180 (if value
181 (do
182 (println-repl "play-sound")
183 (.play bell))))
184 "key-h"
185 (fn [_ value]
186 (if value
187 (swap! muscle-exertion (partial + 20))))
188 "key-n"
189 (fn [_ value]
190 (if value
191 (swap! muscle-exertion (fn [v] (- v 20)))))
193 })
194 (fn [world]
195 (light-up-everything world)
196 (enable-debug world)
197 ;;(dorun (map #(% world) init-vision-fns))
198 ;;(dorun (map #(% world) init-hearing-fns))
200 (add-camera! world
201 (add-eye! creature (test-eye))
202 (comp (view-image) BufferedImage!))
205 ;;(set-gravity world (Vector3f. 0 0 0))
206 ;;(com.aurellem.capture.Capture/captureVideo
207 ;; world (file-str "/home/r/proj/ai-videos/hand"))
208 ;;(.setTimer world (RatchetTimer. 60))
209 (speed-up world)
210 (set-gravity world (Vector3f. 0 0 0))
211 )
212 (fn [world tpf]
213 ;;(dorun
214 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))
216 (prop-display (prop))
218 (touch-display (map #(% (.getRootNode world)) touch))
220 (vision-display (map #(% world) vision))
222 (hearing-display (map #(% world) hearing))
224 (muscle-display (map #(% @muscle-exertion) muscles))
226 ;;(println-repl (vision-data))
227 (.setLocalTranslation me (.getLocation (.getCamera world)))
228 (fix-display world)
230 )]
231 ;;(let [timer (atom 0)]
232 ;; (fn [_ _]
233 ;; (swap! timer inc)
234 ;; (if (= (rem @timer 60) 0)
235 ;; (println-repl (float (/ @timer 60))))))
236 ))))
240 ;; the camera will stay in its initial position/rotation with relation
241 ;; to the spatial.
244 ;;dylan (defn follow-sense, adjoin-sense, attach-stimuli,
245 ;;anchor-qualia, augment-organ, with-organ
247 (defn follow-test
248 "show a camera that stays in the same relative position to a blue cube."
249 []
250 (let [camera-pos (Vector3f. 0 30 0)
251 rock (box 1 1 1 :color ColorRGBA/Blue
252 :position (Vector3f. 0 10 0)
253 :mass 30
254 )
255 rot (.getWorldRotation rock)
257 table (box 3 1 10 :color ColorRGBA/Gray :mass 0
258 :position (Vector3f. 0 -3 0))]
260 (world
261 (nodify [rock table])
262 standard-debug-controls
263 (fn [world]
264 (let
265 [cam (doto (.clone (.getCamera world))
266 (.setLocation camera-pos)
267 (.lookAt Vector3f/ZERO
268 Vector3f/UNIT_X))]
269 (bind-sense rock cam)
271 (.setTimer world (RatchetTimer. 60))
272 (add-camera! world cam (comp (view-image) BufferedImage!))
273 (add-camera! world (.getCamera world) no-op))
274 )
275 (fn [_ _] (println-repl rot)))))
279 #+end_src
281 #+results: body-1
282 : #'cortex.silly/follow-test
285 * COMMENT purgatory
286 #+begin_src clojure
288 (defn bullet-trans* []
289 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
290 :position (Vector3f. 5 0 0)
291 :mass 90)
292 obj-b (sphere 0.5 :color ColorRGBA/Blue
293 :position (Vector3f. -5 0 0)
294 :mass 0)
295 control-a (.getControl obj-a RigidBodyControl)
296 control-b (.getControl obj-b RigidBodyControl)
297 move-up? (atom nil)
298 move-down? (atom nil)
299 move-left? (atom nil)
300 move-right? (atom nil)
301 roll-left? (atom nil)
302 roll-right? (atom nil)
303 force 100
304 swivel
305 (.toRotationMatrix
306 (doto (Quaternion.)
307 (.fromAngleAxis (/ Math/PI 2)
308 Vector3f/UNIT_X)))
309 x-move
310 (doto (Matrix3f.)
311 (.fromStartEndVectors Vector3f/UNIT_X
312 (.normalize (Vector3f. 1 1 0))))
314 timer (atom 0)]
315 (doto
316 (ConeJoint.
317 control-a control-b
318 (Vector3f. -8 0 0)
319 (Vector3f. 2 0 0)
320 ;;swivel swivel
321 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
322 x-move Matrix3f/IDENTITY
323 )
324 (.setCollisionBetweenLinkedBodys false)
325 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
326 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
327 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
328 (world (nodify
329 [obj-a obj-b])
330 (merge standard-debug-controls
331 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
332 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
333 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
334 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
335 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
336 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
338 (fn [world]
339 (enable-debug world)
340 (set-gravity world Vector3f/ZERO)
341 )
343 (fn [world _]
345 (if @move-up?
346 (.applyForce control-a
347 (Vector3f. force 0 0)
348 (Vector3f. 0 0 0)))
349 (if @move-down?
350 (.applyForce control-a
351 (Vector3f. (- force) 0 0)
352 (Vector3f. 0 0 0)))
353 (if @move-left?
354 (.applyForce control-a
355 (Vector3f. 0 force 0)
356 (Vector3f. 0 0 0)))
357 (if @move-right?
358 (.applyForce control-a
359 (Vector3f. 0 (- force) 0)
360 (Vector3f. 0 0 0)))
362 (if @roll-left?
363 (.applyForce control-a
364 (Vector3f. 0 0 force)
365 (Vector3f. 0 0 0)))
366 (if @roll-right?
367 (.applyForce control-a
368 (Vector3f. 0 0 (- force))
369 (Vector3f. 0 0 0)))
371 (if (zero? (rem (swap! timer inc) 100))
372 (.attachChild
373 (.getRootNode world)
374 (sphere 0.05 :color ColorRGBA/Yellow
375 :physical? false :position
376 (.getWorldTranslation obj-a)))))
377 )
378 ))
380 (defn test-joint [joint]
381 (let [[origin top bottom floor] (world-setup joint)
382 control (.getControl top RigidBodyControl)
383 move-up? (atom false)
384 move-down? (atom false)
385 move-left? (atom false)
386 move-right? (atom false)
387 roll-left? (atom false)
388 roll-right? (atom false)
389 timer (atom 0)]
391 (world
392 (nodify [top bottom floor origin])
393 (merge standard-debug-controls
394 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
395 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
396 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
397 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
398 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
399 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
401 (fn [world]
402 (light-up-everything world)
403 (enable-debug world)
404 (set-gravity world (Vector3f. 0 0 0))
405 )
407 (fn [world _]
408 (if (zero? (rem (swap! timer inc) 100))
409 (do
410 ;; (println-repl @timer)
411 (.attachChild (.getRootNode world)
412 (sphere 0.05 :color ColorRGBA/Yellow
413 :position (.getWorldTranslation top)
414 :physical? false))
415 (.attachChild (.getRootNode world)
416 (sphere 0.05 :color ColorRGBA/LightGray
417 :position (.getWorldTranslation bottom)
418 :physical? false))))
420 (if @move-up?
421 (.applyTorque control
422 (.mult (.getPhysicsRotation control)
423 (Vector3f. 0 0 10))))
424 (if @move-down?
425 (.applyTorque control
426 (.mult (.getPhysicsRotation control)
427 (Vector3f. 0 0 -10))))
428 (if @move-left?
429 (.applyTorque control
430 (.mult (.getPhysicsRotation control)
431 (Vector3f. 0 10 0))))
432 (if @move-right?
433 (.applyTorque control
434 (.mult (.getPhysicsRotation control)
435 (Vector3f. 0 -10 0))))
436 (if @roll-left?
437 (.applyTorque control
438 (.mult (.getPhysicsRotation control)
439 (Vector3f. -1 0 0))))
440 (if @roll-right?
441 (.applyTorque control
442 (.mult (.getPhysicsRotation control)
443 (Vector3f. 1 0 0))))))))
444 #+end_src
447 * COMMENT generate source
448 #+begin_src clojure :tangle ../src/cortex/silly.clj
449 <<body-1>>
450 #+end_src