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