forked from foxis/EasyRobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
168 lines (143 loc) · 3.41 KB
/
main.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
//go:generate tinygo flash -target=xiao -tags logless
import (
"context"
"machine"
"sync"
"time"
vaj "github.com/itohio/EasyRobot/pkg/core/math/filter/kinematics"
servos "github.com/itohio/EasyRobot/pkg/robot/actuator/servos"
fw "github.com/itohio/EasyRobot/pkg/robot/actuator/servos/fw"
"github.com/itohio/EasyRobot/pkg/robot/kinematics"
"github.com/itohio/EasyRobot/pkg/robot/transport"
)
var (
manipulatorConfig = []servos.Motor{
servos.NewMotorConfig(servos.WithPin(uint32(machine.D8))),
servos.NewMotorConfig(servos.WithPin(uint32(machine.D9))),
servos.NewMotorConfig(servos.WithPin(uint32(machine.D10))),
}
motion = []kinematics.VAJ1D{
{},
{},
{},
}
manipulator fw.Actuator
motionLock sync.Mutex
)
func blink(led machine.Pin, t time.Duration) {
for {
time.Sleep(t)
led.Set(!led.Get())
}
}
func main() {
led := machine.LED
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
uart.Configure(machine.UARTConfig{TX: tx, RX: rx})
defer blink(led, time.Millisecond*1500)
var err error
manipulator, err = fw.New(manipulatorConfig, timerMapping)
if err != nil {
return
}
ctx := context.Background()
buffer := make([]byte, 128)
var (
n int
data transport.PacketData
)
for {
//ch := transport.ReadPackets(ctx, servos.ID, uart)
for {
buffer, n, data, err = transport.ReadPacketFromReliableStream(ctx, servos.ID, uart, buffer)
if err != nil {
println("read error", err)
continue
}
updateMotion()
if n == 0 {
continue
}
switch data.Type {
case transport.PacketMotorConfig:
println("motors config")
configMotors(data)
case transport.PacketKinematicsConfig:
println("kinematics")
configKinematics(data)
case transport.PacketSetState:
println("set State")
setState(data)
}
led.Set(!led.Get())
}
println("reader failed")
}
blink(led, time.Millisecond*50)
}
func configMotors(packet transport.PacketData) {
var config servos.Config
err := config.Unmarshal(packet.Data)
if err != nil {
return
}
if len(config.Motors) != len(manipulatorConfig) {
return
}
motors := make([]servos.Motor, len(config.Motors))
for i, m := range config.Motors {
motors[i] = *m
}
motionLock.Lock()
defer motionLock.Unlock()
if err := manipulator.Configure(motors); err != nil {
return
}
motion = NewMotion(len(motors))
for i := range motors {
motion[i].Input = motors[i].Default
motion[i].Output = motors[i].Default
motion[i].Target = motors[i].Default
}
}
func NewMotion(N int) []vaj.VAJ1D {
m := make([]vaj.VAJ1D, N)
for i := range m {
m[i] = vaj.New1D(100, 100, 100)
}
return m
}
func configMotionKinematics(packet transport.PacketData) {
var cfg kinematics.Config
err := cfg.Unmarshal(packet.Data)
if err != nil {
return
}
if cfg.Motion == nil || len(cfg.Motion) != len(manipulatorConfig) {
return
}
motionLock.Lock()
defer motionLock.Unlock()
for i := range motion {
if cfg.Motion[i] == nil {
continue
}
motion[i] = vaj.New1D(cfg.Motion[i].Velocity, cfg.Motion[i].Acceleration, cfg.Motion[i].Jerk)
}
}
var now = time.Now()
func updateMotion() {
state := [32]float32{}
motionLock.Lock()
d := time.Since(now)
for i := range motion {
motion[i].Update(float32(d.Seconds()))
state[i] = motion[i].Output
}
manipulator.Set(state[:len(motion)])
println(motion[0].Input, motion[0].Output, motion[0].Target)
motionLock.Unlock()
now = time.Now()
time.Sleep(time.Millisecond * 100)
}