-
Notifications
You must be signed in to change notification settings - Fork 9
/
client.go
408 lines (338 loc) · 9.23 KB
/
client.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package robomaster
import (
"fmt"
"log/slog"
"reflect"
"sync"
"time"
"github.com/brunoga/robomaster/module"
"github.com/brunoga/robomaster/module/camera"
"github.com/brunoga/robomaster/module/chassis"
"github.com/brunoga/robomaster/module/connection"
"github.com/brunoga/robomaster/module/controller"
"github.com/brunoga/robomaster/module/gamepad"
"github.com/brunoga/robomaster/module/gimbal"
"github.com/brunoga/robomaster/module/gun"
"github.com/brunoga/robomaster/module/robot"
"github.com/brunoga/robomaster/module/sdcard"
"github.com/brunoga/robomaster/support/logger"
"github.com/brunoga/robomaster/unitybridge"
"github.com/brunoga/robomaster/unitybridge/wrapper"
)
type Client struct {
l *logger.Logger
ub unitybridge.UnityBridge
connectionModule *connection.Connection
cameraModule *camera.Module
sdCardModule *sdcard.Module
chassisModule *chassis.Chassis
gimbalModule *gimbal.Gimbal
robotModule *robot.Robot
gunModule *gun.Gun
gamePadModule *gamepad.GamePad
controllerModule *controller.Controller
m sync.RWMutex
started bool
}
// New creates a new Client instance with the given logger and appID. The appID
// parameter is used to determine which robot to connect to (i.e. it will only
// connect to robots broadcasting the given appID). If appID is 0, the client
// will connect to the first robot it finds.
//
// To get a robot to broadcast a given appID, use a QRCode to configure it (see
// https://github.com/brunoga/robomaster/unitybridge/blob/main/support/qrcode/qrcode.go).
func New(l *logger.Logger, appID uint64) (*Client, error) {
return new(l, appID, connection.TypeRouter, module.TypeAll)
}
// NewWithModules is like New but allows selecting which mkodules to enable.
// The Connection and Robot modules are required.
func NewWithModules(l *logger.Logger, appID uint64,
modules module.Type) (*Client, error) {
return new(l, appID, connection.TypeRouter, modules)
}
// NewWifiDirect creates a new Client instance with the given logger. This
// client will connect to the robot using WiFi Direct.
func NewWifiDirect(l *logger.Logger) (*Client, error) {
return new(l, 0, connection.TypeWiFiDirect, module.TypeAllButGamePad)
}
func NewWifiDirectWithModules(l *logger.Logger,
modules module.Type) (*Client, error) {
return new(l, 0, connection.TypeWiFiDirect, modules)
}
// Start starts the client and all associated modules.
func (c *Client) Start() error {
c.m.Lock()
defer c.m.Unlock()
if c.started {
return fmt.Errorf("client already started")
}
err := c.ub.Start()
if err != nil {
return err
}
// Start modules.
waitTimeout := 10 * time.Second
// Connection.
err = c.changeStateIfNonNil(c.connectionModule, waitTimeout, true)
if err != nil {
return err
}
// Robot.
err = c.changeStateIfNonNil(c.robotModule, waitTimeout, true)
if err != nil {
return err
}
// Wait for devices to be available.
if !c.robotModule.WaitForDevices(waitTimeout) {
return fmt.Errorf("robot working devices unexpectedly not established")
}
// Controller.
err = c.changeStateIfNonNil(c.controllerModule, waitTimeout, true)
if err != nil {
return err
}
// Camera.
err = c.changeStateIfNonNil(c.cameraModule, waitTimeout, true)
if err != nil {
return err
}
// SDCard.
err = c.changeStateIfNonNil(c.sdCardModule, waitTimeout, true)
if err != nil {
return err
}
// Chassis.
err = c.changeStateIfNonNil(c.chassisModule, waitTimeout, true)
if err != nil {
return err
}
// Gimbal.
err = c.changeStateIfNonNil(c.gimbalModule, waitTimeout, true)
if err != nil {
return err
}
// Gun.
go func() {
err = c.changeStateIfNonNil(c.gunModule, waitTimeout, true)
if err != nil {
if err.Error() == "Gun connection not established" {
// Gun is optional so it is fine it did not connect.
c.l.Warn("Gun connection not established.")
} else {
c.l.Error("Gun connection error", "error", err)
}
}
}()
// GamePad.
go func() {
err = c.changeStateIfNonNil(c.gamePadModule, waitTimeout, true)
if err != nil {
if err.Error() == "GamePad connection not established" {
// GamePad is optional so it is fine it did not connect.
c.l.Warn("GamePad connection not established.")
} else {
c.l.Error("GamePad connection error", "error", err)
}
}
}()
c.started = true
return nil
}
// Connection returns the Connection module.
func (c *Client) Connection() *connection.Connection {
return c.connectionModule
}
// Camera returns the Camera module.
func (c *Client) Camera() *camera.Module {
return c.cameraModule
}
// Chassis returns the Chassis module.
func (c *Client) Chassis() *chassis.Chassis {
return c.chassisModule
}
// Gimbal returns the Gimbal module.
func (c *Client) Gimbal() *gimbal.Gimbal {
return c.gimbalModule
}
// Robot returns the Robot module.
func (c *Client) Robot() *robot.Robot {
return c.robotModule
}
// Gun returns the Gun module.
func (c *Client) Gun() *gun.Gun {
return c.gunModule
}
// GamePad returns the GamePad module. The GamePad is optional and may be nil.
func (c *Client) GamePad() *gamepad.GamePad {
return c.gamePadModule
}
// Controller returns the Controller module.
func (c *Client) Controller() *controller.Controller {
return c.controllerModule
}
// SDCard returns the SDCard module. The SDCard is optional and may be nil.
func (c *Client) SDCard() *sdcard.Module {
return c.sdCardModule
}
// Stop stops the client and all associated modules.
func (c *Client) Stop() error {
c.m.Lock()
defer c.m.Unlock()
if !c.started {
return fmt.Errorf("client not started")
}
// Stop modules.
waitTime := 5 * time.Second
// Gamepad.
err := c.changeStateIfNonNil(c.gamePadModule, waitTime, false)
if err != nil {
return err
}
// Gun.
err = c.changeStateIfNonNil(c.gunModule, waitTime, false)
if err != nil {
return err
}
// Chassis.
err = c.changeStateIfNonNil(c.chassisModule, waitTime, false)
if err != nil {
return err
}
// SDCard.
err = c.changeStateIfNonNil(c.sdCardModule, waitTime, false)
if err != nil {
return err
}
// Camera.
err = c.changeStateIfNonNil(c.cameraModule, waitTime, false)
if err != nil {
return err
}
// Controller.
err = c.changeStateIfNonNil(c.controllerModule, waitTime, false)
if err != nil {
return err
}
// Robot.
err = c.changeStateIfNonNil(c.robotModule, waitTime, false)
if err != nil {
return err
}
// Connection.
err = c.changeStateIfNonNil(c.connectionModule, waitTime, false)
if err != nil {
return err
}
// Stop Unity Bridge.
err = c.ub.Stop()
if err != nil {
return err
}
c.started = false
return nil
}
func new(l *logger.Logger, appID uint64, typ connection.Type,
modules module.Type) (*Client, error) {
if l == nil {
l = logger.New(slog.LevelError)
}
if modules&module.TypeConnection == 0 {
return nil, fmt.Errorf("connection module is required")
}
if modules&module.TypeRobot == 0 {
return nil, fmt.Errorf("robot module is required")
}
// Enable Unity Bridge debug logging if the logger level is trace.
unityBridgeDebugEnabled := l.Level() <= slog.LevelDebug
ub := unitybridge.Get(wrapper.Get(l), unityBridgeDebugEnabled, l)
connectionModule, err := connection.New(ub, l, appID, typ)
if err != nil {
return nil, err
}
robotModule, err := robot.New(ub, l, connectionModule)
if err != nil {
return nil, err
}
var cameraModule *camera.Module
if modules&module.TypeCamera != 0 {
cameraModule, err = camera.New(ub, l, connectionModule)
if err != nil {
return nil, err
}
}
var sdCardModule *sdcard.Module
if modules&module.TypeSDCard != 0 {
sdCardModule, err = sdcard.New(ub, l, connectionModule, cameraModule)
if err != nil {
return nil, err
}
}
var chassisModule *chassis.Chassis
if modules&module.TypeChassis != 0 {
chassisModule, err = chassis.New(ub, l, connectionModule, robotModule)
if err != nil {
return nil, err
}
}
var gimbalModule *gimbal.Gimbal
if modules&module.TypeGimbal != 0 {
gimbalModule, err = gimbal.New(ub, l, connectionModule)
if err != nil {
return nil, err
}
}
var gunModule *gun.Gun
if modules&module.TypeGun != 0 {
gunModule, err = gun.New(ub, l, connectionModule, robotModule)
if err != nil {
return nil, err
}
}
var gamePadModule *gamepad.GamePad
if modules&module.TypeGamePad != 0 {
gamePadModule, err = gamepad.New(ub, l, connectionModule)
if err != nil {
return nil, err
}
}
var controllerModule *controller.Controller
if modules&module.TypeController != 0 {
controllerModule, err = controller.New(ub, l, connectionModule)
if err != nil {
return nil, err
}
}
return &Client{
ub: ub,
l: l,
connectionModule: connectionModule,
robotModule: robotModule,
cameraModule: cameraModule,
sdCardModule: sdCardModule,
gimbalModule: gimbalModule,
chassisModule: chassisModule,
gunModule: gunModule,
gamePadModule: gamePadModule,
controllerModule: controllerModule,
}, nil
}
func (c *Client) changeStateIfNonNil(m module.Module, waitTime time.Duration,
start bool) error {
var err error
if m != nil && !reflect.ValueOf(m).IsNil() {
if start {
err = m.Start()
} else {
err = m.Stop()
}
} else {
return nil
}
if err != nil {
return err
}
if start && !m.WaitForConnection(waitTime) {
return fmt.Errorf("%s connection not established", m)
}
return nil
}