added 'multiplier' field to all records in register

multiplier is a factor by which to multiply values
being pushed from the robot brain through
a register interface to a robot field, and
likewise by which to divide values being pulled from
a robot field to the robot brain
This commit is contained in:
Richard Harrington 2013-08-11 23:33:31 -04:00
parent 50ddd8d2b7
commit 9fc3a06e7e
3 changed files with 25 additions and 23 deletions

View File

@ -39,18 +39,20 @@
; rounded to an integer
{:read-register
(fn [this world]
(Math/round (get-in
world
(conj (path-to-robot (:robot-idx this)) (:field-name this)))))})
(Math/round (/ (get-in
world
(conj (path-to-robot (:robot-idx this)) (:field-name this)))
(:multiplier this))))})
(def robot-field-write-mixin
; returns a world with the value of a field in the robot hash map altered
; (with the number being cast to floating point before being pushed)
{:write-register (fn [this world data]
(assoc-in
world
(conj (path-to-robot (:robot-idx this)) (:field-name this))
(float data)))})
{:write-register
(fn [this world data]
(assoc-in
world
(conj (path-to-robot (:robot-idx this)) (:field-name this))
(float (* data (:multiplier this)))))})
(def no-op-write-mixin
; returns a world with nothing changed
@ -67,12 +69,12 @@
IReadRegister register-field-read-mixin
IWriteRegister register-field-write-mixin)
(defrecord ReadWriteRobotFieldRegister [robot-idx field-name])
(defrecord ReadWriteRobotFieldRegister [robot-idx field-name multiplier])
(extend ReadWriteRobotFieldRegister
IReadRegister robot-field-read-mixin
IWriteRegister robot-field-write-mixin)
(defrecord ReadOnlyRobotFieldRegister [robot-idx field-name])
(defrecord ReadOnlyRobotFieldRegister [robot-idx field-name multiplier])
(extend ReadOnlyRobotFieldRegister
IReadRegister robot-field-read-mixin
IWriteRegister no-op-write-mixin)
@ -114,18 +116,18 @@
the appropriate acceleration, which may have to applied over several ticks."
[robot-idx]
(let [storage-registers (for [reg-name [ "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L"
"M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "Z"]]
"M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "Z"]]
{reg-name (->StorageRegister robot-idx reg-name 0)})
read-only-registers (for [[reg-name robot-field] [["X" :pos-x]
["Y" :pos-y]
["DAMAGE" :damage]]]
{reg-name (->ReadOnlyRobotFieldRegister robot-idx robot-field)})
read-only-registers (for [[reg-name robot-field mult] [["X" :pos-x 1.0]
["Y" :pos-y 1.0]
["DAMAGE" :damage 1.0]]]
{reg-name (->ReadOnlyRobotFieldRegister robot-idx robot-field mult)})
; TODO: change reading from these registers into an error, instead of just a wasted
; processor cyle for the robot.
read-write-registers (for [[reg-name robot-field] [["AIM" :aim]
["SPEEDX" :desired-v-x]
["SPEEDY" :desired-v-y]]]
{reg-name (->ReadWriteRobotFieldRegister robot-idx robot-field)})]
read-write-registers (for [[reg-name robot-field mult] [["AIM" :aim 1.0]
["SPEEDX" :desired-v-x 0.1]
["SPEEDY" :desired-v-y 0.1]]]
{reg-name (->ReadWriteRobotFieldRegister robot-idx robot-field mult)})]
(into {} (concat storage-registers
read-only-registers
read-write-registers

View File

@ -4,7 +4,7 @@
; MAX_ACCEL is in decimeters per second per second.
; TODO: should be passed in from some higher level module, or a config module.
(def MAX_ACCEL 40.0)
(def MAX_ACCEL 4.0)
; yay classical mechanics
@ -12,12 +12,12 @@
[vi vf a]
(let [v-diff (- vf vi)]
(if (zero? v-diff)
0
0.0
(double (/ v-diff a)))))
(defn d-with-constant-a
[d vi a t]
(+ d (* vi t) (/ (* a (Math/pow t 2)) 2.0)))
(+ d (* vi t) (/ (* a (Math/pow t 2)) 2)))
(defn v-with-constant-a
[vi a t]

View File

@ -67,4 +67,4 @@
(is (= (read-register (new-registers "SPEEDX") new-world)
90))
(is (= (get-in new-world [:robots 0 :desired-v-x])
90.0)))))
9.0)))))