-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
112 lines (97 loc) · 2.99 KB
/
config.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package observerconfig
import (
"image"
"github.com/chutommy/observer/config"
"github.com/chutommy/observer/geometry"
)
// ObserverConfig represents a configuration of the Observer's runtime.
type ObserverConfig struct {
MidPoint image.Point
MidRect image.Rectangle
ServoX *Servo
ServoY *Servo
Colors *Colors
Show bool
CalibrateOnStart bool
Haar []string
MaxIdleDuration float64
MaxFPS int
Period int
}
// LoadObserverConfig generates an ObserverConfig and loads required attributes.
func LoadObserverConfig(cfg *config.Config) *ObserverConfig {
rc := &ObserverConfig{}
rc.loadMidPoint(cfg)
rc.loadMidRect(cfg)
rc.loadServoX(cfg)
rc.loadServoY(cfg)
rc.loadColors(cfg)
rc.loadGeneral(cfg)
return rc
}
// loadMidPoint sets MidPoint.
func (rc *ObserverConfig) loadMidPoint(cfg *config.Config) {
frame := cfg.Camera.Frame
rc.MidPoint = image.Point{
X: frame.Width / 2,
Y: frame.Height / 2,
}
}
// loadMidRect sets MidRect.
func (rc *ObserverConfig) loadMidRect(cfg *config.Config) {
half := cfg.Targeting.AimArea / 2
wh := float64(cfg.Camera.Frame.Width) * half
minPoint := image.Point{
X: int(float64(rc.MidPoint.X) - wh),
Y: int(float64(rc.MidPoint.Y) - wh),
}
maxPoint := image.Point{
X: int(float64(rc.MidPoint.X) + wh),
Y: int(float64(rc.MidPoint.Y) + wh),
}
rc.MidRect = image.Rectangle{
Min: minPoint,
Max: maxPoint,
}
}
// loadServoX sets ServoX.
func (rc *ObserverConfig) loadServoX(cfg *config.Config) {
rc.ServoX = &Servo{
Pin: int64(cfg.Servos.PinX),
Calibration: cfg.Calibration.Coefficient.X,
Inverted: cfg.Calibration.Invert.X,
MidPoint: cfg.Camera.Frame.Width / 2,
Toleration: (float64(rc.MidRect.Dx()) / 2) * cfg.Calibration.Tolerate.X,
PxsPerDegree: float64(cfg.Camera.Frame.Width) / cfg.Camera.AngleOfView.Horizontal,
}
}
// loadServoY sets ServoY.
func (rc *ObserverConfig) loadServoY(cfg *config.Config) {
rc.ServoY = &Servo{
Pin: int64(cfg.Servos.PinY),
Calibration: cfg.Calibration.Coefficient.Y,
Inverted: cfg.Calibration.Invert.Y,
MidPoint: cfg.Camera.Frame.Height / 2,
Toleration: (float64(rc.MidRect.Dy()) / 2) * cfg.Calibration.Tolerate.Y,
PxsPerDegree: float64(cfg.Camera.Frame.Height) / cfg.Camera.AngleOfView.Vertical,
}
}
// loadColors sets Colors.
func (rc *ObserverConfig) loadColors(cfg *config.Config) {
color := cfg.Targeting.Color
t, o, m := color.Target, color.Other, color.MidRect
rc.Colors = &Colors{
Target: geometry.NewColor(t.Red, t.Green, t.Blue, t.Thickness),
Other: geometry.NewColor(o.Red, o.Green, o.Blue, o.Thickness),
MidRect: geometry.NewColor(m.Red, m.Green, m.Blue, m.Thickness),
}
}
// loadGeneral sets general variables.
func (rc *ObserverConfig) loadGeneral(cfg *config.Config) {
rc.Show = cfg.General.Show
rc.CalibrateOnStart = cfg.Calibration.CalibrateOnStart
rc.Haar = cfg.Targeting.Cascades
rc.MaxIdleDuration = cfg.General.IdleDuration
rc.MaxFPS = cfg.Camera.MaxFPS
rc.Period = cfg.General.Period
}