-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrollerfeedback.go
63 lines (50 loc) · 1.23 KB
/
controllerfeedback.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
package main
import (
"log"
"net"
"sync"
"github.com/Rione-SSL/RACOON-MW/proto/pb_gen"
"google.golang.org/protobuf/proto"
)
type RobotVelocity struct {
X float32
Y float32
Angular float32
}
var controllerRobotVelocitys [16]RobotVelocity
// mutex
var mutex sync.Mutex
func controllerFeedback(chctrlfb chan bool) {
serverAddr := &net.UDPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 56940,
}
serverConn, err := net.ListenUDP("udp", serverAddr)
CheckError(err)
defer serverConn.Close()
buf := make([]byte, 1024)
for {
n, _, err := serverConn.ReadFromUDP(buf)
CheckError(err)
packet := &pb_gen.GrSim_Packet{}
err = proto.Unmarshal(buf[0:n], packet)
CheckError(err)
mutex.Lock()
for i := range controllerRobotVelocitys {
controllerRobotVelocitys[i] = RobotVelocity{}
}
for _, command := range packet.Commands.RobotCommands {
if command.GetId() > 0 || command.GetId() < 16 {
controllerRobotVelocitys[command.GetId()] = RobotVelocity{
X: command.GetVeltangent(),
Y: command.GetVelnormal(),
Angular: command.GetVelangular(),
}
} else {
log.Println("[MW-Controller-Feedback] Invalid robot id: ", command.GetId())
}
}
mutex.Unlock()
}
chctrlfb <- true
}