changeset 414:634795361af8

working on wiggling? stream predicate.
author Robert McIntyre <rlm@mit.edu>
date Wed, 19 Mar 2014 15:59:46 -0400
parents 54ef2e06c3ef
children af7945c27474
files org/worm_learn.clj
diffstat 1 files changed, 47 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/org/worm_learn.clj	Wed Mar 19 14:24:13 2014 -0400
     1.2 +++ b/org/worm_learn.clj	Wed Mar 19 15:59:46 2014 -0400
     1.3 @@ -81,17 +81,17 @@
     1.4  ;; motions that causes the worm to form a circle.
     1.5  
     1.6  (def curl-script
     1.7 -  [[370 :d-up 40]
     1.8 -   [600 :d-up 0]])
     1.9 +  [[370 :d-flex 40]
    1.10 +   [600 :d-flex 0]])
    1.11  
    1.12  (def period 18)
    1.13  
    1.14  (def worm-muscle-labels
    1.15 -  [:base-down :base-up
    1.16 -   :a-down :a-up
    1.17 -   :b-down :b-up
    1.18 -   :c-down :c-up
    1.19 -   :d-down :d-up])
    1.20 +  [:base-ex :base-flex
    1.21 +   :a-ex :a-flex
    1.22 +   :b-ex :b-flex
    1.23 +   :c-ex :c-flex
    1.24 +   :d-ex :d-flex])
    1.25  
    1.26  (defn gen-wiggle [[flexor extensor :as muscle-pair] time-base]
    1.27    (let [period period
    1.28 @@ -102,7 +102,7 @@
    1.29       [(+ time-base (+ (* 2 period) 2))  extensor 0]]))
    1.30    
    1.31  (def wiggle-script
    1.32 -  (mapcat gen-wiggle (repeat 4000 [:a-down :a-up])
    1.33 +  (mapcat gen-wiggle (repeat 4000 [:a-ex :a-flex])
    1.34                       (range 100 1000000 (+ 3 (* period 2)))))
    1.35  
    1.36  
    1.37 @@ -171,18 +171,49 @@
    1.38        (Math/abs)))
    1.39  
    1.40  (defn fft [nums]
    1.41 -  (.transform
    1.42 -   (FastFourierTransformer. DftNormalization/UNITARY)
    1.43 -   (double-array nums) TransformType/FORWARD))
    1.44 +  (map
    1.45 +   #(.getReal %)
    1.46 +   (.transform
    1.47 +    (FastFourierTransformer. DftNormalization/STANDARD)
    1.48 +    (double-array nums) TransformType/FORWARD)))
    1.49  
    1.50  (def indexed (partial map-indexed vector))
    1.51  
    1.52 +(defn max-indexed [s]
    1.53 +  (first (sort-by (comp - second) (indexed s))))
    1.54 +
    1.55 +
    1.56  (defn wiggling?
    1.57    "Is the worm wiggling?"
    1.58    [experiences]
    1.59 -  (map (comp first :muscle) (vector:last-n experiences 200))
    1.60 -  
    1.61 -)
    1.62 +  (let [analysis-interval 0x40]
    1.63 +    (when (> (count experiences) analysis-interval)
    1.64 +      (let [a-flex 3
    1.65 +            a-ex   2
    1.66 +            muscle-activity
    1.67 +            (map :muscle (vector:last-n experiences analysis-interval))
    1.68 +            base-activity
    1.69 +            (map #(- (% a-flex) (% a-ex)) muscle-activity)]
    1.70 +        (= 2
    1.71 +           (first
    1.72 +            (max-indexed (map #(Math/abs %) (take 20 (fft base-activity))))))))))
    1.73 +
    1.74 +        ;; (println-repl
    1.75 +        ;;  (apply format "%d  %.2f"
    1.76 +        ;;         (first (sort-by
    1.77 +        ;;                 (comp -  second)
    1.78 +        ;;                 (indexed (take 20 ))))))))))
    1.79 +
    1.80 +    ;;     (println-repl
    1.81 +    ;;      (apply
    1.82 +    ;;       format
    1.83 +    ;;       (apply str (repeat analysis-interval "%5.1f"))
    1.84 +    ;;       (fft base-activity)))
    1.85 +
    1.86 +    ;; ;;(println-repl (last base-activity)) 
    1.87 +    ;;   )))
    1.88 +
    1.89 +
    1.90  
    1.91  (def standard-world-view
    1.92    [(Vector3f. 4.207176, -3.7366982, 3.0816958)
    1.93 @@ -249,7 +280,7 @@
    1.94         (fn [world tpf]
    1.95           (if (and end-frame (> (.getTime timer) end-frame))
    1.96             (.stop world))
    1.97 -         (let [muscle-data (motor-control muscles)
    1.98 +         (let [muscle-data (vec (motor-control muscles))
    1.99                 proprioception-data (prop)
   1.100                 touch-data (map #(% (.getRootNode world)) touch)]
   1.101             (when experiences
   1.102 @@ -262,7 +293,7 @@
   1.103               ;; (println-repl
   1.104               ;;  (apply format "%.2f %.2f %.2f %.2f %.2f\n"
   1.105               ;;         (map contact touch-data)))
   1.106 -             
   1.107 +             (wiggling? @experiences)
   1.108               )
   1.109             (muscle-display
   1.110              muscle-data