-
Notifications
You must be signed in to change notification settings - Fork 1
/
active-object.go
80 lines (70 loc) · 2.58 KB
/
active-object.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"fmt"
"math"
"time"
)
// type for input function, k.
// input is duration since an arbitrary start time t0.
type tFunc func(time.Duration) float64
// active integrator object. state variables are not here, but in
// function aif, started as a goroutine in the constructor.
type aio struct {
iCh chan tFunc // channel for setting input function
oCh chan chan float64 // channel for requesting output
}
// constructor
func newAio() *aio {
var a aio
a.iCh = make(chan tFunc)
a.oCh = make(chan chan float64)
go aif(&a)
return &a
}
// input method required by task description. in practice, this method is
// unnecessary; you would just put that single channel send statement in
// your code wherever you wanted to set the input function.
func (a aio) input(f tFunc) {
a.iCh <- f
}
// output method required by task description. in practice, this method too
// would not likely be best. instead any client interested in the value would
// likely make a return channel sCh once, and then reuse it as needed.
func (a aio) output() float64 {
sCh := make(chan float64)
a.oCh <- sCh
return <-sCh
}
// integration function that returns constant 0
func zeroFunc(time.Duration) float64 { return 0 }
// goroutine serializes access to integrated function k and state variable s
func aif(a *aio) {
var k tFunc = zeroFunc // integration function
s := 0. // "object state" initialized to 0
t0 := time.Now() // initial time
k0 := k(0) // initial sample value
t1 := t0 // t1, k1 used for trapezoid formula
k1 := k0
tk := time.Tick(10 * time.Millisecond) // 10 ms -> 100 Hz
for {
select {
case t2 := <-tk: // timer tick event
k2 := k(t2.Sub(t0)) // new sample value
s += (k1 + k2) * .5 * t2.Sub(t1).Seconds() // trapezoid formula
t1, k1 = t2, k2 // save time and value
case k = <-a.iCh: // input method event: function change
case sCh := <-a.oCh: // output method event: sample object state
sCh <- s
}
}
}
func main() {
a := newAio() // create object
a.input(func(t time.Duration) float64 { // 1. set input to sin function
return math.Sin(t.Seconds() * math.Pi)
})
time.Sleep(2 * time.Second) // 2. sleep 2 sec
a.input(zeroFunc) // 3. set input to zero function
time.Sleep(time.Second / 2) // 4. sleep .5 sec
fmt.Println(a.output()) // output should be near zero
}