소스 검색

pipoint: shift param and util under their own packages.

Michael Hope 8 달 전
부모
커밋
480abd4f96
17개의 변경된 파일95개의 추가작업 그리고 65개의 파일을 삭제
  1. 3 1
      audio.go
  2. 6 3
      cycle.go
  3. 5 1
      hold.go
  4. 5 1
      locate.go
  5. 5 1
      orientate.go
  6. 1 1
      param/param.go
  7. 1 1
      param/param_test.go
  8. 1 1
      param/params.go
  9. 4 3
      param/params_mqtt.go
  10. 1 1
      param/params_test.go
  11. 41 38
      pipoint.go
  12. 6 3
      run.go
  13. 8 5
      servo.go
  14. 5 2
      servo_test.go
  15. 1 1
      util/limiter.go
  16. 1 1
      util/util.go
  17. 1 1
      util/util_test.go

+ 3 - 1
audio.go 파일 보기

@@ -19,6 +19,8 @@ import (
19 19
 	"fmt"
20 20
 	"os"
21 21
 	"os/exec"
22
+
23
+	"juju.net.nz/x/pipoint/util"
22 24
 )
23 25
 
24 26
 // AudioOut can play files or speech.
@@ -42,7 +44,7 @@ func (a *AudioOut) Play(path string) {
42 44
 
43 45
 // Say plays a pre-recorded phrase, or falls back to espeak.
44 46
 func (a *AudioOut) Say(text string) {
45
-	rendered := fmt.Sprintf("phrase/%s.ogg", NormText(text))
47
+	rendered := fmt.Sprintf("phrase/%s.ogg", util.NormText(text))
46 48
 	fi, err := os.Stat(rendered)
47 49
 
48 50
 	if err == nil && fi.Mode().IsRegular() {

+ 6 - 3
cycle.go 파일 보기

@@ -17,6 +17,9 @@ package pipoint
17 17
 
18 18
 import (
19 19
 	"math"
20
+
21
+	"juju.net.nz/x/pipoint/param"
22
+	"juju.net.nz/x/pipoint/util"
20 23
 )
21 24
 
22 25
 // CycleState cycles the pan/tilt in a circle.
@@ -30,11 +33,11 @@ func (s *CycleState) Name() string {
30 33
 }
31 34
 
32 35
 // Update reacts to changes in parameters.
33
-func (s *CycleState) Update(param *Param) {
36
+func (s *CycleState) Update(param *param.Param) {
34 37
 	switch param {
35 38
 	case s.pi.tick:
36 39
 		s.cycle += 0.02
37
-		s.pi.pan.Set(Scale(math.Cos(s.cycle), -1, 1, -math.Pi/2, math.Pi/2))
38
-		s.pi.tilt.Set(Scale(math.Sin(s.cycle), -1, 1, -math.Pi/2, 0))
40
+		s.pi.pan.Set(util.Scale(math.Cos(s.cycle), -1, 1, -math.Pi/2, math.Pi/2))
41
+		s.pi.tilt.Set(util.Scale(math.Sin(s.cycle), -1, 1, -math.Pi/2, 0))
39 42
 	}
40 43
 }

+ 5 - 1
hold.go 파일 보기

@@ -15,6 +15,10 @@
15 15
 
16 16
 package pipoint
17 17
 
18
+import (
19
+	"juju.net.nz/x/pipoint/param"
20
+)
21
+
18 22
 // HoldState executes when the camera is tracking the rover.
19 23
 type HoldState struct {
20 24
 	pi *PiPoint
@@ -25,7 +29,7 @@ func (s *HoldState) Name() string {
25 29
 }
26 30
 
27 31
 // Update is called when a param is updated.
28
-func (s *HoldState) Update(param *Param) {
32
+func (s *HoldState) Update(param *param.Param) {
29 33
 	switch param {
30 34
 	case s.pi.mark:
31 35
 		s.pi.state.Dec()

+ 5 - 1
locate.go 파일 보기

@@ -15,6 +15,10 @@
15 15
 
16 16
 package pipoint
17 17
 
18
+import (
19
+	"juju.net.nz/x/pipoint/param"
20
+)
21
+
18 22
 // LocateState runs initially to locate the base unit.
19 23
 type LocateState struct {
20 24
 	name string
@@ -26,7 +30,7 @@ func (s *LocateState) Name() string {
26 30
 }
27 31
 
28 32
 // Update is called when a param is updated.
29
-func (s *LocateState) Update(param *Param) {
33
+func (s *LocateState) Update(param *param.Param) {
30 34
 	switch param {
31 35
 	case s.pi.neu:
32 36
 		s.pi.rover.Set(param.Get())

+ 5 - 1
orientate.go 파일 보기

@@ -15,6 +15,10 @@
15 15
 
16 16
 package pipoint
17 17
 
18
+import (
19
+	"juju.net.nz/x/pipoint/param"
20
+)
21
+
18 22
 // OrientateState runs to set the pan orientation.
19 23
 type OrientateState struct {
20 24
 	name string
@@ -26,7 +30,7 @@ func (s *OrientateState) Name() string {
26 30
 }
27 31
 
28 32
 // Update is called when a param is updated.
29
-func (s *OrientateState) Update(param *Param) {
33
+func (s *OrientateState) Update(param *param.Param) {
30 34
 	switch param {
31 35
 	case s.pi.neu:
32 36
 		s.pi.rover.Set(param.Get())

param.go → param/param.go 파일 보기

@@ -13,7 +13,7 @@
13 13
 // limitations under the License.
14 14
 //
15 15
 
16
-package pipoint
16
+package param
17 17
 
18 18
 import (
19 19
 	"errors"

param_test.go → param/param_test.go 파일 보기

@@ -13,7 +13,7 @@
13 13
 // limitations under the License.
14 14
 //
15 15
 
16
-package pipoint
16
+package param
17 17
 
18 18
 import (
19 19
 	"testing"

params.go → param/params.go 파일 보기

@@ -13,7 +13,7 @@
13 13
 // limitations under the License.
14 14
 //
15 15
 
16
-package pipoint
16
+package param
17 17
 
18 18
 import (
19 19
 	"bytes"

params_mqtt.go → param/params_mqtt.go 파일 보기

@@ -13,7 +13,7 @@
13 13
 // limitations under the License.
14 14
 //
15 15
 
16
-package pipoint
16
+package param
17 17
 
18 18
 import (
19 19
 	"fmt"
@@ -23,6 +23,7 @@ import (
23 23
 	"strings"
24 24
 
25 25
 	"gobot.io/x/gobot/platforms/mqtt"
26
+	"juju.net.nz/x/pipoint/util"
26 27
 )
27 28
 
28 29
 const (
@@ -34,7 +35,7 @@ type ParamMQTTBridge struct {
34 35
 	params    *Params
35 36
 	adaptor   *mqtt.Adaptor
36 37
 	prefix    string
37
-	limiter   *Limiter
38
+	limiter   *util.Limiter
38 39
 	listening bool
39 40
 }
40 41
 
@@ -50,7 +51,7 @@ func NewParamMQTTBridge(params *Params, adaptor *mqtt.Adaptor, device string) *P
50 51
 		params:  params,
51 52
 		adaptor: adaptor,
52 53
 		prefix:  prefix,
53
-		limiter: NewLimiter(),
54
+		limiter: util.NewLimiter(),
54 55
 	}
55 56
 	changed := make(chan *Param, 10)
56 57
 	params.Listen(changed)

params_test.go → param/params_test.go 파일 보기

@@ -13,7 +13,7 @@
13 13
 // limitations under the License.
14 14
 //
15 15
 
16
-package pipoint
16
+package param
17 17
 
18 18
 import (
19 19
 	"reflect"

+ 41 - 38
pipoint.go 파일 보기

@@ -19,6 +19,9 @@ import (
19 19
 	"log"
20 20
 	"time"
21 21
 
22
+	"juju.net.nz/x/pipoint/param"
23
+	"juju.net.nz/x/pipoint/util"
24
+
22 25
 	common "gobot.io/x/gobot/platforms/mavlink/common"
23 26
 	"gobot.io/x/gobot/platforms/mqtt"
24 27
 )
@@ -36,39 +39,39 @@ var (
36 39
 // State is a handler for the current state of the system.
37 40
 type State interface {
38 41
 	Name() string
39
-	Update(param *Param)
42
+	Update(param *param.Param)
40 43
 }
41 44
 
42 45
 // PiPoint is an automatic, GPS based system that points a camera at the rover.
43 46
 type PiPoint struct {
44
-	Params *Params
45
-
46
-	state *Param
47
-
48
-	version    *Param
49
-	tick       *Param
50
-	seconds    *Param
51
-	messages   *Param
52
-	heartbeats *Param
53
-	heartbeat  *Param
54
-	attitude   *Param
55
-	gps        *Param
56
-	gpsFix     *Param
57
-	neu        *Param
58
-	baseOffset *Param
59
-	pred       *Param
60
-	rover      *Param
61
-	base       *Param
62
-	sysStatus  *Param
63
-	link       *Param
47
+	Params *param.Params
48
+
49
+	state *param.Param
50
+
51
+	version    *param.Param
52
+	tick       *param.Param
53
+	seconds    *param.Param
54
+	messages   *param.Param
55
+	heartbeats *param.Param
56
+	heartbeat  *param.Param
57
+	attitude   *param.Param
58
+	gps        *param.Param
59
+	gpsFix     *param.Param
60
+	neu        *param.Param
61
+	baseOffset *param.Param
62
+	pred       *param.Param
63
+	rover      *param.Param
64
+	base       *param.Param
65
+	sysStatus  *param.Param
66
+	link       *param.Param
64 67
 	linkLast   int
65
-	remote     *Param
66
-	command    *Param
67
-	mark       *Param
68
-	vel        *Param
68
+	remote     *param.Param
69
+	command    *param.Param
70
+	mark       *param.Param
71
+	vel        *param.Param
69 72
 
70
-	sp     *Param
71
-	offset *Param
73
+	sp     *param.Param
74
+	offset *param.Param
72 75
 
73 76
 	pan  *Servo
74 77
 	tilt *Servo
@@ -82,7 +85,7 @@ type PiPoint struct {
82 85
 	elog *EventLogger
83 86
 	log  *log.Logger
84 87
 
85
-	param ParamChannel
88
+	param param.ParamChannel
86 89
 
87 90
 	audio *AudioOut
88 91
 }
@@ -90,12 +93,12 @@ type PiPoint struct {
90 93
 // NewPiPoint creates a new camera pointer.
91 94
 func NewPiPoint() *PiPoint {
92 95
 	p := &PiPoint{
93
-		Params:  NewParams("pipoint"),
96
+		Params:  param.NewParams("pipoint"),
94 97
 		latPred: &LinPred{},
95 98
 		lonPred: &LinPred{},
96 99
 		altPred: &LinPred{},
97 100
 		elog:    NewEventLogger("pipoint"),
98
-		param:   make(ParamChannel, 10),
101
+		param:   make(param.ParamChannel, 10),
99 102
 		audio:   NewAudioOut(),
100 103
 	}
101 104
 
@@ -150,7 +153,7 @@ func NewPiPoint() *PiPoint {
150 153
 // AddMQTT adds a new MQTT connection that bridges between MQTT and
151 154
 // params.
152 155
 func (pi *PiPoint) AddMQTT(mqtt *mqtt.Adaptor) {
153
-	NewParamMQTTBridge(pi.Params, mqtt, "")
156
+	param.NewParamMQTTBridge(pi.Params, mqtt, "")
154 157
 }
155 158
 
156 159
 // Run is the main entry point that runs forever.
@@ -170,7 +173,7 @@ func (pi *PiPoint) Run() {
170 173
 }
171 174
 
172 175
 func (pi *PiPoint) ticked() {
173
-	now := Now()
176
+	now := util.Now()
174 177
 	pi.tick.SetFloat64(now)
175 178
 	pi.seconds.UpdateInt(int(now))
176 179
 
@@ -199,7 +202,7 @@ func (pi *PiPoint) predict(gps *Position) {
199 202
 	pi.altPred.SetEx(gps.Alt, now, gps.Time)
200 203
 }
201 204
 
202
-func (pi *PiPoint) update(param *Param) {
205
+func (pi *PiPoint) update(param *param.Param) {
203 206
 	if state := pi.getState(); state != nil {
204 207
 		state.Update(param)
205 208
 	}
@@ -216,7 +219,7 @@ func (pi *PiPoint) getState() State {
216 219
 	return nil
217 220
 }
218 221
 
219
-func (pi *PiPoint) announce(param *Param) {
222
+func (pi *PiPoint) announce(param *param.Param) {
220 223
 	switch param {
221 224
 	case pi.state:
222 225
 		if state := pi.getState(); state != nil {
@@ -271,12 +274,12 @@ func (pi *PiPoint) Message(msg interface{}) {
271 274
 	case *common.RcChannels:
272 275
 		remote := msg.(*common.RcChannels)
273 276
 		att := &Attitude{
274
-			Roll:  ServoToScale(remote.CHAN1_RAW),
275
-			Pitch: ServoToScale(remote.CHAN2_RAW),
276
-			Yaw:   ServoToScale(remote.CHAN4_RAW),
277
+			Roll:  util.ServoToScale(remote.CHAN1_RAW),
278
+			Pitch: util.ServoToScale(remote.CHAN2_RAW),
279
+			Yaw:   util.ServoToScale(remote.CHAN4_RAW),
277 280
 		}
278 281
 		pi.remote.Set(att)
279
-		command := ScaleToPos(att.Yaw)
282
+		command := util.ScaleToPos(att.Yaw)
280 283
 		if changed, _ := pi.command.UpdateInt(command); changed {
281 284
 			if command == -2 {
282 285
 				pi.mark.Inc()

+ 6 - 3
run.go 파일 보기

@@ -19,6 +19,9 @@ import (
19 19
 	"fmt"
20 20
 	"log"
21 21
 	"math"
22
+
23
+	"juju.net.nz/x/pipoint/param"
24
+	"juju.net.nz/x/pipoint/util"
22 25
 )
23 26
 
24 27
 // RunState executes when the camera is tracking the rover.
@@ -31,7 +34,7 @@ func (s *RunState) Name() string {
31 34
 }
32 35
 
33 36
 // Update is called when a param is updated.
34
-func (s *RunState) Update(param *Param) {
37
+func (s *RunState) Update(param *param.Param) {
35 38
 	switch param {
36 39
 	case s.pi.neu:
37 40
 		s.pi.rover.Set(param.Get())
@@ -65,8 +68,8 @@ func (s *RunState) Update(param *Param) {
65 68
 	}
66 69
 
67 70
 	offset := s.pi.offset.Get().(*Attitude)
68
-	s.pi.pan.Set(WrapAngle(att.Yaw + offset.Yaw))
69
-	s.pi.tilt.Set(WrapAngle(att.Pitch + offset.Pitch))
71
+	s.pi.pan.Set(util.WrapAngle(att.Yaw + offset.Yaw))
72
+	s.pi.tilt.Set(util.WrapAngle(att.Pitch + offset.Pitch))
70 73
 }
71 74
 
72 75
 func point(rover, base, offset *NEUPosition) (*Attitude, error) {

+ 8 - 5
servo.go 파일 보기

@@ -17,6 +17,9 @@ package pipoint
17 17
 
18 18
 import (
19 19
 	"math"
20
+
21
+	"juju.net.nz/x/pipoint/param"
22
+	"juju.net.nz/x/pipoint/util"
20 23
 )
21 24
 
22 25
 // ServoParams holds the parameters for a servo including limits.
@@ -35,15 +38,15 @@ type ServoParams struct {
35 38
 // Servo is a servo on a pin with limits, demand, and actual
36 39
 // position.
37 40
 type Servo struct {
38
-	params *Param
39
-	sp     *Param
40
-	pv     *Param
41
+	params *param.Param
42
+	sp     *param.Param
43
+	pv     *param.Param
41 44
 	pwm    *ServoBlaster
42 45
 	filter *Lowpass
43 46
 }
44 47
 
45 48
 // NewServo creates a new servo with params on the given tree.
46
-func NewServo(name string, params *Params) *Servo {
49
+func NewServo(name string, params *param.Params) *Servo {
47 50
 	s := &Servo{
48 51
 		params: params.NewWith(name, &ServoParams{
49 52
 			Pin:  -1,
@@ -78,7 +81,7 @@ func (s *Servo) Tick() {
78 81
 	// Convert to pulse width.
79 82
 	angle += math.Pi / 2
80 83
 
81
-	ms := Scale(angle, 0, params.Span, params.Low, params.High)
84
+	ms := util.Scale(angle, 0, params.Span, params.Low, params.High)
82 85
 	ms = math.Min(params.Max, math.Max(params.Min, ms))
83 86
 	s.pv.SetFloat64(ms)
84 87
 

+ 5 - 2
servo_test.go 파일 보기

@@ -19,14 +19,17 @@ import (
19 19
 	"math"
20 20
 	"testing"
21 21
 
22
+	"juju.net.nz/x/pipoint/param"
23
+	"juju.net.nz/x/pipoint/util"
24
+
22 25
 	"github.com/stretchr/testify/assert"
23 26
 )
24 27
 
25 28
 func TestServo(t *testing.T) {
26
-	p := &Params{}
29
+	p := &param.Params{}
27 30
 	s := NewServo("pan", p)
28 31
 
29
-	OverrideNow(1)
32
+	util.OverrideNow(1)
30 33
 
31 34
 	assert.InDelta(t, s.sp.GetFloat64(), 0, 0.001, "Starts at zero")
32 35
 

limiter.go → util/limiter.go 파일 보기

@@ -13,7 +13,7 @@
13 13
 // limitations under the License.
14 14
 //
15 15
 
16
-package pipoint
16
+package util
17 17
 
18 18
 // Limiter is a simple keyed rate limiter.
19 19
 type Limiter struct {

util.go → util/util.go 파일 보기

@@ -13,7 +13,7 @@
13 13
 // limitations under the License.
14 14
 //
15 15
 
16
-package pipoint
16
+package util
17 17
 
18 18
 import (
19 19
 	"crypto/sha1"

util_test.go → util/util_test.go 파일 보기

@@ -13,7 +13,7 @@
13 13
 // limitations under the License.
14 14
 //
15 15
 
16
-package pipoint
16
+package util
17 17
 
18 18
 import (
19 19
 	"testing"