-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmotor.go
executable file
·71 lines (60 loc) · 1.61 KB
/
motor.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
package aimbotio
import (
"fmt"
)
type motor struct {
connection connection
Value motorValue
}
type motorValue struct {
Head angle
LeftArm angle
RightArm angle
Trunk angle
Speed int
}
type angle struct {
MinMax [2]int
Raw int
}
func (a *angle) cal() int {
maxraw := 100
minraw := -100
if a.Raw > maxraw {
a.Raw = maxraw
}
if a.Raw < minraw {
a.Raw = minraw
}
return (((a.MinMax[1] - a.MinMax[0]) * (a.Raw - minraw) / (maxraw - minraw)) + a.MinMax[0])
}
// Motor object initialization
func Motor() motor {
initializer := motor{}
initializer.Value.Speed = 90
initializer.connection.name = "Motor"
initializer.Value.Head.MinMax = [2]int{461, 563}
initializer.Value.LeftArm.MinMax = [2]int{512, 205}
initializer.Value.RightArm.MinMax = [2]int{512, 819}
initializer.Value.Trunk.MinMax = [2]int{205, 819}
return initializer
}
func (m *motor) Connect(SocketioPort int) {
m.connection.port = SocketioPort
connect(&m.connection)
}
func (m *motor) Set(Head int, LeftArm int, RightArm int, Trunk int) {
m.Value.Head.Raw = Head
m.Value.LeftArm.Raw = LeftArm
m.Value.RightArm.Raw = RightArm
m.Value.Trunk.Raw = Trunk
}
func (m *motor) Drive(Head int, LeftArm int, RightArm int, Trunk int) {
m.Set(Head, LeftArm, RightArm, Trunk)
moveCommand := fmt.Sprintf("{'1': %d, '2': %d, '3': %d, '4': %d, 'speed': %d}", m.Value.Head.cal(), m.Value.LeftArm.cal(), m.Value.RightArm.cal(), m.Value.Trunk.cal(), m.Value.Speed)
emit(&m.connection, "moveCommand", moveCommand)
}
func (m *motor) Disconnect() {
disconnect(&m.connection)
}
//("moveCommand", "{'4': 666, '1': 461, 'speed': 90, '3': 512, '2': 512}")