-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreceive.go
More file actions
191 lines (160 loc) · 4.67 KB
/
receive.go
File metadata and controls
191 lines (160 loc) · 4.67 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"log"
"net"
"time"
"github.com/Rione/ssl-RACOON-Pi2/proto/pb_gen"
"google.golang.org/protobuf/proto"
)
// ダイレクトキック判定用のしきい値
const directKickThreshold float32 = 100
// RunClient はAIからの制御コマンドを受信するUDPクライアントである
func RunClient(done <-chan struct{}, myID uint32, ip string) {
serverAddr := &net.UDPAddr{
IP: net.ParseIP(ip),
Port: UDP_RECV_PORT,
}
serverConn, err := net.ListenUDP("udp", serverAddr)
CheckError(err)
defer serverConn.Close()
buf := make([]byte, 1024)
for {
select {
case <-done:
return
default:
n, _, _ := serverConn.ReadFromUDP(buf)
lastRecvTime = time.Now()
packet := &pb_gen.GrSim_Packet{}
if err := proto.Unmarshal(buf[0:n], packet); err != nil {
log.Fatal("Error: ", err)
}
processRobotCommands(packet, myID)
}
}
}
// processRobotCommands はロボットコマンドを処理する
func processRobotCommands(packet *pb_gen.GrSim_Packet, myID uint32) {
robotCmds := packet.Commands.GetRobotCommands()
if debugReceive {
log.Printf("[AI RX] Received packet with %d robot commands", len(robotCmds))
}
for _, cmd := range robotCmds {
if cmd.GetId() != myID {
continue
}
if debugReceive {
logReceivedCommand(cmd)
}
processCommand(cmd)
}
}
// logReceivedCommand はデバッグ用にコマンドをログ出力する
func logReceivedCommand(cmd *pb_gen.GrSim_Robot_Command) {
log.Printf("[AI RX] === Robot ID: %d (Match) ===", cmd.GetId())
log.Printf("[AI RX] VelTangent: %.3f m/s, VelNormal: %.3f m/s, VelAngular: %.3f rad/s",
cmd.GetVeltangent(), cmd.GetVelnormal(), cmd.GetVelangular())
log.Printf("[AI RX] KickSpeedX: %.1f, KickSpeedZ: %.1f, Spinner: %t, Wheel1(DribblePower): %.1f",
cmd.GetKickspeedx(), cmd.GetKickspeedz(), cmd.GetSpinner(), cmd.GetWheel1())
fmt.Println("---")
}
// processCommand はロボットコマンドを処理し、送信データを構築する
func processCommand(cmd *pb_gen.GrSim_Robot_Command) {
kickSpeedX := cmd.GetKickspeedx()
kickSpeedZ := cmd.GetKickspeedz()
// ダイレクトキック判定(100以上の場合)
if kickSpeedX >= directKickThreshold {
doDirectKick = true
kickSpeedX -= directKickThreshold
}
if kickSpeedZ >= directKickThreshold {
doDirectChipKick = true
kickSpeedZ -= directKickThreshold
}
velTangent := float64(cmd.GetVeltangent())
velNormal := float64(cmd.GetVelnormal())
velAngular := float64(cmd.GetVelangular())
spinner := cmd.GetSpinner()
// キック情報をログ出力
if kickSpeedX > 0 || kickSpeedZ > 0 {
log.Printf("ID: %d, KickX: %.2f, KickZ: %.2f, VelT: %.2f, VelN: %.2f, VelA: %.2f, Spinner: %t",
cmd.GetId(), cmd.GetKickspeedx(), cmd.GetKickspeedz(), velTangent, velNormal, velAngular, spinner)
}
// 送信データを構築
bytearray := SendStruct{
preamble: 0xFF,
velx: int16(velTangent * 1000), // m/s → mm/s
vely: int16(velNormal * 1000), // m/s → mm/s
velang: int16(velAngular * 1000), // rad/s → mrad/s
}
// ドリブラー設定
if spinner {
spinnerVel := cmd.GetWheel1()
if spinnerVel > 100 {
spinnerVel = 100
} else if spinnerVel < 0 {
spinnerVel = 0
}
bytearray.dribblePower = uint8(spinnerVel)
}
// キッカー設定
if kickSpeedX > 0 {
kickerVal = uint8(kickSpeedX * 10)
kickerEnable = true
}
if kickerEnable {
bytearray.kickPower = kickerVal
}
// チップキック設定
if kickSpeedZ > 0 {
chipVal = uint8(kickSpeedZ * 10)
chipEnable = true
}
if chipEnable {
bytearray.chipPower = chipVal
}
// informationsビットフラグを設定
bytearray.informations &= ^uint8(INFO_EMG_STOP) // 緊急停止OFF
if doDirectKick {
bytearray.informations |= INFO_DIRECT_KICK
}
if doDirectChipKick {
bytearray.informations |= INFO_DIRECT_CHIP
}
bytearray.informations |= INFO_DO_CHARGE // 充電ON
// バイナリに変換
sendarray = bytes.Buffer{}
if err := binary.Write(&sendarray, binary.LittleEndian, bytearray); err != nil {
log.Fatal(err)
}
}
// ReceiveData はカメラからの画像データを受信する
func ReceiveData(done <-chan struct{}, myID uint32, ip string) {
serverAddr := &net.UDPAddr{
IP: net.ParseIP(ip),
Port: UDP_CAMERA_PORT,
}
serverConn, err := net.ListenUDP("udp", serverAddr)
CheckError(err)
defer serverConn.Close()
buf := make([]byte, 20240)
for {
select {
case <-done:
return
default:
n, _, _ := serverConn.ReadFromUDP(buf)
var jsonData ImageData
if err := json.Unmarshal(buf[0:n], &jsonData); err != nil {
log.Printf("JSON unmarshal error: %v", err)
continue
}
imageData = jsonData
imageResponse.Frame = jsonData.Frame
}
}
}